Skip to content

Commit 9365b78

Browse files
committed
show some Cppcheck messages on file-level / use full file path in balloon
1 parent 77d081f commit 9365b78

File tree

3 files changed

+76
-37
lines changed

3 files changed

+76
-37
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Deployment.
6363
- Use unique file names for temporary files used for analysis. (Contribution by @firewave)
6464
- Properly handle `debug` messages generated by `--debug-warnings`. (Contribution by @firewave)
6565
- Added `.cl`, `.hxx`, `.tpp` and `.txx` to list of supported file extensions - now matches the ones supported by Cppcheck internally. (Contribution by @firewave)
66+
- Show some Cppcheck messages (`toomanyconfigs`, `missingInclude`, `noValidConfiguration`) on file-level. (Contribution by @firewave)
6667

6768
### 1.5.1 - 2020-11-12
6869

resources/META-INF/plugin.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@
6565
- Fixed scanning of files with whitespaces in name. (Contribution by @firewave)
6666
- Only scan files which actually exist. (Contribution by @firewave)
6767
- Use unique file names for temporary files used for analysis. (Contribution by @firewave)
68-
- Properly handle "debug" messages generated by --debug-warnings. (Contribution by @firewave
69-
- Added .cl, .hxx, .tpp and .txx to list of supported file extensions - now matches the ones supported by Cppcheck internally. (Contribution by @firewave
68+
- Properly handle "debug" messages generated by --debug-warnings. (Contribution by @firewave)
69+
- Added .cl, .hxx, .tpp and .txx to list of supported file extensions - now matches the ones supported by Cppcheck internally. (Contribution by @firewave)
70+
- Show some Cppcheck messages (toomanyconfigs, missingInclude, noValidConfiguration) on file-level. (Contribution by @firewave)
7071
]]>
7172
</change-notes>
7273

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

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ private static ProblemHighlightType severityToHighlightType(@NotNull final Strin
5656
}
5757
}
5858

59+
private static void sendNotification(@NotNull final String title, @NotNull final String content, final NotificationType type) {
60+
Notifications.Bus.notify(new Notification("Cppcheck",
61+
title,
62+
content,
63+
type));
64+
}
65+
5966
// TODO: make configurable
6067
private static final boolean VERBOSE_LOG = false;
6168
private static final String INCONCLUSIVE_TEXT = ":inconclusive";
@@ -69,10 +76,9 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
6976

7077
if (VERBOSE_LOG) {
7178
// TODO: provide XML output via a "Show Cppcheck output" action - event log messages are truncated
72-
Notifications.Bus.notify(new Notification("Cppcheck",
73-
"Cppcheck execution output for " + psiFile.getName(),
79+
sendNotification("Cppcheck execution output for " + psiFile.getVirtualFile().getCanonicalPath(),
7480
cppcheckOutput,
75-
NotificationType.INFORMATION));
81+
NotificationType.INFORMATION);
7682
}
7783

7884
final List<ProblemDescriptor> descriptors = new ArrayList<>();
@@ -96,16 +102,6 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
96102

97103
final String id = attributes.getNamedItem("id").getNodeValue();
98104

