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

Pavel Kalugin via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 14 16:15:51 PST 2023


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

Change the declaration context where we insert top-level statements to CurrentContext.

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

>From 31273597beb9439f4c78123c48f8ecc9bf366e14 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] clang-repl: fix top-level statement declaration context

Change the declaration context where we insert top-level
statements to CurrentContext.

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 | 2 +-
 clang/lib/AST/Decl.cpp         | 3 +--
 clang/lib/Sema/SemaDecl.cpp    | 4 ++--
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index cd0878d7082514..62740d0ea16c75 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -4440,7 +4440,7 @@ 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..de0817e9a3d3a5 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5513,13 +5513,12 @@ 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..f78fada57f62d3 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20287,8 +20287,8 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
 }
 
 Decl *Sema::ActOnTopLevelStmtDecl(Stmt *Statement) {
-  auto *New = TopLevelStmtDecl::Create(Context, Statement);
-  Context.getTranslationUnitDecl()->addDecl(New);
+  auto *New = TopLevelStmtDecl::Create(Context, CurContext, Statement);
+  CurContext->addDecl(New);
   return New;
 }
 



More information about the cfe-commits mailing list