blob: f6e9aa358e2202342d88e823b90a85d10bf6fc4e [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"
Ted Kremeneked122732010-11-16 01:56:27 +000016#include "CXString.h"
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000017#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000018#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000019#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000020#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000023
Benjamin Kramerb846deb2010-04-12 19:45:50 +000024using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000025using namespace clang::cxstring;
26
Ted Kremenekc50277f2010-01-12 23:33:42 +000027//===----------------------------------------------------------------------===//
28// USR generation.
29//===----------------------------------------------------------------------===//
30
31namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000032class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenekf0199432010-11-16 08:15:38 +000033 llvm::OwningPtr<llvm::SmallString<128> > OwnedBuf;
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 SmallVectorImpl<char> &Buf;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000035 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000036 bool IgnoreResults;
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +000037 ASTContext *Context;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000038 bool generatedLoc;
Douglas Gregor66b7fbf2010-09-20 20:37:39 +000039
40 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
41
Ted Kremenek2fee4e62010-01-14 01:50:21 +000042public:
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +000043 explicit USRGenerator(ASTContext *Ctx = 0, SmallVectorImpl<char> *extBuf = 0)
Ted Kremenekf0199432010-11-16 08:15:38 +000044 : OwnedBuf(extBuf ? 0 : new llvm::SmallString<128>()),
45 Buf(extBuf ? *extBuf : *OwnedBuf.get()),
46 Out(Buf),
47 IgnoreResults(false),
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +000048 Context(Ctx),
Ted Kremenekf0199432010-11-16 08:15:38 +000049 generatedLoc(false)
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000050 {
51 // Add the USR space prefix.
52 Out << "c:";
53 }
54
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 StringRef str() {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000056 return Out.str();
57 }
58
59 USRGenerator* operator->() { return this; }
60
61 template <typename T>
62 llvm::raw_svector_ostream &operator<<(const T &x) {
63 Out << x;
64 return Out;
65 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000066
Ted Kremenek3adca6d2010-01-18 22:02:49 +000067 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000068
69 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000070 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000071 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000072 void VisitFunctionDecl(FunctionDecl *D);
73 void VisitNamedDecl(NamedDecl *D);
74 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +000075 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000076 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +000077 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000078 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000079 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000080 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000081 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000082 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000083 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000084 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000085 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000086 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000087 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000088 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
89 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000090 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
91 IgnoreResults = true;
Ted Kremenek8e672192010-05-07 01:04:32 +000092 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000093 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
94 IgnoreResults = true;
95 }
Douglas Gregor7e242562010-09-01 19:52:22 +000096 void VisitUsingDecl(UsingDecl *D) {
97 IgnoreResults = true;
98 }
99 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
100 IgnoreResults = true;
101 }
102 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
103 IgnoreResults = true;
104 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000105
Ted Kremenek6f153952010-04-15 21:51:13 +0000106 /// Generate the string component containing the location of the
107 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000108 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +0000109
Ted Kremenek896b70f2010-03-13 02:50:34 +0000110 /// String generation methods used both by the visitation methods
111 /// and from other clients that want to directly generate USRs. These
112 /// methods do not construct complete USRs (which incorporate the parents
113 /// of an AST element), but only the fragments concerning the AST element
114 /// itself.
115
Ted Kremenek896b70f2010-03-13 02:50:34 +0000116 /// Generate a USR for an Objective-C class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 void GenObjCClass(StringRef cls);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000118 /// Generate a USR for an Objective-C class category.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 void GenObjCCategory(StringRef cls, StringRef cat);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000120 /// Generate a USR fragment for an Objective-C instance variable. The
121 /// complete USR can be created by concatenating the USR for the
122 /// encompassing class with this USR fragment.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 void GenObjCIvar(StringRef ivar);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000124 /// Generate a USR fragment for an Objective-C method.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 void GenObjCMethod(StringRef sel, bool isInstanceMethod);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000126 /// Generate a USR fragment for an Objective-C property.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 void GenObjCProperty(StringRef prop);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000128 /// Generate a USR for an Objective-C protocol.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 void GenObjCProtocol(StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000130
131 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000132 void VisitTemplateParameterList(const TemplateParameterList *Params);
133 void VisitTemplateName(TemplateName Name);
134 void VisitTemplateArgument(const TemplateArgument &Arg);
135
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000136 /// Emit a Decl's name using NamedDecl::printName() and return true if
137 /// the decl had no name.
138 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000139};
140
Ted Kremenekc50277f2010-01-12 23:33:42 +0000141} // end anonymous namespace
142
Ted Kremenek896b70f2010-03-13 02:50:34 +0000143//===----------------------------------------------------------------------===//
144// Generating USRs from ASTS.
145//===----------------------------------------------------------------------===//
146
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000147bool USRGenerator::EmitDeclName(const NamedDecl *D) {
148 Out.flush();
149 const unsigned startSize = Buf.size();
150 D->printName(Out);
151 Out.flush();
152 const unsigned endSize = Buf.size();
153 return startSize == endSize;
154}
155
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000156static bool InAnonymousNamespace(const Decl *D) {
157 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
158 return ND->isAnonymousNamespace();
159 return false;
160}
161
162static inline bool ShouldGenerateLocation(const NamedDecl *D) {
163 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
164}
165
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000166void USRGenerator::VisitDeclContext(DeclContext *DC) {
167 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
168 Visit(D);
169}
170
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000171void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000172 VisitDeclContext(D->getDeclContext());
173 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
174 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000175 // Bit fields can be anonymous.
176 IgnoreResults = true;
177 return;
178 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000179}
180
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000181void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000182 if (ShouldGenerateLocation(D) && GenLoc(D))
183 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000184
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000185 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000186 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
187 Out << "@FT@";
188 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
189 } else
190 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000191 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000192
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000193 ASTContext &Ctx = *Context;
Ted Kremenek8e672192010-05-07 01:04:32 +0000194 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
195 return;
196
197 // Mangle in type information for the arguments.
198 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
199 I != E; ++I) {
200 Out << '#';
201 if (ParmVarDecl *PD = *I)
202 VisitType(PD->getType());
203 }
204 if (D->isVariadic())
205 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000206 Out << '#';
207 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
208 if (MD->isStatic())
209 Out << 'S';
210 if (unsigned quals = MD->getTypeQualifiers())
211 Out << (char)('0' + quals);
212 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000213}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000214
215void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000216 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000217 Out << "@";
218
219 if (EmitDeclName(D)) {
220 // The string can be empty if the declaration has no name; e.g., it is
221 // the ParmDecl with no name for declaration of a function pointer type,
222 // e.g.: void (*f)(void *);
223 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000224 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000225 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000226}
227
Ted Kremeneke542f772010-04-20 23:15:40 +0000228void USRGenerator::VisitVarDecl(VarDecl *D) {
229 // VarDecls can be declared 'extern' within a function or method body,
230 // but their enclosing DeclContext is the function, not the TU. We need
231 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000232 if (ShouldGenerateLocation(D) && GenLoc(D))
233 return;
234
235 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000236
Ted Kremenekcf999102010-04-29 17:43:29 +0000237 // Variables always have simple names.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 StringRef s = D->getName();
Ted Kremenekcf999102010-04-29 17:43:29 +0000239
Ted Kremeneke542f772010-04-20 23:15:40 +0000240 // The string can be empty if the declaration has no name; e.g., it is
241 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000242 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000243 // In this case, don't generate a USR.
244 if (s.empty())
245 IgnoreResults = true;
246 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000247 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000248}
249
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000250void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
251 GenLoc(D);
252 return;
253}
254
255void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
256 GenLoc(D);
257 return;
258}
259
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000260void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000261 if (D->isAnonymousNamespace()) {
262 Out << "@aN";
263 return;
264 }
265
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000266 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000267 if (!IgnoreResults)
268 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000269}
270
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000271void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
272 VisitFunctionDecl(D->getTemplatedDecl());
273}
274
Douglas Gregor39d6f072010-08-31 19:02:00 +0000275void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
276 VisitTagDecl(D->getTemplatedDecl());
277}
278
Douglas Gregor69319002010-08-31 23:48:11 +0000279void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
280 VisitDeclContext(D->getDeclContext());
281 if (!IgnoreResults)
282 Out << "@NA@" << D->getName();
283}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000284
Ted Kremenekc50277f2010-01-12 23:33:42 +0000285void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000286 DeclContext *container = D->getDeclContext();
287 if (ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
288 Visit(pd);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000289 }
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000290 else {
291 // The USR for a method declared in a class extension or category is based on
292 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
293 ObjCInterfaceDecl *ID = D->getClassInterface();
294 if (!ID) {
295 IgnoreResults = true;
296 return;
297 }
298 Visit(ID);
299 }
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000300 // Ideally we would use 'GenObjCMethod', but this is such a hot path
301 // for Objective-C code that we don't want to use
302 // DeclarationName::getAsString().
303 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
304 DeclarationName N(D->getSelector());
305 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000306}
307
Ted Kremeneke74ef122010-04-16 21:31:52 +0000308void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
309 // FIXME: @class declarations can refer to multiple classes. We need
310 // to be able to traverse these.
311 IgnoreResults = true;
312}
313
314void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
315 // FIXME: @protocol declarations can refer to multiple protocols. We need
316 // to be able to traverse these.
317 IgnoreResults = true;
318}
319
Ted Kremenekc50277f2010-01-12 23:33:42 +0000320void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
321 switch (D->getKind()) {
322 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000323 llvm_unreachable("Invalid ObjC container.");
Ted Kremenekc50277f2010-01-12 23:33:42 +0000324 case Decl::ObjCInterface:
325 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000326 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000327 break;
328 case Decl::ObjCCategory: {
329 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000330 ObjCInterfaceDecl *ID = CD->getClassInterface();
331 if (!ID) {
332 // Handle invalid code where the @interface might not
333 // have been specified.
334 // FIXME: We should be able to generate this USR even if the
335 // @interface isn't available.
336 IgnoreResults = true;
337 return;
338 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000339 if (CD->IsClassExtension()) {
Argyrios Kyrtzidis46d1ea42011-10-18 16:50:06 +0000340 // An extension semantically continues the interface of the class.
341 GenObjCClass(ID->getName());
Ted Kremenek28a7f252010-08-24 23:13:41 +0000342 }
343 else
344 GenObjCCategory(ID->getName(), CD->getName());
345
Ted Kremenekc50277f2010-01-12 23:33:42 +0000346 break;
347 }
348 case Decl::ObjCCategoryImpl: {
349 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000350 ObjCInterfaceDecl *ID = CD->getClassInterface();
351 if (!ID) {
352 // Handle invalid code where the @interface might not
353 // have been specified.
354 // FIXME: We should be able to generate this USR even if the
355 // @interface isn't available.
356 IgnoreResults = true;
357 return;
358 }
359 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000360 break;
361 }
362 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000363 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000364 break;
365 }
366}
367
368void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
369 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000370 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000371}
372
Ted Kremeneke542f772010-04-20 23:15:40 +0000373void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
374 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
375 VisitObjCPropertyDecl(PD);
376 return;
377 }
378
379 IgnoreResults = true;
380}
381
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000382void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000383 // Add the location of the tag decl to handle resolution across
384 // translation units.
385 if (ShouldGenerateLocation(D) && GenLoc(D))
386 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000387
Ted Kremenek6f153952010-04-15 21:51:13 +0000388 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000389 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000390
Douglas Gregor74dbe642010-08-31 19:31:58 +0000391 bool AlreadyStarted = false;
392 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000393 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000394 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000395
396 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000397 case TTK_Struct: Out << "@ST"; break;
398 case TTK_Class: Out << "@CT"; break;
399 case TTK_Union: Out << "@UT"; break;
400 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000401 }
402 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000403 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
404 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
405 AlreadyStarted = true;
406
407 switch (D->getTagKind()) {
408 case TTK_Struct: Out << "@SP"; break;
409 case TTK_Class: Out << "@CP"; break;
410 case TTK_Union: Out << "@UP"; break;
411 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
412 }
413 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000414 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000415 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000416
Douglas Gregor74dbe642010-08-31 19:31:58 +0000417 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000418 switch (D->getTagKind()) {
419 case TTK_Struct: Out << "@S"; break;
420 case TTK_Class: Out << "@C"; break;
421 case TTK_Union: Out << "@U"; break;
422 case TTK_Enum: Out << "@E"; break;
423 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000424 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000425
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000426 Out << '@';
427 Out.flush();
428 assert(Buf.size() > 0);
429 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000430
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000431 if (EmitDeclName(D)) {
Richard Smith162e1c12011-04-15 14:24:37 +0000432 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000433 Buf[off] = 'A';
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000434 Out << '@' << *TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000435 }
436 else
437 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000438 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000439
440 // For a class template specialization, mangle the template arguments.
441 if (ClassTemplateSpecializationDecl *Spec
442 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
443 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
444 Out << '>';
445 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
446 Out << '#';
447 VisitTemplateArgument(Args.get(I));
448 }
449 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000450}
451
Ted Kremenekc50277f2010-01-12 23:33:42 +0000452void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000453 if (ShouldGenerateLocation(D) && GenLoc(D))
454 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000455 DeclContext *DC = D->getDeclContext();
456 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000457 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000458 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000459 Out << D->getName();
460}
461
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000462void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
463 GenLoc(D);
464 return;
465}
466
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000467bool USRGenerator::GenLoc(const Decl *D) {
468 if (generatedLoc)
469 return IgnoreResults;
470 generatedLoc = true;
Ted Kremenek8c367582011-04-29 21:35:23 +0000471
472 // Guard against null declarations in invalid code.
473 if (!D) {
474 IgnoreResults = true;
475 return true;
476 }
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000477
Ted Kremenek1d8052d2011-05-03 01:33:35 +0000478 // Use the location of canonical decl.
479 D = D->getCanonicalDecl();
480
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000481 const SourceManager &SM = Context->getSourceManager();
Ted Kremenek6f153952010-04-15 21:51:13 +0000482 SourceLocation L = D->getLocStart();
483 if (L.isInvalid()) {
484 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000485 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000486 }
Chandler Carruth40278532011-07-25 16:49:02 +0000487 L = SM.getExpansionLoc(L);
Ted Kremenek6f153952010-04-15 21:51:13 +0000488 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
489 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000490 if (FE) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000491 Out << llvm::sys::path::filename(FE->getName());
Ted Kremeneke74ef122010-04-16 21:31:52 +0000492 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000493 else {
494 // This case really isn't interesting.
495 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000496 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000497 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000498 // Use the offest into the FileID to represent the location. Using
499 // a line/column can cause us to look back at the original source file,
500 // which is expensive.
501 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000502 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000503}
504
Ted Kremenek8e672192010-05-07 01:04:32 +0000505void USRGenerator::VisitType(QualType T) {
506 // This method mangles in USR information for types. It can possibly
507 // just reuse the naming-mangling logic used by codegen, although the
508 // requirements for USRs might not be the same.
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000509 ASTContext &Ctx = *Context;
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000510
Ted Kremenek8e672192010-05-07 01:04:32 +0000511 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000512 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000513 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000514 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000515 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000516 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000517 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000518 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000519 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000520 qVal |= 0x4;
521 if(qVal)
522 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000523
524 // Mangle in ObjC GC qualifiers?
525
Douglas Gregorba73ada2011-01-19 20:50:07 +0000526 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
527 Out << 'P';
528 T = Expansion->getPattern();
529 }
530
Ted Kremenek8e672192010-05-07 01:04:32 +0000531 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
532 unsigned char c = '\0';
533 switch (BT->getKind()) {
534 case BuiltinType::Void:
535 c = 'v'; break;
536 case BuiltinType::Bool:
537 c = 'b'; break;
538 case BuiltinType::Char_U:
539 case BuiltinType::UChar:
540 c = 'c'; break;
541 case BuiltinType::Char16:
542 c = 'q'; break;
543 case BuiltinType::Char32:
544 c = 'w'; break;
545 case BuiltinType::UShort:
546 c = 's'; break;
547 case BuiltinType::UInt:
548 c = 'i'; break;
549 case BuiltinType::ULong:
550 c = 'l'; break;
551 case BuiltinType::ULongLong:
552 c = 'k'; break;
553 case BuiltinType::UInt128:
554 c = 'j'; break;
555 case BuiltinType::Char_S:
556 case BuiltinType::SChar:
557 c = 'C'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000558 case BuiltinType::WChar_S:
559 case BuiltinType::WChar_U:
Ted Kremenek8e672192010-05-07 01:04:32 +0000560 c = 'W'; break;
561 case BuiltinType::Short:
562 c = 'S'; break;
563 case BuiltinType::Int:
564 c = 'I'; break;
565 case BuiltinType::Long:
566 c = 'L'; break;
567 case BuiltinType::LongLong:
568 c = 'K'; break;
569 case BuiltinType::Int128:
570 c = 'J'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000571 case BuiltinType::Half:
572 c = 'h'; break;
Ted Kremenek8e672192010-05-07 01:04:32 +0000573 case BuiltinType::Float:
574 c = 'f'; break;
575 case BuiltinType::Double:
576 c = 'd'; break;
577 case BuiltinType::LongDouble:
578 c = 'D'; break;
579 case BuiltinType::NullPtr:
580 c = 'n'; break;
581 case BuiltinType::Overload:
John McCall864c0412011-04-26 20:42:42 +0000582 case BuiltinType::BoundMember:
Ted Kremenek8e672192010-05-07 01:04:32 +0000583 case BuiltinType::Dependent:
John McCall1de4d4e2011-04-07 08:22:57 +0000584 case BuiltinType::UnknownAny:
John McCall0ddaeb92011-10-17 18:09:15 +0000585 case BuiltinType::ARCUnbridgedCast:
Ted Kremenek8e672192010-05-07 01:04:32 +0000586 IgnoreResults = true;
587 return;
588 case BuiltinType::ObjCId:
589 c = 'o'; break;
590 case BuiltinType::ObjCClass:
591 c = 'O'; break;
592 case BuiltinType::ObjCSel:
593 c = 'e'; break;
594 }
595 Out << c;
596 return;
597 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000598
599 // If we have already seen this (non-built-in) type, use a substitution
600 // encoding.
601 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
602 = TypeSubstitutions.find(T.getTypePtr());
603 if (Substitution != TypeSubstitutions.end()) {
604 Out << 'S' << Substitution->second << '_';
605 return;
606 } else {
607 // Record this as a substitution.
608 unsigned Number = TypeSubstitutions.size();
609 TypeSubstitutions[T.getTypePtr()] = Number;
610 }
611
612 if (const PointerType *PT = T->getAs<PointerType>()) {
613 Out << '*';
614 T = PT->getPointeeType();
615 continue;
616 }
617 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
618 Out << '&';
619 T = RT->getPointeeType();
620 continue;
621 }
622 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
623 Out << 'F';
624 VisitType(FT->getResultType());
625 for (FunctionProtoType::arg_type_iterator
626 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
627 VisitType(*I);
628 }
629 if (FT->isVariadic())
630 Out << '.';
631 return;
632 }
633 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
634 Out << 'B';
635 T = BT->getPointeeType();
636 continue;
637 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000638 if (const ComplexType *CT = T->getAs<ComplexType>()) {
639 Out << '<';
640 T = CT->getElementType();
641 continue;
642 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000643 if (const TagType *TT = T->getAs<TagType>()) {
644 Out << '$';
645 VisitTagDecl(TT->getDecl());
646 return;
647 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000648 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
649 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
650 return;
651 }
652 if (const TemplateSpecializationType *Spec
653 = T->getAs<TemplateSpecializationType>()) {
654 Out << '>';
655 VisitTemplateName(Spec->getTemplateName());
656 Out << Spec->getNumArgs();
657 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
658 VisitTemplateArgument(Spec->getArg(I));
659 return;
660 }
661
Ted Kremenek8e672192010-05-07 01:04:32 +0000662 // Unhandled type.
663 Out << ' ';
664 break;
665 } while (true);
666}
667
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000668void USRGenerator::VisitTemplateParameterList(
669 const TemplateParameterList *Params) {
670 if (!Params)
671 return;
672 Out << '>' << Params->size();
673 for (TemplateParameterList::const_iterator P = Params->begin(),
674 PEnd = Params->end();
675 P != PEnd; ++P) {
676 Out << '#';
677 if (isa<TemplateTypeParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000678 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
679 Out<< 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000680 Out << 'T';
681 continue;
682 }
683
684 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000685 if (NTTP->isParameterPack())
686 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000687 Out << 'N';
688 VisitType(NTTP->getType());
689 continue;
690 }
691
692 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
Douglas Gregorba73ada2011-01-19 20:50:07 +0000693 if (TTP->isParameterPack())
694 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000695 Out << 't';
696 VisitTemplateParameterList(TTP->getTemplateParameters());
697 }
698}
699
700void USRGenerator::VisitTemplateName(TemplateName Name) {
701 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
702 if (TemplateTemplateParmDecl *TTP
703 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
704 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
705 return;
706 }
707
708 Visit(Template);
709 return;
710 }
711
712 // FIXME: Visit dependent template names.
713}
714
715void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
716 switch (Arg.getKind()) {
717 case TemplateArgument::Null:
718 break;
719
720 case TemplateArgument::Declaration:
Douglas Gregor97475832010-10-05 18:37:06 +0000721 if (Decl *D = Arg.getAsDecl())
722 Visit(D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000723 break;
724
Douglas Gregora7fc9012011-01-05 18:58:31 +0000725 case TemplateArgument::TemplateExpansion:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000726 Out << 'P'; // pack expansion of...
727 // Fall through
728 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000729 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000730 break;
731
732 case TemplateArgument::Expression:
733 // FIXME: Visit expressions.
734 break;
735
736 case TemplateArgument::Pack:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000737 Out << 'p' << Arg.pack_size();
738 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
739 P != PEnd; ++P)
740 VisitTemplateArgument(*P);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000741 break;
742
743 case TemplateArgument::Type:
744 VisitType(Arg.getAsType());
745 break;
746
747 case TemplateArgument::Integral:
748 Out << 'V';
749 VisitType(Arg.getIntegralType());
750 Out << *Arg.getAsIntegral();
751 break;
752 }
753}
754
Ted Kremenek896b70f2010-03-13 02:50:34 +0000755//===----------------------------------------------------------------------===//
756// General purpose USR generation methods.
757//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000758
Chris Lattner5f9e2722011-07-23 10:55:15 +0000759void USRGenerator::GenObjCClass(StringRef cls) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000760 Out << "objc(cs)" << cls;
761}
762
Chris Lattner5f9e2722011-07-23 10:55:15 +0000763void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000764 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000765}
766
Chris Lattner5f9e2722011-07-23 10:55:15 +0000767void USRGenerator::GenObjCIvar(StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000768 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000769}
770
Chris Lattner5f9e2722011-07-23 10:55:15 +0000771void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000772 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
773}
774
Chris Lattner5f9e2722011-07-23 10:55:15 +0000775void USRGenerator::GenObjCProperty(StringRef prop) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000776 Out << "(py)" << prop;
777}
778
Chris Lattner5f9e2722011-07-23 10:55:15 +0000779void USRGenerator::GenObjCProtocol(StringRef prot) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000780 Out << "objc(pl)" << prot;
781}
782
783//===----------------------------------------------------------------------===//
784// API hooks.
785//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000786
Chris Lattner5f9e2722011-07-23 10:55:15 +0000787static inline StringRef extractUSRSuffix(StringRef s) {
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000788 return s.startswith("c:") ? s.substr(2) : "";
789}
790
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000791bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000792 // Don't generate USRs for things with invalid locations.
793 if (!D || D->getLocStart().isInvalid())
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000794 return true;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000795
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000796 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000797 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000798 switch (ND->getLinkage()) {
799 case ExternalLinkage:
800 // Generate USRs for all entities with external linkage.
801 break;
802 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000803 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000804 // We allow enums, typedefs, and structs that have no linkage to
805 // have USRs that are anchored to the file they were defined in
806 // (e.g., the header). This is a little gross, but in principal
807 // enums/anonymous structs/etc. defined in a common header file
808 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000809 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000810 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000811 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000812 break;
813 // Fall-through.
814 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000815 if (isa<FunctionDecl>(ND))
816 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000817 }
818
Ted Kremenekf0199432010-11-16 08:15:38 +0000819 {
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000820 USRGenerator UG(&D->getASTContext(), &Buf);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000821 UG->Visit((Decl*)D);
Ted Kremeneke542f772010-04-20 23:15:40 +0000822
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000823 if (UG->ignoreResults())
824 return true;
Ted Kremenekf0199432010-11-16 08:15:38 +0000825 }
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000826
827 return false;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000828}
829
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000830extern "C" {
831
832CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000833 const CXCursorKind &K = clang_getCursorKind(C);
834
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000835 if (clang_isDeclaration(K)) {
836 Decl *D = cxcursor::getCursorDecl(C);
837 CXTranslationUnit TU = cxcursor::getCursorTU(C);
838 if (!TU)
839 return createCXString("");
840
841 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
842 if (!buf)
843 return createCXString("");
844
845 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
846 if (Ignore) {
847 disposeCXStringBuf(buf);
848 return createCXString("");
849 }
850
851 // Return the C-string, but don't make a copy since it is already in
852 // the string buffer.
853 buf->Data.push_back('\0');
854 return createCXString(buf);
855 }
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000856
857 if (K == CXCursor_MacroDefinition) {
Ted Kremenekf0199432010-11-16 08:15:38 +0000858 CXTranslationUnit TU = cxcursor::getCursorTU(C);
859 if (!TU)
860 return createCXString("");
861
862 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
863 if (!buf)
864 return createCXString("");
865
866 {
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000867 USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(),
868 &buf->Data);
Ted Kremenekf0199432010-11-16 08:15:38 +0000869 UG << "macro@"
870 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
871 }
872 buf->Data.push_back('\0');
873 return createCXString(buf);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000874 }
875
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000876 return createCXString("");
877}
878
Ted Kremenek896b70f2010-03-13 02:50:34 +0000879CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000880 USRGenerator UG;
881 UG << extractUSRSuffix(clang_getCString(classUSR));
882 UG->GenObjCIvar(name);
883 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000884}
885
886CXString clang_constructUSR_ObjCMethod(const char *name,
887 unsigned isInstanceMethod,
888 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000889 USRGenerator UG;
890 UG << extractUSRSuffix(clang_getCString(classUSR));
891 UG->GenObjCMethod(name, isInstanceMethod);
892 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000893}
894
895CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000896 USRGenerator UG;
897 UG->GenObjCClass(name);
898 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000899}
900
901CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000902 USRGenerator UG;
903 UG->GenObjCProtocol(name);
904 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000905}
906
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000907CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000908 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000909 USRGenerator UG;
910 UG->GenObjCCategory(class_name, category_name);
911 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000912}
913
914CXString clang_constructUSR_ObjCProperty(const char *property,
915 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000916 USRGenerator UG;
917 UG << extractUSRSuffix(clang_getCString(classUSR));
918 UG->GenObjCProperty(property);
919 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000920}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000921
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000922} // end extern "C"