-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-repl] fix top-level statement declaration context #75547
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Pavel Kalugin (p4vook) ChangesChange 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 Full diff: https://github.com/llvm/llvm-project/pull/75547.diff 3 Files Affected:
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;
}
|
|
3127359
to
bf1bd0d
Compare
I should probably add a test to fix this behavior, but I haven't figured out how to run the tests locally yet. |
Apparently, we should only clean up parts of the PTU that have changed. Dear reviewers, do you have any idea how that could be implemented? |
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: llvm#73632 Signed-off-by: Pavel Kalugin <pavel@pavelthebest.me>
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.
fd15ef3
to
677ebce
Compare
Looking for someone with knowledge of Clang AST, because I can't figure out the correct context to insert the statement into. Current implementation breaks "for" loop. |
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, #72980