Our C++ source and header files need to have a copyright header, which we want to enforce with SonarQube. Rule cpp:S1451 “Track lack of copyright and license headers” appears to be perfect for this as you can specify a regular expression that the header should match. However I can’t figure out how to actually write a regular expression that will match our files.
Our copyright header looks like this:
////////////////////////////////////////////////////////////////////////////
// Copyright Aperture Laboratories 2024
// PortalUtils.cpp
// Contains utility functions to work with portals.
////////////////////////////////////////////////////////////////////////////
The year is variable, and all lines except “Copyright” line are optional. My initial regular expression was:
///+\r?\n// Copyright Aperture Laboratories .+\r?\n(// .*\r?\n)*///+\r?\n
.
This expression matches the header in Python or ripgrep, however SonarQube claims to use a different dialect it calls “POSIX regular expressions” and consequently the expression doesn’t match.
As the man page makes no reference to escapes for newlines (\r?\n
) I tried literal newlines and a few other alterations to make the regex to conform to POSIX regular expressions.
We are using SonarQube Developer Edition Version 10.3 with the commerical C++ scanner.
Is it possible to match multi-line copyright headers in SonarQube?
How should our regular expression look like?