mirror of
https://github.com/eclipse-cdt/cdt
synced 2024-10-28 12:09:40 +01:00
Bug 433357 - Added fix for false positives from GNU
Statement-Expressions Change-Id: I21eb9978a156f2375e5b9e1992c297e3b6ca78ac Signed-off-by: Chris Marr <cdabone@gmail.com> Reviewed-on: https://git.eclipse.org/r/25600 Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com> Tested-by: Hudson CI Reviewed-by: Elena Laskavaia <elaskavaia.cdt@gmail.com>
This commit is contained in:
parent
e35ccf64a8
commit
fcd66c7d04
2 changed files with 39 additions and 15 deletions
|
@ -56,18 +56,44 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
||||||
public int visit(IASTStatement stmt) {
|
public int visit(IASTStatement stmt) {
|
||||||
if (stmt instanceof IASTExpressionStatement) {
|
if (stmt instanceof IASTExpressionStatement) {
|
||||||
IASTExpression expression = ((IASTExpressionStatement) stmt).getExpression();
|
IASTExpression expression = ((IASTExpressionStatement) stmt).getExpression();
|
||||||
if (hasNoEffect(expression)) {
|
if (hasNoEffect(expression) ) {
|
||||||
|
if (isLastExpressionInStatementExpression(expression))
|
||||||
|
return PROCESS_SKIP;
|
||||||
if (!shouldReportInMacro() && CxxAstUtils.isInMacro(expression))
|
if (!shouldReportInMacro() && CxxAstUtils.isInMacro(expression))
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
String arg = expression.getRawSignature();
|
String arg = expression.getRawSignature();
|
||||||
if (!isFilteredArg(arg))
|
if (isFilteredArg(arg))
|
||||||
reportProblem(ER_ID, stmt, arg);
|
return PROCESS_SKIP;
|
||||||
|
reportProblem(ER_ID, stmt, arg);
|
||||||
}
|
}
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
return PROCESS_CONTINUE;
|
return PROCESS_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Checks to see if the statement is the last one in a GNU Statement-Expression
|
||||||
|
* as these become the expression part of another statement and the test may generate
|
||||||
|
* false positives otherwise
|
||||||
|
*/
|
||||||
|
private boolean isLastExpressionInStatementExpression(IASTExpression e) {
|
||||||
|
// Check if it is part of GNU compound stmt expression i.e. ({int a; foo(a); a;})
|
||||||
|
IASTNode stmt = e.getParent();
|
||||||
|
if (stmt instanceof IASTExpressionStatement) {
|
||||||
|
IASTNode parentComp = stmt.getParent();
|
||||||
|
if (parentComp instanceof IASTCompoundStatement) {
|
||||||
|
IASTNode parentStmtExpr = parentComp.getParent();
|
||||||
|
if (parentStmtExpr instanceof IGNUASTCompoundStatementExpression) {
|
||||||
|
// Are we evaluating the last statement in the list?
|
||||||
|
IASTStatement childlist[] = ((IASTCompoundStatement) parentComp).getStatements();
|
||||||
|
if (stmt == childlist[childlist.length-1])
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We consider has not effect binary statements without assignment and
|
* We consider has not effect binary statements without assignment and
|
||||||
* unary statement which is not dec and inc. If operator is overloaded
|
* unary statement which is not dec and inc. If operator is overloaded
|
||||||
|
@ -109,17 +135,6 @@ public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {
|
||||||
}
|
}
|
||||||
// simply a;
|
// simply a;
|
||||||
if (e instanceof IASTIdExpression) {
|
if (e instanceof IASTIdExpression) {
|
||||||
// check if it is part of GNU comp stmt expression i.e. ({foo();a;})
|
|
||||||
IASTNode parent = e.getParent();
|
|
||||||
if (parent instanceof IASTExpressionStatement) {
|
|
||||||
IASTNode parent2 = parent.getParent();
|
|
||||||
if (parent2 instanceof IASTCompoundStatement) {
|
|
||||||
IASTNode parent3 = parent2.getParent();
|
|
||||||
if (parent3 instanceof IGNUASTCompoundStatementExpression) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -88,12 +88,21 @@ public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
|
||||||
|
|
||||||
// main() {
|
// main() {
|
||||||
// int a=({foo();a;}); // no error here on line 2
|
// int a=({foo();a;}); // no error here on line 2
|
||||||
|
// char *p=({char s[]="Some string";&s[0];}); // no error here on line 3
|
||||||
// }
|
// }
|
||||||
public void testGNUExpressionCompoundStmt() {
|
public void testGNUExpressionCompoundStmtFalsePositives() {
|
||||||
loadCodeAndRun(getAboveComment());
|
loadCodeAndRun(getAboveComment());
|
||||||
checkNoErrors();
|
checkNoErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// main() {
|
||||||
|
// int z=({int a=0; +a; a;}) // error here on line 2
|
||||||
|
// }
|
||||||
|
public void testGNUExpressionCompoundStmtInside() {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkErrorLine(2);
|
||||||
|
}
|
||||||
|
|
||||||
// main() {
|
// main() {
|
||||||
// int a;
|
// int a;
|
||||||
// +a; // error here on line 3
|
// +a; // error here on line 3
|
||||||
|
|
Loading…
Reference in a new issue