1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 372551. Removed the external tool checker support until all design

issues are resolved.
This commit is contained in:
Sergey Prigogin 2012-03-13 18:42:16 -07:00
parent c510e7ad5b
commit ada23a9a2f
39 changed files with 10 additions and 2212 deletions

View file

@ -10,13 +10,4 @@
###############################################################################
#Properties file for org.eclipse.cdt.codan.checkers.ui
Bundle-Vendor = Eclipse CDT
Bundle-Name = Codan Checkers Ui
checker.name.Cppcheck = Cppcheck
problem.description.Cppcheck.Error = Errors reported by Cppcheck (http://cppcheck.sourceforge.net/)
problem.name.Cppcheck.Error = Errors
problem.description.Cppcheck.Warning = Warnings reported by Cppcheck (http://cppcheck.sourceforge.net/)
problem.name.Cppcheck.Warning = Warnings
problem.description.Cppcheck.Syntax = Syntax problems reported by Cppcheck (http://cppcheck.sourceforge.net/)
problem.name.Cppcheck.Syntax = Syntax Problems
problem.messagePattern.Cppcheck.all = {0}
Bundle-Name = Codan Checkers UI

View file

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.cdt.codan.ui.codanMarkerResolution">
<resolution
@ -45,46 +43,5 @@
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CaseBreakQuickFixComment"
problemId="org.eclipse.cdt.codan.internal.checkers.CaseBreakProblem">
</resolution>
</extension>
<extension
point="org.eclipse.cdt.codan.core.checkers"
id="org.eclipse.cdt.codan.core.internal.checkers">
<category
id="org.eclipse.cdt.codan.checkers.cppcheck"
name="%checker.name.Cppcheck">
</category>
<checker
class="org.eclipse.cdt.codan.internal.checkers.ui.CppcheckChecker"
id="org.eclipse.cdt.codan.checkers.CppcheckChecker"
name="Cppcheck">
<problem
category="org.eclipse.cdt.codan.checkers.cppcheck"
defaultEnabled="false"
defaultSeverity="Error"
description="%problem.description.Cppcheck.Error"
id="org.eclipse.cdt.codan.checkers.cppcheck.error"
messagePattern="%problem.messagePattern.Cppcheck.all"
name="%problem.name.Cppcheck.Error">
</problem>
<problem
category="org.eclipse.cdt.codan.checkers.cppcheck"
defaultEnabled="false"
defaultSeverity="Warning"
description="%problem.description.Cppcheck.Warning"
id="org.eclipse.cdt.codan.checkers.cppcheck.warning"
messagePattern="%problem.messagePattern.Cppcheck.all"
name="%problem.name.Cppcheck.Warning">
</problem>
<problem
category="org.eclipse.cdt.codan.checkers.cppcheck"
defaultEnabled="false"
defaultSeverity="Warning"
description="%problem.description.Cppcheck.Syntax"
id="org.eclipse.cdt.codan.checkers.cppcheck.style"
messagePattern="%problem.messagePattern.Cppcheck.all"
name="%problem.name.Cppcheck.Syntax">
</problem>
</checker>
</extension>
</plugin>

View file

@ -1,80 +0,0 @@
/*******************************************************************************
* 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.internal.checkers.ui;
import static java.util.Collections.singletonList;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.codan.core.externaltool.AbstractOutputParser;
import org.eclipse.cdt.codan.core.externaltool.ConfigurationSettings;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.internal.checkers.externaltool.CppcheckOutputParser;
import org.eclipse.cdt.codan.ui.cxx.externaltool.AbstractCxxExternalToolBasedChecker;
/**
* Checker that invokes <a href="http://cppcheck.sourceforge.net/">Cppcheck</a> when a C/C++ file is
* saved.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class CppcheckChecker extends AbstractCxxExternalToolBasedChecker {
private static final String TOOL_NAME = "Cppcheck"; //$NON-NLS-1$
private static final String EXECUTABLE_NAME = "cppcheck"; //$NON-NLS-1$
private static final String DEFAULT_ARGS = ""; //$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<String, String> PROBLEM_IDS = new HashMap<String, String>();
static {
ERROR_PROBLEM_ID = addProblemId("error"); //$NON-NLS-1$
addProblemId("warning"); //$NON-NLS-1$
addProblemId("style"); //$NON-NLS-1$
}
private static String addProblemId(String 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, false));
}
@Override
public void reportProblem(IProblemLocation location, String description, String severity) {
String problemId = PROBLEM_IDS.get(severity);
if (problemId == null) {
problemId = getReferenceProblemId();
}
super.reportProblem(problemId, location, String.format(DESCRIPTION_FORMAT, description));
}
@Override
protected String getReferenceProblemId() {
return ERROR_PROBLEM_ID;
}
@Override
protected List<AbstractOutputParser> createParsers(InvocationParameters parameters) {
AbstractOutputParser parser = new CppcheckOutputParser(parameters, this);
return singletonList(parser);
}
}

View file

@ -15,5 +15,4 @@ Bundle-Vendor: %Bundle-Vendor
Export-Package: org.eclipse.cdt.codan.checkers,
org.eclipse.cdt.codan.internal.checkers;
x-friends:="org.eclipse.cdt.codan.checkers.ui,
org.eclipse.cdt.codan.core.test",
org.eclipse.cdt.codan.internal.checkers.externaltool
org.eclipse.cdt.codan.core.test"

View file

@ -1,83 +0,0 @@
/*******************************************************************************
* 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.internal.checkers.externaltool;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.externaltool.AbstractOutputParser;
import org.eclipse.cdt.codan.core.externaltool.IProblemDisplay;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemLocationFactory;
import org.eclipse.core.resources.IFile;
/**
* Parses the output of Cppcheck.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 1.1
*/
public class CppcheckOutputParser extends AbstractOutputParser {
// 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$
private final InvocationParameters parameters;
private final IProblemDisplay problemDisplay;
/**
* Constructor.
* @param parameters the parameters to pass to Cppcheck.
* @param problemDisplay displays any problems reported by Cppcheck as markers.
*/
public CppcheckOutputParser(InvocationParameters parameters, IProblemDisplay problemDisplay) {
this.parameters = parameters;
this.problemDisplay = problemDisplay;
}
@Override
public boolean parse(String line) {
Matcher matcher = pattern.matcher(line);
if (!matcher.matches()) {
return false;
}
String filePath = matcher.group(1);
if (parameters.getActualFilePath().equals(filePath)) {
int lineNumber = Integer.parseInt(matcher.group(2));
String severity = matcher.group(3);
String description = matcher.group(4);
IProblemLocation location = createProblemLocation(lineNumber);
problemDisplay.reportProblem(location, description, severity);
}
return true;
}
private IProblemLocation createProblemLocation(int lineNumber) {
IProblemLocationFactory factory = CodanRuntime.getInstance().getProblemLocationFactory();
IFile actualFile = (IFile) parameters.getActualFile();
return factory.createProblemLocation(actualFile, -1, -1, lineNumber);
}
@Override
public void reset() {
}
}

View file

@ -10,9 +10,12 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.codan.core.cxx,
org.eclipse.cdt.codan.core.cxx.internal.model;x-friends:="org.eclipse.cdt.codan.checkers.ui,org.eclipse.cdt.codan.ui,org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.core.cxx.internal.model.cfg;x-friends:="org.eclipse.cdt.codan.core.test",
org.eclipse.cdt.codan.core.cxx.model,
org.eclipse.cdt.codan.core.cxx.util
org.eclipse.cdt.codan.core.cxx.internal.model;
x-friends:="org.eclipse.cdt.codan.checkers.ui,
org.eclipse.cdt.codan.ui,
org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.core.cxx.internal.model.cfg;
x-friends:="org.eclipse.cdt.codan.core.test",
org.eclipse.cdt.codan.core.cxx.model
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: %Bundle-Vendor

View file

@ -1,68 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Google, Inc.
* 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 - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.core.cxx.util;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
/**
* Utility methods related to C++ file types.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public final class FileTypes {
private static final String[] CPP_FILE_EXTENSIONS = {
"cc", //$NON-NLS-1$
"cpp", //$NON-NLS-1$
"cxx" //$NON-NLS-1$
};
private static final String[] HEADER_FILE_EXTENSIONS = { "h" }; //$NON-NLS-1$
/**
* Indicates whether the given <code>{@link IResource}</code> is a C++ file.
* @param resource the {@code IResource} to check.
* @return {@code true} if the given {@code IResource} is a C++ file; {@code false} otherwise.
*/
public static boolean isCppFile(IResource resource) {
return isFileWithExtension(resource, CPP_FILE_EXTENSIONS);
}
/**
* Indicates whether the given <code>{@link IResource}</code> is a header file.
* @param resource the {@code IResource} to check.
* @return {@code true} if the given {@code IResource} is a header file; {@code false}
* otherwise.
*/
public static boolean isHeaderFile(IResource resource) {
return isFileWithExtension(resource, HEADER_FILE_EXTENSIONS);
}
private static boolean isFileWithExtension(IResource resource, String[] validExtensions) {
if (!(resource instanceof IFile)) {
return false;
}
IPath path = resource.getFullPath();
return doesPathHaveExtension(path, validExtensions);
}
private static boolean doesPathHaveExtension(IPath path, String[] validExtensions) {
String fileExtension = path.getFileExtension();
for (String extension : validExtensions) {
if (extension.equalsIgnoreCase(fileExtension)) {
return true;
}
}
return false;
}
private FileTypes() {}
}

View file

@ -30,12 +30,10 @@ import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerT
import org.eclipse.cdt.codan.core.internal.checkers.SuggestedParenthesisCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.SuspiciousSemicolonCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.UnusedSymbolInFileScopeCheckerTest;
import org.eclipse.cdt.codan.internal.checkers.externaltool.CppcheckOutputParserTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CaseBreakQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CatchByReferenceQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.CreateLocalVariableQuickFixTest;
import org.eclipse.cdt.codan.internal.checkers.ui.quickfix.SuggestedParenthesisQuickFixTest;
import org.eclipse.cdt.codan.internal.core.externaltool.ExternalToolInvokerTest;
public class AutomatedIntegrationSuite extends TestSuite {
public AutomatedIntegrationSuite() {
@ -71,8 +69,6 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
suite.addTestSuite(SuspiciousSemicolonCheckerTest.class);
suite.addTestSuite(UnusedSymbolInFileScopeCheckerTest.class);
suite.addTestSuite(ExternalToolInvokerTest.class);
suite.addTestSuite(CppcheckOutputParserTest.class);
// framework
suite.addTest(CodanFastTestSuite.suite());
// quick fixes

View file

@ -1,111 +0,0 @@
/*******************************************************************************
* 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.internal.checkers.externaltool;
import org.eclipse.cdt.codan.core.externaltool.IProblemDisplay;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.test.CodanTestCase;
import org.eclipse.core.resources.IResource;
/**
* Tests for <code>{@link CppcheckOutputParser}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@SuppressWarnings("nls")
public class CppcheckOutputParserTest extends CodanTestCase {
private static final String MATCHING_LINE_FORMAT = "[%s:%d]: (%s) %s";
private ProblemDisplayStub problemDisplay;
private CppcheckOutputParser parser;
@Override
public void setUp() throws Exception {
super.setUp();
problemDisplay = new ProblemDisplayStub();
loadcode("void f(int x) {}", "test.cpp");
InvocationParameters parameters = new InvocationParameters(currentIFile, currentIFile,
actualFilePath(), null);
parser = new CppcheckOutputParser(parameters, problemDisplay);
}
public void testProblemIsReportedWhenParsingMatchingLine() {
int lineNumber = 2;
String severity = "warning";
String description = "The scope of the variable 'i' can be reduced";
boolean parsed = parser.parse(createMatchingLine(lineNumber, severity, description));
assertTrue(parsed);
problemDisplay.assertThatLocationHasFile(currentIFile);
problemDisplay.assertThatLocationHasLineNumber(lineNumber);
problemDisplay.assertThatLocationHasNoStartingAndEndingChars();
problemDisplay.assertThatReceivedDescription(description);
problemDisplay.assertThatReceivedSeverity(severity);
}
private String createMatchingLine(int lineNumber, String severity, String description) {
return String.format(MATCHING_LINE_FORMAT, actualFilePath(), lineNumber, severity, description);
}
private String actualFilePath() {
return currentIFile.getLocation().toOSString();
}
public void testProblemIsNotReportedWhenLineDoesNotMatch() {
boolean parsed = parser.parse("Checking usage of global functions..");
assertFalse(parsed);
problemDisplay.assertThatProblemWasNotReported();
}
private static class ProblemDisplayStub implements IProblemDisplay {
private boolean problemReported;
private IProblemLocation location;
private String description;
private String severity;
public void reportProblem(IProblemLocation location, String description) {
throw new UnsupportedOperationException();
}
public void reportProblem(IProblemLocation location, String description, String severity) {
problemReported = true;
this.location = location;
this.description = description;
this.severity = severity;
}
void assertThatLocationHasFile(IResource expected) {
assertSame(expected, location.getFile());
}
void assertThatLocationHasLineNumber(int expected) {
assertEquals(expected, location.getLineNumber());
}
void assertThatLocationHasNoStartingAndEndingChars() {
assertEquals(-1, location.getStartingChar());
assertEquals(-1, location.getEndingChar());
}
void assertThatReceivedDescription(String expected) {
assertEquals(expected, description);
}
void assertThatReceivedSeverity(String expected) {
assertEquals(expected, severity);
}
void assertThatProblemWasNotReported() {
assertFalse(problemReported);
}
}
}

View file

@ -1,290 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertArrayEquals;
import org.eclipse.cdt.codan.core.externaltool.AbstractOutputParser;
import org.eclipse.cdt.codan.core.externaltool.ConfigurationSettings;
import org.eclipse.cdt.codan.core.externaltool.IArgsSeparator;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinter;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinterProvider;
import org.eclipse.cdt.codan.core.externaltool.InvocationFailure;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.cdt.codan.core.externaltool.SpaceArgsSeparator;
import org.eclipse.cdt.codan.core.param.BasicProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreference;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
import org.eclipse.cdt.codan.core.test.CodanTestCase;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Tests for <code>{@link ExternalToolInvoker}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@SuppressWarnings("nls")
public class ExternalToolInvokerTest extends CodanTestCase {
private String externalToolName;
private String externalToolPath;
private boolean shouldDisplayOutput;
private IPath workingDirectory;
private ConsolePrinterProviderStub consolePrinterProvider;
private String[] externalToolOutput;
private ProcessInvokerStub processInvoker;
private OutputParserStub outputParser;
private List<AbstractOutputParser> outputParsers;
private ConfigurationSettings settings;
private IArgsSeparator argsSeparator;
private ExternalToolInvoker externalToolInvoker;
@Override
public void setUp() throws Exception {
super.setUp();
externalToolName = "TestTool";
externalToolPath = "/usr/local/testtool";
shouldDisplayOutput = true;
workingDirectory = new Path("/usr/local/project");
consolePrinterProvider = new ConsolePrinterProviderStub();
externalToolOutput = new String[] { "line1", "line2" };
processInvoker = new ProcessInvokerStub(externalToolOutput);
outputParser = new OutputParserStub();
outputParsers = new ArrayList<AbstractOutputParser>();
outputParsers.add(outputParser);
settings = new ConfigurationSettings(externalToolName, new File("testtool"), "", false);
MapProblemPreference preferences = createPreferences(new File(externalToolPath),
"--include=all --debug=true", shouldDisplayOutput);
settings.updateValuesFrom(preferences);
argsSeparator = new SpaceArgsSeparator();
externalToolInvoker = new ExternalToolInvoker(consolePrinterProvider, processInvoker);
}
private MapProblemPreference createPreferences(File executablePath, String args,
boolean shouldDisplayOutput) {
MapProblemPreference preferences = new MapProblemPreference();
preferences.addChildDescriptor(createPreference(PathSetting.KEY, executablePath));
preferences.addChildDescriptor(createPreference(ArgsSetting.KEY, args));
preferences.addChildDescriptor(
createPreference(ConsoleOutputSetting.KEY, shouldDisplayOutput));
return preferences;
}
private IProblemPreference createPreference(String key, Object value) {
BasicProblemPreference preference = new BasicProblemPreference(key, "");
preference.setValue(value);
return preference;
}
@Override
public boolean isCpp() {
return true;
}
// class C {
// };
public void testInvokesProcessCorrectly() throws Throwable {
String aboveComment = getAboveComment();
loadcode(aboveComment);
String expectedCommand = expectedCommand();
InvocationParameters parameters = new InvocationParameters(currentIFile, currentIFile,
actualFilePath(), workingDirectory);
externalToolInvoker.invoke(parameters, settings, argsSeparator, outputParsers);
consolePrinterProvider.assertThatReceivedExternalToolName(externalToolName);
consolePrinterProvider.assertThatReceivedShouldDisplayOutputFlag(shouldDisplayOutput);
consolePrinterProvider.consolePrinter.assertThatPrinted(expectedCommand, externalToolOutput);
consolePrinterProvider.consolePrinter.assertThatIsClosed();
processInvoker.assertThatReceivedCommand(expectedCommand);
processInvoker.assertThatReceivedWorkingDirectory(workingDirectory);
processInvoker.process.assertThatIsDestroyed();
outputParser.assertThatParsed(externalToolOutput);
outputParser.assertThatWasReset();
}
private String expectedCommand() {
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.append(externalToolPath).append(" ")
.append(actualFilePath()).append(" ")
.append(settings.getArgs().getValue());
return commandBuilder.toString();
}
private String actualFilePath() {
return currentIFile.getLocation().toOSString();
}
private static class ConsolePrinterProviderStub implements IConsolePrinterProvider {
private String externalToolName;
private boolean shouldDisplayOutput;
final ConsolePrinterStub consolePrinter = new ConsolePrinterStub();
@Override
public ConsolePrinterStub createConsole(String externalToolName, boolean shouldDisplayOutput) {
this.externalToolName = externalToolName;
this.shouldDisplayOutput = shouldDisplayOutput;
return consolePrinter;
}
void assertThatReceivedExternalToolName(String expected) {
assertEquals(expected, externalToolName);
}
void assertThatReceivedShouldDisplayOutputFlag(boolean expected) {
assertEquals(expected, shouldDisplayOutput);
}
}
private static class ConsolePrinterStub implements IConsolePrinter {
private final List<String> printed = new ArrayList<String>();
private boolean closed;
@Override
public void clear() {
printed.clear();
}
@Override
public void println(String message) {
printed.add(message);
}
@Override
public void println() {}
@Override
public void close() {
closed = true;
}
void assertThatPrinted(String command, String[] externalToolOutput) {
List<String> expected = new ArrayList<String>();
expected.add(command);
expected.addAll(asList(externalToolOutput));
assertEquals(expected, printed);
}
void assertThatIsClosed() {
assertTrue(closed);
}
}
private static class ProcessInvokerStub extends ProcessInvoker {
final ProcessStub process;
private String command;
private IPath workingDirectory;
ProcessInvokerStub(String[] externalToolOutput) {
process = new ProcessStub(externalToolOutput);
}
@Override
public ProcessStub invoke(String command, IPath workingDirectory) throws InvocationFailure {
this.command = command;
this.workingDirectory = workingDirectory;
return process;
}
void assertThatReceivedCommand(String expected) {
assertEquals(expected, command);
}
void assertThatReceivedWorkingDirectory(IPath expected) {
assertSame(expected, workingDirectory);
}
}
private static class ProcessStub extends Process {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final InputStream inputStream;
private final InputStream errorStream;
private boolean destroyed;
ProcessStub(String[] externalToolOutput) {
StringBuilder builder = new StringBuilder();
for (String s : externalToolOutput) {
builder.append(s).append(LINE_SEPARATOR);
}
inputStream = new ByteArrayInputStream(builder.toString().getBytes());
errorStream = new ByteArrayInputStream(new byte[0]);
}
@Override
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public InputStream getErrorStream() {
return errorStream;
}
@Override
public int waitFor() {
throw new UnsupportedOperationException();
}
@Override
public int exitValue() {
throw new UnsupportedOperationException();
}
@Override
public void destroy() {
destroyed = true;
}
void assertThatIsDestroyed() {
assertTrue(destroyed);
}
}
private static class OutputParserStub extends AbstractOutputParser {
private final List<String> parsed = new ArrayList<String>();
private boolean reset;
@Override
public boolean parse(String line) throws InvocationFailure {
parsed.add(line);
return true;
}
@Override
public void reset() {
reset = true;
}
void assertThatParsed(String[] expected) {
assertArrayEquals(expected, parsed.toArray());
}
void assertThatWasReset() {
assertTrue(reset);
}
}
}

View file

@ -10,7 +10,6 @@ Require-Bundle: org.eclipse.core.runtime,
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: org.eclipse.cdt.codan.core,
org.eclipse.cdt.codan.core.externaltool,
org.eclipse.cdt.codan.core.model,
org.eclipse.cdt.codan.core.model.cfg;
x-friends:="org.eclipse.cdt.codan.core.cxx,
@ -22,7 +21,6 @@ Export-Package: org.eclipse.cdt.codan.core,
org.eclipse.cdt.codan.core.test,
org.eclipse.cdt.codan.ui",
org.eclipse.cdt.codan.internal.core.cfg;x-friends:="org.eclipse.cdt.codan.core.cxx",
org.eclipse.cdt.codan.internal.core.externaltool;x-friends:="org.eclipse.cdt.codan.core.test",
org.eclipse.cdt.codan.internal.core.model;
x-friends:="org.eclipse.cdt.codan.core.cxx,
org.eclipse.cdt.codan.core.test,

View file

@ -1,200 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.codan.core.param.IProblemPreference;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
import org.eclipse.cdt.codan.core.param.RootProblemPreference;
import org.eclipse.cdt.codan.core.param.SharedRootProblemPreference;
import org.eclipse.cdt.codan.internal.core.externaltool.ExternalToolInvoker;
import org.eclipse.core.resources.IResource;
import java.util.List;
/**
* Base class for checkers that invoke external command-line tools to perform code checking.
* <p>
* A file, to be processed by this type of checker, must:
* <ol>
* <li>be in the current active editor</li>
* <li>not have any unsaved changes</li>
* </ol>
* </p>
* By default, implementations of this checker are not allowed to run while the user types, since
* external tools cannot see unsaved changes.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public abstract class AbstractExternalToolBasedChecker extends AbstractCheckerWithProblemPreferences
implements IProblemDisplay {
private static final boolean DO_NOT_TRAVERSE_CHILDREN = false;
private final IInvocationParametersProvider parametersProvider;
private final IArgsSeparator argsSeparator;
private final ConfigurationSettings settings;
private final ExternalToolInvoker externalToolInvoker;
private final RootProblemPreference preferences;
/**
* Constructor.
* @param parametersProvider provides the parameters to pass when invoking the external tool.
* @param argsSeparator separates the arguments to pass to the external tool executable. These
* arguments are stored in a single {@code String}.
* @param consolePrinterProvider creates an Eclipse console that uses the name of an external
* tool as its own.
* @param settings user-configurable external tool configuration settings.
*/
public AbstractExternalToolBasedChecker(IInvocationParametersProvider parametersProvider,
IArgsSeparator argsSeparator, IConsolePrinterProvider consolePrinterProvider,
ConfigurationSettings settings) {
this.parametersProvider = parametersProvider;
this.argsSeparator = argsSeparator;
this.settings = settings;
externalToolInvoker = new ExternalToolInvoker(consolePrinterProvider);
preferences = new SharedRootProblemPreference();
}
/**
* Indicates whether this checker is enabled to run while the user types. By default, this
* method returns {@code false}.
* <p>
* Running command-line based checkers while the user types is unnecessary and wasteful, since
* command-line tools are expensive to call (they run in a separate process) and they cannot
* see unsaved changes.
* </p>
* @return {@code false}.
*/
@Override
public boolean runInEditor() {
return false;
}
@Override
public boolean processResource(IResource resource) {
process(resource);
return DO_NOT_TRAVERSE_CHILDREN;
}
private void process(IResource resource) {
try {
InvocationParameters parameters = parametersProvider.createParameters(resource);
if (parameters != null) {
invokeExternalTool(parameters);
}
} catch (Throwable error) {
logResourceProcessingFailure(error, resource);
}
}
private void invokeExternalTool(InvocationParameters parameters) throws Throwable {
updateConfigurationSettingsFromPreferences(parameters.getActualFile());
List<AbstractOutputParser> parsers = createParsers(parameters);
try {
externalToolInvoker.invoke(parameters, settings, argsSeparator, parsers);
} catch (InvocationFailure error) {
handleInvocationFailure(error, parameters);
}
}
private void updateConfigurationSettingsFromPreferences(IResource fileToProcess) {
IProblem problem = getProblemById(getReferenceProblemId(), fileToProcess);
MapProblemPreference preferences = (MapProblemPreference) problem.getPreference();
settings.updateValuesFrom(preferences);
}
/**
* Creates instances of <code>{@link AbstractOutputParser}</code>.
* @param parameters the parameters to pass when invoking an external tool.
*/
protected abstract List<AbstractOutputParser> createParsers(InvocationParameters parameters);
/**
* Handles a failure reported when invoking the external tool. This implementation simply
* logs the failure.
* @param error the reported failure.
* @param parameters the parameters passed to the external tool executable.
*/
protected void handleInvocationFailure(InvocationFailure error, InvocationParameters parameters) {
logResourceProcessingFailure(error, parameters.getActualFile());
}
private void logResourceProcessingFailure(Throwable error, IResource resource) {
String location = resource.getLocation().toOSString();
String msg = String.format("Unable to process resource %s", location); //$NON-NLS-1$
CodanCorePlugin.log(msg, error);
}
/** {@inheritDoc} */
@Override
public void reportProblem(IProblemLocation location, String description) {
String severity = null;
reportProblem(location, description, severity);
}
/** {@inheritDoc} */
@Override
public void reportProblem(IProblemLocation location, String description, String severity) {
super.reportProblem(getReferenceProblemId(), location, description);
}
/**
* Returns the id of the problem used as reference to obtain this checker's preferences. All
* preferences in a external-tool-based checker are shared among its defined problems.
* @return the id of the problem used as reference to obtain this checker's preferences.
*/
protected abstract String getReferenceProblemId();
/**
* Initializes the preferences of the given problem. All preferences in a external-tool-based
* checker are shared among its defined problems.
* @param problem the given problem.
*/
@Override
public void initPreferences(IProblemWorkingCopy problem) {
super.initPreferences(problem);
addPreference(problem, settings.getPath());
addPreference(problem, settings.getArgs());
addPreference(problem, settings.getShouldDisplayOutput());
}
private void addPreference(IProblemWorkingCopy problem, SingleConfigurationSetting<?> setting) {
IProblemPreference descriptor = (IProblemPreference) setting.getDescriptor();
addPreference(problem, descriptor, setting.getDefaultValue());
}
/** {@inheritDoc} */
@Override
protected void setDefaultPreferenceValue(IProblemWorkingCopy problem, String key,
Object defaultValue) {
MapProblemPreference map = getTopLevelPreference(problem);
map.setChildValue(key, defaultValue);
}
/** {@inheritDoc} */
@Override
public RootProblemPreference getTopLevelPreference(IProblem problem) {
RootProblemPreference map = (RootProblemPreference) problem.getPreference();
if (map == null) {
map = preferences;
if (problem instanceof IProblemWorkingCopy) {
((IProblemWorkingCopy) problem).setPreference(map);
}
}
return map;
}
}

View file

@ -1,36 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
/**
* Parses the output of an external tool.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public abstract class AbstractOutputParser {
/**
* Parses one line of output. Implementations are free to create markers from the information
* retrieved from the parsed output.
* @param line the line to parse.
* @return {@code true} if the line was successfully parsed; {@code false} otherwise.
* @throws InvocationFailure if the output indicates that the invocation of the external tool
* failed.
*/
public abstract boolean parse(String line) throws InvocationFailure;
/**
* Resets the value of this parser, usually after the execution of the external tool is
* finished.
*/
public abstract void reset();
}

View file

@ -1,95 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
import org.eclipse.cdt.codan.internal.core.externaltool.ArgsSetting;
import org.eclipse.cdt.codan.internal.core.externaltool.PathSetting;
import org.eclipse.cdt.codan.internal.core.externaltool.ConsoleOutputSetting;
import java.io.File;
/**
* User-configurable external tool settings.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public final class ConfigurationSettings {
private final PathSetting path;
private final ArgsSetting args;
private final ConsoleOutputSetting shouldDisplayOutput;
private final String externalToolName;
/**
* Constructor.
* @param externalToolName the name of the external tool, to be displayed to the user.
* @param defaultPath the default path of the external tool.
* @param defaultArgs the default arguments to pass when invoking the external tool.
* @param defaultShouldDisplayOutput indicates whether output of an external tool should be
* displayed in an Eclipse console.
*/
public ConfigurationSettings(String externalToolName, File defaultPath, String defaultArgs,
boolean defaultShouldDisplayOutput) {
this.externalToolName = externalToolName;
this.path = new PathSetting(externalToolName, defaultPath);
this.args = new ArgsSetting(externalToolName, defaultArgs);
this.shouldDisplayOutput = new ConsoleOutputSetting(defaultShouldDisplayOutput);
}
/**
* Returns the name of the external tool, to be displayed to the user.
* @return the name of the external tool, to be displayed to the user.
*/
public String getExternalToolName() {
return externalToolName;
}
/**
* Returns the setting that specifies the path and name of the external tool to invoke.
* @return the setting that specifies the path and name of the external tool to invoke.
*/
public SingleConfigurationSetting<File> getPath() {
return path;
}
/**
* Returns the setting that specifies the arguments to pass when invoking the external tool.
* @return the setting that specifies the arguments to pass when invoking the external tool.
*/
public SingleConfigurationSetting<String> getArgs() {
return args;
}
/**
* Returns the setting that specifies whether the output of the external tools should be
* displayed in an Eclipse console.
* @return the shouldDisplayOutput the setting that specifies whether the output of the external
* tools should be displayed in an Eclipse console.
*/
public SingleConfigurationSetting<Boolean> getShouldDisplayOutput() {
return shouldDisplayOutput;
}
/**
* Updates the values of the configuration settings value with the ones stored in the given
* preference map.
* @param preferences the given preference map that may contain the values to set.
* @throws ClassCastException if any of the values to set is not of the same type as the one
* supported by a setting.
*/
public void updateValuesFrom(MapProblemPreference preferences) {
path.updateValue(preferences);
args.updateValue(preferences);
shouldDisplayOutput.updateValue(preferences);
}
}

View file

@ -1,29 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
/**
* Parses a given {@code String} containing the arguments to pass to an external tool and separates
* them into individual values.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public interface IArgsSeparator {
/**
* Parses a given {@code String} containing the arguments to pass to an external tool and
* separates them into individual values.
* @param args contains the arguments to pass to the external tool executable.
* @return the separated argument values.
*/
String[] separateArgs(String args);
}

