N.N.
N.N.

Reputation: 8502

Integrate Cppcheck with Emacs

Is it possible to integrate Cppcheck with Emacs in a more sophisticated way than simply calling the shell command on the current buffer? I would like Emacs to be able to parse Cppcheck's messages and treat them as messages from a compiler (similar to how compile works), such as using C-x ` to visit the targets of Cppcheck's messages.

Here is some example output Cppcheck:

$ cppcheck --enable=all test.cpp 
Checking test.cpp...
[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 5
[test.cpp:38]: (style) The scope of the variable 'i' can be reduced
[test.cpp:38]: (style) Variable 'i' is assigned a value that is never used
[test.cpp:23]: (error) Possible null pointer dereference: p
[test.cpp:33]: (error) Possible null pointer dereference: p
Checking usage of global functions..
[test.cpp:36]: (style) The function 'f' is never used
[test.cpp:1]: (style) The function 'f1' is never used
[test.cpp:9]: (style) The function 'f2' is never used
[test.cpp:26]: (style) The function 'f3' is never used

Upvotes: 3

Views: 2781

Answers (5)

michaelJohn
michaelJohn

Reputation: 719

The quickest way is probably to use flymake and add an extra target to your makefile.

This setup is working for me:

check-syntax:  cppcheck
     g++ -o nul -S ${CHK_SOURCES} $(CFLAGS)

cppcheck:
    cppcheck --enable=all --quiet --template={file}:{line}:{severity}:{message} ${CHK_SOURCES} 

Upvotes: 0

Harry
Harry

Reputation: 31

Hmmm... I think this is really simple actually. Just use 'compile', as in M-x compile, and type as the compile command:

cppcheck --template='{file}:{line}:{severity}:{message}' --quiet <filename>

Where the filename is the name of the file on which you wish to run cppcheck. That puts the output of cppcheck into a form that the compilation buffer can parse and gives you the full functionality you get with, for instance, a g++ compile. The --quiet is probably not necessary but since all I care about are the errors that's what I use.

Upvotes: 3

Daniel Marjam&#228;ki
Daniel Marjam&#228;ki

Reputation: 3037

based on the answers, flymake sounds good.

you can write a cppcheck parser but it sounds easier to use cppcheck --template option.

cppcheck --template="{file}:{line}:{severity}:{message}" foo.cpp

might match flymake pattern. It is just an idea.

Upvotes: 1

Cheeso
Cheeso

Reputation: 192417

Is it possible to integrate Cppcheck with Emacs in a more sophisticated way

Yes, it is possible. Use flymake. I'm surprised no one has done this yet for cppcheck.

No problem, you can do it yourself. Add cppcheck as a new flavor of flymake tool. To do that, follow an existing example. It's not too complicated to do, but coming in cold, it's hard to know what to do. (I've never seen a guide document that describes how to integrate new tools into flymake). Following an existing working example solves that.

Here's a 7k file that adds PHP CodeSniffer as a flymake tool. http://www.emacswiki.org/emacs/flyphpcs.el

Most of that 7k is comments...

You should be able to modify that to add any arbitrary tool to flymake. The key steps are:

  1. Associate your target file extension (example: .cpp) with a pair of functions (init and cleanup) that do your flymake stuff.

  2. define a function to generate the command line, each time flymake scans your buffer. Ths fn gets the name of the file backing the buffer, and produces a command line. In your case it would run cppcheck with all the appropriate command line parameters. The fn that generates the command line is called by the init fn referenced in #1.

  3. define a regex that matches error messages generated by your tool, and add that regex to the flymake list. This allows flymake to extract error messages from the output of the tool you run. By parsing the output, flymake can get the line numbers and highlight broken lines in your buffer.

Upvotes: 2

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72735

You can use flymake which ships with Emacs.

The basic idea is to write a flymake command that runs cppcheck and then massages the output into a format the flymake can use. If you then enable flymake in your c++ mode buffers, you'll get on the fly error highlighting.

This is an example of flymake working with my Python buffers using pyflakes.

flymake in general expects output in this form filename:line_number: class: message

Where filename is the name of the file, number is the line number, class is a string like error or warning indicating the type of message and message is a string indicating the actual error. cppcheck output looks close to this. You should probably write a little wrapper to convert the output of cppcheck to this format and then add a hook to turn on flymake for c++ mode.

With C, adding something like gcc -Wall -o nul -S ${CHK_SOURCES} to your Makefile with under a target check-syntax and running flymake-mode does what's necessary. I think it'll be similar for c++ mode.

For the gory details, you should check out http://flymake.sourceforge.net/

Upvotes: 2

Related Questions