[clang] Issue #63106: [сlang] Representation of ellipsis in AST (PR #80976)
Shahid Iqbal via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 7 03:26:27 PST 2024
https://github.com/shahidiqbal13 created https://github.com/llvm/llvm-project/pull/80976
- Raising for preliminary review (forget about formating)
- Fixed the '...' catch all ellipsis in catch stmt
- Will fix the failed testcases once we get agree on this
- I don't think we need to support for the variadic function support ellipsis token this usecase [void bar(int, ...); ]
- See the issue here: https://github.com/llvm/llvm-project/issues/63106
Output of the fixed 1st issue (catch(...))
```
`-FunctionDecl 0x55bdaf78d9a0 <test1.cpp:1:1, line:4:1> line:1:6 foo 'void ()'
`-CompoundStmt 0x55bdaf78dbb8 <col:12, line:4:1>
`-CXXTryStmt 0x55bdaf78db98 <line:2:3, line:3:14>
|-CompoundStmt 0x55bdaf78da90 <line:2:7, col:8>
`-CXXCatchStmt 0x55bdaf78db78 <line:3:3, col:14>
|-VarDecl 0x55bdaf78db00 <<invalid sloc>, col:12> col:12 catch_all '<unknown type>'
`-CompoundStmt 0x55bdaf78db68 <col:13, col:14>
```
>From b6bfb18e25c111baf6c95a0a4a1c3d667bb25b6d Mon Sep 17 00:00:00 2001
From: Shahid Iqbal <shahidiqbal13 at gmail.com>
Date: Thu, 16 Nov 2023 11:26:43 -0500
Subject: [PATCH 1/2] TESTING infra
---
clang/NOTES.txt | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/NOTES.txt b/clang/NOTES.txt
index f06ea8c70cd340..c83dda52a1fc21 100644
--- a/clang/NOTES.txt
+++ b/clang/NOTES.txt
@@ -4,6 +4,8 @@
//===---------------------------------------------------------------------===//
+//TESTING git infra//
+
To time GCC preprocessing speed without output, use:
"time gcc -MM file"
This is similar to -Eonly.
>From 79c592853c77fb0a96746d860be63d4605ea87e7 Mon Sep 17 00:00:00 2001
From: Shahid Iqbal <shahidiqbal13 at gmail.com>
Date: Wed, 7 Feb 2024 06:08:10 -0500
Subject: [PATCH 2/2] =?UTF-8?q?Issue=20#63106:=20[=D1=81lang]=20Representa?=
=?UTF-8?q?tion=20of=20ellipsis=20in=20AST?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
clang/include/clang/AST/DeclBase.h | 11 +++++++++--
clang/include/clang/Sema/Sema.h | 2 +-
clang/lib/AST/TextNodeDumper.cpp | 3 +++
clang/lib/Parse/ParseStmt.cpp | 14 +++++++++++---
clang/lib/Sema/SemaDeclCXX.cpp | 7 +++++--
clang/test/AST/ast-dump-stmt.cpp | 2 +-
6 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 5b1038582bc674..67f5c7dd1b7f98 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -301,6 +301,9 @@ class alignas(8) Decl {
LLVM_PREFERRED_TYPE(bool)
unsigned Implicit : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned HasCatchEllipsis : 1;
+
/// Whether this declaration was "used", meaning that a definition is
/// required.
LLVM_PREFERRED_TYPE(bool)
@@ -394,7 +397,7 @@ class alignas(8) Decl {
Decl(Kind DK, DeclContext *DC, SourceLocation L)
: NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
- Implicit(false), Used(false), Referenced(false),
+ Implicit(false), HasCatchEllipsis(false), Used(false), Referenced(false),
TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
@@ -402,7 +405,7 @@ class alignas(8) Decl {
}
Decl(Kind DK, EmptyShell Empty)
- : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
+ : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false), HasCatchEllipsis(false),
Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
Access(AS_none), FromASTFile(0),
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
@@ -597,6 +600,10 @@ class alignas(8) Decl {
bool isImplicit() const { return Implicit; }
void setImplicit(bool I = true) { Implicit = I; }
+ /// isCatchEllipsisTok - Indicates whether '...' is present in the catch decl
+ bool isCatchEllipsisTok() const { return HasCatchEllipsis; }
+ void setCatchEllipsisTok(bool I = true) { HasCatchEllipsis = I; }
+
/// Whether *any* (re-)declaration of the entity was used, meaning that
/// a definition is required.
///
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 501dc01200a1c3..ffc6d2926f85f2 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/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index e8274fcd5cfe9c..1da20cdfa36a66 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -272,6 +272,9 @@ void TextNodeDumper::Visit(const Decl *D) {
if (D->isImplicit())
OS << " implicit";
+ if (D->isCatchEllipsisTok())
+ OS << " catch_all ";
+
if (D->isUsed())
OS << " used";
else if (D->isThisDeclarationReferenced())
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 2531147c23196a..56c1e86afd4811 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -2698,9 +2698,17 @@ StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Declarator ExDecl(DS, Attributes, DeclaratorContext::CXXCatch);
ParseDeclarator(ExDecl);
ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
- } else
- ConsumeToken();
-
+ }
+ else {
+ 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);
+ ExceptionDecl->setCatchEllipsisTok();
+ }
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return StmtError();
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 3688192e6cbe5c..1916d3c3d8ebd3 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(),
@@ -17021,7 +17024,7 @@ Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) {
VarDecl *ExDecl = BuildExceptionDeclaration(
S, TInfo, D.getBeginLoc(), D.getIdentifierLoc(), D.getIdentifier());
- if (Invalid)
+ if (Invalid && !isCatchAll)
ExDecl->setInvalidDecl();
// Add the exception declaration into this scope.
diff --git a/clang/test/AST/ast-dump-stmt.cpp b/clang/test/AST/ast-dump-stmt.cpp
index 407584e5b82de4..a5a7e9d535c1d7 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 {{.*}} '<unknown type>'
// CHECK-NEXT: CompoundStmt
catch (...) {
}
More information about the cfe-commits
mailing list