Skip to content

CXX Custom Regex Rules

Günter Wirth edited this page Apr 24, 2021 · 14 revisions

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

  • All Java regular expressions are supported.
  • 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.

Regular Expressions

All regex rules are using Java regular expressions. For a description see here.

There is a set of special characters also known as metacharacters present in a regular expression. When you want to allow the characters as is instead of interpreting them with their special meanings, you need to escape them. By escaping these characters, you force them to be treated as ordinary characters when matching a string with a given regular expression.

The metacharacters that we usually need to escape in this manner are:

<([{\^-=$!|]})?*+.>

The backslash character is an escape character in Java String literals as well. Therefore, you need to double the backslash character when using it to precede any character (including the \ character itself).

Use an online regex tool like https://regex101.com/ to create and test your regex (flavor=Java). Selecting Tools>Code Generator will then give you a corresponding Java string.

  1. Create a Java regular expression.
  2. Generate a Java String from the regular expression.
  3. Use this Java String in the rule.

Ant-style pattern

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