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/AST/ASTContext.h"
12 #include "clang/Frontend/PCHContainerOperations.h"
13 #include "clang/Frontend/TextDiagnosticPrinter.h"
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Lex/PreprocessorOptions.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Signals.h"
22 #include <system_error>
23 
24 using namespace clang;
25 using namespace arcmt;
26 
27 static llvm::cl::opt<bool>
28 CheckOnly("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 
35 static llvm::cl::opt<bool>
36 OutputTransformations("output-transformations",
37                       llvm::cl::desc("Print the source transformations"));
38 
39 static llvm::cl::opt<bool>
40 VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
41 
42 static llvm::cl::opt<bool>
43 VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
44 
45 static llvm::cl::opt<bool>
46 VerifyTransformedFiles("verify-transformed-files",
47 llvm::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 
51 static llvm::cl::opt<std::string>
52 RemappingsFile("remappings-file",
53                llvm::cl::desc("Pairs of file mappings (typically the output of "
54                "c-arcmt-test)"));
55 
56 static llvm::cl::list<std::string>
57 ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
58 
59 static 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).
67 std::string GetExecutablePath(const char *Argv0) {
68   // 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;
71   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
72 }
73 
74 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
75                                 raw_ostream &OS);
76 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
77                              raw_ostream &OS);
78 
79 namespace {
80 
81 class PrintTransforms : public MigrationProcess::RewriteListener {
82   ASTContext *Ctx;
83   raw_ostream &OS;
84 
85 public:
86   PrintTransforms(raw_ostream &OS)
87     : Ctx(nullptr), OS(OS) {}
88 
89   void start(ASTContext &ctx) override { Ctx = &ctx; }
90   void finish() override { Ctx = nullptr; }
91 
92   void insert(SourceLocation loc, StringRef text) override {
93     assert(Ctx);
94     OS << "Insert: ";
95     printSourceLocation(loc, *Ctx, OS);
96     OS << " \"" << text << "\"\n";
97   }
98 
99   void remove(CharSourceRange range) override {
100     assert(Ctx);
101     OS << "Remove: ";
102     printSourceRange(range, *Ctx, OS);
103     OS << '\n';
104   }
105 };
106 
107 } // anonymous namespace
108 
109 static bool checkForMigration(StringRef resourcesPath,
110                               ArrayRef<const char *> Args) {
111   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
112   DiagnosticConsumer *DiagClient =
113     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
114   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
115   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
116       new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
117   // Chain in -verify checker, if requested.
118   VerifyDiagnosticConsumer *verifyDiag = nullptr;
119   if (VerifyDiags) {
120     verifyDiag = new VerifyDiagnosticConsumer(*Diags);
121     Diags->setClient(verifyDiag);
122   }
123 
124   CompilerInvocation CI;
125   if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
126     return true;
127 
128   if (CI.getFrontendOpts().Inputs.empty()) {
129     llvm::errs() << "error: no input files\n";
130     return true;
131   }
132 
133   if (!CI.getLangOpts()->ObjC1)
134     return false;
135 
136   arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
137                               std::make_shared<PCHContainerOperations>(),
138                               Diags->getClient());
139   return Diags->getClient()->getNumErrors() > 0;
140 }
141 
142 static void printResult(FileRemapper &remapper, raw_ostream &OS) {
143   PreprocessorOptions PPOpts;
144   remapper.applyMappings(PPOpts);
145   // The changed files will be in memory buffers, print them.
146   for (const auto &RB : PPOpts.RemappedFileBuffers)
147     OS << RB.second->getBuffer();
148 }
149 
150 static bool performTransformations(StringRef resourcesPath,
151                                    ArrayRef<const char *> Args) {
152   // Check first.
153   if (checkForMigration(resourcesPath, Args))
154     return true;
155 
156   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
157   DiagnosticConsumer *DiagClient =
158     new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
159   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
160   IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
161       new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
162 
163   CompilerInvocation origCI;
164   if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
165                                      *TopDiags))
166     return true;
167 
168   if (origCI.getFrontendOpts().Inputs.empty()) {
169     llvm::errs() << "error: no input files\n";
170     return true;
171   }
172 
173   if (!origCI.getLangOpts()->ObjC1)
174     return false;
175 
176   MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
177                              DiagClient);
178 
179   std::vector<TransformFn>
180     transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
181                                  origCI.getMigratorOpts().NoFinalizeRemoval);
182   assert(!transforms.empty());
183 
184   std::unique_ptr<PrintTransforms> transformPrinter;
185   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 
210 static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
211   using namespace llvm;
212 
213   ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
214   if (!file1)
215     return false;
216 
217   ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
218   if (!file2)
219     return false;
220 
221   return file1.get()->getBuffer() == file2.get()->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   ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
243   if (RemappingsFile.empty())
244     inputBuf = MemoryBuffer::getSTDIN();
245   else
246     inputBuf = MemoryBuffer::getFile(RemappingsFile);
247   if (!inputBuf) {
248     errs() << "error: could not read remappings input\n";
249     return true;
250   }
251 
252   SmallVector<StringRef, 8> strs;
253   inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
254                                     /*KeepEmpty=*/false);
255 
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 
277     if (!sys::fs::exists(It->second)) {
278       errs() << "error: '" << It->second << "' does not exist\n";
279       return true;
280     }
281     if (!sys::fs::exists(inputResultFname)) {
282       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 
305 //===----------------------------------------------------------------------===//
306 // Misc. functions.
307 //===----------------------------------------------------------------------===//
308 
309 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
310                                 raw_ostream &OS) {
311   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 
319 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
320                              raw_ostream &OS) {
321   SourceManager &SM = Ctx.getSourceManager();
322   const LangOptions &langOpts = Ctx.getLangOpts();
323 
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 
344 int main(int argc, const char **argv) {
345   void *MainAddr = (void*) (intptr_t) GetExecutablePath;
346   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
347 
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   }
356   llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
357 
358   if (VerifyTransformedFiles) {
359     if (ResultFiles.empty()) {
360       llvm::cl::PrintHelpMessage();
361       return 1;
362     }
363     return verifyTransformedFiles(ResultFiles);
364   }
365 
366   if (optargc == argc) {
367     llvm::cl::PrintHelpMessage();
368     return 1;
369   }
370 
371   ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
372 
373   if (CheckOnly)
374     return checkForMigration(resourcesPath, Args);
375 
376   return performTransformations(resourcesPath, Args);
377 }
378