For PR950:
[oota-llvm.git] / tools / llvm-upgrade / llvm-upgrade.cpp
1 //===--- llvm-upgrade.cpp - The LLVM Assembly Upgrader --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This utility will upgrade LLVM 1.9 Assembly to 2.0 format. It may be 
11 //  invoked as a filter, like this:
12 //    llvm-1.9/bin/llvm-dis < 1.9.bc | llvm-upgrade | llvm-as > 2.0.bc
13 //  
14 //  or, you can directly upgrade, like this:
15 //    llvm-upgrade -o 2.0.ll < 1.9.ll
16 //  
17 //  llvm-upgrade won't overwrite files by default. Use -f to force it to
18 //  overwrite the output file.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "ParserInternals.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/Support/SystemUtils.h"
27 #include "llvm/System/Signals.h"
28 #include <fstream>
29 #include <iostream>
30 #include <memory>
31 using namespace llvm;
32
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
36 static cl::opt<std::string>
37 OutputFilename("o", cl::desc("Override output filename"),
38                cl::value_desc("filename"), cl::init("-"));
39
40 static cl::opt<bool>
41 Force("f", cl::desc("Overwrite output files"), cl::init(false));
42
43 static cl::opt<bool>
44 AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
45          cl::init(false));
46
47 static cl::opt<bool>
48 Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
49     cl::init(false));
50
51 int main(int argc, char **argv) {
52   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
53   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
54   sys::PrintStackTraceOnErrorSignal();
55
56   int exitCode = 0;
57   std::ostream *Out = 0;
58   std::istream *In = 0;
59   try {
60     if (OutputFilename != "") {   // Specified an output filename?
61       if (OutputFilename != "-") {  // Not stdout?
62         if (!Force && std::ifstream(OutputFilename.c_str())) {
63           // If force is not specified, make sure not to overwrite a file!
64           cerr << argv[0] << ": error opening '" << OutputFilename
65                << "': file exists!\n"
66                << "Use -f command line argument to force output\n";
67           return 1;
68         }
69         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
70                                 std::ios::trunc);
71       } else {                      // Specified stdout
72         Out = &std::cout;
73       }
74     } else {
75       if (InputFilename == "-") {
76         OutputFilename = "-";
77         Out = &std::cout;
78       } else {
79         std::string IFN = InputFilename;
80         int Len = IFN.length();
81         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
82           // Source ends in .ll
83           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
84         } else {
85           OutputFilename = IFN;   // Append to it
86         }
87         OutputFilename += ".llu";
88
89         if (!Force && std::ifstream(OutputFilename.c_str())) {
90           // If force is not specified, make sure not to overwrite a file!
91           cerr << argv[0] << ": error opening '" << OutputFilename
92                << "': file exists!\n"
93                << "Use -f command line argument to force output\n";
94           return 1;
95         }
96
97         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
98                                 std::ios::trunc);
99         // Make sure that the Out file gets unlinked from the disk if we get a
100         // SIGINT
101         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
102       }
103     }
104
105     if (InputFilename == "-") {
106       In = &std::cin;
107       InputFilename = "<stdin>";
108     } else {
109       In = new std::ifstream(InputFilename.c_str());
110     }
111
112     if (!Out->good()) {
113       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
114       return 1;
115     }
116
117     if (!In->good()) {
118       cerr << argv[0] << ": error opening " << InputFilename << "!\n";
119       return 1;
120     }
121
122     UpgradeAssembly(InputFilename, *In, *Out, Debug, AddAttrs);
123
124   } catch (const std::string& caught_message) {
125     cerr << argv[0] << ": " << caught_message << "\n";
126     exitCode = 1;
127   } catch (...) {
128     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
129     exitCode = 1;
130   }
131
132   if (Out != &std::cout) delete Out;
133   return exitCode;
134 }
135