blob: 6ddc22f474b38d954aba56c4260bc2ec6dc2711e [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) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000038 bool Logging = ::getenv("LIBCLANG_LOGGING");
39
40 if (!migrate_dir_path) {
41 if (Logging)
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000042 llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
Craig Topper69186e72014-06-08 08:38:04 +000043 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000044 }
45
46 bool exists = false;
47 llvm::sys::fs::exists(migrate_dir_path, exists);
48 if (!exists) {
49 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000050 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000051 << "\")\n";
52 llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n";
53 }
Craig Topper69186e72014-06-08 08:38:04 +000054 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000055 }
56
57 TextDiagnosticBuffer diagBuffer;
Ahmed Charlesb8984322014-03-07 20:03:18 +000058 std::unique_ptr<Remap> remap(new Remap());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000059
60 bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer);
61
62 if (err) {
63 if (Logging) {
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +000064 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000065 << "\")\n";
66 for (TextDiagnosticBuffer::const_iterator
67 I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
68 llvm::errs() << I->second << '\n';
69 }
Craig Topper69186e72014-06-08 08:38:04 +000070 return nullptr;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000071 }
72
Ahmed Charles9a16beb2014-03-07 19:33:25 +000073 return remap.release();
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000074}
75
Ted Kremenekf7639e12012-03-06 20:06:33 +000076CXRemapping clang_getRemappingsFromFileList(const char **filePaths,
77 unsigned numFiles) {
78 bool Logging = ::getenv("LIBCLANG_LOGGING");
79
Ahmed Charlesb8984322014-03-07 20:03:18 +000080 std::unique_ptr<Remap> remap(new Remap());
Ted Kremenekf7639e12012-03-06 20:06:33 +000081
82 if (numFiles == 0) {
83 if (Logging)
84 llvm::errs() << "clang_getRemappingsFromFileList was called with "
85 "numFiles=0\n";
Ahmed Charles9a16beb2014-03-07 19:33:25 +000086 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +000087 }
88
89 if (!filePaths) {
90 if (Logging)
91 llvm::errs() << "clang_getRemappingsFromFileList was called with "
92 "NULL filePaths\n";
Craig Topper69186e72014-06-08 08:38:04 +000093 return nullptr;
Ted Kremenekf7639e12012-03-06 20:06:33 +000094 }
95
96 TextDiagnosticBuffer diagBuffer;
97 SmallVector<StringRef, 32> Files;
98 for (unsigned i = 0; i != numFiles; ++i)
99 Files.push_back(filePaths[i]);
100
101 bool err = arcmt::getFileRemappingsFromFileList(remap->Vec, Files,
102 &diagBuffer);
103
104 if (err) {
105 if (Logging) {
106 llvm::errs() << "Error by clang_getRemappingsFromFileList\n";
107 for (TextDiagnosticBuffer::const_iterator
108 I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
109 llvm::errs() << I->second << '\n';
110 }
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000111 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +0000112 }
113
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000114 return remap.release();
Ted Kremenekf7639e12012-03-06 20:06:33 +0000115}
116
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000117unsigned clang_remap_getNumFiles(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000118 return static_cast<Remap *>(map)->Vec.size();
119
120}
121
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000122void clang_remap_getFilenames(CXRemapping map, unsigned index,
123 CXString *original, CXString *transformed) {
124 if (original)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000125 *original = cxstring::createDup(
126 static_cast<Remap *>(map)->Vec[index].first);
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000127 if (transformed)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000128 *transformed = cxstring::createDup(
129 static_cast<Remap *>(map)->Vec[index].second);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000130}
131
Argyrios Kyrtzidisf89cc692011-07-11 20:15:00 +0000132void clang_remap_dispose(CXRemapping map) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000133 delete static_cast<Remap *>(map);
134}
135
136} // end: extern "C"