Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate Cppcheck with Emacs

Tags:

emacs

cppcheck

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
like image 680
N.N. Avatar asked Nov 23 '25 13:11

N.N.


1 Answers

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.

like image 188
Harry Avatar answered Nov 25 '25 09:11

Harry