1 //===-- 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"
13 #include "clang/Frontend/Utils.h"
14 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/PathV1.h"
19 #include "llvm/Support/Signals.h"
20 #include "llvm/Support/system_error.h"
21 
22 using namespace clang;
23 using namespace arcmt;
24 
25 static llvm::cl::opt<bool>
26 CheckOnly("check-only",
27       llvm::cl::desc("Just check for issues that need to be handled manually"));
28 
29 //static llvm::cl::opt<bool>
30 //TestResultForARC("test-result",
31 //llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
32 
33 static llvm::cl::opt<bool>
34 OutputTransformations("output-transformations",
35                       llvm::cl::desc("Print the source transformations"));
36 
37 static llvm::cl::opt<bool>
38 VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
39 
40 static llvm::cl::opt<bool>
41 VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
42 
43 static llvm::cl::opt<bool>
44 VerifyTransformedFiles("verify-transformed-files",
45 llvm::cl::desc("Read pairs of file mappings (typically the output of "
46                "c-arcmt-test) and compare their contents with the filenames "
47                "provided in command-line"));
48 
49 static llvm::cl::opt<std::string>
50 RemappingsFile("remappings-file",
51                llvm::cl::desc("Pairs of file mappings (typically the output of "
52                "c-arcmt-test)"));
53 
54 static llvm::cl::list<std::string>
55 ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
56 
57 static llvm::cl::extrahelp extraHelp(
58   "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
59 
60 // This function isn't referenced outside its translation unit, but it
61 // can't use the "static" keyword because its address is used for
62 // GetMainExecutable (since some platforms don't support taking the
63 // address of main, and some platforms can't implement GetMainExecutable
64 // without being given the address of a function in the main executable).
65 llvm::sys::Path GetExecutablePath(const char *Argv0) {
66   // This just needs to be some symbol in the binary; C++ doesn't
67   // allow taking the address of ::main however.
68   void *MainAddr = (void*) (intptr_t) GetExecutablePath;
69   return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
70 }
71 
72 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
73                                 raw_ostream &OS);
74 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
75                              raw_ostream &OS);
76 
77 namespace {
78 
79 class PrintTransforms : public MigrationProcess::RewriteListener {
80   ASTContext *Ctx;
81   raw_ostream &OS;
82 
83 public:
84   PrintTransforms(raw_ostream &OS)
85     : Ctx(0), OS(OS) { }
86 
87   virtual void start(ASTContext &ctx) { Ctx = &ctx; }
88   virtual void finish() { Ctx = 0; }
89 
90   virtual void insert(SourceLocation loc, StringRef text) {
91     assert(Ctx);
92     OS << "Insert: ";
93     printSourceLocation(loc, *Ctx, OS);
94     OS << " \"" << text << "\"\n";
95   }
96 
97   virtual void remove(CharSourceRange range) {
98     assert(Ctx);
99     OS << "Remove: ";
100     printSourceRange(range, *Ctx, OS);
101     OS << '\n';
102   }
103 };
104 
105 } // anonymous namespace
106 
107 static bool checkForMigration(StringRef resourcesPath,
108                               ArrayRef<const char *> Args) {
109   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
110   DiagnosticConsumer *DiagClient =
111     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
112   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
113   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
114       new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
115   // Chain in -verify checker, if requested.
116   VerifyDiagnosticConsumer *verifyDiag = 0;
117   if (VerifyDiags) {
118     verifyDiag = new VerifyDiagnosticConsumer(*Diags);
119     Diags->setClient(verifyDiag);
120   }
121 
122   CompilerInvocation CI;
123   if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
124     return true;
125 
126   if (CI.getFrontendOpts().Inputs.empty()) {
127     llvm::errs() << "error: no input files\n";
128     return true;
129   }
130 
131   if (!CI.getLangOpts()->ObjC1)
132     return false;
133 
134   arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
135                               Diags->getClient());
136   return Diags->getClient()->getNumErrors() > 0;
137 }
138 
139 static void printResult(FileRemapper &remapper, raw_ostream &OS) {
140   PreprocessorOptions PPOpts;
141   remapper.applyMappings(PPOpts);
142   // The changed files will be in memory buffers, print them.
143   for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
144     const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
145     OS << mem->getBuffer();
146   }
147 }
148 
149 static bool performTransformations(StringRef resourcesPath,
150                                    ArrayRef<const char *> Args) {
151   // Check first.
152   if (checkForMigration(resourcesPath, Args))
153     return true;
154 
155   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
156   DiagnosticConsumer *DiagClient =
157     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
158   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
159   IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
160       new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
161 
162   CompilerInvocation origCI;
163   if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
164                                      *TopDiags))
165     return true;
166 
167   if (origCI.getFrontendOpts().Inputs.empty()) {
168     llvm::errs() << "error: no input files\n";
169     return true;
170   }
171 
172   if (!origCI.getLangOpts()->ObjC1)
173     return false;
174 
175   MigrationProcess migration(origCI, DiagClient);
176 
177   std::vector<TransformFn>
178     transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
179                                  origCI.getMigratorOpts().NoFinalizeRemoval);
180   assert(!transforms.empty());
181 
182   OwningPtr<PrintTransforms> transformPrinter;
183   if (OutputTransformations)
184     transformPrinter.reset(new PrintTransforms(llvm::outs()));
185 
186   for (unsigned i=0, e = transforms.size(); i != e; ++i) {
187     bool err = migration.applyTransform(transforms[i], transformPrinter.get());
188     if (err) return true;
189 
190     if (VerboseOpt) {
191       if (i == e-1)
192         llvm::errs() << "\n##### FINAL RESULT #####\n";
193       else
194         llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
195       printResult(migration.getRemapper(), llvm::errs());
196       llvm::errs() << "\n##########################\n\n";
197     }
198   }
199 
200   if (!OutputTransformations)
201     printResult(migration.getRemapper(), llvm::outs());
202 
203   // FIXME: TestResultForARC
204 
205   return false;
206 }
207 
208 static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
209   using namespace llvm;
210 
211   OwningPtr<MemoryBuffer> file1;
212   MemoryBuffer::getFile(fname1, file1);
213   if (!file1)
214     return false;
215 
216   OwningPtr<MemoryBuffer> file2;
217   MemoryBuffer::getFile(fname2, file2);
218   if (!file2)
219     return false;
220 
221   return file1->getBuffer() == file2->getBuffer();
222 }
223 
224 static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
225   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 
242   OwningPtr<MemoryBuffer> inputBuf;
243   if (RemappingsFile.empty())
244     MemoryBuffer::getSTDIN(inputBuf);
245   else
246     MemoryBuffer::getFile(RemappingsFile, inputBuf);
247   if (!inputBuf) {
248     errs() << "error: could not read remappings input\n";
249     return true;
250   }
251 
252   SmallVector<StringRef, 8> strs;
253   inputBuf->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
254 
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 
276     bool exists = false;
277     sys::fs::exists(It->second, exists);
278     if (!exists) {
279       errs() << "error: '" << It->second << "' does not exist\n";
280       return true;
281     }
282     sys::fs::exists(inputResultFname, exists);
283     if (!exists) {
284       errs() << "error: '" << inputResultFname << "' does not exist\n";
285       return true;
286     }
287 
288     if (!filesCompareEqual(It->second, inputResultFname)) {
289       errs() << "error: '" << It->second << "' is different than "
290              << "'" << inputResultFname << "'\n";
291       return true;
292     }
293 
294     resultMap.erase(It);
295   }
296 
297   if (!resultMap.empty()) {
298     for (std::map<StringRef, StringRef>::iterator
299            I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
300       errs() << "error: '" << I->second << "' was not verified!\n";
301     return true;
302   }
303 
304   return false;
305 }
306 
307 //===----------------------------------------------------------------------===//
308 // Misc. functions.
309 //===----------------------------------------------------------------------===//
310 
311 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
312                                 raw_ostream &OS) {
313   SourceManager &SM = Ctx.getSourceManager();
314   PresumedLoc PL = SM.getPresumedLoc(loc);
315 
316   OS << llvm::sys::path::filename(PL.getFilename());
317   OS << ":" << PL.getLine() << ":"
318             << PL.getColumn();
319 }
320 
321 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
322                              raw_ostream &OS) {
323   SourceManager &SM = Ctx.getSourceManager();
324   const LangOptions &langOpts = Ctx.getLangOpts();
325 
326   PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
327 
328   OS << llvm::sys::path::filename(PL.getFilename());
329   OS << " [" << PL.getLine() << ":"
330              << PL.getColumn();
331   OS << " - ";
332 
333   SourceLocation end = range.getEnd();
334   PL = SM.getPresumedLoc(end);
335 
336   unsigned endCol = PL.getColumn() - 1;
337   if (!range.isTokenRange())
338     endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
339   OS << PL.getLine() << ":" << endCol << "]";
340 }
341 
342 //===----------------------------------------------------------------------===//
343 // Command line processing.
344 //===----------------------------------------------------------------------===//
345 
346 int main(int argc, const char **argv) {
347   void *MainAddr = (void*) (intptr_t) GetExecutablePath;
348   llvm::sys::PrintStackTraceOnErrorSignal();
349 
350   std::string
351     resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
352 
353   int optargc = 0;
354   for (; optargc != argc; ++optargc) {
355     if (StringRef(argv[optargc]) == "--args")
356       break;
357   }
358   llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
359 
360   if (VerifyTransformedFiles) {
361     if (ResultFiles.empty()) {
362       llvm::cl::PrintHelpMessage();
363       return 1;
364     }
365     return verifyTransformedFiles(ResultFiles);
366   }
367 
368   if (optargc == argc) {
369     llvm::cl::PrintHelpMessage();
370     return 1;
371   }
372 
373   ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
374 
375   if (CheckOnly)
376     return checkForMigration(resourcesPath, Args);
377 
378   return performTransformations(resourcesPath, Args);
379 }
380