]> granicus.if.org Git - clang/commitdiff
Make sure temporary files get unlinked.
authorSteve Naroff <snaroff@apple.com>
Thu, 15 Oct 2009 22:23:48 +0000 (22:23 +0000)
committerSteve Naroff <snaroff@apple.com>
Thu, 15 Oct 2009 22:23:48 +0000 (22:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84208 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/ASTUnit.h
include/clang/Frontend/PCHReader.h
lib/Frontend/ASTUnit.cpp
tools/CIndex/CIndex.cpp
tools/c-index-test/c-index-test.c

index 89eb3b8821ca0bfc166cba6782c0b7bf04c832fa..c1a0ebb559cb7d7c5389d529b37657c80eb1ab39 100644 (file)
@@ -38,7 +38,8 @@ class ASTUnit {
   llvm::OwningPtr<TargetInfo>       Target;
   llvm::OwningPtr<Preprocessor>     PP;
   llvm::OwningPtr<ASTContext>       Ctx;
-
+  bool                              tempFile;
+  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
   ASTUnit(Diagnostic &_Diag);
@@ -60,7 +61,10 @@ public:
 
   FileManager &getFileManager();
   const std::string &getOriginalSourceFileName();
+  const std::string &getPCHFileName();
 
+  void unlinkTemporaryFile() { tempFile = true; }
+  
   /// \brief Create a ASTUnit from a PCH file.
   ///
   /// \param Filename - The PCH file to load.
index 1230e3753e04db535c9fe3a17e6c0242e3150dd4..27e4d3f1cb57a503d396e49df4f2f017bdc8ca16 100644 (file)
@@ -513,6 +513,9 @@ public:
   /// \brief Sets and initializes the given Context.
   void InitializeContext(ASTContext &Context);
 
+  /// \brief Retrieve the name of the PCH file
+  const std::string &getFileName() { return FileName; }
+
   /// \brief Retrieve the name of the original source file name
   const std::string &getOriginalSourceFile() { return OriginalFileName; }
 
index d3475b5236d926ca7b127d61b4b51786b49e1d83..3b2aa75008e5b136e730fcb61cc86ff7a1a03968 100644 (file)
 
 using namespace clang;
 
-ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { }
-ASTUnit::~ASTUnit() { }
+ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { }
+ASTUnit::~ASTUnit() { 
+  if (tempFile)
+    unlink(getPCHFileName().c_str());
+}
 
 namespace {
 
@@ -80,6 +83,10 @@ const std::string &ASTUnit::getOriginalSourceFileName() {
   return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
 }
 
+const std::string &ASTUnit::getPCHFileName() {
+  return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
+}
+
 FileManager &ASTUnit::getFileManager() {
   return HeaderInfo->getFileMgr();
 }
index 33099cc2393cbd5ca0788bc2fc2c84f5cf70477d..5e999c10037eae65ac510f782c740e91774910ae 100644 (file)
@@ -325,7 +325,10 @@ CXTranslationUnit clang_createTranslationUnitFromSourceFile(
   } while (tpid != child_pid);
   
   // Finally, we create the translation unit from the ast file.
-  return clang_createTranslationUnit(CIdx, astTmpFile);
+  ASTUnit *ATU = static_cast<ASTUnit *>(
+                   clang_createTranslationUnit(CIdx, astTmpFile));
+  ATU->unlinkTemporaryFile();
+  return ATU;
 }
 
 void clang_disposeTranslationUnit(
index c514b63d94be2598c504df6f5e296d5fced91efb..716c15ddecffc346a334f02fddcd1cf43be37efc 100644 (file)
@@ -91,6 +91,7 @@ int main(int argc, char **argv) {
 
   if (!strcmp(argv[2], "all")) {
     clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
+    clang_disposeTranslationUnit(TU);
     return 1;
   }
   /* Perform some simple filtering. */
@@ -101,6 +102,7 @@ int main(int argc, char **argv) {
   else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
 
   clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
+  clang_disposeTranslationUnit(TU);
   return 1;
   }
 }