blob: b397f1e0d42f36fe60429a27ca1fd0ca59ae4505 [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);
Douglas Gregor39d6f072010-08-31 19:02:00 +000069 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000070 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000071 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000072 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000073 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000074 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000075 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000076 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000077 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000078 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000079 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000080 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
81 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000082 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
83 IgnoreResults = true;
84 return;
85 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000086
Ted Kremenek6f153952010-04-15 21:51:13 +000087 /// Generate the string component containing the location of the
88 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000089 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000090
Ted Kremenek896b70f2010-03-13 02:50:34 +000091 /// String generation methods used both by the visitation methods
92 /// and from other clients that want to directly generate USRs. These
93 /// methods do not construct complete USRs (which incorporate the parents
94 /// of an AST element), but only the fragments concerning the AST element
95 /// itself.
96
Ted Kremenek896b70f2010-03-13 02:50:34 +000097 /// Generate a USR for an Objective-C class.
98 void GenObjCClass(llvm::StringRef cls);
99 /// Generate a USR for an Objective-C class category.
100 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
101 /// Generate a USR fragment for an Objective-C instance variable. The
102 /// complete USR can be created by concatenating the USR for the
103 /// encompassing class with this USR fragment.
104 void GenObjCIvar(llvm::StringRef ivar);
105 /// Generate a USR fragment for an Objective-C method.
106 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
107 /// Generate a USR fragment for an Objective-C property.
108 void GenObjCProperty(llvm::StringRef prop);
109 /// Generate a USR for an Objective-C protocol.
110 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000111
112 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000113 void VisitTemplateParameterList(const TemplateParameterList *Params);
114 void VisitTemplateName(TemplateName Name);
115 void VisitTemplateArgument(const TemplateArgument &Arg);
116
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000117 /// Emit a Decl's name using NamedDecl::printName() and return true if
118 /// the decl had no name.
119 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000120};
121
Ted Kremenekc50277f2010-01-12 23:33:42 +0000122} // end anonymous namespace
123
Ted Kremenek896b70f2010-03-13 02:50:34 +0000124//===----------------------------------------------------------------------===//
125// Generating USRs from ASTS.
126//===----------------------------------------------------------------------===//
127
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000128bool USRGenerator::EmitDeclName(const NamedDecl *D) {
129 Out.flush();
130 const unsigned startSize = Buf.size();
131 D->printName(Out);
132 Out.flush();
133 const unsigned endSize = Buf.size();
134 return startSize == endSize;
135}
136
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000137static bool InAnonymousNamespace(const Decl *D) {
138 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
139 return ND->isAnonymousNamespace();
140 return false;
141}
142
143static inline bool ShouldGenerateLocation(const NamedDecl *D) {
144 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
145}
146
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000147void USRGenerator::VisitDeclContext(DeclContext *DC) {
148 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
149 Visit(D);
150}
151
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000152void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000153 VisitDeclContext(D->getDeclContext());
154 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
155 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000156 // Bit fields can be anonymous.
157 IgnoreResults = true;
158 return;
159 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000160}
161
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000162void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000163 if (ShouldGenerateLocation(D) && GenLoc(D))
164 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000165
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000166 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000167 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
168 Out << "@FT@";
169 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
170 } else
171 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000172 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000173
174 ASTContext &Ctx = AU->getASTContext();
175 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
176 return;
177
178 // Mangle in type information for the arguments.
179 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
180 I != E; ++I) {
181 Out << '#';
182 if (ParmVarDecl *PD = *I)
183 VisitType(PD->getType());
184 }
185 if (D->isVariadic())
186 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000187 Out << '#';
188 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
189 if (MD->isStatic())
190 Out << 'S';
191 if (unsigned quals = MD->getTypeQualifiers())
192 Out << (char)('0' + quals);
193 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000194}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000195
196void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000197 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000198 Out << "@";
199
200 if (EmitDeclName(D)) {
201 // The string can be empty if the declaration has no name; e.g., it is
202 // the ParmDecl with no name for declaration of a function pointer type,
203 // e.g.: void (*f)(void *);
204 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000205 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000206 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000207}
208
Ted Kremeneke542f772010-04-20 23:15:40 +0000209void USRGenerator::VisitVarDecl(VarDecl *D) {
210 // VarDecls can be declared 'extern' within a function or method body,
211 // but their enclosing DeclContext is the function, not the TU. We need
212 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000213 if (ShouldGenerateLocation(D) && GenLoc(D))
214 return;
215
216 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000217
Ted Kremenekcf999102010-04-29 17:43:29 +0000218 // Variables always have simple names.
219 llvm::StringRef s = D->getName();
220
Ted Kremeneke542f772010-04-20 23:15:40 +0000221 // The string can be empty if the declaration has no name; e.g., it is
222 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000223 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000224 // In this case, don't generate a USR.
225 if (s.empty())
226 IgnoreResults = true;
227 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000228 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000229}
230
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000231void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
232 GenLoc(D);
233 return;
234}
235
236void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
237 GenLoc(D);
238 return;
239}
240
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000241void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000242 if (D->isAnonymousNamespace()) {
243 Out << "@aN";
244 return;
245 }
246
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000247 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000248 if (!IgnoreResults)
249 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000250}
251
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000252void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
253 VisitFunctionDecl(D->getTemplatedDecl());
254}
255
Douglas Gregor39d6f072010-08-31 19:02:00 +0000256void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
257 VisitTagDecl(D->getTemplatedDecl());
258}
259
260
Ted Kremenekc50277f2010-01-12 23:33:42 +0000261void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000262 Decl *container = cast<Decl>(D->getDeclContext());
263
264 // The USR for a method declared in a class extension is based on
265 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
266 do {
267 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
268 if (CD->IsClassExtension()) {
269 Visit(CD->getClassInterface());
270 break;
271 }
272 Visit(cast<Decl>(D->getDeclContext()));
273 }
274 while (false);
275
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000276 // Ideally we would use 'GenObjCMethod', but this is such a hot path
277 // for Objective-C code that we don't want to use
278 // DeclarationName::getAsString().
279 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
280 DeclarationName N(D->getSelector());
281 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000282}
283
Ted Kremeneke74ef122010-04-16 21:31:52 +0000284void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
285 // FIXME: @class declarations can refer to multiple classes. We need
286 // to be able to traverse these.
287 IgnoreResults = true;
288}
289
290void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
291 // FIXME: @protocol declarations can refer to multiple protocols. We need
292 // to be able to traverse these.
293 IgnoreResults = true;
294}
295
Ted Kremenekc50277f2010-01-12 23:33:42 +0000296void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
297 switch (D->getKind()) {
298 default:
299 assert(false && "Invalid ObjC container.");
300 case Decl::ObjCInterface:
301 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000302 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000303 break;
304 case Decl::ObjCCategory: {
305 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000306 ObjCInterfaceDecl *ID = CD->getClassInterface();
307 if (!ID) {
308 // Handle invalid code where the @interface might not
309 // have been specified.
310 // FIXME: We should be able to generate this USR even if the
311 // @interface isn't available.
312 IgnoreResults = true;
313 return;
314 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000315 // Specially handle class extensions, which are anonymous categories.
316 // We want to mangle in the location to uniquely distinguish them.
317 if (CD->IsClassExtension()) {
318 Out << "objc(ext)" << ID->getName() << '@';
319 GenLoc(CD);
320 }
321 else
322 GenObjCCategory(ID->getName(), CD->getName());
323
Ted Kremenekc50277f2010-01-12 23:33:42 +0000324 break;
325 }
326 case Decl::ObjCCategoryImpl: {
327 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000328 ObjCInterfaceDecl *ID = CD->getClassInterface();
329 if (!ID) {
330 // Handle invalid code where the @interface might not
331 // have been specified.
332 // FIXME: We should be able to generate this USR even if the
333 // @interface isn't available.
334 IgnoreResults = true;
335 return;
336 }
337 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000338 break;
339 }
340 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000341 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000342 break;
343 }
344}
345
346void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
347 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000348 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000349}
350
Ted Kremeneke542f772010-04-20 23:15:40 +0000351void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
352 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
353 VisitObjCPropertyDecl(PD);
354 return;
355 }
356
357 IgnoreResults = true;
358}
359
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000360void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000361 // Add the location of the tag decl to handle resolution across
362 // translation units.
363 if (ShouldGenerateLocation(D) && GenLoc(D))
364 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000365
Ted Kremenek6f153952010-04-15 21:51:13 +0000366 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000367 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000368
Douglas Gregor39d6f072010-08-31 19:02:00 +0000369 bool IsTemplate = false;
370 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D))
371 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
372 IsTemplate = true;
373
374 switch (D->getTagKind()) {
375 case TTK_Struct: Out << "@ST"; break;
376 case TTK_Class: Out << "@CT"; break;
377 case TTK_Union: Out << "@UT"; break;
378 case TTK_Enum: llvm_unreachable("enum template");
379 }
380 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
381 }
382
383 if (!IsTemplate) {
384 switch (D->getTagKind()) {
385 case TTK_Struct: Out << "@S"; break;
386 case TTK_Class: Out << "@C"; break;
387 case TTK_Union: Out << "@U"; break;
388 case TTK_Enum: Out << "@E"; break;
389 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000390 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000391
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000392 Out << '@';
393 Out.flush();
394 assert(Buf.size() > 0);
395 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000396
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000397 if (EmitDeclName(D)) {
398 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
399 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000400 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000401 }
402 else
403 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000404 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000405}
406
Ted Kremenekc50277f2010-01-12 23:33:42 +0000407void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000408 if (ShouldGenerateLocation(D) && GenLoc(D))
409 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000410 DeclContext *DC = D->getDeclContext();
411 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000412 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000413 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000414 Out << D->getName();
415}
416
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000417void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
418 GenLoc(D);
419 return;
420}
421
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000422bool USRGenerator::GenLoc(const Decl *D) {
423 if (generatedLoc)
424 return IgnoreResults;
425 generatedLoc = true;
426
Ted Kremenek6f153952010-04-15 21:51:13 +0000427 const SourceManager &SM = AU->getSourceManager();
428 SourceLocation L = D->getLocStart();
429 if (L.isInvalid()) {
430 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000431 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000432 }
433 L = SM.getInstantiationLoc(L);
434 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
435 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000436 if (FE) {
437 llvm::sys::Path P(FE->getName());
438 Out << P.getLast();
439 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000440 else {
441 // This case really isn't interesting.
442 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000443 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000444 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000445 // Use the offest into the FileID to represent the location. Using
446 // a line/column can cause us to look back at the original source file,
447 // which is expensive.
448 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000449 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000450}
451
Ted Kremenek8e672192010-05-07 01:04:32 +0000452void USRGenerator::VisitType(QualType T) {
453 // This method mangles in USR information for types. It can possibly
454 // just reuse the naming-mangling logic used by codegen, although the
455 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000456 ASTContext &Ctx = AU->getASTContext();
457
Ted Kremenek8e672192010-05-07 01:04:32 +0000458 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000459 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000460 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000461 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000462 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000463 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000464 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000465 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000466 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000467 qVal |= 0x4;
468 if(qVal)
469 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000470
471 // Mangle in ObjC GC qualifiers?
472
473 if (const PointerType *PT = T->getAs<PointerType>()) {
474 Out << '*';
475 T = PT->getPointeeType();
476 continue;
477 }
478 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
479 Out << '&';
480 T = RT->getPointeeType();
481 continue;
482 }
483 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
484 Out << 'F';
485 VisitType(FT->getResultType());
486 for (FunctionProtoType::arg_type_iterator
487 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
488 VisitType(*I);
489 }
490 if (FT->isVariadic())
491 Out << '.';
492 return;
493 }
494 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
495 Out << 'B';
496 T = BT->getPointeeType();
497 continue;
498 }
499 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
500 unsigned char c = '\0';
501 switch (BT->getKind()) {
502 case BuiltinType::Void:
503 c = 'v'; break;
504 case BuiltinType::Bool:
505 c = 'b'; break;
506 case BuiltinType::Char_U:
507 case BuiltinType::UChar:
508 c = 'c'; break;
509 case BuiltinType::Char16:
510 c = 'q'; break;
511 case BuiltinType::Char32:
512 c = 'w'; break;
513 case BuiltinType::UShort:
514 c = 's'; break;
515 case BuiltinType::UInt:
516 c = 'i'; break;
517 case BuiltinType::ULong:
518 c = 'l'; break;
519 case BuiltinType::ULongLong:
520 c = 'k'; break;
521 case BuiltinType::UInt128:
522 c = 'j'; break;
523 case BuiltinType::Char_S:
524 case BuiltinType::SChar:
525 c = 'C'; break;
526 case BuiltinType::WChar:
527 c = 'W'; break;
528 case BuiltinType::Short:
529 c = 'S'; break;
530 case BuiltinType::Int:
531 c = 'I'; break;
532 case BuiltinType::Long:
533 c = 'L'; break;
534 case BuiltinType::LongLong:
535 c = 'K'; break;
536 case BuiltinType::Int128:
537 c = 'J'; break;
538 case BuiltinType::Float:
539 c = 'f'; break;
540 case BuiltinType::Double:
541 c = 'd'; break;
542 case BuiltinType::LongDouble:
543 c = 'D'; break;
544 case BuiltinType::NullPtr:
545 c = 'n'; break;
546 case BuiltinType::Overload:
547 case BuiltinType::Dependent:
548 case BuiltinType::UndeducedAuto:
549 IgnoreResults = true;
550 return;
551 case BuiltinType::ObjCId:
552 c = 'o'; break;
553 case BuiltinType::ObjCClass:
554 c = 'O'; break;
555 case BuiltinType::ObjCSel:
556 c = 'e'; break;
557 }
558 Out << c;
559 return;
560 }
561 if (const ComplexType *CT = T->getAs<ComplexType>()) {
562 Out << '<';
563 T = CT->getElementType();
564 continue;
565 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000566 if (const TagType *TT = T->getAs<TagType>()) {
567 Out << '$';
568 VisitTagDecl(TT->getDecl());
569 return;
570 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000571 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
572 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
573 return;
574 }
575 if (const TemplateSpecializationType *Spec
576 = T->getAs<TemplateSpecializationType>()) {
577 Out << '>';
578 VisitTemplateName(Spec->getTemplateName());
579 Out << Spec->getNumArgs();
580 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
581 VisitTemplateArgument(Spec->getArg(I));
582 return;
583 }
584
Ted Kremenek8e672192010-05-07 01:04:32 +0000585 // Unhandled type.
586 Out << ' ';
587 break;
588 } while (true);
589}
590
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000591void USRGenerator::VisitTemplateParameterList(
592 const TemplateParameterList *Params) {
593 if (!Params)
594 return;
595 Out << '>' << Params->size();
596 for (TemplateParameterList::const_iterator P = Params->begin(),
597 PEnd = Params->end();
598 P != PEnd; ++P) {
599 Out << '#';
600 if (isa<TemplateTypeParmDecl>(*P)) {
601 Out << 'T';
602 continue;
603 }
604
605 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
606 Out << 'N';
607 VisitType(NTTP->getType());
608 continue;
609 }
610
611 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
612 Out << 't';
613 VisitTemplateParameterList(TTP->getTemplateParameters());
614 }
615}
616
617void USRGenerator::VisitTemplateName(TemplateName Name) {
618 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
619 if (TemplateTemplateParmDecl *TTP
620 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
621 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
622 return;
623 }
624
625 Visit(Template);
626 return;
627 }
628
629 // FIXME: Visit dependent template names.
630}
631
632void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
633 switch (Arg.getKind()) {
634 case TemplateArgument::Null:
635 break;
636
637 case TemplateArgument::Declaration:
638 Visit(Arg.getAsDecl());
639 break;
640
641 case TemplateArgument::Template:
642 VisitTemplateName(Arg.getAsTemplate());
643 break;
644
645 case TemplateArgument::Expression:
646 // FIXME: Visit expressions.
647 break;
648
649 case TemplateArgument::Pack:
650 // FIXME: Variadic templates
651 break;
652
653 case TemplateArgument::Type:
654 VisitType(Arg.getAsType());
655 break;
656
657 case TemplateArgument::Integral:
658 Out << 'V';
659 VisitType(Arg.getIntegralType());
660 Out << *Arg.getAsIntegral();
661 break;
662 }
663}
664
Ted Kremenek896b70f2010-03-13 02:50:34 +0000665//===----------------------------------------------------------------------===//
666// General purpose USR generation methods.
667//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000668
Ted Kremenek896b70f2010-03-13 02:50:34 +0000669void USRGenerator::GenObjCClass(llvm::StringRef cls) {
670 Out << "objc(cs)" << cls;
671}
672
673void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000674 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000675}
676
677void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000678 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000679}
680
681void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
682 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
683}
684
685void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
686 Out << "(py)" << prop;
687}
688
689void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
690 Out << "objc(pl)" << prot;
691}
692
693//===----------------------------------------------------------------------===//
694// API hooks.
695//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000696
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000697static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
698 return s.startswith("c:") ? s.substr(2) : "";
699}
700
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000701static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000702 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000703
704 // Don't generate USRs for things with invalid locations.
705 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000706 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000707
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000708 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000709 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000710 switch (ND->getLinkage()) {
711 case ExternalLinkage:
712 // Generate USRs for all entities with external linkage.
713 break;
714 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000715 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000716 // We allow enums, typedefs, and structs that have no linkage to
717 // have USRs that are anchored to the file they were defined in
718 // (e.g., the header). This is a little gross, but in principal
719 // enums/anonymous structs/etc. defined in a common header file
720 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000721 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000722 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000723 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000724 break;
725 // Fall-through.
726 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000727 if (isa<FunctionDecl>(ND))
728 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000729 }
730
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000731 USRGenerator UG(&C);
732 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000733
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000734 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000735 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000736
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000737#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000738 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000739 assert(UG.str().size() > 2);
740#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000741
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000742 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000743 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000744}
745
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000746extern "C" {
747
748CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000749 const CXCursorKind &K = clang_getCursorKind(C);
750
751 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000752 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000753
754 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000755 USRGenerator UG(&C);
756 UG << "macro@"
757 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
758 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000759 }
760
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000761 return createCXString("");
762}
763
Ted Kremenek896b70f2010-03-13 02:50:34 +0000764CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000765 USRGenerator UG;
766 UG << extractUSRSuffix(clang_getCString(classUSR));
767 UG->GenObjCIvar(name);
768 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000769}
770
771CXString clang_constructUSR_ObjCMethod(const char *name,
772 unsigned isInstanceMethod,
773 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000774 USRGenerator UG;
775 UG << extractUSRSuffix(clang_getCString(classUSR));
776 UG->GenObjCMethod(name, isInstanceMethod);
777 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000778}
779
780CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000781 USRGenerator UG;
782 UG->GenObjCClass(name);
783 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000784}
785
786CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000787 USRGenerator UG;
788 UG->GenObjCProtocol(name);
789 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000790}
791
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000792CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000793 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000794 USRGenerator UG;
795 UG->GenObjCCategory(class_name, category_name);
796 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000797}
798
799CXString clang_constructUSR_ObjCProperty(const char *property,
800 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000801 USRGenerator UG;
802 UG << extractUSRSuffix(clang_getCString(classUSR));
803 UG->GenObjCProperty(property);
804 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000805}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000806
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000807} // end extern "C"