View file

@ -1,45 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
/**
* Prints the output of an external tool to an Eclipse console. It uses the name of the external
* tool as the console ID.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public interface IConsolePrinter {
/**
* Clears the contents of the console.
*/
public void clear();
/**
* Prints the specified message to the console, followed by a line separator string.
* @param message the message to print.
*/
public void println(String message);
/**
* Prints a line separator to the console.
*/
public void println();
/**
* Closes the output stream of the console.
*/
public void close();
}

View file

@ -1,33 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
/**
* Creates or finds an Eclipse console that uses the name of an external tool as its own.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public interface IConsolePrinterProvider {
/**
* Creates an Eclipse console that uses the name of an external tool as its own.
* @param externalToolName the name of the external tool that will be used as the name of the
* console.
* @param shouldDisplayOutput indicates whether the user wants to see the output of the external
* tool in the console.
* @return the created or found console.
*/
public IConsolePrinter createConsole(String externalToolName, boolean shouldDisplayOutput);
}

View file

@ -1,30 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.core.resources.IResource;
/**
* Provides the parameters to pass when invoking an external tool.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public interface IInvocationParametersProvider {
/**
* Creates the parameters to pass when invoking an external tool.
* @param fileToProcess the file to process.
* @return the created parameters.
* @throws Throwable if something goes wrong.
*/
InvocationParameters createParameters(IResource fileToProcess) throws Throwable;
}

