[dsymutil] Remove empty reproducer directory on exit
authorKeith Smiley <keithbsmiley@gmail.com>
Tue, 4 Apr 2023 04:43:36 +0000 (21:43 -0700)
committerKeith Smiley <keithbsmiley@gmail.com>
Wed, 5 Apr 2023 18:05:16 +0000 (11:05 -0700)
In the case the reproducer isn't generated, we don't want to leave
around an empty temporary directory.

Fixes https://github.com/llvm/llvm-project/issues/61920

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

llvm/test/tools/dsymutil/X86/reproducer.test
llvm/tools/dsymutil/Reproducer.cpp

index a09d059..7e65d4e 100644 (file)
@@ -4,7 +4,7 @@
 
 # Recreate the folder structure in a temp directory we can remove later.
 RUN: rm -rf %t
-RUN: mkdir -p %t/Inputs
+RUN: mkdir -p %t/Inputs %t/tempdir
 RUN: cp %p/../Inputs/basic.macho.x86_64 %t/Inputs
 RUN: cp %p/../Inputs/basic1.macho.x86_64.o %t/Inputs
 RUN: cp %p/../Inputs/basic2.macho.x86_64.o %t/Inputs
@@ -16,6 +16,10 @@ RUN: dsymutil -f -o - -oso-prepend-path=%t %t/Inputs/basic.macho.x86_64 | llvm-d
 # Make sure we don't crash with an empty TMPDIR.
 RUN: env TMPDIR="" dsymutil -o -f -oso-prepend-path=%t %t/Inputs/basic.macho.x86_64 2>&1
 
+# Make sure we don't leave around a temporary directory.
+RUN: env TMPDIR="%t/tempdir" dsymutil -o - -f %t/Inputs/basic.macho.x86_64
+RUN: not ls %t/tempdir/dsymutil-*
+
 # Create a reproducer.
 RUN: env DSYMUTIL_REPRODUCER_PATH=%t.repro dsymutil -gen-reproducer -f -o %t.generate -oso-prepend-path=%t %t/Inputs/basic.macho.x86_64 2>&1 | FileCheck %s --check-prefixes=REPRODUCER
 RUN: llvm-dwarfdump -a %t.generate | FileCheck %s
index dda5557..2522e2c 100644 (file)
@@ -40,6 +40,8 @@ ReproducerGenerate::ReproducerGenerate(std::error_code &EC, int Argc,
 ReproducerGenerate::~ReproducerGenerate() {
   if (GenerateOnExit && !Generated)
     generate();
+  else if (!Generated && !Root.empty())
+    sys::fs::remove_directories(Root, /* IgnoreErrors */ true);
 }
 
 void ReproducerGenerate::generate() {