Skip to content

Commit fc91eb1

Browse files
committed
fix #1719
1 parent 588a65f commit fc91eb1

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class CxxClangTidySensor extends CxxIssuesReportSensor {
4444
private static final Logger LOG = Loggers.get(CxxClangTidySensor.class);
4545

4646
private static final String REGEX
47-
= "(.+|[a-zA-Z]:\\\\.+):([0-9]+):([0-9]+): ([^:]+): ([^]]+)( \\[([^]]+)\\])?";
47+
= "(.+|[a-zA-Z]:\\\\.+):([0-9]+):([0-9]+): ([^:]+): (.+)";
4848
private static final Pattern PATTERN = Pattern.compile(REGEX);
4949

5050
/**
@@ -59,10 +59,10 @@ public CxxClangTidySensor(CxxLanguage language) {
5959
@Override
6060
public void describe(SensorDescriptor descriptor) {
6161
descriptor
62-
.name(getLanguage().getName() + " ClangTidySensor")
63-
.onlyOnLanguage(getLanguage().getKey())
64-
.createIssuesForRuleRepository(getRuleRepositoryKey())
65-
.onlyWhenConfiguration(conf -> conf.hasKey(getReportPathKey()));
62+
.name(getLanguage().getName() + " ClangTidySensor")
63+
.onlyOnLanguage(getLanguage().getKey())
64+
.createIssuesForRuleRepository(getRuleRepositoryKey())
65+
.onlyWhenConfiguration(conf -> conf.hasKey(getReportPathKey()));
6666
}
6767

6868
@Override
@@ -82,15 +82,33 @@ protected void processReport(final SensorContext context, File report) {
8282
String nextLine = scanner.nextLine();
8383
final Matcher matcher = PATTERN.matcher(nextLine);
8484
if (matcher.matches()) {
85-
// group: 1 2 3 4 5 7
86-
// <path>:<line>:<column>: <level>: <info> [<ruleId>]
85+
// group: 1 2 3 4 5
86+
// <path>:<line>:<column>: <level>: <txt>
8787
MatchResult m = matcher.toMatchResult();
8888
String path = m.group(1); // relative paths
8989
String line = m.group(2);
9090
//String column = m.group(3);
91-
String level = m.group(4); // error, warning, note, ...
92-
String info = m.group(5);
93-
String ruleId = m.group(7); // optional
91+
String level = m.group(4); // error, warning, note, ...
92+
String txt = m.group(5); // info( [ruleId])?
93+
String info = null;
94+
String ruleId = null;
95+
96+
if (txt.endsWith("]")) { // [ruleId]
97+
for (int i = txt.length() - 2; i >= 0; i--) {
98+
char c = txt.charAt(i);
99+
if (c == '[') {
100+
info = txt.substring(0, i - 1);
101+
ruleId = txt.substring(i + 1, txt.length() - 1);
102+
break;
103+
}
104+
if (!(Character.isLetterOrDigit(c) || c == '-' || c == '.')) {
105+
break;
106+
}
107+
}
108+
}
109+
if (info == null) {
110+
info = txt;
111+
}
94112

95113
if (ruleId != null) {
96114
if (issue != null) {

cxx-sensors/src/test/java/org/sonar/cxx/sensors/clangtidy/CxxClangTidySensorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ public void shouldReportWarnings() {
105105
assertThat(context.allIssues()).hasSize(1);
106106
}
107107

108+
@Test
109+
public void shouldReportNodiscard() {
110+
SensorContextTester context = SensorContextTester.create(fs.baseDir());
111+
112+
settings.setProperty(
113+
language.getPluginProperty(CxxClangTidySensor.REPORT_PATH_KEY),
114+
"clang-tidy-reports/cpd.report-nodiscard.txt"
115+
);
116+
context.setSettings(settings);
117+
118+
context.fileSystem().add(TestInputFileBuilder.create("ProjectKey", "sources/utils/code_chunks.cpp")
119+
.setLanguage("cpp").initMetadata("asd\nasdas\nasda\n").build());
120+
121+
CxxClangTidySensor sensor = new CxxClangTidySensor(language);
122+
sensor.execute(context);
123+
assertThat(context.allIssues()).hasSize(1);
124+
}
125+
108126
@Test
109127
public void shouldReportFlow() {
110128
SensorContextTester context = SensorContextTester.create(fs.baseDir());
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 warning generated.
2+
sources\utils\code_chunks.cpp:2:3: warning: function 'empty' should be marked [[nodiscard]] [modernize-use-nodiscard]
3+
bool empty() const;
4+
^
5+
[[nodiscard]]

0 commit comments

Comments
 (0)