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