Skip to content

Commit

Permalink
Convert another use of createUniqueFile to TempFile::create.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318427 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Nov 16, 2017
1 parent 69def8d commit 324f141
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
5 changes: 5 additions & 0 deletions tools/bugpoint/BugDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ namespace llvm {
Triple TargetTriple;
}

DiscardTemp::~DiscardTemp() {
if (Error E = File.discard())
errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
}

// Anonymous namespace to define command line options for debugging.
//
namespace {
Expand Down
6 changes: 6 additions & 0 deletions tools/bugpoint/BugDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "llvm/IR/ValueMap.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include <memory>
#include <string>
Expand Down Expand Up @@ -278,6 +279,11 @@ class BugDriver {
Error initializeExecutionEnvironment();
};

struct DiscardTemp {
sys::fs::TempFile &File;
~DiscardTemp();
};

/// Given a bitcode or assembly input filename, parse and return it, or return
/// null if not possible.
///
Expand Down
28 changes: 11 additions & 17 deletions tools/bugpoint/ExtractFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,48 +373,42 @@ llvm::SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
std::unique_ptr<Module>
BugDriver::extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
Module *M) {
SmallString<128> Filename;
int FD;
std::error_code EC = sys::fs::createUniqueFile(
OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
if (EC) {
auto Temp = sys::fs::TempFile::create(OutputPrefix + "-extractblocks%%%%%%%");
if (!Temp) {
outs() << "*** Basic Block extraction failed!\n";
errs() << "Error creating temporary file: " << EC.message() << "\n";
errs() << "Error creating temporary file: " << toString(Temp.takeError())
<< "\n";
EmitProgressBitcode(M, "basicblockextractfail", true);
return nullptr;
}
sys::RemoveFileOnSignal(Filename);
DiscardTemp Discard{*Temp};

ToolOutputFile BlocksToNotExtractFile(Filename.c_str(), FD);
raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false);
for (std::vector<BasicBlock *>::const_iterator I = BBs.begin(), E = BBs.end();
I != E; ++I) {
BasicBlock *BB = *I;
// If the BB doesn't have a name, give it one so we have something to key
// off of.
if (!BB->hasName())
BB->setName("tmpbb");
BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
<< BB->getName() << "\n";
OS << BB->getParent()->getName() << " " << BB->getName() << "\n";
}
BlocksToNotExtractFile.os().close();
if (BlocksToNotExtractFile.os().has_error()) {
OS.flush();
if (OS.has_error()) {
errs() << "Error writing list of blocks to not extract\n";
EmitProgressBitcode(M, "basicblockextractfail", true);
BlocksToNotExtractFile.os().clear_error();
OS.clear_error();
return nullptr;
}
BlocksToNotExtractFile.keep();

std::string uniqueFN = "--extract-blocks-file=";
uniqueFN += Filename.str();
uniqueFN += Temp->TmpName;
const char *ExtraArg = uniqueFN.c_str();

std::vector<std::string> PI;
PI.push_back("extract-blocks");
std::unique_ptr<Module> Ret = runPassesOn(M, PI, 1, &ExtraArg);

sys::fs::remove(Filename.c_str());

if (!Ret) {
outs() << "*** Basic Block extraction failed, please report a bug!\n";
EmitProgressBitcode(M, "basicblockextractfail", true);
Expand Down
5 changes: 0 additions & 5 deletions tools/bugpoint/OptimizerDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ static cl::list<std::string> OptArgs("opt-args", cl::Positional,
cl::desc("<opt arguments>..."),
cl::ZeroOrMore, cl::PositionalEatsArgs);

struct DiscardTemp {
sys::fs::TempFile &File;
~DiscardTemp() { consumeError(File.discard()); }
};

/// runPasses - Run the specified passes on Program, outputting a bitcode file
/// and writing the filename into OutputFile if successful. If the
/// optimizations fail for some reason (optimizer crashes), return true,
Expand Down

0 comments on commit 324f141

Please sign in to comment.