From f18d0d8b39e891460d50f8a8b85029885b264986 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 12 Aug 2010 23:31:19 +0000 Subject: [PATCH] Teach CompilerInstance to create and hold on to the Sema object used for parsing, so that it can persist beyond the lifetime of the parsing call. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110978 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/CompilerInstance.h | 24 +++++++++++++++++++++++ lib/Frontend/CompilerInstance.cpp | 12 ++++++++++++ lib/Frontend/FrontendAction.cpp | 18 +++++++++++------ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/include/clang/Frontend/CompilerInstance.h b/include/clang/Frontend/CompilerInstance.h index 4a4d5ae1d4..bd1791dae8 100644 --- a/include/clang/Frontend/CompilerInstance.h +++ b/include/clang/Frontend/CompilerInstance.h @@ -36,6 +36,7 @@ class FileManager; class FrontendAction; class PCHReader; class Preprocessor; +class Sema; class SourceManager; class TargetInfo; @@ -91,6 +92,9 @@ class CompilerInstance { /// The code completion consumer. llvm::OwningPtr CompletionConsumer; + /// \brief The semantic analysis object. + llvm::OwningPtr TheSema; + /// The frontend timer llvm::OwningPtr FrontendTimer; @@ -369,6 +373,10 @@ public: /// takes ownership of \arg Value. void setASTContext(ASTContext *Value); + /// \brief Replace the current Sema; the compiler instance takes ownership + /// of S. + void setSema(Sema *S); + /// } /// @name ASTConsumer /// { @@ -388,6 +396,18 @@ public: /// takes ownership of \arg Value. void setASTConsumer(ASTConsumer *Value); + /// } + /// @name Semantic analysis + /// { + bool hasSema() const { return TheSema != 0; } + + Sema &getSema() const { + assert(TheSema && "Compiler instance has no Sema object!"); + return *TheSema; + } + + Sema *takeSema() { return TheSema.take(); } + /// } /// @name Code Completion /// { @@ -526,6 +546,10 @@ public: bool UseDebugPrinter, bool ShowMacros, bool ShowCodePatterns, llvm::raw_ostream &OS); + /// \brief Create the Sema object to be used for parsing. + void createSema(bool CompleteTranslationUnit, + CodeCompleteConsumer *CompletionConsumer); + /// Create the frontend timer and replace any existing one with it. void createFrontendTimer(); diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index 76773e40d3..e2bb0f20db 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "clang/Frontend/CompilerInstance.h" +#include "clang/Sema/Sema.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" #include "clang/Basic/Diagnostic.h" @@ -41,6 +42,7 @@ CompilerInstance::CompilerInstance() } CompilerInstance::~CompilerInstance() { + TheSema.reset(); } void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) { @@ -79,6 +81,10 @@ void CompilerInstance::setASTContext(ASTContext *Value) { Context.reset(Value); } +void CompilerInstance::setSema(Sema *S) { + TheSema.reset(S); +} + void CompilerInstance::setASTConsumer(ASTConsumer *Value) { Consumer.reset(Value); } @@ -362,6 +368,12 @@ CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, return new CIndexCodeCompleteConsumer(ShowMacros, ShowCodePatterns, OS); } +void CompilerInstance::createSema(bool CompleteTranslationUnit, + CodeCompleteConsumer *CompletionConsumer) { + TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), + CompleteTranslationUnit, CompletionConsumer)); +} + // Output Files void CompilerInstance::addOutputFile(llvm::StringRef Path, diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp index 90b87864b8..40b4e7fa24 100644 --- a/lib/Frontend/FrontendAction.cpp +++ b/lib/Frontend/FrontendAction.cpp @@ -196,12 +196,16 @@ void FrontendAction::EndSourceFile() { // FIXME: There is more per-file stuff we could just drop here? if (CI.getFrontendOpts().DisableFree) { CI.takeASTConsumer(); - if (!isCurrentFileAST()) + if (!isCurrentFileAST()) { + CI.takeSema(); CI.takeASTContext(); + } } else { - CI.setASTConsumer(0); - if (!isCurrentFileAST()) + if (!isCurrentFileAST()) { + CI.setSema(0); CI.setASTContext(0); + } + CI.setASTConsumer(0); } // Inform the preprocessor we are done. @@ -225,6 +229,7 @@ void FrontendAction::EndSourceFile() { CI.getDiagnosticClient().EndSourceFile(); if (isCurrentFileAST()) { + CI.takeSema(); CI.takeASTContext(); CI.takePreprocessor(); CI.takeSourceManager(); @@ -253,9 +258,10 @@ void ASTFrontendAction::ExecuteAction() { if (CI.hasCodeCompletionConsumer()) CompletionConsumer = &CI.getCodeCompletionConsumer(); - ParseAST(CI.getPreprocessor(), &CI.getASTConsumer(), CI.getASTContext(), - CI.getFrontendOpts().ShowStats, - usesCompleteTranslationUnit(), CompletionConsumer); + if (!CI.hasSema()) + CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer); + + ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats); } ASTConsumer * -- 2.40.0