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