From: Steve Naroff Date: Thu, 15 Oct 2009 22:23:48 +0000 (+0000) Subject: Make sure temporary files get unlinked. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e19944c93961b7618f4f3f3185f698f46369ea54;p=clang 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 89eb3b8821..c1a0ebb559 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -38,7 +38,8 @@ class ASTUnit { llvm::OwningPtr Target; llvm::OwningPtr PP; llvm::OwningPtr 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. diff --git a/include/clang/Frontend/PCHReader.h b/include/clang/Frontend/PCHReader.h index 1230e3753e..27e4d3f1cb 100644 --- a/include/clang/Frontend/PCHReader.h +++ b/include/clang/Frontend/PCHReader.h @@ -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; } diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index d3475b5236..3b2aa75008 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 @@ const std::string &ASTUnit::getOriginalSourceFileName() { return dyn_cast(Ctx->getExternalSource())->getOriginalSourceFile(); } +const std::string &ASTUnit::getPCHFileName() { + return dyn_cast(Ctx->getExternalSource())->getFileName(); +} + FileManager &ASTUnit::getFileManager() { return HeaderInfo->getFileMgr(); } diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 33099cc239..5e999c1003 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -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( + 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 c514b63d94..716c15ddec 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -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; } }