1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

codan: example of CElement checker - external invoke

Change-Id: I0b88fb611372ffdbcceda3d0588c9fb65486cb03
Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
This commit is contained in:
Alena Laskavaia 2015-03-04 15:18:03 -05:00 committed by Elena Laskavaia
parent dd528c19b6
commit acbd1e14f8
3 changed files with 174 additions and 0 deletions

View file

@ -78,6 +78,21 @@
name="%problem.name.Cppcheck.Syntax">
</problem>
</checker>
<checker
class="org.eclipse.cdt.codan.examples.checkers.CToolChecker"
id="org.eclipse.cdt.codan.examples.checkers.CToolChecker"
name="External C Tool">
<problem
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
defaultEnabled="false"
defaultSeverity="Info"
description="Runs external tool and reports errors that this tool finds"
id="org.eclipse.cdt.codan.examples.checkers.CToolChecker.error"
messagePattern="{0}"
name="External C Tool">
</problem>
</checker>
</extension>
<extension
point="org.eclipse.cdt.codan.ui.codanProblemDetails">

View file

@ -11,7 +11,9 @@
package org.eclipse.cdt.codan.examples;
import org.eclipse.cdt.codan.examples.uicontrib.ProfileChangeListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
/**
@ -61,4 +63,48 @@ public class Activator extends Plugin {
public static Activator getDefault() {
return plugin;
}
/**
* Logs the specified status with this plug-in's log.
*
* @param status
* status to log
*/
public static void log(IStatus status) {
getDefault().getLog().log(status);
}
/**
* Logs an internal error with the specified {@code Throwable}.
*
* @param t
* the {@code Throwable} to be logged
*/
public static void log(Throwable t) {
log(new Status(IStatus.ERROR, PLUGIN_ID, 1, "Internal Error", t)); //$NON-NLS-1$
}
/**
* Logs an internal error with the specified message.
*
* @param message
* the error message to log
*/
public static void log(String message) {
log(new Status(IStatus.ERROR, PLUGIN_ID, 1, message, null));
}
/**
* Logs an internal error with the specified message and {@code Throwable}.
*
* @param message
* the error message to log
* @param t
* the {@code Throwable} to be logged
*
* @since 2.1
*/
public static void log(String message, Throwable t) {
log(new Status(IStatus.ERROR, PLUGIN_ID, 1, message, t));
}
}

View file

@ -0,0 +1,113 @@
/*******************************************************************************
* Copyright (c) Mar 4, 2015 QNX Software Systems. All Rights Reserved.
*
* You must obtain a written license from and pay applicable license fees to QNX
* Software Systems before you may reproduce, modify or distribute this software,
* or any work that includes all or part of this software. Free development
* licenses are available for evaluation and non-commercial purposes. For more
* information visit [http://licensing.qnx.com] or email licensing@qnx.com.
*
* This file may contain contributions from others. Please review this entire
* file for other proprietary rights or license notices, as well as the QNX
* Development Suite License Guide at [http://licensing.qnx.com/license-guide/]
* for other information.
*******************************************************************************/
package org.eclipse.cdt.codan.examples.checkers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.codan.core.cxx.externaltool.InvocationFailure;
import org.eclipse.cdt.codan.core.cxx.internal.externaltool.ExternalToolInvoker;
import org.eclipse.cdt.codan.core.cxx.model.AbstractCElementChecker;
import org.eclipse.cdt.codan.core.cxx.model.CodanMarkerGenerator;
import org.eclipse.cdt.codan.core.model.CheckerLaunchMode;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.codan.examples.Activator;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IConsoleParser;
import org.eclipse.cdt.core.IMarkerGenerator;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
/**
* @author elaskavaia
*
*/
public class CToolChecker extends AbstractCElementChecker {
public final static String ID = "org.eclipse.cdt.codan.examples.checkers.CToolChecker.error";
public static final String MAKE_PLUGIN_ID = "org.eclipse.cdt.make.core"; //$NON-NLS-1$
final ExternalToolInvoker externalToolInvoker = new ExternalToolInvoker();
@Override
public void initPreferences(IProblemWorkingCopy problem) {
getTopLevelPreference(problem); // initialize
getLaunchModePreference(problem).enableInLaunchModes(
CheckerLaunchMode.RUN_ON_FILE_SAVE,
CheckerLaunchMode.RUN_ON_DEMAND);
}
@Override
public void processUnit(ITranslationUnit unit) {
IScannerInfo scannerInfo = unit.getScannerInfo(true);
List<String> res = getCompilerOptionsList(scannerInfo);
res.add("-c");
res.add("-o/dev/null");
res.add("-O2");
res.add("-Wall");
res.add("-Werror");
res.add(unit.getFile().getLocation().toPortableString());
String args[] = res.toArray(new String[res.size()]);
try {
externalToolInvoker.launchOnBuildConsole(unit.getResource().getProject(), new IConsoleParser[] { getConsoleParser(unit) },
"check", getToolPath(), args, new String[] {}, getWorkingDirectory(), new NullProgressMonitor());
} catch (CoreException | InvocationFailure e) {
Activator.log(e);
}
}
protected List<String> getCompilerOptionsList(IScannerInfo scannerInfo) {
final Map<String, String> symbols = scannerInfo.getDefinedSymbols();
List<String> res = new ArrayList<>();
for (String macro : symbols.keySet()) {
if (macro.startsWith("_"))
continue; // likely embedded macro
String value = symbols.get(macro);
if (value.isEmpty())
res.add("-D" + macro);
else
res.add("-D" + macro + "=" + value);
}
for (String inc : scannerInfo.getIncludePaths()) {
res.add("-I" + inc);
}
return res;
}
protected Path getToolPath() {
return new Path("gcc");
}
protected IConsoleParser getConsoleParser(ITranslationUnit unit) {
IProject project = unit.getResource().getProject();
return new ErrorParserManager(project, getWorkingDirectory(), getMarkerGenerator(), getParserIDs());
}
protected IMarkerGenerator getMarkerGenerator() {
return new CodanMarkerGenerator(ID, getProblemReporter());
}
protected String[] getParserIDs() {
return null; // null means all default error parsers
}
protected IPath getWorkingDirectory() {
return null; // null means project root path
}
}