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