[clang] [clang-repl] Names declared in if conditions and for-init statements are local to the inner context (C++ 3.3.2p4) (PR #84150)

Stefan Gränitz via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 6 04:03:48 PST 2024


================
@@ -20446,12 +20446,22 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
   return New;
 }
 
-Decl *Sema::ActOnTopLevelStmtDecl(Stmt *Statement) {
-  auto *New = TopLevelStmtDecl::Create(Context, Statement);
-  Context.getTranslationUnitDecl()->addDecl(New);
+TopLevelStmtDecl *Sema::ActOnStartTopLevelStmtDecl(Scope *S) {
+  auto *New = TopLevelStmtDecl::Create(Context, /*Statement=*/nullptr);
+  CurContext->addDecl(New);
----------------
weliveindetail wrote:

I think we have to add the `TopLevelStmtDecl` to the decl list of the outer context. This happens in other similar cases as well, `ActOnBlockStart()` for example:
https://github.com/llvm/llvm-project/blob/release/18.x/clang/lib/Sema/SemaExpr.cpp#L16954

> Why does the next line not add it to the CurContext?

Not sure about the why, but it doesn't go through `DeclContext::addDecl()`:
https://github.com/llvm/llvm-project/blob/release/18.x/clang/lib/AST/DeclBase.cpp#L1674

FYI: the individual fixup commits contain short notes [like this](https://github.com/llvm/llvm-project/pull/84150/commits/39d0f4ab17e9640acbca2ce5e7bf153d14846ab9)

https://github.com/llvm/llvm-project/pull/84150


More information about the cfe-commits mailing list