Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the generation and use of USRs from CXEntities. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CIndexer.h" |
| 15 | #include "CXCursor.h" |
| 16 | #include "CXString.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 17 | #include "CXTranslationUnit.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 18 | #include "clang/Frontend/ASTUnit.h" |
Argyrios Kyrtzidis | 2845a67 | 2013-08-17 00:40:41 +0000 | [diff] [blame] | 19 | #include "clang/Index/USRGeneration.h" |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PreprocessingRecord.h" |
| 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace clang; |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 25 | using namespace clang::index; |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 26 | |
| 27 | //===----------------------------------------------------------------------===// |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 28 | // API hooks. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | static inline StringRef extractUSRSuffix(StringRef s) { |
| 32 | return s.startswith("c:") ? s.substr(2) : ""; |
| 33 | } |
| 34 | |
| 35 | bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) { |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 36 | return generateUSRForDecl(D, Buf); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | extern "C" { |
| 40 | |
| 41 | CXString clang_getCursorUSR(CXCursor C) { |
| 42 | const CXCursorKind &K = clang_getCursorKind(C); |
| 43 | |
| 44 | if (clang_isDeclaration(K)) { |
Dmitri Gribenko | e22339c | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 45 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 46 | if (!D) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 47 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 48 | |
| 49 | CXTranslationUnit TU = cxcursor::getCursorTU(C); |
| 50 | if (!TU) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 51 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 52 | |
Dmitri Gribenko | aca3e56 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 53 | cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 54 | if (!buf) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 55 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 56 | |
| 57 | bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data); |
| 58 | if (Ignore) { |
Dmitri Gribenko | 9c48d16 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 59 | buf->dispose(); |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 60 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Return the C-string, but don't make a copy since it is already in |
| 64 | // the string buffer. |
| 65 | buf->Data.push_back('\0'); |
| 66 | return createCXString(buf); |
| 67 | } |
| 68 | |
| 69 | if (K == CXCursor_MacroDefinition) { |
| 70 | CXTranslationUnit TU = cxcursor::getCursorTU(C); |
| 71 | if (!TU) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 72 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 73 | |
Dmitri Gribenko | aca3e56 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 74 | cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 75 | if (!buf) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 76 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 77 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 78 | bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C), |
| 79 | cxtu::getASTUnit(TU)->getSourceManager(), |
| 80 | buf->Data); |
| 81 | if (Ignore) { |
| 82 | buf->dispose(); |
| 83 | return cxstring::createEmpty(); |
| 84 | } |
| 85 | |
| 86 | // Return the C-string, but don't make a copy since it is already in |
| 87 | // the string buffer. |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 88 | buf->Data.push_back('\0'); |
| 89 | return createCXString(buf); |
| 90 | } |
| 91 | |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 92 | return cxstring::createEmpty(); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 96 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 97 | llvm::raw_svector_ostream OS(Buf); |
| 98 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 99 | generateUSRForObjCIvar(name, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 100 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 104 | unsigned isInstanceMethod, |
| 105 | CXString classUSR) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 106 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 107 | llvm::raw_svector_ostream OS(Buf); |
| 108 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 109 | generateUSRForObjCMethod(name, isInstanceMethod, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 110 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | CXString clang_constructUSR_ObjCClass(const char *name) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 114 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 115 | llvm::raw_svector_ostream OS(Buf); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 116 | generateUSRForObjCClass(name, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 117 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 121 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 122 | llvm::raw_svector_ostream OS(Buf); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 123 | generateUSRForObjCProtocol(name, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 124 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
| 128 | const char *category_name) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 129 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 130 | llvm::raw_svector_ostream OS(Buf); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 131 | generateUSRForObjCCategory(class_name, category_name, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 132 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 136 | CXString classUSR) { |
Argyrios Kyrtzidis | 3f4ec31 | 2013-08-21 01:51:19 +0000 | [diff] [blame] | 137 | SmallString<128> Buf(getUSRSpacePrefix()); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 138 | llvm::raw_svector_ostream OS(Buf); |
| 139 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
Argyrios Kyrtzidis | e845834 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 140 | generateUSRForObjCProperty(property, OS); |
Argyrios Kyrtzidis | 1216196 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 141 | return cxstring::createDup(OS.str()); |
Guy Benyei | 7f92f2d | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // end extern "C" |