Skip to content

Commit c138228

Browse files
committed
Convert another use of createUniqueFile to TempFile::create.
This one requires a new small feature in TempFile: the ability to keep the temporary file with the temporary name. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318458 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 681627d commit c138228

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

include/llvm/Support/FileSystem.h

+3
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@ class TempFile {
723723
// Keep this with the given name.
724724
Error keep(const Twine &Name);
725725

726+
// Keep this with the temporary name.
727+
Error keep();
728+
726729
// Delete the file.
727730
Error discard();
728731

lib/Support/Path.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,22 @@ Error TempFile::keep(const Twine &Name) {
805805
return errorCodeToError(RenameEC);
806806
}
807807

808+
Error TempFile::keep() {
809+
assert(!Done);
810+
Done = true;
811+
812+
sys::DontRemoveFileOnSignal(TmpName);
813+
TmpName = "";
814+
815+
if (close(FD) == -1) {
816+
std::error_code EC(errno, std::generic_category());
817+
return errorCodeToError(EC);
818+
}
819+
FD = -1;
820+
821+
return Error::success();
822+
}
823+
808824
Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
809825
int FD;
810826
SmallString<128> ResultPath;

tools/bugpoint/BugDriver.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Triple TargetTriple;
3333
}
3434

3535
DiscardTemp::~DiscardTemp() {
36+
if (SaveTemps) {
37+
if (Error E = File.keep())
38+
errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n';
39+
return;
40+
}
3641
if (Error E = File.discard())
3742
errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
3843
}

tools/bugpoint/BugDriver.h

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class BugDriver {
271271
bool writeProgramToFile(const std::string &Filename, const Module *M) const;
272272
bool writeProgramToFile(const std::string &Filename, int FD,
273273
const Module *M) const;
274+
bool writeProgramToFile(int FD, const Module *M) const;
274275

275276
private:
276277
/// initializeExecutionEnvironment - This method is used to set up the

tools/bugpoint/ExecutionDriver.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,23 @@ Error BugDriver::initializeExecutionEnvironment() {
271271
///
272272
Error BugDriver::compileProgram(Module *M) const {
273273
// Emit the program to a bitcode file...
274-
SmallString<128> BitcodeFile;
275-
int BitcodeFD;
276-
std::error_code EC = sys::fs::createUniqueFile(
277-
OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
278-
if (EC) {
279-
errs() << ToolName << ": Error making unique filename: " << EC.message()
274+
auto Temp =
275+
sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
276+
if (!Temp) {
277+
errs() << ToolName
278+
<< ": Error making unique filename: " << toString(Temp.takeError())
280279
<< "\n";
281280
exit(1);
282281
}
283-
if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
284-
errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
282+
DiscardTemp Discard{*Temp};
283+
if (writeProgramToFile(Temp->FD, M)) {
284+
errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
285285
<< "'!\n";
286286
exit(1);
287287
}
288288

289-
// Remove the temporary bitcode file when we are done.
290-
FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
291-
292289
// Actually compile the program!
293-
return Interpreter->compileProgram(BitcodeFile.str(), Timeout, MemoryLimit);
290+
return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
294291
}
295292

296293
/// executeProgram - This method runs "Program", capturing the output of the

tools/bugpoint/OptimizerDriver.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
7474
return writeProgramToFileAux(Out, M);
7575
}
7676

77+
bool BugDriver::writeProgramToFile(int FD, const Module *M) const {
78+
raw_fd_ostream OS(FD, /*shouldClose*/ false);
79+
WriteBitcodeToFile(M, OS, PreserveBitcodeUseListOrder);
80+
OS.flush();
81+
if (!OS.has_error())
82+
return false;
83+
OS.clear_error();
84+
return true;
85+
}
86+
7787
bool BugDriver::writeProgramToFile(const std::string &Filename,
7888
const Module *M) const {
7989
std::error_code EC;

0 commit comments

Comments
 (0)