blob: ed2ecdb29e7ab6bf780e7ff005d5f2dc69fb3d80 [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"
NAKAMURA Takumi7f633df2017-07-18 08:55:03 +000017#include "clang/Config/config.h"
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000018#include "clang/Frontend/TextDiagnosticBuffer.h"
19#include "llvm/Support/FileSystem.h"
20
21using namespace clang;
22using namespace arcmt;
23
24namespace {
25
26struct Remap {
27 std::vector<std::pair<std::string, std::string> > Vec;
28};
29
30} // anonymous namespace.
31
32//===----------------------------------------------------------------------===//
33// libClang public APIs.
34//===----------------------------------------------------------------------===//
35
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000036CXRemapping clang_getRemappings(const char *migrate_dir_path) {
NAKAMURA Takumid9739822017-10-18 05:21:17 +000037#if !CLANG_ENABLE_ARCMT
Alp Tokerf55a3062014-07-14 22:17:16 +000038 llvm::errs() << "error: feature not enabled in this build\n";
39 return nullptr;
40#else
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000041 bool Logging = ::getenv("LIBCLANG_LOGGING");
42
43 if (!migrate_dir_path) {
44 if (Logging)
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000045 llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
Craig Topper69186e72014-06-08 08:38:04 +000046 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000047 }
48
Rafael Espindola611505f2014-09-11 18:10:13 +000049 if (!llvm::sys::fs::exists(migrate_dir_path)) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000050 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000051 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000052 << "\")\n";
53 llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n";
54 }
Craig Topper69186e72014-06-08 08:38:04 +000055 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000056 }
57
58 TextDiagnosticBuffer diagBuffer;
Ahmed Charlesb8984322014-03-07 20:03:18 +000059 std::unique_ptr<Remap> remap(new Remap());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000060
61 bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer);
62
63 if (err) {
64 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000065 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000066 << "\")\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 }
Craig Topper69186e72014-06-08 08:38:04 +000071 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000072 }
73
Ahmed Charles9a16beb2014-03-07 19:33:25 +000074 return remap.release();
Alp Tokerf55a3062014-07-14 22:17:16 +000075#endif
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000076}
77
Ted Kremenekf7639e12012-03-06 20:06:33 +000078CXRemapping clang_getRemappingsFromFileList(const char **filePaths,
79 unsigned numFiles) {
NAKAMURA Takumid9739822017-10-18 05:21:17 +000080#if !CLANG_ENABLE_ARCMT
Alp Tokerf55a3062014-07-14 22:17:16 +000081 llvm::errs() << "error: feature not enabled in this build\n";
82 return nullptr;
83#else
Ted Kremenekf7639e12012-03-06 20:06:33 +000084 bool Logging = ::getenv("LIBCLANG_LOGGING");
85
Ahmed Charlesb8984322014-03-07 20:03:18 +000086 std::unique_ptr<Remap> remap(new Remap());
Ted Kremenekf7639e12012-03-06 20:06:33 +000087
88 if (numFiles == 0) {
89 if (Logging)
90 llvm::errs() << "clang_getRemappingsFromFileList was called with "
91 "numFiles=0\n";
Ahmed Charles9a16beb2014-03-07 19:33:25 +000092 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +000093 }
94
95 if (!filePaths) {
96 if (Logging)
97 llvm::errs() << "clang_getRemappingsFromFileList was called with "
98 "NULL filePaths\n";
Craig Topper69186e72014-06-08 08:38:04 +000099 return nullptr;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000100 }
101
102 TextDiagnosticBuffer diagBuffer;
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000103 SmallVector<StringRef, 32> Files(filePaths, filePaths + numFiles);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000104
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 }
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000115 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +0000116 }
117
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000118 return remap.release();
Alp Tokerf55a3062014-07-14 22:17:16 +0000119#endif
Ted Kremenekf7639e12012-03-06 20:06:33 +0000120}
121
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000122unsigned clang_remap_getNumFiles(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000123 return static_cast<Remap *>(map)->Vec.size();
124
125}
126
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000127void clang_remap_getFilenames(CXRemapping map, unsigned index,
128 CXString *original, CXString *transformed) {
129 if (original)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000130 *original = cxstring::createDup(
131 static_cast<Remap *>(map)->Vec[index].first);
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000132 if (transformed)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000133 *transformed = cxstring::createDup(
134 static_cast<Remap *>(map)->Vec[index].second);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000135}
136
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000137void clang_remap_dispose(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000138 delete static_cast<Remap *>(map);
139}