Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 1 | #include "clang-c/CXCompilationDatabase.h" |
| 2 | #include "clang/Tooling/CompilationDatabase.h" |
| 3 | #include "CXString.h" |
| 4 | |
| 5 | using namespace clang; |
| 6 | using namespace clang::tooling; |
| 7 | using namespace clang::cxstring; |
| 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 | |
| 62 | void |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 63 | clang_CompileCommands_dispose(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 64 | { |
| 65 | delete static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 66 | } |
| 67 | |
| 68 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 69 | clang_CompileCommands_getSize(CXCompileCommands Cmds) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 70 | { |
| 71 | if (!Cmds) |
| 72 | return 0; |
| 73 | |
| 74 | AllocatedCXCompileCommands *ACC = |
| 75 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 76 | |
| 77 | return ACC->CCmd.size(); |
| 78 | } |
| 79 | |
| 80 | CXCompileCommand |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 81 | clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 82 | { |
| 83 | if (!Cmds) |
| 84 | return 0; |
| 85 | |
| 86 | AllocatedCXCompileCommands *ACC = |
| 87 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 88 | |
| 89 | if (I >= ACC->CCmd.size()) |
| 90 | return 0; |
| 91 | |
| 92 | return &ACC->CCmd[I]; |
| 93 | } |
| 94 | |
| 95 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 96 | clang_CompileCommand_getDirectory(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 97 | { |
| 98 | if (!CCmd) |
| 99 | return createCXString((const char*)NULL); |
| 100 | |
| 101 | CompileCommand *cmd = static_cast<CompileCommand *>(CCmd); |
| 102 | return createCXString(cmd->Directory); |
| 103 | } |
| 104 | |
| 105 | unsigned |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 106 | clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 107 | { |
| 108 | if (!CCmd) |
| 109 | return 0; |
| 110 | |
| 111 | return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); |
| 112 | } |
| 113 | |
| 114 | CXString |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 115 | clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 116 | { |
| 117 | if (!CCmd) |
| 118 | return createCXString((const char*)NULL); |
| 119 | |
| 120 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 121 | |
| 122 | if (Arg >= Cmd->CommandLine.size()) |
| 123 | return createCXString((const char*)NULL); |
| 124 | |
| 125 | return createCXString(Cmd->CommandLine[Arg]); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | } // end: extern "C" |