[clang] 3eceab9 - LibclangTest: remove libclang-test-* tmp dir reliably

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 1 06:45:36 PST 2023


Author: Igor Kushnir
Date: 2023-03-01T09:45:24-05:00
New Revision: 3eceab95f314edb85ee72c047a4acd35e757d037

URL: https://github.com/llvm/llvm-project/commit/3eceab95f314edb85ee72c047a4acd35e757d037
DIFF: https://github.com/llvm/llvm-project/commit/3eceab95f314edb85ee72c047a4acd35e757d037.diff

LOG: LibclangTest: remove libclang-test-* tmp dir reliably

Temporary directories created by two LibclangReparseTest tests -
ReparseWithModule and clang_parseTranslationUnit2FullArgv - remained in
the system temporary directory after running libclangTests, because not
all files and subdirectories created in TestDir were added to set
LibclangParseTest::Files.

Differential Revision: https://reviews.llvm.org/D143415

Added: 
    

Modified: 
    clang/unittests/libclang/LibclangTest.cpp
    clang/unittests/libclang/TestUtils.h

Removed: 
    


################################################################################
diff  --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp
index 809426a01ca7..11a729fc040d 100644
--- a/clang/unittests/libclang/LibclangTest.cpp
+++ b/clang/unittests/libclang/LibclangTest.cpp
@@ -515,6 +515,8 @@ TEST_F(LibclangReparseTest, ReparseWithModule) {
   WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom);
   WriteFile(ModName, ModFile);
 
+  // Removing recursively is necessary to delete the module cache.
+  RemoveTestDirRecursivelyDuringTeardown = true;
   std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir;
   const char *Args[] = { "-fmodules", ModulesCache.c_str(),
                          "-I", TestDir.c_str() };

diff  --git a/clang/unittests/libclang/TestUtils.h b/clang/unittests/libclang/TestUtils.h
index 8900d79ea5ee..c69746c4408a 100644
--- a/clang/unittests/libclang/TestUtils.h
+++ b/clang/unittests/libclang/TestUtils.h
@@ -14,18 +14,21 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 
+#include "gtest/gtest.h"
 #include <fstream>
+#include <functional>
 #include <memory>
 #include <string>
 #include <vector>
-#include "gtest/gtest.h"
 
 class LibclangParseTest : public ::testing::Test {
-  std::set<std::string> Files;
+  // std::greater<> to remove files before their parent dirs in TearDown().
+  std::set<std::string, std::greater<>> Files;
   typedef std::unique_ptr<std::string> fixed_addr_string;
   std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents;
 public:
   std::string TestDir;
+  bool RemoveTestDirRecursivelyDuringTeardown = false;
   CXIndex Index;
   CXTranslationUnit ClangTU;
   unsigned TUFlags;
@@ -43,16 +46,26 @@ class LibclangParseTest : public ::testing::Test {
   void TearDown() override {
     clang_disposeTranslationUnit(ClangTU);
     clang_disposeIndex(Index);
+
+    namespace fs = llvm::sys::fs;
     for (const std::string &Path : Files)
-      llvm::sys::fs::remove(Path);
-    llvm::sys::fs::remove(TestDir);
+      EXPECT_FALSE(fs::remove(Path, /*IgnoreNonExisting=*/false));
+    if (RemoveTestDirRecursivelyDuringTeardown)
+      EXPECT_FALSE(fs::remove_directories(TestDir, /*IgnoreErrors=*/false));
+    else
+      EXPECT_FALSE(fs::remove(TestDir, /*IgnoreNonExisting=*/false));
   }
   void WriteFile(std::string &Filename, const std::string &Contents) {
     if (!llvm::sys::path::is_absolute(Filename)) {
       llvm::SmallString<256> Path(TestDir);
-      llvm::sys::path::append(Path, Filename);
+      namespace path = llvm::sys::path;
+      for (auto FileI = path::begin(Filename), FileEnd = path::end(Filename);
+           FileI != FileEnd; ++FileI) {
+        ASSERT_NE(*FileI, ".");
+        path::append(Path, *FileI);
+        Files.emplace(Path.str());
+      }
       Filename = std::string(Path.str());
-      Files.insert(Filename);
     }
     llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename));
     std::ofstream OS(Filename);


        


More information about the cfe-commits mailing list