ARCMigrate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "clang-c/Index.h"
  13. #include "CXString.h"
  14. #include "clang/ARCMigrate/ARCMT.h"
  15. #include "clang/Config/config.h"
  16. #include "clang/Frontend/TextDiagnosticBuffer.h"
  17. #include "llvm/Support/FileSystem.h"
  18. using namespace clang;
  19. using namespace arcmt;
  20. namespace {
  21. struct Remap {
  22. std::vector<std::pair<std::string, std::string> > Vec;
  23. };
  24. } // anonymous namespace.
  25. //===----------------------------------------------------------------------===//
  26. // libClang public APIs.
  27. //===----------------------------------------------------------------------===//
  28. CXRemapping clang_getRemappings(const char *migrate_dir_path) {
  29. #if !CLANG_ENABLE_ARCMT
  30. llvm::errs() << "error: feature not enabled in this build\n";
  31. return nullptr;
  32. #else
  33. bool Logging = ::getenv("LIBCLANG_LOGGING");
  34. if (!migrate_dir_path) {
  35. if (Logging)
  36. llvm::errs() << "clang_getRemappings was called with NULL parameter\n";
  37. return nullptr;
  38. }
  39. if (!llvm::sys::fs::exists(migrate_dir_path)) {
  40. if (Logging) {
  41. llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
  42. << "\")\n";
  43. llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n";
  44. }
  45. return nullptr;
  46. }
  47. TextDiagnosticBuffer diagBuffer;
  48. std::unique_ptr<Remap> remap(new Remap());
  49. bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer);
  50. if (err) {
  51. if (Logging) {
  52. llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path
  53. << "\")\n";
  54. for (TextDiagnosticBuffer::const_iterator
  55. I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
  56. llvm::errs() << I->second << '\n';
  57. }
  58. return nullptr;
  59. }
  60. return remap.release();
  61. #endif
  62. }
  63. CXRemapping clang_getRemappingsFromFileList(const char **filePaths,
  64. unsigned numFiles) {
  65. #if !CLANG_ENABLE_ARCMT
  66. llvm::errs() << "error: feature not enabled in this build\n";
  67. return nullptr;
  68. #else
  69. bool Logging = ::getenv("LIBCLANG_LOGGING");
  70. std::unique_ptr<Remap> remap(new Remap());
  71. if (numFiles == 0) {
  72. if (Logging)
  73. llvm::errs() << "clang_getRemappingsFromFileList was called with "
  74. "numFiles=0\n";
  75. return remap.release();
  76. }
  77. if (!filePaths) {
  78. if (Logging)
  79. llvm::errs() << "clang_getRemappingsFromFileList was called with "
  80. "NULL filePaths\n";
  81. return nullptr;
  82. }
  83. TextDiagnosticBuffer diagBuffer;
  84. SmallVector<StringRef, 32> Files(filePaths, filePaths + numFiles);
  85. bool err = arcmt::getFileRemappingsFromFileList(remap->Vec, Files,
  86. &diagBuffer);
  87. if (err) {
  88. if (Logging) {
  89. llvm::errs() << "Error by clang_getRemappingsFromFileList\n";
  90. for (TextDiagnosticBuffer::const_iterator
  91. I = diagBuffer.err_begin(), E = diagBuffer.err_end(); I != E; ++I)
  92. llvm::errs() << I->second << '\n';
  93. }
  94. return remap.release();
  95. }
  96. return remap.release();
  97. #endif
  98. }
  99. unsigned clang_remap_getNumFiles(CXRemapping map) {
  100. return static_cast<Remap *>(map)->Vec.size();
  101. }
  102. void clang_remap_getFilenames(CXRemapping map, unsigned index,
  103. CXString *original, CXString *transformed) {
  104. if (original)
  105. *original = cxstring::createDup(
  106. static_cast<Remap *>(map)->Vec[index].first);
  107. if (transformed)
  108. *transformed = cxstring::createDup(
  109. static_cast<Remap *>(map)->Vec[index].second);
  110. }
  111. void clang_remap_dispose(CXRemapping map) {
  112. delete static_cast<Remap *>(map);
  113. }