blob: 85478265c061052d657931d7b541cbe7175bb63a [file] [log] [blame]
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001//===- USRGeneration.cpp - Routines for USR generation --------------------===//
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
Argyrios Kyrtzidis15a2fcc2013-08-17 00:40:41 +000010#include "clang/Index/USRGeneration.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000011#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclTemplate.h"
13#include "clang/AST/DeclVisitor.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000014#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/Support/Path.h"
17#include "llvm/Support/raw_ostream.h"
18
19using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000020using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000021
22//===----------------------------------------------------------------------===//
23// USR generation.
24//===----------------------------------------------------------------------===//
25
Dmitri Gribenko237769e2014-03-28 22:21:26 +000026/// \returns true on error.
27static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
28 const SourceManager &SM, bool IncludeOffset) {
29 if (Loc.isInvalid()) {
30 return true;
31 }
32 Loc = SM.getExpansionLoc(Loc);
33 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
34 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
35 if (FE) {
36 OS << llvm::sys::path::filename(FE->getName());
37 } else {
38 // This case really isn't interesting.
39 return true;
40 }
41 if (IncludeOffset) {
42 // Use the offest into the FileID to represent the location. Using
43 // a line/column can cause us to look back at the original source file,
44 // which is expensive.
45 OS << '@' << Decomposed.second;
46 }
47 return false;
48}
49
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000050namespace {
51class USRGenerator : public ConstDeclVisitor<USRGenerator> {
52 SmallVectorImpl<char> &Buf;
53 llvm::raw_svector_ostream Out;
54 bool IgnoreResults;
55 ASTContext *Context;
56 bool generatedLoc;
57
58 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
59
60public:
61 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
62 : Buf(Buf),
63 Out(Buf),
64 IgnoreResults(false),
65 Context(Ctx),
66 generatedLoc(false)
67 {
68 // Add the USR space prefix.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000069 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000070 }
71
72 bool ignoreResults() const { return IgnoreResults; }
73
74 // Visitation methods from generating USRs from AST elements.
75 void VisitDeclContext(const DeclContext *D);
76 void VisitFieldDecl(const FieldDecl *D);
77 void VisitFunctionDecl(const FunctionDecl *D);
78 void VisitNamedDecl(const NamedDecl *D);
79 void VisitNamespaceDecl(const NamespaceDecl *D);
80 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
81 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
82 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
83 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
84 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
85 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
86 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
87 void VisitTagDecl(const TagDecl *D);
88 void VisitTypedefDecl(const TypedefDecl *D);
89 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
90 void VisitVarDecl(const VarDecl *D);
91 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
92 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +000093
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000094 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
95 IgnoreResults = true;
96 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +000097
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000098 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
99 IgnoreResults = true;
100 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000101
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000102 void VisitUsingDecl(const UsingDecl *D) {
103 IgnoreResults = true;
104 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000105
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000106 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
107 IgnoreResults = true;
108 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000109
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000110 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
111 IgnoreResults = true;
112 }
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000113
114 bool ShouldGenerateLocation(const NamedDecl *D);
115
116 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000117 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000118 }
119
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000120 /// Generate the string component containing the location of the
121 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000122 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000123
124 /// String generation methods used both by the visitation methods
125 /// and from other clients that want to directly generate USRs. These
126 /// methods do not construct complete USRs (which incorporate the parents
127 /// of an AST element), but only the fragments concerning the AST element
128 /// itself.
129
130 /// Generate a USR for an Objective-C class.
131 void GenObjCClass(StringRef cls) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000132 generateUSRForObjCClass(cls, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000133 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000134
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000135 /// Generate a USR for an Objective-C class category.
136 void GenObjCCategory(StringRef cls, StringRef cat) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000137 generateUSRForObjCCategory(cls, cat, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000138 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000139
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000140 /// Generate a USR fragment for an Objective-C property.
141 void GenObjCProperty(StringRef prop) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000142 generateUSRForObjCProperty(prop, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000143 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000144
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000145 /// Generate a USR for an Objective-C protocol.
146 void GenObjCProtocol(StringRef prot) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000147 generateUSRForObjCProtocol(prot, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000148 }
149
150 void VisitType(QualType T);
151 void VisitTemplateParameterList(const TemplateParameterList *Params);
152 void VisitTemplateName(TemplateName Name);
153 void VisitTemplateArgument(const TemplateArgument &Arg);
154
155 /// Emit a Decl's name using NamedDecl::printName() and return true if
156 /// the decl had no name.
157 bool EmitDeclName(const NamedDecl *D);
158};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000159} // end anonymous namespace
160
161//===----------------------------------------------------------------------===//
162// Generating USRs from ASTS.
163//===----------------------------------------------------------------------===//
164
165bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000166 const unsigned startSize = Buf.size();
167 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000168 const unsigned endSize = Buf.size();
169 return startSize == endSize;
170}
171
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000172bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
173 if (D->isExternallyVisible())
174 return false;
175 if (D->getParentFunctionOrMethod())
176 return true;
177 const SourceManager &SM = Context->getSourceManager();
178 return !SM.isInSystemHeader(D->getLocation());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000179}
180
181void USRGenerator::VisitDeclContext(const DeclContext *DC) {
182 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
183 Visit(D);
184}
185
186void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
187 // The USR for an ivar declared in a class extension is based on the
188 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
189 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
190 Visit(ID);
191 else
192 VisitDeclContext(D->getDeclContext());
193 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
194 if (EmitDeclName(D)) {
195 // Bit fields can be anonymous.
196 IgnoreResults = true;
197 return;
198 }
199}
200
201void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000202 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000203 return;
204
205 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000206 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000207 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000208 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000209 Out << "@FT@";
210 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
211 } else
212 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000213
214 PrintingPolicy Policy(Context->getLangOpts());
215 // Forward references can have different template argument names. Suppress the
216 // template argument names in constructors to make their USR more stable.
217 Policy.SuppressTemplateArgsInCXXConstructors = true;
218 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000219
220 ASTContext &Ctx = *Context;
221 if (!Ctx.getLangOpts().CPlusPlus || D->isExternC())
222 return;
223
224 if (const TemplateArgumentList *
225 SpecArgs = D->getTemplateSpecializationArgs()) {
226 Out << '<';
227 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
228 Out << '#';
229 VisitTemplateArgument(SpecArgs->get(I));
230 }
231 Out << '>';
232 }
233
234 // Mangle in type information for the arguments.
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000235 for (auto PD : D->params()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000236 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000237 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000238 }
239 if (D->isVariadic())
240 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000241 if (IsTemplate) {
242 // Function templates can be overloaded by return type, for example:
243 // \code
244 // template <class T> typename T::A foo() {}
245 // template <class T> typename T::B foo() {}
246 // \endcode
247 Out << '#';
248 VisitType(D->getReturnType());
249 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000250 Out << '#';
251 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
252 if (MD->isStatic())
253 Out << 'S';
254 if (unsigned quals = MD->getTypeQualifiers())
255 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000256 switch (MD->getRefQualifier()) {
257 case RQ_None: break;
258 case RQ_LValue: Out << '&'; break;
259 case RQ_RValue: Out << "&&"; break;
260 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000261 }
262}
263
264void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
265 VisitDeclContext(D->getDeclContext());
266 Out << "@";
267
268 if (EmitDeclName(D)) {
269 // The string can be empty if the declaration has no name; e.g., it is
270 // the ParmDecl with no name for declaration of a function pointer type,
271 // e.g.: void (*f)(void *);
272 // In this case, don't generate a USR.
273 IgnoreResults = true;
274 }
275}
276
277void USRGenerator::VisitVarDecl(const VarDecl *D) {
278 // VarDecls can be declared 'extern' within a function or method body,
279 // but their enclosing DeclContext is the function, not the TU. We need
280 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000281 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000282 return;
283
284 VisitDeclContext(D->getDeclContext());
285
286 // Variables always have simple names.
287 StringRef s = D->getName();
288
289 // The string can be empty if the declaration has no name; e.g., it is
290 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
291 // void (*f)(void *);
292 // In this case, don't generate a USR.
293 if (s.empty())
294 IgnoreResults = true;
295 else
296 Out << '@' << s;
297}
298
299void USRGenerator::VisitNonTypeTemplateParmDecl(
300 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000301 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000302}
303
304void USRGenerator::VisitTemplateTemplateParmDecl(
305 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000306 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000307}
308
309void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
310 if (D->isAnonymousNamespace()) {
311 Out << "@aN";
312 return;
313 }
314
315 VisitDeclContext(D->getDeclContext());
316 if (!IgnoreResults)
317 Out << "@N@" << D->getName();
318}
319
320void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
321 VisitFunctionDecl(D->getTemplatedDecl());
322}
323
324void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
325 VisitTagDecl(D->getTemplatedDecl());
326}
327
328void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
329 VisitDeclContext(D->getDeclContext());
330 if (!IgnoreResults)
331 Out << "@NA@" << D->getName();
332}
333
334void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
335 const DeclContext *container = D->getDeclContext();
336 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
337 Visit(pd);
338 }
339 else {
340 // The USR for a method declared in a class extension or category is based on
341 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
342 const ObjCInterfaceDecl *ID = D->getClassInterface();
343 if (!ID) {
344 IgnoreResults = true;
345 return;
346 }
347 Visit(ID);
348 }
349 // Ideally we would use 'GenObjCMethod', but this is such a hot path
350 // for Objective-C code that we don't want to use
351 // DeclarationName::getAsString().
352 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
353 << DeclarationName(D->getSelector());
354}
355
356void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
357 switch (D->getKind()) {
358 default:
359 llvm_unreachable("Invalid ObjC container.");
360 case Decl::ObjCInterface:
361 case Decl::ObjCImplementation:
362 GenObjCClass(D->getName());
363 break;
364 case Decl::ObjCCategory: {
365 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
366 const ObjCInterfaceDecl *ID = CD->getClassInterface();
367 if (!ID) {
368 // Handle invalid code where the @interface might not
369 // have been specified.
370 // FIXME: We should be able to generate this USR even if the
371 // @interface isn't available.
372 IgnoreResults = true;
373 return;
374 }
375 // Specially handle class extensions, which are anonymous categories.
376 // We want to mangle in the location to uniquely distinguish them.
377 if (CD->IsClassExtension()) {
378 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000379 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000380 }
381 else
382 GenObjCCategory(ID->getName(), CD->getName());
383
384 break;
385 }
386 case Decl::ObjCCategoryImpl: {
387 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
388 const ObjCInterfaceDecl *ID = CD->getClassInterface();
389 if (!ID) {
390 // Handle invalid code where the @interface might not
391 // have been specified.
392 // FIXME: We should be able to generate this USR even if the
393 // @interface isn't available.
394 IgnoreResults = true;
395 return;
396 }
397 GenObjCCategory(ID->getName(), CD->getName());
398 break;
399 }
400 case Decl::ObjCProtocol:
401 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
402 break;
403 }
404}
405
406void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
407 // The USR for a property declared in a class extension or category is based
408 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
409 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
410 Visit(ID);
411 else
412 Visit(cast<Decl>(D->getDeclContext()));
413 GenObjCProperty(D->getName());
414}
415
416void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
417 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
418 VisitObjCPropertyDecl(PD);
419 return;
420 }
421
422 IgnoreResults = true;
423}
424
425void USRGenerator::VisitTagDecl(const TagDecl *D) {
426 // Add the location of the tag decl to handle resolution across
427 // translation units.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000428 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000429 return;
430
431 D = D->getCanonicalDecl();
432 VisitDeclContext(D->getDeclContext());
433
434 bool AlreadyStarted = false;
435 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
436 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
437 AlreadyStarted = true;
438
439 switch (D->getTagKind()) {
440 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000441 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000442 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000443 case TTK_Union: Out << "@UT"; break;
444 case TTK_Enum: llvm_unreachable("enum template");
445 }
446 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
447 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
448 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
449 AlreadyStarted = true;
450
451 switch (D->getTagKind()) {
452 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000453 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000454 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000455 case TTK_Union: Out << "@UP"; break;
456 case TTK_Enum: llvm_unreachable("enum partial specialization");
457 }
458 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
459 }
460 }
461
462 if (!AlreadyStarted) {
463 switch (D->getTagKind()) {
464 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000465 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000466 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000467 case TTK_Union: Out << "@U"; break;
468 case TTK_Enum: Out << "@E"; break;
469 }
470 }
471
472 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000473 assert(Buf.size() > 0);
474 const unsigned off = Buf.size() - 1;
475
476 if (EmitDeclName(D)) {
477 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
478 Buf[off] = 'A';
479 Out << '@' << *TD;
480 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000481 else {
482 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
483 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
484 } else
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000485 Buf[off] = 'a';
486 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000487 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000488
489 // For a class template specialization, mangle the template arguments.
490 if (const ClassTemplateSpecializationDecl *Spec
491 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
492 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
493 Out << '>';
494 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
495 Out << '#';
496 VisitTemplateArgument(Args.get(I));
497 }
498 }
499}
500
501void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000502 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000503 return;
504 const DeclContext *DC = D->getDeclContext();
505 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
506 Visit(DCN);
507 Out << "@T@";
508 Out << D->getName();
509}
510
511void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000512 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000513}
514
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000515bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000516 if (generatedLoc)
517 return IgnoreResults;
518 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000519
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000520 // Guard against null declarations in invalid code.
521 if (!D) {
522 IgnoreResults = true;
523 return true;
524 }
525
526 // Use the location of canonical decl.
527 D = D->getCanonicalDecl();
528
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000529 IgnoreResults =
530 IgnoreResults || printLoc(Out, D->getLocStart(),
531 Context->getSourceManager(), IncludeOffset);
532
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000533 return IgnoreResults;
534}
535
536void USRGenerator::VisitType(QualType T) {
537 // This method mangles in USR information for types. It can possibly
538 // just reuse the naming-mangling logic used by codegen, although the
539 // requirements for USRs might not be the same.
540 ASTContext &Ctx = *Context;
541
542 do {
543 T = Ctx.getCanonicalType(T);
544 Qualifiers Q = T.getQualifiers();
545 unsigned qVal = 0;
546 if (Q.hasConst())
547 qVal |= 0x1;
548 if (Q.hasVolatile())
549 qVal |= 0x2;
550 if (Q.hasRestrict())
551 qVal |= 0x4;
552 if(qVal)
553 Out << ((char) ('0' + qVal));
554
555 // Mangle in ObjC GC qualifiers?
556
557 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
558 Out << 'P';
559 T = Expansion->getPattern();
560 }
561
562 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
563 unsigned char c = '\0';
564 switch (BT->getKind()) {
565 case BuiltinType::Void:
566 c = 'v'; break;
567 case BuiltinType::Bool:
568 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000569 case BuiltinType::UChar:
570 c = 'c'; break;
571 case BuiltinType::Char16:
572 c = 'q'; break;
573 case BuiltinType::Char32:
574 c = 'w'; break;
575 case BuiltinType::UShort:
576 c = 's'; break;
577 case BuiltinType::UInt:
578 c = 'i'; break;
579 case BuiltinType::ULong:
580 c = 'l'; break;
581 case BuiltinType::ULongLong:
582 c = 'k'; break;
583 case BuiltinType::UInt128:
584 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000585 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000586 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000587 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000588 case BuiltinType::SChar:
589 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000590 case BuiltinType::WChar_S:
591 case BuiltinType::WChar_U:
592 c = 'W'; break;
593 case BuiltinType::Short:
594 c = 'S'; break;
595 case BuiltinType::Int:
596 c = 'I'; break;
597 case BuiltinType::Long:
598 c = 'L'; break;
599 case BuiltinType::LongLong:
600 c = 'K'; break;
601 case BuiltinType::Int128:
602 c = 'J'; break;
603 case BuiltinType::Half:
604 c = 'h'; break;
605 case BuiltinType::Float:
606 c = 'f'; break;
607 case BuiltinType::Double:
608 c = 'd'; break;
609 case BuiltinType::LongDouble:
610 c = 'D'; break;
611 case BuiltinType::NullPtr:
612 c = 'n'; break;
613#define BUILTIN_TYPE(Id, SingletonId)
614#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
615#include "clang/AST/BuiltinTypes.def"
616 case BuiltinType::Dependent:
617 case BuiltinType::OCLImage1d:
618 case BuiltinType::OCLImage1dArray:
619 case BuiltinType::OCLImage1dBuffer:
620 case BuiltinType::OCLImage2d:
621 case BuiltinType::OCLImage2dArray:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000622 case BuiltinType::OCLImage2dDepth:
623 case BuiltinType::OCLImage2dArrayDepth:
624 case BuiltinType::OCLImage2dMSAA:
625 case BuiltinType::OCLImage2dArrayMSAA:
626 case BuiltinType::OCLImage2dMSAADepth:
627 case BuiltinType::OCLImage2dArrayMSAADepth:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000628 case BuiltinType::OCLImage3d:
629 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000630 case BuiltinType::OCLClkEvent:
631 case BuiltinType::OCLQueue:
632 case BuiltinType::OCLNDRange:
633 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000634 case BuiltinType::OCLSampler:
635 IgnoreResults = true;
636 return;
637 case BuiltinType::ObjCId:
638 c = 'o'; break;
639 case BuiltinType::ObjCClass:
640 c = 'O'; break;
641 case BuiltinType::ObjCSel:
642 c = 'e'; break;
643 }
644 Out << c;
645 return;
646 }
647
648 // If we have already seen this (non-built-in) type, use a substitution
649 // encoding.
650 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
651 = TypeSubstitutions.find(T.getTypePtr());
652 if (Substitution != TypeSubstitutions.end()) {
653 Out << 'S' << Substitution->second << '_';
654 return;
655 } else {
656 // Record this as a substitution.
657 unsigned Number = TypeSubstitutions.size();
658 TypeSubstitutions[T.getTypePtr()] = Number;
659 }
660
661 if (const PointerType *PT = T->getAs<PointerType>()) {
662 Out << '*';
663 T = PT->getPointeeType();
664 continue;
665 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000666 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
667 Out << '*';
668 T = OPT->getPointeeType();
669 continue;
670 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000671 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
672 Out << "&&";
673 T = RT->getPointeeType();
674 continue;
675 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000676 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
677 Out << '&';
678 T = RT->getPointeeType();
679 continue;
680 }
681 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
682 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000683 VisitType(FT->getReturnType());
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000684 for (const auto &I : FT->param_types())
685 VisitType(I);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000686 if (FT->isVariadic())
687 Out << '.';
688 return;
689 }
690 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
691 Out << 'B';
692 T = BT->getPointeeType();
693 continue;
694 }
695 if (const ComplexType *CT = T->getAs<ComplexType>()) {
696 Out << '<';
697 T = CT->getElementType();
698 continue;
699 }
700 if (const TagType *TT = T->getAs<TagType>()) {
701 Out << '$';
702 VisitTagDecl(TT->getDecl());
703 return;
704 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000705 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
706 Out << '$';
707 VisitObjCInterfaceDecl(OIT->getDecl());
708 return;
709 }
710 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
711 Out << 'Q';
712 VisitType(OIT->getBaseType());
713 for (auto *Prot : OIT->getProtocols())
714 VisitObjCProtocolDecl(Prot);
715 return;
716 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000717 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
718 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
719 return;
720 }
721 if (const TemplateSpecializationType *Spec
722 = T->getAs<TemplateSpecializationType>()) {
723 Out << '>';
724 VisitTemplateName(Spec->getTemplateName());
725 Out << Spec->getNumArgs();
726 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
727 VisitTemplateArgument(Spec->getArg(I));
728 return;
729 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000730 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
731 Out << '^';
732 // FIXME: Encode the qualifier, don't just print it.
733 PrintingPolicy PO(Ctx.getLangOpts());
734 PO.SuppressTagKeyword = true;
735 PO.SuppressUnwrittenScope = true;
736 PO.ConstantArraySizeAsWritten = false;
737 PO.AnonymousTagLocations = false;
738 DNT->getQualifier()->print(Out, PO);
739 Out << ':' << DNT->getIdentifier()->getName();
740 return;
741 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000742 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
743 T = InjT->getInjectedSpecializationType();
744 continue;
745 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000746
747 // Unhandled type.
748 Out << ' ';
749 break;
750 } while (true);
751}
752
753void USRGenerator::VisitTemplateParameterList(
754 const TemplateParameterList *Params) {
755 if (!Params)
756 return;
757 Out << '>' << Params->size();
758 for (TemplateParameterList::const_iterator P = Params->begin(),
759 PEnd = Params->end();
760 P != PEnd; ++P) {
761 Out << '#';
762 if (isa<TemplateTypeParmDecl>(*P)) {
763 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
764 Out<< 'p';
765 Out << 'T';
766 continue;
767 }
768
769 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
770 if (NTTP->isParameterPack())
771 Out << 'p';
772 Out << 'N';
773 VisitType(NTTP->getType());
774 continue;
775 }
776
777 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
778 if (TTP->isParameterPack())
779 Out << 'p';
780 Out << 't';
781 VisitTemplateParameterList(TTP->getTemplateParameters());
782 }
783}
784
785void USRGenerator::VisitTemplateName(TemplateName Name) {
786 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
787 if (TemplateTemplateParmDecl *TTP
788 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
789 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
790 return;
791 }
792
793 Visit(Template);
794 return;
795 }
796
797 // FIXME: Visit dependent template names.
798}
799
800void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
801 switch (Arg.getKind()) {
802 case TemplateArgument::Null:
803 break;
804
805 case TemplateArgument::Declaration:
806 Visit(Arg.getAsDecl());
807 break;
808
809 case TemplateArgument::NullPtr:
810 break;
811
812 case TemplateArgument::TemplateExpansion:
813 Out << 'P'; // pack expansion of...
814 // Fall through
815 case TemplateArgument::Template:
816 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
817 break;
818
819 case TemplateArgument::Expression:
820 // FIXME: Visit expressions.
821 break;
822
823 case TemplateArgument::Pack:
824 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000825 for (const auto &P : Arg.pack_elements())
826 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000827 break;
828
829 case TemplateArgument::Type:
830 VisitType(Arg.getAsType());
831 break;
832
833 case TemplateArgument::Integral:
834 Out << 'V';
835 VisitType(Arg.getIntegralType());
836 Out << Arg.getAsIntegral();
837 break;
838 }
839}
840
841//===----------------------------------------------------------------------===//
842// USR generation functions.
843//===----------------------------------------------------------------------===//
844
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000845void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000846 OS << "objc(cs)" << Cls;
847}
848
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000849void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
850 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000851 OS << "objc(cy)" << Cls << '@' << Cat;
852}
853
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000854void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000855 OS << '@' << Ivar;
856}
857
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000858void clang::index::generateUSRForObjCMethod(StringRef Sel,
859 bool IsInstanceMethod,
860 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000861 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
862}
863
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000864void clang::index::generateUSRForObjCProperty(StringRef Prop, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000865 OS << "(py)" << Prop;
866}
867
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000868void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000869 OS << "objc(pl)" << Prot;
870}
871
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000872bool clang::index::generateUSRForDecl(const Decl *D,
873 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000874 // Don't generate USRs for things with invalid locations.
875 if (!D || D->getLocStart().isInvalid())
876 return true;
877
878 USRGenerator UG(&D->getASTContext(), Buf);
879 UG.Visit(D);
880 return UG.ignoreResults();
881}
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000882
Richard Smith66a81862015-05-04 02:25:31 +0000883bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000884 const SourceManager &SM,
885 SmallVectorImpl<char> &Buf) {
886 // Don't generate USRs for things with invalid locations.
887 if (!MD || MD->getLocation().isInvalid())
888 return true;
889
890 llvm::raw_svector_ostream Out(Buf);
891
892 // Assume that system headers are sane. Don't put source location
893 // information into the USR if the macro comes from a system header.
894 SourceLocation Loc = MD->getLocation();
895 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
896
897 Out << getUSRSpacePrefix();
898 if (ShouldGenerateLocation)
899 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
900 Out << "@macro@";
Alp Toker541d5072014-06-07 23:30:53 +0000901 Out << MD->getName()->getName();
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000902 return false;
903}