1 //===- CIndexUSRs.cpp - Clang-C Source Indexing Library -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the generation and use of USRs from CXEntities.
11 //===----------------------------------------------------------------------===//
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"
23 using namespace clang
;
24 using namespace clang::index
;
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 static inline StringRef
extractUSRSuffix(StringRef s
) {
31 return s
.starts_with("c:") ? s
.substr(2) : "";
34 bool cxcursor::getDeclCursorUSR(const Decl
*D
, SmallVectorImpl
<char> &Buf
) {
35 return generateUSRForDecl(D
, Buf
);
38 CXString
clang_getCursorUSR(CXCursor C
) {
39 const CXCursorKind
&K
= clang_getCursorKind(C
);
41 if (clang_isDeclaration(K
)) {
42 const Decl
*D
= cxcursor::getCursorDecl(C
);
44 return cxstring::createEmpty();
46 CXTranslationUnit TU
= cxcursor::getCursorTU(C
);
48 return cxstring::createEmpty();
50 cxstring::CXStringBuf
*buf
= cxstring::getCXStringBuf(TU
);
52 return cxstring::createEmpty();
54 bool Ignore
= cxcursor::getDeclCursorUSR(D
, buf
->Data
);
57 return cxstring::createEmpty();
60 // Return the C-string, but don't make a copy since it is already in
62 buf
->Data
.push_back('\0');
63 return createCXString(buf
);
66 if (K
== CXCursor_MacroDefinition
) {
67 CXTranslationUnit TU
= cxcursor::getCursorTU(C
);
69 return cxstring::createEmpty();
71 cxstring::CXStringBuf
*buf
= cxstring::getCXStringBuf(TU
);
73 return cxstring::createEmpty();
75 bool Ignore
= generateUSRForMacro(cxcursor::getCursorMacroDefinition(C
),
76 cxtu::getASTUnit(TU
)->getSourceManager(),
80 return cxstring::createEmpty();
83 // Return the C-string, but don't make a copy since it is already in
85 buf
->Data
.push_back('\0');
86 return createCXString(buf
);
89 return cxstring::createEmpty();
92 CXString
clang_constructUSR_ObjCIvar(const char *name
, CXString classUSR
) {
93 SmallString
<128> Buf(getUSRSpacePrefix());
94 llvm::raw_svector_ostream
OS(Buf
);
95 OS
<< extractUSRSuffix(clang_getCString(classUSR
));
96 generateUSRForObjCIvar(name
, OS
);
97 return cxstring::createDup(OS
.str());
100 CXString
clang_constructUSR_ObjCMethod(const char *name
,
101 unsigned isInstanceMethod
,
103 SmallString
<128> Buf(getUSRSpacePrefix());
104 llvm::raw_svector_ostream
OS(Buf
);
105 OS
<< extractUSRSuffix(clang_getCString(classUSR
));
106 generateUSRForObjCMethod(name
, isInstanceMethod
, OS
);
107 return cxstring::createDup(OS
.str());
110 CXString
clang_constructUSR_ObjCClass(const char *name
) {
111 SmallString
<128> Buf(getUSRSpacePrefix());
112 llvm::raw_svector_ostream
OS(Buf
);
113 generateUSRForObjCClass(name
, OS
);
114 return cxstring::createDup(OS
.str());
117 CXString
clang_constructUSR_ObjCProtocol(const char *name
) {
118 SmallString
<128> Buf(getUSRSpacePrefix());
119 llvm::raw_svector_ostream
OS(Buf
);
120 generateUSRForObjCProtocol(name
, OS
);
121 return cxstring::createDup(OS
.str());
124 CXString
clang_constructUSR_ObjCCategory(const char *class_name
,
125 const char *category_name
) {
126 SmallString
<128> Buf(getUSRSpacePrefix());
127 llvm::raw_svector_ostream
OS(Buf
);
128 generateUSRForObjCCategory(class_name
, category_name
, OS
);
129 return cxstring::createDup(OS
.str());
132 CXString
clang_constructUSR_ObjCProperty(const char *property
,
134 SmallString
<128> Buf(getUSRSpacePrefix());
135 llvm::raw_svector_ostream
OS(Buf
);
136 OS
<< extractUSRSuffix(clang_getCString(classUSR
));
137 generateUSRForObjCProperty(property
, /*isClassProp=*/false, OS
);
138 return cxstring::createDup(OS
.str());