1

I am dealing with the following problem:

Given a number of source code written in C. They are scanned with CppCheck and the output is the CppCheck XML output format. The output per each error found is simply:

error: $filePath, $lineNumber

Question: I would like to find the function name in C that this $lineNumber belongs to.

Is it possible to retrieve this information from CppCheck somehow? Is it possible to implement a Java code to retrieve this information?

Any comment, idea or suggestion is appreciated. Thank you!

userOverflow
  • 33
  • 1
  • 4
  • I think people are going to expect some code before answering. But yes, you can write your own program (in C or Java whatever you like), where you first read the XML to find out which file you are going to read (with your program), then you check the line, so you know where to look and read all lines before that – until you find a line where your function gets declared. – GameDroids Jun 18 '14 at 10:11
  • Looking into the help of that tool's CLI it seems there is no way to get an output of that information which is surely available for the tool. You might want to ask on their [forums or irc-chat](http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page#Forum.2C_chat) – A4L Jun 18 '14 at 10:26
  • I've already a Java code that parse the XML output. Then, I open each file and and start reading each line within the file. Reading backward is a good idea, cite: "until you find a line where your function gets declared" but not sure how to implement it. I don't know the syntax of a C function so I can implement it with regex in Java. Do you have any suggestion? – userOverflow Jun 18 '14 at 10:28
  • 2
    Maybe you could have a look to `ctags`. It is tool specialized in finding function definitions in sources. Using it will save you from the parsing of a C source files that can be "tedious". IMHO the parsing of a `tags` file will be much simpler. – Serge Ballesta Jun 18 '14 at 10:33
  • There is some tools that parse C code. pycparser can help, but you have to pre-process your code. One that I've used to analyse C code is CCCC (metrics retrival). It can give you all functions in a file, location and size (LOC) – Cristiano Araujo Jun 18 '14 at 11:22

0 Answers0