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