[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:20:54 PST 2023
https://github.com/p4vook updated https://github.com/llvm/llvm-project/pull/75547
>From bf1bd0d9618d05ce718a9d834727ff25c3e92a18 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 | 3 ++-
clang/lib/AST/Decl.cpp | 4 ++--
clang/lib/Sema/SemaDecl.cpp | 4 ++--
3 files changed, 6 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..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