Skip to content

CXX Custom Regex Rules

Günter Wirth edited this page May 1, 2021 · 12 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:

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

Use an online regex tool like https://regex101.com/ to create and test your regex (flavor=Java).

Hint: The backslash character \ does not have to be doubled when entering a regular expression in the SonarQube UI!

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
regularExpression: ^\s+#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 {
};

Example: Create a line warning if there is a TODO comment (case insensitive)

matchFilePattern: 
regularExpression: //\s*(?i)TODO$
message: Found TODO comment!
class MyClass { // TODO => message: Found TODO comment!
};
Clone this wiki locally