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 |
| 13 | clang_tooling_CompilationDatabase_fromDirectory( |
| 14 | const char *BuildDir, |
| 15 | CXCompilationDatabase_Error *ErrorCode) |
| 16 | { |
| 17 | std::string ErrorMsg; |
| 18 | CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError; |
| 19 | |
| 20 | CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir, |
| 21 | ErrorMsg); |
| 22 | |
| 23 | if (!db) { |
| 24 | fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str()); |
| 25 | Err = CXCompilationDatabase_CanNotLoadDatabase; |
| 26 | } |
| 27 | |
| 28 | if (ErrorCode) |
| 29 | *ErrorCode = Err; |
| 30 | |
| 31 | return db; |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | clang_tooling_CompilationDatabase_dispose(CXCompilationDatabase CDb) |
| 36 | { |
| 37 | delete static_cast<CompilationDatabase *>(CDb); |
| 38 | } |
| 39 | |
| 40 | struct AllocatedCXCompileCommands |
| 41 | { |
| 42 | std::vector<CompileCommand> CCmd; |
| 43 | |
| 44 | AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd) |
| 45 | : CCmd(Cmd) |
| 46 | { } |
| 47 | }; |
| 48 | |
| 49 | CXCompileCommands |
| 50 | clang_tooling_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, |
| 51 | const char *CompleteFileName) |
| 52 | { |
| 53 | if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) { |
| 54 | const std::vector<CompileCommand> |
| 55 | CCmd(db->getCompileCommands(CompleteFileName)); |
| 56 | if (!CCmd.empty()) |
| 57 | return new AllocatedCXCompileCommands( CCmd ); |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | clang_tooling_CompileCommands_dispose(CXCompileCommands Cmds) |
| 65 | { |
| 66 | delete static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 67 | } |
| 68 | |
| 69 | unsigned |
| 70 | clang_tooling_CompileCommands_getSize(CXCompileCommands Cmds) |
| 71 | { |
| 72 | if (!Cmds) |
| 73 | return 0; |
| 74 | |
| 75 | AllocatedCXCompileCommands *ACC = |
| 76 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 77 | |
| 78 | return ACC->CCmd.size(); |
| 79 | } |
| 80 | |
| 81 | CXCompileCommand |
| 82 | clang_tooling_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) |
| 83 | { |
| 84 | if (!Cmds) |
| 85 | return 0; |
| 86 | |
| 87 | AllocatedCXCompileCommands *ACC = |
| 88 | static_cast<AllocatedCXCompileCommands *>(Cmds); |
| 89 | |
| 90 | if (I >= ACC->CCmd.size()) |
| 91 | return 0; |
| 92 | |
| 93 | return &ACC->CCmd[I]; |
| 94 | } |
| 95 | |
| 96 | CXString |
| 97 | clang_tooling_CompileCommand_getDirectory(CXCompileCommand CCmd) |
| 98 | { |
| 99 | if (!CCmd) |
| 100 | return createCXString((const char*)NULL); |
| 101 | |
| 102 | CompileCommand *cmd = static_cast<CompileCommand *>(CCmd); |
| 103 | return createCXString(cmd->Directory); |
| 104 | } |
| 105 | |
| 106 | unsigned |
| 107 | clang_tooling_CompileCommand_getNumArgs(CXCompileCommand CCmd) |
| 108 | { |
| 109 | if (!CCmd) |
| 110 | return 0; |
| 111 | |
| 112 | return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); |
| 113 | } |
| 114 | |
| 115 | CXString |
| 116 | clang_tooling_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) |
| 117 | { |
| 118 | if (!CCmd) |
| 119 | return createCXString((const char*)NULL); |
| 120 | |
| 121 | CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd); |
| 122 | |
| 123 | if (Arg >= Cmd->CommandLine.size()) |
| 124 | return createCXString((const char*)NULL); |
| 125 | |
| 126 | return createCXString(Cmd->CommandLine[Arg]); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | } // end: extern "C" |