Changed llvm_ostream et all to OStream. llvm_cerr, llvm_cout, llvm_null, are
[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"));
42
43 static cl::opt<bool>
44 Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
45     cl::init(false));
46
47 int main(int argc, char **argv) {
48   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
49   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
50   sys::PrintStackTraceOnErrorSignal();
51
52   int exitCode = 0;
53   std::ostream *Out = 0;
54   std::istream *In = 0;
55   try {
56     if (OutputFilename != "") {   // Specified an output filename?
57       if (OutputFilename != "-") {  // Not stdout?
58         if (!Force && std::ifstream(OutputFilename.c_str())) {
59           // If force is not specified, make sure not to overwrite a file!
60           cerr << argv[0] << ": error opening '" << OutputFilename
61                << "': file exists!\n"
62                << "Use -f command line argument to force output\n";
63           return 1;
64         }
65         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
66                                 std::ios::trunc);
67       } else {                      // Specified stdout
68         Out = &std::cout;
69       }
70     } else {
71       if (InputFilename == "-") {
72         OutputFilename = "-";
73         Out = &std::cout;
74       } else {
75         std::string IFN = InputFilename;
76         int Len = IFN.length();
77         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
78           // Source ends in .ll
79           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
80         } else {
81           OutputFilename = IFN;   // Append to it
82         }
83         OutputFilename += ".llu";
84
85         if (!Force && std::ifstream(OutputFilename.c_str())) {
86           // If force is not specified, make sure not to overwrite a file!
87           cerr << argv[0] << ": error opening '" << OutputFilename
88                << "': file exists!\n"
89                << "Use -f command line argument to force output\n";
90           return 1;
91         }
92
93         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
94                                 std::ios::trunc);
95         // Make sure that the Out file gets unlinked from the disk if we get a
96         // SIGINT
97         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
98       }
99     }
100
101     if (InputFilename == "-") {
102       In = &std::cin;
103       InputFilename = "<stdin>";
104     } else {
105       In = new std::ifstream(InputFilename.c_str());
106     }
107
108     if (!Out->good()) {
109       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
110       return 1;
111     }
112
113     if (!In->good()) {
114       cerr << argv[0] << ": error opening " << InputFilename << "!\n";
115       return 1;
116     }
117
118     UpgradeAssembly(InputFilename, *In, *Out, Debug);
119
120   } catch (const std::string& caught_message) {
121     cerr << argv[0] << ": " << caught_message << "\n";
122     exitCode = 1;
123   } catch (...) {
124     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
125     exitCode = 1;
126   }
127
128   if (Out != &std::cout) delete Out;
129   return exitCode;
130 }
131