blob: 9c9600188f74dd130ec2fc2b4aa8408a0951e711 [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/Support/Path.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000019using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000020
21//===----------------------------------------------------------------------===//
22// USR generation.
23//===----------------------------------------------------------------------===//
24
Dmitri Gribenko237769e2014-03-28 22:21:26 +000025/// \returns true on error.
26static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
27 const SourceManager &SM, bool IncludeOffset) {
28 if (Loc.isInvalid()) {
29 return true;
30 }
31 Loc = SM.getExpansionLoc(Loc);
32 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
33 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
34 if (FE) {
35 OS << llvm::sys::path::filename(FE->getName());
36 } else {
37 // This case really isn't interesting.
38 return true;
39 }
40 if (IncludeOffset) {
41 // Use the offest into the FileID to represent the location. Using
42 // a line/column can cause us to look back at the original source file,
43 // which is expensive.
44 OS << '@' << Decomposed.second;
45 }
46 return false;
47}
48
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000049static StringRef GetExternalSourceContainer(const NamedDecl *D) {
50 if (!D)
51 return StringRef();
Argyrios Kyrtzidis11d70482017-05-20 04:11:33 +000052 if (auto *attr = D->getExternalSourceSymbolAttr()) {
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000053 return attr->getDefinedIn();
54 }
55 return StringRef();
56}
57
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000058namespace {
59class USRGenerator : public ConstDeclVisitor<USRGenerator> {
60 SmallVectorImpl<char> &Buf;
61 llvm::raw_svector_ostream Out;
62 bool IgnoreResults;
63 ASTContext *Context;
64 bool generatedLoc;
65
66 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
67
68public:
69 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
70 : Buf(Buf),
71 Out(Buf),
72 IgnoreResults(false),
73 Context(Ctx),
74 generatedLoc(false)
75 {
76 // Add the USR space prefix.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000077 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000078 }
79
80 bool ignoreResults() const { return IgnoreResults; }
81
82 // Visitation methods from generating USRs from AST elements.
83 void VisitDeclContext(const DeclContext *D);
84 void VisitFieldDecl(const FieldDecl *D);
85 void VisitFunctionDecl(const FunctionDecl *D);
86 void VisitNamedDecl(const NamedDecl *D);
87 void VisitNamespaceDecl(const NamespaceDecl *D);
88 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
89 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
90 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +000091 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
92 const ObjCCategoryDecl *CatD = nullptr);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000093 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
94 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
95 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
96 void VisitTagDecl(const TagDecl *D);
97 void VisitTypedefDecl(const TypedefDecl *D);
98 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
99 void VisitVarDecl(const VarDecl *D);
100 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
101 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000102 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
103 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000104
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000105 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Sam McCall2e50ae62018-02-02 14:13:37 +0000106 IgnoreResults = true; // No USRs for linkage specs themselves.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000107 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000108
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000109 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
110 IgnoreResults = true;
111 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000112
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000113 void VisitUsingDecl(const UsingDecl *D) {
114 IgnoreResults = true;
115 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000116
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000117 bool ShouldGenerateLocation(const NamedDecl *D);
118
119 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000120 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000121 }
122
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000123 void GenExtSymbolContainer(const NamedDecl *D);
124
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000125 /// Generate the string component containing the location of the
126 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000127 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000128
129 /// String generation methods used both by the visitation methods
130 /// and from other clients that want to directly generate USRs. These
131 /// methods do not construct complete USRs (which incorporate the parents
132 /// of an AST element), but only the fragments concerning the AST element
133 /// itself.
134
135 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000136 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
137 StringRef CategoryContextExtSymbolDefinedIn) {
138 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
139 CategoryContextExtSymbolDefinedIn);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000140 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000141
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000142 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000143 void GenObjCCategory(StringRef cls, StringRef cat,
144 StringRef clsExt, StringRef catExt) {
145 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000146 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000147
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000148 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000149 void GenObjCProperty(StringRef prop, bool isClassProp) {
150 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000151 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000152
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000153 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000154 void GenObjCProtocol(StringRef prot, StringRef ext) {
155 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000156 }
157
158 void VisitType(QualType T);
159 void VisitTemplateParameterList(const TemplateParameterList *Params);
160 void VisitTemplateName(TemplateName Name);
161 void VisitTemplateArgument(const TemplateArgument &Arg);
162
163 /// Emit a Decl's name using NamedDecl::printName() and return true if
164 /// the decl had no name.
165 bool EmitDeclName(const NamedDecl *D);
166};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000167} // end anonymous namespace
168
169//===----------------------------------------------------------------------===//
170// Generating USRs from ASTS.
171//===----------------------------------------------------------------------===//
172
173bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000174 const unsigned startSize = Buf.size();
175 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000176 const unsigned endSize = Buf.size();
177 return startSize == endSize;
178}
179
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000180bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
181 if (D->isExternallyVisible())
182 return false;
183 if (D->getParentFunctionOrMethod())
184 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000185 SourceLocation Loc = D->getLocation();
186 if (Loc.isInvalid())
187 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000188 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000189 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000190}
191
192void USRGenerator::VisitDeclContext(const DeclContext *DC) {
193 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
194 Visit(D);
Sam McCall2e50ae62018-02-02 14:13:37 +0000195 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
196 VisitDeclContext(DC->getParent());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000197}
198
199void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
200 // The USR for an ivar declared in a class extension is based on the
201 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
202 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
203 Visit(ID);
204 else
205 VisitDeclContext(D->getDeclContext());
206 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
207 if (EmitDeclName(D)) {
208 // Bit fields can be anonymous.
209 IgnoreResults = true;
210 return;
211 }
212}
213
214void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000215 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000216 return;
217
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000218 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000219 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000220 if (Buf.size() == StartSize)
221 GenExtSymbolContainer(D);
222
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000223 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000224 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000225 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000226 Out << "@FT@";
227 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
228 } else
229 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000230
231 PrintingPolicy Policy(Context->getLangOpts());
232 // Forward references can have different template argument names. Suppress the
233 // template argument names in constructors to make their USR more stable.
234 Policy.SuppressTemplateArgsInCXXConstructors = true;
235 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000236
237 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000238 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
239 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000240 return;
241
242 if (const TemplateArgumentList *
243 SpecArgs = D->getTemplateSpecializationArgs()) {
244 Out << '<';
245 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
246 Out << '#';
247 VisitTemplateArgument(SpecArgs->get(I));
248 }
249 Out << '>';
250 }
251
252 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000253 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000254 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000255 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000256 }
257 if (D->isVariadic())
258 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000259 if (IsTemplate) {
260 // Function templates can be overloaded by return type, for example:
261 // \code
262 // template <class T> typename T::A foo() {}
263 // template <class T> typename T::B foo() {}
264 // \endcode
265 Out << '#';
266 VisitType(D->getReturnType());
267 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000268 Out << '#';
269 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
270 if (MD->isStatic())
271 Out << 'S';
272 if (unsigned quals = MD->getTypeQualifiers())
273 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000274 switch (MD->getRefQualifier()) {
275 case RQ_None: break;
276 case RQ_LValue: Out << '&'; break;
277 case RQ_RValue: Out << "&&"; break;
278 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000279 }
280}
281
282void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
283 VisitDeclContext(D->getDeclContext());
284 Out << "@";
285
286 if (EmitDeclName(D)) {
287 // The string can be empty if the declaration has no name; e.g., it is
288 // the ParmDecl with no name for declaration of a function pointer type,
289 // e.g.: void (*f)(void *);
290 // In this case, don't generate a USR.
291 IgnoreResults = true;
292 }
293}
294
295void USRGenerator::VisitVarDecl(const VarDecl *D) {
296 // VarDecls can be declared 'extern' within a function or method body,
297 // but their enclosing DeclContext is the function, not the TU. We need
298 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000299 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000300 return;
301
302 VisitDeclContext(D->getDeclContext());
303
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000304 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
305 Out << "@VT";
306 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
307 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
308 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
309 Out << "@VP";
310 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
311 }
312
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000313 // Variables always have simple names.
314 StringRef s = D->getName();
315
316 // The string can be empty if the declaration has no name; e.g., it is
317 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
318 // void (*f)(void *);
319 // In this case, don't generate a USR.
320 if (s.empty())
321 IgnoreResults = true;
322 else
323 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000324
325 // For a template specialization, mangle the template arguments.
326 if (const VarTemplateSpecializationDecl *Spec
327 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000328 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000329 Out << '>';
330 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
331 Out << '#';
332 VisitTemplateArgument(Args.get(I));
333 }
334 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000335}
336
337void USRGenerator::VisitNonTypeTemplateParmDecl(
338 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000339 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000340}
341
342void USRGenerator::VisitTemplateTemplateParmDecl(
343 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000344 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000345}
346
347void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
348 if (D->isAnonymousNamespace()) {
349 Out << "@aN";
350 return;
351 }
352
353 VisitDeclContext(D->getDeclContext());
354 if (!IgnoreResults)
355 Out << "@N@" << D->getName();
356}
357
358void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
359 VisitFunctionDecl(D->getTemplatedDecl());
360}
361
362void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
363 VisitTagDecl(D->getTemplatedDecl());
364}
365
366void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
367 VisitDeclContext(D->getDeclContext());
368 if (!IgnoreResults)
369 Out << "@NA@" << D->getName();
370}
371
372void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
373 const DeclContext *container = D->getDeclContext();
374 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
375 Visit(pd);
376 }
377 else {
378 // The USR for a method declared in a class extension or category is based on
379 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
380 const ObjCInterfaceDecl *ID = D->getClassInterface();
381 if (!ID) {
382 IgnoreResults = true;
383 return;
384 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000385 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
386 const ObjCCategoryDecl * {
387 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
388 return CD;
389 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
390 return ICD->getCategoryDecl();
391 return nullptr;
392 };
393 auto *CD = getCategoryContext(D);
394 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000395 }
396 // Ideally we would use 'GenObjCMethod', but this is such a hot path
397 // for Objective-C code that we don't want to use
398 // DeclarationName::getAsString().
399 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
400 << DeclarationName(D->getSelector());
401}
402
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000403void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
404 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000405 switch (D->getKind()) {
406 default:
407 llvm_unreachable("Invalid ObjC container.");
408 case Decl::ObjCInterface:
409 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000410 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
411 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000412 break;
413 case Decl::ObjCCategory: {
414 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
415 const ObjCInterfaceDecl *ID = CD->getClassInterface();
416 if (!ID) {
417 // Handle invalid code where the @interface might not
418 // have been specified.
419 // FIXME: We should be able to generate this USR even if the
420 // @interface isn't available.
421 IgnoreResults = true;
422 return;
423 }
424 // Specially handle class extensions, which are anonymous categories.
425 // We want to mangle in the location to uniquely distinguish them.
426 if (CD->IsClassExtension()) {
427 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000428 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000429 }
430 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000431 GenObjCCategory(ID->getName(), CD->getName(),
432 GetExternalSourceContainer(ID),
433 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000434
435 break;
436 }
437 case Decl::ObjCCategoryImpl: {
438 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
439 const ObjCInterfaceDecl *ID = CD->getClassInterface();
440 if (!ID) {
441 // Handle invalid code where the @interface might not
442 // have been specified.
443 // FIXME: We should be able to generate this USR even if the
444 // @interface isn't available.
445 IgnoreResults = true;
446 return;
447 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000448 GenObjCCategory(ID->getName(), CD->getName(),
449 GetExternalSourceContainer(ID),
450 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000451 break;
452 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000453 case Decl::ObjCProtocol: {
454 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
455 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000456 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000457 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000458 }
459}
460
461void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
462 // The USR for a property declared in a class extension or category is based
463 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
464 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
465 Visit(ID);
466 else
467 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000468 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000469}
470
471void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
472 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
473 VisitObjCPropertyDecl(PD);
474 return;
475 }
476
477 IgnoreResults = true;
478}
479
480void USRGenerator::VisitTagDecl(const TagDecl *D) {
481 // Add the location of the tag decl to handle resolution across
482 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000483 if (!isa<EnumDecl>(D) &&
484 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000485 return;
486
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000487 GenExtSymbolContainer(D);
488
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000489 D = D->getCanonicalDecl();
490 VisitDeclContext(D->getDeclContext());
491
492 bool AlreadyStarted = false;
493 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
494 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
495 AlreadyStarted = true;
496
497 switch (D->getTagKind()) {
498 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000499 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000500 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000501 case TTK_Union: Out << "@UT"; break;
502 case TTK_Enum: llvm_unreachable("enum template");
503 }
504 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
505 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
506 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
507 AlreadyStarted = true;
508
509 switch (D->getTagKind()) {
510 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000511 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000512 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000513 case TTK_Union: Out << "@UP"; break;
514 case TTK_Enum: llvm_unreachable("enum partial specialization");
515 }
516 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
517 }
518 }
519
520 if (!AlreadyStarted) {
521 switch (D->getTagKind()) {
522 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000523 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000524 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000525 case TTK_Union: Out << "@U"; break;
526 case TTK_Enum: Out << "@E"; break;
527 }
528 }
529
530 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000531 assert(Buf.size() > 0);
532 const unsigned off = Buf.size() - 1;
533
534 if (EmitDeclName(D)) {
535 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
536 Buf[off] = 'A';
537 Out << '@' << *TD;
538 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000539 else {
540 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
541 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000542 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000543 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000544 if (auto *ED = dyn_cast<EnumDecl>(D)) {
545 // Distinguish USRs of anonymous enums by using their first enumerator.
546 auto enum_range = ED->enumerators();
547 if (enum_range.begin() != enum_range.end()) {
548 Out << '@' << **enum_range.begin();
549 }
550 }
551 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000552 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000553 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000554
555 // For a class template specialization, mangle the template arguments.
556 if (const ClassTemplateSpecializationDecl *Spec
557 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000558 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000559 Out << '>';
560 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
561 Out << '#';
562 VisitTemplateArgument(Args.get(I));
563 }
564 }
565}
566
567void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000568 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000569 return;
570 const DeclContext *DC = D->getDeclContext();
571 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
572 Visit(DCN);
573 Out << "@T@";
574 Out << D->getName();
575}
576
577void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000578 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000579}
580
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000581void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
582 StringRef Container = GetExternalSourceContainer(D);
583 if (!Container.empty())
584 Out << "@M@" << Container;
585}
586
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000587bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000588 if (generatedLoc)
589 return IgnoreResults;
590 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000591
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000592 // Guard against null declarations in invalid code.
593 if (!D) {
594 IgnoreResults = true;
595 return true;
596 }
597
598 // Use the location of canonical decl.
599 D = D->getCanonicalDecl();
600
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000601 IgnoreResults =
602 IgnoreResults || printLoc(Out, D->getLocStart(),
603 Context->getSourceManager(), IncludeOffset);
604
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000605 return IgnoreResults;
606}
607
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000608static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
609 // FIXME: Encode the qualifier, don't just print it.
610 PrintingPolicy PO(Ctx.getLangOpts());
611 PO.SuppressTagKeyword = true;
612 PO.SuppressUnwrittenScope = true;
613 PO.ConstantArraySizeAsWritten = false;
614 PO.AnonymousTagLocations = false;
615 NNS->print(Out, PO);
616}
617
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000618void USRGenerator::VisitType(QualType T) {
619 // This method mangles in USR information for types. It can possibly
620 // just reuse the naming-mangling logic used by codegen, although the
621 // requirements for USRs might not be the same.
622 ASTContext &Ctx = *Context;
623
624 do {
625 T = Ctx.getCanonicalType(T);
626 Qualifiers Q = T.getQualifiers();
627 unsigned qVal = 0;
628 if (Q.hasConst())
629 qVal |= 0x1;
630 if (Q.hasVolatile())
631 qVal |= 0x2;
632 if (Q.hasRestrict())
633 qVal |= 0x4;
634 if(qVal)
635 Out << ((char) ('0' + qVal));
636
637 // Mangle in ObjC GC qualifiers?
638
639 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
640 Out << 'P';
641 T = Expansion->getPattern();
642 }
643
644 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
645 unsigned char c = '\0';
646 switch (BT->getKind()) {
647 case BuiltinType::Void:
648 c = 'v'; break;
649 case BuiltinType::Bool:
650 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000651 case BuiltinType::UChar:
652 c = 'c'; break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000653 case BuiltinType::Char8:
654 c = 'u'; break; // FIXME: Check this doesn't collide
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000655 case BuiltinType::Char16:
656 c = 'q'; break;
657 case BuiltinType::Char32:
658 c = 'w'; break;
659 case BuiltinType::UShort:
660 c = 's'; break;
661 case BuiltinType::UInt:
662 c = 'i'; break;
663 case BuiltinType::ULong:
664 c = 'l'; break;
665 case BuiltinType::ULongLong:
666 c = 'k'; break;
667 case BuiltinType::UInt128:
668 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000669 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000670 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000671 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000672 case BuiltinType::SChar:
673 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000674 case BuiltinType::WChar_S:
675 case BuiltinType::WChar_U:
676 c = 'W'; break;
677 case BuiltinType::Short:
678 c = 'S'; break;
679 case BuiltinType::Int:
680 c = 'I'; break;
681 case BuiltinType::Long:
682 c = 'L'; break;
683 case BuiltinType::LongLong:
684 c = 'K'; break;
685 case BuiltinType::Int128:
686 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000687 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000688 case BuiltinType::Half:
689 c = 'h'; break;
690 case BuiltinType::Float:
691 c = 'f'; break;
692 case BuiltinType::Double:
693 c = 'd'; break;
694 case BuiltinType::LongDouble:
695 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000696 case BuiltinType::Float128:
697 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000698 case BuiltinType::NullPtr:
699 c = 'n'; break;
700#define BUILTIN_TYPE(Id, SingletonId)
701#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
702#include "clang/AST/BuiltinTypes.def"
703 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000704#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
705 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000706#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000707 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000708 case BuiltinType::OCLClkEvent:
709 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000710 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000711 case BuiltinType::OCLSampler:
Leonard Chanf921d852018-06-04 16:07:52 +0000712 case BuiltinType::ShortAccum:
713 case BuiltinType::Accum:
714 case BuiltinType::LongAccum:
715 case BuiltinType::UShortAccum:
716 case BuiltinType::UAccum:
717 case BuiltinType::ULongAccum:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000718 IgnoreResults = true;
719 return;
720 case BuiltinType::ObjCId:
721 c = 'o'; break;
722 case BuiltinType::ObjCClass:
723 c = 'O'; break;
724 case BuiltinType::ObjCSel:
725 c = 'e'; break;
726 }
727 Out << c;
728 return;
729 }
730
731 // If we have already seen this (non-built-in) type, use a substitution
732 // encoding.
733 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
734 = TypeSubstitutions.find(T.getTypePtr());
735 if (Substitution != TypeSubstitutions.end()) {
736 Out << 'S' << Substitution->second << '_';
737 return;
738 } else {
739 // Record this as a substitution.
740 unsigned Number = TypeSubstitutions.size();
741 TypeSubstitutions[T.getTypePtr()] = Number;
742 }
743
744 if (const PointerType *PT = T->getAs<PointerType>()) {
745 Out << '*';
746 T = PT->getPointeeType();
747 continue;
748 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000749 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
750 Out << '*';
751 T = OPT->getPointeeType();
752 continue;
753 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000754 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
755 Out << "&&";
756 T = RT->getPointeeType();
757 continue;
758 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000759 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
760 Out << '&';
761 T = RT->getPointeeType();
762 continue;
763 }
764 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
765 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000766 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000767 Out << '(';
768 for (const auto &I : FT->param_types()) {
769 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000770 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000771 }
772 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000773 if (FT->isVariadic())
774 Out << '.';
775 return;
776 }
777 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
778 Out << 'B';
779 T = BT->getPointeeType();
780 continue;
781 }
782 if (const ComplexType *CT = T->getAs<ComplexType>()) {
783 Out << '<';
784 T = CT->getElementType();
785 continue;
786 }
787 if (const TagType *TT = T->getAs<TagType>()) {
788 Out << '$';
789 VisitTagDecl(TT->getDecl());
790 return;
791 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000792 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
793 Out << '$';
794 VisitObjCInterfaceDecl(OIT->getDecl());
795 return;
796 }
797 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
798 Out << 'Q';
799 VisitType(OIT->getBaseType());
800 for (auto *Prot : OIT->getProtocols())
801 VisitObjCProtocolDecl(Prot);
802 return;
803 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000804 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
805 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
806 return;
807 }
808 if (const TemplateSpecializationType *Spec
809 = T->getAs<TemplateSpecializationType>()) {
810 Out << '>';
811 VisitTemplateName(Spec->getTemplateName());
812 Out << Spec->getNumArgs();
813 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
814 VisitTemplateArgument(Spec->getArg(I));
815 return;
816 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000817 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
818 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000819 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000820 Out << ':' << DNT->getIdentifier()->getName();
821 return;
822 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000823 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
824 T = InjT->getInjectedSpecializationType();
825 continue;
826 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000827 if (const auto *VT = T->getAs<VectorType>()) {
828 Out << (T->isExtVectorType() ? ']' : '[');
829 Out << VT->getNumElements();
830 T = VT->getElementType();
831 continue;
832 }
Jan Korous663ba152017-10-09 19:51:33 +0000833 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
834 Out << '{';
835 switch (AT->getSizeModifier()) {
836 case ArrayType::Static:
837 Out << 's';
838 break;
839 case ArrayType::Star:
840 Out << '*';
841 break;
842 case ArrayType::Normal:
843 Out << 'n';
844 break;
845 }
846 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
847 Out << CAT->getSize();
848
849 T = AT->getElementType();
850 continue;
851 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000852
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000853 // Unhandled type.
854 Out << ' ';
855 break;
856 } while (true);
857}
858
859void USRGenerator::VisitTemplateParameterList(
860 const TemplateParameterList *Params) {
861 if (!Params)
862 return;
863 Out << '>' << Params->size();
864 for (TemplateParameterList::const_iterator P = Params->begin(),
865 PEnd = Params->end();
866 P != PEnd; ++P) {
867 Out << '#';
868 if (isa<TemplateTypeParmDecl>(*P)) {
869 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
870 Out<< 'p';
871 Out << 'T';
872 continue;
873 }
874
875 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
876 if (NTTP->isParameterPack())
877 Out << 'p';
878 Out << 'N';
879 VisitType(NTTP->getType());
880 continue;
881 }
882
883 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
884 if (TTP->isParameterPack())
885 Out << 'p';
886 Out << 't';
887 VisitTemplateParameterList(TTP->getTemplateParameters());
888 }
889}
890
891void USRGenerator::VisitTemplateName(TemplateName Name) {
892 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
893 if (TemplateTemplateParmDecl *TTP
894 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
895 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
896 return;
897 }
898
899 Visit(Template);
900 return;
901 }
902
903 // FIXME: Visit dependent template names.
904}
905
906void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
907 switch (Arg.getKind()) {
908 case TemplateArgument::Null:
909 break;
910
911 case TemplateArgument::Declaration:
912 Visit(Arg.getAsDecl());
913 break;
914
915 case TemplateArgument::NullPtr:
916 break;
917
918 case TemplateArgument::TemplateExpansion:
919 Out << 'P'; // pack expansion of...
920 // Fall through
921 case TemplateArgument::Template:
922 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
923 break;
924
925 case TemplateArgument::Expression:
926 // FIXME: Visit expressions.
927 break;
928
929 case TemplateArgument::Pack:
930 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000931 for (const auto &P : Arg.pack_elements())
932 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000933 break;
934
935 case TemplateArgument::Type:
936 VisitType(Arg.getAsType());
937 break;
938
939 case TemplateArgument::Integral:
940 Out << 'V';
941 VisitType(Arg.getIntegralType());
942 Out << Arg.getAsIntegral();
943 break;
944 }
945}
946
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000947void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
948 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
949 return;
950 VisitDeclContext(D->getDeclContext());
951 Out << "@UUV@";
952 printQualifier(Out, D->getASTContext(), D->getQualifier());
953 EmitDeclName(D);
954}
955
956void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
957 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
958 return;
959 VisitDeclContext(D->getDeclContext());
960 Out << "@UUT@";
961 printQualifier(Out, D->getASTContext(), D->getQualifier());
962 Out << D->getName(); // Simple name.
963}
964
965
966
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000967//===----------------------------------------------------------------------===//
968// USR generation functions.
969//===----------------------------------------------------------------------===//
970
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000971static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
972 StringRef CatSymDefinedIn,
973 raw_ostream &OS) {
974 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
975 return;
976 if (CatSymDefinedIn.empty()) {
977 OS << "@M@" << ClsSymDefinedIn << '@';
978 return;
979 }
980 OS << "@CM@" << CatSymDefinedIn << '@';
981 if (ClsSymDefinedIn != CatSymDefinedIn) {
982 OS << ClsSymDefinedIn << '@';
983 }
984}
985
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000986void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000987 StringRef ExtSymDefinedIn,
988 StringRef CategoryContextExtSymbolDefinedIn) {
989 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
990 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000991 OS << "objc(cs)" << Cls;
992}
993
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000994void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000995 raw_ostream &OS,
996 StringRef ClsSymDefinedIn,
997 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000998 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000999 OS << "objc(cy)" << Cls << '@' << Cat;
1000}
1001
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001002void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001003 OS << '@' << Ivar;
1004}
1005
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001006void clang::index::generateUSRForObjCMethod(StringRef Sel,
1007 bool IsInstanceMethod,
1008 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001009 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1010}
1011
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001012void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1013 raw_ostream &OS) {
1014 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001015}
1016
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001017void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1018 StringRef ExtSymDefinedIn) {
1019 if (!ExtSymDefinedIn.empty())
1020 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001021 OS << "objc(pl)" << Prot;
1022}
1023
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001024void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1025 StringRef ExtSymDefinedIn) {
1026 if (!ExtSymDefinedIn.empty())
1027 OS << "@M@" << ExtSymDefinedIn;
1028 OS << "@E@" << EnumName;
1029}
1030
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001031void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1032 raw_ostream &OS) {
1033 OS << '@' << EnumConstantName;
1034}
1035
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001036bool clang::index::generateUSRForDecl(const Decl *D,
1037 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001038 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001039 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001040 // We don't ignore decls with invalid source locations. Implicit decls, like
1041 // C++'s operator new function, can have invalid locations but it is fine to
1042 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001043
1044 USRGenerator UG(&D->getASTContext(), Buf);
1045 UG.Visit(D);
1046 return UG.ignoreResults();
1047}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001048
Richard Smith66a81862015-05-04 02:25:31 +00001049bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001050 const SourceManager &SM,
1051 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001052 if (!MD)
1053 return true;
1054 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1055 SM, Buf);
1056
1057}
1058
1059bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1060 const SourceManager &SM,
1061 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001062 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001063 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001064 return true;
1065
1066 llvm::raw_svector_ostream Out(Buf);
1067
1068 // Assume that system headers are sane. Don't put source location
1069 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001070 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1071
1072 Out << getUSRSpacePrefix();
1073 if (ShouldGenerateLocation)
1074 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1075 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001076 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001077 return false;
1078}