Oops. Pass -lgcc _only_ on ARM, not on everything except ARM.
[oota-llvm.git] / tools / bugpoint / BugDriver.cpp
index 340522a4437b034aaf444462fcdd8515c35d9c3b..abf5d8ef7211b219ab5ccf4dfb695153f68f92d1 100644 (file)
 #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 "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
-#include <iostream>
+#include "llvm/System/Host.h"
 #include <memory>
 using namespace llvm;
 
+namespace llvm {
+  Triple TargetTriple;
+}
+
 // Anonymous namespace to define command line options for debugging.
 //
 namespace {
@@ -64,7 +68,8 @@ std::string llvm::getPassesString(const std::vector<const PassInfo*> &Passes) {
 }
 
 BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
-                     unsigned timeout, unsigned memlimit, LLVMContext* ctxt)
+                     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), 
@@ -74,18 +79,27 @@ BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
 /// 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, LLVMContext* Ctxt) {
-  std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(Filename));
-  Module *Result = 0;
-  if (Buffer.get())
-    Result = ParseBitcodeFile(Buffer.get(), Ctxt);
-  
-  ParseError Err;
-  if (!Result && !(Result = ParseAssemblyFile(Filename, Err, Ctxt))) {
-    Err.PrintError("bugpoint", errs()); 
-    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;
 }
 
@@ -104,28 +118,28 @@ bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
     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], 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;
@@ -159,7 +173,7 @@ bool BugDriver::run() {
   // 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();
   }
@@ -168,12 +182,12 @@ bool BugDriver::run() {
   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();
   }
 
@@ -184,7 +198,7 @@ bool BugDriver::run() {
   //
   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();
     }
@@ -194,28 +208,28 @@ bool BugDriver::run() {
   // 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();
   }
 }
@@ -224,18 +238,18 @@ 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();
+    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();
 }