================ @@ -5696,8 +5696,11 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() { TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope()); StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx); Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get()); - if (!R.isUsable()) + if (!R.isUsable()) { + if (DeclContext *DC = TLSD->getDeclContext()) + DC->removeDecl(TLSD); // unlink from TU ---------------- devajithvs wrote:
Yes, we could do something like: ```diff diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp index 6343f17ed822..893aea55e9ab 100644 --- a/clang/lib/Interpreter/IncrementalParser.cpp +++ b/clang/lib/Interpreter/IncrementalParser.cpp @@ -175,6 +175,9 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) { // FIXME: We should de-allocate MostRecentTU for (Decl *D : MostRecentTU->decls()) { + // Remove any TopLevelStmtDecl created for statements that failed to parse. + if (auto *TLSD = dyn_cast<TopLevelStmtDecl>(D)) + if (!TLSD->getStmt()) + MostRecentTU->removeDecl(TLSD); auto *ND = dyn_cast<NamedDecl>(D); if (!ND || ND->getDeclName().isEmpty()) continue; ``` Does that work? I’m wondering, though, if we should fix this at the source: when ParseTopLevelStmtDecl sees !R.isUsable(), we immediately unlink the TopLevelStmtDecl we just attached. That ensures we never see a TU with an invalid TopLevelStmtDecl between parse and cleanup. https://github.com/llvm/llvm-project/pull/153945 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits