mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 06:32:10 +02:00
Bug 485246 - Fix false positive no break at the end of the case
Change-Id: I4712dc9be3c52f3c4b2f9b79d327c43ded36967a
This commit is contained in:
parent
6647808d0e
commit
c25c9672dc
2 changed files with 41 additions and 6 deletions
|
@ -175,34 +175,37 @@ public class CaseBreakChecker extends AbstractIndexAstChecker {
|
|||
* @param body
|
||||
* @return
|
||||
*/
|
||||
public boolean isFallThroughStamement(IASTStatement body, boolean inLoop) {
|
||||
public boolean isFallThroughStamement(IASTStatement body, boolean checkOnlyExit) {
|
||||
if (body == null)
|
||||
return true;
|
||||
if (body instanceof IASTCompoundStatement) {
|
||||
IASTStatement[] statements = ((IASTCompoundStatement) body).getStatements();
|
||||
if (statements.length > 0) {
|
||||
return isFallThroughStamement(statements[statements.length - 1], inLoop);
|
||||
return isFallThroughStamement(statements[statements.length - 1], checkOnlyExit);
|
||||
}
|
||||
return true;
|
||||
} else if (body instanceof IASTDoStatement) {
|
||||
IASTDoStatement dos = (IASTDoStatement) body;
|
||||
return isFallThroughStamement(dos.getBody(), true);
|
||||
} else if (body instanceof IASTSwitchStatement) {
|
||||
IASTSwitchStatement switchs = (IASTSwitchStatement) body;
|
||||
return isFallThroughStamement(switchs.getBody(), true);
|
||||
} else if (body instanceof IASTForStatement) {
|
||||
IASTForStatement fors = (IASTForStatement) body;
|
||||
return isFallThroughStamement(fors.getBody(), true);
|
||||
} else if (body instanceof IASTWhileStatement) {
|
||||
IASTWhileStatement whiles = (IASTWhileStatement) body;
|
||||
return isFallThroughStamement(whiles.getBody(), true);
|
||||
} else if (inLoop && isExitStatement(body)) {
|
||||
} else if (checkOnlyExit && isExitStatement(body)) {
|
||||
return false;
|
||||
} else if (!inLoop && isBreakOrExitStatement(body)) {
|
||||
} else if (!checkOnlyExit && isBreakOrExitStatement(body)) {
|
||||
return false;
|
||||
} else if (body instanceof IASTExpressionStatement) {
|
||||
return true;
|
||||
} else if (body instanceof IASTIfStatement) {
|
||||
IASTIfStatement ifs = (IASTIfStatement) body;
|
||||
return isFallThroughStamement(ifs.getThenClause(), inLoop)
|
||||
|| isFallThroughStamement(ifs.getElseClause(), inLoop);
|
||||
return isFallThroughStamement(ifs.getThenClause(), checkOnlyExit)
|
||||
|| isFallThroughStamement(ifs.getElseClause(), checkOnlyExit);
|
||||
}
|
||||
return true; // TODO
|
||||
}
|
||||
|
|
|
@ -860,4 +860,36 @@ public class CaseBreakCheckerTest extends CheckerTestCase {
|
|||
loadCodeAndRun(getAboveComment());
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue