1 //===-- arcmt-test.cpp - ARC Migration Tool testbed -----------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/ARCMigrate/ARCMT.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/Frontend/PCHContainerOperations.h"
12 #include "clang/Frontend/TextDiagnosticPrinter.h"
13 #include "clang/Frontend/Utils.h"
14 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Lex/PreprocessorOptions.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/Signals.h"
21 #include <system_error>
22 
23 using namespace clang;
24 using namespace arcmt;
25 
26 static llvm::cl::opt<bool>
27 CheckOnly("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 
34 static llvm::cl::opt<bool>
35 OutputTransformations("output-transformations",
36                       llvm::cl::desc("Print the source transformations"));
37 
38 static llvm::cl::opt<bool>
39 VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
40 
41 static llvm::cl::opt<bool>
42 VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
43 
44 static llvm::cl::opt<bool>
45 VerifyTransformedFiles("verify-transformed-files",
46 llvm::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 
50 static llvm::cl::opt<std::string>
51 RemappingsFile("remappings-file",
52                llvm::cl::desc("Pairs of file mappings (typically the output of "
53                "c-arcmt-test)"));
54 
55 static llvm::cl::list<std::string>
56 ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
57 
58 static 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).
66 std::string GetExecutablePath(const char *Argv0) {
67   // 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;
70   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
71 }
72 
73 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
74                                 raw_ostream &OS);
75 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
76                              raw_ostream &OS);
77 
78 namespace {
79 
80 class PrintTransforms : public MigrationProcess::RewriteListener {
81   ASTContext *Ctx;
82   raw_ostream &OS;
83 
84 public:
85   PrintTransforms(raw_ostream &OS)
86     : Ctx(nullptr), OS(OS) {}
87 
88   void start(ASTContext &ctx) override { Ctx = &ctx; }
89   void finish() override { Ctx = nullptr; }
90 
91   void insert(SourceLocation loc, StringRef text) override {
92     assert(Ctx);
93     OS << "Insert: ";
94     printSourceLocation(loc, *Ctx, OS);
95     OS << " \"" << text << "\"\n";
96   }
97 
98   void remove(CharSourceRange range) override {
99     assert(Ctx);
100     OS << "Remove: ";
101     printSourceRange(range, *Ctx, OS);
102     OS << '\n';
103   }
104 };
105 
106 } // anonymous namespace
107 
108 static bool checkForMigration(StringRef resourcesPath,
109                               ArrayRef<const char *> Args) {
110   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
111   DiagnosticConsumer *DiagClient =
112     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
113   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
114   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
115       new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
116   // Chain in -verify checker, if requested.
117   VerifyDiagnosticConsumer *verifyDiag = nullptr;
118   if (VerifyDiags) {
119     verifyDiag = new VerifyDiagnosticConsumer(*Diags);
120     Diags->setClient(verifyDiag);
121   }
122 
123   CompilerInvocation CI;
124   if (!CompilerInvocation::CreateFromArgs(CI, Args, *Diags))
125     return true;
126 
127   if (CI.getFrontendOpts().Inputs.empty()) {
128     llvm::errs() << "error: no input files\n";
129     return true;
130   }
131 
132   if (!CI.getLangOpts()->ObjC)
133     return false;
134 
135   arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
136                               std::make_shared<PCHContainerOperations>(),
137                               Diags->getClient());
138   return Diags->getClient()->getNumErrors() > 0;
139 }
140 
141 static void printResult(FileRemapper &remapper, raw_ostream &OS) {
142   remapper.forEachMapping([](StringRef, StringRef) {},
143                           [&](StringRef, const llvm::MemoryBufferRef &Buffer) {
144                             OS << Buffer.getBuffer();
145                           });
146 }
147 
148 static bool performTransformations(StringRef resourcesPath,
149                                    ArrayRef<const char *> Args) {
150   // Check first.
151   if (checkForMigration(resourcesPath, Args))
152     return true;
153 
154   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
155   DiagnosticConsumer *DiagClient =
156     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
157   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
158   IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
159       new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
160 
161   CompilerInvocation origCI;
162   if (!CompilerInvocation::CreateFromArgs(origCI, Args, *TopDiags))
163     return true;
164 
165   if (origCI.getFrontendOpts().Inputs.empty()) {
166     llvm::errs() << "error: no input files\n";
167     return true;
168   }
169 
170   if (!origCI.getLangOpts()->ObjC)
171     return false;
172 
173   MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
174                              DiagClient);
175 
176   std::vector<TransformFn>
177     transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
178                                  origCI.getMigratorOpts().NoFinalizeRemoval);
179   assert(!transforms.empty());
180 
181   std::unique_ptr<PrintTransforms> transformPrinter;
182   if (OutputTransformations)
183     transformPrinter.reset(new PrintTransforms(llvm::outs()));
184 
185   for (unsigned i=0, e = transforms.size(); i != e; ++i) {
186     bool err = migration.applyTransform(transforms[i], transformPrinter.get());
187     if (err) return true;
188 
189     if (VerboseOpt) {
190       if (i == e-1)
191         llvm::errs() << "\n##### FINAL RESULT #####\n";
192       else
193         llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
194       printResult(migration.getRemapper(), llvm::errs());
195       llvm::errs() << "\n##########################\n\n";
196     }
197   }
198 
199   if (!OutputTransformations)
200     printResult(migration.getRemapper(), llvm::outs());
201 
202   // FIXME: TestResultForARC
203 
204   return false;
205 }
206 
207 static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
208   using namespace llvm;
209 
210   ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(
211       fname1, /*FileSize=*/-1, /*RequiresNullTerminator=*/true,
212       /*IsVolatile=*/false, /*IsText=*/true);
213   if (!file1)
214     return false;
215 
216   ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(
217       fname2, /*FileSize=*/-1, /*RequiresNullTerminator=*/true,
218       /*IsVolatile=*/false, /*IsText=*/true);
219   if (!file2)
220     return false;
221 
222   return file1.get()->getBuffer() == file2.get()->getBuffer();
223 }
224 
225 static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
226   using namespace llvm;
227 
228   assert(!resultFiles.empty());
229 
230   std::map<StringRef, StringRef> resultMap;
231 
232   for (ArrayRef<std::string>::iterator
233          I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
234     StringRef fname(*I);
235     if (!fname.endswith(".result")) {
236       errs() << "error: filename '" << fname
237                    << "' does not have '.result' extension\n";
238       return true;
239     }
240     resultMap[sys::path::stem(fname)] = fname;
241   }
242 
243   ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
244   if (RemappingsFile.empty())
245     inputBuf = MemoryBuffer::getSTDIN();
246   else
247     inputBuf = MemoryBuffer::getFile(RemappingsFile, /*FileSize=*/-1,
248                                      /*RequiresNullTerminator=*/true,
249                                      /*IsVolatile=*/false, /*IsText=*/true);
250   if (!inputBuf) {
251     errs() << "error: could not read remappings input\n";
252     return true;
253   }
254 
255   SmallVector<StringRef, 8> strs;
256   inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
257                                     /*KeepEmpty=*/false);
258 
259   if (strs.empty()) {
260     errs() << "error: no files to verify from stdin\n";
261     return true;
262   }
263   if (strs.size() % 2 != 0) {
264     errs() << "error: files to verify are not original/result pairs\n";
265     return true;
266   }
267 
268   for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
269     StringRef inputOrigFname = strs[i];
270     StringRef inputResultFname = strs[i+1];
271 
272     std::map<StringRef, StringRef>::iterator It;
273     It = resultMap.find(sys::path::filename(inputOrigFname));
274     if (It == resultMap.end()) {
275       errs() << "error: '" << inputOrigFname << "' is not in the list of "
276              << "transformed files to verify\n";
277       return true;
278     }
279 
280     if (!sys::fs::exists(It->second)) {
281       errs() << "error: '" << It->second << "' does not exist\n";
282       return true;
283     }
284     if (!sys::fs::exists(inputResultFname)) {
285       errs() << "error: '" << inputResultFname << "' does not exist\n";
286       return true;
287     }
288 
289     if (!filesCompareEqual(It->second, inputResultFname)) {
290       errs() << "error: '" << It->second << "' is different than "
291              << "'" << inputResultFname << "'\n";
292       return true;
293     }
294 
295     resultMap.erase(It);
296   }
297 
298   if (!resultMap.empty()) {
299     for (std::map<StringRef, StringRef>::iterator
300            I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
301       errs() << "error: '" << I->second << "' was not verified!\n";
302     return true;
303   }
304 
305   return false;
306 }
307 
308 //===----------------------------------------------------------------------===//
309 // Misc. functions.
310 //===----------------------------------------------------------------------===//
311 
312 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
313                                 raw_ostream &OS) {
314   SourceManager &SM = Ctx.getSourceManager();
315   PresumedLoc PL = SM.getPresumedLoc(loc);
316 
317   OS << llvm::sys::path::filename(PL.getFilename());
318   OS << ":" << PL.getLine() << ":"
319             << PL.getColumn();
320 }
321 
322 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
323                              raw_ostream &OS) {
324   SourceManager &SM = Ctx.getSourceManager();
325   const LangOptions &langOpts = Ctx.getLangOpts();
326 
327   PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
328 
329   OS << llvm::sys::path::filename(PL.getFilename());
330   OS << " [" << PL.getLine() << ":"
331              << PL.getColumn();
332   OS << " - ";
333 
334   SourceLocation end = range.getEnd();
335   PL = SM.getPresumedLoc(end);
336 
337   unsigned endCol = PL.getColumn() - 1;
338   if (!range.isTokenRange())
339     endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
340   OS << PL.getLine() << ":" << endCol << "]";
341 }
342 
343 //===----------------------------------------------------------------------===//
344 // Command line processing.
345 //===----------------------------------------------------------------------===//
346 
347 int main(int argc, const char **argv) {
348   void *MainAddr = (void*) (intptr_t) GetExecutablePath;
349   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
350 
351   std::string
352     resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
353 
354   int optargc = 0;
355   for (; optargc != argc; ++optargc) {
356     if (StringRef(argv[optargc]) == "--args")
357       break;
358   }
359   llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
360 
361   if (VerifyTransformedFiles) {
362     if (ResultFiles.empty()) {
363       llvm::cl::PrintHelpMessage();
364       return 1;
365     }
366     return verifyTransformedFiles(ResultFiles);
367   }
368 
369   if (optargc == argc) {
370     llvm::cl::PrintHelpMessage();
371     return 1;
372   }
373 
374   ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
375 
376   if (CheckOnly)
377     return checkForMigration(resourcesPath, Args);
378 
379   return performTransformations(resourcesPath, Args);
380 }
381