From 3319c8596e69fd30084d44a1f364beae61c6768d Mon Sep 17 00:00:00 2001 From: Hansruedi Patzen Date: Fri, 17 Nov 2017 13:14:55 +0100 Subject: Bug 527396: Parser includes curly brace when parsing noexcept functions Change-Id: I0d2626cccf5b093f2f3cc9fbcbeaedbb21ebd508 Signed-off-by: Hansruedi Patzen --- .../core/parser/tests/ast2/DOMLocationTests.java | 55 ++++++++++++++++++++++ .../core/dom/parser/cpp/GNUCPPSourceParser.java | 7 ++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java index 6136c3b48f1..1b9512e9ccc 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java @@ -58,6 +58,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression; import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock; @@ -647,7 +648,61 @@ public class DOMLocationTests extends AST2TestBase { assertSoleLocation(problems[1], code, "\"deprecated include\""); assertSoleLocation(problems[2], code, "#invalid"); } + + public void testBug527396_1() throws Exception { + String code = "void foo() noexcept {}"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + ICPPASTFunctionDefinition definition = (ICPPASTFunctionDefinition) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarator(); + String rawDeclarator = "foo() noexcept"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + + public void testBug527396_2() throws Exception { + String code = "void foo() noexcept(false) {}"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + ICPPASTFunctionDefinition definition = (ICPPASTFunctionDefinition) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarator(); + String rawDeclarator = "foo() noexcept(false)"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + + public void testBug527396_3() throws Exception { + String code = "void foo() {}"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + ICPPASTFunctionDefinition definition = (ICPPASTFunctionDefinition) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarator(); + String rawDeclarator = "foo()"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + + public void testBug527396_4() throws Exception { + String code = "void foo() noexcept;"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + IASTSimpleDeclaration definition = (IASTSimpleDeclaration) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarators()[0]; + String rawDeclarator = "foo() noexcept"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + public void testBug527396_5() throws Exception { + String code = "void foo() noexcept(false);"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + IASTSimpleDeclaration definition = (IASTSimpleDeclaration) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarators()[0]; + String rawDeclarator = "foo() noexcept(false)"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + + public void testBug527396_6() throws Exception { + String code = "void foo();"; //$NON-NLS-1$ + IASTTranslationUnit tu = parse(code, ParserLanguage.CPP); + IASTSimpleDeclaration definition = (IASTSimpleDeclaration) tu.getDeclarations()[0]; + ICPPASTDeclarator declarator = (ICPPASTDeclarator) definition.getDeclarators()[0]; + String rawDeclarator = "foo()"; //$NON-NLS-1$ + assertSoleLocation(declarator, code.indexOf(rawDeclarator), rawDeclarator.length()); + } + // int main(void){ // #define one 1 // int integer = one; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java index 2fc22a307cb..6afe8cca260 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java @@ -4659,15 +4659,14 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { // noexcept specification if (LT(1) == IToken.t_noexcept) { consume(); // noexcept - IASTExpression expression; + IASTExpression expression = ICPPASTFunctionDeclarator.NOEXCEPT_DEFAULT; + endOffset = getEndOffset(); if (LT(1) == IToken.tLPAREN) { consume(); // ( expression = expression(); consume(IToken.tRPAREN); //) - } else { - expression = ICPPASTFunctionDeclarator.NOEXCEPT_DEFAULT; + endOffset = getEndOffset(); } - endOffset = getEndOffset(); fc.setNoexceptExpression((ICPPASTExpression) expression); } -- cgit v1.2.3