[cfe-commits] r96628 - in /cfe/trunk: include/clang/Frontend/ASTUnit.h lib/Frontend/ASTUnit.cpp tools/CIndex/CIndex.cpp tools/CIndex/CIndexCodeCompletion.cpp

Douglas Gregor dgregor at apple.com
Thu Feb 18 15:35:46 PST 2010


Author: dgregor
Date: Thu Feb 18 17:35:40 2010
New Revision: 96628

URL: http://llvm.org/viewvc/llvm-project?rev=96628&view=rev
Log:
Teach ASTUnit to keep track of temporary files, then delete them when
the ASTUnit itself is destroyed. Fixes <rdar://problem/7649385>.

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

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

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Thu Feb 18 17:35:40 2010
@@ -19,6 +19,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Index/ASTLocation.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/System/Path.h"
 #include <string>
 #include <vector>
 #include <cassert>
@@ -52,7 +53,6 @@
   llvm::OwningPtr<TargetInfo>       Target;
   llvm::OwningPtr<Preprocessor>     PP;
   llvm::OwningPtr<ASTContext>       Ctx;
-  bool                              tempFile;
 
   /// Optional owned invocation, just used to make the invocation used in
   /// LoadFromCommandLine available.
@@ -85,6 +85,10 @@
   /// translation unit.
   llvm::SmallVector<StoredDiagnostic, 4> Diagnostics;
 
+  /// \brief Temporary files that should be removed when the ASTUnit is 
+  /// destroyed.
+  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
+  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
 
@@ -109,8 +113,13 @@
   const std::string &getOriginalSourceFileName();
   const std::string &getPCHFileName();
 
-  void unlinkTemporaryFile() { tempFile = true; }
-
+  /// \brief Add a temporary file that the ASTUnit depends on.
+  ///
+  /// This file will be erased when the ASTUnit is destroyed.
+  void addTemporaryFile(const llvm::sys::Path &TempFile) {
+    TemporaryFiles.push_back(TempFile);
+  }
+                        
   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
 
   void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }

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

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Thu Feb 18 17:35:40 2010
@@ -36,11 +36,11 @@
 using namespace clang;
 
 ASTUnit::ASTUnit(bool _MainFileIsAST)
-  : tempFile(false), MainFileIsAST(_MainFileIsAST) {
+  : MainFileIsAST(_MainFileIsAST) {
 }
 ASTUnit::~ASTUnit() {
-  if (tempFile)
-    llvm::sys::Path(getPCHFileName()).eraseFromDisk();
+  for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
+    TemporaryFiles[I].eraseFromDisk();
 }
 
 namespace {

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=96628&r1=96627&r2=96628&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Feb 18 17:35:40 2010
@@ -966,9 +966,9 @@
   llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
   for (unsigned I = 0; I != num_unsaved_files; ++I) {
     const llvm::MemoryBuffer *Buffer
-    = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
-                                       unsaved_files[I].Contents + unsaved_files[I].Length,
-                                       unsaved_files[I].Filename);
+      = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
+                          unsaved_files[I].Contents + unsaved_files[I].Length,
+                                         unsaved_files[I].Filename);
     RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
                                            Buffer));
   }
@@ -1129,12 +1129,19 @@
     }
   }
 
-  if (ATU)
-    ATU->unlinkTemporaryFile();
-
-  for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
-    TemporaryFiles[i].eraseFromDisk();
-
+  if (ATU) {
+    // Make the translation unit responsible for destroying all temporary files.
+    for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
+      ATU->addTemporaryFile(TemporaryFiles[i]);
+    ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
+  } else {
+    // Destroy all of the temporary files now; they can't be referenced any
+    // longer.
+    llvm::sys::Path(astTmpFile).eraseFromDisk();
+    for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
+      TemporaryFiles[i].eraseFromDisk();
+  }
+  
   return ATU;
 }
 

Modified: cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp?rev=96628&r1=96627&r2=96628&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexCodeCompletion.cpp Thu Feb 18 17:35:40 2010
@@ -195,6 +195,10 @@
   
   /// \brief File manager, used for diagnostics.
   FileManager FileMgr;
+  
+  /// \brief Temporary files that should be removed once we have finished
+  /// with the code-completion results.
+  std::vector<llvm::sys::Path> TemporaryFiles;
 };
 
 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 
@@ -205,6 +209,9 @@
     delete (CodeCompletionString *)Results[I].CompletionString;
   delete [] Results;
   delete Buffer;
+  
+  for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
+    TemporaryFiles[I].eraseFromDisk();
 }
   
 CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
@@ -369,8 +376,9 @@
                             Results->FileMgr, Results->SourceMgr, 
                             Results->Diagnostics);
 
-  for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
-    TemporaryFiles[i].eraseFromDisk();
+  // Make sure we delete temporary files when the code-completion results are
+  // destroyed.
+  Results->TemporaryFiles.swap(TemporaryFiles);
 
   return Results;
 }





More information about the cfe-commits mailing list