mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 09:46:02 +02:00
Simplified code by using different API and get rid of warnings
This commit is contained in:
parent
eff03629d6
commit
a88cb349f8
2 changed files with 24 additions and 42 deletions
|
@ -21,12 +21,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
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.ICPPBasicType;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
|
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checker that detects statements without effect such as
|
* Checker that detects statements without effect such as
|
||||||
|
@ -73,37 +68,14 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
||||||
private boolean hasNoEffect(IASTExpression e) {
|
private boolean hasNoEffect(IASTExpression e) {
|
||||||
if (e instanceof IASTBinaryExpression) {
|
if (e instanceof IASTBinaryExpression) {
|
||||||
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
IASTBinaryExpression binExpr = (IASTBinaryExpression) e;
|
||||||
switch (binExpr.getOperator()) {
|
if (binExpr.isLValue())
|
||||||
case IASTBinaryExpression.op_assign:
|
|
||||||
case IASTBinaryExpression.op_binaryAndAssign:
|
|
||||||
case IASTBinaryExpression.op_binaryOrAssign:
|
|
||||||
case IASTBinaryExpression.op_binaryXorAssign:
|
|
||||||
case IASTBinaryExpression.op_divideAssign:
|
|
||||||
case IASTBinaryExpression.op_plusAssign:
|
|
||||||
case IASTBinaryExpression.op_minusAssign:
|
|
||||||
case IASTBinaryExpression.op_multiplyAssign:
|
|
||||||
case IASTBinaryExpression.op_moduloAssign:
|
|
||||||
case IASTBinaryExpression.op_shiftLeftAssign:
|
|
||||||
case IASTBinaryExpression.op_shiftRightAssign:
|
|
||||||
return false;
|
return false;
|
||||||
|
switch (binExpr.getOperator()) {
|
||||||
case IASTBinaryExpression.op_logicalOr:
|
case IASTBinaryExpression.op_logicalOr:
|
||||||
case IASTBinaryExpression.op_logicalAnd:
|
case IASTBinaryExpression.op_logicalAnd:
|
||||||
return hasNoEffect(binExpr.getOperand1())
|
return hasNoEffect(binExpr.getOperand1())
|
||||||
&& hasNoEffect(binExpr.getOperand2());
|
&& hasNoEffect(binExpr.getOperand2());
|
||||||
}
|
}
|
||||||
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 || expressionType instanceof ICPPBasicType)) {
|
|
||||||
return false; // must be overloaded but parser could not find it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (e instanceof IASTUnaryExpression) {
|
if (e instanceof IASTUnaryExpression) {
|
||||||
|
|
|
@ -123,4 +123,14 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
||||||
loadCodeAndRunCpp(getAboveComment());
|
loadCodeAndRunCpp(getAboveComment());
|
||||||
checkNoErrors();
|
checkNoErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// main() {
|
||||||
|
// A a,b;
|
||||||
|
//
|
||||||
|
// b+=a; // error here on line 4
|
||||||
|
// }
|
||||||
|
public void testOverloadedBinaryExpression() {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkNoErrors();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue