Skip to content

CXX Custom Regex Rules

guwirth edited this page Mar 10, 2021 · 14 revisions

The cxx plugin has two very universal Regex rules to create custom rules:

  • All Java regular expressions are supported, for a description see here.
  • Optional for all rules an Ant-style matching pattern (matchFilePattern) for the path can be defined. In case of an empty matchFilePattern all files are used.

Following matchFilePattern rules are applied:

? matches single character
* matches zero or more characters
** matches zero or more 'directories'
use always '/' as a directory separator
there must always be a root directory

Some examples of path patterns:

/**/*.cpp - matches all .cpp files in all directories (you have to define the root directory '/'), e.g. org/Foo.cpp or org/foo/Bar.cpp or org/foo/bar/Baz.cpp
/**/test/**/Foo.cpp - matches all 'Foo.cpp' files in directories with one 'test' directory in the path, e.g. org/test/Foo.cpp or org/bar/test/bar/Foo.cpp
org/T?st.cpp - matches org/Test.cpp and also org/Tost.cpp
org/*.cpp - matches all .cpp files in the org directory, e.g. org/Foo.cpp or org/Bar.cpp
org/** - matches all files underneath the org directory, e.g. org/Foo.cpp or org/foo/bar.jsp
org/**/Test.cpp - matches all Test.cpp files underneath the org directory, e.g. org/Test.cpp or org/foo/Test.cpp or org/foo/bar/Test.cpp
org/**/*.cpp - matches all .cpp files underneath the org directory, e.g. org/Foo.cpp or org/foo/Bar.cpp or org/foo/bar/Baz.cpp

File RegEx rule

This rule can be used to create rules which will be triggered when a file matches a given regular expression. For each matching file a violation will be created.

Example: Create a file warning if a tab character is in the file.

matchFilePattern = "";
regularExpression = "\t";
message = "Found a tabulator in the file. Indent with blanks and not with tabulators!";

Line RegEx rule

This rule can be used to create rules which will be triggered when a line matches a given regular expression. For each matching line a violation will be created.

Example: Create a line warning if '#include "stdafx.h"' is in a header file

matchFilePattern = "/**/*.h"; // all files with .h file extension
regularExpression = "#include\\s+\"stdafx\\.h\"";
message = "Found '#include \"stdafx.h\"' in a header file!";
#include "stdafx.h" // warning "Found '#include "stdafx.h"' in a header file!"

class MyClass {
};
Clone this wiki locally