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