diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index cd0878d708251..d6e22ce6c4672 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -1053,6 +1053,10 @@ class VarDecl : public DeclaratorDecl, public Redeclarable { LLVM_PREFERRED_TYPE(bool) unsigned ExceptionVar : 1; + /// To Check the ellipsis + LLVM_PREFERRED_TYPE(bool) + unsigned EllipsisVar : 1; + /// Whether this local variable could be allocated in the return /// slot of its function, enabling the named return value optimization /// (NRVO). @@ -1474,6 +1478,16 @@ class VarDecl : public DeclaratorDecl, public Redeclarable { NonParmVarDeclBits.ExceptionVar = EV; } + /// Determine the Ellipsis (...) or not + bool isEllipsisVariable() const { + return isa(this) ? false : NonParmVarDeclBits.EllipsisVar; + } + void setEllipsisVariable(bool EV) { + assert(!isa(this)); + NonParmVarDeclBits.EllipsisVar = EV; + } + + /// Determine whether this local variable can be used with the named /// return value optimization (NRVO). /// diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 501dc01200a1c..ffc6d2926f85f 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -5322,7 +5322,7 @@ class Sema final { SourceLocation IdLoc, IdentifierInfo *Id); - Decl *ActOnExceptionDeclarator(Scope *S, Declarator &D); + Decl *ActOnExceptionDeclarator(Scope *S, Declarator &D, bool isCatchAll = false); StmtResult ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, Stmt *HandlerBlock); diff --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp index 637d06cee78c8..2a03ad30dd3ba 100644 --- a/clang/lib/AST/JSONNodeDumper.cpp +++ b/clang/lib/AST/JSONNodeDumper.cpp @@ -115,6 +115,10 @@ void JSONNodeDumper::Visit(const Decl *D) { else if (D->isThisDeclarationReferenced()) JOS.attribute("isReferenced", true); + if (const VarDecl *ND = dyn_cast(D)) + if (ND->isEllipsisVariable()) + JOS.attribute("catch_all", true); + if (const auto *ND = dyn_cast(D)) attributeOnlyIfTrue("isHidden", !ND->isUnconditionallyVisible()); diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp index e8274fcd5cfe9..056ea399d7e36 100644 --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -271,6 +271,9 @@ void TextNodeDumper::Visit(const Decl *D) { OS << " hidden"; if (D->isImplicit()) OS << " implicit"; + if (const VarDecl *ND = dyn_cast(D)) + if (ND->isEllipsisVariable()) + OS << " catch_all"; if (D->isUsed()) OS << " used"; diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 2531147c23196..83f2d451486fb 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -2698,9 +2698,16 @@ StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) { Declarator ExDecl(DS, Attributes, DeclaratorContext::CXXCatch); ParseDeclarator(ExDecl); ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); - } else - ConsumeToken(); - + } + else { + CatchLoc = ConsumeToken(); + // explicitly creating a var of type no-type for '...' and marking it as catch_all + ParsedAttributes Attributes(AttrFactory); + DeclSpec DS(AttrFactory); + Declarator ExDecl(DS, Attributes, DeclaratorContext::BlockLiteral); + ParseDeclarator(ExDecl); + ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl, true); + } T.consumeClose(); if (T.getCloseLocation().isInvalid()) return StmtError(); diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 3688192e6cbe5..93ee1b2e0891f 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -16983,7 +16983,7 @@ VarDecl *Sema::BuildExceptionDeclaration(Scope *S, /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch /// handler. -Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { +Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D, bool isCatchAll) { TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); bool Invalid = D.isInvalidType(); @@ -16994,6 +16994,9 @@ Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { D.getIdentifierLoc()); Invalid = true; } + if (isCatchAll) + TInfo = Context.getTrivialTypeSourceInfo(Context.UnknownAnyTy, + D.getIdentifierLoc()); IdentifierInfo *II = D.getIdentifier(); if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), @@ -17024,6 +17027,9 @@ Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { if (Invalid) ExDecl->setInvalidDecl(); + if (isCatchAll) + ExDecl->setEllipsisVariable(true); + // Add the exception declaration into this scope. if (II) PushOnScopeChains(ExDecl, S); diff --git a/clang/test/AST/ast-dump-stmt-json.cpp b/clang/test/AST/ast-dump-stmt-json.cpp index 667a12a012024..082a6c68f6d80 100644 --- a/clang/test/AST/ast-dump-stmt-json.cpp +++ b/clang/test/AST/ast-dump-stmt-json.cpp @@ -755,10 +755,10 @@ void TestDependentGenericSelectionExpr(Ty T) { // CHECK-NEXT: "kind": "CXXCatchStmt", // CHECK-NEXT: "range": { // CHECK-NEXT: "begin": { -// CHECK-NEXT: "offset": 352, +// CHECK-NEXT: "offset": 359, // CHECK-NEXT: "line": 24, -// CHECK-NEXT: "col": 3, -// CHECK-NEXT: "tokLen": 5 +// CHECK-NEXT: "col": 10, +// CHECK-NEXT: "tokLen": 3 // CHECK-NEXT: }, // CHECK-NEXT: "end": { // CHECK-NEXT: "offset": 368, @@ -769,7 +769,26 @@ void TestDependentGenericSelectionExpr(Ty T) { // CHECK-NEXT: }, // CHECK-NEXT: "inner": [ // CHECK-NEXT: { -// CHECK-NEXT: "id": "0x0" +// CHECK-NEXT: "id": "0x{{.*}}", +// CHECK-NEXT: "kind": "VarDecl", +// CHECK-NEXT: "loc": { +// CHECK-NEXT: "offset": 362, +// CHECK-NEXT: "line": 24, +// CHECK-NEXT: "col": 13, +// CHECK-NEXT: "tokLen": 1 +// CHECK-NEXT: }, +// CHECK-NEXT: "range": { +// CHECK-NEXT: "begin": {}, +// CHECK-NEXT: "end": { +// CHECK-NEXT: "offset": 362, +// CHECK-NEXT: "col": 13, +// CHECK-NEXT: "tokLen": 1 +// CHECK-NEXT: } +// CHECK-NEXT: }, +// CHECK-NEXT: "catch_all": true, +// CHECK-NEXT: "type": { +// CHECK-NEXT: "qualType": "" +// CHECK-NEXT: } // CHECK-NEXT: }, // CHECK-NEXT: { // CHECK-NEXT: "id": "0x{{.*}}", @@ -777,7 +796,6 @@ void TestDependentGenericSelectionExpr(Ty T) { // CHECK-NEXT: "range": { // CHECK-NEXT: "begin": { // CHECK-NEXT: "offset": 364, -// CHECK-NEXT: "line": 24, // CHECK-NEXT: "col": 15, // CHECK-NEXT: "tokLen": 1 // CHECK-NEXT: }, diff --git a/clang/test/AST/ast-dump-stmt.cpp b/clang/test/AST/ast-dump-stmt.cpp index 407584e5b82de..a5a7e9d535c1d 100644 --- a/clang/test/AST/ast-dump-stmt.cpp +++ b/clang/test/AST/ast-dump-stmt.cpp @@ -41,7 +41,7 @@ void TestCatch2() { try { } // CHECK-NEXT: CXXCatchStmt -// CHECK-NEXT: NULL +// CHECK-NEXT: VarDecl {{.*}} '' // CHECK-NEXT: CompoundStmt catch (...) { }