#include "BugDriver.h"
#include "ToolRunner.h"
#include "llvm/Linker.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Assembly/Parser.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>
#include <memory>
using namespace llvm;
namespace {
cl::opt<std::string>
OutputFile("output", cl::desc("Specify a reference program output "
"(for miscompilation detection)"));
}
void BugDriver::setNewProgram(Module *M) {
delete Program;
Program = M;
}
std::string llvm::getPassesString(const std::vector<const PassInfo*> &Passes) {
std::string Result;
for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
if (i) Result += " ";
Result += "-";
Result += Passes[i]->getPassArgument();
}
return Result;
}
BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
unsigned timeout, unsigned memlimit)
: ToolName(toolname), ReferenceOutputFile(OutputFile),
Program(0), Interpreter(0), SafeInterpreter(0), gcc(0),
run_as_child(as_child),
run_find_bugs(find_bugs), Timeout(timeout), MemoryLimit(memlimit) {}
Module *llvm::ParseInputFile(const std::string &Filename) {
std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(Filename));
Module *Result = 0;
if (Buffer.get())
Result = ParseBitcodeFile(Buffer.get());
ParseError Err;
if (!Result && !(Result = ParseAssemblyFile(Filename, Err))) {
Err.PrintError("bugpoint", errs());
Result = 0;
}
return Result;
}
bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
assert(Program == 0 && "Cannot call addSources multiple times!");
assert(!Filenames.empty() && "Must specify at least on input filename!");
try {
Program = ParseInputFile(Filenames[0]);
if (Program == 0) return true;
if (!run_as_child)
std::cout << "Read input file : '" << Filenames[0] << "'\n";
for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
std::auto_ptr<Module> M(ParseInputFile(Filenames[i]));
if (M.get() == 0) return true;
if (!run_as_child)
std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
std::string ErrorMessage;
if (Linker::LinkModules(Program, M.get(), &ErrorMessage)) {
std::cerr << ToolName << ": error linking in '" << Filenames[i] << "': "
<< ErrorMessage << '\n';
return true;
}
}
} catch (const std::string &Error) {
std::cerr << ToolName << ": error reading input '" << Error << "'\n";
return true;
}
if (!run_as_child)
std::cout << "*** All input ok\n";
return false;
}
bool BugDriver::run() {
if (run_as_child) {
return runPassesAsChild(PassesToRun);
}
if (run_find_bugs) {
return runManyPasses(PassesToRun);
}
if (!PassesToRun.empty()) {
std::cout << "Running selected passes on program to test for crash: ";
if (runPasses(PassesToRun))
return debugOptimizerCrash();
}
if (initializeExecutionEnvironment()) return true;
std::cout << "Running the code generator to test for a crash: ";
try {
compileProgram(Program);
std::cout << '\n';
} catch (ToolExecutionError &TEE) {
std::cout << TEE.what();
return debugCodeGeneratorCrash();
}
bool CreatedOutput = false;
if (ReferenceOutputFile.empty()) {
std::cout << "Generating reference output from raw program: ";
if(!createReferenceFile(Program)){
return debugCodeGeneratorCrash();
}
CreatedOutput = true;
}
sys::Path ROF(ReferenceOutputFile);
FileRemover RemoverInstance(ROF, CreatedOutput);
std::cout << "*** Checking the code generator...\n";
try {
if (!diffProgram()) {
std::cout << "\n*** Debugging miscompilation!\n";
return debugMiscompilation();
}
} catch (ToolExecutionError &TEE) {
std::cerr << TEE.what();
return debugCodeGeneratorCrash();
}
std::cout << "\n*** Input program does not match reference diff!\n";
std::cout << "Debugging code generator problem!\n";
try {
return debugCodeGenerator();
} catch (ToolExecutionError &TEE) {
std::cerr << TEE.what();
return debugCodeGeneratorCrash();
}
}
void llvm::PrintFunctionList(const std::vector<Function*> &Funcs) {
unsigned NumPrint = Funcs.size();
if (NumPrint > 10) NumPrint = 10;
for (unsigned i = 0; i != NumPrint; ++i)
std::cout << " " << Funcs[i]->getName();
if (NumPrint < Funcs.size())
std::cout << "... <" << Funcs.size() << " total>";
std::cout << std::flush;
}
void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs) {
unsigned NumPrint = GVs.size();
if (NumPrint > 10) NumPrint = 10;
for (unsigned i = 0; i != NumPrint; ++i)
std::cout << " " << GVs[i]->getName();
if (NumPrint < GVs.size())
std::cout << "... <" << GVs.size() << " total>";
std::cout << std::flush;
}