blob: 446baa08fa99bf08eeb6c98b8fe4e5a82beda786 [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 Kremenek896b70f2010-03-13 02:50:34 +000078 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke542f772010-04-20 23:15:40 +000079 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000080 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000081 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000082 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000083 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000084 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000085 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000086 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
87 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000088 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
89 IgnoreResults = true;
Ted Kremenek8e672192010-05-07 01:04:32 +000090 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000091 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
92 IgnoreResults = true;
93 }
Douglas Gregor7e242562010-09-01 19:52:22 +000094 void VisitUsingDecl(UsingDecl *D) {
95 IgnoreResults = true;
96 }
97 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
98 IgnoreResults = true;
99 }
100 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
101 IgnoreResults = true;
102 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000103
Ted Kremenek6f153952010-04-15 21:51:13 +0000104 /// Generate the string component containing the location of the
105 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000106 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +0000107
Ted Kremenek896b70f2010-03-13 02:50:34 +0000108 /// String generation methods used both by the visitation methods
109 /// and from other clients that want to directly generate USRs. These
110 /// methods do not construct complete USRs (which incorporate the parents
111 /// of an AST element), but only the fragments concerning the AST element
112 /// itself.
113
Ted Kremenek896b70f2010-03-13 02:50:34 +0000114 /// Generate a USR for an Objective-C class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 void GenObjCClass(StringRef cls);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000116 /// Generate a USR for an Objective-C class category.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 void GenObjCCategory(StringRef cls, StringRef cat);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000118 /// Generate a USR fragment for an Objective-C instance variable. The
119 /// complete USR can be created by concatenating the USR for the
120 /// encompassing class with this USR fragment.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 void GenObjCIvar(StringRef ivar);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000122 /// Generate a USR fragment for an Objective-C method.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 void GenObjCMethod(StringRef sel, bool isInstanceMethod);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000124 /// Generate a USR fragment for an Objective-C property.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 void GenObjCProperty(StringRef prop);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000126 /// Generate a USR for an Objective-C protocol.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 void GenObjCProtocol(StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000128
129 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000130 void VisitTemplateParameterList(const TemplateParameterList *Params);
131 void VisitTemplateName(TemplateName Name);
132 void VisitTemplateArgument(const TemplateArgument &Arg);
133
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000134 /// Emit a Decl's name using NamedDecl::printName() and return true if
135 /// the decl had no name.
136 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000137};
138
Ted Kremenekc50277f2010-01-12 23:33:42 +0000139} // end anonymous namespace
140
Ted Kremenek896b70f2010-03-13 02:50:34 +0000141//===----------------------------------------------------------------------===//
142// Generating USRs from ASTS.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000145bool USRGenerator::EmitDeclName(const NamedDecl *D) {
146 Out.flush();
147 const unsigned startSize = Buf.size();
148 D->printName(Out);
149 Out.flush();
150 const unsigned endSize = Buf.size();
151 return startSize == endSize;
152}
153
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000154static bool InAnonymousNamespace(const Decl *D) {
155 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
156 return ND->isAnonymousNamespace();
157 return false;
158}
159
160static inline bool ShouldGenerateLocation(const NamedDecl *D) {
161 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
162}
163
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000164void USRGenerator::VisitDeclContext(DeclContext *DC) {
165 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
166 Visit(D);
167}
168
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000169void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000170 // The USR for an ivar declared in a class extension is based on the
171 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
172 if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
173 Visit(ID);
174 else
175 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000176 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
177 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000178 // Bit fields can be anonymous.
179 IgnoreResults = true;
180 return;
181 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000182}
183
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000184void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000185 if (ShouldGenerateLocation(D) && GenLoc(D))
186 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000187
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000188 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000189 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
190 Out << "@FT@";
191 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
192 } else
193 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000194 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000195
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000196 ASTContext &Ctx = *Context;
Ted Kremenek8e672192010-05-07 01:04:32 +0000197 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
198 return;
199
200 // Mangle in type information for the arguments.
201 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
202 I != E; ++I) {
203 Out << '#';
204 if (ParmVarDecl *PD = *I)
205 VisitType(PD->getType());
206 }
207 if (D->isVariadic())
208 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000209 Out << '#';
210 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
211 if (MD->isStatic())
212 Out << 'S';
213 if (unsigned quals = MD->getTypeQualifiers())
214 Out << (char)('0' + quals);
215 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000216}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000217
218void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000219 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000220 Out << "@";
221
222 if (EmitDeclName(D)) {
223 // The string can be empty if the declaration has no name; e.g., it is
224 // the ParmDecl with no name for declaration of a function pointer type,
225 // e.g.: void (*f)(void *);
226 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000227 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000228 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000229}
230
Ted Kremeneke542f772010-04-20 23:15:40 +0000231void USRGenerator::VisitVarDecl(VarDecl *D) {
232 // VarDecls can be declared 'extern' within a function or method body,
233 // but their enclosing DeclContext is the function, not the TU. We need
234 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000235 if (ShouldGenerateLocation(D) && GenLoc(D))
236 return;
237
238 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000239
Ted Kremenekcf999102010-04-29 17:43:29 +0000240 // Variables always have simple names.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 StringRef s = D->getName();
Ted Kremenekcf999102010-04-29 17:43:29 +0000242
Ted Kremeneke542f772010-04-20 23:15:40 +0000243 // The string can be empty if the declaration has no name; e.g., it is
244 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000245 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000246 // In this case, don't generate a USR.
247 if (s.empty())
248 IgnoreResults = true;
249 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000250 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000251}
252
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000253void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
254 GenLoc(D);
255 return;
256}
257
258void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
259 GenLoc(D);
260 return;
261}
262
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000263void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000264 if (D->isAnonymousNamespace()) {
265 Out << "@aN";
266 return;
267 }
268
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000269 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000270 if (!IgnoreResults)
271 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000272}
273
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000274void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
275 VisitFunctionDecl(D->getTemplatedDecl());
276}
277
Douglas Gregor39d6f072010-08-31 19:02:00 +0000278void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
279 VisitTagDecl(D->getTemplatedDecl());
280}
281
Douglas Gregor69319002010-08-31 23:48:11 +0000282void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
283 VisitDeclContext(D->getDeclContext());
284 if (!IgnoreResults)
285 Out << "@NA@" << D->getName();
286}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000287
Ted Kremenekc50277f2010-01-12 23:33:42 +0000288void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000289 DeclContext *container = D->getDeclContext();
290 if (ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
291 Visit(pd);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000292 }
Ted Kremenek52d6bbe2011-02-05 01:10:26 +0000293 else {
294 // The USR for a method declared in a class extension or category is based on
295 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
296 ObjCInterfaceDecl *ID = D->getClassInterface();
297 if (!ID) {
298 IgnoreResults = true;
299 return;
300 }
301 Visit(ID);
302 }
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000303 // Ideally we would use 'GenObjCMethod', but this is such a hot path
304 // for Objective-C code that we don't want to use
305 // DeclarationName::getAsString().
306 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
307 DeclarationName N(D->getSelector());
308 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000309}
310
311void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
312 switch (D->getKind()) {
313 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000314 llvm_unreachable("Invalid ObjC container.");
Ted Kremenekc50277f2010-01-12 23:33:42 +0000315 case Decl::ObjCInterface:
316 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000317 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000318 break;
319 case Decl::ObjCCategory: {
320 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000321 ObjCInterfaceDecl *ID = CD->getClassInterface();
322 if (!ID) {
323 // Handle invalid code where the @interface might not
324 // have been specified.
325 // FIXME: We should be able to generate this USR even if the
326 // @interface isn't available.
327 IgnoreResults = true;
328 return;
329 }
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000330 // Specially handle class extensions, which are anonymous categories.
331 // We want to mangle in the location to uniquely distinguish them.
Ted Kremenek28a7f252010-08-24 23:13:41 +0000332 if (CD->IsClassExtension()) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000333 Out << "objc(ext)" << ID->getName() << '@';
334 GenLoc(CD);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000335 }
336 else
337 GenObjCCategory(ID->getName(), CD->getName());
338
Ted Kremenekc50277f2010-01-12 23:33:42 +0000339 break;
340 }
341 case Decl::ObjCCategoryImpl: {
342 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000343 ObjCInterfaceDecl *ID = CD->getClassInterface();
344 if (!ID) {
345 // Handle invalid code where the @interface might not
346 // have been specified.
347 // FIXME: We should be able to generate this USR even if the
348 // @interface isn't available.
349 IgnoreResults = true;
350 return;
351 }
352 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000353 break;
354 }
355 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000356 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000357 break;
358 }
359}
360
361void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Argyrios Kyrtzidis87ec9c22011-11-01 17:14:12 +0000362 // The USR for a property declared in a class extension or category is based
363 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
364 if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
365 Visit(ID);
366 else
367 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000368 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000369}
370
Ted Kremeneke542f772010-04-20 23:15:40 +0000371void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
372 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
373 VisitObjCPropertyDecl(PD);
374 return;
375 }
376
377 IgnoreResults = true;
378}
379
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000380void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000381 // Add the location of the tag decl to handle resolution across
382 // translation units.
383 if (ShouldGenerateLocation(D) && GenLoc(D))
384 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000385
Ted Kremenek6f153952010-04-15 21:51:13 +0000386 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000387 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000388
Douglas Gregor74dbe642010-08-31 19:31:58 +0000389 bool AlreadyStarted = false;
390 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000391 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000392 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000393
394 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000395 case TTK_Struct: Out << "@ST"; break;
396 case TTK_Class: Out << "@CT"; break;
397 case TTK_Union: Out << "@UT"; break;
David Blaikie7530c032012-01-17 06:56:22 +0000398 case TTK_Enum: llvm_unreachable("enum template");
Douglas Gregor39d6f072010-08-31 19:02:00 +0000399 }
400 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000401 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
402 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
403 AlreadyStarted = true;
404
405 switch (D->getTagKind()) {
406 case TTK_Struct: Out << "@SP"; break;
407 case TTK_Class: Out << "@CP"; break;
408 case TTK_Union: Out << "@UP"; break;
David Blaikie7530c032012-01-17 06:56:22 +0000409 case TTK_Enum: llvm_unreachable("enum partial specialization");
Douglas Gregor74dbe642010-08-31 19:31:58 +0000410 }
411 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000412 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000413 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000414
Douglas Gregor74dbe642010-08-31 19:31:58 +0000415 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000416 switch (D->getTagKind()) {
417 case TTK_Struct: Out << "@S"; break;
418 case TTK_Class: Out << "@C"; break;
419 case TTK_Union: Out << "@U"; break;
420 case TTK_Enum: Out << "@E"; break;
421 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000422 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000423
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000424 Out << '@';
425 Out.flush();
426 assert(Buf.size() > 0);
427 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000428
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000429 if (EmitDeclName(D)) {
Richard Smith162e1c12011-04-15 14:24:37 +0000430 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000431 Buf[off] = 'A';
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000432 Out << '@' << *TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000433 }
434 else
435 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000436 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000437
438 // For a class template specialization, mangle the template arguments.
439 if (ClassTemplateSpecializationDecl *Spec
440 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
441 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
442 Out << '>';
443 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
444 Out << '#';
445 VisitTemplateArgument(Args.get(I));
446 }
447 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000448}
449
Ted Kremenekc50277f2010-01-12 23:33:42 +0000450void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000451 if (ShouldGenerateLocation(D) && GenLoc(D))
452 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000453 DeclContext *DC = D->getDeclContext();
454 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000455 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000456 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000457 Out << D->getName();
458}
459
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000460void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
461 GenLoc(D);
462 return;
463}
464
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000465bool USRGenerator::GenLoc(const Decl *D) {
466 if (generatedLoc)
467 return IgnoreResults;
468 generatedLoc = true;
Ted Kremenek8c367582011-04-29 21:35:23 +0000469
470 // Guard against null declarations in invalid code.
471 if (!D) {
472 IgnoreResults = true;
473 return true;
474 }
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000475
Ted Kremenek1d8052d2011-05-03 01:33:35 +0000476 // Use the location of canonical decl.
477 D = D->getCanonicalDecl();
478
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000479 const SourceManager &SM = Context->getSourceManager();
Ted Kremenek6f153952010-04-15 21:51:13 +0000480 SourceLocation L = D->getLocStart();
481 if (L.isInvalid()) {
482 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000483 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000484 }
Chandler Carruth40278532011-07-25 16:49:02 +0000485 L = SM.getExpansionLoc(L);
Ted Kremenek6f153952010-04-15 21:51:13 +0000486 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
487 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000488 if (FE) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000489 Out << llvm::sys::path::filename(FE->getName());
Ted Kremeneke74ef122010-04-16 21:31:52 +0000490 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000491 else {
492 // This case really isn't interesting.
493 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000494 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000495 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000496 // Use the offest into the FileID to represent the location. Using
497 // a line/column can cause us to look back at the original source file,
498 // which is expensive.
499 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000500 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000501}
502
Ted Kremenek8e672192010-05-07 01:04:32 +0000503void USRGenerator::VisitType(QualType T) {
504 // This method mangles in USR information for types. It can possibly
505 // just reuse the naming-mangling logic used by codegen, although the
506 // requirements for USRs might not be the same.
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000507 ASTContext &Ctx = *Context;
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000508
Ted Kremenek8e672192010-05-07 01:04:32 +0000509 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000510 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000511 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000512 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000513 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000514 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000515 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000516 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000517 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000518 qVal |= 0x4;
519 if(qVal)
520 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000521
522 // Mangle in ObjC GC qualifiers?
523
Douglas Gregorba73ada2011-01-19 20:50:07 +0000524 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
525 Out << 'P';
526 T = Expansion->getPattern();
527 }
528
Ted Kremenek8e672192010-05-07 01:04:32 +0000529 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
530 unsigned char c = '\0';
531 switch (BT->getKind()) {
532 case BuiltinType::Void:
533 c = 'v'; break;
534 case BuiltinType::Bool:
535 c = 'b'; break;
536 case BuiltinType::Char_U:
537 case BuiltinType::UChar:
538 c = 'c'; break;
539 case BuiltinType::Char16:
540 c = 'q'; break;
541 case BuiltinType::Char32:
542 c = 'w'; break;
543 case BuiltinType::UShort:
544 c = 's'; break;
545 case BuiltinType::UInt:
546 c = 'i'; break;
547 case BuiltinType::ULong:
548 c = 'l'; break;
549 case BuiltinType::ULongLong:
550 c = 'k'; break;
551 case BuiltinType::UInt128:
552 c = 'j'; break;
553 case BuiltinType::Char_S:
554 case BuiltinType::SChar:
555 c = 'C'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000556 case BuiltinType::WChar_S:
557 case BuiltinType::WChar_U:
Ted Kremenek8e672192010-05-07 01:04:32 +0000558 c = 'W'; break;
559 case BuiltinType::Short:
560 c = 'S'; break;
561 case BuiltinType::Int:
562 c = 'I'; break;
563 case BuiltinType::Long:
564 c = 'L'; break;
565 case BuiltinType::LongLong:
566 c = 'K'; break;
567 case BuiltinType::Int128:
568 c = 'J'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000569 case BuiltinType::Half:
570 c = 'h'; break;
Ted Kremenek8e672192010-05-07 01:04:32 +0000571 case BuiltinType::Float:
572 c = 'f'; break;
573 case BuiltinType::Double:
574 c = 'd'; break;
575 case BuiltinType::LongDouble:
576 c = 'D'; break;
577 case BuiltinType::NullPtr:
578 c = 'n'; break;
John McCall2dde35b2011-10-18 22:28:37 +0000579#define BUILTIN_TYPE(Id, SingletonId)
580#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
581#include "clang/AST/BuiltinTypes.def"
Ted Kremenek8e672192010-05-07 01:04:32 +0000582 case BuiltinType::Dependent:
Ted Kremenek8e672192010-05-07 01:04:32 +0000583 IgnoreResults = true;
584 return;
585 case BuiltinType::ObjCId:
586 c = 'o'; break;
587 case BuiltinType::ObjCClass:
588 c = 'O'; break;
589 case BuiltinType::ObjCSel:
590 c = 'e'; break;
591 }
592 Out << c;
593 return;
594 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000595
596 // If we have already seen this (non-built-in) type, use a substitution
597 // encoding.
598 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
599 = TypeSubstitutions.find(T.getTypePtr());
600 if (Substitution != TypeSubstitutions.end()) {
601 Out << 'S' << Substitution->second << '_';
602 return;
603 } else {
604 // Record this as a substitution.
605 unsigned Number = TypeSubstitutions.size();
606 TypeSubstitutions[T.getTypePtr()] = Number;
607 }
608
609 if (const PointerType *PT = T->getAs<PointerType>()) {
610 Out << '*';
611 T = PT->getPointeeType();
612 continue;
613 }
614 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
615 Out << '&';
616 T = RT->getPointeeType();
617 continue;
618 }
619 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
620 Out << 'F';
621 VisitType(FT->getResultType());
622 for (FunctionProtoType::arg_type_iterator
623 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
624 VisitType(*I);
625 }
626 if (FT->isVariadic())
627 Out << '.';
628 return;
629 }
630 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
631 Out << 'B';
632 T = BT->getPointeeType();
633 continue;
634 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000635 if (const ComplexType *CT = T->getAs<ComplexType>()) {
636 Out << '<';
637 T = CT->getElementType();
638 continue;
639 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000640 if (const TagType *TT = T->getAs<TagType>()) {
641 Out << '$';
642 VisitTagDecl(TT->getDecl());
643 return;
644 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000645 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
646 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
647 return;
648 }
649 if (const TemplateSpecializationType *Spec
650 = T->getAs<TemplateSpecializationType>()) {
651 Out << '>';
652 VisitTemplateName(Spec->getTemplateName());
653 Out << Spec->getNumArgs();
654 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
655 VisitTemplateArgument(Spec->getArg(I));
656 return;
657 }
658
Ted Kremenek8e672192010-05-07 01:04:32 +0000659 // Unhandled type.
660 Out << ' ';
661 break;
662 } while (true);
663}
664
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000665void USRGenerator::VisitTemplateParameterList(
666 const TemplateParameterList *Params) {
667 if (!Params)
668 return;
669 Out << '>' << Params->size();
670 for (TemplateParameterList::const_iterator P = Params->begin(),
671 PEnd = Params->end();
672 P != PEnd; ++P) {
673 Out << '#';
674 if (isa<TemplateTypeParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000675 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
676 Out<< 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000677 Out << 'T';
678 continue;
679 }
680
681 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregorba73ada2011-01-19 20:50:07 +0000682 if (NTTP->isParameterPack())
683 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000684 Out << 'N';
685 VisitType(NTTP->getType());
686 continue;
687 }
688
689 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
Douglas Gregorba73ada2011-01-19 20:50:07 +0000690 if (TTP->isParameterPack())
691 Out << 'p';
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000692 Out << 't';
693 VisitTemplateParameterList(TTP->getTemplateParameters());
694 }
695}
696
697void USRGenerator::VisitTemplateName(TemplateName Name) {
698 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
699 if (TemplateTemplateParmDecl *TTP
700 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
701 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
702 return;
703 }
704
705 Visit(Template);
706 return;
707 }
708
709 // FIXME: Visit dependent template names.
710}
711
712void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
713 switch (Arg.getKind()) {
714 case TemplateArgument::Null:
715 break;
716
717 case TemplateArgument::Declaration:
Douglas Gregor97475832010-10-05 18:37:06 +0000718 if (Decl *D = Arg.getAsDecl())
719 Visit(D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000720 break;
721
Douglas Gregora7fc9012011-01-05 18:58:31 +0000722 case TemplateArgument::TemplateExpansion:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000723 Out << 'P'; // pack expansion of...
724 // Fall through
725 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000726 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000727 break;
728
729 case TemplateArgument::Expression:
730 // FIXME: Visit expressions.
731 break;
732
733 case TemplateArgument::Pack:
Douglas Gregorba73ada2011-01-19 20:50:07 +0000734 Out << 'p' << Arg.pack_size();
735 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
736 P != PEnd; ++P)
737 VisitTemplateArgument(*P);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000738 break;
739
740 case TemplateArgument::Type:
741 VisitType(Arg.getAsType());
742 break;
743
744 case TemplateArgument::Integral:
745 Out << 'V';
746 VisitType(Arg.getIntegralType());
747 Out << *Arg.getAsIntegral();
748 break;
749 }
750}
751
Ted Kremenek896b70f2010-03-13 02:50:34 +0000752//===----------------------------------------------------------------------===//
753// General purpose USR generation methods.
754//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000755
Chris Lattner5f9e2722011-07-23 10:55:15 +0000756void USRGenerator::GenObjCClass(StringRef cls) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000757 Out << "objc(cs)" << cls;
758}
759
Chris Lattner5f9e2722011-07-23 10:55:15 +0000760void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000761 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000762}
763
Chris Lattner5f9e2722011-07-23 10:55:15 +0000764void USRGenerator::GenObjCIvar(StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000765 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000766}
767
Chris Lattner5f9e2722011-07-23 10:55:15 +0000768void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000769 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
770}
771
Chris Lattner5f9e2722011-07-23 10:55:15 +0000772void USRGenerator::GenObjCProperty(StringRef prop) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000773 Out << "(py)" << prop;
774}
775
Chris Lattner5f9e2722011-07-23 10:55:15 +0000776void USRGenerator::GenObjCProtocol(StringRef prot) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000777 Out << "objc(pl)" << prot;
778}
779
780//===----------------------------------------------------------------------===//
781// API hooks.
782//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000783
Chris Lattner5f9e2722011-07-23 10:55:15 +0000784static inline StringRef extractUSRSuffix(StringRef s) {
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000785 return s.startswith("c:") ? s.substr(2) : "";
786}
787
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000788bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000789 // Don't generate USRs for things with invalid locations.
790 if (!D || D->getLocStart().isInvalid())
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000791 return true;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000792
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000793 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000794 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000795 switch (ND->getLinkage()) {
796 case ExternalLinkage:
797 // Generate USRs for all entities with external linkage.
798 break;
799 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000800 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000801 // We allow enums, typedefs, and structs that have no linkage to
802 // have USRs that are anchored to the file they were defined in
803 // (e.g., the header). This is a little gross, but in principal
804 // enums/anonymous structs/etc. defined in a common header file
805 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000806 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000807 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000808 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000809 break;
810 // Fall-through.
811 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000812 if (isa<FunctionDecl>(ND))
813 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000814 }
815
Ted Kremenekf0199432010-11-16 08:15:38 +0000816 {
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000817 USRGenerator UG(&D->getASTContext(), &Buf);
Benjamin Kramer854625f2011-10-28 13:37:11 +0000818 UG->Visit(const_cast<Decl*>(D));
Ted Kremeneke542f772010-04-20 23:15:40 +0000819
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000820 if (UG->ignoreResults())
821 return true;
Ted Kremenekf0199432010-11-16 08:15:38 +0000822 }
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000823
824 return false;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000825}
826
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000827extern "C" {
828
829CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000830 const CXCursorKind &K = clang_getCursorKind(C);
831
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000832 if (clang_isDeclaration(K)) {
833 Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +0000834 if (!D)
835 return createCXString("");
836
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000837 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"