blob: e57d69fddbe229aecec8830a11a048f0d6d2ebfd [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000011#include "clang/AST/ASTContext.h"
12#include "clang/Frontend/PCHContainerOperations.h"
John McCalld70fb982011-06-15 23:25:17 +000013#include "clang/Frontend/TextDiagnosticPrinter.h"
John McCalld70fb982011-06-15 23:25:17 +000014#include "clang/Frontend/Utils.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000015#include "clang/Frontend/VerifyDiagnosticConsumer.h"
John McCalld70fb982011-06-15 23:25:17 +000016#include "clang/Lex/Preprocessor.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Lex/PreprocessorOptions.h"
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "llvm/Support/MemoryBuffer.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "llvm/Support/Path.h"
John McCalld70fb982011-06-15 23:25:17 +000021#include "llvm/Support/Signals.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000022#include <system_error>
John McCalld70fb982011-06-15 23:25:17 +000023
24using namespace clang;
25using namespace arcmt;
26
27static llvm::cl::opt<bool>
28CheckOnly("check-only",
29 llvm::cl::desc("Just check for issues that need to be handled manually"));
30
31//static llvm::cl::opt<bool>
32//TestResultForARC("test-result",
33//llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
34
35static llvm::cl::opt<bool>
36OutputTransformations("output-transformations",
37 llvm::cl::desc("Print the source transformations"));
38
39static llvm::cl::opt<bool>
40VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
41
42static llvm::cl::opt<bool>
43VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
44
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000045static llvm::cl::opt<bool>
46VerifyTransformedFiles("verify-transformed-files",
47llvm::cl::desc("Read pairs of file mappings (typically the output of "
48 "c-arcmt-test) and compare their contents with the filenames "
49 "provided in command-line"));
50
51static llvm::cl::opt<std::string>
52RemappingsFile("remappings-file",
53 llvm::cl::desc("Pairs of file mappings (typically the output of "
54 "c-arcmt-test)"));
55
56static llvm::cl::list<std::string>
57ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
58
John McCalld70fb982011-06-15 23:25:17 +000059static llvm::cl::extrahelp extraHelp(
60 "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
61
62// This function isn't referenced outside its translation unit, but it
63// can't use the "static" keyword because its address is used for
64// GetMainExecutable (since some platforms don't support taking the
65// address of main, and some platforms can't implement GetMainExecutable
66// without being given the address of a function in the main executable).
Rafael Espindola9678d272013-06-26 05:03:40 +000067std::string GetExecutablePath(const char *Argv0) {
John McCalld70fb982011-06-15 23:25:17 +000068 // This just needs to be some symbol in the binary; C++ doesn't
69 // allow taking the address of ::main however.
70 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
Rafael Espindola9678d272013-06-26 05:03:40 +000071 return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
John McCalld70fb982011-06-15 23:25:17 +000072}
73
74static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000075 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000076static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000077 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000078
79namespace {
80
81class PrintTransforms : public MigrationProcess::RewriteListener {
82 ASTContext *Ctx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000083 raw_ostream &OS;
John McCalld70fb982011-06-15 23:25:17 +000084
85public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000086 PrintTransforms(raw_ostream &OS)
Craig Topper69186e72014-06-08 08:38:04 +000087 : Ctx(nullptr), OS(OS) {}
John McCalld70fb982011-06-15 23:25:17 +000088
Craig Topper36835562014-03-15 07:47:46 +000089 void start(ASTContext &ctx) override { Ctx = &ctx; }
Craig Topper69186e72014-06-08 08:38:04 +000090 void finish() override { Ctx = nullptr; }
John McCalld70fb982011-06-15 23:25:17 +000091
Craig Topper36835562014-03-15 07:47:46 +000092 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +000093 assert(Ctx);
94 OS << "Insert: ";
95 printSourceLocation(loc, *Ctx, OS);
96 OS << " \"" << text << "\"\n";
97 }
98
Craig Topper36835562014-03-15 07:47:46 +000099 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +0000100 assert(Ctx);
101 OS << "Remove: ";
102 printSourceRange(range, *Ctx, OS);
103 OS << '\n';
104 }
105};
106
107} // anonymous namespace
108
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000109static bool checkForMigration(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000110 ArrayRef<const char *> Args) {
Douglas Gregor811db4e2012-10-23 22:26:28 +0000111 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000112 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000113 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000114 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
115 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000116 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000117 // Chain in -verify checker, if requested.
Craig Topper69186e72014-06-08 08:38:04 +0000118 VerifyDiagnosticConsumer *verifyDiag = nullptr;
John McCalld70fb982011-06-15 23:25:17 +0000119 if (VerifyDiags) {
David Blaikie69609dc2011-09-26 00:38:03 +0000120 verifyDiag = new VerifyDiagnosticConsumer(*Diags);
John McCalld70fb982011-06-15 23:25:17 +0000121 Diags->setClient(verifyDiag);
122 }
123
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000124 CompilerInvocation CI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000125 if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
126 return true;
John McCalld70fb982011-06-15 23:25:17 +0000127
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000128 if (CI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000129 llvm::errs() << "error: no input files\n";
130 return true;
131 }
132
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000133 if (!CI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000134 return false;
135
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000136 arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000137 std::make_shared<PCHContainerOperations>(),
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000138 Diags->getClient());
139 return Diags->getClient()->getNumErrors() > 0;
John McCalld70fb982011-06-15 23:25:17 +0000140}
141
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142static void printResult(FileRemapper &remapper, raw_ostream &OS) {
Ted Kremenekf7639e12012-03-06 20:06:33 +0000143 PreprocessorOptions PPOpts;
144 remapper.applyMappings(PPOpts);
John McCalld70fb982011-06-15 23:25:17 +0000145 // The changed files will be in memory buffers, print them.
Alp Toker1b070d22014-07-07 07:47:20 +0000146 for (const auto &RB : PPOpts.RemappedFileBuffers)
147 OS << RB.second->getBuffer();
John McCalld70fb982011-06-15 23:25:17 +0000148}
149
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000150static bool performTransformations(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000151 ArrayRef<const char *> Args) {
John McCalld70fb982011-06-15 23:25:17 +0000152 // Check first.
153 if (checkForMigration(resourcesPath, Args))
154 return true;
155
Douglas Gregor811db4e2012-10-23 22:26:28 +0000156 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000157 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000158 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000159 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
160 IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000161 new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000162
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000163 CompilerInvocation origCI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000164 if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
165 *TopDiags))
166 return true;
John McCalld70fb982011-06-15 23:25:17 +0000167
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000168 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000169 llvm::errs() << "error: no input files\n";
170 return true;
171 }
172
Ted Kremenek8cf47df2011-11-17 23:01:24 +0000173 if (!origCI.getLangOpts()->ObjC1)
John McCalld70fb982011-06-15 23:25:17 +0000174 return false;
175
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000176 MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
177 DiagClient);
John McCalld70fb982011-06-15 23:25:17 +0000178
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000179 std::vector<TransformFn>
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000180 transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
181 origCI.getMigratorOpts().NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000182 assert(!transforms.empty());
183
Ahmed Charlesb8984322014-03-07 20:03:18 +0000184 std::unique_ptr<PrintTransforms> transformPrinter;
John McCalld70fb982011-06-15 23:25:17 +0000185 if (OutputTransformations)
186 transformPrinter.reset(new PrintTransforms(llvm::outs()));
187
188 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
189 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
190 if (err) return true;
191
192 if (VerboseOpt) {
193 if (i == e-1)
194 llvm::errs() << "\n##### FINAL RESULT #####\n";
195 else
196 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
197 printResult(migration.getRemapper(), llvm::errs());
198 llvm::errs() << "\n##########################\n\n";
199 }
200 }
201
202 if (!OutputTransformations)
203 printResult(migration.getRemapper(), llvm::outs());
204
205 // FIXME: TestResultForARC
206
207 return false;
208}
209
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000210static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000211 using namespace llvm;
212
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000213 ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000214 if (!file1)
215 return false;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000216
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000217 ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000218 if (!file2)
219 return false;
220
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000221 return file1.get()->getBuffer() == file2.get()->getBuffer();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000222}
223
Chris Lattner54b16772011-07-23 17:14:25 +0000224static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000225 using namespace llvm;
226
227 assert(!resultFiles.empty());
228
229 std::map<StringRef, StringRef> resultMap;
230
231 for (ArrayRef<std::string>::iterator
232 I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
233 StringRef fname(*I);
234 if (!fname.endswith(".result")) {
235 errs() << "error: filename '" << fname
236 << "' does not have '.result' extension\n";
237 return true;
238 }
239 resultMap[sys::path::stem(fname)] = fname;
240 }
241
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000242 ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000243 if (RemappingsFile.empty())
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000244 inputBuf = MemoryBuffer::getSTDIN();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000245 else
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000246 inputBuf = MemoryBuffer::getFile(RemappingsFile);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000247 if (!inputBuf) {
248 errs() << "error: could not read remappings input\n";
249 return true;
250 }
251
252 SmallVector<StringRef, 8> strs;
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000253 inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
254 /*KeepEmpty=*/false);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000255
256 if (strs.empty()) {
257 errs() << "error: no files to verify from stdin\n";
258 return true;
259 }
260 if (strs.size() % 2 != 0) {
261 errs() << "error: files to verify are not original/result pairs\n";
262 return true;
263 }
264
265 for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
266 StringRef inputOrigFname = strs[i];
267 StringRef inputResultFname = strs[i+1];
268
269 std::map<StringRef, StringRef>::iterator It;
270 It = resultMap.find(sys::path::filename(inputOrigFname));
271 if (It == resultMap.end()) {
272 errs() << "error: '" << inputOrigFname << "' is not in the list of "
273 << "transformed files to verify\n";
274 return true;
275 }
276
Rafael Espindola611505f2014-09-11 18:10:13 +0000277 if (!sys::fs::exists(It->second)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000278 errs() << "error: '" << It->second << "' does not exist\n";
279 return true;
280 }
Rafael Espindola611505f2014-09-11 18:10:13 +0000281 if (!sys::fs::exists(inputResultFname)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000282 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 McCalld70fb982011-06-15 23:25:17 +0000305//===----------------------------------------------------------------------===//
306// Misc. functions.
307//===----------------------------------------------------------------------===//
308
309static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000310 raw_ostream &OS) {
John McCalld70fb982011-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 Lattner0e62c1c2011-07-23 10:55:15 +0000320 raw_ostream &OS) {
John McCalld70fb982011-06-15 23:25:17 +0000321 SourceManager &SM = Ctx.getSourceManager();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000322 const LangOptions &langOpts = Ctx.getLangOpts();
John McCalld70fb982011-06-15 23:25:17 +0000323
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 McCalld70fb982011-06-15 23:25:17 +0000345 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
Richard Smithdfed58a2016-06-09 00:53:41 +0000346 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
John McCalld70fb982011-06-15 23:25:17 +0000347
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 Blaikie09d20ee2012-02-07 19:36:38 +0000356 llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
Argyrios Kyrtzidis7fbd97f2011-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 McCalld70fb982011-06-15 23:25:17 +0000366 if (optargc == argc) {
367 llvm::cl::PrintHelpMessage();
368 return 1;
369 }
370
Chris Lattner54b16772011-07-23 17:14:25 +0000371 ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
John McCalld70fb982011-06-15 23:25:17 +0000372
373 if (CheckOnly)
374 return checkForMigration(resourcesPath, Args);
375
376 return performTransformations(resourcesPath, Args);
377}