blob: 5f075f860f542f1642bd848f4f22c9860d4f1d27 [file] [log] [blame]
John McCalld70fb982011-06-15 23:25:17 +00001//===-- arcmt-test.cpp - ARC Migration Tool testbed -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
John McCalld70fb982011-06-15 23:25:17 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/ARCMigrate/ARCMT.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000010#include "clang/AST/ASTContext.h"
11#include "clang/Frontend/PCHContainerOperations.h"
John McCalld70fb982011-06-15 23:25:17 +000012#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"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "clang/Lex/PreprocessorOptions.h"
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000017#include "llvm/Support/FileSystem.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000018#include "llvm/Support/MemoryBuffer.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000019#include "llvm/Support/Path.h"
John McCalld70fb982011-06-15 23:25:17 +000020#include "llvm/Support/Signals.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000021#include <system_error>
John McCalld70fb982011-06-15 23:25:17 +000022
23using namespace clang;
24using namespace arcmt;
25
26static llvm::cl::opt<bool>
27CheckOnly("check-only",
28 llvm::cl::desc("Just check for issues that need to be handled manually"));
29
30//static llvm::cl::opt<bool>
31//TestResultForARC("test-result",
32//llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
33
34static llvm::cl::opt<bool>
35OutputTransformations("output-transformations",
36 llvm::cl::desc("Print the source transformations"));
37
38static llvm::cl::opt<bool>
39VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
40
41static llvm::cl::opt<bool>
42VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
43
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000044static llvm::cl::opt<bool>
45VerifyTransformedFiles("verify-transformed-files",
46llvm::cl::desc("Read pairs of file mappings (typically the output of "
47 "c-arcmt-test) and compare their contents with the filenames "
48 "provided in command-line"));
49
50static llvm::cl::opt<std::string>
51RemappingsFile("remappings-file",
52 llvm::cl::desc("Pairs of file mappings (typically the output of "
53 "c-arcmt-test)"));
54
55static llvm::cl::list<std::string>
56ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
57
John McCalld70fb982011-06-15 23:25:17 +000058static llvm::cl::extrahelp extraHelp(
59 "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
60
61// This function isn't referenced outside its translation unit, but it
62// can't use the "static" keyword because its address is used for
63// GetMainExecutable (since some platforms don't support taking the
64// address of main, and some platforms can't implement GetMainExecutable
65// without being given the address of a function in the main executable).
Rafael Espindola9678d272013-06-26 05:03:40 +000066std::string GetExecutablePath(const char *Argv0) {
John McCalld70fb982011-06-15 23:25:17 +000067 // This just needs to be some symbol in the binary; C++ doesn't
68 // allow taking the address of ::main however.
69 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
Rafael Espindola9678d272013-06-26 05:03:40 +000070 return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
John McCalld70fb982011-06-15 23:25:17 +000071}
72
73static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000074 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000075static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000076 raw_ostream &OS);
John McCalld70fb982011-06-15 23:25:17 +000077
78namespace {
79
80class PrintTransforms : public MigrationProcess::RewriteListener {
81 ASTContext *Ctx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000082 raw_ostream &OS;
John McCalld70fb982011-06-15 23:25:17 +000083
84public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000085 PrintTransforms(raw_ostream &OS)
Craig Topper69186e72014-06-08 08:38:04 +000086 : Ctx(nullptr), OS(OS) {}
John McCalld70fb982011-06-15 23:25:17 +000087
Craig Topper36835562014-03-15 07:47:46 +000088 void start(ASTContext &ctx) override { Ctx = &ctx; }
Craig Topper69186e72014-06-08 08:38:04 +000089 void finish() override { Ctx = nullptr; }
John McCalld70fb982011-06-15 23:25:17 +000090
Craig Topper36835562014-03-15 07:47:46 +000091 void insert(SourceLocation loc, StringRef text) override {
John McCalld70fb982011-06-15 23:25:17 +000092 assert(Ctx);
93 OS << "Insert: ";
94 printSourceLocation(loc, *Ctx, OS);
95 OS << " \"" << text << "\"\n";
96 }
97
Craig Topper36835562014-03-15 07:47:46 +000098 void remove(CharSourceRange range) override {
John McCalld70fb982011-06-15 23:25:17 +000099 assert(Ctx);
100 OS << "Remove: ";
101 printSourceRange(range, *Ctx, OS);
102 OS << '\n';
103 }
104};
105
106} // anonymous namespace
107
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000108static bool checkForMigration(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000109 ArrayRef<const char *> Args) {
Douglas Gregor811db4e2012-10-23 22:26:28 +0000110 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000111 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000112 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000113 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
114 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000115 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000116 // Chain in -verify checker, if requested.
Craig Topper69186e72014-06-08 08:38:04 +0000117 VerifyDiagnosticConsumer *verifyDiag = nullptr;
John McCalld70fb982011-06-15 23:25:17 +0000118 if (VerifyDiags) {
David Blaikie69609dc2011-09-26 00:38:03 +0000119 verifyDiag = new VerifyDiagnosticConsumer(*Diags);
John McCalld70fb982011-06-15 23:25:17 +0000120 Diags->setClient(verifyDiag);
121 }
122
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000123 CompilerInvocation CI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000124 if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
125 return true;
John McCalld70fb982011-06-15 23:25:17 +0000126
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000127 if (CI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000128 llvm::errs() << "error: no input files\n";
129 return true;
130 }
131
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000132 if (!CI.getLangOpts()->ObjC)
John McCalld70fb982011-06-15 23:25:17 +0000133 return false;
134
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000135 arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000136 std::make_shared<PCHContainerOperations>(),
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000137 Diags->getClient());
138 return Diags->getClient()->getNumErrors() > 0;
John McCalld70fb982011-06-15 23:25:17 +0000139}
140
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000141static void printResult(FileRemapper &remapper, raw_ostream &OS) {
Ted Kremenekf7639e12012-03-06 20:06:33 +0000142 PreprocessorOptions PPOpts;
143 remapper.applyMappings(PPOpts);
John McCalld70fb982011-06-15 23:25:17 +0000144 // The changed files will be in memory buffers, print them.
Alp Toker1b070d22014-07-07 07:47:20 +0000145 for (const auto &RB : PPOpts.RemappedFileBuffers)
146 OS << RB.second->getBuffer();
John McCalld70fb982011-06-15 23:25:17 +0000147}
148
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000149static bool performTransformations(StringRef resourcesPath,
Chris Lattner54b16772011-07-23 17:14:25 +0000150 ArrayRef<const char *> Args) {
John McCalld70fb982011-06-15 23:25:17 +0000151 // Check first.
152 if (checkForMigration(resourcesPath, Args))
153 return true;
154
Douglas Gregor811db4e2012-10-23 22:26:28 +0000155 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
David Blaikiee2eefae2011-09-25 23:39:51 +0000156 DiagnosticConsumer *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000157 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000158 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
159 IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000160 new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
John McCalld70fb982011-06-15 23:25:17 +0000161
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000162 CompilerInvocation origCI;
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000163 if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
164 *TopDiags))
165 return true;
John McCalld70fb982011-06-15 23:25:17 +0000166
Argyrios Kyrtzidis6e4ef202011-06-16 00:53:46 +0000167 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCalld70fb982011-06-15 23:25:17 +0000168 llvm::errs() << "error: no input files\n";
169 return true;
170 }
171
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000172 if (!origCI.getLangOpts()->ObjC)
John McCalld70fb982011-06-15 23:25:17 +0000173 return false;
174
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000175 MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
176 DiagClient);
John McCalld70fb982011-06-15 23:25:17 +0000177
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000178 std::vector<TransformFn>
Fariborz Jahanian48fd81b2012-01-26 20:57:58 +0000179 transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
180 origCI.getMigratorOpts().NoFinalizeRemoval);
John McCalld70fb982011-06-15 23:25:17 +0000181 assert(!transforms.empty());
182
Ahmed Charlesb8984322014-03-07 20:03:18 +0000183 std::unique_ptr<PrintTransforms> transformPrinter;
John McCalld70fb982011-06-15 23:25:17 +0000184 if (OutputTransformations)
185 transformPrinter.reset(new PrintTransforms(llvm::outs()));
186
187 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
188 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
189 if (err) return true;
190
191 if (VerboseOpt) {
192 if (i == e-1)
193 llvm::errs() << "\n##### FINAL RESULT #####\n";
194 else
195 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
196 printResult(migration.getRemapper(), llvm::errs());
197 llvm::errs() << "\n##########################\n\n";
198 }
199 }
200
201 if (!OutputTransformations)
202 printResult(migration.getRemapper(), llvm::outs());
203
204 // FIXME: TestResultForARC
205
206 return false;
207}
208
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000209static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000210 using namespace llvm;
211
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000212 ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000213 if (!file1)
214 return false;
Ahmed Charlesb8984322014-03-07 20:03:18 +0000215
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000216 ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000217 if (!file2)
218 return false;
219
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000220 return file1.get()->getBuffer() == file2.get()->getBuffer();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000221}
222
Chris Lattner54b16772011-07-23 17:14:25 +0000223static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000224 using namespace llvm;
225
226 assert(!resultFiles.empty());
227
228 std::map<StringRef, StringRef> resultMap;
229
230 for (ArrayRef<std::string>::iterator
231 I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
232 StringRef fname(*I);
233 if (!fname.endswith(".result")) {
234 errs() << "error: filename '" << fname
235 << "' does not have '.result' extension\n";
236 return true;
237 }
238 resultMap[sys::path::stem(fname)] = fname;
239 }
240
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000241 ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000242 if (RemappingsFile.empty())
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000243 inputBuf = MemoryBuffer::getSTDIN();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000244 else
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000245 inputBuf = MemoryBuffer::getFile(RemappingsFile);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000246 if (!inputBuf) {
247 errs() << "error: could not read remappings input\n";
248 return true;
249 }
250
251 SmallVector<StringRef, 8> strs;
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000252 inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
253 /*KeepEmpty=*/false);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000254
255 if (strs.empty()) {
256 errs() << "error: no files to verify from stdin\n";
257 return true;
258 }
259 if (strs.size() % 2 != 0) {
260 errs() << "error: files to verify are not original/result pairs\n";
261 return true;
262 }
263
264 for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
265 StringRef inputOrigFname = strs[i];
266 StringRef inputResultFname = strs[i+1];
267
268 std::map<StringRef, StringRef>::iterator It;
269 It = resultMap.find(sys::path::filename(inputOrigFname));
270 if (It == resultMap.end()) {
271 errs() << "error: '" << inputOrigFname << "' is not in the list of "
272 << "transformed files to verify\n";
273 return true;
274 }
275
Rafael Espindola611505f2014-09-11 18:10:13 +0000276 if (!sys::fs::exists(It->second)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000277 errs() << "error: '" << It->second << "' does not exist\n";
278 return true;
279 }
Rafael Espindola611505f2014-09-11 18:10:13 +0000280 if (!sys::fs::exists(inputResultFname)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000281 errs() << "error: '" << inputResultFname << "' does not exist\n";
282 return true;
283 }
284
285 if (!filesCompareEqual(It->second, inputResultFname)) {
286 errs() << "error: '" << It->second << "' is different than "
287 << "'" << inputResultFname << "'\n";
288 return true;
289 }
290
291 resultMap.erase(It);
292 }
293
294 if (!resultMap.empty()) {
295 for (std::map<StringRef, StringRef>::iterator
296 I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
297 errs() << "error: '" << I->second << "' was not verified!\n";
298 return true;
299 }
300
301 return false;
302}
303
John McCalld70fb982011-06-15 23:25:17 +0000304//===----------------------------------------------------------------------===//
305// Misc. functions.
306//===----------------------------------------------------------------------===//
307
308static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309 raw_ostream &OS) {
John McCalld70fb982011-06-15 23:25:17 +0000310 SourceManager &SM = Ctx.getSourceManager();
311 PresumedLoc PL = SM.getPresumedLoc(loc);
312
313 OS << llvm::sys::path::filename(PL.getFilename());
314 OS << ":" << PL.getLine() << ":"
315 << PL.getColumn();
316}
317
318static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000319 raw_ostream &OS) {
John McCalld70fb982011-06-15 23:25:17 +0000320 SourceManager &SM = Ctx.getSourceManager();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000321 const LangOptions &langOpts = Ctx.getLangOpts();
John McCalld70fb982011-06-15 23:25:17 +0000322
323 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
324
325 OS << llvm::sys::path::filename(PL.getFilename());
326 OS << " [" << PL.getLine() << ":"
327 << PL.getColumn();
328 OS << " - ";
329
330 SourceLocation end = range.getEnd();
331 PL = SM.getPresumedLoc(end);
332
333 unsigned endCol = PL.getColumn() - 1;
334 if (!range.isTokenRange())
335 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
336 OS << PL.getLine() << ":" << endCol << "]";
337}
338
339//===----------------------------------------------------------------------===//
340// Command line processing.
341//===----------------------------------------------------------------------===//
342
343int main(int argc, const char **argv) {
John McCalld70fb982011-06-15 23:25:17 +0000344 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
Richard Smithdfed58a2016-06-09 00:53:41 +0000345 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
John McCalld70fb982011-06-15 23:25:17 +0000346
347 std::string
348 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
349
350 int optargc = 0;
351 for (; optargc != argc; ++optargc) {
352 if (StringRef(argv[optargc]) == "--args")
353 break;
354 }
David Blaikie09d20ee2012-02-07 19:36:38 +0000355 llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000356
357 if (VerifyTransformedFiles) {
358 if (ResultFiles.empty()) {
359 llvm::cl::PrintHelpMessage();
360 return 1;
361 }
362 return verifyTransformedFiles(ResultFiles);
363 }
364
John McCalld70fb982011-06-15 23:25:17 +0000365 if (optargc == argc) {
366 llvm::cl::PrintHelpMessage();
367 return 1;
368 }
369
Chris Lattner54b16772011-07-23 17:14:25 +0000370 ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
John McCalld70fb982011-06-15 23:25:17 +0000371
372 if (CheckOnly)
373 return checkForMigration(resourcesPath, Args);
374
375 return performTransformations(resourcesPath, Args);
376}