CXCompilationDatabase.cpp revision c70851b74c06b27df07b9a772656a2e663d432ee
1#include "clang-c/CXCompilationDatabase.h" 2#include "clang/Tooling/CompilationDatabase.h" 3#include "CXString.h" 4 5using namespace clang; 6using namespace clang::tooling; 7using namespace clang::cxstring; 8 9extern "C" { 10 11// FIXME: do something more usefull with the error message 12CXCompilationDatabase 13clang_CompilationDatabase_fromDirectory(const char *BuildDir, 14 CXCompilationDatabase_Error *ErrorCode) 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 33void 34clang_CompilationDatabase_dispose(CXCompilationDatabase CDb) 35{ 36 delete static_cast<CompilationDatabase *>(CDb); 37} 38 39struct AllocatedCXCompileCommands 40{ 41 std::vector<CompileCommand> CCmd; 42 43 AllocatedCXCompileCommands(const std::vector<CompileCommand>& Cmd) 44 : CCmd(Cmd) 45 { } 46}; 47 48CXCompileCommands 49clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb, 50 const char *CompleteFileName) 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 62void 63clang_CompileCommands_dispose(CXCompileCommands Cmds) 64{ 65 delete static_cast<AllocatedCXCompileCommands *>(Cmds); 66} 67 68unsigned 69clang_CompileCommands_getSize(CXCompileCommands Cmds) 70{ 71 if (!Cmds) 72 return 0; 73 74 AllocatedCXCompileCommands *ACC = 75 static_cast<AllocatedCXCompileCommands *>(Cmds); 76 77 return ACC->CCmd.size(); 78} 79 80CXCompileCommand 81clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I) 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 95CXString 96clang_CompileCommand_getDirectory(CXCompileCommand CCmd) 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 105unsigned 106clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) 107{ 108 if (!CCmd) 109 return 0; 110 111 return static_cast<CompileCommand *>(CCmd)->CommandLine.size(); 112} 113 114CXString 115clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg) 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" 130