blob: 881a058c2c4b6ee3c82b807604a02d782a3846cc [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"
David Blaikie621bc692011-09-26 00:38:03 +000013#include "clang/Frontend/VerifyDiagnosticConsumer.h"
John McCall8f0e8d22011-06-15 23:25:17 +000014#include "clang/Frontend/Utils.h"
15#include "clang/Lex/Preprocessor.h"
16#include "llvm/Support/MemoryBuffer.h"
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +000017#include "llvm/Support/FileSystem.h"
John McCall8f0e8d22011-06-15 23:25:17 +000018#include "llvm/Support/Signals.h"
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +000019#include "llvm/Support/system_error.h"
John McCall8f0e8d22011-06-15 23:25:17 +000020
21using namespace clang;
22using namespace arcmt;
23
24static llvm::cl::opt<bool>
25CheckOnly("check-only",
26 llvm::cl::desc("Just check for issues that need to be handled manually"));
27
28//static llvm::cl::opt<bool>
29//TestResultForARC("test-result",
30//llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
31
32static llvm::cl::opt<bool>
33OutputTransformations("output-transformations",
34 llvm::cl::desc("Print the source transformations"));
35
36static llvm::cl::opt<bool>
37VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
38
39static llvm::cl::opt<bool>
40VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
41
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +000042static llvm::cl::opt<bool>
43VerifyTransformedFiles("verify-transformed-files",
44llvm::cl::desc("Read pairs of file mappings (typically the output of "
45 "c-arcmt-test) and compare their contents with the filenames "
46 "provided in command-line"));
47
48static llvm::cl::opt<std::string>
49RemappingsFile("remappings-file",
50 llvm::cl::desc("Pairs of file mappings (typically the output of "
51 "c-arcmt-test)"));
52
53static llvm::cl::list<std::string>
54ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
55
John McCall8f0e8d22011-06-15 23:25:17 +000056static llvm::cl::extrahelp extraHelp(
57 "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
58
59// This function isn't referenced outside its translation unit, but it
60// can't use the "static" keyword because its address is used for
61// GetMainExecutable (since some platforms don't support taking the
62// address of main, and some platforms can't implement GetMainExecutable
63// without being given the address of a function in the main executable).
64llvm::sys::Path GetExecutablePath(const char *Argv0) {
65 // This just needs to be some symbol in the binary; C++ doesn't
66 // allow taking the address of ::main however.
67 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
68 return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
69}
70
71static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 raw_ostream &OS);
John McCall8f0e8d22011-06-15 23:25:17 +000073static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner5f9e2722011-07-23 10:55:15 +000074 raw_ostream &OS);
John McCall8f0e8d22011-06-15 23:25:17 +000075
76namespace {
77
78class PrintTransforms : public MigrationProcess::RewriteListener {
79 ASTContext *Ctx;
Chris Lattner5f9e2722011-07-23 10:55:15 +000080 raw_ostream &OS;
John McCall8f0e8d22011-06-15 23:25:17 +000081
82public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000083 PrintTransforms(raw_ostream &OS)
John McCall8f0e8d22011-06-15 23:25:17 +000084 : Ctx(0), OS(OS) { }
85
86 virtual void start(ASTContext &ctx) { Ctx = &ctx; }
87 virtual void finish() { Ctx = 0; }
88
Chris Lattner5f9e2722011-07-23 10:55:15 +000089 virtual void insert(SourceLocation loc, StringRef text) {
John McCall8f0e8d22011-06-15 23:25:17 +000090 assert(Ctx);
91 OS << "Insert: ";
92 printSourceLocation(loc, *Ctx, OS);
93 OS << " \"" << text << "\"\n";
94 }
95
96 virtual void remove(CharSourceRange range) {
97 assert(Ctx);
98 OS << "Remove: ";
99 printSourceRange(range, *Ctx, OS);
100 OS << '\n';
101 }
102};
103
104} // anonymous namespace
105
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106static bool checkForMigration(StringRef resourcesPath,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000107 ArrayRef<const char *> Args) {
David Blaikie78ad0b92011-09-25 23:39:51 +0000108 DiagnosticConsumer *DiagClient =
John McCall8f0e8d22011-06-15 23:25:17 +0000109 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000110 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
111 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
David Blaikied6471f72011-09-25 23:23:43 +0000112 new DiagnosticsEngine(DiagID, DiagClient));
John McCall8f0e8d22011-06-15 23:25:17 +0000113 // Chain in -verify checker, if requested.
David Blaikie621bc692011-09-26 00:38:03 +0000114 VerifyDiagnosticConsumer *verifyDiag = 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000115 if (VerifyDiags) {
David Blaikie621bc692011-09-26 00:38:03 +0000116 verifyDiag = new VerifyDiagnosticConsumer(*Diags);
John McCall8f0e8d22011-06-15 23:25:17 +0000117 Diags->setClient(verifyDiag);
118 }
119
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000120 CompilerInvocation CI;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000121 if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
122 return true;
John McCall8f0e8d22011-06-15 23:25:17 +0000123
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000124 if (CI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000125 llvm::errs() << "error: no input files\n";
126 return true;
127 }
128
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000129 if (!CI.getLangOpts()->ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000130 return false;
131
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000132 arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000133 Diags->getClient());
134 return Diags->getClient()->getNumErrors() > 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000135}
136
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137static void printResult(FileRemapper &remapper, raw_ostream &OS) {
John McCall8f0e8d22011-06-15 23:25:17 +0000138 CompilerInvocation CI;
139 remapper.applyMappings(CI);
140 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
141 // The changed files will be in memory buffers, print them.
142 for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
143 const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
144 OS << mem->getBuffer();
145 }
146}
147
Chris Lattner5f9e2722011-07-23 10:55:15 +0000148static bool performTransformations(StringRef resourcesPath,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000149 ArrayRef<const char *> Args) {
John McCall8f0e8d22011-06-15 23:25:17 +0000150 // Check first.
151 if (checkForMigration(resourcesPath, Args))
152 return true;
153
David Blaikie78ad0b92011-09-25 23:39:51 +0000154 DiagnosticConsumer *DiagClient =
John McCall8f0e8d22011-06-15 23:25:17 +0000155 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000156 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
157 IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
David Blaikied6471f72011-09-25 23:23:43 +0000158 new DiagnosticsEngine(DiagID, DiagClient));
John McCall8f0e8d22011-06-15 23:25:17 +0000159
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000160 CompilerInvocation origCI;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000161 if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
162 *TopDiags))
163 return true;
John McCall8f0e8d22011-06-15 23:25:17 +0000164
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000165 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000166 llvm::errs() << "error: no input files\n";
167 return true;
168 }
169
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000170 if (!origCI.getLangOpts()->ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000171 return false;
172
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000173 MigrationProcess migration(origCI, DiagClient);
John McCall8f0e8d22011-06-15 23:25:17 +0000174
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000175 std::vector<TransformFn>
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000176 transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
177 origCI.getMigratorOpts().NoFinalizeRemoval);
John McCall8f0e8d22011-06-15 23:25:17 +0000178 assert(!transforms.empty());
179
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000180 OwningPtr<PrintTransforms> transformPrinter;
John McCall8f0e8d22011-06-15 23:25:17 +0000181 if (OutputTransformations)
182 transformPrinter.reset(new PrintTransforms(llvm::outs()));
183
184 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
185 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
186 if (err) return true;
187
188 if (VerboseOpt) {
189 if (i == e-1)
190 llvm::errs() << "\n##### FINAL RESULT #####\n";
191 else
192 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
193 printResult(migration.getRemapper(), llvm::errs());
194 llvm::errs() << "\n##########################\n\n";
195 }
196 }
197
198 if (!OutputTransformations)
199 printResult(migration.getRemapper(), llvm::outs());
200
201 // FIXME: TestResultForARC
202
203 return false;
204}
205
Chris Lattner5f9e2722011-07-23 10:55:15 +0000206static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000207 using namespace llvm;
208
209 OwningPtr<MemoryBuffer> file1;
210 MemoryBuffer::getFile(fname1, file1);
211 if (!file1)
212 return false;
213
214 OwningPtr<MemoryBuffer> file2;
215 MemoryBuffer::getFile(fname2, file2);
216 if (!file2)
217 return false;
218
219 return file1->getBuffer() == file2->getBuffer();
220}
221
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000222static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000223 using namespace llvm;
224
225 assert(!resultFiles.empty());
226
227 std::map<StringRef, StringRef> resultMap;
228
229 for (ArrayRef<std::string>::iterator
230 I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
231 StringRef fname(*I);
232 if (!fname.endswith(".result")) {
233 errs() << "error: filename '" << fname
234 << "' does not have '.result' extension\n";
235 return true;
236 }
237 resultMap[sys::path::stem(fname)] = fname;
238 }
239
240 OwningPtr<MemoryBuffer> inputBuf;
241 if (RemappingsFile.empty())
242 MemoryBuffer::getSTDIN(inputBuf);
243 else
244 MemoryBuffer::getFile(RemappingsFile, inputBuf);
245 if (!inputBuf) {
246 errs() << "error: could not read remappings input\n";
247 return true;
248 }
249
250 SmallVector<StringRef, 8> strs;
251 inputBuf->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
252
253 if (strs.empty()) {
254 errs() << "error: no files to verify from stdin\n";
255 return true;
256 }
257 if (strs.size() % 2 != 0) {
258 errs() << "error: files to verify are not original/result pairs\n";
259 return true;
260 }
261
262 for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
263 StringRef inputOrigFname = strs[i];
264 StringRef inputResultFname = strs[i+1];
265
266 std::map<StringRef, StringRef>::iterator It;
267 It = resultMap.find(sys::path::filename(inputOrigFname));
268 if (It == resultMap.end()) {
269 errs() << "error: '" << inputOrigFname << "' is not in the list of "
270 << "transformed files to verify\n";
271 return true;
272 }
273
274 bool exists = false;
275 sys::fs::exists(It->second, exists);
276 if (!exists) {
277 errs() << "error: '" << It->second << "' does not exist\n";
278 return true;
279 }
280 sys::fs::exists(inputResultFname, exists);
281 if (!exists) {
282 errs() << "error: '" << inputResultFname << "' does not exist\n";
283 return true;
284 }
285
286 if (!filesCompareEqual(It->second, inputResultFname)) {
287 errs() << "error: '" << It->second << "' is different than "
288 << "'" << inputResultFname << "'\n";
289 return true;
290 }
291
292 resultMap.erase(It);
293 }
294
295 if (!resultMap.empty()) {
296 for (std::map<StringRef, StringRef>::iterator
297 I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
298 errs() << "error: '" << I->second << "' was not verified!\n";
299 return true;
300 }
301
302 return false;
303}
304
John McCall8f0e8d22011-06-15 23:25:17 +0000305//===----------------------------------------------------------------------===//
306// Misc. functions.
307//===----------------------------------------------------------------------===//
308
309static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000310 raw_ostream &OS) {
John McCall8f0e8d22011-06-15 23:25:17 +0000311 SourceManager &SM = Ctx.getSourceManager();
312 PresumedLoc PL = SM.getPresumedLoc(loc);
313
314 OS << llvm::sys::path::filename(PL.getFilename());
315 OS << ":" << PL.getLine() << ":"
316 << PL.getColumn();
317}
318
319static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000320 raw_ostream &OS) {
John McCall8f0e8d22011-06-15 23:25:17 +0000321 SourceManager &SM = Ctx.getSourceManager();
322 const LangOptions &langOpts = Ctx.getLangOptions();
323
324 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
325
326 OS << llvm::sys::path::filename(PL.getFilename());
327 OS << " [" << PL.getLine() << ":"
328 << PL.getColumn();
329 OS << " - ";
330
331 SourceLocation end = range.getEnd();
332 PL = SM.getPresumedLoc(end);
333
334 unsigned endCol = PL.getColumn() - 1;
335 if (!range.isTokenRange())
336 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
337 OS << PL.getLine() << ":" << endCol << "]";
338}
339
340//===----------------------------------------------------------------------===//
341// Command line processing.
342//===----------------------------------------------------------------------===//
343
344int main(int argc, const char **argv) {
John McCall8f0e8d22011-06-15 23:25:17 +0000345 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
346 llvm::sys::PrintStackTraceOnErrorSignal();
347
348 std::string
349 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
350
351 int optargc = 0;
352 for (; optargc != argc; ++optargc) {
353 if (StringRef(argv[optargc]) == "--args")
354 break;
355 }
David Blaikie6bd17d22012-02-07 19:36:38 +0000356 llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000357
358 if (VerifyTransformedFiles) {
359 if (ResultFiles.empty()) {
360 llvm::cl::PrintHelpMessage();
361 return 1;
362 }
363 return verifyTransformedFiles(ResultFiles);
364 }
365
John McCall8f0e8d22011-06-15 23:25:17 +0000366 if (optargc == argc) {
367 llvm::cl::PrintHelpMessage();
368 return 1;
369 }
370
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000371 ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
John McCall8f0e8d22011-06-15 23:25:17 +0000372
373 if (CheckOnly)
374 return checkForMigration(resourcesPath, Args);
375
376 return performTransformations(resourcesPath, Args);
377}