Need <iostream> for the time being.
[oota-llvm.git] / tools / bugpoint / bugpoint.cpp
1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is an automated compiler debugger tool.  It is used to narrow
11 // down miscompilations and crash problems to a specific pass in the compiler,
12 // and the specific Module or Function input that is causing the problem.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "BugDriver.h"
17 #include "ToolRunner.h"
18 #include "llvm/LinkAllPasses.h"
19 #include "llvm/Support/PassNameParser.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/PluginLoader.h"
22 #include "llvm/System/Process.h"
23 #include "llvm/System/Signals.h"
24 #include "llvm/LinkAllVMCore.h"
25 #include <iostream>
26 using namespace llvm;
27
28 // AsChild - Specifies that this invocation of bugpoint is being generated
29 // from a parent process. It is not intended to be used by users so the 
30 // option is hidden.
31 static cl::opt<bool> 
32 AsChild("as-child", cl::desc("Run bugpoint as child process"), 
33         cl::ReallyHidden);
34           
35 static cl::opt<bool> 
36 FindBugs("find-bugs", cl::desc("Run many different optimization sequences"
37                                "on program to find bugs"), cl::init(false));
38
39 static cl::list<std::string>
40 InputFilenames(cl::Positional, cl::OneOrMore,
41                cl::desc("<input llvm ll/bc files>"));
42
43 static cl::opt<unsigned>
44 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
45              cl::desc("Number of seconds program is allowed to run before it "
46                       "is killed (default is 300s), 0 disables timeout"));
47
48 // The AnalysesList is automatically populated with registered Passes by the
49 // PassNameParser.
50 //
51 static cl::list<const PassInfo*, bool, PassNameParser>
52 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
53
54 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
55 bool llvm::BugpointIsInterrupted = false;
56
57 static void BugpointInterruptFunction() {
58   BugpointIsInterrupted = true;
59 }
60
61 int main(int argc, char **argv) {
62   cl::ParseCommandLineOptions(argc, argv,
63                               " LLVM automatic testcase reducer. See\nhttp://"
64                               "llvm.org/docs/CommandGuide/bugpoint.html"
65                               " for more information.\n");
66   sys::PrintStackTraceOnErrorSignal();
67   sys::SetInterruptFunction(BugpointInterruptFunction);
68   
69   BugDriver D(argv[0],AsChild,FindBugs,TimeoutValue);
70   if (D.addSources(InputFilenames)) return 1;
71   D.addPasses(PassList.begin(), PassList.end());
72
73   // Bugpoint has the ability of generating a plethora of core files, so to
74   // avoid filling up the disk, we prevent it
75   sys::Process::PreventCoreFiles();
76
77   try {
78     return D.run();
79   } catch (ToolExecutionError &TEE) {
80     std::cerr << "Tool execution error: " << TEE.what() << '\n';
81   } catch (const std::string& msg) {
82     std::cerr << argv[0] << ": " << msg << "\n";
83   } catch (...) {
84     std::cerr << "Whoops, an exception leaked out of bugpoint.  "
85               << "This is a bug in bugpoint!\n";
86   }
87   return 1;
88 }