blob: a59a869c225c0123752cc967ff79ec79104e9708 [file] [log] [blame]
John McCalld70fb982011-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"
John McCalld70fb982011-06-15 23:25:17 +000013#include "clang/Frontend/Utils.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000014#include "clang/Frontend/VerifyDiagnosticConsumer.h"
John McCalld70fb982011-06-15 23:25:17 +000015#include "clang/Lex/Preprocessor.h"
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000016#include "llvm/Support/FileSystem.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000017#include "llvm/Support/MemoryBuffer.h"
John McCalld70fb982011-06-15 23:25:17 +000018#include "llvm/Support/Signals.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000019#include <system_error>
John McCalld70fb982011-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 Kyrtzidis7fbd97f2011-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 McCalld70fb982011-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).
Rafael Espindola9678d272013-06-26 05:03:40 +000064std::string GetExecutablePath(const char *Argv0) {
John McCalld70fb982011-06-15 23:25:17 +000065 // 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;
Rafael Espindola9678d272013-06-26 05:03:40 +000068 return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
John McCalld70fb982011-06-15 23:25:17 +000069}
70
71static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000072 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000073static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000074 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000075
76namespace {
77
78class PrintTransforms : public MigrationProcess::RewriteListener {
79 ASTContext *Ctx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000080 raw_ostream &OS;
John McCalld70fb982011-06-15 23:25:17 +000081
82public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000083 PrintTransforms(raw_ostream &OS)
Craig Topper69186e72014-06-08 08:38:04 +000084 : Ctx(nullptr), OS(OS) {}
John McCalld70fb982011-06-15 23:25:17 +000085
Craig Topper36835562014-03-15 07:47:46 +000086 void start(ASTContext &ctx) override { Ctx = &ctx; }
Craig Topper69186e72014-06-08 08:38:04 +000087 void finish() override { Ctx = nullptr; }
John McCalld70fb982011-06-15 23:25:17 +000088
Craig Topper36835562014-03-15 07:47:46 +000089 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +000090 assert(Ctx);
91 OS << "Insert: ";
92 printSourceLocation(loc, *Ctx, OS);
93 OS << " \"" << text << "\"\n";
94 }
95
Craig Topper36835562014-03-15 07:47:46 +000096 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +000097 assert(Ctx);
98 OS << "Remove: ";
99 printSourceRange(range, *Ctx, OS);
100 OS << '\n';
101 }
102};
103
104} // anonymous namespace
105
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000106static bool checkForMigration(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000107 ArrayRef<const char *> Args) {
Douglas Gregor811db4e2012-10-23 22:26:28 +0000108 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000109 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000110 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000111 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
112 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000113 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000114 // Chain in -verify checker, if requested.
Craig Topper69186e72014-06-08 08:38:04 +0000115 VerifyDiagnosticConsumer *verifyDiag = nullptr;
John McCalld70fb982011-06-15 23:25:17 +0000116 if (VerifyDiags) {
David Blaikie69609dc2011-09-26 00:38:03 +0000117 verifyDiag = new VerifyDiagnosticConsumer(*Diags);
John McCalld70fb982011-06-15 23:25:17 +0000118 Diags->setClient(verifyDiag);
119 }
120
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000121 CompilerInvocation CI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000122 if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
123 return true;
John McCalld70fb982011-06-15 23:25:17 +0000124
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000125 if (CI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000126 llvm::errs() << "error: no input files\n";
127 return true;
128 }
129
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000130 if (!CI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000131 return false;
132
Douglas Gregor32fbe312012-01-20 16:28:04 +0000133 arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000134 Diags->getClient());
135 return Diags->getClient()->getNumErrors() > 0;
John McCalld70fb982011-06-15 23:25:17 +0000136}
137
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000138static void printResult(FileRemapper &remapper, raw_ostream &OS) {
Ted Kremenekf7639e12012-03-06 20:06:33 +0000139 PreprocessorOptions PPOpts;
140 remapper.applyMappings(PPOpts);
John McCalld70fb982011-06-15 23:25:17 +0000141 // The changed files will be in memory buffers, print them.
Alp Toker1b070d22014-07-07 07:47:20 +0000142 for (const auto &RB : PPOpts.RemappedFileBuffers)
143 OS << RB.second->getBuffer();
John McCalld70fb982011-06-15 23:25:17 +0000144}
145
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000146static bool performTransformations(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000147 ArrayRef<const char *> Args) {
John McCalld70fb982011-06-15 23:25:17 +0000148 // Check first.
149 if (checkForMigration(resourcesPath, Args))
150 return true;
151
Douglas Gregor811db4e2012-10-23 22:26:28 +0000152 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000153 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000154 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000155 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
156 IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000157 new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000158
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000159 CompilerInvocation origCI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000160 if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
161 *TopDiags))
162 return true;
John McCalld70fb982011-06-15 23:25:17 +0000163
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000164 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000165 llvm::errs() << "error: no input files\n";
166 return true;
167 }
168
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000169 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000170 return false;
171
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000172 MigrationProcess migration(origCI, DiagClient);
John McCalld70fb982011-06-15 23:25:17 +0000173
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000174 std::vector<TransformFn>
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000175 transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
176 origCI.getMigratorOpts().NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000177 assert(!transforms.empty());
178
Ahmed Charlesb8984322014-03-07 20:03:18 +0000179 std::unique_ptr<PrintTransforms> transformPrinter;
John McCalld70fb982011-06-15 23:25:17 +0000180 if (OutputTransformations)
181 transformPrinter.reset(new PrintTransforms(llvm::outs()));
182
183 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
184 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
185 if (err) return true;
186
187 if (VerboseOpt) {
188 if (i == e-1)
189 llvm::errs() << "\n##### FINAL RESULT #####\n";
190 else
191 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
192 printResult(migration.getRemapper(), llvm::errs());
193 llvm::errs() << "\n##########################\n\n";
194 }
195 }
196
197 if (!OutputTransformations)
198 printResult(migration.getRemapper(), llvm::outs());
199
200 // FIXME: TestResultForARC
201
202 return false;
203}
204
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000206 using namespace llvm;
207
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000208 ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000209 if (!file1)
210 return false;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000211
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000212 ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000213 if (!file2)
214 return false;
215
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000216 return file1.get()->getBuffer() == file2.get()->getBuffer();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000217}
218
Chris Lattner54b16772011-07-23 17:14:25 +0000219static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000220 using namespace llvm;
221
222 assert(!resultFiles.empty());
223
224 std::map<StringRef, StringRef> resultMap;
225
226 for (ArrayRef<std::string>::iterator
227 I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
228 StringRef fname(*I);
229 if (!fname.endswith(".result")) {
230 errs() << "error: filename '" << fname
231 << "' does not have '.result' extension\n";
232 return true;
233 }
234 resultMap[sys::path::stem(fname)] = fname;
235 }
236
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000237 ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000238 if (RemappingsFile.empty())
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000239 inputBuf = MemoryBuffer::getSTDIN();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000240 else
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000241 inputBuf = MemoryBuffer::getFile(RemappingsFile);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000242 if (!inputBuf) {
243 errs() << "error: could not read remappings input\n";
244 return true;
245 }
246
247 SmallVector<StringRef, 8> strs;
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000248 inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
249 /*KeepEmpty=*/false);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000250
251 if (strs.empty()) {
252 errs() << "error: no files to verify from stdin\n";
253 return true;
254 }
255 if (strs.size() % 2 != 0) {
256 errs() << "error: files to verify are not original/result pairs\n";
257 return true;
258 }
259
260 for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
261 StringRef inputOrigFname = strs[i];
262 StringRef inputResultFname = strs[i+1];
263
264 std::map<StringRef, StringRef>::iterator It;
265 It = resultMap.find(sys::path::filename(inputOrigFname));
266 if (It == resultMap.end()) {
267 errs() << "error: '" << inputOrigFname << "' is not in the list of "
268 << "transformed files to verify\n";
269 return true;
270 }
271
272 bool exists = false;
273 sys::fs::exists(It->second, exists);
274 if (!exists) {
275 errs() << "error: '" << It->second << "' does not exist\n";
276 return true;
277 }
278 sys::fs::exists(inputResultFname, exists);
279 if (!exists) {
280 errs() << "error: '" << inputResultFname << "' does not exist\n";
281 return true;
282 }
283
284 if (!filesCompareEqual(It->second, inputResultFname)) {
285 errs() << "error: '" << It->second << "' is different than "
286 << "'" << inputResultFname << "'\n";
287 return true;
288 }
289
290 resultMap.erase(It);
291 }
292
293 if (!resultMap.empty()) {
294 for (std::map<StringRef, StringRef>::iterator
295 I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
296 errs() << "error: '" << I->second << "' was not verified!\n";
297 return true;
298 }
299
300 return false;
301}
302
John McCalld70fb982011-06-15 23:25:17 +0000303//===----------------------------------------------------------------------===//
304// Misc. functions.
305//===----------------------------------------------------------------------===//
306
307static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000308 raw_ostream &OS) {
John McCalld70fb982011-06-15 23:25:17 +0000309 SourceManager &SM = Ctx.getSourceManager();
310 PresumedLoc PL = SM.getPresumedLoc(loc);
311
312 OS << llvm::sys::path::filename(PL.getFilename());
313 OS << ":" << PL.getLine() << ":"
314 << PL.getColumn();
315}
316
317static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000318 raw_ostream &OS) {
John McCalld70fb982011-06-15 23:25:17 +0000319 SourceManager &SM = Ctx.getSourceManager();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000320 const LangOptions &langOpts = Ctx.getLangOpts();
John McCalld70fb982011-06-15 23:25:17 +0000321
322 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
323
324 OS << llvm::sys::path::filename(PL.getFilename());
325 OS << " [" << PL.getLine() << ":"
326 << PL.getColumn();
327 OS << " - ";
328
329 SourceLocation end = range.getEnd();
330 PL = SM.getPresumedLoc(end);
331
332 unsigned endCol = PL.getColumn() - 1;
333 if (!range.isTokenRange())
334 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
335 OS << PL.getLine() << ":" << endCol << "]";
336}
337
338//===----------------------------------------------------------------------===//
339// Command line processing.
340//===----------------------------------------------------------------------===//
341
342int main(int argc, const char **argv) {
John McCalld70fb982011-06-15 23:25:17 +0000343 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
344 llvm::sys::PrintStackTraceOnErrorSignal();
345
346 std::string
347 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
348
349 int optargc = 0;
350 for (; optargc != argc; ++optargc) {
351 if (StringRef(argv[optargc]) == "--args")
352 break;
353 }
David Blaikie09d20ee2012-02-07 19:36:38 +0000354 llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000355
356 if (VerifyTransformedFiles) {
357 if (ResultFiles.empty()) {
358 llvm::cl::PrintHelpMessage();
359 return 1;
360 }
361 return verifyTransformedFiles(ResultFiles);
362 }
363
John McCalld70fb982011-06-15 23:25:17 +0000364 if (optargc == argc) {
365 llvm::cl::PrintHelpMessage();
366 return 1;
367 }
368
Chris Lattner54b16772011-07-23 17:14:25 +0000369 ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
John McCalld70fb982011-06-15 23:25:17 +0000370
371 if (CheckOnly)
372 return checkForMigration(resourcesPath, Args);
373
374 return performTransformations(resourcesPath, Args);
375}