1 //===- USRGeneration.cpp - Routines for USR generation --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Index/USRGeneration.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclTemplate.h"
13 #include "clang/AST/DeclVisitor.h"
14 #include "clang/Lex/PreprocessingRecord.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/raw_ostream.h"
18 using namespace clang;
19 using namespace clang::index;
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 /// \returns true on error.
26 static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
27 const SourceManager &SM, bool IncludeOffset) {
28 if (Loc.isInvalid()) {
31 Loc = SM.getExpansionLoc(Loc);
32 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
33 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
35 OS << llvm::sys::path::filename(FE->getName());
37 // This case really isn't interesting.
41 // Use the offest into the FileID to represent the location. Using
42 // a line/column can cause us to look back at the original source file,
43 // which is expensive.
44 OS << '@' << Decomposed.second;
50 class USRGenerator : public ConstDeclVisitor<USRGenerator> {
51 SmallVectorImpl<char> &Buf;
52 llvm::raw_svector_ostream Out;
57 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
60 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
67 // Add the USR space prefix.
68 Out << getUSRSpacePrefix();
71 bool ignoreResults() const { return IgnoreResults; }
73 // Visitation methods from generating USRs from AST elements.
74 void VisitDeclContext(const DeclContext *D);
75 void VisitFieldDecl(const FieldDecl *D);
76 void VisitFunctionDecl(const FunctionDecl *D);
77 void VisitNamedDecl(const NamedDecl *D);
78 void VisitNamespaceDecl(const NamespaceDecl *D);
79 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
80 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
81 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
82 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
83 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
84 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
85 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
86 void VisitTagDecl(const TagDecl *D);
87 void VisitTypedefDecl(const TypedefDecl *D);
88 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
89 void VisitVarDecl(const VarDecl *D);
90 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
91 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
93 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
97 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
101 void VisitUsingDecl(const UsingDecl *D) {
102 IgnoreResults = true;
105 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
106 IgnoreResults = true;
109 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
110 IgnoreResults = true;
113 bool ShouldGenerateLocation(const NamedDecl *D);
115 bool isLocal(const NamedDecl *D) {
116 return D->getParentFunctionOrMethod() != nullptr;
119 /// Generate the string component containing the location of the
121 bool GenLoc(const Decl *D, bool IncludeOffset);
123 /// String generation methods used both by the visitation methods
124 /// and from other clients that want to directly generate USRs. These
125 /// methods do not construct complete USRs (which incorporate the parents
126 /// of an AST element), but only the fragments concerning the AST element
129 /// Generate a USR for an Objective-C class.
130 void GenObjCClass(StringRef cls) {
131 generateUSRForObjCClass(cls, Out);
134 /// Generate a USR for an Objective-C class category.
135 void GenObjCCategory(StringRef cls, StringRef cat) {
136 generateUSRForObjCCategory(cls, cat, Out);
139 /// Generate a USR fragment for an Objective-C property.
140 void GenObjCProperty(StringRef prop, bool isClassProp) {
141 generateUSRForObjCProperty(prop, isClassProp, Out);
144 /// Generate a USR for an Objective-C protocol.
145 void GenObjCProtocol(StringRef prot) {
146 generateUSRForObjCProtocol(prot, Out);
149 void VisitType(QualType T);
150 void VisitTemplateParameterList(const TemplateParameterList *Params);
151 void VisitTemplateName(TemplateName Name);
152 void VisitTemplateArgument(const TemplateArgument &Arg);
154 /// Emit a Decl's name using NamedDecl::printName() and return true if
155 /// the decl had no name.
156 bool EmitDeclName(const NamedDecl *D);
158 } // end anonymous namespace
160 //===----------------------------------------------------------------------===//
161 // Generating USRs from ASTS.
162 //===----------------------------------------------------------------------===//
164 bool USRGenerator::EmitDeclName(const NamedDecl *D) {
165 const unsigned startSize = Buf.size();
167 const unsigned endSize = Buf.size();
168 return startSize == endSize;
171 bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
172 if (D->isExternallyVisible())
174 if (D->getParentFunctionOrMethod())
176 SourceLocation Loc = D->getLocation();
179 const SourceManager &SM = Context->getSourceManager();
180 return !SM.isInSystemHeader(Loc);
183 void USRGenerator::VisitDeclContext(const DeclContext *DC) {
184 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
188 void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
189 // The USR for an ivar declared in a class extension is based on the
190 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
191 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
194 VisitDeclContext(D->getDeclContext());
195 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
196 if (EmitDeclName(D)) {
197 // Bit fields can be anonymous.
198 IgnoreResults = true;
203 void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
204 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
207 VisitDeclContext(D->getDeclContext());
208 bool IsTemplate = false;
209 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
212 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
216 PrintingPolicy Policy(Context->getLangOpts());
217 // Forward references can have different template argument names. Suppress the
218 // template argument names in constructors to make their USR more stable.
219 Policy.SuppressTemplateArgsInCXXConstructors = true;
220 D->getDeclName().print(Out, Policy);
222 ASTContext &Ctx = *Context;
223 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
224 !D->hasAttr<OverloadableAttr>())
227 if (const TemplateArgumentList *
228 SpecArgs = D->getTemplateSpecializationArgs()) {
230 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
232 VisitTemplateArgument(SpecArgs->get(I));
237 // Mangle in type information for the arguments.
238 for (auto PD : D->parameters()) {
240 VisitType(PD->getType());
245 // Function templates can be overloaded by return type, for example:
247 // template <class T> typename T::A foo() {}
248 // template <class T> typename T::B foo() {}
251 VisitType(D->getReturnType());
254 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
257 if (unsigned quals = MD->getTypeQualifiers())
258 Out << (char)('0' + quals);
259 switch (MD->getRefQualifier()) {
261 case RQ_LValue: Out << '&'; break;
262 case RQ_RValue: Out << "&&"; break;
267 void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
268 VisitDeclContext(D->getDeclContext());
271 if (EmitDeclName(D)) {
272 // The string can be empty if the declaration has no name; e.g., it is
273 // the ParmDecl with no name for declaration of a function pointer type,
274 // e.g.: void (*f)(void *);
275 // In this case, don't generate a USR.
276 IgnoreResults = true;
280 void USRGenerator::VisitVarDecl(const VarDecl *D) {
281 // VarDecls can be declared 'extern' within a function or method body,
282 // but their enclosing DeclContext is the function, not the TU. We need
283 // to check the storage class to correctly generate the USR.
284 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
287 VisitDeclContext(D->getDeclContext());
289 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
291 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
292 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
293 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
295 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
298 // Variables always have simple names.
299 StringRef s = D->getName();
301 // The string can be empty if the declaration has no name; e.g., it is
302 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
303 // void (*f)(void *);
304 // In this case, don't generate a USR.
306 IgnoreResults = true;
310 // For a template specialization, mangle the template arguments.
311 if (const VarTemplateSpecializationDecl *Spec
312 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
313 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
315 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
317 VisitTemplateArgument(Args.get(I));
322 void USRGenerator::VisitNonTypeTemplateParmDecl(
323 const NonTypeTemplateParmDecl *D) {
324 GenLoc(D, /*IncludeOffset=*/true);
327 void USRGenerator::VisitTemplateTemplateParmDecl(
328 const TemplateTemplateParmDecl *D) {
329 GenLoc(D, /*IncludeOffset=*/true);
332 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
333 if (D->isAnonymousNamespace()) {
338 VisitDeclContext(D->getDeclContext());
340 Out << "@N@" << D->getName();
343 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
344 VisitFunctionDecl(D->getTemplatedDecl());
347 void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
348 VisitTagDecl(D->getTemplatedDecl());
351 void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
352 VisitDeclContext(D->getDeclContext());
354 Out << "@NA@" << D->getName();
357 void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
358 const DeclContext *container = D->getDeclContext();
359 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
363 // The USR for a method declared in a class extension or category is based on
364 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
365 const ObjCInterfaceDecl *ID = D->getClassInterface();
367 IgnoreResults = true;
372 // Ideally we would use 'GenObjCMethod', but this is such a hot path
373 // for Objective-C code that we don't want to use
374 // DeclarationName::getAsString().
375 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
376 << DeclarationName(D->getSelector());
379 void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
380 switch (D->getKind()) {
382 llvm_unreachable("Invalid ObjC container.");
383 case Decl::ObjCInterface:
384 case Decl::ObjCImplementation:
385 GenObjCClass(D->getName());
387 case Decl::ObjCCategory: {
388 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
389 const ObjCInterfaceDecl *ID = CD->getClassInterface();
391 // Handle invalid code where the @interface might not
392 // have been specified.
393 // FIXME: We should be able to generate this USR even if the
394 // @interface isn't available.
395 IgnoreResults = true;
398 // Specially handle class extensions, which are anonymous categories.
399 // We want to mangle in the location to uniquely distinguish them.
400 if (CD->IsClassExtension()) {
401 Out << "objc(ext)" << ID->getName() << '@';
402 GenLoc(CD, /*IncludeOffset=*/true);
405 GenObjCCategory(ID->getName(), CD->getName());
409 case Decl::ObjCCategoryImpl: {
410 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
411 const ObjCInterfaceDecl *ID = CD->getClassInterface();
413 // Handle invalid code where the @interface might not
414 // have been specified.
415 // FIXME: We should be able to generate this USR even if the
416 // @interface isn't available.
417 IgnoreResults = true;
420 GenObjCCategory(ID->getName(), CD->getName());
423 case Decl::ObjCProtocol:
424 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
429 void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
430 // The USR for a property declared in a class extension or category is based
431 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
432 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
435 Visit(cast<Decl>(D->getDeclContext()));
436 GenObjCProperty(D->getName(), D->isClassProperty());
439 void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
440 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
441 VisitObjCPropertyDecl(PD);
445 IgnoreResults = true;
448 void USRGenerator::VisitTagDecl(const TagDecl *D) {
449 // Add the location of the tag decl to handle resolution across
450 // translation units.
451 if (!isa<EnumDecl>(D) &&
452 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
455 D = D->getCanonicalDecl();
456 VisitDeclContext(D->getDeclContext());
458 bool AlreadyStarted = false;
459 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
460 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
461 AlreadyStarted = true;
463 switch (D->getTagKind()) {
466 case TTK_Struct: Out << "@ST"; break;
467 case TTK_Union: Out << "@UT"; break;
468 case TTK_Enum: llvm_unreachable("enum template");
470 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
471 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
472 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
473 AlreadyStarted = true;
475 switch (D->getTagKind()) {
478 case TTK_Struct: Out << "@SP"; break;
479 case TTK_Union: Out << "@UP"; break;
480 case TTK_Enum: llvm_unreachable("enum partial specialization");
482 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
486 if (!AlreadyStarted) {
487 switch (D->getTagKind()) {
490 case TTK_Struct: Out << "@S"; break;
491 case TTK_Union: Out << "@U"; break;
492 case TTK_Enum: Out << "@E"; break;
497 assert(Buf.size() > 0);
498 const unsigned off = Buf.size() - 1;
500 if (EmitDeclName(D)) {
501 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
506 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
507 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
510 if (auto *ED = dyn_cast<EnumDecl>(D)) {
511 // Distinguish USRs of anonymous enums by using their first enumerator.
512 auto enum_range = ED->enumerators();
513 if (enum_range.begin() != enum_range.end()) {
514 Out << '@' << **enum_range.begin();
521 // For a class template specialization, mangle the template arguments.
522 if (const ClassTemplateSpecializationDecl *Spec
523 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
524 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
526 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
528 VisitTemplateArgument(Args.get(I));
533 void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
534 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
536 const DeclContext *DC = D->getDeclContext();
537 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
543 void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
544 GenLoc(D, /*IncludeOffset=*/true);
547 bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
549 return IgnoreResults;
552 // Guard against null declarations in invalid code.
554 IgnoreResults = true;
558 // Use the location of canonical decl.
559 D = D->getCanonicalDecl();
562 IgnoreResults || printLoc(Out, D->getLocStart(),
563 Context->getSourceManager(), IncludeOffset);
565 return IgnoreResults;
568 void USRGenerator::VisitType(QualType T) {
569 // This method mangles in USR information for types. It can possibly
570 // just reuse the naming-mangling logic used by codegen, although the
571 // requirements for USRs might not be the same.
572 ASTContext &Ctx = *Context;
575 T = Ctx.getCanonicalType(T);
576 Qualifiers Q = T.getQualifiers();
585 Out << ((char) ('0' + qVal));
587 // Mangle in ObjC GC qualifiers?
589 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
591 T = Expansion->getPattern();
594 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
595 unsigned char c = '\0';
596 switch (BT->getKind()) {
597 case BuiltinType::Void:
599 case BuiltinType::Bool:
601 case BuiltinType::UChar:
603 case BuiltinType::Char16:
605 case BuiltinType::Char32:
607 case BuiltinType::UShort:
609 case BuiltinType::UInt:
611 case BuiltinType::ULong:
613 case BuiltinType::ULongLong:
615 case BuiltinType::UInt128:
617 case BuiltinType::Char_U:
618 case BuiltinType::Char_S:
620 case BuiltinType::SChar:
622 case BuiltinType::WChar_S:
623 case BuiltinType::WChar_U:
625 case BuiltinType::Short:
627 case BuiltinType::Int:
629 case BuiltinType::Long:
631 case BuiltinType::LongLong:
633 case BuiltinType::Int128:
635 case BuiltinType::Half:
637 case BuiltinType::Float:
639 case BuiltinType::Double:
641 case BuiltinType::LongDouble:
643 case BuiltinType::Float128:
645 case BuiltinType::NullPtr:
647 #define BUILTIN_TYPE(Id, SingletonId)
648 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
649 #include "clang/AST/BuiltinTypes.def"
650 case BuiltinType::Dependent:
651 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
652 case BuiltinType::Id:
653 #include "clang/Basic/OpenCLImageTypes.def"
654 case BuiltinType::OCLEvent:
655 case BuiltinType::OCLClkEvent:
656 case BuiltinType::OCLQueue:
657 case BuiltinType::OCLNDRange:
658 case BuiltinType::OCLReserveID:
659 case BuiltinType::OCLSampler:
660 IgnoreResults = true;
662 case BuiltinType::ObjCId:
664 case BuiltinType::ObjCClass:
666 case BuiltinType::ObjCSel:
673 // If we have already seen this (non-built-in) type, use a substitution
675 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
676 = TypeSubstitutions.find(T.getTypePtr());
677 if (Substitution != TypeSubstitutions.end()) {
678 Out << 'S' << Substitution->second << '_';
681 // Record this as a substitution.
682 unsigned Number = TypeSubstitutions.size();
683 TypeSubstitutions[T.getTypePtr()] = Number;
686 if (const PointerType *PT = T->getAs<PointerType>()) {
688 T = PT->getPointeeType();
691 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
693 T = OPT->getPointeeType();
696 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
698 T = RT->getPointeeType();
701 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
703 T = RT->getPointeeType();
706 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
708 VisitType(FT->getReturnType());
709 for (const auto &I : FT->param_types())
711 if (FT->isVariadic())
715 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
717 T = BT->getPointeeType();
720 if (const ComplexType *CT = T->getAs<ComplexType>()) {
722 T = CT->getElementType();
725 if (const TagType *TT = T->getAs<TagType>()) {
727 VisitTagDecl(TT->getDecl());
730 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
732 VisitObjCInterfaceDecl(OIT->getDecl());
735 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
737 VisitType(OIT->getBaseType());
738 for (auto *Prot : OIT->getProtocols())
739 VisitObjCProtocolDecl(Prot);
742 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
743 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
746 if (const TemplateSpecializationType *Spec
747 = T->getAs<TemplateSpecializationType>()) {
749 VisitTemplateName(Spec->getTemplateName());
750 Out << Spec->getNumArgs();
751 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
752 VisitTemplateArgument(Spec->getArg(I));
755 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
757 // FIXME: Encode the qualifier, don't just print it.
758 PrintingPolicy PO(Ctx.getLangOpts());
759 PO.SuppressTagKeyword = true;
760 PO.SuppressUnwrittenScope = true;
761 PO.ConstantArraySizeAsWritten = false;
762 PO.AnonymousTagLocations = false;
763 DNT->getQualifier()->print(Out, PO);
764 Out << ':' << DNT->getIdentifier()->getName();
767 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
768 T = InjT->getInjectedSpecializationType();
778 void USRGenerator::VisitTemplateParameterList(
779 const TemplateParameterList *Params) {
782 Out << '>' << Params->size();
783 for (TemplateParameterList::const_iterator P = Params->begin(),
784 PEnd = Params->end();
787 if (isa<TemplateTypeParmDecl>(*P)) {
788 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
794 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
795 if (NTTP->isParameterPack())
798 VisitType(NTTP->getType());
802 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
803 if (TTP->isParameterPack())
806 VisitTemplateParameterList(TTP->getTemplateParameters());
810 void USRGenerator::VisitTemplateName(TemplateName Name) {
811 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
812 if (TemplateTemplateParmDecl *TTP
813 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
814 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
822 // FIXME: Visit dependent template names.
825 void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
826 switch (Arg.getKind()) {
827 case TemplateArgument::Null:
830 case TemplateArgument::Declaration:
831 Visit(Arg.getAsDecl());
834 case TemplateArgument::NullPtr:
837 case TemplateArgument::TemplateExpansion:
838 Out << 'P'; // pack expansion of...
840 case TemplateArgument::Template:
841 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
844 case TemplateArgument::Expression:
845 // FIXME: Visit expressions.
848 case TemplateArgument::Pack:
849 Out << 'p' << Arg.pack_size();
850 for (const auto &P : Arg.pack_elements())
851 VisitTemplateArgument(P);
854 case TemplateArgument::Type:
855 VisitType(Arg.getAsType());
858 case TemplateArgument::Integral:
860 VisitType(Arg.getIntegralType());
861 Out << Arg.getAsIntegral();
866 //===----------------------------------------------------------------------===//
867 // USR generation functions.
868 //===----------------------------------------------------------------------===//
870 void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) {
871 OS << "objc(cs)" << Cls;
874 void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
876 OS << "objc(cy)" << Cls << '@' << Cat;
879 void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
883 void clang::index::generateUSRForObjCMethod(StringRef Sel,
884 bool IsInstanceMethod,
886 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
889 void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
891 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
894 void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) {
895 OS << "objc(pl)" << Prot;
898 bool clang::index::generateUSRForDecl(const Decl *D,
899 SmallVectorImpl<char> &Buf) {
902 // We don't ignore decls with invalid source locations. Implicit decls, like
903 // C++'s operator new function, can have invalid locations but it is fine to
904 // create USRs that can identify them.
906 USRGenerator UG(&D->getASTContext(), Buf);
908 return UG.ignoreResults();
911 bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
912 const SourceManager &SM,
913 SmallVectorImpl<char> &Buf) {
914 // Don't generate USRs for things with invalid locations.
915 if (!MD || MD->getLocation().isInvalid())
918 llvm::raw_svector_ostream Out(Buf);
920 // Assume that system headers are sane. Don't put source location
921 // information into the USR if the macro comes from a system header.
922 SourceLocation Loc = MD->getLocation();
923 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
925 Out << getUSRSpacePrefix();
926 if (ShouldGenerateLocation)
927 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
929 Out << MD->getName()->getName();