1 //===- ARCMigrate.cpp - Clang-C ARC Migration Library ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the main API hooks in the Clang-C ARC Migration library.
11 //===----------------------------------------------------------------------===//
13 #include "clang-c/Index.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"
20 using namespace clang
;
21 using namespace arcmt
;
26 std::vector
<std::pair
<std::string
, std::string
> > Vec
;
29 } // anonymous namespace.
31 //===----------------------------------------------------------------------===//
32 // libClang public APIs.
33 //===----------------------------------------------------------------------===//
35 CXRemapping
clang_getRemappings(const char *migrate_dir_path
) {
36 #if !CLANG_ENABLE_ARCMT
37 llvm::errs() << "error: feature not enabled in this build\n";
40 bool Logging
= ::getenv("LIBCLANG_LOGGING");
42 if (!migrate_dir_path
) {
44 llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
48 if (!llvm::sys::fs::exists(migrate_dir_path
)) {
50 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
52 llvm::errs() << "\"" << migrate_dir_path
<< "\" does not exist\n";
57 TextDiagnosticBuffer diagBuffer
;
58 std::unique_ptr
<Remap
> remap(new Remap());
60 bool err
= arcmt::getFileRemappings(remap
->Vec
, migrate_dir_path
,&diagBuffer
);
64 llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
66 for (TextDiagnosticBuffer::const_iterator
67 I
= diagBuffer
.err_begin(), E
= diagBuffer
.err_end(); I
!= E
; ++I
)
68 llvm::errs() << I
->second
<< '\n';
73 return remap
.release();
77 CXRemapping
clang_getRemappingsFromFileList(const char **filePaths
,
79 #if !CLANG_ENABLE_ARCMT
80 llvm::errs() << "error: feature not enabled in this build\n";
83 bool Logging
= ::getenv("LIBCLANG_LOGGING");
85 std::unique_ptr
<Remap
> remap(new Remap());
89 llvm::errs() << "clang_getRemappingsFromFileList was called with "
91 return remap
.release();
96 llvm::errs() << "clang_getRemappingsFromFileList was called with "
101 TextDiagnosticBuffer diagBuffer
;
102 SmallVector
<StringRef
, 32> Files(filePaths
, filePaths
+ numFiles
);
104 bool err
= arcmt::getFileRemappingsFromFileList(remap
->Vec
, Files
,
109 llvm::errs() << "Error by clang_getRemappingsFromFileList\n";
110 for (TextDiagnosticBuffer::const_iterator
111 I
= diagBuffer
.err_begin(), E
= diagBuffer
.err_end(); I
!= E
; ++I
)
112 llvm::errs() << I
->second
<< '\n';
114 return remap
.release();
117 return remap
.release();
121 unsigned clang_remap_getNumFiles(CXRemapping map
) {
122 return static_cast<Remap
*>(map
)->Vec
.size();
126 void clang_remap_getFilenames(CXRemapping map
, unsigned index
,
127 CXString
*original
, CXString
*transformed
) {
129 *original
= cxstring::createDup(
130 static_cast<Remap
*>(map
)->Vec
[index
].first
);
132 *transformed
= cxstring::createDup(
133 static_cast<Remap
*>(map
)->Vec
[index
].second
);
136 void clang_remap_dispose(CXRemapping map
) {
137 delete static_cast<Remap
*>(map
);