From 2afd0be069e87664b64bea0229f266fd8192ebf1 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 25 Nov 2008 03:22:00 +0000 Subject: [PATCH] Simple parsing of exception specifications, with no semantic analysis yet llvm-svn: 60005 --- clang/include/clang/Parse/Parser.h | 1 + clang/lib/Parse/ParseDecl.cpp | 12 ++++++--- clang/lib/Parse/ParseDeclCXX.cpp | 33 ++++++++++++++++++++++++ clang/test/Parser/cxx-exception-spec.cpp | 15 +++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 clang/test/Parser/cxx-exception-spec.cpp diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index e505e4d9cafb..47b119181821 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -492,6 +492,7 @@ private: //===--------------------------------------------------------------------===// // C++ 15: C++ Throw Expression ExprResult ParseThrowExpression(); + bool ParseExceptionSpecification(); //===--------------------------------------------------------------------===// // C++ 2.13.5: C++ Boolean Literals diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 1b5a0a5e7a35..eaf9f8b0f98a 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -1653,7 +1653,10 @@ void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, DeclSpec DS; if (getLang().CPlusPlus) { ParseTypeQualifierListOpt(DS); - // FIXME: Parse exception-specification[opt]. + + // Parse exception-specification[opt]. + if (Tok.is(tok::kw_throw)) + ParseExceptionSpecification(); } // Remember that we parsed a function type, and remember the attributes. @@ -1783,11 +1786,14 @@ void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, // If we have the closing ')', eat it. MatchRHSPunctuation(tok::r_paren, LParenLoc); - // cv-qualifier-seq[opt]. DeclSpec DS; if (getLang().CPlusPlus) { + // Parse cv-qualifier-seq[opt]. ParseTypeQualifierListOpt(DS); - // FIXME: Parse exception-specification[opt]. + + // Parse exception-specification[opt]. + if (Tok.is(tok::kw_throw)) + ParseExceptionSpecification(); } // Remember that we parsed a function type, and remember the attributes. diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 17f5763c1d60..9e380c903694 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -760,3 +760,36 @@ Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) { LParenLoc, &ArgExprs[0], ArgExprs.size(), &CommaLocs[0], RParenLoc); } + +/// ParseExceptionSpecification - Parse a C++ exception-specification +/// (C++ [except.spec]). +/// +/// exception-specification: +/// 'throw' '(' type-id-list [opt] ')' +/// +/// type-id-list: +/// type-id +/// type-id-list ',' type-id +/// +bool Parser::ParseExceptionSpecification() { + assert(Tok.is(tok::kw_throw) && "expected throw"); + + SourceLocation ThrowLoc = ConsumeToken(); + + if (!Tok.is(tok::l_paren)) { + return Diag(Tok, diag::err_expected_lparen_after) << "throw"; + } + SourceLocation LParenLoc = ConsumeParen(); + + // Parse the sequence of type-ids. + while (Tok.isNot(tok::r_paren)) { + ParseTypeName(); + if (Tok.is(tok::comma)) + ConsumeToken(); + else + break; + } + + SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); + return false; +} diff --git a/clang/test/Parser/cxx-exception-spec.cpp b/clang/test/Parser/cxx-exception-spec.cpp new file mode 100644 index 000000000000..47e9ffb50b31 --- /dev/null +++ b/clang/test/Parser/cxx-exception-spec.cpp @@ -0,0 +1,15 @@ +// RUN: clang -fsyntax-only %s + +struct X { }; + +struct Y { }; + +void f() throw() { } + +void g(int) throw(X) { } + +void h() throw(X, Y) { } + +class Class { + void foo() throw (X, Y) { } +};