diff --git a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties index d5739593f8e..7cf30922b76 100644 --- a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties +++ b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties @@ -44,4 +44,8 @@ problem.messagePattern.UnusedReturnValue = Return has value, in function returni problem.name.UnusedReturnValue = Unused return value problem.description.NoReturn = No return statement in a function which is declared to return value problem.messagePattern.NoReturn = No return, in function returning non-void -problem.name.NoReturn = No return \ No newline at end of file +problem.name.NoReturn = No return +checker.name.AssignmentToItself = Assignment to itself +problem.messagePattern.AssignmentToItself = Assignment to itself ''{0}'' +problem.name.AssignmentToItself = Assignment to itself +problem.description.AssignmentToItself = Finds expression where left and right side of the assignment is the same, i.e. 'var = var' \ No newline at end of file diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml index babda982ac1..876728d3853 100644 --- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml @@ -250,5 +250,19 @@ name="Field cannot be resolved"> + + + + diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentToItselfChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentToItselfChecker.java new file mode 100644 index 00000000000..f672f8f4f24 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/AssignmentToItselfChecker.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2010 Severin Gehwolf + * 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: + * Severin Gehwolf - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.internal.checkers; + +import org.eclipse.cdt.codan.core.cxx.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.IASTTranslationUnit; + +/** + * Checker that find assignment to itself cases, such a + * a = a. It can produce some false positives such as + * a[f()]=a[f()] - but who write codes like that? + */ +public class AssignmentToItselfChecker extends AbstractIndexAstChecker { + + private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.AssignmentToItselfProblem"; //$NON-NLS-1$ + + public void processAst(IASTTranslationUnit ast) { + // traverse the ast using the visitor pattern. + ast.accept(new ASTVisitor() { + { // constructor + shouldVisitExpressions = true; + } + + // visit expressions + public int visit(IASTExpression expression) { + if (isAssignmentToItself(expression)) { + reportProblem(ER_ID, expression, expression.getRawSignature()); + } + return PROCESS_CONTINUE; + } + + private boolean isAssignmentToItself(IASTExpression e) { + if (e instanceof IASTBinaryExpression) { + IASTBinaryExpression binExpr = (IASTBinaryExpression) e; + if (binExpr.getOperator() == IASTBinaryExpression.op_assign) { + return binExpr.getOperand1().getRawSignature().equals(binExpr.getOperand2().getRawSignature()); + } + } + return false; + } + }); + } +} \ No newline at end of file diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java new file mode 100644 index 00000000000..e4e14848a23 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/AssignmentToItselfCheckerTest.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2010 Severin Gehwolf + * 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: + * Severin Gehwolf - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.codan.core.internal.checkers; + +import org.eclipse.cdt.codan.core.test.CheckerTestCase; + +/** + * Test for {@see AssignmentToItselfChecker} class + * + */ +public class AssignmentToItselfCheckerTest extends CheckerTestCase { + // void main() { + // int x = 0; + // x = 10; + // } + public void testNoErrorConstants() { + loadCodeAndRun(getAboveComment()); + checkNoErrors(); + } + + // void main() { + // int x = 10; + // int s = 10; + // x = s; + // } + public void testNoErrorVariables() { + loadCodeAndRun(getAboveComment()); + checkNoErrors(); + } + + // void main() { + // int x = 0; + // x = x; + // } + public void testSimpleVariableSelfAssignmentError() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(3); + } + + // void main() { + // char str[] = "hello testing world"; + // int x = 10; + // str[i] = str[i]; + // } + public void testArraySelfAssignmentError() { + loadCodeAndRun(getAboveComment()); + checkErrorLine(4); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java index 8be78165a2b..eb341b82d96 100644 --- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java +++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java @@ -15,6 +15,7 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.eclipse.cdt.codan.core.internal.checkers.AssignmentInConditionCheckerTest; +import org.eclipse.cdt.codan.core.internal.checkers.AssignmentToItselfCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.CatchByReferenceTest; import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest; import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest; @@ -45,6 +46,7 @@ public class AutomatedIntegrationSuite extends TestSuite { suite.addTestSuite(ReturnCheckerTest.class); suite.addTestSuite(CatchByReferenceTest.class); suite.addTestSuite(AssignmentInConditionCheckerTest.class); + suite.addTestSuite(AssignmentToItselfCheckerTest.class); // framework suite.addTest(CodanFastTestSuite.suite()); // quick fixes