blob: 702e13a4145cb846813e5825269159d37a1a3ffe [file] [log] [blame]
John McCall8f0e8d22011-06-15 23:25:17 +00001//===-- arcmt-test.cpp - ARC Migration Tool testbed -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/ARCMigrate/ARCMT.h"
11#include "clang/Frontend/ASTUnit.h"
12#include "clang/Frontend/TextDiagnosticPrinter.h"
13#include "clang/Frontend/VerifyDiagnosticsClient.h"
14#include "clang/Frontend/Utils.h"
15#include "clang/Lex/Preprocessor.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/Signals.h"
18
19using namespace clang;
20using namespace arcmt;
21
22static llvm::cl::opt<bool>
23CheckOnly("check-only",
24 llvm::cl::desc("Just check for issues that need to be handled manually"));
25
26//static llvm::cl::opt<bool>
27//TestResultForARC("test-result",
28//llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
29
30static llvm::cl::opt<bool>
31OutputTransformations("output-transformations",
32 llvm::cl::desc("Print the source transformations"));
33
34static llvm::cl::opt<bool>
35VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
36
37static llvm::cl::opt<bool>
38VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
39
40static llvm::cl::extrahelp extraHelp(
41 "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
42
43// This function isn't referenced outside its translation unit, but it
44// can't use the "static" keyword because its address is used for
45// GetMainExecutable (since some platforms don't support taking the
46// address of main, and some platforms can't implement GetMainExecutable
47// without being given the address of a function in the main executable).
48llvm::sys::Path GetExecutablePath(const char *Argv0) {
49 // This just needs to be some symbol in the binary; C++ doesn't
50 // allow taking the address of ::main however.
51 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
52 return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
53}
54
55static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
56 llvm::raw_ostream &OS);
57static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
58 llvm::raw_ostream &OS);
59
60namespace {
61
62class PrintTransforms : public MigrationProcess::RewriteListener {
63 ASTContext *Ctx;
64 llvm::raw_ostream &OS;
65
66public:
67 PrintTransforms(llvm::raw_ostream &OS)
68 : Ctx(0), OS(OS) { }
69
70 virtual void start(ASTContext &ctx) { Ctx = &ctx; }
71 virtual void finish() { Ctx = 0; }
72
73 virtual void insert(SourceLocation loc, llvm::StringRef text) {
74 assert(Ctx);
75 OS << "Insert: ";
76 printSourceLocation(loc, *Ctx, OS);
77 OS << " \"" << text << "\"\n";
78 }
79
80 virtual void remove(CharSourceRange range) {
81 assert(Ctx);
82 OS << "Remove: ";
83 printSourceRange(range, *Ctx, OS);
84 OS << '\n';
85 }
86};
87
88} // anonymous namespace
89
90static bool checkForMigration(llvm::StringRef resourcesPath,
91 llvm::ArrayRef<const char *> Args) {
92 DiagnosticClient *DiagClient =
93 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
94 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
95 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID, DiagClient));
96 // Chain in -verify checker, if requested.
97 VerifyDiagnosticsClient *verifyDiag = 0;
98 if (VerifyDiags) {
99 verifyDiag = new VerifyDiagnosticsClient(*Diags, Diags->takeClient());
100 Diags->setClient(verifyDiag);
101 }
102
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000103 CompilerInvocation CI;
104 CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags);
John McCall8f0e8d22011-06-15 23:25:17 +0000105
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000106 if (CI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000107 llvm::errs() << "error: no input files\n";
108 return true;
109 }
110
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000111 if (!CI.getLangOpts().ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000112 return false;
113
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000114 arcmt::checkForManualIssues(CI,
115 CI.getFrontendOpts().Inputs[0].second,
116 CI.getFrontendOpts().Inputs[0].first,
117 Diags->getClient());
118 return Diags->getClient()->getNumErrors() > 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000119}
120
121static void printResult(FileRemapper &remapper, llvm::raw_ostream &OS) {
122 CompilerInvocation CI;
123 remapper.applyMappings(CI);
124 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
125 // The changed files will be in memory buffers, print them.
126 for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
127 const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
128 OS << mem->getBuffer();
129 }
130}
131
132static bool performTransformations(llvm::StringRef resourcesPath,
133 llvm::ArrayRef<const char *> Args) {
134 // Check first.
135 if (checkForMigration(resourcesPath, Args))
136 return true;
137
138 DiagnosticClient *DiagClient =
139 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
140 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
141 llvm::IntrusiveRefCntPtr<Diagnostic> TopDiags(new Diagnostic(DiagID, DiagClient));
142
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000143 CompilerInvocation origCI;
144 CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
145 *TopDiags);
John McCall8f0e8d22011-06-15 23:25:17 +0000146
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000147 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000148 llvm::errs() << "error: no input files\n";
149 return true;
150 }
151
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000152 if (!origCI.getLangOpts().ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000153 return false;
154
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000155 MigrationProcess migration(origCI, DiagClient);
John McCall8f0e8d22011-06-15 23:25:17 +0000156
157 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
158 assert(!transforms.empty());
159
160 llvm::OwningPtr<PrintTransforms> transformPrinter;
161 if (OutputTransformations)
162 transformPrinter.reset(new PrintTransforms(llvm::outs()));
163
164 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
165 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
166 if (err) return true;
167
168 if (VerboseOpt) {
169 if (i == e-1)
170 llvm::errs() << "\n##### FINAL RESULT #####\n";
171 else
172 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
173 printResult(migration.getRemapper(), llvm::errs());
174 llvm::errs() << "\n##########################\n\n";
175 }
176 }
177
178 if (!OutputTransformations)
179 printResult(migration.getRemapper(), llvm::outs());
180
181 // FIXME: TestResultForARC
182
183 return false;
184}
185
186//===----------------------------------------------------------------------===//
187// Misc. functions.
188//===----------------------------------------------------------------------===//
189
190static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
191 llvm::raw_ostream &OS) {
192 SourceManager &SM = Ctx.getSourceManager();
193 PresumedLoc PL = SM.getPresumedLoc(loc);
194
195 OS << llvm::sys::path::filename(PL.getFilename());
196 OS << ":" << PL.getLine() << ":"
197 << PL.getColumn();
198}
199
200static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
201 llvm::raw_ostream &OS) {
202 SourceManager &SM = Ctx.getSourceManager();
203 const LangOptions &langOpts = Ctx.getLangOptions();
204
205 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
206
207 OS << llvm::sys::path::filename(PL.getFilename());
208 OS << " [" << PL.getLine() << ":"
209 << PL.getColumn();
210 OS << " - ";
211
212 SourceLocation end = range.getEnd();
213 PL = SM.getPresumedLoc(end);
214
215 unsigned endCol = PL.getColumn() - 1;
216 if (!range.isTokenRange())
217 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
218 OS << PL.getLine() << ":" << endCol << "]";
219}
220
221//===----------------------------------------------------------------------===//
222// Command line processing.
223//===----------------------------------------------------------------------===//
224
225int main(int argc, const char **argv) {
226 using llvm::StringRef;
227 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
228 llvm::sys::PrintStackTraceOnErrorSignal();
229
230 std::string
231 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
232
233 int optargc = 0;
234 for (; optargc != argc; ++optargc) {
235 if (StringRef(argv[optargc]) == "--args")
236 break;
237 }
238 llvm::cl::ParseCommandLineOptions(optargc, const_cast<char **>(argv), "arcmt-test");
239
240 if (optargc == argc) {
241 llvm::cl::PrintHelpMessage();
242 return 1;
243 }
244
245 llvm::ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
246
247 if (CheckOnly)
248 return checkForMigration(resourcesPath, Args);
249
250 return performTransformations(resourcesPath, Args);
251}