blob: 6dfe9df4dd775744383de48594b55fca31d5b330 [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"
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000017#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000018#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000019#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000020#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000021#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000022
Benjamin Kramerb846deb2010-04-12 19:45:50 +000023using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000024using namespace clang::cxstring;
25
Ted Kremenekc50277f2010-01-12 23:33:42 +000026//===----------------------------------------------------------------------===//
27// USR generation.
28//===----------------------------------------------------------------------===//
29
30namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000031class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000032 llvm::SmallString<1024> Buf;
33 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000034 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000035 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000036 bool generatedLoc;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000037public:
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000038 USRGenerator(const CXCursor *C = 0)
39 : Out(Buf),
40 IgnoreResults(false),
41 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
42 generatedLoc(false)
43 {
44 // Add the USR space prefix.
45 Out << "c:";
46 }
47
48 llvm::StringRef str() {
49 return Out.str();
50 }
51
52 USRGenerator* operator->() { return this; }
53
54 template <typename T>
55 llvm::raw_svector_ostream &operator<<(const T &x) {
56 Out << x;
57 return Out;
58 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000059
Ted Kremenek3adca6d2010-01-18 22:02:49 +000060 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000061
62 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000063 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000064 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000065 void VisitFunctionDecl(FunctionDecl *D);
66 void VisitNamedDecl(NamedDecl *D);
67 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000068 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000069 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000070 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000071 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000072 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000073 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000074 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000075 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000076 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000077 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000078 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000079 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
80 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000081 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
82 IgnoreResults = true;
83 return;
84 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000085
Ted Kremenek6f153952010-04-15 21:51:13 +000086 /// Generate the string component containing the location of the
87 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000088 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000089
Ted Kremenek896b70f2010-03-13 02:50:34 +000090 /// String generation methods used both by the visitation methods
91 /// and from other clients that want to directly generate USRs. These
92 /// methods do not construct complete USRs (which incorporate the parents
93 /// of an AST element), but only the fragments concerning the AST element
94 /// itself.
95
Ted Kremenek896b70f2010-03-13 02:50:34 +000096 /// Generate a USR for an Objective-C class.
97 void GenObjCClass(llvm::StringRef cls);
98 /// Generate a USR for an Objective-C class category.
99 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
100 /// Generate a USR fragment for an Objective-C instance variable. The
101 /// complete USR can be created by concatenating the USR for the
102 /// encompassing class with this USR fragment.
103 void GenObjCIvar(llvm::StringRef ivar);
104 /// Generate a USR fragment for an Objective-C method.
105 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
106 /// Generate a USR fragment for an Objective-C property.
107 void GenObjCProperty(llvm::StringRef prop);
108 /// Generate a USR for an Objective-C protocol.
109 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000110
111 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000112 void VisitTemplateParameterList(const TemplateParameterList *Params);
113 void VisitTemplateName(TemplateName Name);
114 void VisitTemplateArgument(const TemplateArgument &Arg);
115
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000116 /// Emit a Decl's name using NamedDecl::printName() and return true if
117 /// the decl had no name.
118 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000119};
120
Ted Kremenekc50277f2010-01-12 23:33:42 +0000121} // end anonymous namespace
122
Ted Kremenek896b70f2010-03-13 02:50:34 +0000123//===----------------------------------------------------------------------===//
124// Generating USRs from ASTS.
125//===----------------------------------------------------------------------===//
126
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000127bool USRGenerator::EmitDeclName(const NamedDecl *D) {
128 Out.flush();
129 const unsigned startSize = Buf.size();
130 D->printName(Out);
131 Out.flush();
132 const unsigned endSize = Buf.size();
133 return startSize == endSize;
134}
135
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000136static bool InAnonymousNamespace(const Decl *D) {
137 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
138 return ND->isAnonymousNamespace();
139 return false;
140}
141
142static inline bool ShouldGenerateLocation(const NamedDecl *D) {
143 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
144}
145
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000146void USRGenerator::VisitDeclContext(DeclContext *DC) {
147 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
148 Visit(D);
149}
150
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000151void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000152 VisitDeclContext(D->getDeclContext());
153 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
154 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000155 // Bit fields can be anonymous.
156 IgnoreResults = true;
157 return;
158 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000159}
160
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000161void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000162 if (ShouldGenerateLocation(D) && GenLoc(D))
163 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000164
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000165 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000166 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
167 Out << "@FT@";
168 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
169 } else
170 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000171 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000172
173 ASTContext &Ctx = AU->getASTContext();
174 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
175 return;
176
177 // Mangle in type information for the arguments.
178 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
179 I != E; ++I) {
180 Out << '#';
181 if (ParmVarDecl *PD = *I)
182 VisitType(PD->getType());
183 }
184 if (D->isVariadic())
185 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000186 Out << '#';
187 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
188 if (MD->isStatic())
189 Out << 'S';
190 if (unsigned quals = MD->getTypeQualifiers())
191 Out << (char)('0' + quals);
192 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000193}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000194
195void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000196 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000197 Out << "@";
198
199 if (EmitDeclName(D)) {
200 // The string can be empty if the declaration has no name; e.g., it is
201 // the ParmDecl with no name for declaration of a function pointer type,
202 // e.g.: void (*f)(void *);
203 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000204 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000205 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000206}
207
Ted Kremeneke542f772010-04-20 23:15:40 +0000208void USRGenerator::VisitVarDecl(VarDecl *D) {
209 // VarDecls can be declared 'extern' within a function or method body,
210 // but their enclosing DeclContext is the function, not the TU. We need
211 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000212 if (ShouldGenerateLocation(D) && GenLoc(D))
213 return;
214
215 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000216
Ted Kremenekcf999102010-04-29 17:43:29 +0000217 // Variables always have simple names.
218 llvm::StringRef s = D->getName();
219
Ted Kremeneke542f772010-04-20 23:15:40 +0000220 // 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, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000222 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000223 // In this case, don't generate a USR.
224 if (s.empty())
225 IgnoreResults = true;
226 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000227 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000228}
229
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000230void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
231 GenLoc(D);
232 return;
233}
234
235void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
236 GenLoc(D);
237 return;
238}
239
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000240void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000241 if (D->isAnonymousNamespace()) {
242 Out << "@aN";
243 return;
244 }
245
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000246 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000247 if (!IgnoreResults)
248 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000249}
250
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000251void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
252 VisitFunctionDecl(D->getTemplatedDecl());
253}
254
Ted Kremenekc50277f2010-01-12 23:33:42 +0000255void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000256 Decl *container = cast<Decl>(D->getDeclContext());
257
258 // The USR for a method declared in a class extension is based on
259 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
260 do {
261 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
262 if (CD->IsClassExtension()) {
263 Visit(CD->getClassInterface());
264 break;
265 }
266 Visit(cast<Decl>(D->getDeclContext()));
267 }
268 while (false);
269
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000270 // Ideally we would use 'GenObjCMethod', but this is such a hot path
271 // for Objective-C code that we don't want to use
272 // DeclarationName::getAsString().
273 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
274 DeclarationName N(D->getSelector());
275 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000276}
277
Ted Kremeneke74ef122010-04-16 21:31:52 +0000278void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
279 // FIXME: @class declarations can refer to multiple classes. We need
280 // to be able to traverse these.
281 IgnoreResults = true;
282}
283
284void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
285 // FIXME: @protocol declarations can refer to multiple protocols. We need
286 // to be able to traverse these.
287 IgnoreResults = true;
288}
289
Ted Kremenekc50277f2010-01-12 23:33:42 +0000290void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
291 switch (D->getKind()) {
292 default:
293 assert(false && "Invalid ObjC container.");
294 case Decl::ObjCInterface:
295 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000296 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000297 break;
298 case Decl::ObjCCategory: {
299 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000300 ObjCInterfaceDecl *ID = CD->getClassInterface();
301 if (!ID) {
302 // Handle invalid code where the @interface might not
303 // have been specified.
304 // FIXME: We should be able to generate this USR even if the
305 // @interface isn't available.
306 IgnoreResults = true;
307 return;
308 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000309 // Specially handle class extensions, which are anonymous categories.
310 // We want to mangle in the location to uniquely distinguish them.
311 if (CD->IsClassExtension()) {
312 Out << "objc(ext)" << ID->getName() << '@';
313 GenLoc(CD);
314 }
315 else
316 GenObjCCategory(ID->getName(), CD->getName());
317
Ted Kremenekc50277f2010-01-12 23:33:42 +0000318 break;
319 }
320 case Decl::ObjCCategoryImpl: {
321 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000322 ObjCInterfaceDecl *ID = CD->getClassInterface();
323 if (!ID) {
324 // Handle invalid code where the @interface might not
325 // have been specified.
326 // FIXME: We should be able to generate this USR even if the
327 // @interface isn't available.
328 IgnoreResults = true;
329 return;
330 }
331 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000332 break;
333 }
334 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000335 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000336 break;
337 }
338}
339
340void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
341 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000342 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000343}
344
Ted Kremeneke542f772010-04-20 23:15:40 +0000345void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
346 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
347 VisitObjCPropertyDecl(PD);
348 return;
349 }
350
351 IgnoreResults = true;
352}
353
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000354void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000355 // Add the location of the tag decl to handle resolution across
356 // translation units.
357 if (ShouldGenerateLocation(D) && GenLoc(D))
358 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000359
Ted Kremenek6f153952010-04-15 21:51:13 +0000360 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000361 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000362
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000363 switch (D->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000364 case TTK_Struct: Out << "@S"; break;
365 case TTK_Class: Out << "@C"; break;
366 case TTK_Union: Out << "@U"; break;
367 case TTK_Enum: Out << "@E"; break;
Ted Kremeneke74ef122010-04-16 21:31:52 +0000368 }
369
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000370 Out << '@';
371 Out.flush();
372 assert(Buf.size() > 0);
373 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000374
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000375 if (EmitDeclName(D)) {
376 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
377 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000378 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000379 }
380 else
381 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000382 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000383}
384
Ted Kremenekc50277f2010-01-12 23:33:42 +0000385void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000386 if (ShouldGenerateLocation(D) && GenLoc(D))
387 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000388 DeclContext *DC = D->getDeclContext();
389 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000390 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000391 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000392 Out << D->getName();
393}
394
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000395void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
396 GenLoc(D);
397 return;
398}
399
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000400bool USRGenerator::GenLoc(const Decl *D) {
401 if (generatedLoc)
402 return IgnoreResults;
403 generatedLoc = true;
404
Ted Kremenek6f153952010-04-15 21:51:13 +0000405 const SourceManager &SM = AU->getSourceManager();
406 SourceLocation L = D->getLocStart();
407 if (L.isInvalid()) {
408 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000409 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000410 }
411 L = SM.getInstantiationLoc(L);
412 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
413 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000414 if (FE) {
415 llvm::sys::Path P(FE->getName());
416 Out << P.getLast();
417 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000418 else {
419 // This case really isn't interesting.
420 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000421 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000422 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000423 // Use the offest into the FileID to represent the location. Using
424 // a line/column can cause us to look back at the original source file,
425 // which is expensive.
426 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000427 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000428}
429
Ted Kremenek8e672192010-05-07 01:04:32 +0000430void USRGenerator::VisitType(QualType T) {
431 // This method mangles in USR information for types. It can possibly
432 // just reuse the naming-mangling logic used by codegen, although the
433 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000434 ASTContext &Ctx = AU->getASTContext();
435
Ted Kremenek8e672192010-05-07 01:04:32 +0000436 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000437 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000438 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000439 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000440 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000441 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000442 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000443 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000444 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000445 qVal |= 0x4;
446 if(qVal)
447 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000448
449 // Mangle in ObjC GC qualifiers?
450
451 if (const PointerType *PT = T->getAs<PointerType>()) {
452 Out << '*';
453 T = PT->getPointeeType();
454 continue;
455 }
456 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
457 Out << '&';
458 T = RT->getPointeeType();
459 continue;
460 }
461 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
462 Out << 'F';
463 VisitType(FT->getResultType());
464 for (FunctionProtoType::arg_type_iterator
465 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
466 VisitType(*I);
467 }
468 if (FT->isVariadic())
469 Out << '.';
470 return;
471 }
472 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
473 Out << 'B';
474 T = BT->getPointeeType();
475 continue;
476 }
477 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
478 unsigned char c = '\0';
479 switch (BT->getKind()) {
480 case BuiltinType::Void:
481 c = 'v'; break;
482 case BuiltinType::Bool:
483 c = 'b'; break;
484 case BuiltinType::Char_U:
485 case BuiltinType::UChar:
486 c = 'c'; break;
487 case BuiltinType::Char16:
488 c = 'q'; break;
489 case BuiltinType::Char32:
490 c = 'w'; break;
491 case BuiltinType::UShort:
492 c = 's'; break;
493 case BuiltinType::UInt:
494 c = 'i'; break;
495 case BuiltinType::ULong:
496 c = 'l'; break;
497 case BuiltinType::ULongLong:
498 c = 'k'; break;
499 case BuiltinType::UInt128:
500 c = 'j'; break;
501 case BuiltinType::Char_S:
502 case BuiltinType::SChar:
503 c = 'C'; break;
504 case BuiltinType::WChar:
505 c = 'W'; break;
506 case BuiltinType::Short:
507 c = 'S'; break;
508 case BuiltinType::Int:
509 c = 'I'; break;
510 case BuiltinType::Long:
511 c = 'L'; break;
512 case BuiltinType::LongLong:
513 c = 'K'; break;
514 case BuiltinType::Int128:
515 c = 'J'; break;
516 case BuiltinType::Float:
517 c = 'f'; break;
518 case BuiltinType::Double:
519 c = 'd'; break;
520 case BuiltinType::LongDouble:
521 c = 'D'; break;
522 case BuiltinType::NullPtr:
523 c = 'n'; break;
524 case BuiltinType::Overload:
525 case BuiltinType::Dependent:
526 case BuiltinType::UndeducedAuto:
527 IgnoreResults = true;
528 return;
529 case BuiltinType::ObjCId:
530 c = 'o'; break;
531 case BuiltinType::ObjCClass:
532 c = 'O'; break;
533 case BuiltinType::ObjCSel:
534 c = 'e'; break;
535 }
536 Out << c;
537 return;
538 }
539 if (const ComplexType *CT = T->getAs<ComplexType>()) {
540 Out << '<';
541 T = CT->getElementType();
542 continue;
543 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000544 if (const TagType *TT = T->getAs<TagType>()) {
545 Out << '$';
546 VisitTagDecl(TT->getDecl());
547 return;
548 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000549 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
550 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
551 return;
552 }
553 if (const TemplateSpecializationType *Spec
554 = T->getAs<TemplateSpecializationType>()) {
555 Out << '>';
556 VisitTemplateName(Spec->getTemplateName());
557 Out << Spec->getNumArgs();
558 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
559 VisitTemplateArgument(Spec->getArg(I));
560 return;
561 }
562
Ted Kremenek8e672192010-05-07 01:04:32 +0000563 // Unhandled type.
564 Out << ' ';
565 break;
566 } while (true);
567}
568
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000569void USRGenerator::VisitTemplateParameterList(
570 const TemplateParameterList *Params) {
571 if (!Params)
572 return;
573 Out << '>' << Params->size();
574 for (TemplateParameterList::const_iterator P = Params->begin(),
575 PEnd = Params->end();
576 P != PEnd; ++P) {
577 Out << '#';
578 if (isa<TemplateTypeParmDecl>(*P)) {
579 Out << 'T';
580 continue;
581 }
582
583 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
584 Out << 'N';
585 VisitType(NTTP->getType());
586 continue;
587 }
588
589 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
590 Out << 't';
591 VisitTemplateParameterList(TTP->getTemplateParameters());
592 }
593}
594
595void USRGenerator::VisitTemplateName(TemplateName Name) {
596 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
597 if (TemplateTemplateParmDecl *TTP
598 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
599 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
600 return;
601 }
602
603 Visit(Template);
604 return;
605 }
606
607 // FIXME: Visit dependent template names.
608}
609
610void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
611 switch (Arg.getKind()) {
612 case TemplateArgument::Null:
613 break;
614
615 case TemplateArgument::Declaration:
616 Visit(Arg.getAsDecl());
617 break;
618
619 case TemplateArgument::Template:
620 VisitTemplateName(Arg.getAsTemplate());
621 break;
622
623 case TemplateArgument::Expression:
624 // FIXME: Visit expressions.
625 break;
626
627 case TemplateArgument::Pack:
628 // FIXME: Variadic templates
629 break;
630
631 case TemplateArgument::Type:
632 VisitType(Arg.getAsType());
633 break;
634
635 case TemplateArgument::Integral:
636 Out << 'V';
637 VisitType(Arg.getIntegralType());
638 Out << *Arg.getAsIntegral();
639 break;
640 }
641}
642
Ted Kremenek896b70f2010-03-13 02:50:34 +0000643//===----------------------------------------------------------------------===//
644// General purpose USR generation methods.
645//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000646
Ted Kremenek896b70f2010-03-13 02:50:34 +0000647void USRGenerator::GenObjCClass(llvm::StringRef cls) {
648 Out << "objc(cs)" << cls;
649}
650
651void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000652 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000653}
654
655void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000656 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000657}
658
659void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
660 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
661}
662
663void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
664 Out << "(py)" << prop;
665}
666
667void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
668 Out << "objc(pl)" << prot;
669}
670
671//===----------------------------------------------------------------------===//
672// API hooks.
673//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000674
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000675static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
676 return s.startswith("c:") ? s.substr(2) : "";
677}
678
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000679static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000680 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000681
682 // Don't generate USRs for things with invalid locations.
683 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000684 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000685
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000686 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000687 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000688 switch (ND->getLinkage()) {
689 case ExternalLinkage:
690 // Generate USRs for all entities with external linkage.
691 break;
692 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000693 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000694 // We allow enums, typedefs, and structs that have no linkage to
695 // have USRs that are anchored to the file they were defined in
696 // (e.g., the header). This is a little gross, but in principal
697 // enums/anonymous structs/etc. defined in a common header file
698 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000699 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000700 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000701 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000702 break;
703 // Fall-through.
704 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000705 if (isa<FunctionDecl>(ND))
706 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000707 }
708
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000709 USRGenerator UG(&C);
710 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000711
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000712 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000713 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000714
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000715#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000716 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000717 assert(UG.str().size() > 2);
718#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000719
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000720 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000721 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000722}
723
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000724extern "C" {
725
726CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000727 const CXCursorKind &K = clang_getCursorKind(C);
728
729 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000730 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000731
732 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000733 USRGenerator UG(&C);
734 UG << "macro@"
735 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
736 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000737 }
738
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000739 return createCXString("");
740}
741
Ted Kremenek896b70f2010-03-13 02:50:34 +0000742CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000743 USRGenerator UG;
744 UG << extractUSRSuffix(clang_getCString(classUSR));
745 UG->GenObjCIvar(name);
746 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000747}
748
749CXString clang_constructUSR_ObjCMethod(const char *name,
750 unsigned isInstanceMethod,
751 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000752 USRGenerator UG;
753 UG << extractUSRSuffix(clang_getCString(classUSR));
754 UG->GenObjCMethod(name, isInstanceMethod);
755 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000756}
757
758CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000759 USRGenerator UG;
760 UG->GenObjCClass(name);
761 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000762}
763
764CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000765 USRGenerator UG;
766 UG->GenObjCProtocol(name);
767 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000768}
769
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000770CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000771 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000772 USRGenerator UG;
773 UG->GenObjCCategory(class_name, category_name);
774 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000775}
776
777CXString clang_constructUSR_ObjCProperty(const char *property,
778 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000779 USRGenerator UG;
780 UG << extractUSRSuffix(clang_getCString(classUSR));
781 UG->GenObjCProperty(property);
782 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000783}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000784
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000785} // end extern "C"