[clang] [clang-repl] fix top-level statement declaration context (PR #75547)

Pavel Kalugin via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 15 10:52:48 PST 2023


https://github.com/p4vook updated https://github.com/llvm/llvm-project/pull/75547

>From 97c3ed6c8a007dbca32416ebb31cf4c3bd52b264 Mon Sep 17 00:00:00 2001
From: Pavel Kalugin <pavel at pavelthebest.me>
Date: Fri, 15 Dec 2023 03:07:06 +0300
Subject: [PATCH 1/3] [clang-repl] fix top-level statement declaration context

Change the declaration context where we insert top-level
statements to the current enclosing namespace context.

Previously, top-level statement declarations were inserted
directly into the translation unit. This is incorrect, as
it leads to ignoring such statements located inside namespaces.

Fixes: #73632
Signed-off-by: Pavel Kalugin <pavel at pavelthebest.me>
---
 clang/include/clang/AST/Decl.h | 3 ++-
 clang/lib/AST/Decl.cpp         | 4 ++--
 clang/lib/Sema/SemaDecl.cpp    | 5 +++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index cd0878d7082514..7b1d9e8be6c59a 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -4440,7 +4440,8 @@ class TopLevelStmtDecl : public Decl {
   virtual void anchor();
 
 public:
-  static TopLevelStmtDecl *Create(ASTContext &C, Stmt *Statement);
+  static TopLevelStmtDecl *Create(ASTContext &C, DeclContext *DC,
+                                  Stmt *Statement);
   static TopLevelStmtDecl *CreateDeserialized(ASTContext &C, unsigned ID);
 
   SourceRange getSourceRange() const override LLVM_READONLY;
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 527ea6042daa03..5c0f4f5b6b4ca1 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5513,13 +5513,13 @@ FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
 
 void TopLevelStmtDecl::anchor() {}
 
-TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) {
+TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, DeclContext *DC,
+                                           Stmt *Statement) {
   assert(Statement);
   assert(C.getLangOpts().IncrementalExtensions &&
          "Must be used only in incremental mode");
 
   SourceLocation BeginLoc = Statement->getBeginLoc();
-  DeclContext *DC = C.getTranslationUnitDecl();
 
   return new (C, DC) TopLevelStmtDecl(DC, BeginLoc, Statement);
 }
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index be6a136ef37bc4..f88c2f9a76b232 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20287,8 +20287,9 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
 }
 
 Decl *Sema::ActOnTopLevelStmtDecl(Stmt *Statement) {
-  auto *New = TopLevelStmtDecl::Create(Context, Statement);
-  Context.getTranslationUnitDecl()->addDecl(New);
+  DeclContext* NamespaceContext = CurContext->getEnclosingNamespaceContext();
+  auto *New = TopLevelStmtDecl::Create(Context, NamespaceContext, Statement);
+  NamespaceContext->addDecl(New);
   return New;
 }
 

>From 677ebce95e29ca810cb6489583cf7eb8a929a4d2 Mon Sep 17 00:00:00 2001
From: Pavel Kalugin <pavel at pavelthebest.me>
Date: Fri, 15 Dec 2023 15:11:43 +0300
Subject: [PATCH 2/3] [clang-repl] fix cleanup context in CleanUpPTU()

Get the last context returned by collectAllContexts() instead of just
the primary context.

This fixes cleanup in nested namespaces: instead of cleaning up all the
namespace hierarchy it cleans up just the innermost namespace.
---
 clang/lib/Interpreter/IncrementalParser.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbfee8b014..feeca0b6c7cd11 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -374,7 +374,17 @@ std::unique_ptr<llvm::Module> IncrementalParser::GenModule() {
 void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
   TranslationUnitDecl *MostRecentTU = PTU.TUPart;
   TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
-  if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
+  if (!FirstTU) {
+    return;
+  }
+
+  SmallVector<DeclContext *> contexts;
+  FirstTU->collectAllContexts(contexts);
+  if (contexts.empty()) {
+    return;
+  }
+
+  if (StoredDeclsMap *Map = contexts.back()->getLookupPtr()) {
     for (auto I = Map->begin(); I != Map->end(); ++I) {
       StoredDeclsList &List = I->second;
       DeclContextLookupResult R = List.getLookupResult();

>From 1fa6bf71b90e99d58734f7935b6f5dcdfb55fab2 Mon Sep 17 00:00:00 2001
From: Pavel Kalugin <pavel at pavelthebest.me>
Date: Fri, 15 Dec 2023 21:52:23 +0300
Subject: [PATCH 3/3] fixup formatting

---
 clang/lib/Sema/SemaDecl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f88c2f9a76b232..ae73c0ba023777 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20287,7 +20287,7 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
 }
 
 Decl *Sema::ActOnTopLevelStmtDecl(Stmt *Statement) {
-  DeclContext* NamespaceContext = CurContext->getEnclosingNamespaceContext();
+  DeclContext *NamespaceContext = CurContext->getEnclosingNamespaceContext();
   auto *New = TopLevelStmtDecl::Create(Context, NamespaceContext, Statement);
   NamespaceContext->addDecl(New);
   return New;



More information about the cfe-commits mailing list