View file

@ -1,40 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
/**
* Reports problems found in code, reported by an external tool.
*
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public interface IProblemDisplay {
/**
* Reports a problem found by an external tool.
* @param location the problem's location (e.g. file and line number.)
* @param description the description of the problem.
*/
public void reportProblem(IProblemLocation location, String description);
/**
* Reports a problem found by an external tool.
* @param location the problem's location (e.g. file and line number.)
* @param description the description of the problem.
* @param severity the problem's severity (e.g. "error", "warning", etc.)
*/
public void reportProblem(IProblemLocation location, String description, String severity);
}

View file

@ -1,40 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
/**
* Indicates that invocation of an external tool failed.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public class InvocationFailure extends Exception {
private static final long serialVersionUID = 6727101323050538885L;
/**
* Constructor.
* @param message the detail message.
*/
public InvocationFailure(String message) {
super(message);
}
/**
* Constructor.
* @param message the detail message.
* @param cause the cause (which is saved for later retrieval by
* the <code>{@link #getCause()}</code> method.)
*/
public InvocationFailure(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -1,90 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
/**
* Parameters to pass when invoking an external tool.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public final class InvocationParameters {
private final IResource originalFile;
private final IResource actualFile;
private final String actualFilePath;
private final IPath workingDirectory;
/**
* Constructor.
* @param originalFile the original file to process.
* @param actualFile the actual file to process.
* @param actualFilePath the path of {@code actual}, in a format that the external tool can
* understand.
* @param workingDirectory the directory where the external tool should be executed.
* @see #getOriginalFile()
* @see #getActualFile()
*/
public InvocationParameters(IResource originalFile, IResource actualFile, String actualFilePath,
IPath workingDirectory) {
this.originalFile = originalFile;
this.actualFile = actualFile;
this.actualFilePath = actualFilePath;
this.workingDirectory = workingDirectory;
}
/**
* Returns the original file to process. This is the file that triggered execution of
* a command-line tool when saved.
* @return the original file to process.
*/
public IResource getOriginalFile() {
return originalFile;
}
/**
* Returns the actual file to process. It may not be the same as
* <code>{@link #getOriginalFile()}</code>, depending on how the external tool works.
* <p>
* A good example is an external tool that can only process C++ source files but not header
* files. If the <em>original</em> file is a header file, the checker could potentially find
* a C++ file that includes such header and use it as the <em>actual</em> file to process.
* </p>
* <p>
* We still need to keep a reference to the <em>actual</em> file, in order to add markers to
* the editor in case of problems found.
* </p>
* @return the actual file to process.
*/
public IResource getActualFile() {
return actualFile;
}
/**
* Returns the path of <code>{@link #getActualFile()}</code>, in a format the external tool can
* understand.
* @return the path of the <em>actual</em> file to process.
*/
public String getActualFilePath() {
return actualFilePath;
}
/**
* Returns the directory where the external tool should be executed.
* @return the directory where the external tool should be executed.
*/
public IPath getWorkingDirectory() {
return workingDirectory;
}
}

