diff options
author | Elliott Baron <ebaron@fedoraproject.org> | 2009-12-16 16:51:53 -0500 |
---|---|---|
committer | Elliott Baron <ebaron@fedoraproject.org> | 2009-12-16 16:51:53 -0500 |
commit | af92b8417ae76946c7d10772d8848abbe1126209 (patch) | |
tree | 4f1f7ab3def1e2e5facd2582645f663aba399fc3 /org.eclipse.cdt.codan.checkers/src | |
parent | 4baa2f35d979b4e2336431460e313e055661327f (diff) | |
download | codan-af92b8417ae76946c7d10772d8848abbe1126209.tar.gz codan-af92b8417ae76946c7d10772d8848abbe1126209.tar.xz codan-af92b8417ae76946c7d10772d8848abbe1126209.zip |
Update codan plugins to HEAD.
* 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.
Diffstat (limited to 'org.eclipse.cdt.codan.checkers/src')
10 files changed, 227 insertions, 0 deletions
diff --git a/org.eclipse.cdt.codan.checkers/src/CVS/Template b/org.eclipse.cdt.codan.checkers/src/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/CVS/Template diff --git a/org.eclipse.cdt.codan.checkers/src/org/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/CVS/Template diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/CVS/Template diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/CVS/Template diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/CVS/Template diff --git a/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/CVS/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/CVS/Template 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 index 7071b8d..705d9ab 100644 --- 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 @@ -1,3 +1,5 @@ /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/Template b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Template new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CVS/Template 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 new file mode 100644 index 0000000..b3556cc --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/CatchUsesReference.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * 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/SuggestedParenthesisChecker.java b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java new file mode 100644 index 0000000..27712b4 --- /dev/null +++ b/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/checkers/sample/SuggestedParenthesisChecker.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * 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; + } + } +} |