1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 317042 added more cases for assignment in condition checker

This commit is contained in:
Alena Laskavaia 2010-06-19 18:05:56 +00:00
parent f912261f97
commit b5a9a032db
3 changed files with 102 additions and 1 deletions

View file

@ -14,10 +14,14 @@ import org.eclipse.cdt.codan.core.cxx.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.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
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;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.AssignmentInConditionProblem"; //$NON-NLS-1$
@ -51,8 +55,18 @@ public class AssignmentInConditionChecker extends AbstractIndexAstChecker {
private boolean isUsedAsCondition(IASTExpression expression) {
ASTNodeProperty prop = expression.getPropertyInParent();
if (prop == IASTForStatement.CONDITION
|| prop == IASTIfStatement.CONDITION)
|| prop == IASTIfStatement.CONDITION
|| prop == IASTWhileStatement.CONDITIONEXPRESSION
|| prop == IASTDoStatement.CONDITION)
return true;
if (prop == IASTUnaryExpression.OPERAND) {
IASTUnaryExpression expr = (IASTUnaryExpression) expression
.getParent();
if (expr.getOperator() == IASTUnaryExpression.op_bracketedPrimary &&
expr.getPropertyInParent() == IASTConditionalExpression.LOGICAL_CONDITION) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright (c) 2009, 2010 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.core.internal.checkers;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
/**
* Test for {@see SuggestedParenthesisChecker} class
*
*/
public class AssignmentInConditionCheckerTest extends CheckerTestCase {
// main() {
// int a=1,b=3;
// if (a=b) b=4; // error here on line 3
// }
public void test_basic() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// main() {
// int a=1,b=3;
//
// if ((a=b)) b--; // no error
// }
public void test_fixed() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// main() {
// int a=1,b=3;
// if ((a=b)!=0) b=4; // no error here on line 3
// }
public void test3() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
// main() {
// int i,a[10];
// if (a[i]=0) b=4; // no error here on line 3
// }
public void test_array() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// main() {
// int i,b=3;
// for (i = 0; i=b; i++) { // here
// }
// }
public void test_for() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// main() {
// int i,b=3;
// while (i=b) { // here
// }
// }
public void test_while() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// main() {
// int i,b=3;
// (i=b)?i++:b++; // here
// }
public void test_tri() {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
}

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.codan.core.test;
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.CatchByReferenceTest;
import org.eclipse.cdt.codan.core.internal.checkers.ReturnCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.StatementHasNoEffectCheckerTest;
@ -41,6 +42,7 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
suite.addTestSuite(ReturnCheckerTest.class);
suite.addTestSuite(CatchByReferenceTest.class);
suite.addTestSuite(AssignmentInConditionCheckerTest.class);
suite.addTest(CodanFastTestSuite.suite());
return suite;
}