blob: 40d4fb1c694642b2fa9d3283d07d58ffc91874fc [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) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000172 // The USR for an ivar declared in a class extension is based on the
173 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
174 if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
175 Visit(ID);
176 else
177 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000178 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
179 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000180 // Bit fields can be anonymous.
181 IgnoreResults = true;
182 return;
183 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000184}
185
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000186void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000187 if (ShouldGenerateLocation(D) && GenLoc(D))
188 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000189
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000190 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000191 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
192 Out << "@FT@";
193 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
194 } else
195 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000196 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000197
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000198 ASTContext &Ctx = *Context;
Ted Kremenek8e672192010-05-07 01:04:32 +0000199 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
200 return;
201
202 // Mangle in type information for the arguments.
203 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
204 I != E; ++I) {
205 Out << '#';
206 if (ParmVarDecl *PD = *I)
207 VisitType(PD->getType());
208 }
209 if (D->isVariadic())
210 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000211 Out << '#';
212 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
213 if (MD->isStatic())
214 Out << 'S';
215 if (unsigned quals = MD->getTypeQualifiers())
216 Out << (char)('0' + quals);
217 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000218}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000219
220void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000221 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000222 Out << "@";
223
224 if (EmitDeclName(D)) {
225 // The string can be empty if the declaration has no name; e.g., it is
226 // the ParmDecl with no name for declaration of a function pointer type,
227 // e.g.: void (*f)(void *);
228 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000229 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000230 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000231}
232
Ted Kremeneke542f772010-04-20 23:15:40 +0000233void USRGenerator::VisitVarDecl(VarDecl *D) {
234 // VarDecls can be declared 'extern' within a function or method body,
235 // but their enclosing DeclContext is the function, not the TU. We need
236 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000237 if (ShouldGenerateLocation(D) && GenLoc(D))
238 return;
239
240 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000241
Ted Kremenekcf999102010-04-29 17:43:29 +0000242 // Variables always have simple names.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000243 StringRef s = D->getName();
Ted Kremenekcf999102010-04-29 17:43:29 +0000244
Ted Kremeneke542f772010-04-20 23:15:40 +0000245 // The string can be empty if the declaration has no name; e.g., it is
246 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000247 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000248 // In this case, don't generate a USR.
249 if (s.empty())
250 IgnoreResults = true;
251 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000252 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000253}
254
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000255void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
256 GenLoc(D);
257 return;
258}
259
260void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
261 GenLoc(D);
262 return;
263}
264
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000265void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000266 if (D->isAnonymousNamespace()) {
267 Out << "@aN";
268 return;
269 }
270
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000271 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000272 if (!IgnoreResults)
273 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000274}
275
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000276void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
277 VisitFunctionDecl(D->getTemplatedDecl());
278}
279
Douglas Gregor39d6f072010-08-31 19:02:00 +0000280void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
281 VisitTagDecl(D->getTemplatedDecl());
282}
283
Douglas Gregor69319002010-08-31 23:48:11 +0000284void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
285 VisitDeclContext(D->getDeclContext());
286 if (!IgnoreResults)
287 Out << "@NA@" << D->getName();
288}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000289
Ted Kremenekc50277f2010-01-12 23:33:42 +0000290void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000291 DeclContext *container = D->getDeclContext();
292 if (ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
293 Visit(pd);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000294 }
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000295 else {
296 // The USR for a method declared in a class extension or category is based on
297 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
298 ObjCInterfaceDecl *ID = D->getClassInterface();
299 if (!ID) {
300 IgnoreResults = true;
301 return;
302 }
303 Visit(ID);
304 }
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000305 // Ideally we would use 'GenObjCMethod', but this is such a hot path
306 // for Objective-C code that we don't want to use
307 // DeclarationName::getAsString().
308 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
309 DeclarationName N(D->getSelector());
310 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000311}
312
Ted Kremeneke74ef122010-04-16 21:31:52 +0000313void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
314 // FIXME: @class declarations can refer to multiple classes. We need
315 // to be able to traverse these.
316 IgnoreResults = true;
317}
318
319void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
320 // FIXME: @protocol declarations can refer to multiple protocols. We need
321 // to be able to traverse these.
322 IgnoreResults = true;
323}
324
Ted Kremenekc50277f2010-01-12 23:33:42 +0000325void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
326 switch (D->getKind()) {
327 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000328 llvm_unreachable("Invalid ObjC container.");
Ted Kremenekc50277f2010-01-12 23:33:42 +0000329 case Decl::ObjCInterface:
330 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000331 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000332 break;
333 case Decl::ObjCCategory: {
334 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000335 ObjCInterfaceDecl *ID = CD->getClassInterface();
336 if (!ID) {
337 // Handle invalid code where the @interface might not
338 // have been specified.
339 // FIXME: We should be able to generate this USR even if the
340 // @interface isn't available.
341 IgnoreResults = true;
342 return;
343 }
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000344 // Specially handle class extensions, which are anonymous categories.
345 // We want to mangle in the location to uniquely distinguish them.
Ted Kremenek28a7f252010-08-24 23:13:41 +0000346 if (CD->IsClassExtension()) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000347 Out << "objc(ext)" << ID->getName() << '@';
348 GenLoc(CD);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000349 }
350 else
351 GenObjCCategory(ID->getName(), CD->getName());
352
Ted Kremenekc50277f2010-01-12 23:33:42 +0000353 break;
354 }
355 case Decl::ObjCCategoryImpl: {
356 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000357 ObjCInterfaceDecl *ID = CD->getClassInterface();
358 if (!ID) {
359 // Handle invalid code where the @interface might not
360 // have been specified.
361 // FIXME: We should be able to generate this USR even if the
362 // @interface isn't available.
363 IgnoreResults = true;
364 return;
365 }
366 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000367 break;
368 }
369 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000370 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000371 break;
372 }
373}
374
375void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000376 // The USR for a property declared in a class extension or category is based
377 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
378 if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
379 Visit(ID);
380 else
381 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000382 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000383}
384
Ted Kremeneke542f772010-04-20 23:15:40 +0000385void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
386 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
387 VisitObjCPropertyDecl(PD);
388 return;
389 }
390
391 IgnoreResults = true;
392}
393
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000394void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000395 // Add the location of the tag decl to handle resolution across
396 // translation units.
397 if (ShouldGenerateLocation(D) && GenLoc(D))
398 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000399
Ted Kremenek6f153952010-04-15 21:51:13 +0000400 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000401 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000402
Douglas Gregor74dbe642010-08-31 19:31:58 +0000403 bool AlreadyStarted = false;
404 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000405 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000406 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000407
408 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000409 case TTK_Struct: Out << "@ST"; break;
410 case TTK_Class: Out << "@CT"; break;
411 case TTK_Union: Out << "@UT"; break;
412 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000413 }
414 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000415 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
416 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
417 AlreadyStarted = true;
418
419 switch (D->getTagKind()) {
420 case TTK_Struct: Out << "@SP"; break;
421 case TTK_Class: Out << "@CP"; break;
422 case TTK_Union: Out << "@UP"; break;
423 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
424 }
425 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000426 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000427 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000428
Douglas Gregor74dbe642010-08-31 19:31:58 +0000429 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000430 switch (D->getTagKind()) {
431 case TTK_Struct: Out << "@S"; break;
432 case TTK_Class: Out << "@C"; break;
433 case TTK_Union: Out << "@U"; break;
434 case TTK_Enum: Out << "@E"; break;
435 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000436 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000437
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000438 Out << '@';
439 Out.flush();
440 assert(Buf.size() > 0);
441 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000442
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000443 if (EmitDeclName(D)) {
Richard Smith162e1c12011-04-15 14:24:37 +0000444 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000445 Buf[off] = 'A';
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000446 Out << '@' << *TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000447 }
448 else
449 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000450 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000451
452 // For a class template specialization, mangle the template arguments.
453 if (ClassTemplateSpecializationDecl *Spec
454 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
455 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
456 Out << '>';
457 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
458 Out << '#';
459 VisitTemplateArgument(Args.get(I));
460 }
461 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000462}
463
Ted Kremenekc50277f2010-01-12 23:33:42 +0000464void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000465 if (ShouldGenerateLocation(D) && GenLoc(D))
466 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000467 DeclContext *DC = D->getDeclContext();
468 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000469 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000470 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000471 Out << D->getName();
472}
473
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000474void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
475 GenLoc(D);
476 return;
477}
478
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000479bool USRGenerator::GenLoc(const Decl *D) {
480 if (generatedLoc)
481 return IgnoreResults;
482 generatedLoc = true;
Ted Kremenek8c367582011-04-29 21:35:23 +0000483
484 // Guard against null declarations in invalid code.
485 if (!D) {
486 IgnoreResults = true;
487 return true;
488 }
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000489
Ted Kremenek1d8052d2011-05-03 01:33:35 +0000490 // Use the location of canonical decl.
491 D = D->getCanonicalDecl();
492
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000493 const SourceManager &SM = Context->getSourceManager();
Ted Kremenek6f153952010-04-15 21:51:13 +0000494 SourceLocation L = D->getLocStart();
495 if (L.isInvalid()) {
496 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000497 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000498 }
Chandler Carruth40278532011-07-25 16:49:02 +0000499 L = SM.getExpansionLoc(L);
Ted Kremenek6f153952010-04-15 21:51:13 +0000500 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
501 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000502 if (FE) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000503 Out << llvm::sys::path::filename(FE->getName());
Ted Kremeneke74ef122010-04-16 21:31:52 +0000504 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000505 else {
506 // This case really isn't interesting.
507 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000508 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000509 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000510 // Use the offest into the FileID to represent the location. Using
511 // a line/column can cause us to look back at the original source file,
512 // which is expensive.
513 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000514 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000515}
516
Ted Kremenek8e672192010-05-07 01:04:32 +0000517void USRGenerator::VisitType(QualType T) {
518 // This method mangles in USR information for types. It can possibly
519 // just reuse the naming-mangling logic used by codegen, although the
520 // requirements for USRs might not be the same.
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000521 ASTContext &Ctx = *Context;
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000522
Ted Kremenek8e672192010-05-07 01:04:32 +0000523 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000524 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000525 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000526 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000527 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000528 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000529 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000530 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000531 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000532 qVal |= 0x4;
533 if(qVal)
534 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000535
536 // Mangle in ObjC GC qualifiers?
537
Douglas Gregorba73ada2011-01-19 20:50:07 +0000538 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
539 Out << 'P';
540 T = Expansion->getPattern();
541 }
542
Ted Kremenek8e672192010-05-07 01:04:32 +0000543 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
544 unsigned char c = '\0';
545 switch (BT->getKind()) {
546 case BuiltinType::Void:
547 c = 'v'; break;
548 case BuiltinType::Bool:
549 c = 'b'; break;
550 case BuiltinType::Char_U:
551 case BuiltinType::UChar:
552 c = 'c'; break;
553 case BuiltinType::Char16:
554 c = 'q'; break;
555 case BuiltinType::Char32:
556 c = 'w'; break;
557 case BuiltinType::UShort:
558 c = 's'; break;
559 case BuiltinType::UInt:
560 c = 'i'; break;
561 case BuiltinType::ULong:
562 c = 'l'; break;
563 case BuiltinType::ULongLong:
564 c = 'k'; break;
565 case BuiltinType::UInt128:
566 c = 'j'; break;
567 case BuiltinType::Char_S:
568 case BuiltinType::SChar:
569 c = 'C'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000570 case BuiltinType::WChar_S:
571 case BuiltinType::WChar_U:
Ted Kremenek8e672192010-05-07 01:04:32 +0000572 c = 'W'; break;
573 case BuiltinType::Short:
574 c = 'S'; break;
575 case BuiltinType::Int:
576 c = 'I'; break;
577 case BuiltinType::Long:
578 c = 'L'; break;
579 case BuiltinType::LongLong:
580 c = 'K'; break;
581 case BuiltinType::Int128:
582 c = 'J'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000583 case BuiltinType::Half:
584 c = 'h'; break;
Ted Kremenek8e672192010-05-07 01:04:32 +0000585 case BuiltinType::Float:
586 c = 'f'; break;
587 case BuiltinType::Double:
588 c = 'd'; break;
589 case BuiltinType::LongDouble:
590 c = 'D'; break;
591 case BuiltinType::NullPtr:
592 c = 'n'; break;
John McCall2dde35b2011-10-18 22:28:37 +0000593#define BUILTIN_TYPE(Id, SingletonId)
594#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
595#include "clang/AST/BuiltinTypes.def"
Ted Kremenek8e672192010-05-07 01:04:32 +0000596 case BuiltinType::Dependent:
Ted Kremenek8e672192010-05-07 01:04:32 +0000597 IgnoreResults = true;
598 return;
599 case BuiltinType::ObjCId:
600 c = 'o'; break;
601 case BuiltinType::ObjCClass:
602 c = 'O'; break;
603 case BuiltinType::ObjCSel:
604 c = 'e'; break;
605 }
606 Out << c;
607 return;
608 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000609
610 // If we have already seen this (non-built-in) type, use a substitution
611 // encoding.
612 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
613 = TypeSubstitutions.find(T.getTypePtr());
614 if (Substitution != TypeSubstitutions.end()) {
615 Out << 'S' << Substitution->second << '_';
616 return;
617 } else {
618 // Record this as a substitution.
619 unsigned Number = TypeSubstitutions.size();
620 TypeSubstitutions[T.getTypePtr()] = Number;
621 }
622
623 if (const PointerType *PT = T->getAs<PointerType>()) {
624 Out << '*';
625 T = PT->getPointeeType();
626 continue;
627 }
628 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
629 Out << '&';
630 T = RT->getPointeeType();
631 continue;
632 }
633 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
634 Out << 'F';
635 VisitType(FT->getResultType());
636 for (FunctionProtoType::arg_type_iterator
637 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
638 VisitType(*I);
639 }
640 if (FT->isVariadic())
641 Out << '.';
642 return;
643 }
644 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
645 Out << 'B';
646 T = BT->getPointeeType();
647 continue;
648 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000649 if (const ComplexType *CT = T->getAs<ComplexType>()) {
650 Out << '<';
651 T = CT->getElementType();
652 continue;
653 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000654 if (const TagType *TT = T->getAs<TagType>()) {
655 Out << '$';
656 VisitTagDecl(TT->getDecl());
657 return;
658 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000659 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
660 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
661 return;
662 }
663 if (const TemplateSpecializationType *Spec
664 = T->getAs<TemplateSpecializationType>()) {
665 Out << '>';
666 VisitTemplateName(Spec->getTemplateName());
667 Out << Spec->getNumArgs();
668 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
669 VisitTemplateArgument(Spec->getArg(I));
670 return;
671 }
672
Ted Kremenek8e672192010-05-07 01:04:32 +0000673 // Unhandled type.
674 Out << ' ';
675 break;
676 } while (true);
677}
678
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000679void USRGenerator::VisitTemplateParameterList(
680 const TemplateParameterList *Params) {
681 if (!Params)
682 return;
683 Out << '>' << Params->size();
684 for (TemplateParameterList::const_iterator P = Params->begin(),
685 PEnd = Params->end();
686 P != PEnd; ++P) {
687 Out << '#';
688 if (isa<TemplateTypeParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000689 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
690 Out<< 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000691 Out << 'T';
692 continue;
693 }
694
695 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000696 if (NTTP->isParameterPack())
697 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000698 Out << 'N';
699 VisitType(NTTP->getType());
700 continue;
701 }
702
703 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
Douglas Gregorba73ada2011-01-19 20:50:07 +0000704 if (TTP->isParameterPack())
705 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000706 Out << 't';
707 VisitTemplateParameterList(TTP->getTemplateParameters());
708 }
709}
710
711void USRGenerator::VisitTemplateName(TemplateName Name) {
712 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
713 if (TemplateTemplateParmDecl *TTP
714 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
715 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
716 return;
717 }
718
719 Visit(Template);
720 return;
721 }
722
723 // FIXME: Visit dependent template names.
724}
725
726void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
727 switch (Arg.getKind()) {
728 case TemplateArgument::Null:
729 break;
730
731 case TemplateArgument::Declaration:
Douglas Gregor97475832010-10-05 18:37:06 +0000732 if (Decl *D = Arg.getAsDecl())
733 Visit(D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000734 break;
735
Douglas Gregora7fc9012011-01-05 18:58:31 +0000736 case TemplateArgument::TemplateExpansion:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000737 Out << 'P'; // pack expansion of...
738 // Fall through
739 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000740 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000741 break;
742
743 case TemplateArgument::Expression:
744 // FIXME: Visit expressions.
745 break;
746
747 case TemplateArgument::Pack:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000748 Out << 'p' << Arg.pack_size();
749 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
750 P != PEnd; ++P)
751 VisitTemplateArgument(*P);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000752 break;
753
754 case TemplateArgument::Type:
755 VisitType(Arg.getAsType());
756 break;
757
758 case TemplateArgument::Integral:
759 Out << 'V';
760 VisitType(Arg.getIntegralType());
761 Out << *Arg.getAsIntegral();
762 break;
763 }
764}
765
Ted Kremenek896b70f2010-03-13 02:50:34 +0000766//===----------------------------------------------------------------------===//
767// General purpose USR generation methods.
768//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000769
Chris Lattner5f9e2722011-07-23 10:55:15 +0000770void USRGenerator::GenObjCClass(StringRef cls) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000771 Out << "objc(cs)" << cls;
772}
773
Chris Lattner5f9e2722011-07-23 10:55:15 +0000774void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000775 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000776}
777
Chris Lattner5f9e2722011-07-23 10:55:15 +0000778void USRGenerator::GenObjCIvar(StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000779 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000780}
781
Chris Lattner5f9e2722011-07-23 10:55:15 +0000782void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000783 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
784}
785
Chris Lattner5f9e2722011-07-23 10:55:15 +0000786void USRGenerator::GenObjCProperty(StringRef prop) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000787 Out << "(py)" << prop;
788}
789
Chris Lattner5f9e2722011-07-23 10:55:15 +0000790void USRGenerator::GenObjCProtocol(StringRef prot) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000791 Out << "objc(pl)" << prot;
792}
793
794//===----------------------------------------------------------------------===//
795// API hooks.
796//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000797
Chris Lattner5f9e2722011-07-23 10:55:15 +0000798static inline StringRef extractUSRSuffix(StringRef s) {
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000799 return s.startswith("c:") ? s.substr(2) : "";
800}
801
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000802bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000803 // Don't generate USRs for things with invalid locations.
804 if (!D || D->getLocStart().isInvalid())
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000805 return true;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000806
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000807 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000808 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000809 switch (ND->getLinkage()) {
810 case ExternalLinkage:
811 // Generate USRs for all entities with external linkage.
812 break;
813 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000814 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000815 // We allow enums, typedefs, and structs that have no linkage to
816 // have USRs that are anchored to the file they were defined in
817 // (e.g., the header). This is a little gross, but in principal
818 // enums/anonymous structs/etc. defined in a common header file
819 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000820 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000821 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000822 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000823 break;
824 // Fall-through.
825 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000826 if (isa<FunctionDecl>(ND))
827 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000828 }
829
Ted Kremenekf0199432010-11-16 08:15:38 +0000830 {
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000831 USRGenerator UG(&D->getASTContext(), &Buf);
Benjamin Kramer854625f2011-10-28 13:37:11 +0000832 UG->Visit(const_cast<Decl*>(D));
Ted Kremeneke542f772010-04-20 23:15:40 +0000833
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000834 if (UG->ignoreResults())
835 return true;
Ted Kremenekf0199432010-11-16 08:15:38 +0000836 }
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000837
838 return false;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000839}
840
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000841extern "C" {
842
843CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000844 const CXCursorKind &K = clang_getCursorKind(C);
845
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000846 if (clang_isDeclaration(K)) {
847 Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +0000848 if (!D)
849 return createCXString("");
850
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000851 CXTranslationUnit TU = cxcursor::getCursorTU(C);
852 if (!TU)
853 return createCXString("");
854
855 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
856 if (!buf)
857 return createCXString("");
858
859 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
860 if (Ignore) {
861 disposeCXStringBuf(buf);
862 return createCXString("");
863 }
864
865 // Return the C-string, but don't make a copy since it is already in
866 // the string buffer.
867 buf->Data.push_back('\0');
868 return createCXString(buf);
869 }
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000870
871 if (K == CXCursor_MacroDefinition) {
Ted Kremenekf0199432010-11-16 08:15:38 +0000872 CXTranslationUnit TU = cxcursor::getCursorTU(C);
873 if (!TU)
874 return createCXString("");
875
876 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
877 if (!buf)
878 return createCXString("");
879
880 {
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000881 USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(),
882 &buf->Data);
Ted Kremenekf0199432010-11-16 08:15:38 +0000883 UG << "macro@"
884 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
885 }
886 buf->Data.push_back('\0');
887 return createCXString(buf);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000888 }
889
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000890 return createCXString("");
891}
892
Ted Kremenek896b70f2010-03-13 02:50:34 +0000893CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000894 USRGenerator UG;
895 UG << extractUSRSuffix(clang_getCString(classUSR));
896 UG->GenObjCIvar(name);
897 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000898}
899
900CXString clang_constructUSR_ObjCMethod(const char *name,
901 unsigned isInstanceMethod,
902 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000903 USRGenerator UG;
904 UG << extractUSRSuffix(clang_getCString(classUSR));
905 UG->GenObjCMethod(name, isInstanceMethod);
906 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000907}
908
909CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000910 USRGenerator UG;
911 UG->GenObjCClass(name);
912 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000913}
914
915CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000916 USRGenerator UG;
917 UG->GenObjCProtocol(name);
918 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000919}
920
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000921CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000922 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000923 USRGenerator UG;
924 UG->GenObjCCategory(class_name, category_name);
925 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000926}
927
928CXString clang_constructUSR_ObjCProperty(const char *property,
929 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000930 USRGenerator UG;
931 UG << extractUSRSuffix(clang_getCString(classUSR));
932 UG->GenObjCProperty(property);
933 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000934}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000935
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000936} // end extern "C"