/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CIndexUSRs.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
13 | | #include "CIndexer.h" |
14 | | #include "CXCursor.h" |
15 | | #include "CXString.h" |
16 | | #include "CXTranslationUnit.h" |
17 | | #include "clang/Frontend/ASTUnit.h" |
18 | | #include "clang/Index/USRGeneration.h" |
19 | | #include "clang/Lex/PreprocessingRecord.h" |
20 | | #include "llvm/ADT/SmallString.h" |
21 | | #include "llvm/Support/raw_ostream.h" |
22 | | |
23 | | using namespace clang; |
24 | | using namespace clang::index; |
25 | | |
26 | | //===----------------------------------------------------------------------===// |
27 | | // API hooks. |
28 | | //===----------------------------------------------------------------------===// |
29 | | |
30 | 4 | static inline StringRef extractUSRSuffix(StringRef s) { |
31 | 4 | return s.startswith("c:") ? s.substr(2) : ""0 ; |
32 | 4 | } |
33 | | |
34 | 4.71k | bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) { |
35 | 4.71k | return generateUSRForDecl(D, Buf); |
36 | 4.71k | } |
37 | | |
38 | 3.97k | CXString clang_getCursorUSR(CXCursor C) { |
39 | 3.97k | const CXCursorKind &K = clang_getCursorKind(C); |
40 | | |
41 | 3.97k | if (clang_isDeclaration(K)) { |
42 | 568 | const Decl *D = cxcursor::getCursorDecl(C); |
43 | 568 | if (!D) |
44 | 0 | return cxstring::createEmpty(); |
45 | | |
46 | 568 | CXTranslationUnit TU = cxcursor::getCursorTU(C); |
47 | 568 | if (!TU) |
48 | 0 | return cxstring::createEmpty(); |
49 | | |
50 | 568 | cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); |
51 | 568 | if (!buf) |
52 | 0 | return cxstring::createEmpty(); |
53 | | |
54 | 568 | bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data); |
55 | 568 | if (Ignore) { |
56 | 62 | buf->dispose(); |
57 | 62 | return cxstring::createEmpty(); |
58 | 62 | } |
59 | | |
60 | | // Return the C-string, but don't make a copy since it is already in |
61 | | // the string buffer. |
62 | 506 | buf->Data.push_back('\0'); |
63 | 506 | return createCXString(buf); |
64 | 568 | } |
65 | | |
66 | 3.41k | if (K == CXCursor_MacroDefinition) { |
67 | 2.90k | CXTranslationUnit TU = cxcursor::getCursorTU(C); |
68 | 2.90k | if (!TU) |
69 | 0 | return cxstring::createEmpty(); |
70 | | |
71 | 2.90k | cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU); |
72 | 2.90k | if (!buf) |
73 | 0 | return cxstring::createEmpty(); |
74 | | |
75 | 2.90k | bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C), |
76 | 2.90k | cxtu::getASTUnit(TU)->getSourceManager(), |
77 | 2.90k | buf->Data); |
78 | 2.90k | if (Ignore) { |
79 | 0 | buf->dispose(); |
80 | 0 | return cxstring::createEmpty(); |
81 | 0 | } |
82 | | |
83 | | // Return the C-string, but don't make a copy since it is already in |
84 | | // the string buffer. |
85 | 2.90k | buf->Data.push_back('\0'); |
86 | 2.90k | return createCXString(buf); |
87 | 2.90k | } |
88 | | |
89 | 505 | return cxstring::createEmpty(); |
90 | 3.41k | } |
91 | | |
92 | 1 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
93 | 1 | SmallString<128> Buf(getUSRSpacePrefix()); |
94 | 1 | llvm::raw_svector_ostream OS(Buf); |
95 | 1 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
96 | 1 | generateUSRForObjCIvar(name, OS); |
97 | 1 | return cxstring::createDup(OS.str()); |
98 | 1 | } |
99 | | |
100 | | CXString clang_constructUSR_ObjCMethod(const char *name, |
101 | | unsigned isInstanceMethod, |
102 | 2 | CXString classUSR) { |
103 | 2 | SmallString<128> Buf(getUSRSpacePrefix()); |
104 | 2 | llvm::raw_svector_ostream OS(Buf); |
105 | 2 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
106 | 2 | generateUSRForObjCMethod(name, isInstanceMethod, OS); |
107 | 2 | return cxstring::createDup(OS.str()); |
108 | 2 | } |
109 | | |
110 | 1 | CXString clang_constructUSR_ObjCClass(const char *name) { |
111 | 1 | SmallString<128> Buf(getUSRSpacePrefix()); |
112 | 1 | llvm::raw_svector_ostream OS(Buf); |
113 | 1 | generateUSRForObjCClass(name, OS); |
114 | 1 | return cxstring::createDup(OS.str()); |
115 | 1 | } |
116 | | |
117 | 1 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
118 | 1 | SmallString<128> Buf(getUSRSpacePrefix()); |
119 | 1 | llvm::raw_svector_ostream OS(Buf); |
120 | 1 | generateUSRForObjCProtocol(name, OS); |
121 | 1 | return cxstring::createDup(OS.str()); |
122 | 1 | } |
123 | | |
124 | | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
125 | 1 | const char *category_name) { |
126 | 1 | SmallString<128> Buf(getUSRSpacePrefix()); |
127 | 1 | llvm::raw_svector_ostream OS(Buf); |
128 | 1 | generateUSRForObjCCategory(class_name, category_name, OS); |
129 | 1 | return cxstring::createDup(OS.str()); |
130 | 1 | } |
131 | | |
132 | | CXString clang_constructUSR_ObjCProperty(const char *property, |
133 | 1 | CXString classUSR) { |
134 | 1 | SmallString<128> Buf(getUSRSpacePrefix()); |
135 | 1 | llvm::raw_svector_ostream OS(Buf); |
136 | 1 | OS << extractUSRSuffix(clang_getCString(classUSR)); |
137 | 1 | generateUSRForObjCProperty(property, /*isClassProp=*/false, OS); |
138 | 1 | return cxstring::createDup(OS.str()); |
139 | 1 | } |