2

Suppose I have some software project I'm working on, and I want to compute some statistics about it:

  • Number of lines (total & per file)
  • Comment, spacing and other lines
  • Numbers of functions, classes, methods
  • Average length of a file, a function, a class, a method
  • Cyclomatic complexity
  • Dependency DAG depths for files, types etc.

I'll settle for only some of these, of course - the more the merrier. I do not guarantee the repository has any form of source control (although personally I'm working with mercurial right now); and it's not a clone of an online repository (so nothing like gitstats).

Requirements:

  • Libre
  • Gratis
  • Works on Linux (AMD x86_64 CPUs)
  • Supports at the very least C, C++98, C++11
  • Support for multiple output formats
  • Can produce output as console "ASCII-art" charts (a-la, say, hg diff --stat)

Desirable feature:

  • Small standalone utility
  • Not too many dependencies on other programs and libraries
  • Fast

2 Answers 2

1

CLOC - Count Lines of Code - does some (not all) of those things, meeting all the requirements. It's also fast, small, and depends only on perl and a couple of perl libs.

Sample outout:

http://cloc.sourceforge.net v 1.60  T=0.46 s (354.6 files/s, 64728.0 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C/C++ Header                    108           2761           2452          10226
C++                              47           1626            772           9703
make                              1            476            193            931
Bourne Again Shell                3             50             25            346
CMake                             2             48             68            225
XML                               1              0              0             11
YAML                              1              1              6              8
Bourne Shell                      1              1              2              5
--------------------------------------------------------------------------------
SUM:                            164           4963           3518          21455
--------------------------------------------------------------------------------
1

There is a cross platform python metrics library that gives you metrics either on a single file, or can recurse a directory tree. It is available from the original project or my fork adds recursive option on github.

Running on an old project that I have to hand with the recurse option:

Files                       Language        SLOC Comment McCabe
----- ------------------------------ ----------- ------- ------
 102                            XML       10601    2415      0
   7                            VGL        4837      14     49
 588                              C      441797  135164  11715   
 302                              D        7750       0      0    
  27                           XSLT         691     313      0    
  13                         Python         881     203    141
   4                        XML+PHP       21489       0      0
  38                       Makefile        1747    3948     18
   7                      Batchfile          45       8      7
   2                           HTML           9      36      0    
  17                      Text only           0       0      0
   1                         POVRay           8      19      0
----- ------------------------------ ----------- ------- ------
1108                          Total      489855  142120  11930
  • Libre Yes
  • Gratis Yes
  • Works on Linux (AMD x86_64 CPUs) Yes it's python
  • Supports at the very least C, C++98, C++11 Yes
  • Support for multiple output formats Yes Can output as text, xml or csv
  • Can produce output as console "ASCII-art" charts (a-la, say, hg diff --stat) Yes See above
  • Small standalone utility Small, (tiny), python library that adds command line utility
  • Not too many dependencies on other programs and libraries Depends on python, its standard libraries & pygments
  • Fast Reasonably so
2
  • How does it compare with CLOC w.r.t. the kinds of statistics it gathers and presents? Beyond overall line breakdown into code/comments/space ? Also, what is "McCabe"?
    – einpoklum
    Commented Oct 19, 2016 at 6:45
  • 1
    McCabe is the cyclomatic complexity. Commented Aug 20, 2020 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.