Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

p4vook
Copy link

@p4vook p4vook commented Dec 15, 2023

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

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2023

@llvm/pr-subscribers-clang

Author: Pavel Kalugin (p4vook)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/75547.diff

3 Files Affected:

  • (modified) clang/include/clang/AST/Decl.h (+1-1)
  • (modified) clang/lib/AST/Decl.cpp (+1-2)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+2-2)
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;
 }
 

Copy link

github-actions bot commented Dec 15, 2023

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

@p4vook p4vook changed the title clang-repl: fix top-level statement declaration context [clang-repl] fix top-level statement declaration context Dec 15, 2023
@p4vook
Copy link
Author

p4vook commented Dec 15, 2023

I should probably add a test to fix this behavior, but I haven't figured out how to run the tests locally yet.

@p4vook
Copy link
Author

p4vook commented Dec 15, 2023

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.
@p4vook
Copy link
Author

p4vook commented Apr 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-repl doesn't execute top-level code located inside a namespace
2 participants