View file

@ -1,40 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.core.resources.IResource;
/**
* Default implementation of <code>{@link InvocationParameters}</code>
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public class InvocationParametersProvider implements IInvocationParametersProvider {
/**
* Creates the parameters to pass when invoking an external tool.
* <p>
* In this implementation:
* <ul>
* <li>the <em>actual</em> file to process is the same as the <em>original</em> file</li>
* <li>the path of the actual file is its absolute path in the file system</li>
* <li>the working directory is {@code null}</li>
* </ul>
* @param fileToProcess the file to process.
* @return the created parameters.
*/
@Override
public InvocationParameters createParameters(IResource fileToProcess) {
String path = fileToProcess.getLocation().toOSString();
return new InvocationParameters(fileToProcess, fileToProcess, path, null);
}
}

View file

@ -1,82 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import org.eclipse.cdt.codan.core.param.IProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor;
import org.eclipse.cdt.codan.core.param.MapProblemPreference;
/**
* Single external tool configuration setting.
* @param <T> the type of the value this setting stores.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @noextend This class is not intended to be extended by clients.
* @since 2.1
*/
public class SingleConfigurationSetting<T> {
private final IProblemPreferenceDescriptor descriptor;
private final T defaultValue;
private final Class<T> valueType;
private T value;
/**
* Constructor.
* @param descriptor meta-data that tells the UI how to display this setting.
* @param defaultValue the setting's default value.
* @param valueType the type of the value to store (used for safe casting.)
*/
public SingleConfigurationSetting(IProblemPreferenceDescriptor descriptor, T defaultValue,
Class<T> valueType) {
this.descriptor = descriptor;
this.defaultValue = defaultValue;
this.valueType = valueType;
}
/**
* Returns this setting's value.
* @return this setting's value.
*/
public T getValue() {
return value;
}
/**
* Returns the meta-data that tells the UI how to display this setting.
* @return the meta-data that tells the UI how to display this setting.
*/
public IProblemPreferenceDescriptor getDescriptor() {
return descriptor;
}
/**
* Returns this setting's default value.
* @return this setting's default value.
*/
public T getDefaultValue() {
return defaultValue;
}
/**
* Updates this setting's value with the one stored in the given preference map.
* @param preferences the given preference map that may contain the value to set.
* @throws ClassCastException if the value to set is not of the same type as the one supported
* by this setting.
*/
public void updateValue(MapProblemPreference preferences) {
IProblemPreference childDescriptor = preferences.getChildDescriptor(descriptor.getKey());
if (childDescriptor != null) {
value = valueType.cast(childDescriptor.getValue());
}
}
}

