blob: 2a9d0c76f526c4d2bb33db0ed4be0f7d69b90a99 [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 Kremenek2fee4e62010-01-14 01:50:21 +0000174}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000175
176void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000177 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000178 Out << "@";
179
180 if (EmitDeclName(D)) {
181 // The string can be empty if the declaration has no name; e.g., it is
182 // the ParmDecl with no name for declaration of a function pointer type,
183 // e.g.: void (*f)(void *);
184 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000185 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000186 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000187}
188
Ted Kremeneke542f772010-04-20 23:15:40 +0000189void USRGenerator::VisitVarDecl(VarDecl *D) {
190 // VarDecls can be declared 'extern' within a function or method body,
191 // but their enclosing DeclContext is the function, not the TU. We need
192 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000193 if (ShouldGenerateLocation(D) && GenLoc(D))
194 return;
195
196 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000197
Ted Kremenekcf999102010-04-29 17:43:29 +0000198 // Variables always have simple names.
199 llvm::StringRef s = D->getName();
200
Ted Kremeneke542f772010-04-20 23:15:40 +0000201 // The string can be empty if the declaration has no name; e.g., it is
202 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
203 // void (*f)(void *);
204 // In this case, don't generate a USR.
205 if (s.empty())
206 IgnoreResults = true;
207 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000208 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000209}
210
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000211void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000212 if (D->isAnonymousNamespace()) {
213 Out << "@aN";
214 return;
215 }
216
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000217 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000218 if (!IgnoreResults)
219 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000220}
221
Ted Kremenekc50277f2010-01-12 23:33:42 +0000222void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
223 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000224 // Ideally we would use 'GenObjCMethod', but this is such a hot path
225 // for Objective-C code that we don't want to use
226 // DeclarationName::getAsString().
227 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
228 DeclarationName N(D->getSelector());
229 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000230}
231
Ted Kremeneke74ef122010-04-16 21:31:52 +0000232void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
233 // FIXME: @class declarations can refer to multiple classes. We need
234 // to be able to traverse these.
235 IgnoreResults = true;
236}
237
238void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
239 // FIXME: @protocol declarations can refer to multiple protocols. We need
240 // to be able to traverse these.
241 IgnoreResults = true;
242}
243
Ted Kremenekc50277f2010-01-12 23:33:42 +0000244void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
245 switch (D->getKind()) {
246 default:
247 assert(false && "Invalid ObjC container.");
248 case Decl::ObjCInterface:
249 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000250 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000251 break;
252 case Decl::ObjCCategory: {
253 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000254 ObjCInterfaceDecl *ID = CD->getClassInterface();
255 if (!ID) {
256 // Handle invalid code where the @interface might not
257 // have been specified.
258 // FIXME: We should be able to generate this USR even if the
259 // @interface isn't available.
260 IgnoreResults = true;
261 return;
262 }
263 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000264 break;
265 }
266 case Decl::ObjCCategoryImpl: {
267 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000268 ObjCInterfaceDecl *ID = CD->getClassInterface();
269 if (!ID) {
270 // Handle invalid code where the @interface might not
271 // have been specified.
272 // FIXME: We should be able to generate this USR even if the
273 // @interface isn't available.
274 IgnoreResults = true;
275 return;
276 }
277 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000278 break;
279 }
280 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000281 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000282 break;
283 }
284}
285
286void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
287 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000288 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000289}
290
Ted Kremeneke542f772010-04-20 23:15:40 +0000291void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
292 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
293 VisitObjCPropertyDecl(PD);
294 return;
295 }
296
297 IgnoreResults = true;
298}
299
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000300void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000301 // Add the location of the tag decl to handle resolution across
302 // translation units.
303 if (ShouldGenerateLocation(D) && GenLoc(D))
304 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000305
Ted Kremenek6f153952010-04-15 21:51:13 +0000306 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000307 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000308
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000309 switch (D->getTagKind()) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000310 case TagDecl::TK_struct: Out << "@S"; break;
311 case TagDecl::TK_class: Out << "@C"; break;
312 case TagDecl::TK_union: Out << "@U"; break;
313 case TagDecl::TK_enum: Out << "@E"; break;
314 }
315
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000316 Out << '@';
317 Out.flush();
318 assert(Buf.size() > 0);
319 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000320
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000321 if (EmitDeclName(D)) {
322 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
323 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000324 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000325 }
326 else
327 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000328 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000329}
330
Ted Kremenekc50277f2010-01-12 23:33:42 +0000331void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000332 if (ShouldGenerateLocation(D) && GenLoc(D))
333 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000334 DeclContext *DC = D->getDeclContext();
335 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000336 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000337 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000338 Out << D->getName();
339}
340
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000341bool USRGenerator::GenLoc(const Decl *D) {
342 if (generatedLoc)
343 return IgnoreResults;
344 generatedLoc = true;
345
Ted Kremenek6f153952010-04-15 21:51:13 +0000346 const SourceManager &SM = AU->getSourceManager();
347 SourceLocation L = D->getLocStart();
348 if (L.isInvalid()) {
349 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000350 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000351 }
352 L = SM.getInstantiationLoc(L);
353 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
354 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000355 if (FE) {
356 llvm::sys::Path P(FE->getName());
357 Out << P.getLast();
358 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000359 else {
360 // This case really isn't interesting.
361 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000362 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000363 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000364 Out << '@'
365 << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
366 << SM.getColumnNumber(Decomposed.first, Decomposed.second);
Ted Kremenek8e672192010-05-07 01:04:32 +0000367
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000368 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000369}
370
Ted Kremenek8e672192010-05-07 01:04:32 +0000371void USRGenerator::VisitType(QualType T) {
372 // This method mangles in USR information for types. It can possibly
373 // just reuse the naming-mangling logic used by codegen, although the
374 // requirements for USRs might not be the same.
375 do {
376 T = T.getTypePtr()->getCanonicalTypeInternal();
377 Qualifiers Q = T.getQualifiers();
378 if (Q.hasConst())
379 Out << '1';
380 if (Q.hasVolatile())
381 Out << '2';
382 if (Q.hasRestrict())
383 Out << '3';
384
385 // Mangle in ObjC GC qualifiers?
386
387 if (const PointerType *PT = T->getAs<PointerType>()) {
388 Out << '*';
389 T = PT->getPointeeType();
390 continue;
391 }
392 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
393 Out << '&';
394 T = RT->getPointeeType();
395 continue;
396 }
397 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
398 Out << 'F';
399 VisitType(FT->getResultType());
400 for (FunctionProtoType::arg_type_iterator
401 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
402 VisitType(*I);
403 }
404 if (FT->isVariadic())
405 Out << '.';
406 return;
407 }
408 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
409 Out << 'B';
410 T = BT->getPointeeType();
411 continue;
412 }
413 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
414 unsigned char c = '\0';
415 switch (BT->getKind()) {
416 case BuiltinType::Void:
417 c = 'v'; break;
418 case BuiltinType::Bool:
419 c = 'b'; break;
420 case BuiltinType::Char_U:
421 case BuiltinType::UChar:
422 c = 'c'; break;
423 case BuiltinType::Char16:
424 c = 'q'; break;
425 case BuiltinType::Char32:
426 c = 'w'; break;
427 case BuiltinType::UShort:
428 c = 's'; break;
429 case BuiltinType::UInt:
430 c = 'i'; break;
431 case BuiltinType::ULong:
432 c = 'l'; break;
433 case BuiltinType::ULongLong:
434 c = 'k'; break;
435 case BuiltinType::UInt128:
436 c = 'j'; break;
437 case BuiltinType::Char_S:
438 case BuiltinType::SChar:
439 c = 'C'; break;
440 case BuiltinType::WChar:
441 c = 'W'; break;
442 case BuiltinType::Short:
443 c = 'S'; break;
444 case BuiltinType::Int:
445 c = 'I'; break;
446 case BuiltinType::Long:
447 c = 'L'; break;
448 case BuiltinType::LongLong:
449 c = 'K'; break;
450 case BuiltinType::Int128:
451 c = 'J'; break;
452 case BuiltinType::Float:
453 c = 'f'; break;
454 case BuiltinType::Double:
455 c = 'd'; break;
456 case BuiltinType::LongDouble:
457 c = 'D'; break;
458 case BuiltinType::NullPtr:
459 c = 'n'; break;
460 case BuiltinType::Overload:
461 case BuiltinType::Dependent:
462 case BuiltinType::UndeducedAuto:
463 IgnoreResults = true;
464 return;
465 case BuiltinType::ObjCId:
466 c = 'o'; break;
467 case BuiltinType::ObjCClass:
468 c = 'O'; break;
469 case BuiltinType::ObjCSel:
470 c = 'e'; break;
471 }
472 Out << c;
473 return;
474 }
475 if (const ComplexType *CT = T->getAs<ComplexType>()) {
476 Out << '<';
477 T = CT->getElementType();
478 continue;
479 }
480
481 // Unhandled type.
482 Out << ' ';
483 break;
484 } while (true);
485}
486
Ted Kremenek896b70f2010-03-13 02:50:34 +0000487//===----------------------------------------------------------------------===//
488// General purpose USR generation methods.
489//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000490
Ted Kremenek896b70f2010-03-13 02:50:34 +0000491void USRGenerator::GenObjCClass(llvm::StringRef cls) {
492 Out << "objc(cs)" << cls;
493}
494
495void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000496 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000497}
498
499void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000500 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000501}
502
503void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
504 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
505}
506
507void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
508 Out << "(py)" << prop;
509}
510
511void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
512 Out << "objc(pl)" << prot;
513}
514
515//===----------------------------------------------------------------------===//
516// API hooks.
517//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000518
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000519static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
520 return s.startswith("c:") ? s.substr(2) : "";
521}
522
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000523static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000524 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000525
526 // Don't generate USRs for things with invalid locations.
527 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000528 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000529
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000530 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000531 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000532 switch (ND->getLinkage()) {
533 case ExternalLinkage:
534 // Generate USRs for all entities with external linkage.
535 break;
536 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000537 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000538 // We allow enums, typedefs, and structs that have no linkage to
539 // have USRs that are anchored to the file they were defined in
540 // (e.g., the header). This is a little gross, but in principal
541 // enums/anonymous structs/etc. defined in a common header file
542 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000543 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000544 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000545 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000546 break;
547 // Fall-through.
548 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000549 if (isa<FunctionDecl>(ND))
550 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000551 }
552
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000553 USRGenerator UG(&C);
554 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000555
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000556 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000557 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000558
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000559#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000560 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000561 assert(UG.str().size() > 2);
562#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000563
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000564 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000565 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000566}
567
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000568extern "C" {
569
570CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000571 const CXCursorKind &K = clang_getCursorKind(C);
572
573 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000574 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000575
576 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000577 USRGenerator UG(&C);
578 UG << "macro@"
579 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
580 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000581 }
582
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000583 return createCXString("");
584}
585
Ted Kremenek896b70f2010-03-13 02:50:34 +0000586CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000587 USRGenerator UG;
588 UG << extractUSRSuffix(clang_getCString(classUSR));
589 UG->GenObjCIvar(name);
590 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000591}
592
593CXString clang_constructUSR_ObjCMethod(const char *name,
594 unsigned isInstanceMethod,
595 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000596 USRGenerator UG;
597 UG << extractUSRSuffix(clang_getCString(classUSR));
598 UG->GenObjCMethod(name, isInstanceMethod);
599 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000600}
601
602CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000603 USRGenerator UG;
604 UG->GenObjCClass(name);
605 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000606}
607
608CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000609 USRGenerator UG;
610 UG->GenObjCProtocol(name);
611 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000612}
613
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000614CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000615 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000616 USRGenerator UG;
617 UG->GenObjCCategory(class_name, category_name);
618 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000619}
620
621CXString clang_constructUSR_ObjCProperty(const char *property,
622 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000623 USRGenerator UG;
624 UG << extractUSRSuffix(clang_getCString(classUSR));
625 UG->GenObjCProperty(property);
626 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000627}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000628
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000629} // end extern "C"