From c98bf28da729205daf67b22e39f068ddbbd9479d Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 1 Apr 2009 11:56:47 +0000 Subject: [PATCH] Fix case ranges, bug 270430. --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 23 ++++++++++++-- .../ASTWriterDeclaratorTestSource.awts | 2 +- .../rewrite/ASTWriterStatementTestSource.awts | 30 +++++++++++++++++++ .../parser/AbstractGNUSourceCodeParser.java | 2 +- .../rewrite/astwriter/ExpressionWriter.java | 4 +-- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 11196402bc8..90ea39c5c2c 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.ExpansionOverlapsBoundaryException; import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression; import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTCaseStatement; import org.eclipse.cdt.core.dom.ast.IASTCastExpression; import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; @@ -4840,8 +4841,26 @@ public class AST2Tests extends AST2BaseTest { // } public void testCaseRange_Bug211882() throws Exception { final String code = getAboveComment(); - parseAndCheckBindings(code, ParserLanguage.C, true); - parseAndCheckBindings(code, ParserLanguage.CPP, true); + { + IASTTranslationUnit tu = parseAndCheckBindings(code, ParserLanguage.C, true); + IASTCompoundStatement body = (IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[0]).getBody(); + IASTSwitchStatement switchStmt = (IASTSwitchStatement)body.getStatements()[0]; + IASTCaseStatement caseStmt = (IASTCaseStatement)((IASTCompoundStatement)switchStmt.getBody()).getStatements()[0]; + IASTBinaryExpression binExpr = (IASTBinaryExpression)caseStmt.getExpression(); + assertTrue(binExpr.getOperator() == IASTBinaryExpression.op_ellipses); + assertTrue(binExpr.getOperand1() instanceof IASTLiteralExpression); + assertTrue(binExpr.getOperand2() instanceof IASTLiteralExpression); + } + { + IASTTranslationUnit tu = parseAndCheckBindings(code, ParserLanguage.CPP, true); + IASTCompoundStatement body = (IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[0]).getBody(); + IASTSwitchStatement switchStmt = (IASTSwitchStatement)body.getStatements()[0]; + IASTCaseStatement caseStmt = (IASTCaseStatement)((IASTCompoundStatement)switchStmt.getBody()).getStatements()[0]; + IASTBinaryExpression binExpr = (IASTBinaryExpression)caseStmt.getExpression(); + assertTrue(binExpr.getOperator() == IASTBinaryExpression.op_ellipses); + assertTrue(binExpr.getOperand1() instanceof IASTLiteralExpression); + assertTrue(binExpr.getOperand2() instanceof IASTLiteralExpression); + } } // template class X { diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclaratorTestSource.awts b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclaratorTestSource.awts index 904a520af14..a1e74cdc39e 100644 --- a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclaratorTestSource.awts +++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclaratorTestSource.awts @@ -56,7 +56,7 @@ TestClass::TestClass(int a) } void undefPar(const char *c) throw (int); -virtual void pure() =0; +virtual void pure() = 0; int getV() const; int vol() volatile; diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterStatementTestSource.awts b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterStatementTestSource.awts index ac59d068cff..0d1dfe14ba6 100644 --- a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterStatementTestSource.awts +++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterStatementTestSource.awts @@ -206,6 +206,36 @@ void foo() } +//!GNUSwitchStatementTest +//%CPP +int foo(int a) +{ + switch (a){ + case 1: + return 1; + case 2 ... 4: + return 0; + default: + return -1; + } +} + + +//!GNUSwitchStatementTest +//%C +int foo(int a) +{ + switch (a){ + case 1: + return 1; + case 2 ... 4: + return 0; + default: + return -1; + } +} + + //!WhileStatementTest //%CPP void foo() diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java index b8178ed989e..e71d02e799e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java @@ -2020,7 +2020,7 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { if (lt1 == IToken.tELLIPSIS) { consume(); IASTExpression upperBoundExpression= constantExpression(); - caseExpression = buildBinaryExpression(IASTBinaryExpression.op_assign, + caseExpression = buildBinaryExpression(IASTBinaryExpression.op_ellipses, caseExpression, upperBoundExpression, calculateEndOffset(upperBoundExpression)); lt1= LT(1); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java index 2693a6c8073..673529a4ef0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ExpressionWriter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -82,7 +82,7 @@ public class ExpressionWriter extends NodeWriter{ private static final String MAX_OP = " >? "; //$NON-NLS-1$ private static final String PMARROW_OP = "->*"; //$NON-NLS-1$ private static final String PMDOT_OP = ".*"; //$NON-NLS-1$ - private static final String ELLIPSES = "..."; //$NON-NLS-1$ + private static final String ELLIPSES = " ... "; //$NON-NLS-1$ private static final String NOT_EQUALS_OP = " != "; //$NON-NLS-1$ private static final String EQUALS_OP = " == "; //$NON-NLS-1$ private static final String BINARY_OR_ASSIGN = " |= "; //$NON-NLS-1$