View file

@ -1,34 +0,0 @@
/*******************************************************************************
* 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.core.externaltool;
import java.util.regex.Pattern;
/**
* Default implementation of <code>{@link IArgsSeparator}</code> that uses an empty space as
* the delimiter to separate the arguments to pass to an external tool.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public final class SpaceArgsSeparator implements IArgsSeparator {
private static final String[] NO_ARGS = {};
private static final Pattern EMPTY_SPACE_PATTERN = Pattern.compile("\\s+"); //$NON-NLS-1$
@Override
public String[] separateArgs(String args) {
if (args == null || args.isEmpty()) {
return NO_ARGS;
}
return EMPTY_SPACE_PATTERN.split(args);
}
}

View file

@ -1,28 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Google, Inc.
* 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 - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.core.param;
/**
* Preferences that can be shared among several problems.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 2.1
*/
public class SharedRootProblemPreference extends RootProblemPreference {
@Override
public Object clone() {
SharedRootProblemPreference map = (SharedRootProblemPreference) super.clone();
// alruiz: sharing the internal hash is the only way I could make this work.
map.hash = hash;
return map;
}
}

View file

@ -1,42 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import static org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType.TYPE_STRING;
import static org.eclipse.cdt.codan.internal.core.externaltool.Messages.ConfigurationSettings_args_format;
import org.eclipse.cdt.codan.core.externaltool.SingleConfigurationSetting;
import org.eclipse.cdt.codan.core.param.BasicProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor;
/**
* User-configurable setting that specifies the arguments to pass when invoking the external tool.
* The arguments are stored in a single {@code String}.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class ArgsSetting extends SingleConfigurationSetting<String> {
static final String KEY = "externalToolArgs"; //$NON-NLS-1$
/**
* Constructor.
* @param externalToolName the name of the external tool, to be displayed to the user.
* @param defaultValue the default value of the setting.
*/
public ArgsSetting(String externalToolName, String defaultValue) {
super(newPreferenceDescriptor(externalToolName), defaultValue, String.class);
}
private static IProblemPreferenceDescriptor newPreferenceDescriptor(String externalToolName) {
String label = String.format(ConfigurationSettings_args_format, externalToolName);
return new BasicProblemPreference(KEY, label, TYPE_STRING);
}
}

