Fixing up a few cases of std::unique_ptr<T>::release to push explicitly typed
memory ownership through several APIs.
One of the weirder things was the crash cleanup handling in
libclang/Indexing.cpp - in several existing cases, as well as one I had to
modify, objects are dynamically allocated, owned via std::unique_ptr, then
registered with a crash handler to delete the object in case of a crash. I
think these could be reasonably simplified by having a crash handler that just
calls the dtor rather than delete, thus leaving these objects on the stack
instead.
The general ownership code in this whole area (see other members of
CompilerInvocation and other related types nearby) seems rather complex and I'm
wondering if anyone has a bigger picture idea of what direction this code
should be heading in. Is there some unwritten rewrite that's desired here that
would simplify this ownership in another way? I'd be willing to scrap this
patch and work on that, otherwise I'll probably keep muddling through with
unique_ptr/shared_ptr doing mechanical cleanup and perhaps some better design
will become clearer over time.
I've used a mix of unique_ptr and shared_ptr here - starting out with
unique_ptr as far as it would go, then having to switch to shared_ptr in areas
where sharing seems to be occurring based on my best effort to understand the
ownership here. This means we don't get the single-allocation benefits of
std::make_shared, but simplifies parts of the code where only single ownership
is required. Open to competing attitudes/ideas in how to approach this.
http://reviews.llvm.org/D4838
Files:
examples/clang-interpreter/main.cpp
include/clang/Frontend/ASTUnit.h
include/clang/Frontend/CompilerInstance.h
include/clang/Frontend/CompilerInvocation.h
include/clang/Frontend/Utils.h
include/clang/Tooling/Tooling.h
lib/ARCMigrate/ARCMT.cpp
lib/Frontend/ASTUnit.cpp
lib/Frontend/ChainedIncludesSource.cpp
lib/Frontend/CompilerInstance.cpp
lib/Frontend/CompilerInvocation.cpp
lib/Frontend/CreateInvocationFromCommandLine.cpp
lib/Tooling/Tooling.cpp
tools/libclang/Indexing.cpp
unittests/AST/ExternalASTSourceTest.cpp
unittests/Frontend/FrontendActionTest.cpp
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<CompilerInvocation> Invocation;
-
+ std::shared_ptr<CompilerInvocation> 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<std::string, llvm::MemoryBuffer *> 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<CompilerInvocation> CI,
IntrusiveRefCntPtr<DiagnosticsEngine> 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<DiagnosticsEngine> Diags,
+ std::shared_ptr<CompilerInvocation> CI,
+ IntrusiveRefCntPtr<DiagnosticsEngine> 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<ASTUnit> LoadFromCompilerInvocation(
- CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
- bool PrecompilePreamble = false, TranslationUnitKind TUKind = TU_Complete,
+ std::unique_ptr<CompilerInvocation> CI,
+ IntrusiveRefCntPtr<DiagnosticsEngine> 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<CompilerInvocation> Invocation;
+ std::shared_ptr<CompilerInvocation> Invocation;
/// The diagnostics engine instance.
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
@@ -206,7 +206,7 @@
}
/// setInvocation - Replace the current invocation.
- void setInvocation(CompilerInvocation *Value);
+ void setInvocation(std::shared_ptr<CompilerInvocation> 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<CompilerInvocation> {
+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<CompilerInvocation>
createInvocationFromCommandLine(ArrayRef<const char *> Args,
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
- IntrusiveRefCntPtr<DiagnosticsEngine>());
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
+ IntrusiveRefCntPtr<DiagnosticsEngine>());
/// 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<clang::CompilerInvocation> 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<clang::CompilerInvocation> 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<clang::CompilerInvocation> Invocation);
std::vector<std::string> 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<CompilerInvocation>
createInvocationForMigration(CompilerInvocation &origCI) {
- std::unique_ptr<CompilerInvocation> CInvok;
- CInvok.reset(new CompilerInvocation(origCI));
+ auto CInvok = llvm::make_unique<CompilerInvocation>(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<CompilerInvocation> 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<ASTUnit> 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<CompilerInvocation> 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<ARCMTMacroTrackerAction> ASTAction;
- ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs));
+ auto ASTAction = llvm::make_unique<ARCMTMacroTrackerAction>(ARCMTMacroLocs);
std::unique_ptr<ASTUnit> 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<CompilerInstance>
CICleanup(Clang.get());
- IntrusiveRefCntPtr<CompilerInvocation>
- CCInvocation(new CompilerInvocation(*Invocation));
-
- Clang->setInvocation(CCInvocation.get());
+ Clang->setInvocation(llvm::make_unique<CompilerInvocation>(*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<CompilerInvocation>
- PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
+
+ auto PreambleInvocation =
+ llvm::make_unique<CompilerInvocation>(PreambleInvocationIn);
FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
PreprocessorOptions &PreprocessorOpts
= PreambleInvocation->getPreprocessorOpts();
@@ -1540,7 +1537,7 @@
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
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<CompilerInvocation> CI,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- bool CaptureDiagnostics,
- bool UserFilesAreVolatile) {
- std::unique_ptr<ASTUnit> AST;
- AST.reset(new ASTUnit(false));
+ bool CaptureDiagnostics, bool UserFilesAreVolatile) {
+ std::unique_ptr<ASTUnit> 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::FileSystem> 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<DiagnosticsEngine> Diags,
- ASTFrontendAction *Action, ASTUnit *Unit, bool Persistent,
- StringRef ResourceFilesPath, bool OnlyLocalDecls, bool CaptureDiagnostics,
- bool PrecompilePreamble, bool CacheCodeCompletionResults,
- bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile,
- std::unique_ptr<ASTUnit> *ErrAST) {
+ std::shared_ptr<CompilerInvocation> CI,
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags, ASTFrontendAction *Action,
+ ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
+ bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble,
+ bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
+ bool UserFilesAreVolatile, std::unique_ptr<ASTUnit> *ErrAST) {
assert(CI && "A CompilerInvocation is required");
std::unique_ptr<ASTUnit> OwnAST;
@@ -1796,13 +1790,13 @@
ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
+ auto Clang = llvm::make_unique<CompilerInstance>();
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
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> ASTUnit::LoadFromCompilerInvocation(
- CompilerInvocation *CI, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
- bool OnlyLocalDecls, bool CaptureDiagnostics, bool PrecompilePreamble,
+ std::unique_ptr<CompilerInvocation> CI,
+ IntrusiveRefCntPtr<DiagnosticsEngine> 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::FileSystem> 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<StoredDiagnostic, 4> StoredDiagnostics;
-
- IntrusiveRefCntPtr<CompilerInvocation> CI;
+
+ std::unique_ptr<CompilerInvocation> CI;
{
@@ -1998,8 +1993,7 @@
CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
// Create the AST unit.
- std::unique_ptr<ASTUnit> AST;
- AST.reset(new ASTUnit(false));
+ std::unique_ptr<ASTUnit> 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<ASTUnit>
@@ -2344,12 +2337,13 @@
CompletionTimer.setOutput("Code completion @ " + File + ":" +
Twine(Line) + ":" + Twine(Column));
- IntrusiveRefCntPtr<CompilerInvocation>
- CCInvocation(new CompilerInvocation(*Invocation));
+ auto Clang = llvm::make_unique<CompilerInstance>();
+ Clang->setInvocation(llvm::make_unique<CompilerInvocation>(*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,24 +2358,21 @@
FrontendOpts.CodeCompletionAt.Column = Column;
// Set the language options appropriately.
- LangOpts = *CCInvocation->getLangOpts();
-
- std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
+ LangOpts = *CCInvocation.getLangOpts();
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- Clang->setInvocation(&*CCInvocation);
OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
// Set up diagnostics, capturing any diagnostics produced.
Clang->setDiagnostics(&Diag);
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<CompilerInvocation> CInvok;
- CInvok.reset(new CompilerInvocation(CI.getInvocation()));
-
+ auto CInvok = llvm::make_unique<CompilerInvocation>(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<CompilerInstance> 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<CompilerInvocation>()),
+ 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<CompilerInvocation> 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<CompilerInvocation> Invocation
- (new CompilerInvocation(ImportingInstance.getInvocation()));
+ auto Invocation =
+ llvm::make_unique<CompilerInvocation>(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<CompilerInvocation>(),
- 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<const char *> ArgList,
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
+std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
+ ArrayRef<const char *> ArgList,
+ IntrusiveRefCntPtr<DiagnosticsEngine> 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<CompilerInvocation> CI(new CompilerInvocation());
+ auto CI = llvm::make_unique<CompilerInvocation>();
if (!CompilerInvocation::CreateFromArgs(*CI,
const_cast<const char **>(CCArgs.data()),
const_cast<const char **>(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,29 +226,28 @@
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<clang::CompilerInvocation> Invocation) {
// Show the invocation, with -v.
if (Invocation->getHeaderSearchOpts().Verbose) {
llvm::errs() << "clang Invocation:\n";
Compilation->getJobs().Print(llvm::errs(), "\n", true);
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<CompilerInvocation> 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<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
- bool runInvocation(CompilerInvocation *Invocation, FileManager *Files,
+ bool runInvocation(std::unique_ptr<CompilerInvocation> Invocation,
+ FileManager *Files,
DiagnosticConsumer *DiagConsumer) override {
// FIXME: This should use the provided FileManager.
+ auto *DiagOpts = &Invocation->getDiagnosticOpts();
std::unique_ptr<ASTUnit> 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<CompilerInvocation>
- CInvok(createInvocationFromCommandLine(*Args, Diags));
+
+ auto CInvokHolder = llvm::make_unique<std::shared_ptr<CompilerInvocation>>(
+ createInvocationFromCommandLine(*Args, Diags));
+ CompilerInvocation *CInvok = CInvokHolder->get();
if (!CInvok)
return;
// Recover resources if we crash before exiting this function.
- llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
- llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
- CInvokCleanup(CInvok.get());
+ llvm::CrashRecoveryContextCleanupRegistrar<
+ std::shared_ptr<CompilerInvocation>> 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<CompilerInvocation>();
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,15 +63,15 @@
};
TEST(ASTFrontendAction, Sanity) {
- CompilerInvocation *invocation = new CompilerInvocation;
+ auto invocation = llvm::make_unique<CompilerInvocation>();
invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
IK_CXX));
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,15 +82,15 @@
}
TEST(ASTFrontendAction, IncrementalParsing) {
- CompilerInvocation *invocation = new CompilerInvocation;
+ auto invocation = llvm::make_unique<CompilerInvocation>();
invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
IK_CXX));
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,15 +126,15 @@
};
TEST(PreprocessorFrontendAction, EndSourceFile) {
- CompilerInvocation *Invocation = new CompilerInvocation;
+ auto Invocation = llvm::make_unique<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", IK_CXX));
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;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits