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 | |
| 11 | // FIXME: do something more usefull with the error message |
| 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 | |
| 19 | CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir, |
| 20 | ErrorMsg); |
| 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 | |
| 30 | return db; |
| 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 | |
| 43 | AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd) |
| 44 | : CCmd(Cmd) |
| 45 | { } |
| 46 | }; |
| 47 | |
| 48 | CXCompileCommands |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 49 | clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, |
| 50 | const char *CompleteFileName) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 51 | { |
| 52 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
| 53 | const std::vector<CompileCommand> |
| 54 | CCmd(db->getCompileCommands(CompleteFileName)); |
| 55 | if (!CCmd.empty()) |
| 56 | return new AllocatedCXCompileCommands( CCmd ); |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 62 | CXCompileCommands |
| 63 | clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) { |
| 64 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
| 65 | const std::vector<CompileCommand> CCmd(db->getAllCompileCommands()); |
| 66 | if (!CCmd.empty()) |
| 67 | return new AllocatedCXCompileCommands( CCmd ); |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 73 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 74 | clang_CompileCommands_dispose(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 75 | { |
| 76 | delete static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 77 | } |
| 78 | |
| 79 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 80 | clang_CompileCommands_getSize(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 81 | { |
| 82 | if (!Cmds) |
| 83 | return 0; |
| 84 | |
| 85 | AllocatedCXCompileCommands *ACC = |
| 86 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 87 | |
| 88 | return ACC->CCmd.size(); |
| 89 | } |
| 90 | |
| 91 | CXCompileCommand |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 92 | clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 93 | { |
| 94 | if (!Cmds) |
| 95 | return 0; |
| 96 | |
| 97 | AllocatedCXCompileCommands *ACC = |
| 98 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 99 | |
| 100 | if (I >= ACC->CCmd.size()) |
| 101 | return 0; |
| 102 | |
| 103 | return &ACC->CCmd[I]; |
| 104 | } |
| 105 | |
| 106 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 107 | clang_CompileCommand_getDirectory(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 108 | { |
| 109 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 110 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 111 | |
| 112 | CompileCommand *cmd = static_cast<CompileCommand *>(CCmd); |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 113 | return cxstring::createRef(cmd->Directory.c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 117 | clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 118 | { |
| 119 | if (!CCmd) |
| 120 | return 0; |
| 121 | |
| 122 | return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); |
| 123 | } |
| 124 | |
| 125 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 126 | clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 127 | { |
| 128 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 129 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 130 | |
| 131 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 132 | |
| 133 | if (Arg >= Cmd->CommandLine.size()) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 134 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 135 | |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 136 | return cxstring::createRef(Cmd->CommandLine[Arg].c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | } // end: extern "C" |