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

Bug 485246 - Fix false positive no break at the end of the case

Change-Id: I4712dc9be3c52f3c4b2f9b79d327c43ded36967a
This commit is contained in:
Marco Stornelli 2020-01-19 10:09:41 +01:00
parent 6647808d0e
commit c25c9672dc
2 changed files with 41 additions and 6 deletions

View file

@ -175,34 +175,37 @@ public class CaseBreakChecker extends AbstractIndexAstChecker {
* @param body * @param body
* @return * @return
*/ */
public boolean isFallThroughStamement(IASTStatement body, boolean inLoop) { public boolean isFallThroughStamement(IASTStatement body, boolean checkOnlyExit) {
if (body == null) if (body == null)
return true; return true;
if (body instanceof IASTCompoundStatement) { if (body instanceof IASTCompoundStatement) {
IASTStatement[] statements = ((IASTCompoundStatement) body).getStatements(); IASTStatement[] statements = ((IASTCompoundStatement) body).getStatements();
if (statements.length > 0) { if (statements.length > 0) {
return isFallThroughStamement(statements[statements.length - 1], inLoop); return isFallThroughStamement(statements[statements.length - 1], checkOnlyExit);
} }
return true; return true;
} else if (body instanceof IASTDoStatement) { } else if (body instanceof IASTDoStatement) {
IASTDoStatement dos = (IASTDoStatement) body; IASTDoStatement dos = (IASTDoStatement) body;
return isFallThroughStamement(dos.getBody(), true); return isFallThroughStamement(dos.getBody(), true);
} else if (body instanceof IASTSwitchStatement) {
IASTSwitchStatement switchs = (IASTSwitchStatement) body;
return isFallThroughStamement(switchs.getBody(), true);
} else if (body instanceof IASTForStatement) { } else if (body instanceof IASTForStatement) {
IASTForStatement fors = (IASTForStatement) body; IASTForStatement fors = (IASTForStatement) body;
return isFallThroughStamement(fors.getBody(), true); return isFallThroughStamement(fors.getBody(), true);
} else if (body instanceof IASTWhileStatement) { } else if (body instanceof IASTWhileStatement) {
IASTWhileStatement whiles = (IASTWhileStatement) body; IASTWhileStatement whiles = (IASTWhileStatement) body;
return isFallThroughStamement(whiles.getBody(), true); return isFallThroughStamement(whiles.getBody(), true);
} else if (inLoop && isExitStatement(body)) { } else if (checkOnlyExit && isExitStatement(body)) {
return false; return false;
} else if (!inLoop && isBreakOrExitStatement(body)) { } else if (!checkOnlyExit && isBreakOrExitStatement(body)) {
return false; return false;
} else if (body instanceof IASTExpressionStatement) { } else if (body instanceof IASTExpressionStatement) {
return true; return true;
} else if (body instanceof IASTIfStatement) { } else if (body instanceof IASTIfStatement) {
IASTIfStatement ifs = (IASTIfStatement) body; IASTIfStatement ifs = (IASTIfStatement) body;
return isFallThroughStamement(ifs.getThenClause(), inLoop) return isFallThroughStamement(ifs.getThenClause(), checkOnlyExit)
|| isFallThroughStamement(ifs.getElseClause(), inLoop); || isFallThroughStamement(ifs.getElseClause(), checkOnlyExit);
} }
return true; // TODO return true; // TODO
} }

View file

@ -860,4 +860,36 @@ public class CaseBreakCheckerTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment()); loadCodeAndRun(getAboveComment());
checkNoErrorsOfKind(ER_ID); checkNoErrorsOfKind(ER_ID);
} }
//int main() {
// int a, b;
//
// switch (a) {
// case 0:
// switch (b) {
// default: return 1;
// }
// case 1:
// switch (b) {
// case 1: return 1;
// case 2: return 1;
// case 3: return 1;
// }
// case 2:
// switch (b) {
// case 1: return 1;
// case 2: return 1;
// case 3: return 1;
// default: return 1;
// }
// default:
// return 1;
// }
// return 0;
//}
public void testWithSwitchInCase_Bug485246() throws Exception {
loadCodeAndRun(getAboveComment());
checkNoErrorsOfKind(ER_ID);
}
} }