From 8245f2665240f7961ae39bc606d7b6a4b0d62c4d Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Mon, 3 Apr 2023 21:43:36 -0700 Subject: [PATCH] [dsymutil] Remove empty reproducer directory on exit 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 | 6 +++++- llvm/tools/dsymutil/Reproducer.cpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/llvm/test/tools/dsymutil/X86/reproducer.test b/llvm/test/tools/dsymutil/X86/reproducer.test index a09d059..7e65d4e 100644 --- a/llvm/test/tools/dsymutil/X86/reproducer.test +++ b/llvm/test/tools/dsymutil/X86/reproducer.test @@ -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 diff --git a/llvm/tools/dsymutil/Reproducer.cpp b/llvm/tools/dsymutil/Reproducer.cpp index dda5557..2522e2c 100644 --- a/llvm/tools/dsymutil/Reproducer.cpp +++ b/llvm/tools/dsymutil/Reproducer.cpp @@ -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() { -- 2.7.4