Skip to content

Commit 62cfe04

Browse files
committed
show some Cppcheck messages on file-level
1 parent db4e737 commit 62cfe04

File tree

3 files changed

+67
-39
lines changed

3 files changed

+67
-39
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Deployment.
108108
## Releases
109109

110110
### 1.7.0 - XXXX-XX-XX
111+
- Show some Cppcheck messages (`toomanyconfigs`, `missingInclude`, `noValidConfiguration`) on file-level. See [Known Issues](#known-issues) on how to fix these. (Contribution by @firewave)
111112

112113
### 1.6.2 - 2022-01-25
113114

resources/META-INF/plugin.xml

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
]]></description>
5757

5858
<change-notes><![CDATA[
59+
- Show some Cppcheck messages (toomanyconfigs, missingInclude, noValidConfiguration) on file-level. See <a href="https://github.com/johnthagen/clion-cppcheck#known-issues">Known Issues</a> on how to fix these. (Contribution by @firewave)
5960
]]>
6061
</change-notes>
6162

src/com/github/johnthagen/cppcheck/CppCheckInspectionImpl.java

+65-39
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
9393

9494
final String id = attributes.getNamedItem("id").getNodeValue();
9595

96-
// Skip the "toomanyconfigs" error
97-
/*
98-
<error id="toomanyconfigs" severity="information" msg="Too many #ifdef configurations - cppcheck only checks 1 of 12 configurations. Use --force to check all configurations." verbose="The checking of the file will be interrupted because there are too many #ifdef configurations. Checking of all #ifdef configurations can be forced by --force command line option or from GUI preferences. However that may increase the checking time." cwe="398">
99-
<location file="C:\Users\Name\AppData\Local\Temp\___valueflow.cpp" line="0" column="0"/>
100-
</error>
101-
*/
102-
if (id.equals("toomanyconfigs")) {
103-
continue;
104-
}
105-
10696
// Skip the "missingIncludeSystem" error
10797
/*
10898
<error id="missingIncludeSystem" severity="information" msg="Cppcheck cannot find all the include files (use --check-config for details)" verbose="Cppcheck cannot find all the include files. Cppcheck can check the code without the include files found. But the results will probably be more accurate if all the include files are found. Please check your project&apos;s include directories and add all of them as include directories for Cppcheck. To see what files Cppcheck cannot find use --check-config."/>
@@ -116,11 +106,6 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
116106
continue;
117107
}
118108

119-
// suppress this warnings for now - will be properly handled in an upcoming patch
120-
if (id.equals("noValidConfiguration") || id.equals("missingInclude")) {
121-
continue;
122-
}
123-
124109
// we are never interested in these
125110
if (id.equals("unmatchedSuppression") || id.equals("purgedConfiguration")) {
126111
continue;
@@ -148,49 +133,90 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
148133
}
149134
}
150135

136+
String fileName = null;
137+
int lineNumber = 0;
138+
int column = 0;
139+
140+
/*
141+
<error id="missingInclude" severity="information" msg="Cppcheck cannot find all the include files (use --check-config for details)" verbose="Cppcheck cannot find all the include files. Cppcheck can check the code without the include files found. But the results will probably be more accurate if all the include files are found. Please check your project&apos;s include directories and add all of them as include directories for Cppcheck. To see what files Cppcheck cannot find use --check-config."/>
142+
*/
143+
// TODO: handle like any warning when Cppcheck provides the --check-config results with the normal analysis
144+
if (id.equals("missingInclude")) {
145+
// is a global warning without location information
146+
}
151147
// ignore entries without location e.g. missingIncludeSystem
152-
if (location == null) {
148+
else if (location == null) {
153149
CppcheckNotification.send("no location for " + psiFile.getVirtualFile().getCanonicalPath(),
154150
id + " " + severity + " " + inconclusive + " " + errorMessage,
155151
NotificationType.ERROR);
156152
continue;
157153
}
154+
else {
155+
final NamedNodeMap locationAttributes = location.getAttributes();
156+
fileName = new File(locationAttributes.getNamedItem("file").getNodeValue()).getName();
157+
lineNumber = Integer.parseInt(locationAttributes.getNamedItem("line").getNodeValue());
158+
final Node columnAttr = locationAttributes.getNamedItem("column");
159+
// TODO: use in ProblemDescriptor
160+
column = -1;
161+
// the "column" attribute was added in Cppcheck 1.89
162+
if (columnAttr != null) {
163+
column = Integer.parseInt(columnAttr.getNodeValue());
164+
}
158165

159-
final NamedNodeMap locationAttributes = location.getAttributes();
160-
final String fileName = new File(locationAttributes.getNamedItem("file").getNodeValue()).getName();
161-
int lineNumber = Integer.parseInt(locationAttributes.getNamedItem("line").getNodeValue());
162-
final Node columnAttr = locationAttributes.getNamedItem("column");
163-
// TODO: use in ProblemDescriptor
164-
int column = -1;
165-
// the "column" attribute was added in Cppcheck 1.89
166-
if (columnAttr != null) {
167-
column = Integer.parseInt(columnAttr.getNodeValue());
166+
// If a file #include's header files, Cppcheck will also run on the header files and print
167+
// any errors. These errors don't apply to the current file and should not be drawn. They can
168+
// be distinguished by checking the file name.
169+
if (!fileName.equals(sourceFileName)) {
170+
continue;
171+
}
168172
}
169173

170-
// If a file #include's header files, Cppcheck will also run on the header files and print
171-
// any errors. These errors don't apply to the current file and should not be drawn. They can
172-
// be distinguished by checking the file name.
173-
if (!fileName.equals(sourceFileName)) {
174-
continue;
175-
}
174+
// leaving it at null will report it for the whole file
175+
TextRange range = null;
176176

177-
// Cppcheck error
178-
if (lineNumber <= 0 || lineNumber > document.getLineCount()) {
177+
/*
178+
<error id="toomanyconfigs" severity="information" msg="Too many #ifdef configurations - cppcheck only checks 1 of 12 configurations. Use --force to check all configurations." verbose="The checking of the file will be interrupted because there are too many #ifdef configurations. Checking of all #ifdef configurations can be forced by --force command line option or from GUI preferences. However that may increase the checking time." cwe="398">
179+
<location file="C:\Users\Name\AppData\Local\Temp\___valueflow.cpp" line="0" column="0"/>
180+
</error>
181+
*/
182+
if (id.equals("toomanyconfigs")) {
183+
// show as message for the file
184+
}
185+
// TODO: handle like any warning when Cppcheck provides the --check-config results with the normal analysis
186+
else if (id.equals("missingInclude")) {
187+
// show as message for the file
188+
}
189+
/*
190+
<error id="noValidConfiguration" severity="information" msg="This file is not analyzed. Cppcheck failed to extract a valid configuration. Use -v for more details." verbose="This file is not analyzed. Cppcheck failed to extract a valid configuration. The tested configurations have these preprocessor errors:\012&apos;&apos; : [/mnt/s/GitHub/cppcheck-fw/gui/temp/moc_platforms.cpp:13] #error &quot;The header file &apos;platforms.h&apos; doesn&apos;t include &lt;QObject&gt;.&quot;\012Q_MOC_OUTPUT_REVISION : [/mnt/s/GitHub/cppcheck-fw/gui/temp/moc_platforms.cpp:15] #error &quot;This file was generated using the moc from 5.12.5. It&quot;">
191+
<location file="/mnt/s/GitHub/cppcheck-fw/gui/temp/moc_platforms.cpp" line="0" column="0"/>
192+
</error>
193+
*/
194+
else if (id.equals("noValidConfiguration")) {
195+
// show as message for the file
196+
// show the verbose message as notification since it contains important details
197+
final String verboseMessage = attributes.getNamedItem("verbose").getNodeValue();
198+
CppcheckNotification.send("noValidConfiguration for "+ psiFile.getVirtualFile().getCanonicalPath(),
199+
verboseMessage,
200+
NotificationType.INFORMATION);
201+
}
202+
else if (lineNumber <= 0 || lineNumber > document.getLineCount()) {
179203
CppcheckNotification.send("line number out-of-bounds for " + psiFile.getVirtualFile().getCanonicalPath(),
180204
id + " " + severity + " " + inconclusive + " " + errorMessage + " " + fileName + " " + lineNumber + " " + column,
181205
NotificationType.ERROR);
182206
continue;
183207
}
208+
else {
209+
// Document counts lines starting at 0, rather than 1 like in cppcheck.
210+
lineNumber -= 1;
184211

185-
// Document counts lines starting at 0, rather than 1 like in cppcheck.
186-
lineNumber -= 1;
187-
188-
final int lineStartOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, lineNumber);
189-
final int lineEndOffset = document.getLineEndOffset(lineNumber);
212+
final int lineStartOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, lineNumber);
213+
final int lineEndOffset = document.getLineEndOffset(lineNumber);
214+
range = TextRange.create(lineStartOffset, lineEndOffset);
215+
}
190216

191217
final ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
192218
psiFile,
193-
TextRange.create(lineStartOffset, lineEndOffset),
219+
range,
194220
"Cppcheck: (" + severity + (inconclusive ? INCONCLUSIVE_TEXT : "") + ") " + id + ": " + errorMessage,
195221
severityToHighlightType(severity),
196222
true);

0 commit comments

Comments
 (0)