From 7cd285f0fe89dcc3f47cc11d5bd9db8ad4d85d37 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Mon, 21 Sep 2009 03:03:39 +0000 Subject: [PATCH] Change ASTUnit to take the Diagnostic as an argument, the client should have control of this. llvm-svn: 82430 --- clang/include/clang/Frontend/ASTUnit.h | 33 ++++++++++++++------------ clang/lib/Frontend/ASTUnit.cpp | 22 ++++++----------- clang/tools/CIndex/CIndex.cpp | 3 ++- clang/tools/clang-cc/clang-cc.cpp | 2 +- clang/tools/index-test/index-test.cpp | 4 ++-- clang/tools/wpa/clang-wpa.cpp | 7 +++++- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h index 3b72c37bcd1e..89eb3b8821ca 100644 --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -14,6 +14,7 @@ #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H #define LLVM_CLANG_FRONTEND_ASTUNIT_H +#include "clang/Basic/SourceManager.h" #include "llvm/ADT/OwningPtr.h" #include @@ -21,7 +22,6 @@ namespace clang { class FileManager; class FileEntry; class SourceManager; - class DiagnosticClient; class Diagnostic; class HeaderSearch; class TargetInfo; @@ -32,23 +32,22 @@ namespace clang { /// \brief Utility class for loading a ASTContext from a PCH file. /// class ASTUnit { - llvm::OwningPtr SourceMgr; - llvm::OwningPtr DiagClient; - llvm::OwningPtr Diags; + Diagnostic &Diags; + SourceManager SourceMgr; llvm::OwningPtr HeaderInfo; llvm::OwningPtr Target; llvm::OwningPtr PP; llvm::OwningPtr Ctx; - ASTUnit(const ASTUnit&); // do not implement - ASTUnit &operator=(const ASTUnit &); // do not implement - ASTUnit(); + ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT + ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT + ASTUnit(Diagnostic &_Diag); public: ~ASTUnit(); - const SourceManager &getSourceManager() const { return *SourceMgr.get(); } - SourceManager &getSourceManager() { return *SourceMgr.get(); } + const SourceManager &getSourceManager() const { return SourceMgr; } + SourceManager &getSourceManager() { return SourceMgr; } const Preprocessor &getPreprocessor() const { return *PP.get(); } Preprocessor &getPreprocessor() { return *PP.get(); } @@ -56,22 +55,26 @@ public: const ASTContext &getASTContext() const { return *Ctx.get(); } ASTContext &getASTContext() { return *Ctx.get(); } - const Diagnostic &getDiagnostic() const { return *Diags.get(); } - Diagnostic &getDiagnostic() { return *Diags.get(); } + const Diagnostic &getDiagnostic() const { return Diags; } + Diagnostic &getDiagnostic() { return Diags; } FileManager &getFileManager(); const std::string &getOriginalSourceFileName(); /// \brief Create a ASTUnit from a PCH file. /// - /// \param Filename PCH filename + /// \param Filename - The PCH file to load. /// - /// \param FileMgr The FileManager to use + /// \param Diags - The Diagnostic implementation to use. /// - /// \param ErrMsg Error message to report if the PCH file could not be loaded + /// \param FileMgr - The FileManager to use. /// - /// \returns the initialized ASTUnit or NULL if the PCH failed to load + /// \param ErrMsg - Error message to report if the PCH file could not be + /// loaded. + /// + /// \returns - The initialized ASTUnit or null if the PCH failed to load. static ASTUnit *LoadFromPCHFile(const std::string &Filename, + Diagnostic &Diags, FileManager &FileMgr, std::string *ErrMsg = 0); }; diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 609889aad9fe..1e2ad46b93a6 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -13,7 +13,6 @@ #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/PCHReader.h" -#include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/AST/ASTContext.h" #include "clang/AST/DeclVisitor.h" #include "clang/AST/StmtVisitor.h" @@ -25,7 +24,7 @@ using namespace clang; -ASTUnit::ASTUnit() { } +ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { } ASTUnit::~ASTUnit() { } namespace { @@ -86,19 +85,12 @@ FileManager &ASTUnit::getFileManager() { } ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, + Diagnostic &Diags, FileManager &FileMgr, std::string *ErrMsg) { - - llvm::OwningPtr AST(new ASTUnit()); - - AST->DiagClient.reset(new TextDiagnosticBuffer()); - AST->Diags.reset(new Diagnostic(AST->DiagClient.get())); + llvm::OwningPtr AST(new ASTUnit(Diags)); AST->HeaderInfo.reset(new HeaderSearch(FileMgr)); - AST->SourceMgr.reset(new SourceManager()); - - Diagnostic &Diags = *AST->Diags.get(); - SourceManager &SourceMgr = *AST->SourceMgr.get(); // Gather Info for preprocessor construction later on. @@ -111,7 +103,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, llvm::OwningPtr Reader; llvm::OwningPtr Source; - Reader.reset(new PCHReader(SourceMgr, FileMgr, Diags)); + Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags)); Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, Predefines, Counter)); @@ -130,8 +122,8 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, // Get information about the target being compiled for. AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple)); - AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(), - SourceMgr, HeaderInfo)); + AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(), + AST->getSourceManager(), HeaderInfo)); Preprocessor &PP = *AST->PP.get(); PP.setPredefines(Predefines); @@ -141,7 +133,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, // Create and initialize the ASTContext. AST->Ctx.reset(new ASTContext(LangInfo, - SourceMgr, + AST->getSourceManager(), *AST->Target.get(), PP.getIdentifierTable(), PP.getSelectorTable(), diff --git a/clang/tools/CIndex/CIndex.cpp b/clang/tools/CIndex/CIndex.cpp index 55bcd9330ada..de50b8007962 100644 --- a/clang/tools/CIndex/CIndex.cpp +++ b/clang/tools/CIndex/CIndex.cpp @@ -206,7 +206,8 @@ CXTranslationUnit clang_createTranslationUnit( std::string astName(ast_filename); std::string ErrMsg; - return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); + return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(), + CXXIdx->getFileManager(), &ErrMsg); } void clang_disposeTranslationUnit( diff --git a/clang/tools/clang-cc/clang-cc.cpp b/clang/tools/clang-cc/clang-cc.cpp index 89ca5db86521..6c2f1d6d077b 100644 --- a/clang/tools/clang-cc/clang-cc.cpp +++ b/clang/tools/clang-cc/clang-cc.cpp @@ -2161,7 +2161,7 @@ static void ProcessASTInputFile(const std::string &InFile, ProgActions PA, // FIXME: This is manufactoring its own diags and source manager, we should // reuse ours. std::string Error; - llvm::OwningPtr AST(ASTUnit::LoadFromPCHFile(InFile, FileMgr, + llvm::OwningPtr AST(ASTUnit::LoadFromPCHFile(InFile, Diags, FileMgr, &Error)); if (!AST) { Diags.Report(FullSourceLoc(), diag::err_fe_invalid_ast_file) << Error; diff --git a/clang/tools/index-test/index-test.cpp b/clang/tools/index-test/index-test.cpp index a0546bfcd8e3..decc921ded73 100644 --- a/clang/tools/index-test/index-test.cpp +++ b/clang/tools/index-test/index-test.cpp @@ -225,8 +225,8 @@ int main(int argc, char **argv) { std::string ErrMsg; llvm::OwningPtr AST; - AST.reset(ASTUnit::LoadFromPCHFile(InFile, Idxer.getFileManager(), - &ErrMsg)); + AST.reset(ASTUnit::LoadFromPCHFile(InFile, Idxer.getDiagnostics(), + Idxer.getFileManager(), &ErrMsg)); if (!AST) { llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n'; return 1; diff --git a/clang/tools/wpa/clang-wpa.cpp b/clang/tools/wpa/clang-wpa.cpp index 2cdc1c6d9888..fa2326dc2ba4 100644 --- a/clang/tools/wpa/clang-wpa.cpp +++ b/clang/tools/wpa/clang-wpa.cpp @@ -15,6 +15,8 @@ #include "clang/Analysis/CallGraph.h" #include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Frontend/TextDiagnosticBuffer.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -31,13 +33,16 @@ int main(int argc, char **argv) { if (InputFilenames.empty()) return 0; + TextDiagnosticBuffer DiagClient; + Diagnostic Diags(&DiagClient); + for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { const std::string &InFile = InputFilenames[i]; std::string ErrMsg; llvm::OwningPtr AST; - AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg)); + AST.reset(ASTUnit::LoadFromPCHFile(InFile, Diags, FileMgr, &ErrMsg)); if (!AST) { llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n';