Index: examples/clang-interpreter/main.cpp =================================================================== --- examples/clang-interpreter/main.cpp +++ examples/clang-interpreter/main.cpp @@ -144,7 +144,7 @@ // Create a compiler instance to handle the actual work. CompilerInstance Clang; - Clang.setInvocation(CI.release()); + Clang.setInvocation(std::move(CI)); // Create the compilers actual diagnostics engine. Clang.createDiagnostics(); Index: include/clang/Frontend/ASTUnit.h =================================================================== --- include/clang/Frontend/ASTUnit.h +++ include/clang/Frontend/ASTUnit.h @@ -111,8 +111,8 @@ /// Optional owned invocation, just used to make the invocation used in /// LoadFromCommandLine available. - IntrusiveRefCntPtr Invocation; - + std::shared_ptr Invocation; + // OnlyLocalDecls - when true, walking this AST should only visit declarations // that come from the AST itself, not from included precompiled headers. // FIXME: This is temporary; eventually, CIndex will always do this. @@ -694,11 +694,10 @@ /// remapped contents of that file. typedef std::pair RemappedFile; - /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation. - static ASTUnit *create(CompilerInvocation *CI, + /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation. + static ASTUnit *create(std::shared_ptr CI, IntrusiveRefCntPtr Diags, - bool CaptureDiagnostics, - bool UserFilesAreVolatile); + bool CaptureDiagnostics, bool UserFilesAreVolatile); /// \brief Create a ASTUnit from an AST file. /// @@ -756,7 +755,8 @@ /// created ASTUnit was passed in \p Unit then the caller can check that. /// static ASTUnit *LoadFromCompilerInvocationAction( - CompilerInvocation *CI, IntrusiveRefCntPtr Diags, + std::shared_ptr CI, + IntrusiveRefCntPtr Diags, ASTFrontendAction *Action = nullptr, ASTUnit *Unit = nullptr, bool Persistent = true, StringRef ResourceFilesPath = StringRef(), bool OnlyLocalDecls = false, bool CaptureDiagnostics = false, @@ -777,9 +777,10 @@ // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we // shouldn't need to specify them at construction time. static std::unique_ptr LoadFromCompilerInvocation( - CompilerInvocation *CI, IntrusiveRefCntPtr Diags, - bool OnlyLocalDecls = false, bool CaptureDiagnostics = false, - bool PrecompilePreamble = false, TranslationUnitKind TUKind = TU_Complete, + std::unique_ptr CI, + IntrusiveRefCntPtr Diags, bool OnlyLocalDecls = false, + bool CaptureDiagnostics = false, bool PrecompilePreamble = false, + TranslationUnitKind TUKind = TU_Complete, bool CacheCodeCompletionResults = false, bool IncludeBriefCommentsInCodeCompletion = false, bool UserFilesAreVolatile = false); Index: include/clang/Frontend/CompilerInstance.h =================================================================== --- include/clang/Frontend/CompilerInstance.h +++ include/clang/Frontend/CompilerInstance.h @@ -67,7 +67,7 @@ /// and a long form that takes explicit instances of any required objects. class CompilerInstance : public ModuleLoader { /// The options used in this compiler instance. - IntrusiveRefCntPtr Invocation; + std::shared_ptr Invocation; /// The diagnostics engine instance. IntrusiveRefCntPtr Diagnostics; @@ -206,7 +206,7 @@ } /// setInvocation - Replace the current invocation. - void setInvocation(CompilerInvocation *Value); + void setInvocation(std::shared_ptr Value); /// \brief Indicates whether we should (re)build the global module index. bool shouldBuildGlobalModuleIndex() const; Index: include/clang/Frontend/CompilerInvocation.h =================================================================== --- include/clang/Frontend/CompilerInvocation.h +++ include/clang/Frontend/CompilerInvocation.h @@ -49,7 +49,7 @@ bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args, DiagnosticsEngine *Diags = nullptr); -class CompilerInvocationBase : public RefCountedBase { +class CompilerInvocationBase { void operator=(const CompilerInvocationBase &) LLVM_DELETED_FUNCTION; public: Index: include/clang/Frontend/Utils.h =================================================================== --- include/clang/Frontend/Utils.h +++ include/clang/Frontend/Utils.h @@ -174,10 +174,10 @@ /// /// \return A CompilerInvocation, or 0 if none was built for the given /// argument vector. -CompilerInvocation * +std::unique_ptr createInvocationFromCommandLine(ArrayRef Args, - IntrusiveRefCntPtr Diags = - IntrusiveRefCntPtr()); + IntrusiveRefCntPtr Diags = + IntrusiveRefCntPtr()); /// Return the value of the last argument as an integer, or a default. If Diags /// is non-null, emits an error if the argument is given, but non-integral. Index: include/clang/Tooling/Tooling.h =================================================================== --- include/clang/Tooling/Tooling.h +++ include/clang/Tooling/Tooling.h @@ -64,9 +64,9 @@ virtual ~ToolAction(); /// \brief Perform an action for an invocation. - virtual bool runInvocation(clang::CompilerInvocation *Invocation, - FileManager *Files, - DiagnosticConsumer *DiagConsumer) = 0; + virtual bool + runInvocation(std::unique_ptr Invocation, + FileManager *Files, DiagnosticConsumer *DiagConsumer) = 0; }; /// \brief Interface to generate clang::FrontendActions. @@ -80,7 +80,8 @@ virtual ~FrontendActionFactory(); /// \brief Invokes the compiler with a FrontendAction created by create(). - bool runInvocation(clang::CompilerInvocation *Invocation, FileManager *Files, + bool runInvocation(std::unique_ptr Invocation, + FileManager *Files, DiagnosticConsumer *DiagConsumer) override; /// \brief Returns a new clang::FrontendAction. @@ -220,7 +221,7 @@ bool runInvocation(const char *BinaryName, clang::driver::Compilation *Compilation, - clang::CompilerInvocation *Invocation); + std::unique_ptr Invocation); std::vector CommandLine; ToolAction *Action; Index: lib/ARCMigrate/ARCMT.cpp =================================================================== --- lib/ARCMigrate/ARCMT.cpp +++ lib/ARCMigrate/ARCMT.cpp @@ -165,10 +165,9 @@ return false; } -static CompilerInvocation * +static std::unique_ptr createInvocationForMigration(CompilerInvocation &origCI) { - std::unique_ptr CInvok; - CInvok.reset(new CompilerInvocation(origCI)); + auto CInvok = llvm::make_unique(origCI); PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); if (!PPOpts.ImplicitPCHInclude.empty()) { // We can't use a PCH because it was likely built in non-ARC mode and we @@ -208,7 +207,7 @@ CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI); - return CInvok.release(); + return CInvok; } static void emitPremigrationErrors(const CapturedDiagList &arcDiags, @@ -246,8 +245,7 @@ NoFinalizeRemoval); assert(!transforms.empty()); - std::unique_ptr CInvok; - CInvok.reset(createInvocationForMigration(origCI)); + auto CInvok = createInvocationForMigration(origCI); CInvok->getFrontendOpts().Inputs.clear(); CInvok->getFrontendOpts().Inputs.push_back(Input); @@ -264,7 +262,7 @@ Diags->setClient(&errRec, /*ShouldOwnClient=*/false); std::unique_ptr Unit( - ASTUnit::LoadFromCompilerInvocationAction(CInvok.release(), Diags)); + ASTUnit::LoadFromCompilerInvocationAction(std::move(CInvok), Diags)); if (!Unit) { errRec.FinishCapture(); return true; @@ -371,9 +369,8 @@ if (outputDir.empty()) { origCI.getLangOpts()->ObjCAutoRefCount = true; return migration.getRemapper().overwriteOriginal(*Diags); - } else { - return migration.getRemapper().flushToDisk(outputDir, *Diags); } + return migration.getRemapper().flushToDisk(outputDir, *Diags); } bool arcmt::applyTransformations(CompilerInvocation &origCI, @@ -514,8 +511,7 @@ bool MigrationProcess::applyTransform(TransformFn trans, RewriteListener *listener) { - std::unique_ptr CInvok; - CInvok.reset(createInvocationForMigration(OrigCI)); + auto CInvok = createInvocationForMigration(OrigCI); CInvok->getDiagnosticOpts().IgnoreWarnings = true; Remapper.applyMappings(CInvok->getPreprocessorOpts()); @@ -533,11 +529,10 @@ CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); Diags->setClient(&errRec, /*ShouldOwnClient=*/false); - std::unique_ptr ASTAction; - ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); + auto ASTAction = llvm::make_unique(ARCMTMacroLocs); std::unique_ptr Unit(ASTUnit::LoadFromCompilerInvocationAction( - CInvok.release(), Diags, ASTAction.get())); + std::move(CInvok), Diags, ASTAction.get())); if (!Unit) { errRec.FinishCapture(); return true; Index: lib/Frontend/ASTUnit.cpp =================================================================== --- lib/Frontend/ASTUnit.cpp +++ lib/Frontend/ASTUnit.cpp @@ -1047,10 +1047,7 @@ llvm::CrashRecoveryContextCleanupRegistrar CICleanup(Clang.get()); - IntrusiveRefCntPtr - CCInvocation(new CompilerInvocation(*Invocation)); - - Clang->setInvocation(CCInvocation.get()); + Clang->setInvocation(llvm::make_unique(*Invocation)); OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); // Set up diagnostics, capturing any diagnostics that would @@ -1362,9 +1359,9 @@ const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild, unsigned MaxLines) { - - IntrusiveRefCntPtr - PreambleInvocation(new CompilerInvocation(PreambleInvocationIn)); + + auto PreambleInvocation = + llvm::make_unique(PreambleInvocationIn); FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts(); PreprocessorOptions &PreprocessorOpts = PreambleInvocation->getPreprocessorOpts(); @@ -1540,7 +1537,7 @@ llvm::CrashRecoveryContextCleanupRegistrar CICleanup(Clang.get()); - Clang->setInvocation(&*PreambleInvocation); + Clang->setInvocation(std::move(PreambleInvocation)); OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); // Set up diagnostics, capturing all of the diagnostics produced. @@ -1707,8 +1704,7 @@ const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0]; if (Input.isFile()) return Input.getFile(); - else - return Input.getBuffer()->getBufferIdentifier(); + return Input.getBuffer()->getBufferIdentifier(); } if (SourceMgr) { @@ -1729,18 +1725,16 @@ return Mod.FileName; } -ASTUnit *ASTUnit::create(CompilerInvocation *CI, +ASTUnit *ASTUnit::create(std::shared_ptr CI, IntrusiveRefCntPtr Diags, - bool CaptureDiagnostics, - bool UserFilesAreVolatile) { - std::unique_ptr AST; - AST.reset(new ASTUnit(false)); + bool CaptureDiagnostics, bool UserFilesAreVolatile) { + std::unique_ptr AST(new ASTUnit(false)); ConfigureDiags(Diags, nullptr, nullptr, *AST, CaptureDiagnostics); AST->Diagnostics = Diags; - AST->Invocation = CI; - AST->FileSystemOpts = CI->getFileSystemOpts(); + AST->Invocation = std::move(CI); + AST->FileSystemOpts = AST->Invocation->getFileSystemOpts(); IntrusiveRefCntPtr VFS = - createVFSFromCompilerInvocation(*CI, *Diags); + createVFSFromCompilerInvocation(*AST->Invocation, *Diags); if (!VFS) return nullptr; AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); @@ -1752,12 +1746,12 @@ } ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( - CompilerInvocation *CI, IntrusiveRefCntPtr Diags, - ASTFrontendAction *Action, ASTUnit *Unit, bool Persistent, - StringRef ResourceFilesPath, bool OnlyLocalDecls, bool CaptureDiagnostics, - bool PrecompilePreamble, bool CacheCodeCompletionResults, - bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile, - std::unique_ptr *ErrAST) { + std::shared_ptr CI, + IntrusiveRefCntPtr Diags, ASTFrontendAction *Action, + ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath, + bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble, + bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, + bool UserFilesAreVolatile, std::unique_ptr *ErrAST) { assert(CI && "A CompilerInvocation is required"); std::unique_ptr OwnAST; @@ -1796,13 +1790,13 @@ ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); // Create the compiler instance to use for building the AST. - std::unique_ptr Clang(new CompilerInstance()); + auto Clang = llvm::make_unique(); // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar CICleanup(Clang.get()); - Clang->setInvocation(CI); + Clang->setInvocation(std::move(CI)); AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); // Set up diagnostics, capturing any diagnostics that would @@ -1916,8 +1910,9 @@ } std::unique_ptr ASTUnit::LoadFromCompilerInvocation( - CompilerInvocation *CI, IntrusiveRefCntPtr Diags, - bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble, + std::unique_ptr CI, + IntrusiveRefCntPtr Diags, bool OnlyLocalDecls, + bool CaptureDiagnostics, bool PrecompilePreamble, TranslationUnitKind TUKind, bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile) { // Create the AST unit. @@ -1930,10 +1925,10 @@ AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; AST->IncludeBriefCommentsInCodeCompletion = IncludeBriefCommentsInCodeCompletion; - AST->Invocation = CI; AST->FileSystemOpts = CI->getFileSystemOpts(); IntrusiveRefCntPtr VFS = createVFSFromCompilerInvocation(*CI, *Diags); + AST->Invocation = std::move(CI); if (!VFS) return nullptr; AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); @@ -1968,8 +1963,8 @@ } SmallVector StoredDiagnostics; - - IntrusiveRefCntPtr CI; + + std::unique_ptr CI; { @@ -1998,8 +1993,7 @@ CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies; // Create the AST unit. - std::unique_ptr AST; - AST.reset(new ASTUnit(false)); + std::unique_ptr AST(new ASTUnit(false)); ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics); AST->Diagnostics = Diags; Diags = nullptr; // Zero out now to ease cleanup during crash recovery. @@ -2018,10 +2012,9 @@ AST->UserFilesAreVolatile = UserFilesAreVolatile; AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); AST->StoredDiagnostics.swap(StoredDiagnostics); - AST->Invocation = CI; + AST->Invocation = std::move(CI); if (ForSerialization) AST->WriterData.reset(new ASTWriterData()); - CI = nullptr; // Zero out now to ease cleanup during crash recovery. // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar @@ -2344,12 +2337,13 @@ CompletionTimer.setOutput("Code completion @ " + File + ":" + Twine(Line) + ":" + Twine(Column)); - IntrusiveRefCntPtr - CCInvocation(new CompilerInvocation(*Invocation)); + auto Clang = llvm::make_unique(); + Clang->setInvocation(llvm::make_unique(*Invocation)); + auto &CCInvocation = Clang->getInvocation(); - FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); + FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts(); CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; - PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); + PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts(); CodeCompleteOpts.IncludeMacros = IncludeMacros && CachedCompletionResults.empty(); @@ -2364,15 +2358,12 @@ FrontendOpts.CodeCompletionAt.Column = Column; // Set the language options appropriately. - LangOpts = *CCInvocation->getLangOpts(); - - std::unique_ptr Clang(new CompilerInstance()); + LangOpts = *CCInvocation.getLangOpts(); // Recover resources if we crash before exiting this method. llvm::CrashRecoveryContextCleanupRegistrar CICleanup(Clang.get()); - Clang->setInvocation(&*CCInvocation); OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); // Set up diagnostics, capturing any diagnostics produced. @@ -2380,8 +2371,8 @@ CaptureDroppedDiagnostics Capture(true, Clang->getDiagnostics(), StoredDiagnostics); - ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts()); - + ProcessWarningOptions(Diag, CCInvocation.getDiagnosticOpts()); + // Create the target instance. Clang->setTarget(TargetInfo::CreateTargetInfo( Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); @@ -2437,9 +2428,8 @@ llvm::sys::fs::UniqueID MainID; if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) { if (CompleteFileID == MainID && Line > 1) - OverrideMainBuffer - = getMainBufferWithPrecompiledPreamble(*CCInvocation, false, - Line - 1); + OverrideMainBuffer = getMainBufferWithPrecompiledPreamble( + CCInvocation, false, Line - 1); } } } Index: lib/Frontend/ChainedIncludesSource.cpp =================================================================== --- lib/Frontend/ChainedIncludesSource.cpp +++ lib/Frontend/ChainedIncludesSource.cpp @@ -123,9 +123,8 @@ for (unsigned i = 0, e = includes.size(); i != e; ++i) { bool firstInclude = (i == 0); - std::unique_ptr CInvok; - CInvok.reset(new CompilerInvocation(CI.getInvocation())); - + auto CInvok = llvm::make_unique(CI.getInvocation()); + CInvok->getPreprocessorOpts().ChainedIncludes.clear(); CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear(); CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear(); @@ -145,7 +144,7 @@ new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient)); std::unique_ptr Clang(new CompilerInstance()); - Clang->setInvocation(CInvok.release()); + Clang->setInvocation(std::move(CInvok)); Clang->setDiagnostics(Diags.get()); Clang->setTarget(TargetInfo::CreateTargetInfo( Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); Index: lib/Frontend/CompilerInstance.cpp =================================================================== --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -52,18 +52,18 @@ using namespace clang; CompilerInstance::CompilerInstance(bool BuildingModule) - : ModuleLoader(BuildingModule), - Invocation(new CompilerInvocation()), ModuleManager(nullptr), - BuildGlobalModuleIndex(false), HaveFullGlobalModuleIndex(false), - ModuleBuildFailed(false) { -} + : ModuleLoader(BuildingModule), + Invocation(llvm::make_unique()), + ModuleManager(nullptr), BuildGlobalModuleIndex(false), + HaveFullGlobalModuleIndex(false), ModuleBuildFailed(false) {} CompilerInstance::~CompilerInstance() { assert(OutputFiles.empty() && "Still output files in flight?"); } -void CompilerInstance::setInvocation(CompilerInvocation *Value) { - Invocation = Value; +void +CompilerInstance::setInvocation(std::shared_ptr Value) { + Invocation = std::move(Value); } bool CompilerInstance::shouldBuildGlobalModuleIndex() const { @@ -862,8 +862,8 @@ = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); // Construct a compiler invocation for creating this module. - IntrusiveRefCntPtr Invocation - (new CompilerInvocation(ImportingInstance.getInvocation())); + auto Invocation = + llvm::make_unique(ImportingInstance.getInvocation()); PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); @@ -915,7 +915,7 @@ // Construct a compiler instance that will be used to actually create the // module. CompilerInstance Instance(/*BuildingModule=*/true); - Instance.setInvocation(&*Invocation); + Instance.setInvocation(std::move(Invocation)); Instance.createDiagnostics(new ForwardingDiagnosticConsumer( ImportingInstance.getDiagnosticClient()), Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -51,12 +51,11 @@ PreprocessorOpts(new PreprocessorOptions()) {} CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &X) - : RefCountedBase(), - LangOpts(new LangOptions(*X.getLangOpts())), - TargetOpts(new TargetOptions(X.getTargetOpts())), - DiagnosticOpts(new DiagnosticOptions(X.getDiagnosticOpts())), - HeaderSearchOpts(new HeaderSearchOptions(X.getHeaderSearchOpts())), - PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())) {} + : LangOpts(new LangOptions(*X.getLangOpts())), + TargetOpts(new TargetOptions(X.getTargetOpts())), + DiagnosticOpts(new DiagnosticOptions(X.getDiagnosticOpts())), + HeaderSearchOpts(new HeaderSearchOptions(X.getHeaderSearchOpts())), + PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())) {} CompilerInvocationBase::~CompilerInvocationBase() {} Index: lib/Frontend/CreateInvocationFromCommandLine.cpp =================================================================== --- lib/Frontend/CreateInvocationFromCommandLine.cpp +++ lib/Frontend/CreateInvocationFromCommandLine.cpp @@ -29,9 +29,9 @@ /// /// \return A CompilerInvocation, or 0 if none was built for the given /// argument vector. -CompilerInvocation * -clang::createInvocationFromCommandLine(ArrayRef ArgList, - IntrusiveRefCntPtr Diags) { +std::unique_ptr clang::createInvocationFromCommandLine( + ArrayRef ArgList, + IntrusiveRefCntPtr Diags) { if (!Diags.get()) { // No diagnostics engine was provided, so create our own diagnostics object // with the default options. @@ -78,12 +78,12 @@ } const ArgStringList &CCArgs = Cmd->getArguments(); - std::unique_ptr CI(new CompilerInvocation()); + auto CI = llvm::make_unique(); if (!CompilerInvocation::CreateFromArgs(*CI, const_cast(CCArgs.data()), const_cast(CCArgs.data()) + CCArgs.size(), *Diags)) return nullptr; - return CI.release(); + return CI; } Index: lib/Tooling/Tooling.cpp =================================================================== --- lib/Tooling/Tooling.cpp +++ lib/Tooling/Tooling.cpp @@ -226,13 +226,12 @@ auto *Input = llvm::MemoryBuffer::getMemBuffer(It.getValue()); Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), Input); } - return runInvocation(BinaryName, Compilation.get(), Invocation.release()); + return runInvocation(BinaryName, Compilation.get(), std::move(Invocation)); } bool ToolInvocation::runInvocation( - const char *BinaryName, - clang::driver::Compilation *Compilation, - clang::CompilerInvocation *Invocation) { + const char *BinaryName, clang::driver::Compilation *Compilation, + std::unique_ptr Invocation) { // Show the invocation, with -v. if (Invocation->getHeaderSearchOpts().Verbose) { llvm::errs() << "clang Invocation:\n"; @@ -240,15 +239,15 @@ llvm::errs() << "\n"; } - return Action->runInvocation(Invocation, Files, DiagConsumer); + return Action->runInvocation(std::move(Invocation), Files, DiagConsumer); } -bool FrontendActionFactory::runInvocation(CompilerInvocation *Invocation, - FileManager *Files, - DiagnosticConsumer *DiagConsumer) { +bool FrontendActionFactory::runInvocation( + std::unique_ptr Invocation, FileManager *Files, + DiagnosticConsumer *DiagConsumer) { // Create a compiler instance to handle the actual work. clang::CompilerInstance Compiler; - Compiler.setInvocation(Invocation); + Compiler.setInvocation(std::move(Invocation)); Compiler.setFileManager(Files); // The FrontendAction can have lifetime requirements for Compiler or its @@ -374,13 +373,15 @@ public: ASTBuilderAction(std::vector> &ASTs) : ASTs(ASTs) {} - bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, + bool runInvocation(std::unique_ptr Invocation, + FileManager *Files, DiagnosticConsumer *DiagConsumer) override { // FIXME: This should use the provided FileManager. + auto *DiagOpts = &Invocation->getDiagnosticOpts(); std::unique_ptr AST = ASTUnit::LoadFromCompilerInvocation( - Invocation, CompilerInstance::createDiagnostics( - &Invocation->getDiagnosticOpts(), DiagConsumer, - /*ShouldOwnClient=*/false)); + std::move(Invocation), + CompilerInstance::createDiagnostics(DiagOpts, DiagConsumer, + /*ShouldOwnClient=*/false)); if (!AST) return false; Index: tools/libclang/Indexing.cpp =================================================================== --- tools/libclang/Indexing.cpp +++ tools/libclang/Indexing.cpp @@ -553,17 +553,17 @@ // present it will be unused. if (source_filename) Args->push_back(source_filename); - - IntrusiveRefCntPtr - CInvok(createInvocationFromCommandLine(*Args, Diags)); + + auto CInvokHolder = llvm::make_unique>( + createInvocationFromCommandLine(*Args, Diags)); + CompilerInvocation *CInvok = CInvokHolder->get(); if (!CInvok) return; // Recover resources if we crash before exiting this function. - llvm::CrashRecoveryContextCleanupRegistrar > - CInvokCleanup(CInvok.get()); + llvm::CrashRecoveryContextCleanupRegistrar< + std::shared_ptr> CInvokCleanup(CInvokHolder.get()); if (CInvok->getFrontendOpts().Inputs.empty()) return; @@ -591,8 +591,7 @@ if (index_options & CXIndexOpt_SuppressWarnings) CInvok->getDiagnosticOpts().IgnoreWarnings = true; - ASTUnit *Unit = ASTUnit::create(CInvok.get(), Diags, - CaptureDiagnostics, + ASTUnit *Unit = ASTUnit::create(*CInvokHolder, Diags, CaptureDiagnostics, /*UserFilesAreVolatile=*/true); if (!Unit) { ITUI->result = CXError_InvalidArguments; @@ -645,17 +644,12 @@ PPOpts.DetailedRecord = false; DiagnosticErrorTrap DiagTrap(*Diags); - bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.get(), Diags, - IndexAction.get(), - Unit, - Persistent, - CXXIdx->getClangResourcesPath(), - OnlyLocalDecls, - CaptureDiagnostics, - PrecompilePreamble, - CacheCodeCompletionResults, - /*IncludeBriefCommentsInCodeCompletion=*/false, - /*UserFilesAreVolatile=*/true); + bool Success = ASTUnit::LoadFromCompilerInvocationAction( + *CInvokHolder, Diags, IndexAction.get(), Unit, Persistent, + CXXIdx->getClangResourcesPath(), OnlyLocalDecls, CaptureDiagnostics, + PrecompilePreamble, CacheCodeCompletionResults, + /*IncludeBriefCommentsInCodeCompletion=*/false, + /*UserFilesAreVolatile=*/true); if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) printDiagsToStderr(Unit); Index: unittests/AST/ExternalASTSourceTest.cpp =================================================================== --- unittests/AST/ExternalASTSourceTest.cpp +++ unittests/AST/ExternalASTSourceTest.cpp @@ -48,14 +48,14 @@ CompilerInstance Compiler; Compiler.createDiagnostics(); - CompilerInvocation *Invocation = new CompilerInvocation; + auto Invocation = llvm::make_unique(); Invocation->getPreprocessorOpts().addRemappedFile( "test.cc", MemoryBuffer::getMemBuffer(FileContents)); const char *Args[] = { "test.cc" }; CompilerInvocation::CreateFromArgs(*Invocation, Args, Args + array_lengthof(Args), Compiler.getDiagnostics()); - Compiler.setInvocation(Invocation); + Compiler.setInvocation(std::move(Invocation)); TestFrontendAction Action(Source); return Compiler.ExecuteAction(Action); Index: unittests/Frontend/FrontendActionTest.cpp =================================================================== --- unittests/Frontend/FrontendActionTest.cpp +++ unittests/Frontend/FrontendActionTest.cpp @@ -63,7 +63,7 @@ }; TEST(ASTFrontendAction, Sanity) { - CompilerInvocation *invocation = new CompilerInvocation; + auto invocation = llvm::make_unique(); invocation->getPreprocessorOpts().addRemappedFile( "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }")); invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc", @@ -71,7 +71,7 @@ invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; CompilerInstance compiler; - compiler.setInvocation(invocation); + compiler.setInvocation(std::move(invocation)); compiler.createDiagnostics(); TestASTFrontendAction test_action; @@ -82,7 +82,7 @@ } TEST(ASTFrontendAction, IncrementalParsing) { - CompilerInvocation *invocation = new CompilerInvocation; + auto invocation = llvm::make_unique(); invocation->getPreprocessorOpts().addRemappedFile( "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }")); invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc", @@ -90,7 +90,7 @@ invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; CompilerInstance compiler; - compiler.setInvocation(invocation); + compiler.setInvocation(std::move(invocation)); compiler.createDiagnostics(); TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true); @@ -126,7 +126,7 @@ }; TEST(PreprocessorFrontendAction, EndSourceFile) { - CompilerInvocation *Invocation = new CompilerInvocation; + auto Invocation = llvm::make_unique(); Invocation->getPreprocessorOpts().addRemappedFile( "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }")); Invocation->getFrontendOpts().Inputs.push_back( @@ -134,7 +134,7 @@ Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; CompilerInstance Compiler; - Compiler.setInvocation(Invocation); + Compiler.setInvocation(std::move(Invocation)); Compiler.createDiagnostics(); TestPPCallbacks *Callbacks = new TestPPCallbacks;