From 89bd8d62f3c3305d4ec433307dc4c0b1df1d4be7 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Wed, 22 Oct 2014 17:50:19 +0000 Subject: [PATCH] Reland r219810 "Fix late template parsing leak with incremental processing" Original message: Add a second late template parser callback meant to cleanup any resources allocated by late template parsing. Call it from the Sema::ActOnEndOfTranslationUnit method after all pending template instantiations have been completed. Teach Parser::ParseTopLevelDecl to install the cleanup callback when incremental processing is enabled so that Parser::TemplateIds can be freed. Patch by Brad King! llvm-svn: 220400 --- clang/include/clang/Parse/Parser.h | 1 + clang/include/clang/Sema/Sema.h | 7 +++- clang/lib/Parse/Parser.cpp | 12 +++++- clang/lib/Sema/Sema.cpp | 4 ++ clang/unittests/Frontend/FrontendActionTest.cpp | 49 +++++++++++++++++++++++-- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 0de7254..6526d47 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -1155,6 +1155,7 @@ private: void ParseLateTemplatedFuncDef(LateParsedTemplate &LPT); static void LateTemplateParserCallback(void *P, LateParsedTemplate &LPT); + static void LateTemplateParserCleanupCallback(void *P); Sema::ParsingClassState PushParsingClass(Decl *TagOrTemplate, bool TopLevelClass, bool IsInterface); diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 3723654..ba15f38 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -480,11 +480,16 @@ public: /// \brief Callback to the parser to parse templated functions when needed. typedef void LateTemplateParserCB(void *P, LateParsedTemplate &LPT); + typedef void LateTemplateParserCleanupCB(void *P); LateTemplateParserCB *LateTemplateParser; + LateTemplateParserCleanupCB *LateTemplateParserCleanup; void *OpaqueParser; - void SetLateTemplateParser(LateTemplateParserCB *LTP, void *P) { + void SetLateTemplateParser(LateTemplateParserCB *LTP, + LateTemplateParserCleanupCB *LTPCleanup, + void *P) { LateTemplateParser = LTP; + LateTemplateParserCleanup = LTPCleanup; OpaqueParser = P; } diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 27a852e..93e893e 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -510,6 +510,13 @@ namespace { }; } +void Parser::LateTemplateParserCleanupCallback(void *P) { + // While this RAII helper doesn't bracket any actual work, the destructor will + // clean up annotations that were created during ActOnEndOfTranslationUnit + // when incremental processing is enabled. + DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(((Parser *)P)->TemplateIds); +} + /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the /// action tells us to. This returns true if the EOF was encountered. bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { @@ -542,7 +549,10 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) { case tok::eof: // Late template parsing can begin. if (getLangOpts().DelayedTemplateParsing) - Actions.SetLateTemplateParser(LateTemplateParserCallback, this); + Actions.SetLateTemplateParser(LateTemplateParserCallback, + PP.isIncrementalProcessingEnabled() ? + LateTemplateParserCleanupCallback : nullptr, + this); if (!PP.isIncrementalProcessingEnabled()) Actions.ActOnEndOfTranslationUnit(); //else don't tell Sema that we ended parsing: more input might come. diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index aae2008..901b669 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -88,6 +88,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, CodeSegStack(nullptr), CurInitSeg(nullptr), VisContext(nullptr), IsBuildingRecoveryCallExpr(false), ExprNeedsCleanups(false), LateTemplateParser(nullptr), + LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp), StdInitializerList(nullptr), CXXTypeInfoDecl(nullptr), MSVCGuidDecl(nullptr), NSNumberDecl(nullptr), @@ -672,6 +673,9 @@ void Sema::ActOnEndOfTranslationUnit() { } PerformPendingInstantiations(); + if (LateTemplateParserCleanup) + LateTemplateParserCleanup(OpaqueParser); + CheckDelayedMemberExceptionSpecs(); } diff --git a/clang/unittests/Frontend/FrontendActionTest.cpp b/clang/unittests/Frontend/FrontendActionTest.cpp index 3171156..5581c44 100644 --- a/clang/unittests/Frontend/FrontendActionTest.cpp +++ b/clang/unittests/Frontend/FrontendActionTest.cpp @@ -14,6 +14,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Lex/Preprocessor.h" +#include "clang/Sema/Sema.h" #include "llvm/ADT/Triple.h" #include "llvm/Support/MemoryBuffer.h" #include "gtest/gtest.h" @@ -25,10 +26,13 @@ namespace { class TestASTFrontendAction : public ASTFrontendAction { public: - TestASTFrontendAction(bool enableIncrementalProcessing = false) - : EnableIncrementalProcessing(enableIncrementalProcessing) { } + TestASTFrontendAction(bool enableIncrementalProcessing = false, + bool actOnEndOfTranslationUnit = false) + : EnableIncrementalProcessing(enableIncrementalProcessing), + ActOnEndOfTranslationUnit(actOnEndOfTranslationUnit) { } bool EnableIncrementalProcessing; + bool ActOnEndOfTranslationUnit; std::vector decl_names; virtual bool BeginSourceFileAction(CompilerInstance &ci, StringRef filename) { @@ -40,15 +44,22 @@ public: virtual std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { - return llvm::make_unique(decl_names); + return llvm::make_unique(CI, ActOnEndOfTranslationUnit, + decl_names); } private: class Visitor : public ASTConsumer, public RecursiveASTVisitor { public: - Visitor(std::vector &decl_names) : decl_names_(decl_names) {} + Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit, + std::vector &decl_names) : + CI(CI), ActOnEndOfTranslationUnit(ActOnEndOfTranslationUnit), + decl_names_(decl_names) {} virtual void HandleTranslationUnit(ASTContext &context) { + if (ActOnEndOfTranslationUnit) { + CI.getSema().ActOnEndOfTranslationUnit(); + } TraverseDecl(context.getTranslationUnitDecl()); } @@ -58,6 +69,8 @@ private: } private: + CompilerInstance &CI; + bool ActOnEndOfTranslationUnit; std::vector &decl_names_; }; }; @@ -102,6 +115,34 @@ TEST(ASTFrontendAction, IncrementalParsing) { EXPECT_EQ("x", test_action.decl_names[1]); } +TEST(ASTFrontendAction, LateTemplateIncrementalParsing) { + CompilerInvocation *invocation = new CompilerInvocation; + invocation->getLangOpts()->CPlusPlus = true; + invocation->getLangOpts()->DelayedTemplateParsing = true; + invocation->getPreprocessorOpts().addRemappedFile( + "test.cc", MemoryBuffer::getMemBuffer( + "template struct A { A(T); T data; };\n" + "template struct B: public A {\n" + " B();\n" + " B(B const& b): A(b.data) {}\n" + "};\n" + "B c() { return B(); }\n").release()); + invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc", + IK_CXX)); + invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; + invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; + CompilerInstance compiler; + compiler.setInvocation(invocation); + compiler.createDiagnostics(); + + TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true, + /*actOnEndOfTranslationUnit=*/true); + ASSERT_TRUE(compiler.ExecuteAction(test_action)); + ASSERT_EQ(13U, test_action.decl_names.size()); + EXPECT_EQ("A", test_action.decl_names[0]); + EXPECT_EQ("c", test_action.decl_names[12]); +} + struct TestPPCallbacks : public PPCallbacks { TestPPCallbacks() : SeenEnd(false) {} -- 2.7.4