Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'codan/org.eclipse.cdt.codan.checkers/src')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/Activator.java74
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java61
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java185
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java111
4 files changed, 0 insertions, 431 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/Activator.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/Activator.java
deleted file mode 100644
index 2915e7d0d80..00000000000
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/Activator.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package org.eclipse.cdt.codan.checkers;
-
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Plugin;
-import org.eclipse.core.runtime.Status;
-import org.osgi.framework.BundleContext;
-
-/**
- * The activator class controls the plug-in life cycle
- */
-public class Activator extends Plugin {
- // The plug-in ID
- public static final String PLUGIN_ID = "org.eclipse.cdt.codan.checkers";
- // 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/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java
deleted file mode 100644
index d4ee6487dd5..00000000000
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2009 Alena Laskavaia
- * 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:
- * Alena Laskavaia - initial API and implementation
- *******************************************************************************/
-package org.eclipse.cdt.codan.checkers.sample;
-
-import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker;
-import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
-import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
-import org.eclipse.cdt.core.dom.ast.IASTExpression;
-import org.eclipse.cdt.core.dom.ast.IASTForStatement;
-import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
-import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
-
-public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
- private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.AssignmentInConditionProblem";
-
- public void processAst(IASTTranslationUnit ast) {
- // traverse the ast using the visitor pattern.
- ast.accept(new CheckCodeVisitor());
- }
-
- class CheckCodeVisitor extends ASTVisitor {
- CheckCodeVisitor() {
- shouldVisitExpressions = true;
- }
-
- public int visit(IASTExpression expression) {
- if (isAssignmentExpression(expression)
- && isUsedAsCondition(expression)) {
- reportProblem(ER_ID, getFile(), expression.getFileLocation()
- .getStartingLineNumber(),
- "Possible assignment in condition");
- }
- return PROCESS_CONTINUE;
- }
-
- private boolean isAssignmentExpression(IASTExpression e) {
- if (e instanceof IASTBinaryExpression) {
- IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
- return binExpr.getOperator() == IASTBinaryExpression.op_assign;
- }
- return false;
- }
-
- private boolean isUsedAsCondition(IASTExpression expression) {
- ASTNodeProperty prop = expression.getPropertyInParent();
- if (prop == IASTForStatement.CONDITION
- || prop == IASTIfStatement.CONDITION)
- return true;
- return false;
- }
- }
-}
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java
deleted file mode 100644
index d1e9b510ebf..00000000000
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java
+++ /dev/null
@@ -1,185 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2009 Alena Laskavaia
- * 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:
- * Alena Laskavaia - initial API and implementation
- *******************************************************************************/
-package org.eclipse.cdt.codan.checkers.sample;
-
-import java.text.MessageFormat;
-
-import org.eclipse.cdt.codan.checkers.Activator;
-import org.eclipse.cdt.codan.core.model.AbstractIndexAstChecker;
-import org.eclipse.cdt.core.dom.ast.ASTVisitor;
-import org.eclipse.cdt.core.dom.ast.DOMException;
-import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
-import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
-import org.eclipse.cdt.core.dom.ast.IASTName;
-import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
-import org.eclipse.cdt.core.dom.ast.IBinding;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
-import org.eclipse.core.resources.IFile;
-
-/**
- * Checker to find that class has virtual method and non virtual destructor
- *
- * @author Alena
- *
- */
-public class NonVirtualDestructor extends AbstractIndexAstChecker {
- private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.NonVirtualDestructorProblem";
-
- public void processAst(IASTTranslationUnit ast) {
- // traverse the ast using the visitor pattern.
- ast.accept(new OnEachClass());
- }
-
- class OnEachClass extends ASTVisitor {
- private IASTName className;
- private IBinding virMethodName;
- private IBinding destName;
-
- OnEachClass() {
- // shouldVisitDeclarations = true;
- shouldVisitDeclSpecifiers = true;
- }
-
- public int visit(IASTDeclSpecifier decl) {
- if (isClassDecl(decl)) {
- try {
- boolean err = hasErrorCondition(decl);
- if (err) {
- String mess;
- String clazz = className.toString();
- String method = virMethodName.getName();
- int line = 1;
- IFile file = getFile();
- if (destName != null) {
- if (destName instanceof ICPPInternalBinding) {
- ICPPInternalBinding bin = (ICPPInternalBinding) destName;
- IASTFileLocation fileLocation = bin
- .getDeclarations()[0].getFileLocation();
- line = fileLocation.getStartingLineNumber();
- }
- mess = MessageFormat
- .format(
- "Class ''{0}'' has virtual method ''{1}'' but non-virtual destructor ''{2}''",
- clazz, method, destName.getName());
- reportProblem(ER_ID, file, line, mess);
- }
- }
- } catch (DOMException e) {
- // ignore, no error
- } catch (Exception e) {
- Activator.log(e);
- }
- return PROCESS_SKIP;
- }
- return PROCESS_CONTINUE;
- }
-
- /**
- * @param decl
- * @throws DOMException
- */
- private boolean hasErrorCondition(IASTDeclSpecifier decl)
- throws DOMException {
- ICPPASTCompositeTypeSpecifier spec = (ICPPASTCompositeTypeSpecifier) decl;
- className = spec.getName();
- IBinding binding = className.getBinding();
- if (binding == null) {
- binding = className.resolveBinding();
- }
- if (binding instanceof ICPPClassType) {
- ICPPClassType type = (ICPPClassType) binding;
- virMethodName = null;
- destName = null;
- // check for the following conditions:
- // class has own virtual method and own non-virtual destructor
- // class has own virtual method and base non-virtual destructor
- // class has base virtual method and own non-virtual destructor
- ICPPMethod[] declaredMethods = type.getDeclaredMethods();
- boolean hasOwnVirtualMethod = false;
- boolean hasOwnNonVirDestructor = false;
- boolean hasDestructor = false;
- boolean hasVirtualMethod = false;
- for (int i = 0; i < declaredMethods.length; i++) {
- ICPPMethod icppMethod = declaredMethods[i];
- if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
- hasOwnVirtualMethod = true;
- virMethodName = icppMethod;
- }
- if (icppMethod.isDestructor()) {
- hasDestructor = true;
- if (!icppMethod.isVirtual()) {
- hasOwnNonVirDestructor = true;
- destName = icppMethod;
- }
- }
- }
- boolean hasVirDestructor = false;
- // class has own virtual method and own non-virtual destructor
- if (hasOwnVirtualMethod && hasOwnNonVirDestructor) {
- return true;
- }
- // class does not have virtual methods but has virtual
- // destructor
- // - not an error
- if (hasOwnVirtualMethod == false && hasDestructor == true
- && hasOwnNonVirDestructor == false) {
- return false;
- }
- ICPPMethod[] allDeclaredMethods = type.getAllDeclaredMethods();
- for (int i = 0; i < allDeclaredMethods.length; i++) {
- ICPPMethod icppMethod = allDeclaredMethods[i];
- if (icppMethod.isVirtual() && !icppMethod.isDestructor()) {
- hasVirtualMethod = true;
- if (virMethodName == null)
- virMethodName = icppMethod;
- }
- if (icppMethod.isDestructor()) {
- hasDestructor = true;
- if (icppMethod.isVirtual()) {
- hasVirDestructor = true;
- } else {
- if (destName == null)
- destName = icppMethod;
- }
- }
- }
- if (hasOwnVirtualMethod) {
- // class has own virtual method and base non-virtual
- // destructor
- if (hasDestructor == true && hasVirDestructor == false) {
- return true;
- }
- } else if (hasVirtualMethod) {
- // class has base virtual method and own non-virtual
- // destructor
- if (hasOwnNonVirDestructor == true) {
- return true;
- }
- }
- }
- return false;
- }
-
- /**
- * @param decl
- * @return
- */
- private boolean isClassDecl(IASTDeclSpecifier decl) {
- if (decl instanceof ICPPASTCompositeTypeSpecifier) {
- return true;
- }
- return false;
- }
- }
-}
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java
deleted file mode 100644
index 98f0c81a516..00000000000
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2009 Alena Laskavaia
- * 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:
- * Alena Laskavaia - initial API and implementation
- *******************************************************************************/
-package org.eclipse.cdt.codan.checkers.sample;
-
-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.IASTExpression;
-import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
-import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
-import org.eclipse.cdt.core.dom.ast.IASTStatement;
-import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
-import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
-import org.eclipse.cdt.core.dom.ast.IBasicType;
-import org.eclipse.cdt.core.dom.ast.IType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
-
-/**
- * Checker that detects statements without effect such as
- *
- * a+b;
- *
- * or
- *
- * +b;
- *
- *
- */
-public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
- private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.StatementHasNoEffectProblem";
-
- public void processAst(IASTTranslationUnit ast) {
- // traverse the ast using the visitor pattern.
- ast.accept(new CheckStmpVisitor());
- }
-
- class CheckStmpVisitor extends ASTVisitor {
- CheckStmpVisitor() {
- shouldVisitStatements = true;
- }
-
- public int visit(IASTStatement stmt) {
- if (stmt instanceof IASTExpressionStatement) {
- if (hasNoEffect(((IASTExpressionStatement) stmt)
- .getExpression())) {
- reportProblem(ER_ID, getFile(), stmt.getFileLocation()
- .getStartingLineNumber(), "Statement has no effect");
- }
- return PROCESS_SKIP;
- }
- return PROCESS_CONTINUE;
- }
-
- /**
- * We consider has not effect binary statements without assignment and
- * unary statement which is not dec and inc. If operator is overloaded
- * we not going to bother.
- *
- * @param e
- * @return
- */
- private boolean hasNoEffect(IASTExpression e) {
- if (e instanceof IASTBinaryExpression) {
- IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
- if (binExpr.getOperator() == IASTBinaryExpression.op_assign)
- return false;
- if (binExpr instanceof CPPASTBinaryExpression) {
- // unfortunately ICPPASTBinaryExpression does not have
- // getOverload public method
- CPPASTBinaryExpression cppBin = (CPPASTBinaryExpression) binExpr;
- ICPPFunction overload = cppBin.getOverload();
- if (overload != null)
- return false;
- IType expressionType = binExpr.getOperand1()
- .getExpressionType();
- if (!(expressionType instanceof IBasicType)) {
- return false; // must be overloaded but parser could not
- // find it
- }
- }
- return true;
- }
- if (e instanceof IASTUnaryExpression) {
- IASTUnaryExpression unaryExpr = (IASTUnaryExpression) e;
- int operator = unaryExpr.getOperator();
- switch (operator) {
- case IASTUnaryExpression.op_postFixDecr:
- case IASTUnaryExpression.op_prefixDecr:
- case IASTUnaryExpression.op_postFixIncr:
- case IASTUnaryExpression.op_prefixIncr:
- return false;
- }
- return true;
- }
- if (e instanceof IASTIdExpression) {
- // simply a;
- return true;
- }
- return false;
- }
- }
-}

Back to the top