blob: b597383dfd2b1de488202f94cf30bd202edf6b80 [file] [log] [blame]
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +00001//===- 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
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000014#include "clang-c/Index.h"
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000015#include "CXString.h"
16#include "clang/ARCMigrate/ARCMT.h"
17#include "clang/Frontend/TextDiagnosticBuffer.h"
18#include "llvm/Support/FileSystem.h"
19
20using namespace clang;
21using namespace arcmt;
22
23namespace {
24
25struct Remap {
26 std::vector<std::pair<std::string, std::string> > Vec;
27};
28
29} // anonymous namespace.
30
31//===----------------------------------------------------------------------===//
32// libClang public APIs.
33//===----------------------------------------------------------------------===//
34
35extern "C" {
36
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000037CXRemapping clang_getRemappings(const char *migrate_dir_path) {
Alp Tokerf55a3062014-07-14 22:17:16 +000038#ifndef CLANG_ENABLE_ARCMT
39 llvm::errs() << "error: feature not enabled in this build\n";
40 return nullptr;
41#else
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000042 bool Logging = ::getenv("LIBCLANG_LOGGING");
43
44 if (!migrate_dir_path) {
45 if (Logging)
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000046 llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
Craig Topper69186e72014-06-08 08:38:04 +000047 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000048 }
49
Rafael Espindola611505f2014-09-11 18:10:13 +000050 if (!llvm::sys::fs::exists(migrate_dir_path)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000051 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000052 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000053 << "\")\n";
54 llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n";
55 }
Craig Topper69186e72014-06-08 08:38:04 +000056 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000057 }
58
59 TextDiagnosticBuffer diagBuffer;
Ahmed Charlesb8984322014-03-07 20:03:18 +000060 std::unique_ptr<Remap> remap(new Remap());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000061
62 bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer);
63
64 if (err) {
65 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000066 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000067 << "\")\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 }
Craig Topper69186e72014-06-08 08:38:04 +000072 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000073 }
74
Ahmed Charles9a16beb2014-03-07 19:33:25 +000075 return remap.release();
Alp Tokerf55a3062014-07-14 22:17:16 +000076#endif
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000077}
78
Ted Kremenekf7639e12012-03-06 20:06:33 +000079CXRemapping clang_getRemappingsFromFileList(const char **filePaths,
80 unsigned numFiles) {
Alp Tokerf55a3062014-07-14 22:17:16 +000081#ifndef CLANG_ENABLE_ARCMT
82 llvm::errs() << "error: feature not enabled in this build\n";
83 return nullptr;
84#else
Ted Kremenekf7639e12012-03-06 20:06:33 +000085 bool Logging = ::getenv("LIBCLANG_LOGGING");
86
Ahmed Charlesb8984322014-03-07 20:03:18 +000087 std::unique_ptr<Remap> remap(new Remap());
Ted Kremenekf7639e12012-03-06 20:06:33 +000088
89 if (numFiles == 0) {
90 if (Logging)
91 llvm::errs() << "clang_getRemappingsFromFileList was called with "
92 "numFiles=0\n";
Ahmed Charles9a16beb2014-03-07 19:33:25 +000093 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +000094 }
95
96 if (!filePaths) {
97 if (Logging)
98 llvm::errs() << "clang_getRemappingsFromFileList was called with "
99 "NULL filePaths\n";
Craig Topper69186e72014-06-08 08:38:04 +0000100 return nullptr;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000101 }
102
103 TextDiagnosticBuffer diagBuffer;
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000104 SmallVector<StringRef, 32> Files(filePaths, filePaths + numFiles);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000105
106 bool err = arcmt::getFileRemappingsFromFileList(remap->Vec, Files,
107 &diagBuffer);
108
109 if (err) {
110 if (Logging) {
111 llvm::errs() << "Error by clang_getRemappingsFromFileList\n";
112 for (TextDiagnosticBuffer::const_iterator
113 I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
114 llvm::errs() << I->second << '\n';
115 }
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000116 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +0000117 }
118
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000119 return remap.release();
Alp Tokerf55a3062014-07-14 22:17:16 +0000120#endif
Ted Kremenekf7639e12012-03-06 20:06:33 +0000121}
122
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000123unsigned clang_remap_getNumFiles(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000124 return static_cast<Remap *>(map)->Vec.size();
125
126}
127
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000128void clang_remap_getFilenames(CXRemapping map, unsigned index,
129 CXString *original, CXString *transformed) {
130 if (original)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000131 *original = cxstring::createDup(
132 static_cast<Remap *>(map)->Vec[index].first);
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000133 if (transformed)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000134 *transformed = cxstring::createDup(
135 static_cast<Remap *>(map)->Vec[index].second);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000136}
137
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000138void clang_remap_dispose(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000139 delete static_cast<Remap *>(map);
140}
141
142} // end: extern "C"