Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.codechecker.eclipse.plugin.codechecker;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.locator.InvalidCodeCheckerException;
import org.codechecker.eclipse.plugin.config.CcConfigurationBase;
import org.codechecker.eclipse.plugin.config.Config.ConfigTypes;
import org.codechecker.eclipse.plugin.runtime.LogI;
import org.codechecker.eclipse.plugin.runtime.SLogger;
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;
import org.eclipse.core.runtime.IProgressMonitor;

import com.google.common.base.Optional;

/**
* Internal representation of a CodeChecker package.
*/
public class CodeChecker implements ICodeChecker {

private Path location;
private ShellExecutorHelper she;

/**
*
* @param path
* Path to the binary itself.
* @param she
* The ShellExecutor to be used.
*
* @throws InvalidCodeCheckerException
* Thrown when no CodeChecker found.
*/
public CodeChecker(Path path, ShellExecutorHelper she) throws InvalidCodeCheckerException {
location = path;
this.she = she;
getVersion();
}

@Override
public String getCheckers() {
String cmd = location.toAbsolutePath().toString() + " checkers";
Optional<String> ccOutput = she.waitReturnOutput(cmd, false);
return ccOutput.or("No Checkers found");
}

@Override
public String getVersion() throws InvalidCodeCheckerException {
String cmd = location.toAbsolutePath().toString() + " version";
Optional<String> ccOutput = she.waitReturnOutput(cmd, false);
if (!ccOutput.isPresent() || ccOutput.get().isEmpty())
throw new InvalidCodeCheckerException("Couldn't run CodeChecker version!");
return ccOutput.get();
}

@Override
public Path getLocation() {
return location;
}

@Override
public String analyze(Path logFile, boolean logToConsole, IProgressMonitor monitor, int taskCount,
CcConfigurationBase config) {

String cmd = location.toAbsolutePath().toString() + " analyze " + config.get(ConfigTypes.CHECKER_LIST) + " -j "
+ config.get(ConfigTypes.ANAL_THREADS) + " -n javarunner" + " -o "
+ logFile.toAbsolutePath().getParent().toString() + "/results/ "
+ logFile.toAbsolutePath().toString();

SLogger.log(LogI.INFO, "Running analyze Command: " + cmd);
Optional<String> ccOutput = she.progressableWaitReturnOutput(cmd, logToConsole, monitor, taskCount);

return ccOutput.or("");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.codechecker.eclipse.plugin.codechecker;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.locator.InvalidCodeCheckerException;
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;

/**
* Implementation of the {@link ICodeCheckerFactory} interface.
*/
public class CodeCheckerFactory implements ICodeCheckerFactory {
@Override
public ICodeChecker createCodeChecker(Path pathToBin, ShellExecutorHelper she)
throws InvalidCodeCheckerException {
return new CodeChecker(pathToBin, she);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.codechecker.eclipse.plugin.codechecker;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.locator.InvalidCodeCheckerException;
import org.codechecker.eclipse.plugin.config.CcConfigurationBase;
import org.eclipse.core.runtime.IProgressMonitor;

/**
* Interface representing a CodeChecker package.
*/
public interface ICodeChecker {
/**
* Returns the unformatted output of the CodeChecker checkers command.
*
* @return The checker list.
*/
public String getCheckers();

/**
* Returns the full and complete version string of the CodeChecker package.
*
* @return The version String.
* @throws InvalidCodeCheckerException
* Thrown when no version string can be returned.
*/
public String getVersion() throws InvalidCodeCheckerException;

/**
* To get the location of the CodeChecker binary.
*
* @return The path.
*/
public Path getLocation();

/**
* Executes CodeChecker check command on the build log received in the fileName
* parameter.
*
* @param logFile
* A Path to the build log in the followin format:
* http://clang.llvm.org/docs/JSONCompilationDatabase.html .
* @param logToConsole
* Flag for indicating console logging.
* @param monitor
* ProgressMonitor for to be able to increment progress bar.
* @param taskCount
* How many analyze step to be taken.
* @param config
* The configuration being used.
* @return CodeChecker The full analyze command output.
*/
public String analyze(Path logFile, boolean logToConsole, IProgressMonitor monitor, int taskCount,
CcConfigurationBase config);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.codechecker.eclipse.plugin.codechecker;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.locator.InvalidCodeCheckerException;
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;

/**
* Interface for CodeChecker factory.
*/
public interface ICodeCheckerFactory {
/**
* Method for creating CodeChecker instances.
*
* @param pathToBin
* Path to the CodeChecker binary. (Not to root!)
* @param she
* The shell executor helper that will be used.
* @return A newly created {@link ICodeChecker} ICodeChecker instance.
* @throws InvalidCodeCheckerException
* Thrown when a new instance couldn't be created.
*/
public ICodeChecker createCodeChecker(Path pathToBin, ShellExecutorHelper she)
throws InvalidCodeCheckerException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

/**
* Class responsible to create {@link CodeCheckerLocatorService} instances.
*/
public class CodeCheckerLocatorFactory {
/**
* Returns a {@link CodeCheckerLocatorService} depending on a the input
* parameter.
*
* @param t
* Any of the {@link ResolutionMethodTypes} enum values.
* @return A {@link CodeCheckerLocatorService} instance.
*/
public CodeCheckerLocatorService create(ResolutionMethodTypes t) {
switch (t) {
case PATH:
return new EnvCodeCheckerLocatorService();
case PRE:
return new PreBuiltCodeCheckerLocatorService();
case PY:
return new CustomBuiltCodeCheckerLocatorService();
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.ICodeChecker;
import org.codechecker.eclipse.plugin.codechecker.ICodeCheckerFactory;
import org.codechecker.eclipse.plugin.runtime.IShellExecutorHelperFactory;

/**
* Implementations of this interface should return the location of the
* CodeChecker package.
*/
public abstract class CodeCheckerLocatorService {
/**
* @param pathToBin
* Path to CodeChecker package root.
* @param pathToVenv
* Path to the root of the virtual environment.
* @param ccfactory
* An {@link ICodeCheckerFactory} that will create the CodeChecker.
* @param sheFactory
* A {@link IShellExecutorHelperFactory} to be used.
* @return A CodeChecker Instance.
* @throws InvalidCodeCheckerException
* Thrown when the {@link ICodeChecker} instantiation fails.
*/
public abstract ICodeChecker findCodeChecker(Path pathToBin, Path pathToVenv, ICodeCheckerFactory ccfactory,
IShellExecutorHelperFactory sheFactory)
throws InvalidCodeCheckerException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.codechecker.eclipse.plugin.codechecker.ICodeChecker;
import org.codechecker.eclipse.plugin.codechecker.ICodeCheckerFactory;
import org.codechecker.eclipse.plugin.runtime.IShellExecutorHelperFactory;
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;

import com.google.common.base.Optional;

/**
* Provides a CodeChecker instance which is tied to a Custom built CodeChecker
* package.
*/
public class CustomBuiltCodeCheckerLocatorService extends CodeCheckerLocatorService {
public static final String ERROR = "Couldn't find CodeChecker at the given destination!";
public static final String ENV_ERROR = "There was an error when reading the given python environment!";

@Override
public ICodeChecker findCodeChecker(Path pathToBin, Path pathToVenv,
ICodeCheckerFactory ccfactory, IShellExecutorHelperFactory sheFactory) throws InvalidCodeCheckerException {
ShellExecutorHelper she = sheFactory.createShellExecutorHelper(System.getenv());
Optional<String> output = she
.quickReturnOutput(". " + pathToVenv.toAbsolutePath().toString() + "/bin/activate ; env");
if (!output.isPresent())
throw new IllegalArgumentException();
try {
Map<String, String> env = parseEnvironment(output.get());
ShellExecutorHelper pyShe = sheFactory.createShellExecutorHelper(env);
return ccfactory.createCodeChecker(pathToBin, pyShe);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(ENV_ERROR);
} catch (InvalidCodeCheckerException e) {
throw new InvalidCodeCheckerException(ERROR);
}
}

/**
* Returns a String - String map from a raw "Key=Value\n..." String.
* @param envOutput The String input.
* @return A Map<String,String> containing all the environment variables.
*/
private Map<String, String> parseEnvironment(String envOutput){
Map<String, String> map = Stream.of(envOutput.split("\n")).map(String::trim).map(line -> line.split("="))
.collect(Collectors.toMap(k -> k[0], v -> v.length < 2 ? "" : v[1]));
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.codechecker.eclipse.plugin.codechecker.ICodeChecker;
import org.codechecker.eclipse.plugin.codechecker.ICodeCheckerFactory;
import org.codechecker.eclipse.plugin.runtime.IShellExecutorHelperFactory;
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;

/**
* Provides a CodeChecker instance tied to a CodeChecker package resulting of
* "which CodeChecker".
*/
public class EnvCodeCheckerLocatorService extends CodeCheckerLocatorService {
public static final String ERROR = "CodeChecker wasn't found in PATH environment variable!";

@Override
public ICodeChecker findCodeChecker(Path path, Path pathToVenv,
ICodeCheckerFactory ccFactory, IShellExecutorHelperFactory sheFactory) throws InvalidCodeCheckerException {

ShellExecutorHelper she = sheFactory.createShellExecutorHelper(System.getenv());
String location = she.quickReturnFirstLine("which CodeChecker").or("");
try {
return ccFactory.createCodeChecker(Paths.get(location), she);
} catch (InvalidCodeCheckerException e) {
throw new InvalidCodeCheckerException(ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

/**
* Custom exception indicating a failed CodeChecker instance creation.
*/
@SuppressWarnings("serial")
public class InvalidCodeCheckerException extends Exception {
/**
* Ctor.
*
* @param errorMessage
* Error message.
*/
public InvalidCodeCheckerException(String errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.codechecker.eclipse.plugin.codechecker.locator;

import java.nio.file.Path;

import org.codechecker.eclipse.plugin.codechecker.ICodeChecker;
import org.codechecker.eclipse.plugin.codechecker.ICodeCheckerFactory;
import org.codechecker.eclipse.plugin.runtime.IShellExecutorHelperFactory;

/**
* Provides a CodeChecker instance which is tied to a pre-built CodeChecker
* package.
*/
public class PreBuiltCodeCheckerLocatorService extends CodeCheckerLocatorService {
public static final String INVALID = "The path to the CodeChecker binary is not valid";
public static final String ERROR = "Couldn't find CodeChecker at the given destination!";

@Override
public ICodeChecker findCodeChecker(Path pathToBin, Path pathToVenv,
ICodeCheckerFactory ccfactory, IShellExecutorHelperFactory sheFactory) throws InvalidCodeCheckerException {
if (pathToBin == null)
throw new IllegalArgumentException(INVALID);
try {
return ccfactory.createCodeChecker(pathToBin, sheFactory.createShellExecutorHelper(System.getenv()));
} catch (InvalidCodeCheckerException e) {
throw new InvalidCodeCheckerException(ERROR);
}
}

}
Loading