CXCompilationDatabase.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "clang-c/CXCompilationDatabase.h"
  2. #include "CXString.h"
  3. #include "clang/Tooling/CompilationDatabase.h"
  4. #include <cstdio>
  5. using namespace clang;
  6. using namespace clang::tooling;
  7. // FIXME: do something more useful with the error message
  8. CXCompilationDatabase
  9. clang_CompilationDatabase_fromDirectory(const char *BuildDir,
  10. CXCompilationDatabase_Error *ErrorCode)
  11. {
  12. std::string ErrorMsg;
  13. CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
  14. std::unique_ptr<CompilationDatabase> db =
  15. CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
  16. if (!db) {
  17. fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
  18. Err = CXCompilationDatabase_CanNotLoadDatabase;
  19. }
  20. if (ErrorCode)
  21. *ErrorCode = Err;
  22. return db.release();
  23. }
  24. void
  25. clang_CompilationDatabase_dispose(CXCompilationDatabase CDb)
  26. {
  27. delete static_cast<CompilationDatabase *>(CDb);
  28. }
  29. struct AllocatedCXCompileCommands
  30. {
  31. std::vector<CompileCommand> CCmd;
  32. AllocatedCXCompileCommands(std::vector<CompileCommand> Cmd)
  33. : CCmd(std::move(Cmd)) {}
  34. };
  35. CXCompileCommands
  36. clang_CompilationDatabase_getCompileCommands(CXCompilationDatabase CDb,
  37. const char *CompleteFileName)
  38. {
  39. if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
  40. std::vector<CompileCommand> CCmd(db->getCompileCommands(CompleteFileName));
  41. if (!CCmd.empty())
  42. return new AllocatedCXCompileCommands(std::move(CCmd));
  43. }
  44. return nullptr;
  45. }
  46. CXCompileCommands
  47. clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) {
  48. if (CompilationDatabase *db = static_cast<CompilationDatabase *>(CDb)) {
  49. std::vector<CompileCommand> CCmd(db->getAllCompileCommands());
  50. if (!CCmd.empty())
  51. return new AllocatedCXCompileCommands(std::move(CCmd));
  52. }
  53. return nullptr;
  54. }
  55. void
  56. clang_CompileCommands_dispose(CXCompileCommands Cmds)
  57. {
  58. delete static_cast<AllocatedCXCompileCommands *>(Cmds);
  59. }
  60. unsigned
  61. clang_CompileCommands_getSize(CXCompileCommands Cmds)
  62. {
  63. if (!Cmds)
  64. return 0;
  65. AllocatedCXCompileCommands *ACC =
  66. static_cast<AllocatedCXCompileCommands *>(Cmds);
  67. return ACC->CCmd.size();
  68. }
  69. CXCompileCommand
  70. clang_CompileCommands_getCommand(CXCompileCommands Cmds, unsigned I)
  71. {
  72. if (!Cmds)
  73. return nullptr;
  74. AllocatedCXCompileCommands *ACC =
  75. static_cast<AllocatedCXCompileCommands *>(Cmds);
  76. if (I >= ACC->CCmd.size())
  77. return nullptr;
  78. return &ACC->CCmd[I];
  79. }
  80. CXString
  81. clang_CompileCommand_getDirectory(CXCompileCommand CCmd)
  82. {
  83. if (!CCmd)
  84. return cxstring::createNull();
  85. CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
  86. return cxstring::createRef(cmd->Directory.c_str());
  87. }
  88. CXString
  89. clang_CompileCommand_getFilename(CXCompileCommand CCmd)
  90. {
  91. if (!CCmd)
  92. return cxstring::createNull();
  93. CompileCommand *cmd = static_cast<CompileCommand *>(CCmd);
  94. return cxstring::createRef(cmd->Filename.c_str());
  95. }
  96. unsigned
  97. clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
  98. {
  99. if (!CCmd)
  100. return 0;
  101. return static_cast<CompileCommand *>(CCmd)->CommandLine.size();
  102. }
  103. CXString
  104. clang_CompileCommand_getArg(CXCompileCommand CCmd, unsigned Arg)
  105. {
  106. if (!CCmd)
  107. return cxstring::createNull();
  108. CompileCommand *Cmd = static_cast<CompileCommand *>(CCmd);
  109. if (Arg >= Cmd->CommandLine.size())
  110. return cxstring::createNull();
  111. return cxstring::createRef(Cmd->CommandLine[Arg].c_str());
  112. }
  113. unsigned
  114. clang_CompileCommand_getNumMappedSources(CXCompileCommand CCmd)
  115. {
  116. // Left here for backward compatibility. No mapped sources exists in the C++
  117. // backend anymore.
  118. return 0;
  119. }
  120. CXString
  121. clang_CompileCommand_getMappedSourcePath(CXCompileCommand CCmd, unsigned I)
  122. {
  123. // Left here for backward compatibility. No mapped sources exists in the C++
  124. // backend anymore.
  125. return cxstring::createNull();
  126. }
  127. CXString
  128. clang_CompileCommand_getMappedSourceContent(CXCompileCommand CCmd, unsigned I)
  129. {
  130. // Left here for backward compatibility. No mapped sources exists in the C++
  131. // backend anymore.
  132. return cxstring::createNull();
  133. }