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" |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4 | |
| 5 | using namespace clang; |
| 6 | using namespace clang::tooling; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 7 | |
| 8 | extern "C" { |
| 9 | |
| 10 | // FIXME: do something more usefull with the error message |
| 11 | CXCompilationDatabase |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 12 | clang_CompilationDatabase_fromDirectory(const char *BuildDir, |
| 13 | CXCompilationDatabase_Error *ErrorCode) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 14 | { |
| 15 | std::string ErrorMsg; |
| 16 | CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError; |
| 17 | |
| 18 | CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir, |
| 19 | ErrorMsg); |
| 20 | |
| 21 | if (!db) { |
| 22 | fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str()); |
| 23 | Err = CXCompilationDatabase_CanNotLoadDatabase; |
| 24 | } |
| 25 | |
| 26 | if (ErrorCode) |
| 27 | *ErrorCode = Err; |
| 28 | |
| 29 | return db; |
| 30 | } |
| 31 | |
| 32 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 33 | clang_CompilationDatabase_dispose(CXCompilationDatabase CDb) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 34 | { |
| 35 | delete static_cast<CompilationDatabase *>(CDb); |
| 36 | } |
| 37 | |
| 38 | struct AllocatedCXCompileCommands |
| 39 | { |
| 40 | std::vector<CompileCommand> CCmd; |
| 41 | |
| 42 | AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd) |
| 43 | : CCmd(Cmd) |
| 44 | { } |
| 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)) { |
| 52 | const std::vector<CompileCommand> |
| 53 | CCmd(db->getCompileCommands(CompleteFileName)); |
| 54 | if (!CCmd.empty()) |
| 55 | return new AllocatedCXCompileCommands( CCmd ); |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 61 | CXCompileCommands |
| 62 | clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) { |
| 63 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
| 64 | const std::vector<CompileCommand> CCmd(db->getAllCompileCommands()); |
| 65 | if (!CCmd.empty()) |
| 66 | return new AllocatedCXCompileCommands( CCmd ); |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 72 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 73 | clang_CompileCommands_dispose(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 74 | { |
| 75 | delete static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 76 | } |
| 77 | |
| 78 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 79 | clang_CompileCommands_getSize(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 80 | { |
| 81 | if (!Cmds) |
| 82 | return 0; |
| 83 | |
| 84 | AllocatedCXCompileCommands *ACC = |
| 85 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 86 | |
| 87 | return ACC->CCmd.size(); |
| 88 | } |
| 89 | |
| 90 | CXCompileCommand |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 91 | clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 92 | { |
| 93 | if (!Cmds) |
| 94 | return 0; |
| 95 | |
| 96 | AllocatedCXCompileCommands *ACC = |
| 97 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 98 | |
| 99 | if (I >= ACC->CCmd.size()) |
| 100 | return 0; |
| 101 | |
| 102 | return &ACC->CCmd[I]; |
| 103 | } |
| 104 | |
| 105 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 106 | clang_CompileCommand_getDirectory(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 107 | { |
| 108 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 109 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 110 | |
| 111 | CompileCommand *cmd = static_cast<CompileCommand *>(CCmd); |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 112 | return cxstring::createRef(cmd->Directory.c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 116 | clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 117 | { |
| 118 | if (!CCmd) |
| 119 | return 0; |
| 120 | |
| 121 | return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); |
| 122 | } |
| 123 | |
| 124 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 125 | clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 126 | { |
| 127 | if (!CCmd) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 128 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 129 | |
| 130 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 131 | |
| 132 | if (Arg >= Cmd->CommandLine.size()) |
Dmitri Gribenko | dad4c1a | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 133 | return cxstring::createNull(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 134 | |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 135 | return cxstring::createRef(Cmd->CommandLine[Arg].c_str()); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | } // end: extern "C" |