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

Bug 441714 - Consider spurious semicolons in CaseBreakChecker

Change-Id: Id4fe394164063007c45da37ae82cc730a9e726dd
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
Nathan Ridge 2015-02-14 20:50:32 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 155fa65c1c
commit 0221ee20b1
2 changed files with 25 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement; import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement; import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
@ -90,6 +91,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
IASTStatement next = null; IASTStatement next = null;
if (i < statements.length - 1) if (i < statements.length - 1)
next = statements[i + 1]; next = statements[i + 1];
IASTStatement prev = null;
if (i > 0)
prev = statements[i - 1];
if (isCaseStatement(curr)) { if (isCaseStatement(curr)) {
prevCase = curr; prevCase = curr;
} }
@ -102,6 +106,13 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
if (!fCheckLastCase && next == null) { if (!fCheckLastCase && next == null) {
continue; // Last case and we don't care continue; // Last case and we don't care
} }
// If this is the null statement, base the decision on the previous statement
// instead (if there is one). Null statements can sneak in via macros in cases
// where the macro expansion ends in a semicolon, and the macro use is followed
// by a semicolon as well.
if (curr instanceof IASTNullStatement && (prev != prevCase)) {
curr = prev;
}
if (!isProducedByMacroExpansion(prevCase) && isFallThroughStamement(curr)) { if (!isProducedByMacroExpansion(prevCase) && isFallThroughStamement(curr)) {
IASTComment comment = null; IASTComment comment = null;
if (next != null) { if (next != null) {

View file

@ -563,4 +563,18 @@ public class CaseBreakCheckerTest extends CheckerTestCase {
loadCodeAndRun(code); loadCodeAndRun(code);
checkErrorLine(4, ER_ID); checkErrorLine(4, ER_ID);
} }
// void foo() {
// switch (0) {
// case 0:
// return 42;;
// case 1:
// break;
// }
// }
public void testDoubleSemicolon_bug441714() {
String code = getAboveComment();
loadCodeAndRun(code);
checkNoErrorsOfKind(ER_ID);
}
} }