View file

@ -1,41 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import static org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType.TYPE_BOOLEAN;
import static org.eclipse.cdt.codan.internal.core.externaltool.Messages.ConfigurationSettings_should_display_output;
import org.eclipse.cdt.codan.core.externaltool.SingleConfigurationSetting;
import org.eclipse.cdt.codan.core.param.BasicProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor;
/**
* User-configurable setting that specifies whether the output of an external tool should be
* displayed in an Eclipse console.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class ConsoleOutputSetting extends SingleConfigurationSetting<Boolean> {
static final String KEY = "externalToolShouldDisplayOutput"; //$NON-NLS-1$
/**
* Constructor.
* @param defaultValue the default value of the setting.
*/
public ConsoleOutputSetting(boolean defaultValue) {
super(newPreferenceDescriptor(), defaultValue, Boolean.class);
}
private static IProblemPreferenceDescriptor newPreferenceDescriptor() {
String label = ConfigurationSettings_should_display_output;
return new BasicProblemPreference(KEY, label, TYPE_BOOLEAN);
}
}

View file

@ -1,176 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinter;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinterProvider;
import org.eclipse.cdt.codan.core.externaltool.AbstractOutputParser;
import org.eclipse.cdt.codan.core.externaltool.ConfigurationSettings;
import org.eclipse.cdt.codan.core.externaltool.IArgsSeparator;
import org.eclipse.cdt.codan.core.externaltool.InvocationFailure;
import org.eclipse.cdt.codan.core.externaltool.InvocationParameters;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
/**
* Invokes an external tool to perform checks on a single file.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class ExternalToolInvoker {
private final IConsolePrinterProvider consolePrinterProvider;
private final ProcessInvoker processInvoker;
/**
* Constructor.
* @param consolePrinterProvider creates an Eclipse console that uses the name of an external
* tool as its own.
*/
public ExternalToolInvoker(IConsolePrinterProvider consolePrinterProvider) {
this(consolePrinterProvider, new ProcessInvoker());
}
/**
* Constructor, for testing purposes only.
* @param consolePrinterProvider creates an Eclipse console that uses the name of an external
* tool as its own.
* @param processInvoker executes a command in a separate process.
*/
public ExternalToolInvoker(IConsolePrinterProvider consolePrinterProvider,
ProcessInvoker processInvoker) {
this.consolePrinterProvider = consolePrinterProvider;
this.processInvoker = processInvoker;
}
/**
* Invokes an external tool.
*
* @param parameters the parameters to pass to the external tool executable.
* @param configurationSettings user-configurable settings.
* @param argsSeparator separates the arguments to pass to the external tool executable. These
* arguments are stored in a single {@code String}.
* @param parsers parse the output of the external tool.
* @throws InvocationFailure if the external tool reports that it cannot be executed.
* @throws Throwable if the external tool cannot be launched.
*/
public void invoke(InvocationParameters parameters, ConfigurationSettings configurationSettings,
IArgsSeparator argsSeparator, List<AbstractOutputParser> parsers)
throws InvocationFailure, Throwable {
IPath executablePath = executablePath(configurationSettings);
String[] args = argsToPass(parameters, configurationSettings, argsSeparator);
boolean shouldDisplayOutput = configurationSettings.getShouldDisplayOutput().getValue();
try {
buildAndLaunchCommand(configurationSettings.getExternalToolName(), executablePath, args,
parameters.getWorkingDirectory(), shouldDisplayOutput, parsers);
} finally {
reset(parsers);
}
}
private IPath executablePath(ConfigurationSettings configurationSettings) {
File executablePath = configurationSettings.getPath().getValue();
return new Path(executablePath.toString());
}
private String[] argsToPass(InvocationParameters parameters,
ConfigurationSettings configurationSettings, IArgsSeparator argsSeparator) {
String actualFilePath = parameters.getActualFilePath();
String[] args = configuredArgs(configurationSettings, argsSeparator);
return addFilePathToArgs(actualFilePath, args);
}
private String[] configuredArgs(ConfigurationSettings configurationSettings,
IArgsSeparator argsSeparator) {
String args = configurationSettings.getArgs().getValue();
return argsSeparator.separateArgs(args);
}
private String[] addFilePathToArgs(String actualFilePath, String[] configuredArgs) {
int argCount = configuredArgs.length;
String[] allArgs = new String[argCount + 1];
// alruiz: Arrays.copyOf leaves empty cells at the end. We need an empty cell at the
// beginning of the array.
System.arraycopy(configuredArgs, 0, allArgs, 1, argCount);
// add file to process as the first argument
allArgs[0] = actualFilePath;
return allArgs;
}
private void buildAndLaunchCommand(String externalToolName, IPath executablePath, String[] args,
IPath workingDirectory, boolean shouldDisplayOutput, List<AbstractOutputParser> parsers)
throws InvocationFailure, Throwable {
String command = buildCommand(executablePath, args);
Process process = null;
IConsolePrinter console =
consolePrinterProvider.createConsole(externalToolName, shouldDisplayOutput);
try {
console.clear();
console.println(command);
console.println();
process = processInvoker.invoke(command, workingDirectory);
processStream(process.getInputStream(), parsers, console);
processStream(process.getErrorStream(), parsers, console);
} finally {
if (process != null) {
process.destroy();
}
console.close();
}
}
private String buildCommand(IPath executablePath, String[] args) {
StringBuilder builder = new StringBuilder();
builder.append(executablePath.toOSString());
for (String arg : args) {
builder.append(" ").append(arg); //$NON-NLS-1$
}
return builder.toString();
}
private void processStream(InputStream inputStream, List<AbstractOutputParser> parsers,
IConsolePrinter console) throws IOException, InvocationFailure {
Reader reader = null;
try {
reader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
console.println(line);
for (AbstractOutputParser parser : parsers) {
if (parser.parse(line)) {
break;
}
}
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}
private void reset(List<AbstractOutputParser> parsers) {
for (AbstractOutputParser parser : parsers) {
parser.reset();
}
}
}

View file

@ -1,29 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import org.eclipse.osgi.util.NLS;
/**
* @author alruiz@google.com (Alex Ruiz)
*/
public class Messages extends NLS {
public static String ConfigurationSettings_args_format;
public static String ConfigurationSettings_path_format;
public static String ConfigurationSettings_should_display_output;
static {
NLS.initializeMessages(Messages.class.getName(), Messages.class);
}
private Messages() {
}
}

View file

@ -1,13 +0,0 @@
###############################################################################
# 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
###############################################################################
ConfigurationSettings_args_format=%s Args:
ConfigurationSettings_path_format=%s Path:
ConfigurationSettings_should_display_output=Display Output in Console

View file

@ -1,43 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import static org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor.PreferenceType.TYPE_FILE;
import static org.eclipse.cdt.codan.internal.core.externaltool.Messages.ConfigurationSettings_path_format;
import org.eclipse.cdt.codan.core.externaltool.SingleConfigurationSetting;
import org.eclipse.cdt.codan.core.param.BasicProblemPreference;
import org.eclipse.cdt.codan.core.param.IProblemPreferenceDescriptor;
import java.io.File;
/**
* User-configurable setting that specifies the path and name of an external tool's executable.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class PathSetting extends SingleConfigurationSetting<File> {
static final String KEY = "externalToolPath"; //$NON-NLS-1$
/**
* Constructor.
* @param externalToolName the name of the external tool, to be displayed to the user.
* @param defaultValue the default value of the setting.
*/
public PathSetting(String externalToolName, File defaultValue) {
super(newPreferenceDescriptor(externalToolName), defaultValue, File.class);
}
private static IProblemPreferenceDescriptor newPreferenceDescriptor(String externalToolName) {
String label = String.format(ConfigurationSettings_path_format, externalToolName);
return new BasicProblemPreference(KEY, label, TYPE_FILE);
}
}

View file

@ -1,46 +0,0 @@
/*******************************************************************************
* 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.internal.core.externaltool;
import org.eclipse.cdt.codan.core.externaltool.InvocationFailure;
import org.eclipse.core.runtime.IPath;
import java.io.IOException;
/**
* Executes a command in a separate process.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class ProcessInvoker {
private static final String[] ENVIRONMENT_VARIABLE_SETTINGS = {};
private static final String ERROR_FORMAT = "Unable to invoke command '%s'"; //$NON-NLS-1$
/**
* Invokes the given command in a separate process.
* @param command the command to execute.
* @param workingDirectory the working directory of the process, or {@code null} should inherit
* the working directory of the current process.
* @return the process where the given command is being executed.
* @throws InvocationFailure if the command fails to be executed.
*/
public Process invoke(String command, IPath workingDirectory) throws InvocationFailure {
try {
Runtime runtime = Runtime.getRuntime();
if (workingDirectory == null) {
return runtime.exec(command);
}
return runtime.exec(command, ENVIRONMENT_VARIABLE_SETTINGS, workingDirectory.toFile());
} catch (IOException e) {
throw new InvocationFailure(String.format(ERROR_FORMAT, command), e);
}
}
}

View file

@ -19,5 +19,4 @@ Require-Bundle: org.eclipse.ui,
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.codan.internal.ui.cxx;x-internal:=true,
org.eclipse.cdt.codan.ui,
org.eclipse.cdt.codan.ui.cxx.externaltool
org.eclipse.cdt.codan.ui

View file

