Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 1 | //===- 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 Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 14 | #include "clang-c/Index.h" |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CXString.h" |
| 17 | #include "clang/ARCMigrate/ARCMT.h" |
| 18 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 19 | #include "llvm/Support/FileSystem.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace arcmt; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | struct Remap { |
| 27 | std::vector<std::pair<std::string, std::string> > Vec; |
| 28 | }; |
| 29 | |
| 30 | } // anonymous namespace. |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // libClang public APIs. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | extern "C" { |
| 37 | |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 38 | CXRemapping clang_getRemappings(const char *migrate_dir_path) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 39 | bool Logging = ::getenv("LIBCLANG_LOGGING"); |
| 40 | |
| 41 | if (!migrate_dir_path) { |
| 42 | if (Logging) |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 43 | llvm::errs() << "clang_getRemappings was called with NULL parameter\n"; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | bool exists = false; |
| 48 | llvm::sys::fs::exists(migrate_dir_path, exists); |
| 49 | if (!exists) { |
| 50 | if (Logging) { |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 51 | llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 52 | << "\")\n"; |
| 53 | llvm::errs() << "\"" << migrate_dir_path << "\" does not exist\n"; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | TextDiagnosticBuffer diagBuffer; |
| 59 | llvm::OwningPtr<Remap> remap(new Remap()); |
| 60 | |
| 61 | bool err = arcmt::getFileRemappings(remap->Vec, migrate_dir_path,&diagBuffer); |
| 62 | |
| 63 | if (err) { |
| 64 | if (Logging) { |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 65 | llvm::errs() << "Error by clang_getRemappings(\"" << migrate_dir_path |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 66 | << "\")\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 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return remap.take(); |
| 75 | } |
| 76 | |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 77 | unsigned clang_remap_getNumFiles(CXRemapping map) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 78 | return static_cast<Remap *>(map)->Vec.size(); |
| 79 | |
| 80 | } |
| 81 | |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 82 | void clang_remap_getFilenames(CXRemapping map, unsigned index, |
| 83 | CXString *original, CXString *transformed) { |
| 84 | if (original) |
| 85 | *original = cxstring::createCXString( |
| 86 | static_cast<Remap *>(map)->Vec[index].first, |
| 87 | /*DupString =*/ true); |
| 88 | if (transformed) |
| 89 | *transformed = cxstring::createCXString( |
| 90 | static_cast<Remap *>(map)->Vec[index].second, |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 91 | /*DupString =*/ true); |
| 92 | } |
| 93 | |
Argyrios Kyrtzidis | 97c337c | 2011-07-11 20:15:00 +0000 | [diff] [blame^] | 94 | void clang_remap_dispose(CXRemapping map) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 95 | delete static_cast<Remap *>(map); |
| 96 | } |
| 97 | |
| 98 | } // end: extern "C" |