Make sure temporary files get unlinked.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84208 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h
index 89eb3b8..c1a0ebb 100644
--- a/include/clang/Frontend/ASTUnit.h
+++ b/include/clang/Frontend/ASTUnit.h
@@ -38,7 +38,8 @@
   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 @@
 
   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.
diff --git a/include/clang/Frontend/PCHReader.h b/include/clang/Frontend/PCHReader.h
index 1230e37..27e4d3f 100644
--- a/include/clang/Frontend/PCHReader.h
+++ b/include/clang/Frontend/PCHReader.h
@@ -513,6 +513,9 @@
   /// \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; }
 
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index d3475b5..3b2aa75 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -24,8 +24,11 @@
 
 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 @@
   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();
 }
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 33099cc..5e999c1 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -325,7 +325,10 @@
   } 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(
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index c514b63..716c15d 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -91,6 +91,7 @@
 
   if (!strcmp(argv[2], "all")) {
     clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
+    clang_disposeTranslationUnit(TU);
     return 1;
   }
   /* Perform some simple filtering. */
@@ -101,6 +102,7 @@
   else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
 
   clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
+  clang_disposeTranslationUnit(TU);
   return 1;
   }
 }