1 //===- USRGeneration.cpp - Routines for USR generation --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 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/ADT/SmallString.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 using namespace clang; 20 using namespace clang::index; 21 22 //===----------------------------------------------------------------------===// 23 // USR generation. 24 //===----------------------------------------------------------------------===// 25 26 /// \returns true on error. 27 static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, 28 const SourceManager &SM, bool IncludeOffset) { 29 if (Loc.isInvalid()) { 30 return true; 31 } 32 Loc = SM.getExpansionLoc(Loc); 33 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc); 34 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); 35 if (FE) { 36 OS << llvm::sys::path::filename(FE->getName()); 37 } else { 38 // This case really isn't interesting. 39 return true; 40 } 41 if (IncludeOffset) { 42 // Use the offest into the FileID to represent the location. Using 43 // a line/column can cause us to look back at the original source file, 44 // which is expensive. 45 OS << '@' << Decomposed.second; 46 } 47 return false; 48 } 49 50 namespace { 51 class USRGenerator : public ConstDeclVisitor<USRGenerator> { 52 SmallVectorImpl<char> &Buf; 53 llvm::raw_svector_ostream Out; 54 bool IgnoreResults; 55 ASTContext *Context; 56 bool generatedLoc; 57 58 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions; 59 60 public: 61 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf) 62 : Buf(Buf), 63 Out(Buf), 64 IgnoreResults(false), 65 Context(Ctx), 66 generatedLoc(false) 67 { 68 // Add the USR space prefix. 69 Out << getUSRSpacePrefix(); 70 } 71 72 bool ignoreResults() const { return IgnoreResults; } 73 74 // Visitation methods from generating USRs from AST elements. 75 void VisitDeclContext(const DeclContext *D); 76 void VisitFieldDecl(const FieldDecl *D); 77 void VisitFunctionDecl(const FunctionDecl *D); 78 void VisitNamedDecl(const NamedDecl *D); 79 void VisitNamespaceDecl(const NamespaceDecl *D); 80 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); 81 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); 82 void VisitClassTemplateDecl(const ClassTemplateDecl *D); 83 void VisitObjCContainerDecl(const ObjCContainerDecl *CD); 84 void VisitObjCMethodDecl(const ObjCMethodDecl *MD); 85 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); 86 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); 87 void VisitTagDecl(const TagDecl *D); 88 void VisitTypedefDecl(const TypedefDecl *D); 89 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); 90 void VisitVarDecl(const VarDecl *D); 91 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); 92 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); 93 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) { 94 IgnoreResults = true; 95 } 96 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 97 IgnoreResults = true; 98 } 99 void VisitUsingDecl(const UsingDecl *D) { 100 IgnoreResults = true; 101 } 102 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) { 103 IgnoreResults = true; 104 } 105 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) { 106 IgnoreResults = true; 107 } 108 109 bool ShouldGenerateLocation(const NamedDecl *D); 110 111 bool isLocal(const NamedDecl *D) { 112 return D->getParentFunctionOrMethod() != nullptr; 113 } 114 115 /// Generate the string component containing the location of the 116 /// declaration. 117 bool GenLoc(const Decl *D, bool IncludeOffset); 118 119 /// String generation methods used both by the visitation methods 120 /// and from other clients that want to directly generate USRs. These 121 /// methods do not construct complete USRs (which incorporate the parents 122 /// of an AST element), but only the fragments concerning the AST element 123 /// itself. 124 125 /// Generate a USR for an Objective-C class. 126 void GenObjCClass(StringRef cls) { 127 generateUSRForObjCClass(cls, Out); 128 } 129 /// Generate a USR for an Objective-C class category. 130 void GenObjCCategory(StringRef cls, StringRef cat) { 131 generateUSRForObjCCategory(cls, cat, Out); 132 } 133 /// Generate a USR fragment for an Objective-C property. 134 void GenObjCProperty(StringRef prop) { 135 generateUSRForObjCProperty(prop, Out); 136 } 137 /// Generate a USR for an Objective-C protocol. 138 void GenObjCProtocol(StringRef prot) { 139 generateUSRForObjCProtocol(prot, Out); 140 } 141 142 void VisitType(QualType T); 143 void VisitTemplateParameterList(const TemplateParameterList *Params); 144 void VisitTemplateName(TemplateName Name); 145 void VisitTemplateArgument(const TemplateArgument &Arg); 146 147 /// Emit a Decl's name using NamedDecl::printName() and return true if 148 /// the decl had no name. 149 bool EmitDeclName(const NamedDecl *D); 150 }; 151 152 } // end anonymous namespace 153 154 //===----------------------------------------------------------------------===// 155 // Generating USRs from ASTS. 156 //===----------------------------------------------------------------------===// 157 158 bool USRGenerator::EmitDeclName(const NamedDecl *D) { 159 const unsigned startSize = Buf.size(); 160 D->printName(Out); 161 const unsigned endSize = Buf.size(); 162 return startSize == endSize; 163 } 164 165 bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) { 166 if (D->isExternallyVisible()) 167 return false; 168 if (D->getParentFunctionOrMethod()) 169 return true; 170 const SourceManager &SM = Context->getSourceManager(); 171 return !SM.isInSystemHeader(D->getLocation()); 172 } 173 174 void USRGenerator::VisitDeclContext(const DeclContext *DC) { 175 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC)) 176 Visit(D); 177 } 178 179 void USRGenerator::VisitFieldDecl(const FieldDecl *D) { 180 // The USR for an ivar declared in a class extension is based on the 181 // ObjCInterfaceDecl, not the ObjCCategoryDecl. 182 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 183 Visit(ID); 184 else 185 VisitDeclContext(D->getDeclContext()); 186 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); 187 if (EmitDeclName(D)) { 188 // Bit fields can be anonymous. 189 IgnoreResults = true; 190 return; 191 } 192 } 193 194 void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) { 195 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 196 return; 197 198 VisitDeclContext(D->getDeclContext()); 199 bool IsTemplate = false; 200 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { 201 IsTemplate = true; 202 Out << "@FT@"; 203 VisitTemplateParameterList(FunTmpl->getTemplateParameters()); 204 } else 205 Out << "@F@"; 206 D->printName(Out); 207 208 ASTContext &Ctx = *Context; 209 if (!Ctx.getLangOpts().CPlusPlus || D->isExternC()) 210 return; 211 212 if (const TemplateArgumentList * 213 SpecArgs = D->getTemplateSpecializationArgs()) { 214 Out << '<'; 215 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) { 216 Out << '#'; 217 VisitTemplateArgument(SpecArgs->get(I)); 218 } 219 Out << '>'; 220 } 221 222 // Mangle in type information for the arguments. 223 for (auto PD : D->params()) { 224 Out << '#'; 225 VisitType(PD->getType()); 226 } 227 if (D->isVariadic()) 228 Out << '.'; 229 if (IsTemplate) { 230 // Function templates can be overloaded by return type, for example: 231 // \code 232 // template <class T> typename T::A foo() {} 233 // template <class T> typename T::B foo() {} 234 // \endcode 235 Out << '#'; 236 VisitType(D->getReturnType()); 237 } 238 Out << '#'; 239 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 240 if (MD->isStatic()) 241 Out << 'S'; 242 if (unsigned quals = MD->getTypeQualifiers()) 243 Out << (char)('0' + quals); 244 switch (MD->getRefQualifier()) { 245 case RQ_None: break; 246 case RQ_LValue: Out << '&'; break; 247 case RQ_RValue: Out << "&&"; break; 248 } 249 } 250 } 251 252 void USRGenerator::VisitNamedDecl(const NamedDecl *D) { 253 VisitDeclContext(D->getDeclContext()); 254 Out << "@"; 255 256 if (EmitDeclName(D)) { 257 // The string can be empty if the declaration has no name; e.g., it is 258 // the ParmDecl with no name for declaration of a function pointer type, 259 // e.g.: void (*f)(void *); 260 // In this case, don't generate a USR. 261 IgnoreResults = true; 262 } 263 } 264 265 void USRGenerator::VisitVarDecl(const VarDecl *D) { 266 // VarDecls can be declared 'extern' within a function or method body, 267 // but their enclosing DeclContext is the function, not the TU. We need 268 // to check the storage class to correctly generate the USR. 269 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 270 return; 271 272 VisitDeclContext(D->getDeclContext()); 273 274 // Variables always have simple names. 275 StringRef s = D->getName(); 276 277 // The string can be empty if the declaration has no name; e.g., it is 278 // the ParmDecl with no name for declaration of a function pointer type, e.g.: 279 // void (*f)(void *); 280 // In this case, don't generate a USR. 281 if (s.empty()) 282 IgnoreResults = true; 283 else 284 Out << '@' << s; 285 } 286 287 void USRGenerator::VisitNonTypeTemplateParmDecl( 288 const NonTypeTemplateParmDecl *D) { 289 GenLoc(D, /*IncludeOffset=*/true); 290 return; 291 } 292 293 void USRGenerator::VisitTemplateTemplateParmDecl( 294 const TemplateTemplateParmDecl *D) { 295 GenLoc(D, /*IncludeOffset=*/true); 296 return; 297 } 298 299 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) { 300 if (D->isAnonymousNamespace()) { 301 Out << "@aN"; 302 return; 303 } 304 305 VisitDeclContext(D->getDeclContext()); 306 if (!IgnoreResults) 307 Out << "@N@" << D->getName(); 308 } 309 310 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 311 VisitFunctionDecl(D->getTemplatedDecl()); 312 } 313 314 void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) { 315 VisitTagDecl(D->getTemplatedDecl()); 316 } 317 318 void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 319 VisitDeclContext(D->getDeclContext()); 320 if (!IgnoreResults) 321 Out << "@NA@" << D->getName(); 322 } 323 324 void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 325 const DeclContext *container = D->getDeclContext(); 326 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) { 327 Visit(pd); 328 } 329 else { 330 // The USR for a method declared in a class extension or category is based on 331 // the ObjCInterfaceDecl, not the ObjCCategoryDecl. 332 const ObjCInterfaceDecl *ID = D->getClassInterface(); 333 if (!ID) { 334 IgnoreResults = true; 335 return; 336 } 337 Visit(ID); 338 } 339 // Ideally we would use 'GenObjCMethod', but this is such a hot path 340 // for Objective-C code that we don't want to use 341 // DeclarationName::getAsString(). 342 Out << (D->isInstanceMethod() ? "(im)" : "(cm)") 343 << DeclarationName(D->getSelector()); 344 } 345 346 void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) { 347 switch (D->getKind()) { 348 default: 349 llvm_unreachable("Invalid ObjC container."); 350 case Decl::ObjCInterface: 351 case Decl::ObjCImplementation: 352 GenObjCClass(D->getName()); 353 break; 354 case Decl::ObjCCategory: { 355 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); 356 const ObjCInterfaceDecl *ID = CD->getClassInterface(); 357 if (!ID) { 358 // Handle invalid code where the @interface might not 359 // have been specified. 360 // FIXME: We should be able to generate this USR even if the 361 // @interface isn't available. 362 IgnoreResults = true; 363 return; 364 } 365 // Specially handle class extensions, which are anonymous categories. 366 // We want to mangle in the location to uniquely distinguish them. 367 if (CD->IsClassExtension()) { 368 Out << "objc(ext)" << ID->getName() << '@'; 369 GenLoc(CD, /*IncludeOffset=*/true); 370 } 371 else 372 GenObjCCategory(ID->getName(), CD->getName()); 373 374 break; 375 } 376 case Decl::ObjCCategoryImpl: { 377 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); 378 const ObjCInterfaceDecl *ID = CD->getClassInterface(); 379 if (!ID) { 380 // Handle invalid code where the @interface might not 381 // have been specified. 382 // FIXME: We should be able to generate this USR even if the 383 // @interface isn't available. 384 IgnoreResults = true; 385 return; 386 } 387 GenObjCCategory(ID->getName(), CD->getName()); 388 break; 389 } 390 case Decl::ObjCProtocol: 391 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); 392 break; 393 } 394 } 395 396 void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 397 // The USR for a property declared in a class extension or category is based 398 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl. 399 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 400 Visit(ID); 401 else 402 Visit(cast<Decl>(D->getDeclContext())); 403 GenObjCProperty(D->getName()); 404 } 405 406 void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 407 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { 408 VisitObjCPropertyDecl(PD); 409 return; 410 } 411 412 IgnoreResults = true; 413 } 414 415 void USRGenerator::VisitTagDecl(const TagDecl *D) { 416 // Add the location of the tag decl to handle resolution across 417 // translation units. 418 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 419 return; 420 421 D = D->getCanonicalDecl(); 422 VisitDeclContext(D->getDeclContext()); 423 424 bool AlreadyStarted = false; 425 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { 426 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) { 427 AlreadyStarted = true; 428 429 switch (D->getTagKind()) { 430 case TTK_Interface: 431 case TTK_Class: 432 case TTK_Struct: Out << "@ST"; break; 433 case TTK_Union: Out << "@UT"; break; 434 case TTK_Enum: llvm_unreachable("enum template"); 435 } 436 VisitTemplateParameterList(ClassTmpl->getTemplateParameters()); 437 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec 438 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) { 439 AlreadyStarted = true; 440 441 switch (D->getTagKind()) { 442 case TTK_Interface: 443 case TTK_Class: 444 case TTK_Struct: Out << "@SP"; break; 445 case TTK_Union: Out << "@UP"; break; 446 case TTK_Enum: llvm_unreachable("enum partial specialization"); 447 } 448 VisitTemplateParameterList(PartialSpec->getTemplateParameters()); 449 } 450 } 451 452 if (!AlreadyStarted) { 453 switch (D->getTagKind()) { 454 case TTK_Interface: 455 case TTK_Class: 456 case TTK_Struct: Out << "@S"; break; 457 case TTK_Union: Out << "@U"; break; 458 case TTK_Enum: Out << "@E"; break; 459 } 460 } 461 462 Out << '@'; 463 assert(Buf.size() > 0); 464 const unsigned off = Buf.size() - 1; 465 466 if (EmitDeclName(D)) { 467 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) { 468 Buf[off] = 'A'; 469 Out << '@' << *TD; 470 } 471 else { 472 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) { 473 printLoc(Out, D->getLocation(), Context->getSourceManager(), true); 474 } else 475 Buf[off] = 'a'; 476 } 477 } 478 479 // For a class template specialization, mangle the template arguments. 480 if (const ClassTemplateSpecializationDecl *Spec 481 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 482 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs(); 483 Out << '>'; 484 for (unsigned I = 0, N = Args.size(); I != N; ++I) { 485 Out << '#'; 486 VisitTemplateArgument(Args.get(I)); 487 } 488 } 489 } 490 491 void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) { 492 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 493 return; 494 const DeclContext *DC = D->getDeclContext(); 495 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) 496 Visit(DCN); 497 Out << "@T@"; 498 Out << D->getName(); 499 } 500 501 void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 502 GenLoc(D, /*IncludeOffset=*/true); 503 return; 504 } 505 506 bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) { 507 if (generatedLoc) 508 return IgnoreResults; 509 generatedLoc = true; 510 511 // Guard against null declarations in invalid code. 512 if (!D) { 513 IgnoreResults = true; 514 return true; 515 } 516 517 // Use the location of canonical decl. 518 D = D->getCanonicalDecl(); 519 520 IgnoreResults = 521 IgnoreResults || printLoc(Out, D->getLocStart(), 522 Context->getSourceManager(), IncludeOffset); 523 524 return IgnoreResults; 525 } 526 527 void USRGenerator::VisitType(QualType T) { 528 // This method mangles in USR information for types. It can possibly 529 // just reuse the naming-mangling logic used by codegen, although the 530 // requirements for USRs might not be the same. 531 ASTContext &Ctx = *Context; 532 533 do { 534 T = Ctx.getCanonicalType(T); 535 Qualifiers Q = T.getQualifiers(); 536 unsigned qVal = 0; 537 if (Q.hasConst()) 538 qVal |= 0x1; 539 if (Q.hasVolatile()) 540 qVal |= 0x2; 541 if (Q.hasRestrict()) 542 qVal |= 0x4; 543 if(qVal) 544 Out << ((char) ('0' + qVal)); 545 546 // Mangle in ObjC GC qualifiers? 547 548 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) { 549 Out << 'P'; 550 T = Expansion->getPattern(); 551 } 552 553 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 554 unsigned char c = '\0'; 555 switch (BT->getKind()) { 556 case BuiltinType::Void: 557 c = 'v'; break; 558 case BuiltinType::Bool: 559 c = 'b'; break; 560 case BuiltinType::UChar: 561 c = 'c'; break; 562 case BuiltinType::Char16: 563 c = 'q'; break; 564 case BuiltinType::Char32: 565 c = 'w'; break; 566 case BuiltinType::UShort: 567 c = 's'; break; 568 case BuiltinType::UInt: 569 c = 'i'; break; 570 case BuiltinType::ULong: 571 c = 'l'; break; 572 case BuiltinType::ULongLong: 573 c = 'k'; break; 574 case BuiltinType::UInt128: 575 c = 'j'; break; 576 case BuiltinType::Char_U: 577 case BuiltinType::Char_S: 578 c = 'C'; break; 579 case BuiltinType::SChar: 580 c = 'r'; break; 581 case BuiltinType::WChar_S: 582 case BuiltinType::WChar_U: 583 c = 'W'; break; 584 case BuiltinType::Short: 585 c = 'S'; break; 586 case BuiltinType::Int: 587 c = 'I'; break; 588 case BuiltinType::Long: 589 c = 'L'; break; 590 case BuiltinType::LongLong: 591 c = 'K'; break; 592 case BuiltinType::Int128: 593 c = 'J'; break; 594 case BuiltinType::Half: 595 c = 'h'; break; 596 case BuiltinType::Float: 597 c = 'f'; break; 598 case BuiltinType::Double: 599 c = 'd'; break; 600 case BuiltinType::LongDouble: 601 c = 'D'; break; 602 case BuiltinType::NullPtr: 603 c = 'n'; break; 604 #define BUILTIN_TYPE(Id, SingletonId) 605 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: 606 #include "clang/AST/BuiltinTypes.def" 607 case BuiltinType::Dependent: 608 case BuiltinType::OCLImage1d: 609 case BuiltinType::OCLImage1dArray: 610 case BuiltinType::OCLImage1dBuffer: 611 case BuiltinType::OCLImage2d: 612 case BuiltinType::OCLImage2dArray: 613 case BuiltinType::OCLImage3d: 614 case BuiltinType::OCLEvent: 615 case BuiltinType::OCLSampler: 616 IgnoreResults = true; 617 return; 618 case BuiltinType::ObjCId: 619 c = 'o'; break; 620 case BuiltinType::ObjCClass: 621 c = 'O'; break; 622 case BuiltinType::ObjCSel: 623 c = 'e'; break; 624 } 625 Out << c; 626 return; 627 } 628 629 // If we have already seen this (non-built-in) type, use a substitution 630 // encoding. 631 llvm::DenseMap<const Type *, unsigned>::iterator Substitution 632 = TypeSubstitutions.find(T.getTypePtr()); 633 if (Substitution != TypeSubstitutions.end()) { 634 Out << 'S' << Substitution->second << '_'; 635 return; 636 } else { 637 // Record this as a substitution. 638 unsigned Number = TypeSubstitutions.size(); 639 TypeSubstitutions[T.getTypePtr()] = Number; 640 } 641 642 if (const PointerType *PT = T->getAs<PointerType>()) { 643 Out << '*'; 644 T = PT->getPointeeType(); 645 continue; 646 } 647 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) { 648 Out << "&&"; 649 T = RT->getPointeeType(); 650 continue; 651 } 652 if (const ReferenceType *RT = T->getAs<ReferenceType>()) { 653 Out << '&'; 654 T = RT->getPointeeType(); 655 continue; 656 } 657 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { 658 Out << 'F'; 659 VisitType(FT->getReturnType()); 660 for (const auto &I : FT->param_types()) 661 VisitType(I); 662 if (FT->isVariadic()) 663 Out << '.'; 664 return; 665 } 666 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { 667 Out << 'B'; 668 T = BT->getPointeeType(); 669 continue; 670 } 671 if (const ComplexType *CT = T->getAs<ComplexType>()) { 672 Out << '<'; 673 T = CT->getElementType(); 674 continue; 675 } 676 if (const TagType *TT = T->getAs<TagType>()) { 677 Out << '$'; 678 VisitTagDecl(TT->getDecl()); 679 return; 680 } 681 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { 682 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 683 return; 684 } 685 if (const TemplateSpecializationType *Spec 686 = T->getAs<TemplateSpecializationType>()) { 687 Out << '>'; 688 VisitTemplateName(Spec->getTemplateName()); 689 Out << Spec->getNumArgs(); 690 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 691 VisitTemplateArgument(Spec->getArg(I)); 692 return; 693 } 694 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { 695 Out << '^'; 696 // FIXME: Encode the qualifier, don't just print it. 697 PrintingPolicy PO(Ctx.getLangOpts()); 698 PO.SuppressTagKeyword = true; 699 PO.SuppressUnwrittenScope = true; 700 PO.ConstantArraySizeAsWritten = false; 701 PO.AnonymousTagLocations = false; 702 DNT->getQualifier()->print(Out, PO); 703 Out << ':' << DNT->getIdentifier()->getName(); 704 return; 705 } 706 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) { 707 T = InjT->getInjectedSpecializationType(); 708 continue; 709 } 710 711 // Unhandled type. 712 Out << ' '; 713 break; 714 } while (true); 715 } 716 717 void USRGenerator::VisitTemplateParameterList( 718 const TemplateParameterList *Params) { 719 if (!Params) 720 return; 721 Out << '>' << Params->size(); 722 for (TemplateParameterList::const_iterator P = Params->begin(), 723 PEnd = Params->end(); 724 P != PEnd; ++P) { 725 Out << '#'; 726 if (isa<TemplateTypeParmDecl>(*P)) { 727 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack()) 728 Out<< 'p'; 729 Out << 'T'; 730 continue; 731 } 732 733 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { 734 if (NTTP->isParameterPack()) 735 Out << 'p'; 736 Out << 'N'; 737 VisitType(NTTP->getType()); 738 continue; 739 } 740 741 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); 742 if (TTP->isParameterPack()) 743 Out << 'p'; 744 Out << 't'; 745 VisitTemplateParameterList(TTP->getTemplateParameters()); 746 } 747 } 748 749 void USRGenerator::VisitTemplateName(TemplateName Name) { 750 if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 751 if (TemplateTemplateParmDecl *TTP 752 = dyn_cast<TemplateTemplateParmDecl>(Template)) { 753 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 754 return; 755 } 756 757 Visit(Template); 758 return; 759 } 760 761 // FIXME: Visit dependent template names. 762 } 763 764 void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { 765 switch (Arg.getKind()) { 766 case TemplateArgument::Null: 767 break; 768 769 case TemplateArgument::Declaration: 770 Visit(Arg.getAsDecl()); 771 break; 772 773 case TemplateArgument::NullPtr: 774 break; 775 776 case TemplateArgument::TemplateExpansion: 777 Out << 'P'; // pack expansion of... 778 // Fall through 779 case TemplateArgument::Template: 780 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); 781 break; 782 783 case TemplateArgument::Expression: 784 // FIXME: Visit expressions. 785 break; 786 787 case TemplateArgument::Pack: 788 Out << 'p' << Arg.pack_size(); 789 for (const auto &P : Arg.pack_elements()) 790 VisitTemplateArgument(P); 791 break; 792 793 case TemplateArgument::Type: 794 VisitType(Arg.getAsType()); 795 break; 796 797 case TemplateArgument::Integral: 798 Out << 'V'; 799 VisitType(Arg.getIntegralType()); 800 Out << Arg.getAsIntegral(); 801 break; 802 } 803 } 804 805 //===----------------------------------------------------------------------===// 806 // USR generation functions. 807 //===----------------------------------------------------------------------===// 808 809 void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) { 810 OS << "objc(cs)" << Cls; 811 } 812 813 void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat, 814 raw_ostream &OS) { 815 OS << "objc(cy)" << Cls << '@' << Cat; 816 } 817 818 void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) { 819 OS << '@' << Ivar; 820 } 821 822 void clang::index::generateUSRForObjCMethod(StringRef Sel, 823 bool IsInstanceMethod, 824 raw_ostream &OS) { 825 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel; 826 } 827 828 void clang::index::generateUSRForObjCProperty(StringRef Prop, raw_ostream &OS) { 829 OS << "(py)" << Prop; 830 } 831 832 void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) { 833 OS << "objc(pl)" << Prot; 834 } 835 836 bool clang::index::generateUSRForDecl(const Decl *D, 837 SmallVectorImpl<char> &Buf) { 838 // Don't generate USRs for things with invalid locations. 839 if (!D || D->getLocStart().isInvalid()) 840 return true; 841 842 USRGenerator UG(&D->getASTContext(), Buf); 843 UG.Visit(D); 844 return UG.ignoreResults(); 845 } 846 847 bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD, 848 const SourceManager &SM, 849 SmallVectorImpl<char> &Buf) { 850 // Don't generate USRs for things with invalid locations. 851 if (!MD || MD->getLocation().isInvalid()) 852 return true; 853 854 llvm::raw_svector_ostream Out(Buf); 855 856 // Assume that system headers are sane. Don't put source location 857 // information into the USR if the macro comes from a system header. 858 SourceLocation Loc = MD->getLocation(); 859 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc); 860 861 Out << getUSRSpacePrefix(); 862 if (ShouldGenerateLocation) 863 printLoc(Out, Loc, SM, /*IncludeOffset=*/true); 864 Out << "@macro@"; 865 Out << MD->getName()->getName(); 866 return false; 867 } 868 869