diff options
author | Alex Ruiz | 2012-04-18 19:50:11 +0000 |
---|---|---|
committer | Sergey Prigogin | 2012-04-18 19:52:48 +0000 |
commit | 1f83e4aa213209926b082cc53db587314d1d9f0b (patch) | |
tree | b6c7f0ac5e4f1fcf3c67216ea51b9f9d1fc9a751 /codan/org.eclipse.cdt.codan.examples/src | |
parent | 4a8860255eea49dae2ec96d302088b39bd80f3b5 (diff) | |
download | org.eclipse.cdt-1f83e4aa213209926b082cc53db587314d1d9f0b.tar.gz org.eclipse.cdt-1f83e4aa213209926b082cc53db587314d1d9f0b.tar.xz org.eclipse.cdt-1f83e4aa213209926b082cc53db587314d1d9f0b.zip |
Bug 372551. Codan support for writing checkers that invoke external
tools.
Change-Id: Ia5cfd24d54ec6e67e08f24a367a6bc689ae407d0
Reviewed-on: https://git.eclipse.org/r/5586
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Diffstat (limited to 'codan/org.eclipse.cdt.codan.examples/src')
5 files changed, 210 insertions, 0 deletions
diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckChecker.java b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckChecker.java new file mode 100644 index 00000000000..c82a4d7f74b --- /dev/null +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckChecker.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2012 Google, Inc and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alex Ruiz (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.examples.checkers.cppcheck; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.cdt.codan.core.cxx.externaltool.AbstractExternalToolBasedChecker; +import org.eclipse.cdt.codan.core.cxx.externaltool.ConfigurationSettings; +import org.eclipse.cdt.core.ProblemMarkerInfo; + +/** + * Checker that invokes <a href="http://cppcheck.sourceforge.net/">Cppcheck</a> when a C/C++ file is + * saved. + */ +public class CppcheckChecker extends AbstractExternalToolBasedChecker { + private static final String TOOL_NAME = Messages.CppcheckChecker_toolName; + private static final String EXECUTABLE_NAME = "cppcheck"; //$NON-NLS-1$ + private static final String DEFAULT_ARGS = "--enable=all"; //$NON-NLS-1$ + + private static final String DESCRIPTION_FORMAT = "[" + TOOL_NAME + "] %s"; //$NON-NLS-1$ //$NON-NLS-2$ + + private static final String ERROR_PROBLEM_ID; + + // key: severity (error, warning, etc.) - value : problem ID associated to severity + private static final Map<Severity, String> PROBLEM_IDS = new HashMap<Severity, String>(); + + static { + ERROR_PROBLEM_ID = addProblemId(Severity.ERROR); + addProblemId(Severity.WARNING); + addProblemId(Severity.STYLE); + } + + private static String addProblemId(Severity severity) { + String problemId = "org.eclipse.cdt.codan.checkers.cppcheck." + severity; //$NON-NLS-1$ + PROBLEM_IDS.put(severity, problemId); + return problemId; + } + + public CppcheckChecker() { + super(new ConfigurationSettings(TOOL_NAME, new File(EXECUTABLE_NAME), DEFAULT_ARGS)); + } + + @Override + protected String[] getParserIDs() { + return new String[] { "org.eclipse.cdt.codan.checkers.externaltool.CppcheckChecker" }; //$NON-NLS-1$ + } + + @Override + public void addMarker(ProblemMarkerInfo info) { + Severity severity = Severity.findSeverity(info.severity); + String description = String.format(DESCRIPTION_FORMAT, info.description); + reportProblem(PROBLEM_IDS.get(severity), createProblemLocation(info), description); + } + + @Override + protected String getReferenceProblemId() { + return ERROR_PROBLEM_ID; + } +} diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckOutputParser.java b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckOutputParser.java new file mode 100644 index 00000000000..c9ff4ce889e --- /dev/null +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/CppcheckOutputParser.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2012 Google, Inc and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alex Ruiz (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.examples.checkers.cppcheck; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.cdt.core.ErrorParserManager; +import org.eclipse.cdt.core.IErrorParser; +import org.eclipse.cdt.core.ProblemMarkerInfo; +import org.eclipse.core.resources.IFile; + +/** + * Parses the output of Cppcheck. + */ +public class CppcheckOutputParser implements IErrorParser { + // sample line to parse: + // + // [/src/HelloWorld.cpp:19]: (style) The scope of the variable 'i' can be reduced + // ----------1--------- -2 --3-- ------------------4------------------------- + // + // groups: + // 1: file path and name + // 2: line where problem was found + // 3: problem severity + // 4: problem description + private static Pattern pattern = Pattern.compile("\\[(.*):(\\d+)\\]:\\s*\\((.*)\\)\\s*(.*)"); //$NON-NLS-1$ + + @Override + public boolean processLine(String line, ErrorParserManager eoParser) { + Matcher matcher = pattern.matcher(line); + if (!matcher.matches()) { + return false; + } + IFile fileName = eoParser.findFileName(matcher.group(1)); + if (fileName != null) { + int lineNumber = Integer.parseInt(matcher.group(2)); + String description = matcher.group(4); + int severity = Severity.findSeverityCode(matcher.group(3)); + ProblemMarkerInfo info = new ProblemMarkerInfo(fileName, lineNumber, description, severity, null); + eoParser.addProblemMarker(info); + return true; + } + return false; + } +} diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.java b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.java new file mode 100644 index 00000000000..b194e9f2b4f --- /dev/null +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2012 Google, Inc and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alex Ruiz (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.examples.checkers.cppcheck; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + public static String CppcheckChecker_toolName; + + static { + // initialize resource bundle + Class<Messages> clazz = Messages.class; + NLS.initializeMessages(clazz.getName(), clazz); + } + + private Messages() { + } +} diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.properties b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.properties new file mode 100644 index 00000000000..8dda9c9c3ec --- /dev/null +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Messages.properties @@ -0,0 +1,11 @@ +############################################################################### +# Copyright (c) 2012 Google, Inc and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Alex Ruiz (Google) - initial API and implementation +############################################################################### +CppcheckChecker_toolName=Cppcheck diff --git a/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Severity.java b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Severity.java new file mode 100644 index 00000000000..f8f91cfda46 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.examples/src/org/eclipse/cdt/codan/examples/checkers/cppcheck/Severity.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2012 Google, Inc and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alex Ruiz (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.examples.checkers.cppcheck; + +import org.eclipse.cdt.core.IMarkerGenerator; + +enum Severity { + ERROR(IMarkerGenerator.SEVERITY_ERROR_RESOURCE, "error"), //$NON-NLS-1$ + WARNING(IMarkerGenerator.SEVERITY_WARNING, "warning"), //$NON-NLS-1$ + STYLE(IMarkerGenerator.SEVERITY_INFO, "style"); //$NON-NLS-1$ + + private final int code; + private final String text; + + private Severity(int code, String text) { + this.code = code; + this.text = text; + } + + static int findSeverityCode(String text) { + for (Severity severity : values()) { + if (severity.text.equals(text)) { + return severity.code; + } + } + return STYLE.code; + } + + static Severity findSeverity(int code) { + for (Severity severity : values()) { + if (severity.code == code) { + return severity; + } + } + return STYLE; + } + + @Override + public String toString() { + return text; + } +}
\ No newline at end of file |