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