blob: 602a0c7c36a926428f2709d32f52524f43fe896d [file] [log] [blame]
Ted Kremenek1b6869a2010-01-05 22:06:45 +00001//===- 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"
Ted Kremenekcf84aa42010-01-18 20:23:29 +000015#include "CXCursor.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000016#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000017#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000018#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000019#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000020#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000021
Benjamin Kramerb846deb2010-04-12 19:45:50 +000022using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000023using namespace clang::cxstring;
24
Ted Kremenekc50277f2010-01-12 23:33:42 +000025//===----------------------------------------------------------------------===//
26// USR generation.
27//===----------------------------------------------------------------------===//
28
29namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000030class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000031 llvm::SmallString<1024> Buf;
32 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000033 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000034 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000035 bool generatedLoc;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000036public:
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000037 USRGenerator(const CXCursor *C = 0)
38 : Out(Buf),
39 IgnoreResults(false),
40 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
41 generatedLoc(false)
42 {
43 // Add the USR space prefix.
44 Out << "c:";
45 }
46
47 llvm::StringRef str() {
48 return Out.str();
49 }
50
51 USRGenerator* operator->() { return this; }
52
53 template <typename T>
54 llvm::raw_svector_ostream &operator<<(const T &x) {
55 Out << x;
56 return Out;
57 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000058
Ted Kremenek3adca6d2010-01-18 22:02:49 +000059 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000060
61 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000062 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000063 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000064 void VisitFunctionDecl(FunctionDecl *D);
65 void VisitNamedDecl(NamedDecl *D);
66 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000067 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000068 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000069 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000071 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000072 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000073 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000074 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000075 void VisitVarDecl(VarDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000076 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
77 IgnoreResults = true;
78 return;
79 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000080
Ted Kremenek6f153952010-04-15 21:51:13 +000081 /// Generate the string component containing the location of the
82 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000083 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000084
Ted Kremenek896b70f2010-03-13 02:50:34 +000085 /// String generation methods used both by the visitation methods
86 /// and from other clients that want to directly generate USRs. These
87 /// methods do not construct complete USRs (which incorporate the parents
88 /// of an AST element), but only the fragments concerning the AST element
89 /// itself.
90
Ted Kremenek896b70f2010-03-13 02:50:34 +000091 /// Generate a USR for an Objective-C class.
92 void GenObjCClass(llvm::StringRef cls);
93 /// Generate a USR for an Objective-C class category.
94 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
95 /// Generate a USR fragment for an Objective-C instance variable. The
96 /// complete USR can be created by concatenating the USR for the
97 /// encompassing class with this USR fragment.
98 void GenObjCIvar(llvm::StringRef ivar);
99 /// Generate a USR fragment for an Objective-C method.
100 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
101 /// Generate a USR fragment for an Objective-C property.
102 void GenObjCProperty(llvm::StringRef prop);
103 /// Generate a USR for an Objective-C protocol.
104 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000105
106 void VisitType(QualType T);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000107
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000108 /// Emit a Decl's name using NamedDecl::printName() and return true if
109 /// the decl had no name.
110 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000111};
112
Ted Kremenekc50277f2010-01-12 23:33:42 +0000113} // end anonymous namespace
114
Ted Kremenek896b70f2010-03-13 02:50:34 +0000115//===----------------------------------------------------------------------===//
116// Generating USRs from ASTS.
117//===----------------------------------------------------------------------===//
118
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000119bool USRGenerator::EmitDeclName(const NamedDecl *D) {
120 Out.flush();
121 const unsigned startSize = Buf.size();
122 D->printName(Out);
123 Out.flush();
124 const unsigned endSize = Buf.size();
125 return startSize == endSize;
126}
127
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000128static bool InAnonymousNamespace(const Decl *D) {
129 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
130 return ND->isAnonymousNamespace();
131 return false;
132}
133
134static inline bool ShouldGenerateLocation(const NamedDecl *D) {
135 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
136}
137
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000138void USRGenerator::VisitDeclContext(DeclContext *DC) {
139 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
140 Visit(D);
141}
142
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000143void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000144 VisitDeclContext(D->getDeclContext());
145 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
146 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000147 // Bit fields can be anonymous.
148 IgnoreResults = true;
149 return;
150 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000151}
152
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000153void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000154 if (ShouldGenerateLocation(D) && GenLoc(D))
155 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000156
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000157 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000158 Out << "@F@";
159 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000160
161 ASTContext &Ctx = AU->getASTContext();
162 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
163 return;
164
165 // Mangle in type information for the arguments.
166 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
167 I != E; ++I) {
168 Out << '#';
169 if (ParmVarDecl *PD = *I)
170 VisitType(PD->getType());
171 }
172 if (D->isVariadic())
173 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000174 Out << '#';
175 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
176 if (MD->isStatic())
177 Out << 'S';
178 if (unsigned quals = MD->getTypeQualifiers())
179 Out << (char)('0' + quals);
180 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000181}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000182
183void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000184 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000185 Out << "@";
186
187 if (EmitDeclName(D)) {
188 // The string can be empty if the declaration has no name; e.g., it is
189 // the ParmDecl with no name for declaration of a function pointer type,
190 // e.g.: void (*f)(void *);
191 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000192 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000193 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000194}
195
Ted Kremeneke542f772010-04-20 23:15:40 +0000196void USRGenerator::VisitVarDecl(VarDecl *D) {
197 // VarDecls can be declared 'extern' within a function or method body,
198 // but their enclosing DeclContext is the function, not the TU. We need
199 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000200 if (ShouldGenerateLocation(D) && GenLoc(D))
201 return;
202
203 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000204
Ted Kremenekcf999102010-04-29 17:43:29 +0000205 // Variables always have simple names.
206 llvm::StringRef s = D->getName();
207
Ted Kremeneke542f772010-04-20 23:15:40 +0000208 // The string can be empty if the declaration has no name; e.g., it is
209 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
210 // void (*f)(void *);
211 // In this case, don't generate a USR.
212 if (s.empty())
213 IgnoreResults = true;
214 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000215 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000216}
217
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000218void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000219 if (D->isAnonymousNamespace()) {
220 Out << "@aN";
221 return;
222 }
223
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000224 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000225 if (!IgnoreResults)
226 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000227}
228
Ted Kremenekc50277f2010-01-12 23:33:42 +0000229void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
230 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000231 // Ideally we would use 'GenObjCMethod', but this is such a hot path
232 // for Objective-C code that we don't want to use
233 // DeclarationName::getAsString().
234 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
235 DeclarationName N(D->getSelector());
236 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000237}
238
Ted Kremeneke74ef122010-04-16 21:31:52 +0000239void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
240 // FIXME: @class declarations can refer to multiple classes. We need
241 // to be able to traverse these.
242 IgnoreResults = true;
243}
244
245void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
246 // FIXME: @protocol declarations can refer to multiple protocols. We need
247 // to be able to traverse these.
248 IgnoreResults = true;
249}
250
Ted Kremenekc50277f2010-01-12 23:33:42 +0000251void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
252 switch (D->getKind()) {
253 default:
254 assert(false && "Invalid ObjC container.");
255 case Decl::ObjCInterface:
256 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000257 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000258 break;
259 case Decl::ObjCCategory: {
260 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000261 ObjCInterfaceDecl *ID = CD->getClassInterface();
262 if (!ID) {
263 // Handle invalid code where the @interface might not
264 // have been specified.
265 // FIXME: We should be able to generate this USR even if the
266 // @interface isn't available.
267 IgnoreResults = true;
268 return;
269 }
270 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000271 break;
272 }
273 case Decl::ObjCCategoryImpl: {
274 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000275 ObjCInterfaceDecl *ID = CD->getClassInterface();
276 if (!ID) {
277 // Handle invalid code where the @interface might not
278 // have been specified.
279 // FIXME: We should be able to generate this USR even if the
280 // @interface isn't available.
281 IgnoreResults = true;
282 return;
283 }
284 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000285 break;
286 }
287 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000288 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000289 break;
290 }
291}
292
293void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
294 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000295 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000296}
297
Ted Kremeneke542f772010-04-20 23:15:40 +0000298void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
299 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
300 VisitObjCPropertyDecl(PD);
301 return;
302 }
303
304 IgnoreResults = true;
305}
306
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000307void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000308 // Add the location of the tag decl to handle resolution across
309 // translation units.
310 if (ShouldGenerateLocation(D) && GenLoc(D))
311 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000312
Ted Kremenek6f153952010-04-15 21:51:13 +0000313 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000314 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000315
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000316 switch (D->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000317 case TTK_Struct: Out << "@S"; break;
318 case TTK_Class: Out << "@C"; break;
319 case TTK_Union: Out << "@U"; break;
320 case TTK_Enum: Out << "@E"; break;
Ted Kremeneke74ef122010-04-16 21:31:52 +0000321 }
322
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000323 Out << '@';
324 Out.flush();
325 assert(Buf.size() > 0);
326 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000327
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000328 if (EmitDeclName(D)) {
329 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
330 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000331 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000332 }
333 else
334 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000335 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000336}
337
Ted Kremenekc50277f2010-01-12 23:33:42 +0000338void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000339 if (ShouldGenerateLocation(D) && GenLoc(D))
340 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000341 DeclContext *DC = D->getDeclContext();
342 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000343 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000344 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000345 Out << D->getName();
346}
347
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000348bool USRGenerator::GenLoc(const Decl *D) {
349 if (generatedLoc)
350 return IgnoreResults;
351 generatedLoc = true;
352
Ted Kremenek6f153952010-04-15 21:51:13 +0000353 const SourceManager &SM = AU->getSourceManager();
354 SourceLocation L = D->getLocStart();
355 if (L.isInvalid()) {
356 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000357 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000358 }
359 L = SM.getInstantiationLoc(L);
360 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
361 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000362 if (FE) {
363 llvm::sys::Path P(FE->getName());
364 Out << P.getLast();
365 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000366 else {
367 // This case really isn't interesting.
368 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000369 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000370 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000371 // Use the offest into the FileID to represent the location. Using
372 // a line/column can cause us to look back at the original source file,
373 // which is expensive.
374 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000375 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000376}
377
Ted Kremenek8e672192010-05-07 01:04:32 +0000378void USRGenerator::VisitType(QualType T) {
379 // This method mangles in USR information for types. It can possibly
380 // just reuse the naming-mangling logic used by codegen, although the
381 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000382 ASTContext &Ctx = AU->getASTContext();
383
Ted Kremenek8e672192010-05-07 01:04:32 +0000384 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000385 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000386 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000387 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000388 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000389 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000390 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000391 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000392 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000393 qVal |= 0x4;
394 if(qVal)
395 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000396
397 // Mangle in ObjC GC qualifiers?
398
399 if (const PointerType *PT = T->getAs<PointerType>()) {
400 Out << '*';
401 T = PT->getPointeeType();
402 continue;
403 }
404 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
405 Out << '&';
406 T = RT->getPointeeType();
407 continue;
408 }
409 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
410 Out << 'F';
411 VisitType(FT->getResultType());
412 for (FunctionProtoType::arg_type_iterator
413 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
414 VisitType(*I);
415 }
416 if (FT->isVariadic())
417 Out << '.';
418 return;
419 }
420 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
421 Out << 'B';
422 T = BT->getPointeeType();
423 continue;
424 }
425 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
426 unsigned char c = '\0';
427 switch (BT->getKind()) {
428 case BuiltinType::Void:
429 c = 'v'; break;
430 case BuiltinType::Bool:
431 c = 'b'; break;
432 case BuiltinType::Char_U:
433 case BuiltinType::UChar:
434 c = 'c'; break;
435 case BuiltinType::Char16:
436 c = 'q'; break;
437 case BuiltinType::Char32:
438 c = 'w'; break;
439 case BuiltinType::UShort:
440 c = 's'; break;
441 case BuiltinType::UInt:
442 c = 'i'; break;
443 case BuiltinType::ULong:
444 c = 'l'; break;
445 case BuiltinType::ULongLong:
446 c = 'k'; break;
447 case BuiltinType::UInt128:
448 c = 'j'; break;
449 case BuiltinType::Char_S:
450 case BuiltinType::SChar:
451 c = 'C'; break;
452 case BuiltinType::WChar:
453 c = 'W'; break;
454 case BuiltinType::Short:
455 c = 'S'; break;
456 case BuiltinType::Int:
457 c = 'I'; break;
458 case BuiltinType::Long:
459 c = 'L'; break;
460 case BuiltinType::LongLong:
461 c = 'K'; break;
462 case BuiltinType::Int128:
463 c = 'J'; break;
464 case BuiltinType::Float:
465 c = 'f'; break;
466 case BuiltinType::Double:
467 c = 'd'; break;
468 case BuiltinType::LongDouble:
469 c = 'D'; break;
470 case BuiltinType::NullPtr:
471 c = 'n'; break;
472 case BuiltinType::Overload:
473 case BuiltinType::Dependent:
474 case BuiltinType::UndeducedAuto:
475 IgnoreResults = true;
476 return;
477 case BuiltinType::ObjCId:
478 c = 'o'; break;
479 case BuiltinType::ObjCClass:
480 c = 'O'; break;
481 case BuiltinType::ObjCSel:
482 c = 'e'; break;
483 }
484 Out << c;
485 return;
486 }
487 if (const ComplexType *CT = T->getAs<ComplexType>()) {
488 Out << '<';
489 T = CT->getElementType();
490 continue;
491 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000492 if (const TagType *TT = T->getAs<TagType>()) {
493 Out << '$';
494 VisitTagDecl(TT->getDecl());
495 return;
496 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000497
498 // Unhandled type.
499 Out << ' ';
500 break;
501 } while (true);
502}
503
Ted Kremenek896b70f2010-03-13 02:50:34 +0000504//===----------------------------------------------------------------------===//
505// General purpose USR generation methods.
506//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000507
Ted Kremenek896b70f2010-03-13 02:50:34 +0000508void USRGenerator::GenObjCClass(llvm::StringRef cls) {
509 Out << "objc(cs)" << cls;
510}
511
512void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000513 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000514}
515
516void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000517 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000518}
519
520void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
521 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
522}
523
524void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
525 Out << "(py)" << prop;
526}
527
528void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
529 Out << "objc(pl)" << prot;
530}
531
532//===----------------------------------------------------------------------===//
533// API hooks.
534//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000535
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000536static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
537 return s.startswith("c:") ? s.substr(2) : "";
538}
539
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000540static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000541 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000542
543 // Don't generate USRs for things with invalid locations.
544 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000545 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000546
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000547 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000548 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000549 switch (ND->getLinkage()) {
550 case ExternalLinkage:
551 // Generate USRs for all entities with external linkage.
552 break;
553 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000554 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000555 // We allow enums, typedefs, and structs that have no linkage to
556 // have USRs that are anchored to the file they were defined in
557 // (e.g., the header). This is a little gross, but in principal
558 // enums/anonymous structs/etc. defined in a common header file
559 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000560 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000561 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000562 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000563 break;
564 // Fall-through.
565 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000566 if (isa<FunctionDecl>(ND))
567 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000568 }
569
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000570 USRGenerator UG(&C);
571 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000572
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000573 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000574 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000575
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000576#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000577 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000578 assert(UG.str().size() > 2);
579#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000580
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000581 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000582 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000583}
584
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000585extern "C" {
586
587CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000588 const CXCursorKind &K = clang_getCursorKind(C);
589
590 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000591 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000592
593 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000594 USRGenerator UG(&C);
595 UG << "macro@"
596 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
597 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000598 }
599
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000600 return createCXString("");
601}
602
Ted Kremenek896b70f2010-03-13 02:50:34 +0000603CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000604 USRGenerator UG;
605 UG << extractUSRSuffix(clang_getCString(classUSR));
606 UG->GenObjCIvar(name);
607 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000608}
609
610CXString clang_constructUSR_ObjCMethod(const char *name,
611 unsigned isInstanceMethod,
612 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000613 USRGenerator UG;
614 UG << extractUSRSuffix(clang_getCString(classUSR));
615 UG->GenObjCMethod(name, isInstanceMethod);
616 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000617}
618
619CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000620 USRGenerator UG;
621 UG->GenObjCClass(name);
622 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000623}
624
625CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000626 USRGenerator UG;
627 UG->GenObjCProtocol(name);
628 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000629}
630
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000631CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000632 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000633 USRGenerator UG;
634 UG->GenObjCCategory(class_name, category_name);
635 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000636}
637
638CXString clang_constructUSR_ObjCProperty(const char *property,
639 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000640 USRGenerator UG;
641 UG << extractUSRSuffix(clang_getCString(classUSR));
642 UG->GenObjCProperty(property);
643 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000644}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000645
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000646} // end extern "C"