Do you know a simple script to count NLOCs (netto lines of code). The script should count lines of C Code. It should not count empty lines or lines with just braces. But it doesn't need to be overly exact either.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
If the comments can still be in, the standard unix tool are sufficent:
Ohloh offers the free Ohcount which counts lines of code and comments.
Locmetrics works well.
Here's a simple Perl script eLOC.pl:
Looking NLOC on the Net, I found mostly "Non-commented lines of code".
You don't specify if comments must be skipped...
So if I stick to your current message, the following one-liner in Perl should do the job:
I can extend it to handle line comments:
or perhaps
Handling block comments is slightly more tricky (I am not a Perl expert!).
[EDIT] Got it... First part can be probably improved (shorter). Was fun to experiment with.
PS.: I use double quotes because I tested on Windows...
I have a program called
scc
that strips C comments (and C++ comments, though with C99 they're the same). Apply that plus a filter to remove blank lines and, if so desired, lines containing just open and close braces, to generate the line counts. I've used that on internal projects - not needed to discount open/close braces. Those scripts were more complex, comparing the source code for two different versions of a substantial project stored in ClearCase. They also did statistics on files added and removed, and on lines added and removed from common files, etc.Not counting braces makes quite a difference:
So, 144 lines under your rules; 208 counting open and close brace lines; 271 counting everything.
Lemme know if you want the code for
scc
(send email to first dot last at gmail dot com). It's 13 KB of gzipped tar file including man page, torture test, and some library files.@litb commented that '
cpp -fpreprocessed -P file
' handles stripping of comments. It mostly does. However, when I run it on the stress test for SCC, it complains when (in my opinion) it should not:When the CPP from GCC 4.3.2 processes this, it complains (warns):
Section 5.1.1.2 Translation Phases of the C99 standard says:
Consequently, in my view, CPP is mishandling phase two in the example text. Or, at least, the warning is not what I want - the construct is valid C and it is not self-evident that the warning is warranted.
Granted, it is an edge case, and extra warnings are permitted. But it would annoy the living daylights out of me. If I didn't have my own, possibly better tool for the job, then using '
cpp -fpreprocessed -P
' would do - it is an extreme edge case that I'm complaining about (and, it might be legitimate to argue that it is more likely that there is a problem than not -- though a better heuristic would observe that the line was spliced and the result was a legitimate single character constant and therefore the complaint should be suppressed; if the result was not a legitimate single character constant, then the complaint should be produced. (On my test case - admittedly a torture test - CPP yields 13 problems, mostly related to the one I'm complaining about, where SCC correctly yields 2.)(I observe that the '
-P
' manages to suppress a '#line
' directive in the output that appears when the option is omitted.)