@ -1,97 +0,0 @@
/*******************************************************************************
* 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.ui.cxx.externaltool;
import org.eclipse.cdt.codan.core.cxx.util.FileTypes;
import org.eclipse.cdt.codan.core.externaltool.AbstractExternalToolBasedChecker;
import org.eclipse.cdt.codan.core.externaltool.ConfigurationSettings;
import org.eclipse.cdt.codan.core.externaltool.IArgsSeparator;
import org.eclipse.cdt.codan.core.externaltool.IInvocationParametersProvider;
import org.eclipse.cdt.codan.core.externaltool.InvocationParametersProvider;
import org.eclipse.cdt.codan.core.externaltool.SpaceArgsSeparator;
import org.eclipse.cdt.codan.internal.ui.externaltool.ConsolePrinterProvider;
import org.eclipse.cdt.codan.ui.CodanEditorUtility;
import org.eclipse.cdt.codan.ui.cxx.util.CEditors;
import org.eclipse.core.resources.IResource;
import org.eclipse.ui.editors.text.TextEditor;
/**
* Base class for checkers that invoke external command-line tools to perform code checking
* on C++ files.
* <p>
* A file, to be processed by this type of checker, must:
* <ol>
* <li>a C++ file</li>
* <li>be in the current active editor</li>
* <li>not have any unsaved changes</li>
* </ol>
* </p>
* By default, implementations of this checker are not allowed to run while the user types, since
* external tools cannot see unsaved changes.
*
* @author alruiz@google.com (Alex Ruiz)
*
* @since 3.0
*/
public abstract class AbstractCxxExternalToolBasedChecker extends AbstractExternalToolBasedChecker {
/**
* Constructor
* @param configurationSettings user-configurable external tool configuration settings.
*/
public AbstractCxxExternalToolBasedChecker(ConfigurationSettings configurationSettings) {
this(new InvocationParametersProvider(), new SpaceArgsSeparator(), configurationSettings);
}
/**
* Constructor.
* @param parametersProvider provides the parameters to pass when invoking the external tool.
* @param argsSeparator separates the arguments to pass to the external tool executable. These
* arguments are stored in a single {@code String}.
* @param configurationSettings user-configurable external tool configuration settings.
*/
public AbstractCxxExternalToolBasedChecker(IInvocationParametersProvider parametersProvider,
IArgsSeparator argsSeparator, ConfigurationSettings configurationSettings) {
super(parametersProvider, argsSeparator, new ConsolePrinterProvider(),
configurationSettings);
}
/**
* Indicates whether the external tool is capable of processing the given
* <code>{@link IResource}</code>.
* <p>
* The minimum requirements that the given {@code IResource} should satisfy are:
* <ul>
* <li>should be C/C++ file</li>
* <li>should be displayed in the current active {@code CEditor}</li>
* <li>should not have any unsaved changes</li>
* </ul>
* </p>
* @param resource the given {@code IResource}.
* @return {@code true} if the external tool is capable of processing the given file,
* {@code false} otherwise.
*/
@Override
public boolean enabledInContext(IResource resource) {
return isFileOfSupportedType(resource) && isOpenInActiveCleanEditor(resource);
}
private boolean isFileOfSupportedType(IResource resource) {
return FileTypes.isCppFile(resource) || FileTypes.isHeaderFile(resource);
}
private boolean isOpenInActiveCleanEditor(IResource resource) {
TextEditor editor = CEditors.activeCEditor();
if (editor == null) {
return false;
}
return !editor.isDirty() && CodanEditorUtility.isResourceOpenInEditor(resource, editor);
}
}

View file

@ -1,45 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Google, Inc.
* 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 - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.ui.cxx.util;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
/**
* Utility methods related to <code>{@link CEditor}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@SuppressWarnings("restriction") // CEditor is internal API
public final class CEditors {
/**
* Finds the current {@code CEditor}.
* @return the current {@code CEditor}, or {@code null} if one cannot be found.
*/
public static TextEditor activeCEditor() {
IWorkbench workbench = PlatformUI.getWorkbench();
for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) {
IWorkbenchPage activePage = w.getActivePage();
IEditorPart editor = activePage.getActiveEditor();
if (editor instanceof CEditor) {
return (CEditor) editor;
}
}
return null;
}
private CEditors() {}
}

View file

@ -20,7 +20,6 @@ Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.internal.ui.actions;x-friends:="org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.internal.ui.dialogs;x-friends:="org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.internal.ui.externaltool;x-friends:="org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.internal.ui.preferences;x-friends:="org.eclipse.cdt.codan.ui.cxx",
org.eclipse.cdt.codan.internal.ui.views;x-internal:=true,
org.eclipse.cdt.codan.internal.ui.widgets;x-internal:=true,

View file

@ -1,54 +0,0 @@
/*******************************************************************************
* 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.internal.ui.externaltool;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinter;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import java.io.IOException;
/**
* Default implementation of <code>{@link IConsolePrinter}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
class ConsolePrinter implements IConsolePrinter {
private final MessageConsole console;
private final MessageConsoleStream out;
ConsolePrinter(MessageConsole console) {
this.console = console;
out = console.newMessageStream();
}
@Override
public void clear() {
console.clearConsole();
}
@Override
public void println(String s) {
out.println(s);
}
@Override
public void println() {
out.println();
}
@Override
public void close() {
try {
out.close();
} catch (IOException ignored) {}
}
}

View file

@ -1,68 +0,0 @@
/*******************************************************************************
* 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.internal.ui.externaltool;
import static org.eclipse.ui.console.IConsoleConstants.ID_CONSOLE_VIEW;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinter;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinterProvider;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.codan.ui.CodanEditorUtility;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.MessageConsole;
/**
* Default implementation of <code>{@link IConsolePrinterProvider}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public class ConsolePrinterProvider implements IConsolePrinterProvider {
private static final NullConsolePrinter NULL_CONSOLE = new NullConsolePrinter();
@Override
public IConsolePrinter createConsole(String externalToolName, boolean shouldDisplayOutput) {
if (shouldDisplayOutput) {
try {
return createOrFindConsole(externalToolName);
} catch (Throwable e) {
CodanUIActivator.log("Unable to create/find console", e); //$NON-NLS-1$
}
}
return NULL_CONSOLE;
}
private IConsolePrinter createOrFindConsole(String externalToolName) throws PartInitException {
MessageConsole console = findConsole(externalToolName);
IWorkbenchPage page = CodanEditorUtility.getActivePage();
if (page != null) {
IConsoleView view = (IConsoleView) page.showView(ID_CONSOLE_VIEW);
view.display(console);
}
return new ConsolePrinter(console);
}
private static MessageConsole findConsole(String externalToolName) {
IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
for (IConsole console : consoleManager.getConsoles()) {
if (externalToolName.equals(console.getName()) && console instanceof MessageConsole) {
return (MessageConsole) console;
}
}
MessageConsole console = new MessageConsole(externalToolName, null);
consoleManager.addConsoles(new IConsole[] { console });
return console;
}
}

View file

@ -1,36 +0,0 @@
/*******************************************************************************
* 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.internal.ui.externaltool;
import org.eclipse.cdt.codan.core.externaltool.IConsolePrinter;
/**
* No-op implementation of <code>{@link IConsolePrinter}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
class NullConsolePrinter implements IConsolePrinter {
@Override
public void clear() {
}
@Override
public void println(String message) {
}
@Override
public void println() {
}
@Override
public void close() {
}
}