[cfe-commits] r94924 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp

Daniel Dunbar daniel at zuster.org
Sat Jan 30 13:47:17 PST 2010


Author: ddunbar
Date: Sat Jan 30 15:47:16 2010
New Revision: 94924

URL: http://llvm.org/viewvc/llvm-project?rev=94924&view=rev
Log:
ASTUnit: Ensure the CompilerInvocation object used in LoadFromCommandLine is
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.

Modified:
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=94924&r1=94923&r2=94924&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Sat Jan 30 15:47:16 2010
@@ -53,6 +53,10 @@
   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 @@
   /// 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.

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=94924&r1=94923&r2=94924&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Sat Jan 30 15:47:16 2010
@@ -238,7 +238,7 @@
   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 @@
 
   Clang.takeDiagnosticClient();
   Clang.takeDiagnostics();
+  Clang.takeInvocation();
 
   return AST.take();
 
@@ -349,19 +350,23 @@
   }
 
   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;
 }





More information about the cfe-commits mailing list