7991ab835e8635498da646e05cd5714871b31a53
[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 "llvm/Analysis/LinkAllAnalyses.h"
18 #include "llvm/Transforms/LinkAllPasses.h"
19 #include "llvm/Support/PassNameParser.h"
20 #include "llvm/Support/ToolRunner.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/PluginLoader.h"
23 #include "llvm/System/Process.h"
24 #include "llvm/System/Signals.h"
25 using namespace llvm;
26
27 // AsChild - Specifies that this invocation of bugpoint is being generated
28 // from a parent process. It is not intended to be used by users so the 
29 // option is hidden.
30 static cl::opt<bool> 
31   AsChild("as-child", cl::desc("Run bugpoint as child process"), 
32           cl::ReallyHidden);
33
34 static cl::list<std::string>
35 InputFilenames(cl::Positional, cl::OneOrMore,
36                cl::desc("<input llvm ll/bc files>"));
37
38 // The AnalysesList is automatically populated with registered Passes by the
39 // PassNameParser.
40 //
41 static cl::list<const PassInfo*, bool, PassNameParser>
42 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
43
44 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
45 bool llvm::BugpointIsInterrupted = false;
46
47 static void BugpointInterruptFunction() {
48   BugpointIsInterrupted = true;
49 }
50
51 int main(int argc, char **argv) {
52   cl::ParseCommandLineOptions(argc, argv,
53                               " LLVM automatic testcase reducer. See\nhttp://"
54                               "llvm.org/docs/CommandGuide/bugpoint.html"
55                               " for more information.\n");
56   sys::PrintStackTraceOnErrorSignal();
57   sys::SetInterruptFunction(BugpointInterruptFunction);
58   
59   BugDriver D(argv[0],AsChild);
60   if (D.addSources(InputFilenames)) return 1;
61   D.addPasses(PassList.begin(), PassList.end());
62
63   // Bugpoint has the ability of generating a plethora of core files, so to
64   // avoid filling up the disk, we prevent it
65   sys::Process::PreventCoreFiles();
66
67   try {
68     return D.run();
69   } catch (ToolExecutionError &TEE) {
70     std::cerr << "Tool execution error: " << TEE.what() << '\n';
71   } catch (const std::string& msg) {
72     std::cerr << argv[0] << ": " << msg << "\n";
73   } catch (...) {
74     std::cerr << "Whoops, an exception leaked out of bugpoint.  "
75               << "This is a bug in bugpoint!\n";
76   }
77   return 1;
78 }