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/Frontend/PCHContainerOperations.h"
11#include "clang/ARCMigrate/ARCMT.h"
12#include "clang/Frontend/ASTUnit.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 "llvm/Support/FileSystem.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Signals.h"
20#include <system_error>
21
22using namespace clang;
23using namespace arcmt;
24
25static llvm::cl::opt<bool>
26CheckOnly("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
33static llvm::cl::opt<bool>
34OutputTransformations("output-transformations",
35                      llvm::cl::desc("Print the source transformations"));
36
37static llvm::cl::opt<bool>
38VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
39
40static llvm::cl::opt<bool>
41VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
42
43static llvm::cl::opt<bool>
44VerifyTransformedFiles("verify-transformed-files",
45llvm::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
49static llvm::cl::opt<std::string>
50RemappingsFile("remappings-file",
51               llvm::cl::desc("Pairs of file mappings (typically the output of "
52               "c-arcmt-test)"));
53
54static llvm::cl::list<std::string>
55ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>..."));
56
57static 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).
65std::string 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::fs::getMainExecutable(Argv0, MainAddr);
70}
71
72static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
73                                raw_ostream &OS);
74static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
75                             raw_ostream &OS);
76
77namespace {
78
79class PrintTransforms : public MigrationProcess::RewriteListener {
80  ASTContext *Ctx;
81  raw_ostream &OS;
82
83public:
84  PrintTransforms(raw_ostream &OS)
85    : Ctx(nullptr), OS(OS) {}
86
87  void start(ASTContext &ctx) override { Ctx = &ctx; }
88  void finish() override { Ctx = nullptr; }
89
90  void insert(SourceLocation loc, StringRef text) override {
91    assert(Ctx);
92    OS << "Insert: ";
93    printSourceLocation(loc, *Ctx, OS);
94    OS << " \"" << text << "\"\n";
95  }
96
97  void remove(CharSourceRange range) override {
98    assert(Ctx);
99    OS << "Remove: ";
100    printSourceRange(range, *Ctx, OS);
101    OS << '\n';
102  }
103};
104
105} // anonymous namespace
106
107static 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 = nullptr;
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                              std::make_shared<PCHContainerOperations>(),
136                              Diags->getClient());
137  return Diags->getClient()->getNumErrors() > 0;
138}
139
140static void printResult(FileRemapper &remapper, raw_ostream &OS) {
141  PreprocessorOptions PPOpts;
142  remapper.applyMappings(PPOpts);
143  // The changed files will be in memory buffers, print them.
144  for (const auto &RB : PPOpts.RemappedFileBuffers)
145    OS << RB.second->getBuffer();
146}
147
148static 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.begin(), Args.end(),
163                                     *TopDiags))
164    return true;
165
166  if (origCI.getFrontendOpts().Inputs.empty()) {
167    llvm::errs() << "error: no input files\n";
168    return true;
169  }
170
171  if (!origCI.getLangOpts()->ObjC1)
172    return false;
173
174  MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
175                             DiagClient);
176
177  std::vector<TransformFn>
178    transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
179                                 origCI.getMigratorOpts().NoFinalizeRemoval);
180  assert(!transforms.empty());
181
182  std::unique_ptr<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
208static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
209  using namespace llvm;
210
211  ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
212  if (!file1)
213    return false;
214
215  ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
216  if (!file2)
217    return false;
218
219  return file1.get()->getBuffer() == file2.get()->getBuffer();
220}
221
222static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
223  using namespace llvm;
224
225  assert(!resultFiles.empty());
226
227  std::map<StringRef, StringRef> resultMap;
228
229  for (ArrayRef<std::string>::iterator
230         I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
231    StringRef fname(*I);
232    if (!fname.endswith(".result")) {
233      errs() << "error: filename '" << fname
234                   << "' does not have '.result' extension\n";
235      return true;
236    }
237    resultMap[sys::path::stem(fname)] = fname;
238  }
239
240  ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
241  if (RemappingsFile.empty())
242    inputBuf = MemoryBuffer::getSTDIN();
243  else
244    inputBuf = MemoryBuffer::getFile(RemappingsFile);
245  if (!inputBuf) {
246    errs() << "error: could not read remappings input\n";
247    return true;
248  }
249
250  SmallVector<StringRef, 8> strs;
251  inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
252                                    /*KeepEmpty=*/false);
253
254  if (strs.empty()) {
255    errs() << "error: no files to verify from stdin\n";
256    return true;
257  }
258  if (strs.size() % 2 != 0) {
259    errs() << "error: files to verify are not original/result pairs\n";
260    return true;
261  }
262
263  for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
264    StringRef inputOrigFname = strs[i];
265    StringRef inputResultFname = strs[i+1];
266
267    std::map<StringRef, StringRef>::iterator It;
268    It = resultMap.find(sys::path::filename(inputOrigFname));
269    if (It == resultMap.end()) {
270      errs() << "error: '" << inputOrigFname << "' is not in the list of "
271             << "transformed files to verify\n";
272      return true;
273    }
274
275    if (!sys::fs::exists(It->second)) {
276      errs() << "error: '" << It->second << "' does not exist\n";
277      return true;
278    }
279    if (!sys::fs::exists(inputResultFname)) {
280      errs() << "error: '" << inputResultFname << "' does not exist\n";
281      return true;
282    }
283
284    if (!filesCompareEqual(It->second, inputResultFname)) {
285      errs() << "error: '" << It->second << "' is different than "
286             << "'" << inputResultFname << "'\n";
287      return true;
288    }
289
290    resultMap.erase(It);
291  }
292
293  if (!resultMap.empty()) {
294    for (std::map<StringRef, StringRef>::iterator
295           I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
296      errs() << "error: '" << I->second << "' was not verified!\n";
297    return true;
298  }
299
300  return false;
301}
302
303//===----------------------------------------------------------------------===//
304// Misc. functions.
305//===----------------------------------------------------------------------===//
306
307static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
308                                raw_ostream &OS) {
309  SourceManager &SM = Ctx.getSourceManager();
310  PresumedLoc PL = SM.getPresumedLoc(loc);
311
312  OS << llvm::sys::path::filename(PL.getFilename());
313  OS << ":" << PL.getLine() << ":"
314            << PL.getColumn();
315}
316
317static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
318                             raw_ostream &OS) {
319  SourceManager &SM = Ctx.getSourceManager();
320  const LangOptions &langOpts = Ctx.getLangOpts();
321
322  PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
323
324  OS << llvm::sys::path::filename(PL.getFilename());
325  OS << " [" << PL.getLine() << ":"
326             << PL.getColumn();
327  OS << " - ";
328
329  SourceLocation end = range.getEnd();
330  PL = SM.getPresumedLoc(end);
331
332  unsigned endCol = PL.getColumn() - 1;
333  if (!range.isTokenRange())
334    endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
335  OS << PL.getLine() << ":" << endCol << "]";
336}
337
338//===----------------------------------------------------------------------===//
339// Command line processing.
340//===----------------------------------------------------------------------===//
341
342int main(int argc, const char **argv) {
343  void *MainAddr = (void*) (intptr_t) GetExecutablePath;
344  llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
345
346  std::string
347    resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
348
349  int optargc = 0;
350  for (; optargc != argc; ++optargc) {
351    if (StringRef(argv[optargc]) == "--args")
352      break;
353  }
354  llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
355
356  if (VerifyTransformedFiles) {
357    if (ResultFiles.empty()) {
358      llvm::cl::PrintHelpMessage();
359      return 1;
360    }
361    return verifyTransformedFiles(ResultFiles);
362  }
363
364  if (optargc == argc) {
365    llvm::cl::PrintHelpMessage();
366    return 1;
367  }
368
369  ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
370
371  if (CheckOnly)
372    return checkForMigration(resourcesPath, Args);
373
374  return performTransformations(resourcesPath, Args);
375}
376