Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 1 | #include "clang-c/CXCompilationDatabase.h" |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 2 | #include "CXString.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 3 | #include "clang/Tooling/CompilationDatabase.h" |
Dmitri Gribenko | 9af20d8 | 2013-08-19 16:14:33 +0000 | [diff] [blame] | 4 | #include <cstdio> |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 5 | |
| 6 | using namespace clang; |
| 7 | using namespace clang::tooling; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 8 | |
| 9 | extern "C" { |
| 10 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11 | // FIXME: do something more useful with the error message |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 12 | CXCompilationDatabase |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 13 | clang_CompilationDatabase_fromDirectory(const char *BuildDir, |
| 14 | CXCompilationDatabase_Error *ErrorCode) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 15 | { |
| 16 | std::string ErrorMsg; |
| 17 | CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError; |
| 18 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 19 | std::unique_ptr<CompilationDatabase> db = |
| 20 | CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 21 | |
| 22 | if (!db) { |
| 23 | fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str()); |
| 24 | Err = CXCompilationDatabase_CanNotLoadDatabase; |
| 25 | } |
| 26 | |
| 27 | if (ErrorCode) |
| 28 | *ErrorCode = Err; |
| 29 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 30 | return db.release(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 34 | clang_CompilationDatabase_dispose(CXCompilationDatabase CDb) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 35 | { |
| 36 | delete static_cast<CompilationDatabase *>(CDb); |
| 37 | } |
| 38 | |
| 39 | struct AllocatedCXCompileCommands |
| 40 | { |
| 41 | std::vector<CompileCommand> CCmd; |
| 42 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 43 | AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd) |
| 44 | : CCmd(std::move(Cmd)) {} |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | CXCompileCommands |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 48 | clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, |
| 49 | const char *CompleteFileName) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 50 | { |
| 51 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 52 | std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName)); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 53 | if (!CCmd.empty()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 54 | return new AllocatedCXCompileCommands(std::move(CCmd)); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 57 | return nullptr; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 60 | CXCompileCommands |
| 61 | clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) { |
| 62 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 63 | std::vector<CompileCommand> CCmd(db->getAllCompileCommands()); |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 64 | if (!CCmd.empty()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 65 | return new AllocatedCXCompileCommands(std::move(CCmd)); |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 68 | return nullptr; |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 71 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 72 | clang_CompileCommands_dispose(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 73 | { |
| 74 | delete static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 75 | } |
| 76 | |
| 77 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 78 | clang_CompileCommands_getSize(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 79 | { |
| 80 | if (!Cmds) |
| 81 | return 0; |
| 82 | |
| 83 | AllocatedCXCompileCommands *ACC = |
| 84 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 85 | |
| 86 | return ACC->CCmd.size(); |
| 87 | } |
| 88 | |
| 89 | CXCompileCommand |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 90 | clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 91 | { |
| 92 | if (!Cmds) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 93 | return nullptr; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 94 | |
| 95 | AllocatedCXCompileCommands *ACC = |
| 96 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 97 | |
| 98 | if (I >= ACC->CCmd.size()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 99 | return nullptr; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 100 | |
| 101 | return &ACC->CCmd[I]; |
| 102 | } |
| 103 | |
| 104 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 105 | clang_CompileCommand_getDirectory(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 106 | { |
| 107 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 108 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 109 | |
| 110 | CompileCommand *cmd = static_cast<CompileCommand *>(CCmd); |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 111 | return cxstring::createRef(cmd->Directory.c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 115 | clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 116 | { |
| 117 | if (!CCmd) |
| 118 | return 0; |
| 119 | |
| 120 | return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); |
| 121 | } |
| 122 | |
| 123 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 124 | clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 125 | { |
| 126 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 127 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 128 | |
| 129 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 130 | |
| 131 | if (Arg >= Cmd->CommandLine.size()) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 132 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 133 | |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 134 | return cxstring::createRef(Cmd->CommandLine[Arg].c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Manuel Klimek | f88536a | 2013-11-13 13:23:27 +0000 | [diff] [blame] | 137 | unsigned |
| 138 | clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd) |
| 139 | { |
| 140 | if (!CCmd) |
| 141 | return 0; |
| 142 | |
| 143 | return static_cast<CompileCommand *>(CCmd)->MappedSources.size(); |
| 144 | } |
| 145 | |
| 146 | CXString |
| 147 | clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I) |
| 148 | { |
| 149 | if (!CCmd) |
| 150 | return cxstring::createNull(); |
| 151 | |
| 152 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 153 | |
| 154 | if (I >= Cmd->MappedSources.size()) |
| 155 | return cxstring::createNull(); |
| 156 | |
| 157 | return cxstring::createRef(Cmd->MappedSources[I].first.c_str()); |
| 158 | } |
| 159 | |
| 160 | CXString |
| 161 | clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I) |
| 162 | { |
| 163 | if (!CCmd) |
| 164 | return cxstring::createNull(); |
| 165 | |
| 166 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 167 | |
| 168 | if (I >= Cmd->MappedSources.size()) |
| 169 | return cxstring::createNull(); |
| 170 | |
| 171 | return cxstring::createRef(Cmd->MappedSources[I].second.c_str()); |
| 172 | } |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 173 | |
| 174 | } // end: extern "C" |