Home | History | Annotate | Download | only in libclang
      1 //===- ARCMigrate.cpp - Clang-C ARC Migration Library ---------------------===//
      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 // This file implements the main API hooks in the Clang-C ARC Migration library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang-c/Index.h"
     15 #include "CXString.h"
     16 #include "clang/ARCMigrate/ARCMT.h"
     17 #include "clang/Frontend/TextDiagnosticBuffer.h"
     18 #include "llvm/Support/FileSystem.h"
     19 
     20 using namespace clang;
     21 using namespace arcmt;
     22 
     23 namespace {
     24 
     25 struct Remap {
     26   std::vector<std::pair<std::string, std::string> > Vec;
     27 };
     28 
     29 } // anonymous namespace.
     30 
     31 //===----------------------------------------------------------------------===//
     32 // libClang public APIs.
     33 //===----------------------------------------------------------------------===//
     34 
     35 extern "C" {
     36 
     37 CXRemapping clang_getRemappings(const char *migrate_dir_path) {
     38 #ifndef CLANG_ENABLE_ARCMT
     39   llvm::errs() << "error: feature not enabled in this build\n";
     40   return nullptr;
     41 #else
     42   bool Logging = ::getenv("LIBCLANG_LOGGING");
     43 
     44   if (!migrate_dir_path) {
     45     if (Logging)
     46       llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
     47     return nullptr;
     48   }
     49 
     50   if (!llvm::sys::fs::exists(migrate_dir_path)) {
     51     if (Logging) {
     52       llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
     53                    << "\")\n";
     54       llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n";
     55     }
     56     return nullptr;
     57   }
     58 
     59   TextDiagnosticBuffer diagBuffer;
     60   std::unique_ptr<Remap> remap(new Remap());
     61 
     62   bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer);
     63 
     64   if (err) {
     65     if (Logging) {
     66       llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
     67                    << "\")\n";
     68       for (TextDiagnosticBuffer::const_iterator
     69              I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
     70         llvm::errs() << I->second << '\n';
     71     }
     72     return nullptr;
     73   }
     74 
     75   return remap.release();
     76 #endif
     77 }
     78 
     79 CXRemapping clang_getRemappingsFromFileList(const char **filePaths,
     80                                             unsigned numFiles) {
     81 #ifndef CLANG_ENABLE_ARCMT
     82   llvm::errs() << "error: feature not enabled in this build\n";
     83   return nullptr;
     84 #else
     85   bool Logging = ::getenv("LIBCLANG_LOGGING");
     86 
     87   std::unique_ptr<Remap> remap(new Remap());
     88 
     89   if (numFiles == 0) {
     90     if (Logging)
     91       llvm::errs() << "clang_getRemappingsFromFileList was called with "
     92                       "numFiles=0\n";
     93     return remap.release();
     94   }
     95 
     96   if (!filePaths) {
     97     if (Logging)
     98       llvm::errs() << "clang_getRemappingsFromFileList was called with "
     99                       "NULL filePaths\n";
    100     return nullptr;
    101   }
    102 
    103   TextDiagnosticBuffer diagBuffer;
    104   SmallVector<StringRef, 32> Files;
    105   for (unsigned i = 0; i != numFiles; ++i)
    106     Files.push_back(filePaths[i]);
    107 
    108   bool err = arcmt::getFileRemappingsFromFileList(remap->Vec, Files,
    109                                                   &diagBuffer);
    110 
    111   if (err) {
    112     if (Logging) {
    113       llvm::errs() << "Error by clang_getRemappingsFromFileList\n";
    114       for (TextDiagnosticBuffer::const_iterator
    115              I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
    116         llvm::errs() << I->second << '\n';
    117     }
    118     return remap.release();
    119   }
    120 
    121   return remap.release();
    122 #endif
    123 }
    124 
    125 unsigned clang_remap_getNumFiles(CXRemapping map) {
    126   return static_cast<Remap *>(map)->Vec.size();
    127 
    128 }
    129 
    130 void clang_remap_getFilenames(CXRemapping map, unsigned index,
    131                               CXString *original, CXString *transformed) {
    132   if (original)
    133     *original = cxstring::createDup(
    134                     static_cast<Remap *>(map)->Vec[index].first);
    135   if (transformed)
    136     *transformed = cxstring::createDup(
    137                     static_cast<Remap *>(map)->Vec[index].second);
    138 }
    139 
    140 void clang_remap_dispose(CXRemapping map) {
    141   delete static_cast<Remap *>(map);
    142 }
    143 
    144 } // end: extern "C"
    145