First version of llvm-upgrade that can correctly upgrade a large test
[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/Streams.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include "llvm/System/Signals.h"
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
30 using namespace llvm;
31
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
34
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37                cl::value_desc("filename"), cl::init("-"));
38
39 static cl::opt<bool>
40 Force("f", cl::desc("Overwrite output files"));
41
42 static cl::opt<bool>
43 Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
44     cl::init(false));
45
46 int main(int argc, char **argv) {
47   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
48   sys::PrintStackTraceOnErrorSignal();
49
50   int exitCode = 0;
51   std::ostream *Out = 0;
52   std::istream *In = 0;
53   try {
54     if (OutputFilename != "") {   // Specified an output filename?
55       if (OutputFilename != "-") {  // Not stdout?
56         if (!Force && std::ifstream(OutputFilename.c_str())) {
57           // If force is not specified, make sure not to overwrite a file!
58           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
59                     << "': file exists!\n"
60                     << "Use -f command line argument to force output\n";
61           return 1;
62         }
63         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
64                                 std::ios::trunc);
65       } else {                      // Specified stdout
66         Out = &std::cout;
67       }
68     } else {
69       if (InputFilename == "-") {
70         OutputFilename = "-";
71         Out = &std::cout;
72       } else {
73         std::string IFN = InputFilename;
74         int Len = IFN.length();
75         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
76           // Source ends in .ll
77           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
78         } else {
79           OutputFilename = IFN;   // Append to it
80         }
81         OutputFilename += ".llu";
82
83         if (!Force && std::ifstream(OutputFilename.c_str())) {
84           // If force is not specified, make sure not to overwrite a file!
85           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
86                     << "': file exists!\n"
87                     << "Use -f command line argument to force output\n";
88           return 1;
89         }
90
91         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
92                                 std::ios::trunc);
93         // Make sure that the Out file gets unlinked from the disk if we get a
94         // SIGINT
95         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
96       }
97     }
98
99     if (InputFilename == "-") {
100       In = &std::cin;
101       InputFilename = "<stdin>";
102     } else {
103       In = new std::ifstream(InputFilename.c_str());
104     }
105
106     if (!Out->good()) {
107       llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
108       return 1;
109     }
110
111     if (!In->good()) {
112       llvm_cerr << argv[0] << ": error opening " << InputFilename << "!\n";
113       return 1;
114     }
115
116     UpgradeAssembly(InputFilename, *In, *Out, Debug);
117
118   } catch (const std::string& caught_message) {
119     llvm_cerr << argv[0] << ": " << caught_message << "\n";
120     exitCode = 1;
121   } catch (...) {
122     llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
123     exitCode = 1;
124   }
125
126   if (Out != &std::cout) delete Out;
127   return exitCode;
128 }
129