From 4e112eca7750a4f530c986be55c178c43c16d3ea Mon Sep 17 00:00:00 2001 From: Elliott Baron Date: Sat, 26 Jun 2010 22:27:34 -0400 Subject: Update codan plugins to CDT 7.0. * org.eclipse.cdt.codan.checkers.ui: Updated. * org.eclipse.cdt.codan.checkers: Updated. * org.eclipse.cdt.codan.core: Updated. * org.eclipse.cdt.codan.ui: Updated. * org.eclipse.cdt.codan.core.cxx: Added. * org.eclipse.cdt.codan.extension/META-INF/MANIFEST.MF: Import org.eclipse.cdt.codan.core.cxx.model. * org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/checkers/AbstractPropSimChecker.java: Superclass moved. --- .../sample/AssignmentInConditionChecker.java | 59 ------- .../eclipse/cdt/codan/checkers/sample/CVS/Entries | 5 - .../cdt/codan/checkers/sample/CVS/Repository | 1 - .../org/eclipse/cdt/codan/checkers/sample/CVS/Root | 1 - .../eclipse/cdt/codan/checkers/sample/CVS/Template | 0 .../codan/checkers/sample/CatchUsesReference.java | 82 ---------- .../checkers/sample/NonVirtualDestructor.java | 181 --------------------- .../sample/StatementHasNoEffectChecker.java | 110 ------------- .../sample/SuggestedParenthesisChecker.java | 143 ---------------- 9 files changed, 582 deletions(-) delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Entries delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Repository delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Root delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Template delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java delete mode 100644 org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java (limited to 'org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample') diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java deleted file mode 100644 index 5ce5661..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/AssignmentInConditionChecker.java +++ /dev/null @@ -1,59 +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, expression, "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/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Entries b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Entries deleted file mode 100644 index 705d9ab..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Entries +++ /dev/null @@ -1,5 +0,0 @@ -/AssignmentInConditionChecker.java/1.4/Fri Jul 31 20:39:13 2009// -/CatchUsesReference.java/1.2/Sat Nov 21 02:25:12 2009// -/NonVirtualDestructor.java/1.4/Fri Jul 31 20:39:13 2009// -/StatementHasNoEffectChecker.java/1.5/Fri Jul 31 20:39:13 2009// -/SuggestedParenthesisChecker.java/1.2/Sat Nov 21 03:03:08 2009// diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Repository b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Repository deleted file mode 100644 index 28a5431..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Repository +++ /dev/null @@ -1 +0,0 @@ -org.eclipse.cdt/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Root b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Root deleted file mode 100644 index 04efa23..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Root +++ /dev/null @@ -1 +0,0 @@ -:pserver:anonymous@dev.eclipse.org:/cvsroot/tools diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Template deleted file mode 100644 index e69de29..0000000 diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java deleted file mode 100644 index b3556cc..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java +++ /dev/null @@ -1,82 +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.IASTDeclaration; -import org.eclipse.cdt.core.dom.ast.IASTDeclarator; -import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; -import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; -import org.eclipse.cdt.core.dom.ast.IASTStatement; -import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement; - -/** - * @author Alena - * - */ -public class CatchUsesReference extends AbstractIndexAstChecker { - private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.CatchUsesReference"; - - public void processAst(IASTTranslationUnit ast) { - // traverse the ast using the visitor pattern. - ast.accept(new OnCatch()); - } - - class OnCatch extends ASTVisitor { - OnCatch() { - shouldVisitStatements = true; - } - public int visit(IASTStatement stmt) { - if (stmt instanceof ICPPASTTryBlockStatement) { - ICPPASTTryBlockStatement tblock = (ICPPASTTryBlockStatement) stmt; - ICPPASTCatchHandler[] catchHandlers = tblock.getCatchHandlers(); - for (int i = 0; i < catchHandlers.length; i++) { - ICPPASTCatchHandler catchHandler = catchHandlers[i]; - if (usesReference(catchHandler)) { - reportProblem(ER_ID, catchHandler.getDeclaration(), "Catch clause uses reference in declaration of exception"); - } - } - - return PROCESS_SKIP; - } - return PROCESS_CONTINUE; - } - /** - * @param catchHandler - * @return - */ - private boolean usesReference(ICPPASTCatchHandler catchHandler) { - IASTDeclaration declaration = catchHandler.getDeclaration(); - if (declaration instanceof IASTSimpleDeclaration) { - IASTDeclarator[] declarators = ((IASTSimpleDeclaration) declaration).getDeclarators(); - for (int i = 0; i < declarators.length; i++) { - IASTDeclarator d = declarators[i]; - IASTPointerOperator[] pointerOperators = d.getPointerOperators(); - for (int j = 0; j < pointerOperators.length; j++) { - IASTPointerOperator po = pointerOperators[j]; - if (po instanceof ICPPASTReferenceOperator) { - return true; - } - - } - } - } - return false; - } - } - - -} diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java deleted file mode 100644 index 45f7e72..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/NonVirtualDestructor.java +++ /dev/null @@ -1,181 +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.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTNode; -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; - -/** - * 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(); - IASTNode ast = decl; - if (destName != null) { - if (destName instanceof ICPPInternalBinding) { - ICPPInternalBinding bin = (ICPPInternalBinding) destName; - ast = bin.getDeclarations()[0]; - } - mess = MessageFormat - .format( - "Class ''{0}'' has virtual method ''{1}'' but non-virtual destructor ''{2}''", - clazz, method, destName.getName()); - reportProblem(ER_ID, ast, 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/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java deleted file mode 100644 index c08f58a..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/StatementHasNoEffectChecker.java +++ /dev/null @@ -1,110 +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, stmt, "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; - } - } -} diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java deleted file mode 100644 index 27712b4..0000000 --- a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java +++ /dev/null @@ -1,143 +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; -import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; - -/** - * This checker finds a problems that cause by lack of understanding operator - * precedence in C. In any case it is better to surround expressions in - * parenthesis to improve readability. Example: ! x>0 && x<10 (this would be - * (!x)>0 && x<10 in C) We only look for &&, || and ! operators (and binary | & - * ^ ~) - * - * @author Alena - * - */ -public class SuggestedParenthesisChecker extends AbstractIndexAstChecker { - private static final String ER_ID = "org.eclipse.cdt.codan.checkers.sample.SuggestedParenthesisProblem"; - - public void processAst(IASTTranslationUnit ast) { - // traverse the ast using the visitor pattern. - ast.accept(new ExpressionVisitor()); - } - - class ExpressionVisitor extends ASTVisitor { - private SuspiciousExpressionVisitor svis; - - ExpressionVisitor() { - shouldVisitExpressions = true; - svis = new SuspiciousExpressionVisitor(); - } - - public int visit(IASTExpression expression) { - int precedence = getPrecedence(expression); - if (precedence == 2) { // unary not - if (isUsedAsOperand(expression)) { - reportProblem(ER_ID, expression, - "Suggested parenthesis around expression"); - return PROCESS_SKIP; - } - } - if (precedence >= 0) { - synchronized (svis) { // since we use only one instance of this - // visitor sync just in case - svis.init(expression); - expression.accept(svis); - if (svis.suspicious == true) { - reportProblem(ER_ID, svis.other, - "Suggested parenthesis around expression"); - return PROCESS_SKIP; - } - } - } - return PROCESS_CONTINUE; - } - - private boolean isUsedAsOperand(IASTExpression expression) { - ASTNodeProperty prop = expression.getPropertyInParent(); - if (prop == IASTBinaryExpression.OPERAND_ONE - || prop == IASTBinaryExpression.OPERAND_TWO - || prop == IASTUnaryExpression.OPERAND) - return true; - return false; - } - } - - private int getPrecedence(IASTExpression e) { - if (e instanceof IASTBinaryExpression) { - IASTBinaryExpression binExpr = (IASTBinaryExpression) e; - int operator = binExpr.getOperator(); - if (operator == IASTBinaryExpression.op_binaryAnd) - return 8; - if (operator == IASTBinaryExpression.op_binaryXor) - return 9; - if (operator == IASTBinaryExpression.op_binaryOr) - return 10; - if (operator == IASTBinaryExpression.op_logicalAnd) - return 11; - if (operator == IASTBinaryExpression.op_logicalOr) - return 12; - } - if (e instanceof IASTUnaryExpression) { - IASTUnaryExpression binExpr = (IASTUnaryExpression) e; - int operator = binExpr.getOperator(); - if (operator == IASTUnaryExpression.op_not) - return 2; - if (operator == IASTUnaryExpression.op_tilde) - return 2; - } - return -1; - } - - class SuspiciousExpressionVisitor extends ASTVisitor { - IASTExpression parent; - IASTExpression other; - boolean suspicious = false; - - void init(IASTExpression e) { - parent = e; - suspicious = false; - } - - SuspiciousExpressionVisitor() { - shouldVisitExpressions = true; - } - - public int visit(IASTExpression expression) { - if (expression == parent) - return PROCESS_CONTINUE; - if (expression instanceof IASTUnaryExpression) { - IASTUnaryExpression uExpr = (IASTUnaryExpression) expression; - int operator = uExpr.getOperator(); - if (operator == IASTUnaryExpression.op_bracketedPrimary) { - return PROCESS_SKIP; - } - } - if (getPrecedence(expression) < 0) // not considered operator - return PROCESS_CONTINUE; - if (getPrecedence(expression) == getPrecedence(parent)) { - return PROCESS_SKIP; - } - suspicious = true; - other = expression; - return PROCESS_ABORT; - } - } -} -- cgit