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