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