clang 24.0.0git
USRGeneration.cpp
Go to the documentation of this file.
1//===- USRGeneration.cpp - Routines for USR generation --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "clang/AST/Attr.h"
12#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ODRHash.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace clang;
21using namespace clang::index;
22
23//===----------------------------------------------------------------------===//
24// USR generation.
25//===----------------------------------------------------------------------===//
26
27/// Print only the offset part of \p Loc
28/// \returns true on error.
29static bool printLocOffset(llvm::raw_ostream &OS, SourceLocation Loc,
30 const SourceManager &SM) {
31 if (Loc.isInvalid())
32 return true;
33 if (SM.getExpansionLoc(Loc) == SM.getSpellingLoc(Loc)) {
34 // Use the offest into the FileID to represent the location. Using
35 // a line/column can cause us to look back at the original source file,
36 // which is expensive.
37 OS << '@' << SM.getDecomposedLoc(Loc).second;
38 return false;
39 }
40 // In case expansion and spelling locations differ, we need both of them to
41 // make the USR distinguishable:
42 OS << '@' << SM.getDecomposedLoc(SM.getExpansionLoc(Loc)).second;
43 OS << '@' << SM.getDecomposedLoc(SM.getSpellingLoc(Loc)).second;
44 return false;
45}
46
47/// Print \p Loc including both the file and offset, if \p IncludeOffset is
48/// true. Print only the file of \p Loc otherwise.
49/// \returns true on error.
50static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
51 const SourceManager &SM, bool IncludeOffset) {
52 if (Loc.isInvalid()) {
53 return true;
54 }
55 Loc = SM.getExpansionLoc(Loc);
56 const FileIDAndOffset &Decomposed = SM.getDecomposedLoc(Loc);
57 OptionalFileEntryRef FE = SM.getFileEntryRefForID(Decomposed.first);
58 if (FE) {
59 OS << llvm::sys::path::filename(FE->getName());
60 } else {
61 // This case really isn't interesting.
62 return true;
63 }
64 if (IncludeOffset) {
65 // Use the offest into the FileID to represent the location. Using
66 // a line/column can cause us to look back at the original source file,
67 // which is expensive.
68 printLocOffset(OS, Loc, SM);
69 }
70 return false;
71}
72
73static StringRef GetExternalSourceContainer(const NamedDecl *D) {
74 if (!D)
75 return StringRef();
76 if (auto *attr = D->getExternalSourceSymbolAttr()) {
77 return attr->getDefinedIn();
78 }
79 return StringRef();
80}
81
82namespace {
83class USRGenerator : public ConstDeclVisitor<USRGenerator> {
84 SmallVectorImpl<char> &Buf;
85 llvm::raw_svector_ostream Out;
86 ASTContext *Context;
87 const LangOptions &LangOpts;
88 bool IgnoreResults = false;
89 // The flag below ensures that, when a source location needs to be printed,
90 // the filename is printed at most once during the recursive visit. The
91 // offset part may be printed multiple times, since sub-decls along the
92 // visit may each need a distinct location to disambiguate them.
93 bool GeneratedFilename = false;
94
95 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
96
97public:
98 USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf,
99 const LangOptions &LangOpts)
100 : Buf(Buf), Out(Buf), Context(Ctx), LangOpts(LangOpts) {
101 // Add the USR space prefix.
102 Out << getUSRSpacePrefix();
103 }
104
105 bool ignoreResults() const { return IgnoreResults; }
106
107 // Visitation methods from generating USRs from AST elements.
108 void VisitDeclContext(const DeclContext *D);
109 void VisitFieldDecl(const FieldDecl *D);
110 void VisitFunctionDecl(const FunctionDecl *D);
111 void VisitNamedDecl(const NamedDecl *D);
112 void VisitNamespaceDecl(const NamespaceDecl *D);
113 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
114 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
115 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
116 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
117 const ObjCCategoryDecl *CatD = nullptr);
118 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
119 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
120 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
121 void VisitTagDecl(const TagDecl *D);
122 void VisitTypedefDecl(const TypedefDecl *D);
123 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
124 void VisitVarDecl(const VarDecl *D);
125 void VisitBindingDecl(const BindingDecl *D);
126 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
127 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
128 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
129 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
130 void VisitConceptDecl(const ConceptDecl *D);
131
132 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
133 IgnoreResults = true; // No USRs for linkage specs themselves.
134 }
135
136 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
137 IgnoreResults = true;
138 }
139
140 void VisitUsingDecl(const UsingDecl *D) {
141 VisitDeclContext(D->getDeclContext());
142 Out << "@UD@";
143
144 bool EmittedDeclName = !EmitDeclName(D);
145 assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
146 (void)EmittedDeclName;
147 }
148
149 bool ShouldGenerateLocation(const NamedDecl *D);
150
151 bool isLocal(const NamedDecl *D) {
152 return D->getParentFunctionOrMethod() != nullptr;
153 }
154
155 void GenExtSymbolContainer(const NamedDecl *D);
156
157 /// Generate the string component containing the location of the
158 /// declaration.
159 bool GenLoc(const Decl *D, bool IncludeOffset);
160
161 /// String generation methods used both by the visitation methods
162 /// and from other clients that want to directly generate USRs. These
163 /// methods do not construct complete USRs (which incorporate the parents
164 /// of an AST element), but only the fragments concerning the AST element
165 /// itself.
166
167 /// Generate a USR for an Objective-C class.
168 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
169 StringRef CategoryContextExtSymbolDefinedIn) {
170 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
171 CategoryContextExtSymbolDefinedIn);
172 }
173
174 /// Generate a USR for an Objective-C class category.
175 void GenObjCCategory(StringRef cls, StringRef cat, StringRef clsExt,
176 StringRef catExt) {
177 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
178 }
179
180 /// Generate a USR fragment for an Objective-C property.
181 void GenObjCProperty(StringRef prop, bool isClassProp) {
182 generateUSRForObjCProperty(prop, isClassProp, Out);
183 }
184
185 /// Generate a USR for an Objective-C protocol.
186 void GenObjCProtocol(StringRef prot, StringRef ext) {
187 generateUSRForObjCProtocol(prot, Out, ext);
188 }
189
190 void VisitType(QualType T);
191 void VisitTemplateParameterList(const TemplateParameterList *Params);
192 void VisitTemplateName(TemplateName Name);
193 void VisitTemplateArgument(const TemplateArgument &Arg);
194
195 void VisitMSGuidDecl(const MSGuidDecl *D);
196 void VisitTemplateParamObjectDecl(const TemplateParamObjectDecl *D);
197
198 /// Emit a Decl's name using NamedDecl::printName() and return true if
199 /// the decl had no name.
200 bool EmitDeclName(const NamedDecl *D);
201};
202} // end anonymous namespace
203
204//===----------------------------------------------------------------------===//
205// Generating USRs from ASTS.
206//===----------------------------------------------------------------------===//
207
208bool USRGenerator::EmitDeclName(const NamedDecl *D) {
209 DeclarationName N = D->getDeclName();
210 if (N.isEmpty())
211 return true;
212 Out << N;
213 return false;
214}
215
216bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
217 if (D->isExternallyVisible())
218 return false;
220 return true;
221 SourceLocation Loc = D->getLocation();
222 if (Loc.isInvalid())
223 return false;
224 const SourceManager &SM = Context->getSourceManager();
225 return !SM.isInSystemHeader(Loc);
226}
227
228void USRGenerator::VisitDeclContext(const DeclContext *DC) {
229 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
230 Visit(D);
231 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
232 VisitDeclContext(DC->getParent());
233}
234
235void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
236 // The USR for an ivar declared in a class extension is based on the
237 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
238 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
239 Visit(ID);
240 else
241 VisitDeclContext(D->getDeclContext());
242 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
243 if (EmitDeclName(D)) {
244 // Bit fields can be anonymous.
245 IgnoreResults = true;
246 return;
247 }
248}
249
250void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
251 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
252 return;
253
254 if (D->getType().isNull()) {
255 IgnoreResults = true;
256 return;
257 }
258
259 const unsigned StartSize = Buf.size();
260 VisitDeclContext(D->getDeclContext());
261 if (Buf.size() == StartSize)
262 GenExtSymbolContainer(D);
263
264 bool IsTemplate = false;
265 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
266 IsTemplate = true;
267 Out << "@FT@";
268 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
269 } else
270 Out << "@F@";
271
272 PrintingPolicy Policy(LangOpts);
273 // Forward references can have different template argument names. Suppress the
274 // template argument names in constructors to make their USR more stable.
275 Policy.SuppressTemplateArgsInCXXConstructors = true;
276 D->getDeclName().print(Out, Policy);
277
278 if ((!LangOpts.CPlusPlus || D->isExternC()) &&
279 !D->hasAttr<OverloadableAttr>())
280 return;
281
283 Out << '<';
284 if (const TemplateArgumentList *SpecArgs =
286 for (const auto &Arg : SpecArgs->asArray()) {
287 Out << '#';
288 VisitTemplateArgument(Arg);
289 }
290 } else if (const ASTTemplateArgumentListInfo *SpecArgsWritten =
292 for (const auto &ArgLoc : SpecArgsWritten->arguments()) {
293 Out << '#';
294 VisitTemplateArgument(ArgLoc.getArgument());
295 }
296 }
297 Out << '>';
298 }
299
300 QualType CanonicalType = D->getType().getCanonicalType();
301 // Mangle in type information for the arguments.
302 if (const auto *FPT = CanonicalType->getAs<FunctionProtoType>()) {
303 for (QualType PT : FPT->param_types()) {
304 Out << '#';
305 VisitType(PT);
306 }
307 }
308 if (D->isVariadic())
309 Out << '.';
310 if (IsTemplate) {
311 // Function templates can be overloaded by return type, for example:
312 // \code
313 // template <class T> typename T::A foo() {}
314 // template <class T> typename T::B foo() {}
315 // \endcode
316 Out << '#';
317 VisitType(D->getReturnType());
318 }
319 Out << '#';
320 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
321 if (MD->isStatic())
322 Out << 'S';
323 // FIXME: OpenCL: Need to consider address spaces
324 if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers())
325 Out << (char)('0' + quals);
326 switch (MD->getRefQualifier()) {
327 case RQ_None:
328 break;
329 case RQ_LValue:
330 Out << '&';
331 break;
332 case RQ_RValue:
333 Out << "&&";
334 break;
335 }
336 }
337}
338
339void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
340 VisitDeclContext(D->getDeclContext());
341 Out << "@";
342
343 if (EmitDeclName(D)) {
344 // The string can be empty if the declaration has no name; e.g., it is
345 // the ParmDecl with no name for declaration of a function pointer type,
346 // e.g.: void (*f)(void *);
347 // In this case, don't generate a USR.
348 IgnoreResults = true;
349 }
350}
351
352void USRGenerator::VisitVarDecl(const VarDecl *D) {
353 // VarDecls can be declared 'extern' within a function or method body,
354 // but their enclosing DeclContext is the function, not the TU. We need
355 // to check the storage class to correctly generate the USR.
356 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
357 return;
358
359 VisitDeclContext(D->getDeclContext());
360
361 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
362 Out << "@VT";
363 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
364 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec =
365 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
366 Out << "@VP";
367 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
368 }
369
370 // Variables always have simple names.
371 StringRef s = D->getName();
372
373 // The string can be empty if the declaration has no name; e.g., it is
374 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
375 // void (*f)(void *);
376 // In this case, don't generate a USR.
377 if (s.empty())
378 IgnoreResults = true;
379 else
380 Out << '@' << s;
381
382 // For a template specialization, mangle the template arguments.
383 if (const VarTemplateSpecializationDecl *Spec =
384 dyn_cast<VarTemplateSpecializationDecl>(D)) {
385 const TemplateArgumentList &Args = Spec->getTemplateArgs();
386 Out << '>';
387 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
388 Out << '#';
389 VisitTemplateArgument(Args.get(I));
390 }
391 }
392}
393
394void USRGenerator::VisitBindingDecl(const BindingDecl *D) {
395 if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true))
396 return;
397 VisitNamedDecl(D);
398}
399
400void USRGenerator::VisitNonTypeTemplateParmDecl(
401 const NonTypeTemplateParmDecl *D) {
402 GenLoc(D, /*IncludeOffset=*/true);
403}
404
405void USRGenerator::VisitTemplateTemplateParmDecl(
406 const TemplateTemplateParmDecl *D) {
407 GenLoc(D, /*IncludeOffset=*/true);
408}
409
410void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
411 if (IgnoreResults)
412 return;
413 VisitDeclContext(D->getDeclContext());
414 if (D->isAnonymousNamespace()) {
415 Out << "@aN";
416 return;
417 }
418 Out << "@N@" << D->getName();
419}
420
421void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
422 VisitFunctionDecl(D->getTemplatedDecl());
423}
424
425void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
426 VisitTagDecl(D->getTemplatedDecl());
427}
428
429void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
430 VisitDeclContext(D->getDeclContext());
431 if (!IgnoreResults)
432 Out << "@NA@" << D->getName();
433}
434
436 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
437 return CD;
438 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
439 return ICD->getCategoryDecl();
440 return nullptr;
441}
442
443void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
444 const DeclContext *container = D->getDeclContext();
445 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
446 Visit(pd);
447 } else {
448 // The USR for a method declared in a class extension or category is based
449 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
450 const ObjCInterfaceDecl *ID = D->getClassInterface();
451 if (!ID) {
452 IgnoreResults = true;
453 return;
454 }
455 auto *CD = getCategoryContext(D);
456 VisitObjCContainerDecl(ID, CD);
457 }
458 // Ideally we would use 'GenObjCMethod', but this is such a hot path
459 // for Objective-C code that we don't want to use
460 // DeclarationName::getAsString().
461 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
462 << DeclarationName(D->getSelector());
463}
464
465void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
466 const ObjCCategoryDecl *CatD) {
467 switch (D->getKind()) {
468 default:
469 llvm_unreachable("Invalid ObjC container.");
470 case Decl::ObjCInterface:
471 case Decl::ObjCImplementation:
472 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
474 break;
475 case Decl::ObjCCategory: {
476 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
477 const ObjCInterfaceDecl *ID = CD->getClassInterface();
478 if (!ID) {
479 // Handle invalid code where the @interface might not
480 // have been specified.
481 // FIXME: We should be able to generate this USR even if the
482 // @interface isn't available.
483 IgnoreResults = true;
484 return;
485 }
486 // Specially handle class extensions, which are anonymous categories.
487 // We want to mangle in the location to uniquely distinguish them.
488 if (CD->IsClassExtension()) {
489 Out << "objc(ext)" << ID->getName() << '@';
490 GenLoc(CD, /*IncludeOffset=*/true);
491 } else
492 GenObjCCategory(ID->getName(), CD->getName(),
495
496 break;
497 }
498 case Decl::ObjCCategoryImpl: {
499 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
500 const ObjCInterfaceDecl *ID = CD->getClassInterface();
501 if (!ID) {
502 // Handle invalid code where the @interface might not
503 // have been specified.
504 // FIXME: We should be able to generate this USR even if the
505 // @interface isn't available.
506 IgnoreResults = true;
507 return;
508 }
509 GenObjCCategory(ID->getName(), CD->getName(),
512 break;
513 }
514 case Decl::ObjCProtocol: {
515 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
516 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
517 break;
518 }
519 }
520}
521
522void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
523 // The USR for a property declared in a class extension or category is based
524 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
525 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
526 VisitObjCContainerDecl(ID, getCategoryContext(D));
527 else
528 Visit(cast<Decl>(D->getDeclContext()));
529 GenObjCProperty(D->getName(), D->isClassProperty());
530}
531
532void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
533 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
534 VisitObjCPropertyDecl(PD);
535 return;
536 }
537
538 IgnoreResults = true;
539}
540
541void USRGenerator::VisitTagDecl(const TagDecl *D) {
542 // Add the location of the tag decl to handle resolution across
543 // translation units.
544 if (!isa<EnumDecl>(D) && ShouldGenerateLocation(D) &&
545 GenLoc(D, /*IncludeOffset=*/isLocal(D)))
546 return;
547
548 GenExtSymbolContainer(D);
549
550 D = D->getCanonicalDecl();
551 VisitDeclContext(D->getDeclContext());
552
553 bool AlreadyStarted = false;
554 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
555 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
556 AlreadyStarted = true;
557
558 switch (D->getTagKind()) {
559 case TagTypeKind::Interface:
560 case TagTypeKind::Class:
561 case TagTypeKind::Struct:
562 Out << "@ST";
563 break;
564 case TagTypeKind::Union:
565 Out << "@UT";
566 break;
567 case TagTypeKind::Enum:
568 llvm_unreachable("enum template");
569 }
570 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
571 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec =
572 dyn_cast<ClassTemplatePartialSpecializationDecl>(
573 CXXRecord)) {
574 AlreadyStarted = true;
575
576 switch (D->getTagKind()) {
577 case TagTypeKind::Interface:
578 case TagTypeKind::Class:
579 case TagTypeKind::Struct:
580 Out << "@SP";
581 break;
582 case TagTypeKind::Union:
583 Out << "@UP";
584 break;
585 case TagTypeKind::Enum:
586 llvm_unreachable("enum partial specialization");
587 }
588 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
589 }
590 }
591
592 if (!AlreadyStarted) {
593 switch (D->getTagKind()) {
594 case TagTypeKind::Interface:
595 case TagTypeKind::Class:
596 case TagTypeKind::Struct:
597 Out << "@S";
598 break;
599 case TagTypeKind::Union:
600 Out << "@U";
601 break;
602 case TagTypeKind::Enum:
603 Out << "@E";
604 break;
605 }
606 }
607
608 Out << '@';
609 assert(Buf.size() > 0);
610 const unsigned off = Buf.size() - 1;
611
612 if (EmitDeclName(D)) {
613 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
614 Buf[off] = 'A';
615 Out << '@' << *TD;
616 } else {
617 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
618 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
619 } else {
620 Buf[off] = 'a';
621 if (auto *ED = dyn_cast<EnumDecl>(D)) {
622 // Distinguish USRs of anonymous enums by using their first
623 // enumerator.
624 auto enum_range = ED->enumerators();
625 if (enum_range.begin() != enum_range.end()) {
626 Out << '@' << **enum_range.begin();
627 }
628 }
629 }
630 }
631 }
632
633 // For a class template specialization, mangle the template arguments.
634 if (const ClassTemplateSpecializationDecl *Spec =
635 dyn_cast<ClassTemplateSpecializationDecl>(D)) {
636 const TemplateArgumentList &Args = Spec->getTemplateArgs();
637 Out << '>';
638 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
639 Out << '#';
640 VisitTemplateArgument(Args.get(I));
641 }
642 }
643}
644
645void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
646 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
647 return;
648 const DeclContext *DC = D->getDeclContext();
649 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
650 Visit(DCN);
651 Out << "@T@";
652 Out << D->getName();
653}
654
655void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
656 GenLoc(D, /*IncludeOffset=*/true);
657}
658
659void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
660 StringRef Container = GetExternalSourceContainer(D);
661 if (!Container.empty())
662 Out << "@M@" << Container;
663}
664
665bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
666 // Guard against null declarations in invalid code.
667 if (!D) {
668 IgnoreResults = true;
669 return true;
670 }
671 // Do nothing if no need to print the offset nor the filename:
672 if (!IncludeOffset && GeneratedFilename)
673 return IgnoreResults;
674
675 bool PrintErr = false;
676
677 // Use the location of canonical decl.
678 D = D->getCanonicalDecl();
679 if (!GeneratedFilename) {
680 GeneratedFilename = true;
681 PrintErr = printLoc(Out, D->getBeginLoc(), Context->getSourceManager(),
682 IncludeOffset);
683 } else {
684 PrintErr =
685 printLocOffset(Out, D->getBeginLoc(), Context->getSourceManager());
686 }
687 IgnoreResults = IgnoreResults || PrintErr;
688 return IgnoreResults;
689}
690
691static void printQualifier(llvm::raw_ostream &Out, const LangOptions &LangOpts,
693 // FIXME: Encode the qualifier, don't just print it.
694 PrintingPolicy PO(LangOpts);
695 PO.SuppressTagKeyword = true;
696 PO.SuppressUnwrittenScope = true;
699 llvm::to_underlying(PrintingPolicy::AnonymousTagMode::Plain);
700 NNS.print(Out, PO);
701}
702
703void USRGenerator::VisitType(QualType T) {
704 // This method mangles in USR information for types. It can possibly
705 // just reuse the naming-mangling logic used by codegen, although the
706 // requirements for USRs might not be the same.
707 ASTContext &Ctx = *Context;
708
709 do {
710 T = Ctx.getCanonicalType(T);
711 Qualifiers Q = T.getQualifiers();
712 unsigned qVal = 0;
713 if (Q.hasConst())
714 qVal |= 0x1;
715 if (Q.hasVolatile())
716 qVal |= 0x2;
717 if (Q.hasRestrict())
718 qVal |= 0x4;
719 if (qVal)
720 Out << ((char)('0' + qVal));
721
722 // Mangle in ObjC GC qualifiers?
723
724 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
725 Out << 'P';
726 T = Expansion->getPattern();
727 }
728
729 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
730 switch (BT->getKind()) {
731 case BuiltinType::Void:
732 Out << 'v';
733 break;
734 case BuiltinType::Bool:
735 Out << 'b';
736 break;
737 case BuiltinType::UChar:
738 Out << 'c';
739 break;
740 case BuiltinType::Char8:
741 Out << 'u';
742 break;
743 case BuiltinType::Char16:
744 Out << 'q';
745 break;
746 case BuiltinType::Char32:
747 Out << 'w';
748 break;
749 case BuiltinType::UShort:
750 Out << 's';
751 break;
752 case BuiltinType::UInt:
753 Out << 'i';
754 break;
755 case BuiltinType::ULong:
756 Out << 'l';
757 break;
758 case BuiltinType::ULongLong:
759 Out << 'k';
760 break;
761 case BuiltinType::UInt128:
762 Out << 'j';
763 break;
764 case BuiltinType::Char_U:
765 case BuiltinType::Char_S:
766 Out << 'C';
767 break;
768 case BuiltinType::SChar:
769 Out << 'r';
770 break;
771 case BuiltinType::WChar_S:
772 case BuiltinType::WChar_U:
773 Out << 'W';
774 break;
775 case BuiltinType::Short:
776 Out << 'S';
777 break;
778 case BuiltinType::Int:
779 Out << 'I';
780 break;
781 case BuiltinType::Long:
782 Out << 'L';
783 break;
784 case BuiltinType::LongLong:
785 Out << 'K';
786 break;
787 case BuiltinType::Int128:
788 Out << 'J';
789 break;
790 case BuiltinType::Float16:
791 case BuiltinType::Half:
792 Out << 'h';
793 break;
794 case BuiltinType::Float:
795 Out << 'f';
796 break;
797 case BuiltinType::Double:
798 Out << 'd';
799 break;
800 case BuiltinType::LongDouble:
801 Out << 'D';
802 break;
803 case BuiltinType::Float128:
804 Out << 'Q';
805 break;
806 case BuiltinType::NullPtr:
807 Out << 'n';
808 break;
809#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
810 case BuiltinType::Id: \
811 Out << "@BT@" << #Suffix << "_" << #ImgType; \
812 break;
813#include "clang/Basic/OpenCLImageTypes.def"
814#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
815 case BuiltinType::Id: \
816 Out << "@BT@" << #ExtType; \
817 break;
818#include "clang/Basic/OpenCLExtensionTypes.def"
819 case BuiltinType::OCLEvent:
820 Out << "@BT@OCLEvent";
821 break;
822 case BuiltinType::OCLClkEvent:
823 Out << "@BT@OCLClkEvent";
824 break;
825 case BuiltinType::OCLQueue:
826 Out << "@BT@OCLQueue";
827 break;
828 case BuiltinType::OCLReserveID:
829 Out << "@BT@OCLReserveID";
830 break;
831 case BuiltinType::OCLSampler:
832 Out << "@BT@OCLSampler";
833 break;
834#define SVE_TYPE(Name, Id, SingletonId) \
835 case BuiltinType::Id: \
836 Out << "@BT@" << #Name; \
837 break;
838#include "clang/Basic/AArch64ACLETypes.def"
839#define PPC_VECTOR_TYPE(Name, Id, Size) \
840 case BuiltinType::Id: \
841 Out << "@BT@" << #Name; \
842 break;
843#include "clang/Basic/PPCTypes.def"
844#define RVV_TYPE(Name, Id, SingletonId) \
845 case BuiltinType::Id: \
846 Out << "@BT@" << Name; \
847 break;
848#include "clang/Basic/RISCVVTypes.def"
849#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
850#include "clang/Basic/WebAssemblyReferenceTypes.def"
851#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
852 case BuiltinType::Id: \
853 Out << "@BT@" << #Name; \
854 break;
855#include "clang/Basic/AMDGPUTypes.def"
856#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
857 case BuiltinType::Id: \
858 Out << "@BT@" << #Name; \
859 break;
860#include "clang/Basic/HLSLIntangibleTypes.def"
861 case BuiltinType::ShortAccum:
862 Out << "@BT@ShortAccum";
863 break;
864 case BuiltinType::Accum:
865 Out << "@BT@Accum";
866 break;
867 case BuiltinType::LongAccum:
868 Out << "@BT@LongAccum";
869 break;
870 case BuiltinType::UShortAccum:
871 Out << "@BT@UShortAccum";
872 break;
873 case BuiltinType::UAccum:
874 Out << "@BT@UAccum";
875 break;
876 case BuiltinType::ULongAccum:
877 Out << "@BT@ULongAccum";
878 break;
879 case BuiltinType::ShortFract:
880 Out << "@BT@ShortFract";
881 break;
882 case BuiltinType::Fract:
883 Out << "@BT@Fract";
884 break;
885 case BuiltinType::LongFract:
886 Out << "@BT@LongFract";
887 break;
888 case BuiltinType::UShortFract:
889 Out << "@BT@UShortFract";
890 break;
891 case BuiltinType::UFract:
892 Out << "@BT@UFract";
893 break;
894 case BuiltinType::ULongFract:
895 Out << "@BT@ULongFract";
896 break;
897 case BuiltinType::SatShortAccum:
898 Out << "@BT@SatShortAccum";
899 break;
900 case BuiltinType::SatAccum:
901 Out << "@BT@SatAccum";
902 break;
903 case BuiltinType::SatLongAccum:
904 Out << "@BT@SatLongAccum";
905 break;
906 case BuiltinType::SatUShortAccum:
907 Out << "@BT@SatUShortAccum";
908 break;
909 case BuiltinType::SatUAccum:
910 Out << "@BT@SatUAccum";
911 break;
912 case BuiltinType::SatULongAccum:
913 Out << "@BT@SatULongAccum";
914 break;
915 case BuiltinType::SatShortFract:
916 Out << "@BT@SatShortFract";
917 break;
918 case BuiltinType::SatFract:
919 Out << "@BT@SatFract";
920 break;
921 case BuiltinType::SatLongFract:
922 Out << "@BT@SatLongFract";
923 break;
924 case BuiltinType::SatUShortFract:
925 Out << "@BT@SatUShortFract";
926 break;
927 case BuiltinType::SatUFract:
928 Out << "@BT@SatUFract";
929 break;
930 case BuiltinType::SatULongFract:
931 Out << "@BT@SatULongFract";
932 break;
933 case BuiltinType::BFloat16:
934 Out << "@BT@__bf16";
935 break;
936 case BuiltinType::Ibm128:
937 Out << "@BT@__ibm128";
938 break;
939 case BuiltinType::ObjCId:
940 Out << 'o';
941 break;
942 case BuiltinType::ObjCClass:
943 Out << 'O';
944 break;
945 case BuiltinType::ObjCSel:
946 Out << 'e';
947 break;
948#define BUILTIN_TYPE(Id, SingletonId)
949#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
950#include "clang/AST/BuiltinTypes.def"
951 case BuiltinType::Dependent:
952 // If you're adding a new builtin type, please add its name prefixed
953 // with "@BT@" to `Out` (see cases above).
954 IgnoreResults = true;
955 break;
956 }
957 return;
958 }
959
960 // If we have already seen this (non-built-in) type, use a substitution
961 // encoding. Otherwise, record this as a substitution.
962 auto [Substitution, Inserted] =
963 TypeSubstitutions.try_emplace(T.getTypePtr(), TypeSubstitutions.size());
964 if (!Inserted) {
965 Out << 'S' << Substitution->second << '_';
966 return;
967 }
968
969 if (const PointerType *PT = T->getAs<PointerType>()) {
970 Out << '*';
971 T = PT->getPointeeType();
972 continue;
973 }
974 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
975 Out << '*';
976 T = OPT->getPointeeType();
977 continue;
978 }
979 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
980 Out << "&&";
981 T = RT->getPointeeType();
982 continue;
983 }
984 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
985 Out << '&';
986 T = RT->getPointeeType();
987 continue;
988 }
989 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
990 Out << 'F';
991 VisitType(FT->getReturnType());
992 Out << '(';
993 for (const auto &I : FT->param_types()) {
994 Out << '#';
995 VisitType(I);
996 }
997 Out << ')';
998 if (FT->isVariadic())
999 Out << '.';
1000 return;
1001 }
1002 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
1003 Out << 'B';
1004 T = BT->getPointeeType();
1005 continue;
1006 }
1007 if (const ComplexType *CT = T->getAs<ComplexType>()) {
1008 Out << '<';
1009 T = CT->getElementType();
1010 continue;
1011 }
1012 if (const TagType *TT = T->getAs<TagType>()) {
1013 if (const auto *ICNT = dyn_cast<InjectedClassNameType>(TT)) {
1014 T = ICNT->getDecl()->getCanonicalTemplateSpecializationType(Ctx);
1015 } else {
1016 Out << '$';
1017 VisitTagDecl(TT->getDecl());
1018 return;
1019 }
1020 }
1021 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
1022 Out << '$';
1023 VisitObjCInterfaceDecl(OIT->getDecl());
1024 return;
1025 }
1026 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
1027 Out << 'Q';
1028 VisitType(OIT->getBaseType());
1029 for (auto *Prot : OIT->getProtocols())
1030 VisitObjCProtocolDecl(Prot);
1031 return;
1032 }
1033 if (const TemplateTypeParmType *TTP =
1034 T->getAsCanonical<TemplateTypeParmType>()) {
1035 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
1036 return;
1037 }
1038 if (const TemplateSpecializationType *Spec =
1039 T->getAs<TemplateSpecializationType>()) {
1040 Out << '>';
1041 VisitTemplateName(Spec->getTemplateName());
1042 Out << Spec->template_arguments().size();
1043 for (const auto &Arg : Spec->template_arguments())
1044 VisitTemplateArgument(Arg);
1045 return;
1046 }
1047 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
1048 Out << '^';
1049 printQualifier(Out, LangOpts, DNT->getQualifier());
1050 Out << ':' << DNT->getIdentifier()->getName();
1051 return;
1052 }
1053 if (const auto *VT = T->getAs<VectorType>()) {
1054 Out << (T->isExtVectorType() ? ']' : '[');
1055 Out << VT->getNumElements();
1056 T = VT->getElementType();
1057 continue;
1058 }
1059 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
1060 Out << '{';
1061 switch (AT->getSizeModifier()) {
1062 case ArraySizeModifier::Static:
1063 Out << 's';
1064 break;
1065 case ArraySizeModifier::Star:
1066 Out << '*';
1067 break;
1068 case ArraySizeModifier::Normal:
1069 Out << 'n';
1070 break;
1071 }
1072 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
1073 Out << CAT->getSize();
1074
1075 T = AT->getElementType();
1076 continue;
1077 }
1078
1079 // Unhandled type.
1080 Out << ' ';
1081 break;
1082 } while (true);
1083}
1084
1085void USRGenerator::VisitTemplateParameterList(
1086 const TemplateParameterList *Params) {
1087 if (!Params)
1088 return;
1089 Out << '>' << Params->size();
1090 for (TemplateParameterList::const_iterator P = Params->begin(),
1091 PEnd = Params->end();
1092 P != PEnd; ++P) {
1093 Out << '#';
1094 if (isa<TemplateTypeParmDecl>(*P)) {
1095 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
1096 Out << 'p';
1097 Out << 'T';
1098 continue;
1099 }
1100
1101 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1102 if (NTTP->isParameterPack())
1103 Out << 'p';
1104 Out << 'N';
1105 VisitType(NTTP->getType());
1106 continue;
1107 }
1108
1109 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
1110 if (TTP->isParameterPack())
1111 Out << 'p';
1112 Out << 't';
1113 VisitTemplateParameterList(TTP->getTemplateParameters());
1114 }
1115}
1116
1117void USRGenerator::VisitTemplateName(TemplateName Name) {
1118 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1119 if (TemplateTemplateParmDecl *TTP =
1120 dyn_cast<TemplateTemplateParmDecl>(Template)) {
1121 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
1122 return;
1123 }
1124
1125 Visit(Template);
1126 return;
1127 }
1128
1129 // FIXME: Visit dependent template names.
1130}
1131
1132void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
1133 switch (Arg.getKind()) {
1135 break;
1136
1138 Visit(Arg.getAsDecl());
1139 break;
1140
1142 break;
1143
1145 Out << 'P'; // pack expansion of...
1146 [[fallthrough]];
1148 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1149 break;
1150
1152 // FIXME: Visit expressions.
1153 break;
1154
1156 Out << 'p' << Arg.pack_size();
1157 for (const auto &P : Arg.pack_elements())
1158 VisitTemplateArgument(P);
1159 break;
1160
1162 VisitType(Arg.getAsType());
1163 break;
1164
1166 Out << 'V';
1167 VisitType(Arg.getIntegralType());
1168 Out << Arg.getAsIntegral();
1169 break;
1170
1172 Out << 'S';
1173 VisitType(Arg.getStructuralValueType());
1174 ODRHash Hash{};
1176 Out << Hash.CalculateHash();
1177 break;
1178 }
1179 }
1180}
1181
1182void USRGenerator::VisitUnresolvedUsingValueDecl(
1183 const UnresolvedUsingValueDecl *D) {
1184 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
1185 return;
1186 VisitDeclContext(D->getDeclContext());
1187 Out << "@UUV@";
1188 printQualifier(Out, LangOpts, D->getQualifier());
1189 EmitDeclName(D);
1190}
1191
1192void USRGenerator::VisitUnresolvedUsingTypenameDecl(
1193 const UnresolvedUsingTypenameDecl *D) {
1194 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
1195 return;
1196 VisitDeclContext(D->getDeclContext());
1197 Out << "@UUT@";
1198 printQualifier(Out, LangOpts, D->getQualifier());
1199 Out << D->getName(); // Simple name.
1200}
1201
1202void USRGenerator::VisitConceptDecl(const ConceptDecl *D) {
1203 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
1204 return;
1205 VisitDeclContext(D->getDeclContext());
1206 Out << "@CT@";
1207 EmitDeclName(D);
1208}
1209
1210void USRGenerator::VisitMSGuidDecl(const MSGuidDecl *D) {
1211 VisitDeclContext(D->getDeclContext());
1212 Out << "@MG@";
1213 D->NamedDecl::printName(Out);
1214}
1215
1216void USRGenerator::VisitTemplateParamObjectDecl(
1217 const TemplateParamObjectDecl *D) {
1218 Out << "@TPO@";
1219 VisitType(D->getType());
1220 ODRHash Hash{};
1221 Hash.AddStructuralValue(D->getValue());
1222 Out << Hash.CalculateHash();
1223}
1224
1225//===----------------------------------------------------------------------===//
1226// USR generation functions.
1227//===----------------------------------------------------------------------===//
1228
1229static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
1230 StringRef CatSymDefinedIn,
1231 raw_ostream &OS) {
1232 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
1233 return;
1234 if (CatSymDefinedIn.empty()) {
1235 OS << "@M@" << ClsSymDefinedIn << '@';
1236 return;
1237 }
1238 OS << "@CM@" << CatSymDefinedIn << '@';
1239 if (ClsSymDefinedIn != CatSymDefinedIn) {
1240 OS << ClsSymDefinedIn << '@';
1241 }
1242}
1243
1245 StringRef Cls, raw_ostream &OS, StringRef ExtSymDefinedIn,
1246 StringRef CategoryContextExtSymbolDefinedIn) {
1248 CategoryContextExtSymbolDefinedIn, OS);
1249 OS << "objc(cs)" << Cls;
1250}
1251
1252void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
1253 raw_ostream &OS,
1254 StringRef ClsSymDefinedIn,
1255 StringRef CatSymDefinedIn) {
1256 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
1257 OS << "objc(cy)" << Cls << '@' << Cat;
1258}
1259
1260void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
1261 OS << '@' << Ivar;
1262}
1263
1265 bool IsInstanceMethod,
1266 raw_ostream &OS) {
1267 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1268}
1269
1270void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1271 raw_ostream &OS) {
1272 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
1273}
1274
1275void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1276 StringRef ExtSymDefinedIn) {
1277 if (!ExtSymDefinedIn.empty())
1278 OS << "@M@" << ExtSymDefinedIn << '@';
1279 OS << "objc(pl)" << Prot;
1280}
1281
1282void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1283 StringRef ExtSymDefinedIn) {
1284 if (!ExtSymDefinedIn.empty())
1285 OS << "@M@" << ExtSymDefinedIn;
1286 OS << "@E@" << EnumName;
1287}
1288
1289void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1290 raw_ostream &OS) {
1291 OS << '@' << EnumConstantName;
1292}
1293
1295 SmallVectorImpl<char> &Buf) {
1296 if (!D)
1297 return true;
1298 return generateUSRForDecl(D, Buf, D->getASTContext().getLangOpts());
1299}
1300
1302 const LangOptions &LangOpts) {
1303 if (!D)
1304 return true;
1305 // We don't ignore decls with invalid source locations. Implicit decls, like
1306 // C++'s operator new function, can have invalid locations but it is fine to
1307 // create USRs that can identify them.
1308
1309 // Check if the declaration has explicit external USR specified.
1310 auto *CD = D->getCanonicalDecl();
1311 if (auto *ExternalSymAttr = CD->getAttr<ExternalSourceSymbolAttr>()) {
1312 if (!ExternalSymAttr->getUSR().empty()) {
1313 llvm::raw_svector_ostream Out(Buf);
1314 Out << ExternalSymAttr->getUSR();
1315 return false;
1316 }
1317 }
1318 USRGenerator UG(&D->getASTContext(), Buf, LangOpts);
1319 UG.Visit(D);
1320 return UG.ignoreResults();
1321}
1322
1324 const SourceManager &SM,
1325 SmallVectorImpl<char> &Buf) {
1326 if (!MD)
1327 return true;
1328 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(), SM,
1329 Buf);
1330}
1331
1333 const SourceManager &SM,
1334 SmallVectorImpl<char> &Buf) {
1335 if (MacroName.empty())
1336 return true;
1337
1338 llvm::raw_svector_ostream Out(Buf);
1339
1340 // Assume that system headers are sane. Don't put source location
1341 // information into the USR if the macro comes from a system header.
1342 bool ShouldGenerateLocation = Loc.isValid() && !SM.isInSystemHeader(Loc);
1343
1344 Out << getUSRSpacePrefix();
1345 if (ShouldGenerateLocation)
1346 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1347 Out << "@macro@";
1348 Out << MacroName;
1349 return false;
1350}
1351
1356
1359 const LangOptions &LangOpts) {
1360 if (T.isNull())
1361 return true;
1362 T = T.getCanonicalType();
1363
1364 USRGenerator UG(&Ctx, Buf, LangOpts);
1365 UG.VisitType(T);
1366 return UG.ignoreResults();
1367}
1368
1370 raw_ostream &OS) {
1371 if (!Mod->Parent)
1373 if (generateFullUSRForModule(Mod->Parent, OS))
1374 return true;
1375 return generateUSRFragmentForModule(Mod, OS);
1376}
1377
1379 raw_ostream &OS) {
1380 OS << getUSRSpacePrefix();
1381 return generateUSRFragmentForModuleName(ModName, OS);
1382}
1383
1385 raw_ostream &OS) {
1386 return generateUSRFragmentForModuleName(Mod->Name, OS);
1387}
1388
1390 raw_ostream &OS) {
1391 OS << "@M@" << ModName;
1392 return false;
1393}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
This file contains the declaration of the ODRHash class, which calculates a hash based on AST nodes,...
static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn, StringRef CatSymDefinedIn, raw_ostream &OS)
static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, const SourceManager &SM, bool IncludeOffset)
Print Loc including both the file and offset, if IncludeOffset is true.
static const ObjCCategoryDecl * getCategoryContext(const NamedDecl *D)
static void printQualifier(llvm::raw_ostream &Out, const LangOptions &LangOpts, NestedNameSpecifier NNS)
static bool printLocOffset(llvm::raw_ostream &OS, SourceLocation Loc, const SourceManager &SM)
Print only the offset part of Loc.
static StringRef GetExternalSourceContainer(const NamedDecl *D)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
SourceManager & getSourceManager()
Definition ASTContext.h:869
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
const LangOptions & getLangOpts() const
Definition ASTContext.h:965
const ObjCInterfaceDecl * getObjContainingInterface(const NamedDecl *ND) const
Returns the Objective-C interface that ND belongs to if it is an Objective-C method/property/ivar etc...
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
A simple visitor class that helps create declaration visitors.
Definition DeclVisitor.h:75
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2126
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition DeclBase.cpp:344
T * getAttr() const
Definition DeclBase.h:581
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:550
ExternalSourceSymbolAttr * getExternalSourceSymbolAttr() const
Looks on this and related declarations for an applicable external source symbol attribute.
Definition DeclBase.cpp:616
SourceLocation getLocation() const
Definition DeclBase.h:447
DeclContext * getDeclContext()
Definition DeclBase.h:456
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:439
bool hasAttr() const
Definition DeclBase.h:585
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:995
Kind getKind() const
Definition DeclBase.h:450
void print(raw_ostream &OS, const PrintingPolicy &Policy) const
bool isEmpty() const
Evaluates true when this declaration name is empty.
StringRef getName() const
The name of this FileEntry.
Definition FileEntry.h:61
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4185
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4173
QualType getReturnType() const
Definition Decl.h:2885
bool isVariadic() const
Whether this function is variadic.
Definition Decl.cpp:3112
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition Decl.cpp:4309
bool isExternC() const
Determines whether this function is a function with external, C linkage.
Definition Decl.cpp:3598
const ASTTemplateArgumentListInfo * getTemplateSpecializationArgsAsWritten() const
Retrieve the template argument list as written in the sources, if any.
Definition Decl.cpp:4319
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
StringRef getName() const
Return the actual identifier string.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Record the location of a macro definition.
SourceLocation getLocation() const
Retrieve the location of the macro name in the definition.
const IdentifierInfo * getName() const
Retrieve the name of the macro being defined.
Module * Parent
The parent of this module.
Definition Module.h:389
std::string Name
The name of this module.
Definition Module.h:343
This represents a decl that may have a name.
Definition Decl.h:274
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
bool isExternallyVisible() const
Definition Decl.h:433
bool isAnonymousNamespace() const
Returns true if this is an anonymous namespace declaration.
Definition Decl.h:643
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool ResolveTemplateArguments=false, bool PrintFinalScopeResOp=true) const
Print this nested name specifier to the given output stream.
void AddStructuralValue(const APValue &)
Definition ODRHash.cpp:1258
unsigned CalculateHash()
Definition ODRHash.cpp:231
ObjCCategoryDecl - Represents a category declaration.
Definition DeclObjC.h:2335
ObjCInterfaceDecl * getClassInterface()
Definition DeclObjC.h:2378
bool IsClassExtension() const
Definition DeclObjC.h:2443
const ObjCInterfaceDecl * getClassInterface() const
Definition DeclObjC.h:2492
Selector getSelector() const
Definition DeclObjC.h:330
bool isInstanceMethod() const
Definition DeclObjC.h:429
ObjCInterfaceDecl * getClassInterface()
bool isClassProperty() const
Definition DeclObjC.h:861
ObjCPropertyDecl * getPropertyDecl() const
Definition DeclObjC.h:2876
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
QualType getCanonicalType() const
Definition TypeBase.h:8547
bool hasConst() const
Definition TypeBase.h:458
bool hasRestrict() const
Definition TypeBase.h:478
bool hasVolatile() const
Definition TypeBase.h:468
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
FileIDAndOffset getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID.
bool isEmbeddedInDeclarator() const
True if this tag declaration is "embedded" (i.e., defined or declared for the very first time) in the...
Definition Decl.h:3886
TypedefNameDecl * getTypedefNameForAnonDecl() const
Definition Decl.h:3998
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:4899
bool isFreeStanding() const
True if this tag is free standing, e.g. "struct foo;".
Definition Decl.h:3897
TagKind getTagKind() const
Definition Decl.h:3961
unsigned size() const
Retrieve the number of template arguments in this template argument list.
const TemplateArgument & get(unsigned Idx) const
Retrieve the template argument at a given index.
QualType getStructuralValueType() const
Get the type of a StructuralValue.
QualType getAsType() const
Retrieve the type for a type template argument.
llvm::APSInt getAsIntegral() const
Retrieve the template argument as an integral value.
unsigned pack_size() const
The number of template arguments in the given template argument pack.
QualType getIntegralType() const
Retrieve the type of the integral value.
ValueDecl * getAsDecl() const
Retrieve the declaration for a declaration non-type template argument.
ArrayRef< TemplateArgument > pack_elements() const
Iterator range referencing all of the elements of a template argument pack.
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
@ Template
The template argument is a template name that was provided for a template template parameter.
@ StructuralValue
The template argument is a non-type template argument that can't be represented by the special-case D...
@ Pack
The template argument is actually a parameter pack.
@ TemplateExpansion
The template argument is a pack expansion of a template name that was provided for a template templat...
@ NullPtr
The template argument is a null pointer or null pointer to member that was provided for a non-type te...
@ Type
The template argument is a type.
@ Null
Represents an empty template argument, e.g., one that has not been deduced.
@ Integral
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
@ Expression
The template argument is an expression, and we've not resolved it to one of the other forms yet,...
ArgKind getKind() const
Return the kind of stored template argument.
TemplateName getAsTemplateOrTemplatePattern() const
Retrieve the template argument as a template name; if the argument is a pack expansion,...
const APValue & getAsStructuralValue() const
Get the value of a StructuralValue.
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
TemplateDecl * getAsTemplateDecl(bool IgnoreDeduced=false) const
Retrieve the underlying template declaration that this template name refers to, if known.
const APValue & getValue() const
bool isParameterPack() const
Whether this template template parameter is a template parameter pack.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isExtVectorType() const
Definition TypeBase.h:8875
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2992
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9325
NestedNameSpecifier getQualifier() const
Retrieve the nested-name-specifier that qualifies the name.
Definition DeclCXX.h:4095
NestedNameSpecifier getQualifier() const
Retrieve the nested-name-specifier that qualifies the name.
Definition DeclCXX.h:4005
QualType getType() const
Definition Decl.h:723
VarTemplateDecl * getDescribedVarTemplate() const
Retrieves the variable template that is described by this variable declaration.
Definition Decl.cpp:2773
bool generateFullUSRForTopLevelModuleName(StringRef ModName, raw_ostream &OS)
Generate a USR for a top-level module name, including the USR prefix.
static StringRef getUSRSpacePrefix()
void generateUSRForObjCCategory(StringRef Cls, StringRef Cat, raw_ostream &OS, StringRef ClsExtSymbolDefinedIn="", StringRef CatExtSymbolDefinedIn="")
Generate a USR fragment for an Objective-C class category.
bool generateFullUSRForModule(const Module *Mod, raw_ostream &OS)
Generate a USR for a module, including the USR prefix.
bool generateUSRFragmentForModuleName(StringRef ModName, raw_ostream &OS)
Generate a USR fragment for a module name.
bool generateUSRForMacro(const MacroDefinitionRecord *MD, const SourceManager &SM, SmallVectorImpl< char > &Buf)
Generate a USR for a macro, including the USR prefix.
void generateUSRForObjCProperty(StringRef Prop, bool isClassProp, raw_ostream &OS)
Generate a USR fragment for an Objective-C property.
void generateUSRForEnumConstant(StringRef EnumConstantName, raw_ostream &OS)
Generate a USR fragment for an enum constant.
void generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS)
Generate a USR fragment for an Objective-C instance variable.
void generateUSRForObjCMethod(StringRef Sel, bool IsInstanceMethod, raw_ostream &OS)
Generate a USR fragment for an Objective-C method.
bool generateUSRForType(QualType T, ASTContext &Ctx, SmallVectorImpl< char > &Buf)
Generates a USR for a type.
void generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, StringRef ExtSymbolDefinedIn="", StringRef CategoryContextExtSymbolDefinedIn="")
Generate a USR fragment for an Objective-C class.
void generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, StringRef ExtSymbolDefinedIn="")
Generate USR fragment for a global (non-nested) enum.
void generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS, StringRef ExtSymbolDefinedIn="")
Generate a USR fragment for an Objective-C protocol.
bool generateUSRFragmentForModule(const Module *Mod, raw_ostream &OS)
Generate a USR fragment for a module.
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:196
@ RQ_None
No ref-qualifier was provided.
Definition TypeBase.h:1798
@ RQ_LValue
An lvalue ref-qualifier was provided (&).
Definition TypeBase.h:1801
@ RQ_RValue
An rvalue ref-qualifier was provided (&&).
Definition TypeBase.h:1804
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
Definition Parser.h:61
std::pair< FileID, unsigned > FileIDAndOffset
const FunctionProtoType * T
@ Template
We are parsing a template declaration.
Definition Parser.h:81
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressUnwrittenScope
Suppress printing parts of scope specifiers that are never written, e.g., for anonymous namespaces.
unsigned ConstantArraySizeAsWritten
Whether we should print the sizes of constant array expressions as written in the sources.
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.
@ Plain
E.g., (anonymous enum)/(unnamed struct)/etc.