From 36c4464ba6cfc2a63dc67c493ef2f5ab2aea09cc Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Mon, 19 Oct 2009 14:34:22 +0000 Subject: [PATCH] Move Diagnostic/DiagClient/FileManager from Indexer => ASTUnit. Removing this shared data should enable clang_createTranslationUnit/clang_createTranslationUnitFromSourceFile to be run from multiple threads (related to ). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84499 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/ASTUnit.h | 16 +++++++++++----- include/clang/Index/Indexer.h | 13 +------------ lib/Frontend/ASTUnit.cpp | 16 ++++++---------- tools/CIndex/CIndex.cpp | 3 +-- tools/clang-cc/clang-cc.cpp | 3 +-- tools/index-test/index-test.cpp | 7 +++---- 6 files changed, 23 insertions(+), 35 deletions(-) diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index dd3db39343..9a0ede6ca3 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -16,6 +16,8 @@ #include "clang/Basic/SourceManager.h" #include "llvm/ADT/OwningPtr.h" +#include "clang/Frontend/TextDiagnosticBuffer.h" +#include "clang/Basic/FileManager.h" #include namespace clang { @@ -23,6 +25,7 @@ namespace clang { class FileEntry; class SourceManager; class Diagnostic; + class TextDiagnosticBuffer; class HeaderSearch; class TargetInfo; class Preprocessor; @@ -32,7 +35,10 @@ namespace clang { /// \brief Utility class for loading a ASTContext from a PCH file. /// class ASTUnit { - Diagnostic &Diags; + TextDiagnosticBuffer DiagClient; + Diagnostic Diags; + FileManager FileMgr; + SourceManager SourceMgr; llvm::OwningPtr HeaderInfo; llvm::OwningPtr Target; @@ -47,7 +53,7 @@ class ASTUnit { ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT - ASTUnit(Diagnostic &_Diag); + ASTUnit(); public: ~ASTUnit(); @@ -64,7 +70,9 @@ public: const Diagnostic &getDiagnostic() const { return Diags; } Diagnostic &getDiagnostic() { return Diags; } - FileManager &getFileManager(); + const FileManager &getFileManager() const { return FileMgr; } + FileManager &getFileManager() { return FileMgr; } + const std::string &getOriginalSourceFileName(); const std::string &getPCHFileName(); @@ -85,8 +93,6 @@ public: /// /// \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, bool OnlyLocalDecls = false, bool UseBumpAllocator = false); diff --git a/include/clang/Index/Indexer.h b/include/clang/Index/Indexer.h index 8b1d2dd38b..361e729fea 100644 --- a/include/clang/Index/Indexer.h +++ b/include/clang/Index/Indexer.h @@ -14,13 +14,11 @@ #ifndef LLVM_CLANG_INDEX_INDEXER_H #define LLVM_CLANG_INDEX_INDEXER_H -#include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Index/IndexProvider.h" #include "clang/Index/Entity.h" #include "clang/Index/GlobalSelector.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/DenseMap.h" -#include "clang/Basic/FileManager.h" #include namespace clang { @@ -39,16 +37,10 @@ public: typedef std::map SelMapTy; explicit Indexer(Program &prog) : - Prog(prog), Diags(&DiagClient) { } + Prog(prog) { } Program &getProgram() const { return Prog; } - Diagnostic &getDiagnostics() { return Diags; } - const Diagnostic &getDiagnostics() const { return Diags; } - - FileManager &getFileManager() { return FileMgr; } - const FileManager &getFileManager() const { return FileMgr; } - /// \brief Find all Entities and map them to the given translation unit. void IndexAST(TranslationUnit *TU); @@ -59,9 +51,6 @@ public: private: Program &Prog; - TextDiagnosticBuffer DiagClient; - Diagnostic Diags; - FileManager FileMgr; MapTy Map; CtxTUMapTy CtxTUMap; diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 084fa4d345..a7a62fb93a 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -25,7 +25,9 @@ using namespace clang; -ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { } +ASTUnit::ASTUnit() : tempFile(false) { + Diags.setClient(&DiagClient); +} ASTUnit::~ASTUnit() { if (tempFile) llvm::sys::Path(getPCHFileName()).eraseFromDisk(); @@ -88,19 +90,13 @@ const std::string &ASTUnit::getPCHFileName() { return dyn_cast(Ctx->getExternalSource())->getFileName(); } -FileManager &ASTUnit::getFileManager() { - return HeaderInfo->getFileMgr(); -} - ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, - Diagnostic &Diags, - FileManager &FileMgr, std::string *ErrMsg, bool OnlyLocalDecls, bool UseBumpAllocator) { - llvm::OwningPtr AST(new ASTUnit(Diags)); + llvm::OwningPtr AST(new ASTUnit()); AST->OnlyLocalDecls = OnlyLocalDecls; - AST->HeaderInfo.reset(new HeaderSearch(FileMgr)); + AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); // Gather Info for preprocessor construction later on. @@ -113,7 +109,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, llvm::OwningPtr Reader; llvm::OwningPtr Source; - Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags)); + Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), AST->Diags)); Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, Predefines, Counter)); diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 99f6154056..cc49ee4236 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -347,8 +347,7 @@ CXTranslationUnit clang_createTranslationUnit( std::string astName(ast_filename); std::string ErrMsg; - return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(), - CXXIdx->getFileManager(), &ErrMsg, + return ASTUnit::LoadFromPCHFile(astName, &ErrMsg, CXXIdx->getOnlyLocalDecls(), /* UseBumpAllocator = */ true); } diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 833386dd17..f77767c150 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -2188,8 +2188,7 @@ static void ProcessASTInputFile(const std::string &InFile, ProgActions PA, Diagnostic &Diags, FileManager &FileMgr, llvm::LLVMContext& Context) { std::string Error; - llvm::OwningPtr AST(ASTUnit::LoadFromPCHFile(InFile, Diags, FileMgr, - &Error)); + llvm::OwningPtr AST(ASTUnit::LoadFromPCHFile(InFile, &Error)); if (!AST) { Diags.Report(FullSourceLoc(), diag::err_fe_invalid_ast_file) << Error; return; diff --git a/tools/index-test/index-test.cpp b/tools/index-test/index-test.cpp index 103874c77d..fce48edf26 100644 --- a/tools/index-test/index-test.cpp +++ b/tools/index-test/index-test.cpp @@ -225,8 +225,7 @@ int main(int argc, char **argv) { std::string ErrMsg; llvm::OwningPtr AST; - AST.reset(ASTUnit::LoadFromPCHFile(InFile, Idxer.getDiagnostics(), - Idxer.getFileManager(), &ErrMsg)); + AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg)); if (!AST) { llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n'; return 1; @@ -244,7 +243,7 @@ int main(int argc, char **argv) { if (!PointAtLocation.empty()) { const std::string &Filename = PointAtLocation[0].FileName; - const FileEntry *File = Idxer.getFileManager().getFile(Filename); + const FileEntry *File = FirstAST->getFileManager().getFile(Filename); if (File == 0) { llvm::errs() << "File '" << Filename << "' does not exist\n"; return 1; @@ -253,7 +252,7 @@ int main(int argc, char **argv) { // Safety check. Using an out-of-date AST file will only lead to crashes // or incorrect results. // FIXME: Check all the source files that make up the AST file. - const FileEntry *ASTFile = Idxer.getFileManager().getFile(FirstFile); + const FileEntry *ASTFile = FirstAST->getFileManager().getFile(FirstFile); if (File->getModificationTime() > ASTFile->getModificationTime()) { llvm::errs() << "[" << FirstFile << "] Error: " << "Pointing at a source file which was modified after creating "