1

I want to make sure that the function body for each function fits on the screen. Therefore I want to to generate a list that contains the LOC for each function (in a .cpp/.h - file, or better in all source code files in a directory). For example the list could be an CSV-file containing

foo.cpp,foobar,42
foo.cpp,foozar,13
goo.cpp,bla,666

if the file foo.cpp contains a function foobar which has a body of 42 lines, etc...

Can you recommend/suggest any tool?

levzettelin
  • 2,600
  • 19
  • 32
  • http://cloc.sourceforge.net/ – timrau Aug 01 '14 at 15:41
  • 1
    @timrau: I looked at cloc but it seems to only count lines on a per file basis. I was not able to determine how to make the mapping to a particular function. – levzettelin Aug 01 '14 at 15:59
  • Do you want an accurate answer, or is a huerestic good enough? – Ira Baxter Aug 03 '14 at 16:14
  • @IraBaxter I would say it depends on the quality of the heuristic. If the heuristic answer would be obtainable fast, it might suffice. – levzettelin Aug 04 '14 at 07:29
  • What is the purpose of making sure all function bodies fit on screen? Screens don't all have the same size, nor do editors (or terminals) use the same fonts. – Damon Aug 04 '14 at 11:26
  • @Damon I do not like functions to be excessively long and I use the screen of my local machine as an approximation to determine if functions are becoming excessively long. I simply want to know which of my functions are the longest, so that I can then work on them. – levzettelin Aug 04 '14 at 12:45

2 Answers2

1

The issue is simple. If you want accurate data no matter what C++ you encounter, you will need a full C++ parser with preprocessor capability. That's very hard to build due to the complex nature of C++ (now C++11 is pretty standard and C++14 is not far behind). Pretty much your choices are limited to:

  • Edison Design Group C++ front end
  • Clang
  • GCC
  • Our DMS Software Reengineering Toolkit with its C++ front end
  • Elsa (if it still maintained)

These are big, complex engines and take effort to configure for your task (esp. GCC, which wants to be a compiler no matter what you want it to be). An additional complication that may or may not matter to you: Clang, GCC and Elsa don't handle the MS dialects, if I understand them correctly.

If you don't care if you get the right answer all the time, you could build a very simple scanner to look for apparant function heads, counting { ... } and ( ... ) to make sure you know where the function body terminates. You'll probably have to recognize namespace and class constructs, to know to look inside them for function declarations. This seems like the easiset solution, thus fastest in time and least effort.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

You can write a plugin for clang. It is well suited for these kinds of extensions.

Daniel
  • 30,896
  • 18
  • 85
  • 139