From 41ed5a7362c01b8969b9b2bbde5318bf447d1e0d Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Wed, 15 Oct 2014 00:33:06 +0000 Subject: [PATCH] Frontend: Don't accept null DiagnosticsEngines when building ASTUnits The various ways to create an ASTUnit all take a refcounted pointer to a diagnostics engine as an argument, and if it isn't pointing at anything they initialize it. This is a pretty confusing API, and it really makes more sense for the caller to initialize the thing since they control the lifetime anyway. This fixes the one caller that didn't bother initializing the pointer and asserts that the argument is initialized. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@219752 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/ASTUnit.h | 2 +- lib/Frontend/ASTUnit.cpp | 21 ++++----------------- tools/libclang/CIndex.cpp | 3 ++- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index 1709089328..634224d08b 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -305,7 +305,7 @@ private: /// \brief The language options used when we load an AST file. LangOptions ASTFileLangOpts; - static void ConfigureDiags(IntrusiveRefCntPtr &Diags, + static void ConfigureDiags(IntrusiveRefCntPtr Diags, ASTUnit &AST, bool CaptureDiagnostics); void TranslateStoredDiagnostics(FileManager &FileMgr, diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 8a77d87f64..8742a54c01 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -642,20 +642,11 @@ ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) { } /// \brief Configure the diagnostics object for use with ASTUnit. -void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr &Diags, +void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr Diags, ASTUnit &AST, bool CaptureDiagnostics) { - if (!Diags.get()) { - // No diagnostics engine was provided, so create our own diagnostics object - // with the default options. - DiagnosticConsumer *Client = nullptr; - if (CaptureDiagnostics) - Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics); - Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(), - Client, - /*ShouldOwnClient=*/true); - } else if (CaptureDiagnostics) { + assert(Diags.get() && "no DiagnosticsEngine was provided"); + if (CaptureDiagnostics) Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics)); - } } std::unique_ptr ASTUnit::LoadFromASTFile( @@ -1928,11 +1919,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine( bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies, bool UserFilesAreVolatile, bool ForSerialization, std::unique_ptr *ErrAST) { - if (!Diags.get()) { - // No diagnostics engine was provided, so create our own diagnostics object - // with the default options. - Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions()); - } + assert(Diags.get() && "no DiagnosticsEngine was provided"); SmallVector StoredDiagnostics; diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index eab289206e..552f5a70a5 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -2807,7 +2807,8 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, CIndexer *CXXIdx = static_cast(CIdx); FileSystemOptions FileSystemOpts; - IntrusiveRefCntPtr Diags; + IntrusiveRefCntPtr Diags = + CompilerInstance::createDiagnostics(new DiagnosticOptions()); std::unique_ptr AU = ASTUnit::LoadFromASTFile( ast_filename, Diags, FileSystemOpts, CXXIdx->getOnlyLocalDecls(), None, /*CaptureDiagnostics=*/true, -- 2.40.0