#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/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
-#include <iostream>
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Host.h"
#include <memory>
using namespace llvm;
+namespace llvm {
+ Triple TargetTriple;
+}
+
// Anonymous namespace to define command line options for debugging.
//
namespace {
}
BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
- unsigned timeout, unsigned memlimit)
- : ToolName(toolname), ReferenceOutputFile(OutputFile),
- Program(0), Interpreter(0), cbe(0), gcc(0), run_as_child(as_child),
- run_find_bugs(find_bugs), Timeout(timeout), MemoryLimit(memlimit) {}
+ unsigned timeout, unsigned memlimit,
+ LLVMContext& ctxt)
+ : Context(ctxt), 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) {}
/// ParseInputFile - Given a bitcode or assembly input filename, parse and
/// return it, or return null if not possible.
///
-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))) {
- std::cerr << "bugpoint: " << Err.getMessage() << "\n";
- Result = 0;
+Module *llvm::ParseInputFile(const std::string &Filename,
+ LLVMContext& Ctxt) {
+ SMDiagnostic Err;
+ Module *Result = ParseIRFile(Filename, Err, Ctxt);
+ if (!Result)
+ Err.Print("bugpoint", errs());
+
+ // If we don't have an override triple, use the first one to configure
+ // bugpoint, or use the host triple if none provided.
+ if (Result) {
+ if (TargetTriple.getTriple().empty()) {
+ Triple TheTriple(Result->getTargetTriple());
+
+ if (TheTriple.getTriple().empty())
+ TheTriple.setTriple(sys::getHostTriple());
+
+ TargetTriple.setTriple(TheTriple.getTriple());
+ }
+
+ Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
}
-
return Result;
}
try {
// Load the first input file.
- Program = ParseInputFile(Filenames[0]);
+ Program = ParseInputFile(Filenames[0], Context);
if (Program == 0) return true;
if (!run_as_child)
- std::cout << "Read input file : '" << Filenames[0] << "'\n";
+ outs() << "Read input file : '" << Filenames[0] << "'\n";
for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
- std::auto_ptr<Module> M(ParseInputFile(Filenames[i]));
+ std::auto_ptr<Module> M(ParseInputFile(Filenames[i], Context));
if (M.get() == 0) return true;
if (!run_as_child)
- std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
+ outs() << "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';
+ errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
+ << ErrorMessage << '\n';
return true;
}
}
} catch (const std::string &Error) {
- std::cerr << ToolName << ": error reading input '" << Error << "'\n";
+ errs() << ToolName << ": error reading input '" << Error << "'\n";
return true;
}
if (!run_as_child)
- std::cout << "*** All input ok\n";
+ outs() << "*** All input ok\n";
// All input files read successfully!
return false;
// file, then we know the compiler didn't crash, so try to diagnose a
// miscompilation.
if (!PassesToRun.empty()) {
- std::cout << "Running selected passes on program to test for crash: ";
+ outs() << "Running selected passes on program to test for crash: ";
if (runPasses(PassesToRun))
return debugOptimizerCrash();
}
if (initializeExecutionEnvironment()) return true;
// Test to see if we have a code generator crash.
- std::cout << "Running the code generator to test for a crash: ";
+ outs() << "Running the code generator to test for a crash: ";
try {
compileProgram(Program);
- std::cout << '\n';
+ outs() << '\n';
} catch (ToolExecutionError &TEE) {
- std::cout << TEE.what();
+ outs() << TEE.what();
return debugCodeGeneratorCrash();
}
//
bool CreatedOutput = false;
if (ReferenceOutputFile.empty()) {
- std::cout << "Generating reference output from raw program: ";
+ outs() << "Generating reference output from raw program: ";
if(!createReferenceFile(Program)){
return debugCodeGeneratorCrash();
}
// Make sure the reference output file gets deleted on exit from this
// function, if appropriate.
sys::Path ROF(ReferenceOutputFile);
- FileRemover RemoverInstance(ROF, CreatedOutput);
+ FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
// Diff the output of the raw program against the reference output. If it
// matches, then we assume there is a miscompilation bug and try to
// diagnose it.
- std::cout << "*** Checking the code generator...\n";
+ outs() << "*** Checking the code generator...\n";
try {
if (!diffProgram()) {
- std::cout << "\n*** Debugging miscompilation!\n";
+ outs() << "\n*** Output matches: Debugging miscompilation!\n";
return debugMiscompilation();
}
} catch (ToolExecutionError &TEE) {
- std::cerr << TEE.what();
+ errs() << TEE.what();
return debugCodeGeneratorCrash();
}
- std::cout << "\n*** Input program does not match reference diff!\n";
- std::cout << "Debugging code generator problem!\n";
+ outs() << "\n*** Input program does not match reference diff!\n";
+ outs() << "Debugging code generator problem!\n";
try {
return debugCodeGenerator();
} catch (ToolExecutionError &TEE) {
- std::cerr << TEE.what();
+ errs() << TEE.what();
return debugCodeGeneratorCrash();
}
}
unsigned NumPrint = Funcs.size();
if (NumPrint > 10) NumPrint = 10;
for (unsigned i = 0; i != NumPrint; ++i)
- std::cout << " " << Funcs[i]->getName();
+ outs() << " " << Funcs[i]->getName();
if (NumPrint < Funcs.size())
- std::cout << "... <" << Funcs.size() << " total>";
- std::cout << std::flush;
+ outs() << "... <" << Funcs.size() << " total>";
+ outs().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();
+ outs() << " " << GVs[i]->getName();
if (NumPrint < GVs.size())
- std::cout << "... <" << GVs.size() << " total>";
- std::cout << std::flush;
+ outs() << "... <" << GVs.size() << " total>";
+ outs().flush();
}