Skip to content

Commit

Permalink
Document that --enable=unusedFunction is not supported (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthagen authored Nov 1, 2020
1 parent 3b8e1d1 commit 1b13229
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ plugin, and as a result may have false positives.

When run on header files directly, `cppcheck` defaults to C as the language, which will generate
false positives for C++ projects. C++ projects should append `--language=c++` to the
`cppcheck` options.
`cppcheck` options. In addition, `--enable=unusedFunction` is not supported and will generate false positives.

## Development

Expand Down
7 changes: 5 additions & 2 deletions src/com/github/johnthagen/cppcheck/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ public class Configuration implements Configurable {
"Note: C++ projects should leave --language=c++ appended to the Cppcheck options to avoid some " +
"false positives in header files due to the fact that Cppcheck implicitly defaults to " +
"setting --language to \"c\" for .h files.\n\n" +
"You should not include any --template={} in the options.";
"You should not include any --template={} in the options.\n\n" +
"Due to how the Cppcheck plugin executes on one file at a time, `--enable=unusedFunction`" +
"is not supported.";
private static final String CPPCHECK_MISRA_NOTE =
"Using MISRA requires a rule texts file, which can be obtained from MISRA themselves (Their license prohibits distributing the rules texts)\n\n" +
"Using MISRA requires a rule texts file, which can be obtained from MISRA themselves " +
"(Their license prohibits distributing the rules texts)\n\n" +
"Create a .json file near your Cppcheck installation and point to it here\n" +
"Within that file, create something like this:\n" +
"{\n" +
Expand Down
46 changes: 28 additions & 18 deletions src/com/github/johnthagen/cppcheck/CppcheckInspection.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ public class CppcheckInspection extends LocalInspectionTool {
public ProblemDescriptor[] checkFile(@NotNull PsiFile file,
@NotNull InspectionManager manager,
boolean isOnTheFly) {
String cppcheckPath = Properties.get(Configuration.CONFIGURATION_KEY_CPPCHECK_PATH);
final String cppcheckPath = Properties.get(Configuration.CONFIGURATION_KEY_CPPCHECK_PATH);
String cppcheckOptions = Properties.get(Configuration.CONFIGURATION_KEY_CPPCHECK_OPTIONS);

String cppcheckMisraPath = Properties.get(Configuration.CONFIGURATION_KEY_CPPCHECK_MISRA_PATH);
final String cppcheckMisraPath = Properties.get(Configuration.CONFIGURATION_KEY_CPPCHECK_MISRA_PATH);
if (cppcheckMisraPath != null && !cppcheckMisraPath.isEmpty()) {
cppcheckOptions = String.format("%s --addon=%s", cppcheckOptions, cppcheckMisraPath);
}

VirtualFile vFile = file.getVirtualFile();
final VirtualFile vFile = file.getVirtualFile();
if (vFile == null || !isCFamilyFile(vFile)) {
return ProblemDescriptor.EMPTY_ARRAY;
}

if (cppcheckPath == null || cppcheckPath.isEmpty()) {
StatusBar.Info.set("[!] Error: Please set path of cppcheck in File->Settings->Other Settings",
StatusBar.Info.set("[!] Error: Please set path of cppcheck in File->Settings->Cppcheck Configuration",
file.getProject());
return ProblemDescriptor.EMPTY_ARRAY;
}

Document document = FileDocumentManager.getInstance().getDocument(vFile);
final Document document = FileDocumentManager.getInstance().getDocument(vFile);
if (document == null || document.getLineCount() == 0) {
return ProblemDescriptor.EMPTY_ARRAY;
}
Expand All @@ -71,14 +71,16 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file,
tempFile = FileUtil.createTempFile("", vFile.getName(), true);
FileUtil.writeToFile(tempFile, document.getText());
String cppcheckOutput =
executeCommandOnFile(cppcheckPath, prependIncludeDir(cppcheckOptions, vFile), tempFile.getAbsolutePath(), cppcheckMisraPath);
executeCommandOnFile(cppcheckPath, prependIncludeDir(cppcheckOptions, vFile),
tempFile.getAbsolutePath(), cppcheckMisraPath);

if (!cppcheckOutput.isEmpty()) {
List<ProblemDescriptor> descriptors = parseOutput(file, manager, document, cppcheckOutput, tempFile.getName());
List<ProblemDescriptor> descriptors = parseOutput(file, manager, document, cppcheckOutput,
tempFile.getName());
return descriptors.toArray(new ProblemDescriptor[0]);
}
} catch (ExecutionException | IOException ex) {
Notifications.Bus.notify(new Notification("cppcheck",
Notifications.Bus.notify(new Notification("Cppcheck",
"Error",
ex.getClass().getSimpleName() + ": " + ex.getMessage(),
NotificationType.INFORMATION));
Expand All @@ -94,10 +96,14 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file,

@NotNull
private static String prependIncludeDir(@NotNull String cppcheckOptions, @NotNull VirtualFile vFile) {
VirtualFile dir = vFile.getParent();
if (dir == null) return cppcheckOptions;
String path = dir.getCanonicalPath();
if (path == null) return cppcheckOptions;
final VirtualFile dir = vFile.getParent();
if (dir == null) {
return cppcheckOptions;
}
final String path = dir.getCanonicalPath();
if (path == null) {
return cppcheckOptions;
}
return String.format("-I\"%s\" %s", path, cppcheckOptions);
}

Expand Down Expand Up @@ -160,7 +166,7 @@ private List<ProblemDescriptor> parseOutput(@NotNull PsiFile psiFile,
ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
psiFile,
TextRange.create(lineStartOffset, lintEndOffset),
"cppcheck: (" + severity + ") " + errorMessage,
"Cppcheck: (" + severity + ") " + errorMessage,
severityToHighlightType(severity),
true);
descriptors.add(problemDescriptor);
Expand All @@ -176,17 +182,19 @@ private static String executeCommandOnFile(@NotNull final String command,
final String cppcheckMisraPath) throws ExecutionException {

if (options.contains("--template")) {
throw new ExecutionException("Cppcheck Error: Cppcheck options contains --template field. Please remove this, the plugin defines its own.");
throw new ExecutionException("Cppcheck Error: Cppcheck options contains --template field. " +
"Please remove this, the plugin defines its own.");
}

GeneralCommandLine cmd = new GeneralCommandLine()
.withExePath(command)
.withParameters(ParametersListUtil.parse("--template=\"[{file}:{line}]: ({severity}) {id}: {message}\""))
.withParameters(ParametersListUtil.parse(
"--template=\"[{file}:{line}]: ({severity}) {id}: {message}\""))
.withParameters(ParametersListUtil.parse(options))
.withParameters(ParametersListUtil.parse(filePath));

// Need to be able to get python from the system env
if (cppcheckMisraPath!= null && !cppcheckMisraPath.isEmpty()) {
if (cppcheckMisraPath != null && !cppcheckMisraPath.isEmpty()) {
cmd.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.SYSTEM);
}

Expand All @@ -205,13 +213,15 @@ private static String executeCommandOnFile(@NotNull final String command,
}

if (output.getExitCode() != 0) {
throw new ExecutionException("Cppcheck Error : Exit Code - " + output.getExitCode() + " : " + cmd.getCommandLineString());
throw new ExecutionException("Cppcheck Error: Exit Code - " + output.getExitCode() + " : " +
cmd.getCommandLineString());
}

if (cppcheckMisraPath != null && !cppcheckMisraPath.isEmpty()) {
if (output.getStdout().contains("Bailing out from checking")) {
// MISRA Mode and something went wrong with the misra addon
throw new ExecutionException("Cppcheck MISRA Bail : " + cmd.getCommandLineString() + "\n StdOut : \n" + output.getStdout() + "\n StdErr : " + output.getStderr());
throw new ExecutionException("Cppcheck MISRA Bail: " + cmd.getCommandLineString() +
"\n StdOut: \n" + output.getStdout() + "\n StdErr: " + output.getStderr());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/com/github/johnthagen/cppcheck/JFilePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ private void buttonActionPerformed() {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}


}
}

0 comments on commit 1b13229

Please sign in to comment.