1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Switch without compound statement, bug 105334.

This commit is contained in:
Markus Schorn 2008-06-25 12:33:40 +00:00
parent e6baa5b577
commit 307d084357
4 changed files with 45 additions and 8 deletions

View file

@ -56,6 +56,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
@ -5019,4 +5020,23 @@ public class AST2Tests extends AST2BaseTest {
assertEquals(";", empty.getRawSignature());
}
}
// void test() {
// int foux = 3;
// switch(foux) // no brace!
// case 0:
// case 1:
// foux= 0;
// foux= 1;
// }
public void testSwitchWithoutCompound_Bug105334() throws Exception {
final String comment= getAboveComment();
for (ParserLanguage lang : ParserLanguage.values()) {
IASTTranslationUnit tu= parse(comment, lang);
IASTFunctionDefinition fdef= getDeclaration(tu, 0);
IASTSwitchStatement sw= getStatement(fdef, 1);
IASTStatement stmt= getStatement(fdef, 2);
assertEquals("foux= 1;", stmt.getRawSignature());
}
}
}

View file

@ -1650,6 +1650,27 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
return break_statement;
}
protected IASTStatement parseSwitchBody() throws EndOfFileException, BacktrackException {
IASTStatement stmt= null;
if (LT(1) != IToken.tEOC)
stmt= statement();
if (stmt instanceof IASTCaseStatement == false)
return stmt;
// bug 105334, switch without compound statement
IASTCompoundStatement comp= createCompoundStatement();
((ASTNode) comp).setOffsetAndLength((ASTNode) stmt);
comp.addStatement(stmt);
while (LT(1) != IToken.tEOC && stmt instanceof IASTCaseStatement) {
stmt= statement();
comp.addStatement(stmt);
}
adjustLength(comp, stmt);
return comp;
}
protected IASTStatement parseContinueStatement() throws EndOfFileException,
BacktrackException {
int startOffset = consume().getOffset(); // t_continue

View file

@ -2488,10 +2488,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
default:
throwBacktrack(LA(1));
}
IASTStatement switch_body = null;
if (LT(1) != IToken.tEOC)
switch_body = statement();
IASTStatement switch_body = parseSwitchBody();
IASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode) switch_statement).setOffsetAndLength(startOffset,
(switch_body != null ? calculateEndOffset(switch_body) : LA(1).getEndOffset()) - startOffset);

View file

@ -4799,10 +4799,8 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
default:
throwBacktrack(LA(1));
}
IASTStatement switch_body = null;
if (LT(1) != IToken.tEOC)
switch_body = statement();
IASTStatement switch_body = parseSwitchBody();
ICPPASTSwitchStatement switch_statement = createSwitchStatement();
((ASTNode) switch_statement).setOffsetAndLength(startOffset,
(switch_body != null ? calculateEndOffset(switch_body) : LA(1).getEndOffset()) - startOffset);