Skip to content

Commit 3e8b176

Browse files
authored
Merge pull request #2188 from guwirth/optimize-regex
optimize regular expressions
2 parents 9bed31d + 6fd6ef7 commit 3e8b176

File tree

10 files changed

+39
-33
lines changed

10 files changed

+39
-33
lines changed

cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangtidy/ClangTidyParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ClangTidyParser {
3333

3434
private static final Logger LOG = Loggers.get(ClangTidyParser.class);
3535

36-
private static final String REGEX = "(.+|[a-zA-Z]:\\\\.+):([0-9]+):([0-9]+): ([^:]+): (.+)";
36+
private static final String REGEX = "((?>[a-zA-Z]:\\\\)??[^:]++):(\\d{1,5}):(\\d{1,5}): ([^:]++): (.+)";
3737
private static final Pattern PATTERN = Pattern.compile(REGEX);
3838

3939
private final CxxClangTidySensor sensor;
@@ -44,7 +44,7 @@ public ClangTidyParser(CxxClangTidySensor sensor) {
4444
}
4545

4646
public void parse(File report, String defaultEncoding) throws IOException {
47-
try ( var scanner = new TextScanner(report, defaultEncoding)) {
47+
try (var scanner = new TextScanner(report, defaultEncoding)) {
4848
LOG.debug("Encoding='{}'", scanner.encoding());
4949

5050
CxxReportIssue currentIssue = null;

cxx-sensors/src/main/java/org/sonar/cxx/sensors/compiler/gcc/CxxCompilerGccSensor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class CxxCompilerGccSensor extends CxxCompilerSensor {
4242
*/
4343
public static final String DEFAULT_ID = "default";
4444
public static final String DEFAULT_REGEX_DEF
45-
= "(?<file>.*):(?<line>[0-9]+):[0-9]+:\\x20warning:\\x20"
45+
= "(?<file>[^:]*+):(?<line>\\d{1,5}):\\d{1,5}:\\x20warning:\\x20"
4646
+ "(?<message>.*?)(\\x20\\[(?<id>.*)\\])?\\s*$";
4747

4848
public static List<PropertyDefinition> properties() {

cxx-sensors/src/main/java/org/sonar/cxx/sensors/compiler/vc/CxxCompilerVcSensor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public class CxxCompilerVcSensor extends CxxCompilerSensor {
3636
public static final String REPORT_ENCODING_DEF = "sonar.cxx.vc.encoding";
3737
public static final String DEFAULT_ENCODING_DEF = StandardCharsets.UTF_8.name();
3838
public static final String DEFAULT_REGEX_DEF
39-
= "(.*>)?(?<file>.*)\\((?<line>\\d+)\\)\\x20:\\x20warning\\x20"
40-
+ "(?<id>C\\d+):(?<message>.*)";
39+
= "(?>[^>]*+>)?(?<file>(?>[^\\\\]{1,260}\\\\)*[^\\\\]{1,260})"
40+
+ "\\((?<line>\\d{1,5})\\)\\x20?:\\x20warning\\x20(?<id>C\\d{4,5}):"
41+
+ "\\x20?(?<message>.*)";
4142

4243
public static List<PropertyDefinition> properties() {
4344
var category = "CXX External Analyzers";

cxx-sensors/src/main/java/org/sonar/cxx/sensors/drmemory/DrMemoryParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
public final class DrMemoryParser {
3636

3737
private static final Logger LOG = Loggers.get(DrMemoryParser.class);
38-
private static final Pattern RX_MESSAGE_FINDER = Pattern.compile("^Error #\\d+:(.*)");
39-
private static final Pattern RX_FILE_FINDER = Pattern.compile("^.*\\[(.*):(\\d+)\\]$");
38+
private static final Pattern RX_MESSAGE_FINDER = Pattern.compile("^Error #\\d{1,6}:(.*)");
39+
private static final Pattern RX_FILE_FINDER = Pattern.compile(
40+
"\\s*+#[^\\[]++\\[((?>[a-zA-Z]:\\\\)??[^:]++):(\\d{1,5})\\]");
4041
private static final int TOP_COUNT = 4;
4142

4243
private DrMemoryParser() {
@@ -90,7 +91,7 @@ public static List<DrMemoryError> parse(File file, String encoding) {
9091
public static List<String> getElements(File file, String encoding) {
9192

9293
var list = new ArrayList<String>();
93-
try ( var br = new BufferedReader(
94+
try (var br = new BufferedReader(
9495
new InputStreamReader(java.nio.file.Files.newInputStream(file.toPath()), encoding))) {
9596
var sb = new StringBuilder(4096);
9697
String line;

cxx-sensors/src/main/java/org/sonar/cxx/sensors/pclint/CxxPCLintSensor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public class CxxPCLintSensor extends CxxIssuesReportSensor {
6060

6161
private static final String PREFIX_DURING_SPECIFIC_WALK_MSG = "during specific walk";
6262

63-
private static final Pattern SUPPLEMENTAL_MSG_PATTERN
64-
= Pattern.compile(PREFIX_DURING_SPECIFIC_WALK_MSG + "\\s+(.+):(\\d+):(\\d+)\\s+.+");
63+
private static final Pattern SUPPLEMENTAL_MSG_PATTERN = Pattern.compile(
64+
PREFIX_DURING_SPECIFIC_WALK_MSG + "\\s+([^:]++):(\\d{1,5}):(\\d{1,5}).+");
6565

6666
public static List<PropertyDefinition> properties() {
6767
return Collections.unmodifiableList(Arrays.asList(

cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/dotnet/VisualStudioTestResultsFileParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static class Parser {
4848
private final File file;
4949
private final UnitTestResults unitTestResults;
5050
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
51-
private final Pattern millisecondsPattern = Pattern.compile("(\\.([0-9]{0,3}))[0-9]*+");
51+
private final Pattern millisecondsPattern = Pattern.compile("(\\.(\\d{0,3}))\\d*+");
5252

5353
private boolean foundCounters;
5454

@@ -62,7 +62,7 @@ private static void checkRootTag(XmlParserHelper xmlParserHelper) {
6262
}
6363

6464
public void parse() {
65-
try ( var xmlParserHelper = new XmlParserHelper(file)) {
65+
try (var xmlParserHelper = new XmlParserHelper(file)) {
6666
checkRootTag(xmlParserHelper);
6767
dispatchTags(xmlParserHelper);
6868
if (!foundCounters) {

cxx-sensors/src/test/java/org/sonar/cxx/sensors/compiler/vc/CxxCompilerVcSensorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void shouldReportBCorrectVcViolations() {
7777
settings.setProperty(CxxCompilerVcSensor.REPORT_PATH_KEY, "compiler-reports/VC-report.vclog");
7878
settings.setProperty(CxxCompilerVcSensor.REPORT_ENCODING_DEF, StandardCharsets.UTF_8.name());
7979
settings.setProperty(CxxCompilerVcSensor.REPORT_REGEX_DEF,
80-
".*>(?<file>.*)\\((?<line>\\d+)\\):\\x20warning\\x20(?<id>C\\d+):(?<message>.*)");
80+
"[^>]*+>(?<file>.*)\\((?<line>\\d{1,5})\\):\\x20warning\\x20(?<id>C\\d{4,5}):(?<message>.*)");
8181
context.setSettings(settings);
8282

8383
context.fileSystem().add(TestInputFileBuilder.create("ProjectKey", "Server/source/zip/zipmanager.cpp")

cxx-squid/src/main/java/org/sonar/cxx/config/MsBuild.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
public class MsBuild {
4040

41-
/**
41+
/**
4242
* the following settings are in use by the feature to read configuration settings from the VC compiler report
4343
*/
4444
public static final String REPORT_PATH_KEY = "sonar.cxx.msbuild.reportPaths";
@@ -54,18 +54,22 @@ public class MsBuild {
5454
private static final String CPPVERSION = "__cplusplus=199711L";
5555

5656
private static final Pattern[] INCLUDE_PATTERNS = {Pattern.compile("/I\"(.*?)\""),
57-
Pattern.compile("/I([^\\s\"]+) ")};
58-
private static final Pattern[] DEFINE_PATTERNS = {Pattern.compile("[/-]D\\s([^\\s]+)"),
59-
Pattern.compile("[/-]D([^\\s]+)")};
60-
private static final Pattern PATH_TO_CL_PATTERN = Pattern.compile("^.*\\\\bin\\\\.*CL.exe\\x20.*$");
57+
Pattern.compile("/I([^\\s\"]++) ")};
58+
private static final Pattern[] DEFINE_PATTERNS = {Pattern.compile("[/-]D\\s([^\\s]++)"),
59+
Pattern.compile("[/-]D([^\\s]++)")};
60+
private static final Pattern PATH_TO_CL_PATTERN = Pattern.compile(
61+
"^(?>[^\\\\]{0,260}\\\\)+bin\\\\(?>[^\\\\]{1,260}\\\\)*CL.exe\\x20.*$");
6162
private static final Pattern PLATFORM_X86_PATTERN = Pattern.compile("Building solution configuration \".*\\|x64\".");
62-
private static final Pattern TOOLSET_V141_PATTERN = Pattern
63-
.compile("^.*VC\\\\Tools\\\\MSVC\\\\14\\.1\\d\\.\\d+\\\\bin\\\\HostX(86|64)\\\\x(86|64)\\\\CL.exe.*$");
64-
private static final Pattern TOOLSET_V142_PATTERN = Pattern
65-
.compile("^.*VC\\\\Tools\\\\MSVC\\\\14\\.2\\d\\.\\d+\\\\bin\\\\HostX(86|64)\\\\x(86|64)\\\\CL.exe.*$");
63+
private static final Pattern TOOLSET_V141_PATTERN = Pattern.compile(
64+
"^(?>[^\\\\]{0,260}\\\\)+VC\\\\Tools\\\\MSVC\\\\14\\.1\\d\\.\\d{1,6}"
65+
+ "\\\\bin\\\\HostX(86|64)\\\\x(86|64)\\\\CL.exe.*$");
66+
private static final Pattern TOOLSET_V142_PATTERN = Pattern.compile(
67+
"^(?>[^\\\\]{0,260}\\\\)+VC\\\\Tools\\\\MSVC\\\\14\\.2\\d\\.\\d{1,6}"
68+
+ "\\\\bin\\\\HostX(86|64)\\\\x(86|64)\\\\CL.exe.*$");
6669

6770
// It seems that the required line in any language has these elements: "ClCompile" and (*.vcxproj)
68-
private static final Pattern PATH_TO_VCXPROJ = Pattern.compile("^(?:\\S+)\\s(?:\"ClCompile\").*\"(.+vcxproj)\".*$");
71+
private static final Pattern PATH_TO_VCXPROJ = Pattern.compile(
72+
"^\\S+\\s\\\"ClCompile\\\".+\\\"((?>[^\\\\]{1,260}\\\\)*[^\\\\]{1,260}\\.vcxproj)\\\".*$");
6973

7074
private String platformToolset = "V120";
7175
private String platform = "Win32";
@@ -75,7 +79,7 @@ public class MsBuild {
7579
/**
7680
* CxxVCppBuildLogParser (ctor)
7781
*
78-
* @param db
82+
* @param squidConfig
7983
*/
8084
public MsBuild(CxxSquidConfiguration squidConfig) {
8185
this.squidConfig = squidConfig;
@@ -120,7 +124,7 @@ public void parse(String line, String projectPath, String compilationFile) {
120124
* @param encodingName
121125
*/
122126
public void parse(File buildLog, String baseDir, String encodingName) {
123-
LOG.info("Processing MsBuild log '{}', Encoding= '{}'", buildLog.getName(), encodingName);
127+
LOG.info("Processing MsBuild log '{}', Encoding= '{}'", buildLog.getName(), encodingName);
124128

125129
var detectedPlatform = false;
126130
try (var br = new BufferedReader(new InputStreamReader(java.nio.file.Files.newInputStream(buildLog.toPath()),
@@ -133,7 +137,7 @@ public void parse(File buildLog, String baseDir, String encodingName) {
133137
if (line.trim().startsWith("INCLUDE=")) { // handle environment includes
134138
String[] includes = line.split("=")[1].split(";");
135139
for (var include : includes) {
136-
squidConfig.add(CxxSquidConfiguration.GLOBAL, CxxSquidConfiguration.INCLUDE_DIRECTORIES,include);
140+
squidConfig.add(CxxSquidConfiguration.GLOBAL, CxxSquidConfiguration.INCLUDE_DIRECTORIES, include);
137141
}
138142
}
139143

@@ -296,7 +300,7 @@ private void parseInclude(String element, String project, String fileElement) {
296300
includeRoot = new File(project, includeRoot.getPath());
297301
}
298302
}
299-
squidConfig.add(fileElement, CxxSquidConfiguration.INCLUDE_DIRECTORIES,includeRoot.getCanonicalPath());
303+
squidConfig.add(fileElement, CxxSquidConfiguration.INCLUDE_DIRECTORIES, includeRoot.getCanonicalPath());
300304
} catch (IOException e) {
301305
LOG.error("Cannot parse include path using element '{}' : '{}'", element, e.getMessage());
302306
}

cxx-squid/src/main/java/org/sonar/cxx/parser/CxxLexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public final class CxxLexer {
4747
private static final String HEX_PREFIX = "0[xX]";
4848
private static final String BIN_PREFIX = "0[bB]";
4949
private static final String EXPONENT = "[Ee][+-]?+[0-9_]([']?+[0-9_]++)*+";
50-
private static final String BINARY_EXPONENT = "[pP][+-]?+[0-9]([']?+[0-9]++)*+"; // since C++17
50+
private static final String BINARY_EXPONENT = "[pP][+-]?+\\d([']?+\\d++)*+"; // since C++17
5151
//private static final String INTEGER_SUFFIX = "(((U|u)(i64|LL|ll|L|l)?)|((i64|LL|ll|L|l)(u|U)?))";
5252
//private static final String FLOAT_SUFFIX = "(f|l|F|L)";
5353
// ud-suffix: identifier (including INTEGER_SUFFIX, FLOAT_SUFFIX)
54-
private static final String UD_SUFFIX = "[_a-zA-Z][_a-zA-Z0-9]*+";
55-
private static final String DECDIGIT_SEQUENCE = "[0-9]([']?+[0-9]++)*+";
54+
private static final String UD_SUFFIX = "[_a-zA-Z]\\w*+";
55+
private static final String DECDIGIT_SEQUENCE = "\\d([']?+\\d++)*+";
5656
private static final String HEXDIGIT_SEQUENCE = "[0-9a-fA-F]([']?+[0-9a-fA-F]++)*+";
5757
private static final String BINDIGIT_SEQUENCE = "[01]([']?+[01]++)*+";
5858
private static final String POINT = "\\.";

cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppLexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public final class CppLexer {
4343
private static final String HEX_PREFIX = "0[xX]";
4444
private static final String BIN_PREFIX = "0[bB]";
4545
private static final String EXPONENT = "[eE][+-]?+[0-9_]([']?+[0-9_]++)*+";
46-
private static final String BINARY_EXPONENT = "[pP][+-]?+[0-9]([']?+[0-9]++)*+"; // since C++17
46+
private static final String BINARY_EXPONENT = "[pP][+-]?+\\d([']?+\\d++)*+"; // since C++17
4747
//private static final String INTEGER_SUFFIX = "(((U|u)(LL|ll|L|l)?)|((LL|ll|L|l)(u|U)?))";
4848
//private static final String FLOAT_SUFFIX = "(f|l|F|L)";
4949
// ud-suffix: identifier (including INTEGER_SUFFIX, FLOAT_SUFFIX)
50-
private static final String UD_SUFFIX = "[_a-zA-Z][_a-zA-Z0-9]*+";
51-
private static final String DECDIGIT_SEQUENCE = "[0-9]([']?+[0-9]++)*+";
50+
private static final String UD_SUFFIX = "[_a-zA-Z]\\w*+";
51+
private static final String DECDIGIT_SEQUENCE = "\\d([']?+\\d++)*+";
5252
private static final String HEXDIGIT_SEQUENCE = "[0-9a-fA-F]([']?+[0-9a-fA-F]++)*+";
5353
private static final String BINDIGIT_SEQUENCE = "[01]([']?+[01]++)*+";
5454
private static final String POINT = "\\.";

0 commit comments

Comments
 (0)