From 81a7a466e948fb152b46d85641ed2bf23968649a Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 11 Apr 2008 06:47:44 +0000 Subject: [PATCH] Support for case-ranges, bug 211882. --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 11 ++++++ .../core/dom/ast/IASTBinaryExpression.java | 36 +++++++++++++++++-- .../dom/ast/cpp/ICPPASTBinaryExpression.java | 10 +++--- .../ast/gnu/cpp/IGPPASTBinaryExpression.java | 11 +++--- .../GNUScannerExtensionConfiguration.java | 6 ++++ .../eclipse/cdt/core/parser/GCCKeywords.java | 10 ++++-- .../parser/AbstractGNUSourceCodeParser.java | 14 ++++++-- 7 files changed, 83 insertions(+), 15 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 ed4dc63f18a..0f46b607f46 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 @@ -4493,4 +4493,15 @@ public class AST2Tests extends AST2BaseTest { parseAndCheckBindings(code, ParserLanguage.C, true); parseAndCheckBindings(code, ParserLanguage.CPP, true); } + + // void test(int count) { + // switch(count) { + // case 1 ... 3: break; + // } + // } + public void testCaseRange_Bug211882() throws Exception { + final String code = getAboveComment(); + parseAndCheckBindings(code, ParserLanguage.C, true); + parseAndCheckBindings(code, ParserLanguage.CPP, true); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryExpression.java index f2e045c3098..c0eb4c67ee5 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryExpression.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.core.dom.ast; + /** * This interface represents a binary expression. * @@ -194,9 +195,40 @@ public interface IASTBinaryExpression extends IASTExpression { public static final int op_notequals = 29; /** - * op_last is the field used in subinterfaces to start their operators at + * For c==, only. + * op_pmdot pointer-to-member field dereference. */ - public static final int op_last = op_notequals; + public static final int op_pmdot = 30; + + /** + * For c++, only. + * op_pmarrow pointer-to-member pointer dereference. + */ + public static final int op_pmarrow = 31; + + /** + * For g++, only. + * op_max represents >? + */ + public static final int op_max = 32; + + /** + * For g++, only. + * op_min represents op_ellipses represents ... as used for case ranges. + */ + public static final int op_ellipses= 34; + + /** + * @deprecated all constants must be defined here, to avoid using the same value twice. + */ + @Deprecated + public static final int op_last = op_ellipses; /** * Get the first operand. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTBinaryExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTBinaryExpression.java index 964dd009915..5c225ab099b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTBinaryExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTBinaryExpression.java @@ -22,15 +22,17 @@ public interface ICPPASTBinaryExpression extends IASTBinaryExpression { /** * op_pmdot pointer-to-member field dereference. */ - public static final int op_pmdot = IASTBinaryExpression.op_last + 1; + public static final int op_pmdot = IASTBinaryExpression.op_pmdot; /** * op_pmarrow pointer-to-member pointer dereference. */ - public static final int op_pmarrow = IASTBinaryExpression.op_last + 2; + public static final int op_pmarrow = IASTBinaryExpression.op_pmarrow; /** - * op_last is defined for subinterfaces to further extend. + * @deprecated all constants must be defined in {@link IASTBinaryExpression}, to avoid + * duplicate usage of the same constant. */ - public static final int op_last = op_pmarrow; + @Deprecated + public static final int op_last = IASTBinaryExpression.op_last; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/gnu/cpp/IGPPASTBinaryExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/gnu/cpp/IGPPASTBinaryExpression.java index 7b97592569d..f4edba4721d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/gnu/cpp/IGPPASTBinaryExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/gnu/cpp/IGPPASTBinaryExpression.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.core.dom.ast.gnu.cpp; +import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression; /** @@ -22,16 +23,18 @@ public interface IGPPASTBinaryExpression extends ICPPASTBinaryExpression { /** * op_max represents >? */ - public static final int op_max = ICPPASTBinaryExpression.op_last + 1; + public static final int op_max = IASTBinaryExpression.op_max; /** * op_min represents op_last provided for sub-interfaces to extend. + * @deprecated all constants must be defined in {@link IASTBinaryExpression} to avoid + * using a constant twice. */ - public static final int op_last = op_min; + @Deprecated + public static final int op_last = IASTBinaryExpression.op_last; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/GNUScannerExtensionConfiguration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/GNUScannerExtensionConfiguration.java index c727a541294..a93316e84a8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/GNUScannerExtensionConfiguration.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/GNUScannerExtensionConfiguration.java @@ -43,17 +43,23 @@ public abstract class GNUScannerExtensionConfiguration extends AbstractScannerEx public static void addAdditionalGNUKeywords(CharArrayIntMap target) { target.put(GCCKeywords.cp__ALIGNOF__, IGCCToken.t___alignof__ ); + target.put(GCCKeywords.cp__ALIGNOF__, IGCCToken.t___alignof__ ); + target.put(GCCKeywords.cp__ASM, IToken.t_asm); target.put(GCCKeywords.cp__ASM__, IToken.t_asm); target.put(GCCKeywords.cp__ATTRIBUTE, IGCCToken.t__attribute__ ); target.put(GCCKeywords.cp__ATTRIBUTE__, IGCCToken.t__attribute__ ); target.put(GCCKeywords.cp__CONST, IToken.t_const); target.put(GCCKeywords.cp__CONST__, IToken.t_const); target.put(GCCKeywords.cp__DECLSPEC, IGCCToken.t__declspec ); + target.put(GCCKeywords.cp__INLINE, IToken.t_inline); target.put(GCCKeywords.cp__INLINE__, IToken.t_inline); target.put(GCCKeywords.cp__RESTRICT, IToken.t_restrict); target.put(GCCKeywords.cp__RESTRICT__, IToken.t_restrict); + target.put(GCCKeywords.cp__VOLATILE, IToken.t_volatile); target.put(GCCKeywords.cp__VOLATILE__, IToken.t_volatile); + target.put(GCCKeywords.cp__SIGNED, IToken.t_signed); target.put(GCCKeywords.cp__SIGNED__, IToken.t_signed); + target.put(GCCKeywords.cp__TYPEOF, IGCCToken.t_typeof); target.put(GCCKeywords.cp__TYPEOF__, IGCCToken.t_typeof); target.put(GCCKeywords.cpTYPEOF, IGCCToken.t_typeof ); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java index 304147916ce..c39046aae1d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java @@ -27,14 +27,20 @@ public class GCCKeywords { public static final char [] cp__ATTRIBUTE__ = __ATTRIBUTE__.toCharArray(); public static final char [] cp__DECLSPEC = __DECLSPEC.toCharArray(); + public static final char [] cp__ALIGNOF = "__alignof".toCharArray(); //$NON-NLS-1$ public static final char [] cp__ATTRIBUTE = "__attribute".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__ASM= "__asm".toCharArray(); //$NON-NLS-1$ public static final char [] cp__ASM__= "__asm__".toCharArray(); //$NON-NLS-1$ - public static final char [] cp__CONST__= "__const__".toCharArray(); //$NON-NLS-1$ public static final char [] cp__CONST= "__const".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__CONST__= "__const__".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__INLINE= "__inline".toCharArray(); //$NON-NLS-1$ public static final char [] cp__INLINE__= "__inline__".toCharArray(); //$NON-NLS-1$ - public static final char [] cp__RESTRICT__= "__restrict__".toCharArray(); //$NON-NLS-1$ public static final char [] cp__RESTRICT= "__restrict".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__RESTRICT__= "__restrict__".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__VOLATILE= "__volatile".toCharArray(); //$NON-NLS-1$ public static final char [] cp__VOLATILE__= "__volatile__".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__SIGNED= "__signed".toCharArray(); //$NON-NLS-1$ public static final char [] cp__SIGNED__= "__signed__".toCharArray(); //$NON-NLS-1$ + public static final char [] cp__TYPEOF= "__typeof".toCharArray(); //$NON-NLS-1$ public static final char [] cp__TYPEOF__= "__typeof__".toCharArray(); //$NON-NLS-1$ } 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 84e7aca8f76..c1e39dab151 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 @@ -1689,9 +1689,17 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { protected IASTStatement parseCaseStatement() throws EndOfFileException, BacktrackException { int startOffset = consume().getOffset(); // t_case - IASTExpression case_exp = constantExpression(); + IASTExpression caseExpression = constantExpression(); + int lt1 = LT(1); + if (lt1 == IToken.tELLIPSIS) { + consume(); + IASTExpression upperBoundExpression= constantExpression(); + caseExpression = buildBinaryExpression(IASTBinaryExpression.op_assign, + caseExpression, upperBoundExpression, calculateEndOffset(upperBoundExpression)); + lt1= LT(1); + } int lastOffset = 0; - switch (LT(1)) { + switch (lt1) { case IToken.tCOLON: case IToken.tEOC: lastOffset = consume().getEndOffset(); @@ -1702,7 +1710,7 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { IASTCaseStatement cs = createCaseStatement(); ((ASTNode) cs).setOffsetAndLength(startOffset, lastOffset - startOffset); - cs.setExpression(case_exp); + cs.setExpression(caseExpression); return cs; }