CIndexUSRs.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- CIndexUSRs.cpp - Clang-C Source Indexing Library -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the generation and use of USRs from CXEntities.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CIndexer.h"
  13. #include "CXCursor.h"
  14. #include "CXString.h"
  15. #include "CXTranslationUnit.h"
  16. #include "clang/Frontend/ASTUnit.h"
  17. #include "clang/Index/USRGeneration.h"
  18. #include "clang/Lex/PreprocessingRecord.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace clang;
  22. using namespace clang::index;
  23. //===----------------------------------------------------------------------===//
  24. // API hooks.
  25. //===----------------------------------------------------------------------===//
  26. static inline StringRef extractUSRSuffix(StringRef s) {
  27. return s.startswith("c:") ? s.substr(2) : "";
  28. }
  29. bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
  30. return generateUSRForDecl(D, Buf);
  31. }
  32. CXString clang_getCursorUSR(CXCursor C) {
  33. const CXCursorKind &K = clang_getCursorKind(C);
  34. if (clang_isDeclaration(K)) {
  35. const Decl *D = cxcursor::getCursorDecl(C);
  36. if (!D)
  37. return cxstring::createEmpty();
  38. CXTranslationUnit TU = cxcursor::getCursorTU(C);
  39. if (!TU)
  40. return cxstring::createEmpty();
  41. cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
  42. if (!buf)
  43. return cxstring::createEmpty();
  44. bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
  45. if (Ignore) {
  46. buf->dispose();
  47. return cxstring::createEmpty();
  48. }
  49. // Return the C-string, but don't make a copy since it is already in
  50. // the string buffer.
  51. buf->Data.push_back('\0');
  52. return createCXString(buf);
  53. }
  54. if (K == CXCursor_MacroDefinition) {
  55. CXTranslationUnit TU = cxcursor::getCursorTU(C);
  56. if (!TU)
  57. return cxstring::createEmpty();
  58. cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
  59. if (!buf)
  60. return cxstring::createEmpty();
  61. bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C),
  62. cxtu::getASTUnit(TU)->getSourceManager(),
  63. buf->Data);
  64. if (Ignore) {
  65. buf->dispose();
  66. return cxstring::createEmpty();
  67. }
  68. // Return the C-string, but don't make a copy since it is already in
  69. // the string buffer.
  70. buf->Data.push_back('\0');
  71. return createCXString(buf);
  72. }
  73. return cxstring::createEmpty();
  74. }
  75. CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
  76. SmallString<128> Buf(getUSRSpacePrefix());
  77. llvm::raw_svector_ostream OS(Buf);
  78. OS << extractUSRSuffix(clang_getCString(classUSR));
  79. generateUSRForObjCIvar(name, OS);
  80. return cxstring::createDup(OS.str());
  81. }
  82. CXString clang_constructUSR_ObjCMethod(const char *name,
  83. unsigned isInstanceMethod,
  84. CXString classUSR) {
  85. SmallString<128> Buf(getUSRSpacePrefix());
  86. llvm::raw_svector_ostream OS(Buf);
  87. OS << extractUSRSuffix(clang_getCString(classUSR));
  88. generateUSRForObjCMethod(name, isInstanceMethod, OS);
  89. return cxstring::createDup(OS.str());
  90. }
  91. CXString clang_constructUSR_ObjCClass(const char *name) {
  92. SmallString<128> Buf(getUSRSpacePrefix());
  93. llvm::raw_svector_ostream OS(Buf);
  94. generateUSRForObjCClass(name, OS);
  95. return cxstring::createDup(OS.str());
  96. }
  97. CXString clang_constructUSR_ObjCProtocol(const char *name) {
  98. SmallString<128> Buf(getUSRSpacePrefix());
  99. llvm::raw_svector_ostream OS(Buf);
  100. generateUSRForObjCProtocol(name, OS);
  101. return cxstring::createDup(OS.str());
  102. }
  103. CXString clang_constructUSR_ObjCCategory(const char *class_name,
  104. const char *category_name) {
  105. SmallString<128> Buf(getUSRSpacePrefix());
  106. llvm::raw_svector_ostream OS(Buf);
  107. generateUSRForObjCCategory(class_name, category_name, OS);
  108. return cxstring::createDup(OS.str());
  109. }
  110. CXString clang_constructUSR_ObjCProperty(const char *property,
  111. CXString classUSR) {
  112. SmallString<128> Buf(getUSRSpacePrefix());
  113. llvm::raw_svector_ostream OS(Buf);
  114. OS << extractUSRSuffix(clang_getCString(classUSR));
  115. generateUSRForObjCProperty(property, /*isClassProp=*/false, OS);
  116. return cxstring::createDup(OS.str());
  117. }