]> granicus.if.org Git - clang/commitdiff
ASTUnit: Ensure the CompilerInvocation object used in LoadFromCommandLine is
authorDaniel Dunbar <daniel@zuster.org>
Sat, 30 Jan 2010 21:47:16 +0000 (21:47 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sat, 30 Jan 2010 21:47:16 +0000 (21:47 +0000)
live as long as the ASTUnit. This is useful for clients which want to maintain
pointers to the LangOptions object which ultimately lives in the
CompilerInvocation, although it would be nice to make all of this ownership
stuff more explicit and obvious.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94924 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/ASTUnit.h
lib/Frontend/ASTUnit.cpp

index 2659dbb2a30ce1c005b956ada7e9d498fb842ba5..7aeabe57ac002b6b484879de97b12c9c8c64ba54 100644 (file)
@@ -53,6 +53,10 @@ class ASTUnit {
   llvm::OwningPtr<ASTContext>       Ctx;
   bool                              tempFile;
 
+  /// Optional owned invocation, just used to make the invocation used in
+  /// LoadFromCommandLine available.
+  llvm::OwningPtr<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.
@@ -139,7 +143,8 @@ public:
   /// CompilerInvocation object.
   ///
   /// \param CI - The compiler invocation to use; it must have exactly one input
-  /// source file.
+  /// source file. The caller is responsible for ensuring the lifetime of the
+  /// invocation extends past that of the returned ASTUnit.
   ///
   /// \param Diags - The diagnostics engine to use for reporting errors; its
   /// lifetime is expected to extend past that of the returned ASTUnit.
index eaa2062c862f15faa86003e14582316b545e3ed9..7f1e722f32c374e4366b8b7bcd3f1d3bf428d129 100644 (file)
@@ -238,7 +238,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI,
   llvm::OwningPtr<ASTUnit> AST;
   llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
 
-  Clang.getInvocation() = CI;
+  Clang.setInvocation(const_cast<CompilerInvocation*>(&CI));
 
   Clang.setDiagnostics(&Diags);
   Clang.setDiagnosticClient(Diags.getClient());
@@ -294,6 +294,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI,
 
   Clang.takeDiagnosticClient();
   Clang.takeDiagnostics();
+  Clang.takeInvocation();
 
   return AST.take();
 
@@ -349,19 +350,23 @@ ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
   }
 
   const driver::ArgStringList &CCArgs = Cmd->getArguments();
-  CompilerInvocation CI;
-  CompilerInvocation::CreateFromArgs(CI, (const char**) CCArgs.data(),
+  llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
+  CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
                                      (const char**) CCArgs.data()+CCArgs.size(),
                                      Diags);
 
   // Override any files that need remapping
   for (unsigned I = 0; I != NumRemappedFiles; ++I)
-    CI.getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
+    CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
                                              RemappedFiles[I].second);
   
   // Override the resources path.
-  CI.getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
+  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
 
-  CI.getFrontendOpts().DisableFree = UseBumpAllocator;
-  return LoadFromCompilerInvocation(CI, Diags, OnlyLocalDecls);
+  CI->getFrontendOpts().DisableFree = UseBumpAllocator;
+  ASTUnit *Unit = LoadFromCompilerInvocation(*CI, Diags, OnlyLocalDecls);
+  if (Unit)
+    Unit->Invocation.reset(CI.take());
+
+  return Unit;
 }