[clang] [clang-repl] fix CleanUpPTU by removing decl according to C implicitly FuncitonDecl. (PR #178648)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 10 08:36:37 PST 2026


================
@@ -182,6 +183,30 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) {
     }
   }
 
+  auto *ECCD = S.getASTContext().getExternCContextDecl();
+  if (auto *Map = ECCD->getPrimaryContext()->getLookupPtr()) {
+    for (auto &&[Key, List] : *Map) {
+      DeclContextLookupResult R = List.getLookupResult();
+      std::vector<NamedDecl *> NamedDeclsToRemove;
+      for (NamedDecl *D : R) {
+        // Implicitly generated C decl is not attached to the current TU but
+        // lexically attached to the recent TU, so we need to check the lexical
+        // context.
----------------
fogsong233 wrote:

 The total stack from parse to really add it is here:
 ```
 #1 0x00005649db60da48 clang::DeclContext::makeDeclVisibleInContextImpl(clang::NamedDecl*, bool) (/home/kacent/llvm-project/build2/bin/clang-repl+0x3aa3a48)
 #2 0x00005649db60f024 clang::DeclContext::makeDeclVisibleInContextWithFlags(clang::NamedDecl*, bool, bool) (/home/kacent/llvm-project/build2/bin/clang-repl+0x3aa5024)
 #3 0x00005649dd9d50ed clang::Sema::ActOnFunctionDeclarator(clang::Scope*, clang::Declarator&, clang::DeclContext*, clang::TypeSourceInfo*, clang::LookupResult&, llvm::MutableArrayRef<clang::TemplateParameterList*>, bool&) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e6b0ed)
 #4 0x00005649dd9d89ec clang::Sema::HandleDeclarator(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef<clang::TemplateParameterList*>) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e6e9ec)
 #5 0x00005649dd9d982a clang::Sema::ActOnDeclarator(clang::Scope*, clang::Declarator&) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e6f82a)
 #6 0x00005649dd9dac64 clang::Sema::ImplicitlyDefineFunction(clang::SourceLocation, clang::IdentifierInfo&, clang::Scope*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e70c64)
 #7 0x00005649dd9ded32 clang::Sema::ClassifyName(clang::Scope*, clang::CXXScopeSpec&, clang::IdentifierInfo*&, clang::SourceLocation, clang::Token const&, clang::CorrectionCandidateCallback*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e74d32)
 #8 0x00005649dd3ddf4d clang::Parser::TryAnnotateName(clang::CorrectionCandidateCallback*, clang::ImplicitTypenameContext) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5873f4d)
 #9 0x00005649dd4b6d61 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x594cd61)
#10 0x00005649dd4b8762 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x594e762)
#11 0x00005649dd3f5c99 clang::Parser::ParseTopLevelStmtDecl() (/home/kacent/llvm-project/build2/bin/clang-repl+0x588bc99)
#12 0x00005649dd3e9fbc clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x587ffbc)
#13 0x00005649dd3ea7fd clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/home/kacent/llvm-project/build2/bin/clang-repl+0x58807fd)
#14 0x00005649dd3eac81 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5880c81)
#15 0x00005649dc0ec504 clang::IncrementalParser::ParseOrWrapTopLevelDecl() (/home/kacent/llvm-project/build2/bin/clang-repl+0x4582504)
#16 0x00005649dc0ed6bd clang::IncrementalParser::Parse(llvm::StringRef) (/home/kacent/llvm-project/build2/bin/clang-repl+0x45836bd)
#17 0x00005649dc0e2acd clang::Interpreter::Parse(llvm::StringRef) (/home/kacent/llvm-project/build2/bin/clang-repl+0x4578acd)
#18 0x00005649dc0e337d clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x457937d)
```
You can see the main logic in
```
 #6 0x00005649dd9dac64 clang::Sema::ImplicitlyDefineFunction(clang::SourceLocation, clang::IdentifierInfo&, clang::Scope*) (/home/kacent/llvm-project/build2/bin/clang-repl+0x5e70c64)
 ```

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


More information about the cfe-commits mailing list