diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-12-03 01:45:44 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-12-03 01:45:44 +0000 |
commit | 5262fda30b876c8aae95f2eb92e349418d6b14bb (patch) | |
tree | 258af401a1fdf30edb4079efece5a40e0ed3551d | |
parent | 7d9bd4257f817494b2fa5b310df05807050a9c18 (diff) |
Fix ASTUnit to allows require a (persistent) Diagnostic object be provided; propogate and simplify.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90379 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | examples/wpa/clang-wpa.cpp | 16 | ||||
-rw-r--r-- | include/clang/Basic/DiagnosticFrontendKinds.td | 2 | ||||
-rw-r--r-- | include/clang/Frontend/ASTUnit.h | 25 | ||||
-rw-r--r-- | lib/Frontend/ASTUnit.cpp | 26 | ||||
-rw-r--r-- | lib/Frontend/FrontendAction.cpp | 6 | ||||
-rw-r--r-- | tools/CIndex/CIndex.cpp | 44 | ||||
-rw-r--r-- | tools/index-test/index-test.cpp | 17 |
7 files changed, 53 insertions, 83 deletions
diff --git a/examples/wpa/clang-wpa.cpp b/examples/wpa/clang-wpa.cpp index 4a0fe49939..a0182c1788 100644 --- a/examples/wpa/clang-wpa.cpp +++ b/examples/wpa/clang-wpa.cpp @@ -16,7 +16,7 @@ #include "clang/Frontend/ASTUnit.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" -#include "clang/Frontend/TextDiagnosticBuffer.h" +#include "clang/Frontend/CompilerInstance.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -33,20 +33,14 @@ int main(int argc, char **argv) { if (InputFilenames.empty()) return 0; - TextDiagnosticBuffer DiagClient; + llvm::OwningPtr<Diagnostic> Diags( + CompilerInstance::createDiagnostics(DiagnosticOptions(), argc, argv)); for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { const std::string &InFile = InputFilenames[i]; - - std::string ErrMsg; - llvm::OwningPtr<ASTUnit> AST; - - AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg, &DiagClient)); - - if (!AST) { - llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n'; + llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, *Diags)); + if (!AST) return 1; - } ASTUnits.push_back(AST.take()); } diff --git a/include/clang/Basic/DiagnosticFrontendKinds.td b/include/clang/Basic/DiagnosticFrontendKinds.td index 50d997c986..c36d1c442b 100644 --- a/include/clang/Basic/DiagnosticFrontendKinds.td +++ b/include/clang/Basic/DiagnosticFrontendKinds.td @@ -37,6 +37,8 @@ def err_fe_remap_missing_to_file : Error< "could not remap file '%0' to the contents of file '%1'">, DefaultFatal; def err_fe_remap_missing_from_file : Error< "could not remap from missing file '%0'">, DefaultFatal; +def err_fe_unable_to_load_pch : Error< + "unable to load PCH file">; def err_verify_bogus_characters : Error< "bogus characters before '{{' in expected string">; diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index a0c0e2207c..3bc815446f 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -16,7 +16,6 @@ #include "clang/Basic/SourceManager.h" #include "llvm/ADT/OwningPtr.h" -#include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Basic/FileManager.h" #include "clang/Index/ASTLocation.h" #include <string> @@ -32,14 +31,12 @@ class HeaderSearch; class Preprocessor; class SourceManager; class TargetInfo; -class TextDiagnosticBuffer; using namespace idx; /// \brief Utility class for loading a ASTContext from a PCH file. /// class ASTUnit { - Diagnostic Diags; FileManager FileMgr; SourceManager SourceMgr; @@ -67,7 +64,7 @@ class ASTUnit { ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT public: - ASTUnit(bool MainFileIsAST, DiagnosticClient *diagClient = NULL); + ASTUnit(bool MainFileIsAST); ~ASTUnit(); bool isMainFileAST() const { return MainFileIsAST; } @@ -81,9 +78,6 @@ public: const ASTContext &getASTContext() const { return *Ctx.get(); } ASTContext &getASTContext() { return *Ctx.get(); } - const Diagnostic &getDiagnostic() const { return Diags; } - Diagnostic &getDiagnostic() { return Diags; } - const FileManager &getFileManager() const { return FileMgr; } FileManager &getFileManager() { return FileMgr; } @@ -101,17 +95,12 @@ public: /// /// \param Filename - The PCH file to load. /// - /// \param DiagClient - The diagnostics client to use. Specify NULL - /// to use a default client that emits warnings/errors to standard error. - /// The ASTUnit objects takes ownership of this object. - /// - /// \param ErrMsg - Error message to report if the PCH file could not be - /// loaded. + /// \param Diags - The diagnostics engine to use for reporting errors; its + /// lifetime is expected to extend past that of the returned ASTUnit. /// /// \returns - The initialized ASTUnit or null if the PCH failed to load. static ASTUnit *LoadFromPCHFile(const std::string &Filename, - std::string *ErrMsg = 0, - DiagnosticClient *DiagClient = NULL, + Diagnostic &Diags, bool OnlyLocalDecls = false, bool UseBumpAllocator = false); @@ -121,7 +110,8 @@ public: /// \param CI - The compiler invocation to use; it must have exactly one input /// source file. /// - /// \param Diags - The diagnostics engine to use for reporting errors. + /// \param Diags - The diagnostics engine to use for reporting errors; its + /// lifetime is expected to extend past that of the returned ASTUnit. // // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we // shouldn't need to specify them at construction time. @@ -136,7 +126,8 @@ public: /// /// \param ArgEnd - The end of the argument vector. /// - /// \param Diags - The diagnostics engine to use for reporting errors. + /// \param Diags - The diagnostics engine to use for reporting errors; its + /// lifetime is expected to extend past that of the returned ASTUnit. /// /// \param Argv0 - The program path (from argv[0]), for finding the builtin /// compiler path. diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index a3db339b83..2afbcd133e 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -34,18 +34,12 @@ #include "llvm/System/Path.h" using namespace clang; -ASTUnit::ASTUnit(bool _MainFileIsAST, - DiagnosticClient *diagClient) - : tempFile(false), MainFileIsAST(_MainFileIsAST) -{ - Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer()); +ASTUnit::ASTUnit(bool _MainFileIsAST) + : tempFile(false), MainFileIsAST(_MainFileIsAST) { } ASTUnit::~ASTUnit() { if (tempFile) llvm::sys::Path(getPCHFileName()).eraseFromDisk(); - - // The ASTUnit object owns the DiagnosticClient. - delete Diags.getClient(); } namespace { @@ -107,11 +101,10 @@ const std::string &ASTUnit::getPCHFileName() { } ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, - std::string *ErrMsg, - DiagnosticClient *diagClient, + Diagnostic &Diags, bool OnlyLocalDecls, bool UseBumpAllocator) { - llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true, diagClient)); + llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); AST->OnlyLocalDecls = OnlyLocalDecls; AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); @@ -127,7 +120,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, llvm::OwningPtr<ExternalASTSource> Source; Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), - AST->Diags)); + Diags)); Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, Predefines, Counter)); @@ -137,8 +130,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, case PCHReader::Failure: case PCHReader::IgnorePCH: - if (ErrMsg) - *ErrMsg = "Could not load PCH file"; + Diags.Report(diag::err_fe_unable_to_load_pch); return NULL; } @@ -154,8 +146,8 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, TargetOpts.CPU = ""; TargetOpts.Features.clear(); TargetOpts.Triple = TargetTriple; - AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts)); - AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(), + AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts)); + AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(), AST->getSourceManager(), HeaderInfo)); Preprocessor &PP = *AST->PP.get(); @@ -231,8 +223,6 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI, "FIXME: AST inputs not yet supported here!"); // Create the AST unit. - // - // FIXME: Use the provided diagnostic client. AST.reset(new ASTUnit(false)); AST->OnlyLocalDecls = OnlyLocalDecls; diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp index 91c946c9cf..e244d3cc21 100644 --- a/lib/Frontend/FrontendAction.cpp +++ b/lib/Frontend/FrontendAction.cpp @@ -46,11 +46,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, assert(hasASTSupport() && "This action does not have AST support!"); std::string Error; - ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, &Error); - if (!AST) { - CI.getDiagnostics().Report(diag::err_fe_invalid_ast_file) << Error; + ASTUnit *AST = ASTUnit::LoadFromPCHFile(Filename, CI.getDiagnostics()); + if (!AST) goto failure; - } setCurrentFile(Filename, AST); diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 87d7856203..a5b7e99622 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -23,6 +23,7 @@ #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Config/config.h" #include "llvm/Support/Compiler.h" @@ -291,10 +292,24 @@ public: }; class CIndexer : public Indexer { + IgnoreDiagnosticsClient IgnoreDiagClient; + llvm::OwningPtr<Diagnostic> TextDiags; + Diagnostic IgnoreDiags; + bool UseExternalASTGeneration; + bool OnlyLocalDecls; + bool DisplayDiagnostics; + + llvm::sys::Path ClangPath; + public: explicit CIndexer(Program *prog) : Indexer(*prog), + IgnoreDiags(&IgnoreDiagClient), + UseExternalASTGeneration(false), OnlyLocalDecls(false), - DisplayDiagnostics(false) {} + DisplayDiagnostics(false) { + TextDiags.reset( + CompilerInstance::createDiagnostics(DiagnosticOptions(), 0, 0)); + } virtual ~CIndexer() { delete &getProgram(); } @@ -304,18 +319,17 @@ public: bool getOnlyLocalDecls() const { return OnlyLocalDecls; } void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } + bool getDisplayDiagnostics() const { return DisplayDiagnostics; } void setDisplayDiagnostics(bool Display = true) { DisplayDiagnostics = Display; } - bool getDisplayDiagnostics() const { return DisplayDiagnostics; } + + Diagnostic &getDiags() { + return DisplayDiagnostics ? *TextDiags : IgnoreDiags; + } /// \brief Get the path of the clang binary. const llvm::sys::Path& getClangPath(); -private: - bool OnlyLocalDecls; - bool DisplayDiagnostics; - - llvm::sys::Path ClangPath; }; const llvm::sys::Path& CIndexer::getClangPath() { @@ -457,20 +471,10 @@ CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, const char *ast_filename) { assert(CIdx && "Passed null CXIndex"); CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); - std::string astName(ast_filename); - std::string ErrMsg; - - CXTranslationUnit TU = - ASTUnit::LoadFromPCHFile(astName, &ErrMsg, - CXXIdx->getDisplayDiagnostics() ? - NULL : new IgnoreDiagnosticsClient(), - CXXIdx->getOnlyLocalDecls(), - /* UseBumpAllocator = */ true); - - if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) - llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n'; - return TU; + return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), + CXXIdx->getOnlyLocalDecls(), + /* UseBumpAllocator = */ true); } CXTranslationUnit diff --git a/tools/index-test/index-test.cpp b/tools/index-test/index-test.cpp index fc8547b784..d13b2d4926 100644 --- a/tools/index-test/index-test.cpp +++ b/tools/index-test/index-test.cpp @@ -238,7 +238,6 @@ int main(int argc, char **argv) { Indexer Idxer(Prog); llvm::SmallVector<TUnit*, 4> TUnits; - TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions(), false); llvm::OwningPtr<Diagnostic> Diags( CompilerInstance::createDiagnostics(DiagnosticOptions(), argc, argv)); @@ -248,21 +247,13 @@ int main(int argc, char **argv) { for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { const std::string &InFile = InputFilenames[i]; - - std::string ErrMsg; llvm::OwningPtr<ASTUnit> AST; - - if (ASTFromSource) { + if (ASTFromSource) AST.reset(CreateFromSource(InFile, *Diags, argv[0])); - if (!AST || Diags->getNumErrors()) - ErrMsg = "unable to create AST"; - } else - AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg)); - - if (!AST) { - llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n'; + else + AST.reset(ASTUnit::LoadFromPCHFile(InFile, *Diags)); + if (!AST) return 1; - } TUnit *TU = new TUnit(AST.take(), InFile); TUnits.push_back(TU); |