blob: bcb4f75d9e3ea6bb3e32eddb34167b12539e3ac6 [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> {
31 llvm::raw_ostream &Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000032 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000033 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000034 bool generatedLoc;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000035public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000036 USRGenerator(ASTUnit *au, llvm::raw_ostream &out)
Ted Kremenekcbd66f02010-05-06 23:38:28 +000037 : Out(out), IgnoreResults(false), AU(au), generatedLoc(false) {}
Ted Kremenek896b70f2010-03-13 02:50:34 +000038
Ted Kremenek3adca6d2010-01-18 22:02:49 +000039 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000040
41 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000042 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000043 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000044 void VisitFunctionDecl(FunctionDecl *D);
45 void VisitNamedDecl(NamedDecl *D);
46 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000047 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000048 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000049 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000050 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000051 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000052 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000053 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000054 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000055 void VisitVarDecl(VarDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000056 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
57 IgnoreResults = true;
58 return;
59 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000060
Ted Kremenek6f153952010-04-15 21:51:13 +000061 /// Generate the string component containing the location of the
62 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000063 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000064
Ted Kremenek896b70f2010-03-13 02:50:34 +000065 /// String generation methods used both by the visitation methods
66 /// and from other clients that want to directly generate USRs. These
67 /// methods do not construct complete USRs (which incorporate the parents
68 /// of an AST element), but only the fragments concerning the AST element
69 /// itself.
70
71 /// Generate a USR fragment for a named declaration. This does
72 /// not include the USR component for the parent.
73 void GenNamedDecl(llvm::StringRef name);
74
75 /// Generate a USR for an Objective-C class.
76 void GenObjCClass(llvm::StringRef cls);
77 /// Generate a USR for an Objective-C class category.
78 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
79 /// Generate a USR fragment for an Objective-C instance variable. The
80 /// complete USR can be created by concatenating the USR for the
81 /// encompassing class with this USR fragment.
82 void GenObjCIvar(llvm::StringRef ivar);
83 /// Generate a USR fragment for an Objective-C method.
84 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
85 /// Generate a USR fragment for an Objective-C property.
86 void GenObjCProperty(llvm::StringRef prop);
87 /// Generate a USR for an Objective-C protocol.
88 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +000089
90 void VisitType(QualType T);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000091};
Ted Kremenek896b70f2010-03-13 02:50:34 +000092
93class StringUSRGenerator {
94private:
95 llvm::SmallString<1024> StrBuf;
96 llvm::raw_svector_ostream Out;
97 USRGenerator UG;
98public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000099 StringUSRGenerator(const CXCursor *C = 0)
100 : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) {
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000101 // Add the USR space prefix.
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000102 Out << "c:";
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000103 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000104
105 llvm::StringRef str() {
106 return Out.str();
107 }
108
109 USRGenerator* operator->() { return &UG; }
110
111 template <typename T>
112 llvm::raw_svector_ostream &operator<<(const T &x) {
113 Out << x;
114 return Out;
115 }
116};
117
Ted Kremenekc50277f2010-01-12 23:33:42 +0000118} // end anonymous namespace
119
Ted Kremenek896b70f2010-03-13 02:50:34 +0000120//===----------------------------------------------------------------------===//
121// Generating USRs from ASTS.
122//===----------------------------------------------------------------------===//
123
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000124static bool InAnonymousNamespace(const Decl *D) {
125 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
126 return ND->isAnonymousNamespace();
127 return false;
128}
129
130static inline bool ShouldGenerateLocation(const NamedDecl *D) {
131 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
132}
133
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000134void USRGenerator::VisitDeclContext(DeclContext *DC) {
135 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
136 Visit(D);
137}
138
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000139void USRGenerator::VisitFieldDecl(FieldDecl *D) {
140 const std::string &s = D->getNameAsString();
141 if (s.empty()) {
142 // Bit fields can be anonymous.
143 IgnoreResults = true;
144 return;
145 }
146 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000147 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@") << s;
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000148}
149
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000150void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000151 if (ShouldGenerateLocation(D) && GenLoc(D))
152 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000153
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000154 VisitDeclContext(D->getDeclContext());
155 Out << "@F@" << D->getNameAsString();
Ted Kremenek8e672192010-05-07 01:04:32 +0000156
157 ASTContext &Ctx = AU->getASTContext();
158 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
159 return;
160
161 // Mangle in type information for the arguments.
162 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
163 I != E; ++I) {
164 Out << '#';
165 if (ParmVarDecl *PD = *I)
166 VisitType(PD->getType());
167 }
168 if (D->isVariadic())
169 Out << '.';
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000170}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000171
172void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000173 VisitDeclContext(D->getDeclContext());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000174 const std::string &s = D->getNameAsString();
Ted Kremeneke74ef122010-04-16 21:31:52 +0000175 // The string can be empty if the declaration has no name; e.g., it is
176 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
177 // void (*f)(void *);
178 // In this case, don't generate a USR.
179 if (s.empty())
180 IgnoreResults = true;
181 else
182 GenNamedDecl(s);
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000183}
184
Ted Kremeneke542f772010-04-20 23:15:40 +0000185void USRGenerator::VisitVarDecl(VarDecl *D) {
186 // VarDecls can be declared 'extern' within a function or method body,
187 // but their enclosing DeclContext is the function, not the TU. We need
188 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000189 if (ShouldGenerateLocation(D) && GenLoc(D))
190 return;
191
192 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000193
Ted Kremenekcf999102010-04-29 17:43:29 +0000194 // Variables always have simple names.
195 llvm::StringRef s = D->getName();
196
Ted Kremeneke542f772010-04-20 23:15:40 +0000197 // The string can be empty if the declaration has no name; e.g., it is
198 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
199 // void (*f)(void *);
200 // In this case, don't generate a USR.
201 if (s.empty())
202 IgnoreResults = true;
203 else
204 GenNamedDecl(s);
205}
206
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000207void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000208 if (D->isAnonymousNamespace()) {
209 Out << "@aN";
210 return;
211 }
212
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000213 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000214 if (!IgnoreResults)
215 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000216}
217
Ted Kremenekc50277f2010-01-12 23:33:42 +0000218void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
219 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000220 GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
221 D->isInstanceMethod());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000222}
223
Ted Kremeneke74ef122010-04-16 21:31:52 +0000224void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
225 // FIXME: @class declarations can refer to multiple classes. We need
226 // to be able to traverse these.
227 IgnoreResults = true;
228}
229
230void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
231 // FIXME: @protocol declarations can refer to multiple protocols. We need
232 // to be able to traverse these.
233 IgnoreResults = true;
234}
235
Ted Kremenekc50277f2010-01-12 23:33:42 +0000236void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
237 switch (D->getKind()) {
238 default:
239 assert(false && "Invalid ObjC container.");
240 case Decl::ObjCInterface:
241 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000242 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000243 break;
244 case Decl::ObjCCategory: {
245 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000246 ObjCInterfaceDecl *ID = CD->getClassInterface();
247 if (!ID) {
248 // Handle invalid code where the @interface might not
249 // have been specified.
250 // FIXME: We should be able to generate this USR even if the
251 // @interface isn't available.
252 IgnoreResults = true;
253 return;
254 }
255 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000256 break;
257 }
258 case Decl::ObjCCategoryImpl: {
259 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000260 ObjCInterfaceDecl *ID = CD->getClassInterface();
261 if (!ID) {
262 // Handle invalid code where the @interface might not
263 // have been specified.
264 // FIXME: We should be able to generate this USR even if the
265 // @interface isn't available.
266 IgnoreResults = true;
267 return;
268 }
269 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000270 break;
271 }
272 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000273 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000274 break;
275 }
276}
277
278void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
279 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000280 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000281}
282
Ted Kremeneke542f772010-04-20 23:15:40 +0000283void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
284 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
285 VisitObjCPropertyDecl(PD);
286 return;
287 }
288
289 IgnoreResults = true;
290}
291
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000292void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000293 // Add the location of the tag decl to handle resolution across
294 // translation units.
295 if (ShouldGenerateLocation(D) && GenLoc(D))
296 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000297
Ted Kremenek6f153952010-04-15 21:51:13 +0000298 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000299 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000300
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000301 switch (D->getTagKind()) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000302 case TagDecl::TK_struct: Out << "@S"; break;
303 case TagDecl::TK_class: Out << "@C"; break;
304 case TagDecl::TK_union: Out << "@U"; break;
305 case TagDecl::TK_enum: Out << "@E"; break;
306 }
307
308 const std::string &s = D->getNameAsString();
309 const TypedefDecl *TD = 0;
310 if (s.empty()) {
311 TD = D->getTypedefForAnonDecl();
312 Out << (TD ? 'A' : 'a');
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000313 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000314
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000315 if (s.empty()) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000316 if (TD)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000317 Out << '@' << TD;
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000318 }
319 else
Ted Kremeneke74ef122010-04-16 21:31:52 +0000320 Out << '@' << s;
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000321}
322
Ted Kremenekc50277f2010-01-12 23:33:42 +0000323void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000324 if (ShouldGenerateLocation(D) && GenLoc(D))
325 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000326 DeclContext *DC = D->getDeclContext();
327 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000328 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000329 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000330 Out << D->getName();
331}
332
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000333bool USRGenerator::GenLoc(const Decl *D) {
334 if (generatedLoc)
335 return IgnoreResults;
336 generatedLoc = true;
337
Ted Kremenek6f153952010-04-15 21:51:13 +0000338 const SourceManager &SM = AU->getSourceManager();
339 SourceLocation L = D->getLocStart();
340 if (L.isInvalid()) {
341 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000342 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000343 }
344 L = SM.getInstantiationLoc(L);
345 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
346 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000347 if (FE) {
348 llvm::sys::Path P(FE->getName());
349 Out << P.getLast();
350 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000351 else {
352 // This case really isn't interesting.
353 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000354 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000355 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000356 Out << '@'
357 << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
358 << SM.getColumnNumber(Decomposed.first, Decomposed.second);
Ted Kremenek8e672192010-05-07 01:04:32 +0000359
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000360 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000361}
362
Ted Kremenek8e672192010-05-07 01:04:32 +0000363void USRGenerator::VisitType(QualType T) {
364 // This method mangles in USR information for types. It can possibly
365 // just reuse the naming-mangling logic used by codegen, although the
366 // requirements for USRs might not be the same.
367 do {
368 T = T.getTypePtr()->getCanonicalTypeInternal();
369 Qualifiers Q = T.getQualifiers();
370 if (Q.hasConst())
371 Out << '1';
372 if (Q.hasVolatile())
373 Out << '2';
374 if (Q.hasRestrict())
375 Out << '3';
376
377 // Mangle in ObjC GC qualifiers?
378
379 if (const PointerType *PT = T->getAs<PointerType>()) {
380 Out << '*';
381 T = PT->getPointeeType();
382 continue;
383 }
384 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
385 Out << '&';
386 T = RT->getPointeeType();
387 continue;
388 }
389 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
390 Out << 'F';
391 VisitType(FT->getResultType());
392 for (FunctionProtoType::arg_type_iterator
393 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
394 VisitType(*I);
395 }
396 if (FT->isVariadic())
397 Out << '.';
398 return;
399 }
400 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
401 Out << 'B';
402 T = BT->getPointeeType();
403 continue;
404 }
405 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
406 unsigned char c = '\0';
407 switch (BT->getKind()) {
408 case BuiltinType::Void:
409 c = 'v'; break;
410 case BuiltinType::Bool:
411 c = 'b'; break;
412 case BuiltinType::Char_U:
413 case BuiltinType::UChar:
414 c = 'c'; break;
415 case BuiltinType::Char16:
416 c = 'q'; break;
417 case BuiltinType::Char32:
418 c = 'w'; break;
419 case BuiltinType::UShort:
420 c = 's'; break;
421 case BuiltinType::UInt:
422 c = 'i'; break;
423 case BuiltinType::ULong:
424 c = 'l'; break;
425 case BuiltinType::ULongLong:
426 c = 'k'; break;
427 case BuiltinType::UInt128:
428 c = 'j'; break;
429 case BuiltinType::Char_S:
430 case BuiltinType::SChar:
431 c = 'C'; break;
432 case BuiltinType::WChar:
433 c = 'W'; break;
434 case BuiltinType::Short:
435 c = 'S'; break;
436 case BuiltinType::Int:
437 c = 'I'; break;
438 case BuiltinType::Long:
439 c = 'L'; break;
440 case BuiltinType::LongLong:
441 c = 'K'; break;
442 case BuiltinType::Int128:
443 c = 'J'; break;
444 case BuiltinType::Float:
445 c = 'f'; break;
446 case BuiltinType::Double:
447 c = 'd'; break;
448 case BuiltinType::LongDouble:
449 c = 'D'; break;
450 case BuiltinType::NullPtr:
451 c = 'n'; break;
452 case BuiltinType::Overload:
453 case BuiltinType::Dependent:
454 case BuiltinType::UndeducedAuto:
455 IgnoreResults = true;
456 return;
457 case BuiltinType::ObjCId:
458 c = 'o'; break;
459 case BuiltinType::ObjCClass:
460 c = 'O'; break;
461 case BuiltinType::ObjCSel:
462 c = 'e'; break;
463 }
464 Out << c;
465 return;
466 }
467 if (const ComplexType *CT = T->getAs<ComplexType>()) {
468 Out << '<';
469 T = CT->getElementType();
470 continue;
471 }
472
473 // Unhandled type.
474 Out << ' ';
475 break;
476 } while (true);
477}
478
Ted Kremenek896b70f2010-03-13 02:50:34 +0000479//===----------------------------------------------------------------------===//
480// General purpose USR generation methods.
481//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000482
Ted Kremenek896b70f2010-03-13 02:50:34 +0000483void USRGenerator::GenNamedDecl(llvm::StringRef name) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000484 Out << "@" << name;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000485}
486
487void USRGenerator::GenObjCClass(llvm::StringRef cls) {
488 Out << "objc(cs)" << cls;
489}
490
491void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000492 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000493}
494
495void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
496 GenNamedDecl(ivar);
497}
498
499void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
500 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
501}
502
503void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
504 Out << "(py)" << prop;
505}
506
507void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
508 Out << "objc(pl)" << prot;
509}
510
511//===----------------------------------------------------------------------===//
512// API hooks.
513//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000514
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000515static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
516 return s.startswith("c:") ? s.substr(2) : "";
517}
518
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000519static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000520 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000521
522 // Don't generate USRs for things with invalid locations.
523 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000524 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000525
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000526 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000527 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000528 switch (ND->getLinkage()) {
529 case ExternalLinkage:
530 // Generate USRs for all entities with external linkage.
531 break;
532 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000533 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000534 // We allow enums, typedefs, and structs that have no linkage to
535 // have USRs that are anchored to the file they were defined in
536 // (e.g., the header). This is a little gross, but in principal
537 // enums/anonymous structs/etc. defined in a common header file
538 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000539 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000540 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000541 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000542 break;
543 // Fall-through.
544 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000545 if (isa<FunctionDecl>(ND))
546 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000547 }
548
549 StringUSRGenerator SUG(&C);
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000550 SUG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000551
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000552 if (SUG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000553 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000554
Ted Kremeneke542f772010-04-20 23:15:40 +0000555 // For development testing.
556 // assert(SUG.str().size() > 2);
557
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000558 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek896b70f2010-03-13 02:50:34 +0000559 return createCXString(SUG.str(), true);
560}
561
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000562extern "C" {
563
564CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000565 const CXCursorKind &K = clang_getCursorKind(C);
566
567 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000568 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000569
570 if (K == CXCursor_MacroDefinition) {
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000571 StringUSRGenerator SUG(&C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000572 SUG << "macro@"
573 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
574 return createCXString(SUG.str(), true);
575 }
576
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000577 return createCXString("");
578}
579
Ted Kremenek896b70f2010-03-13 02:50:34 +0000580CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
581 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000582 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000583 SUG->GenObjCIvar(name);
584 return createCXString(SUG.str(), true);
585}
586
587CXString clang_constructUSR_ObjCMethod(const char *name,
588 unsigned isInstanceMethod,
589 CXString classUSR) {
590 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000591 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000592 SUG->GenObjCMethod(name, isInstanceMethod);
593 return createCXString(SUG.str(), true);
594}
595
596CXString clang_constructUSR_ObjCClass(const char *name) {
597 StringUSRGenerator SUG;
598 SUG->GenObjCClass(name);
599 return createCXString(SUG.str(), true);
600}
601
602CXString clang_constructUSR_ObjCProtocol(const char *name) {
603 StringUSRGenerator SUG;
604 SUG->GenObjCProtocol(name);
605 return createCXString(SUG.str(), true);
606}
607
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000608CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000609 const char *category_name) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000610 StringUSRGenerator SUG;
611 SUG->GenObjCCategory(class_name, category_name);
612 return createCXString(SUG.str(), true);
613}
614
615CXString clang_constructUSR_ObjCProperty(const char *property,
616 CXString classUSR) {
617 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000618 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000619 SUG->GenObjCProperty(property);
620 return createCXString(SUG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000621}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000622
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000623} // end extern "C"