Checkstyle suppressions: what am I doing wrong? - regex

I'm using checkstyle on the commandline. Not in Maven or ANT.
I've defined a suppressions.xml file and referenced it from my -c CheckstyleConfig.xml.
Checkstyle runs (no parsing errors), but it isn't suppressing anything.
I'm trying to make Checkstyle ignore .class files. Here is the content of my suppression.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress checks="." files="(.*\.class)"/>
</suppressions>
Any ideas on what I'm doing wrong? Checkstyle is still processing .class files.
Thanks.

Checkstyle does not work on class files, it cannot parse them. Checkstyle works on Java source code, which are files that end with ".java".
So, the problem is your regular expression to suppress errors. The suppression should be:
<suppress checks="." files="(.*\.java)"/>

Checkstyle works on whatever you feed it. A default eclipse-cs installation will only feed it Java files and property files, but since you're running from the command line, you'll have to exclude the .class files from checking or suppress the warnings.
It is better to exclude the .class files from checking (so that Checkstyle never sees them), because it will save you time.
But if you want to suppress the warnings, you can do that, too. The files regex in a suppressions.xml is greedy, so .* already covers the whole file name, and the pattern in your example never matches. Try this syntax (suppressing all .class files):
<suppressions>
<suppress checks=".*" files="\.class$" />
</suppressions>

Related

cppcheck -i switch (ignore) is being ignored

I have a folder in which there are several .c and .h files, plus a message.xml file.
I don't want to scan the XML.
If I run cppcheck --enable=all *.* it finds and balks at the contents of the XML. Fine, I don't care about the XML, but still. I hate seeing ignorable errors/warnings.
Of course, cppcheck *.c --enable=all or cppcheck *.c *.h --enable=all ignores the XML.
But curiosity got to me and I found out about the "ignore" switch but its use is unclear. If I try
cppcheck *.* --enable=all -imessage.xml or various perturbations of that line, it still finds the XML and complains.
So what is the command syntax to ignore a specific file and to ignore, say, all *.xml or *.xls files?
I am a Cppcheck developer.
I guess it's a bug in Cppcheck. As far as I see in the help output you should be able to use -imessage.xml.
But I would suggest cppcheck . --enable=all. You don't normally compile headers directly and therefore you shouldn't analyze them directly neither. You get wrong handling of the include guard and you probably get false positives about unused struct members.. well there can be some wrong behavior and don't blame cppcheck.

How to use the exclude_files regex in cpplint?