99-
// Skip the "toomanyconfigs" error
100-
/*
101-
<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">
102-
<location file="C:\Users\Name\AppData\Local\Temp\___valueflow.cpp" line="0" column="0"/>
103-
</error>
104-
*/
105-
if (id.equals("toomanyconfigs")) {
106-
continue;
107-
}
108-
109105
// Skip the "missingIncludeSystem" error
110106
/*
111107
<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."/>
@@ -131,41 +127,82 @@ public static List<ProblemDescriptor> parseOutput(@NotNull final PsiFile psiFile
131127
}
132128
}
133129

130+
int lineNumber = 0;
131+
int column = 0;
132+
133+
/*
134+
<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."/>
135+
*/
136+
// TODO: handle like any warning when Cppcheck provides the --check-config results with the normal analysis
137+
if (id.equals("missingInclude")) {
138+
// is a global warning without location information
139+
}
134140
// ignore entries without location e.g. missingIncludeSystem
135-
if (location == null) {
141+
else if (location == null) {
136142
continue;
137143
}
144+
else {
145+
final NamedNodeMap locationAttributes = location.getAttributes();
146+
final String fileName = new File(locationAttributes.getNamedItem("file").getNodeValue()).getName();
147+
lineNumber = Integer.parseInt(locationAttributes.getNamedItem("line").getNodeValue());
148+
column = Integer.parseInt(locationAttributes.getNamedItem("column").getNodeValue()); // TODO: use in problem?
149+
150+
// If a file #include's header files, Cppcheck will also run on the header files and print
151+
// any errors. These errors don't apply to the current file and should not be drawn. They can
152+
// be distinguished by checking the file name.
153+
if (!fileName.equals(sourceFileName)) {
154+
continue;
155+
}
156+
}
138157

139-
final NamedNodeMap locationAttributes = location.getAttributes();
140-
final String fileName = new File(locationAttributes.getNamedItem("file").getNodeValue()).getName();
141-
int lineNumber = Integer.parseInt(locationAttributes.getNamedItem("line").getNodeValue());
142-
final int column = Integer.parseInt(locationAttributes.getNamedItem("column").getNodeValue()); // TODO
158+
// leaving it at null will report it for the whole file
159+
TextRange range = null;
143160

144-
// If a file #include's header files, Cppcheck will also run on the header files and print
145-
// any errors. These errors don't apply to the current file and should not be drawn. They can
146-
// be distinguished by checking the file name.
147-
if (!fileName.equals(sourceFileName)) {
148-
continue;
161+
/*
162+
<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">
163+
<location file="C:\Users\Name\AppData\Local\Temp\___valueflow.cpp" line="0" column="0"/>
164+
</error>
165+
*/
166+
if (id.equals("toomanyconfigs")) {
167+
// show as message for the file
149168
}
150-
151-
// Cppcheck error
152-
if (lineNumber <= 0 || lineNumber > document.getLineCount()) {
153-
Notifications.Bus.notify(new Notification("Cppcheck",
169+
// TODO: handle like any warning when Cppcheck provides the --check-config results with the normal analysis
170+
else if (id.equals("missingInclude")) {
171+
// show as message for the file
172+
}
173+
/*
174+
<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;">
175+
<location file="/mnt/s/GitHub/cppcheck-fw/gui/temp/moc_platforms.cpp" line="0" column="0"/>
176+
</error>
177+
*/
178+
else if (id.equals("noValidConfiguration")) {
179+
// show as message for the file
180+
// show the verbose message as notification since it contains important details
181+
final String verboseMessage = attributes.getNamedItem("verbose").getNodeValue();
182+
sendNotification(
183+
"Cppcheck noValidConfiguration for "+ psiFile.getVirtualFile().getCanonicalPath(),
184+
verboseMessage,
185+
NotificationType.INFORMATION);
186+
}
187+
else if (lineNumber <= 0 || lineNumber > document.getLineCount()) {
188+
sendNotification(
154189
"Cppcheck line number out-of-bounds " + i,
155-
id + " " + severity + " " + inconclusive + " " + errorMessage + " " + fileName + " " + lineNumber + " " + column,
156-
NotificationType.ERROR));
190+
id + " " + severity + " " + inconclusive + " " + errorMessage + " " + psiFile.getVirtualFile().getCanonicalPath() + " " + lineNumber + " " + column,
191+
NotificationType.ERROR);
157192
continue;
158193
}
194+
else {
195+
// Document counts lines starting at 0, rather than 1 like in cppcheck.
196+
lineNumber -= 1;
159197

160-
// Document counts lines starting at 0, rather than 1 like in cppcheck.
161-
lineNumber -= 1;
162-
163-
final int lineStartOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, lineNumber);
164-
final int lineEndOffset = document.getLineEndOffset(lineNumber);
198+
final int lineStartOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, lineNumber);
199+
final int lineEndOffset = document.getLineEndOffset(lineNumber);
200+
range = TextRange.create(lineStartOffset, lineEndOffset);
201+
}
165202

166203
final ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
167204
psiFile,
168-
TextRange.create(lineStartOffset, lineEndOffset),
205+
range,
169206
"Cppcheck: (" + severity + (inconclusive ? INCONCLUSIVE_TEXT : "") + ") " + id + ": " + errorMessage,
170207
severityToHighlightType(severity),
171208
true);

0 commit comments

Comments
 (0)