diff options
author | Elliott Baron <ebaron@fedoraproject.org> | 2009-09-21 15:09:03 -0400 |
---|---|---|
committer | Elliott Baron <ebaron@fedoraproject.org> | 2009-09-21 15:09:03 -0400 |
commit | e35f5131df28786841f5e09d9982b912bd3469d0 (patch) | |
tree | b1699b9eb1a0ce4691f1836c3359628cb8505ecc /org.eclipse.cdt.codan.extension | |
parent | d581ec1fce73716b9d422cf2e4a255871f0bcd9b (diff) | |
download | codan-e35f5131df28786841f5e09d9982b912bd3469d0.tar.gz codan-e35f5131df28786841f5e09d9982b912bd3469d0.tar.xz codan-e35f5131df28786841f5e09d9982b912bd3469d0.zip |
Created .extension plugin with my modifications. Initial use of CFG, pulled in from PTP. Fixed a bug in CFG printing in PTP plugin.
* org.eclipse.cdt.codan.checkers: Moved changes to codan.extension.
* org.eclipse.cdt.codan.extension: New plugin.
* org.eclipse.ptp.pldt.mpi.analysis.cdt: Small bugfix to block printing.
Diffstat (limited to 'org.eclipse.cdt.codan.extension')
8 files changed, 313 insertions, 0 deletions
diff --git a/org.eclipse.cdt.codan.extension/.classpath b/org.eclipse.cdt.codan.extension/.classpath new file mode 100644 index 0000000..ad32c83 --- /dev/null +++ b/org.eclipse.cdt.codan.extension/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/org.eclipse.cdt.codan.extension/.project b/org.eclipse.cdt.codan.extension/.project new file mode 100644 index 0000000..5300627 --- /dev/null +++ b/org.eclipse.cdt.codan.extension/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.cdt.codan.extension</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/org.eclipse.cdt.codan.extension/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.cdt.codan.extension/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..d0c24ad --- /dev/null +++ b/org.eclipse.cdt.codan.extension/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Sun Sep 20 15:26:36 EDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/org.eclipse.cdt.codan.extension/META-INF/MANIFEST.MF b/org.eclipse.cdt.codan.extension/META-INF/MANIFEST.MF new file mode 100644 index 0000000..fde603b --- /dev/null +++ b/org.eclipse.cdt.codan.extension/META-INF/MANIFEST.MF @@ -0,0 +1,17 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Code Analysis Extension +Bundle-SymbolicName: org.eclipse.cdt.codan.extension;singleton:=true +Bundle-Version: 1.0.0.qualifier +Bundle-Vendor: Eclipse +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Import-Package: org.eclipse.cdt.codan.core, + org.eclipse.cdt.codan.core.model, + org.eclipse.cdt.core.dom, + org.eclipse.cdt.core.dom.ast, + org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs, + org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.impl +Require-Bundle: org.eclipse.core.runtime;bundle-version="3.5.0", + org.eclipse.ui;bundle-version="3.5.0", + org.eclipse.core.resources;bundle-version="3.5.0" +Bundle-Activator: org.eclipse.cdt.codan.extension.Activator diff --git a/org.eclipse.cdt.codan.extension/build.properties b/org.eclipse.cdt.codan.extension/build.properties new file mode 100644 index 0000000..e9863e2 --- /dev/null +++ b/org.eclipse.cdt.codan.extension/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.xml diff --git a/org.eclipse.cdt.codan.extension/plugin.xml b/org.eclipse.cdt.codan.extension/plugin.xml new file mode 100644 index 0000000..f0ae36c --- /dev/null +++ b/org.eclipse.cdt.codan.extension/plugin.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + <extension + point="org.eclipse.cdt.codan.core.checkers"> + <checker + class="org.eclipse.cdt.codan.extension.CloseOpenedFilesChecker" + id="org.eclipse.cdt.codan.extension.CloseOpenedFilesChecker" + name="Closing unopened file"> + <problem + category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems" + defaultSeverity="Warning" + id="org.eclipse.cdt.codan.extension.CloseOpenedFilesProblem" + name="Attempting to close an unopened file"> + </problem> + </checker> + </extension> + +</plugin> diff --git a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/Activator.java b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/Activator.java new file mode 100644 index 0000000..4deea76 --- /dev/null +++ b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/Activator.java @@ -0,0 +1,72 @@ +package org.eclipse.cdt.codan.extension; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +public class Activator extends AbstractUIPlugin { + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.cdt.codan.extension"; + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext + * ) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext + * ) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + /** + * @param e + */ + public static void log(Throwable e) { + getDefault().getLog().log(getStatus(e)); + } + + public static void log(String message) { + getDefault().getLog().log(new Status(Status.ERROR, PLUGIN_ID, message)); + } + + /** + * @param e + * @return + */ + public static IStatus getStatus(Throwable e) { + return new Status(Status.ERROR, PLUGIN_ID, e.getLocalizedMessage(), e); + } + +} diff --git a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/CloseOpenedFilesChecker.java b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/CloseOpenedFilesChecker.java new file mode 100644 index 0000000..8acdc81 --- /dev/null +++ b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/CloseOpenedFilesChecker.java @@ -0,0 +1,157 @@ +/******************************************************************************* + * Copyright (c) 2009 Elliott Baron + * 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: + * Elliott Baron - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.extension; + +import java.io.File; +import java.net.URI; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression; +import org.eclipse.cdt.core.dom.ast.IASTIdExpression; +import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTStatement; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.GraphCreator; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IBlock; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.ICallGraph; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.ICallGraphNode; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.IControlFlowGraph; +import org.eclipse.ptp.pldt.mpi.analysis.cdt.graphs.impl.ControlFlowGraph; + +public class CloseOpenedFilesChecker extends AbstractIndexAstChecker { + private static final String ERR_ID = Activator.PLUGIN_ID + ".CloseOpenedFilesProblem"; + + public void processAst(IASTTranslationUnit ast) { + // traverse the AST using the visitor pattern + CloseOpenedFilesVisitor visitor = new CloseOpenedFilesVisitor(); + + GraphCreator creator = new GraphCreator(); + + // Retrieve resource corresponding to this translation unit + String path = ast.getFilePath(); + URI fileURI = new File(path).toURI(); + IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); + IResource[] resources = wsRoot.findFilesForLocationURI(fileURI); + if (resources != null && resources.length > 0) { + IProject proj = resources[0].getProject(); + + // Create call graph for project + ICallGraph cg = creator.createCallGraph(proj); + creator.computeCallGraph(cg); + + // Create control flow graph + ICallGraphNode cgRoot = cg.topEntry(); + IASTStatement fnBody = cgRoot.getFuncDef().getBody(); + IControlFlowGraph cfg = new ControlFlowGraph(fnBody); + cfg.buildCFG(); + cfg.print(); + + ast.accept(visitor); + } + } + + + private void reportProblem(IASTName closeFD) { + String message = MessageFormat.format("File descriptor \"{0}\" has not been opened", closeFD.toString()); + reportProblem(ERR_ID, closeFD, message); + } + + + class CloseOpenedFilesVisitor extends ASTVisitor { + + public static final String OPEN = "open"; + public static final String CLOSE = "close"; + + private List<IASTName> openedFDs; + + public CloseOpenedFilesVisitor() { + openedFDs = new ArrayList<IASTName>(); + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTFunctionCallExpression) { + IASTFunctionCallExpression callExpression = (IASTFunctionCallExpression) expression; + IASTExpression funcName = callExpression.getFunctionNameExpression(); + if (funcName instanceof IASTIdExpression) { + IASTName name = ((IASTIdExpression) funcName).getName(); + String simpleName = String.valueOf(name.getSimpleID()); + if (simpleName.equals(OPEN)) { + IASTNode parent = callExpression.getParent(); + // Handle initialization in declaration + if (parent instanceof IASTInitializerExpression) { + parent = parent.getParent(); + if (parent instanceof IASTDeclarator) { + openedFDs.add(((IASTDeclarator) parent).getName()); + } + } + // Assignment after declaration + else if (parent instanceof IASTBinaryExpression) { + IASTExpression op2 = ((IASTBinaryExpression) parent).getOperand2(); + int operator = ((IASTBinaryExpression) parent).getOperator(); + if (callExpression.equals(op2) && operator == IASTBinaryExpression.op_assign) { + IASTExpression op1 = ((IASTBinaryExpression) parent).getOperand1(); + if (op1 instanceof IASTIdExpression) { + openedFDs.add(((IASTIdExpression) op1).getName()); + } + } + } + } + else if (simpleName.equals(CLOSE)) { + IASTExpression paramExpression = callExpression.getParameterExpression(); + if (paramExpression instanceof IASTIdExpression) { + IASTName fd = ((IASTIdExpression) paramExpression).getName(); + // Add only if no matching opened FD + boolean match = false; + for (int i = 0; !match && i < openedFDs.size(); i++) { + IASTName opened = openedFDs.get(i); + match = matchingFileDescriptors(fd, opened); + } + + if (!match) { + reportProblem(fd); + } + } + } + } + + return PROCESS_SKIP; + } + else { + return PROCESS_CONTINUE; + } + } + + private boolean matchingFileDescriptors(IASTName closeFD, IASTName openFD) { + // FIXME elaborate + IBinding closeBinding = closeFD.getBinding(); + IBinding openBinding = openFD.getBinding(); + + return openBinding != null && closeBinding != null && openBinding.equals(closeBinding); + } + } + +} |