I am using cpplint to check my sourcode agains the google style guide.
Cpplint's help says:
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
files. CPPLINT.cfg file can contain a number of key=value pairs.
Currently the following options are supported:
"exclude_files" allows to specify a regular expression to be matched against
a file name. If the expression matches, the file is skipped and not run
through liner.
Example file:
filter=-build/include_order,+build/include_alpha
exclude_files=.*\.cc
The above example disables build/include_order warning and enables
build/include_alpha as well as excludes all .cc from being
processed by linter, in the current directory (where the .cfg
file is located) and all sub-directories.
How I use cpplint:
I use cpplint by this command to check all files in my source folder:
cpplint src/*.c
Well there is one special file foo.cc which must not be checked. So I tried to create a CPPLIN.cfg to use the exclude_files property. My file looks like this:
set noparent
filter=-build/include_dir
exclude_files=foo.cc
Nevertheless foo.cc is still checked.
What I have already tried to do:
I tried exclude_files=/.*\.cc/. This should exclude all files ending with *.cc. Nevertheless all files are still checked.
I tried to remove my filter from the file. This caused more errors than before. So I am now sure that my CPPLINT.cfg file is found by cpplint.
Question:
How to use the exclude_files regex in cpplint correctly?
Turns out apparently that the doc is wrong: exclude_files only excludes files in the same directory as CPPLINT.cfg, not in subdirectories. See https://github.com/google/styleguide/issues/220
So the solution would be to create src/CPPLINT.cfg and put exclude_files=.*\.cc in it.

How to ignore header files in gcov output?

I am using gcov and gcovr to generate my code test coverage (the tests are done with google test++).
So, I compile with the -ftest-coverage -fprofile-arcs options, and then I run gcovr (which itself runs gcov).
However, on my output, I have the cpp files, with a cover of 100%, but also the .h files, even if they do not have executable code, and hence they have a 0% coverage output.
This 0% does not mean anything, and hence, I would like to remove the .h files from the coverage output. I can't find anything about that...
I already try to add : -e "*.h" to the govr options, to exclude files with .h extension, but it doesn't work (it actually excludes everything...).
Does anybody have an idea ??
Thank you !!
I was also struggling with this, now found the right solution. Here an example and way of working:
When you run gcov, use option -o coverage.xml, then open the file. Find the filename you want to exclude and copy the filename.
Open in your browser following link and copy the entire filename to the part which says: TEST STRING
https://regex101.com/
Now, where it says: REGULAR EXPRESSION, create a regular expression which makes the entire filename BLUE. Make sure this expression does not apply to other files which are needed to show in your coverage report.
Here some basic rules: Usually, for "some characters" you can use ".*" what has nothing to do with files of type *.cpp, where you want to see the cpp files! So if you want to exclude anything like "<some characters>include<more characters>", then you can use ".*include.*". This will also exclude some filename like include.cpp
Because the . has a meaning in regular expressions, use \. So if you want to exclude "<something>.h" files, use ".*\.h"
Example what works for me: (exclude include files and the test framework and linux include directory and exclude jenkins files)
Also I want to exclude any cpp file which has the word test, what I can do with ".*test[_-[A-Z|a-z]*\.cpp"
Real life example:
gcovr --xml -e ".*unit_test\.cpp" -e ".*\.h" -e ".*usr/include/.*" -e ".*jenkins.*" -e ".*test[_-|A-Z|a-z]*\.cpp" -o coverage.xml
have fun!
Just use -e to exclude you files..
Your command will look like
gcovr -r . -e "include.*" --html --html-details -o $result_dir/result.html
It will exclude all include files of your project.

make cppcheck skip the PACKAGE definition

I'm using the GUI version of cppcheck 1.64 for static code analysis on C++-Builde-6 code. For DLL exports and imports, the definition of PACKAGE is necessary:
/// A dialog exported from a BPL (a VCL-specific kind of DLL)
class PACKAGE MySharedDialog {
public:
// lots of methods to-be checked
private:
// lots of methods to-be checked
// lots of members
};
Cppcheck stops when it encounters PACKAGE because it doesn't know what it means:
The code 'class PACKAGE TAppInfoDialog {' is not handled. You can use -I or --include to add handling of this code.
...and this of course means that the entire class isn't checked. If I could make cppcheck simply ignore the PACKAGE "keyword", it would do exactly the right thing, but how to do it? Including its original definition via include path, seems to be not an option: cppcheck then tells me a lot about headers of the VCL framework I cannot change...
The manual does not describe an option to do it, Google doesn't help, SO does not have an answer yet.
In the cppcheck issue tracker, I found the analogous problem #4707 (Microsoft 'abstract' and 'sealed' extension for class) – cppcheck. Here the lead developer suggests to create a file and (pre-?)include it to the cppcheck run, but I'm using the GUI version and there is no option to include a single file. So I tried to add a directors to the include section of my project options (an XML file), then I edited the corresponding line to a file specification, but that's clearly nonsense, because this section contains include paths.
What can I try next?
A solution is to add the definition of PACKAGE (being empty) to the project file:
<?xml version="1.0" encoding="UTF-8"?>
<project version="1">
<defines>
<define name="PACKAGE=" />
</defines>
</project>
This solution I finally found in this small but valuable project file description at the project repo cppcheck/gui/projectfile.txt at master · danmar/cppcheck · GitHub

Can checkstype use regex in "files from package" filter

The code generated by xjc.exe to create classes from an XML Schema has lots of Checkstyle warnings. I can use a Checkstyle "files from package" filter in a project to ignore the source generated by xjc.exe but I have to select each package individually and I use JAXB classes quite a bit.
I use a naming convention for the generated code where the package name contains .JAXB. and ends with .classes
Is there a way to specify a generic filter in Checkstyle so that packages generated by xjc.exe are ignored?
Can I do it globally in the the Checkstyle configuration, so it is applied to all projects in the workspace?
Can it be done in the project's .checkstyle file?
Thanks,
Steve
Yes, you can define a suppressions.xml in your project like this:
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files="[\\/]JAXB[\\/].+?[\\/]classes[\\/][A-Z]" checks="." />
</suppressions>
The [A-Z] at the end of the files regex assumes that your classes start with an uppercase letter. This way, classes only matches as the last package name element (regex explanation).
Then, in your checkstyle configuration, you enable the SuppressionFilter like this:
...snip...
<module name="SuppressionFilter">
<property name="file" value="${workspace_loc}/Project/suppressions.xml"/>
</module>
...snip...