1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===// 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/IndexSymbol.h" 11 #include "clang/AST/DeclCXX.h" 12 #include "clang/AST/DeclObjC.h" 13 #include "clang/AST/DeclTemplate.h" 14 #include "clang/AST/PrettyPrinter.h" 15 #include "clang/Lex/MacroInfo.h" 16 17 using namespace clang; 18 using namespace clang::index; 19 20 /// \returns true if \c D is a subclass of 'XCTestCase'. 21 static bool isUnitTestCase(const ObjCInterfaceDecl *D) { 22 if (!D) 23 return false; 24 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 25 if (SuperD->getName() == "XCTestCase") 26 return true; 27 D = SuperD; 28 } 29 return false; 30 } 31 32 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has 33 /// no parameters, and its name starts with 'test'. 34 static bool isUnitTest(const ObjCMethodDecl *D) { 35 if (!D->parameters().empty()) 36 return false; 37 if (!D->getReturnType()->isVoidType()) 38 return false; 39 if (!D->getSelector().getNameForSlot(0).startswith("test")) 40 return false; 41 return isUnitTestCase(D->getClassInterface()); 42 } 43 44 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) { 45 if (D->hasAttr<IBOutletAttr>()) { 46 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated; 47 } else if (D->hasAttr<IBOutletCollectionAttr>()) { 48 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated; 49 PropSet |= (SymbolPropertySet)SymbolProperty::IBOutletCollection; 50 } 51 } 52 53 bool index::isFunctionLocalSymbol(const Decl *D) { 54 assert(D); 55 56 if (isa<ParmVarDecl>(D)) 57 return true; 58 59 if (isa<TemplateTemplateParmDecl>(D)) 60 return true; 61 62 if (isa<ObjCTypeParamDecl>(D)) 63 return true; 64 65 if (isa<UsingDirectiveDecl>(D)) 66 return false; 67 if (!D->getParentFunctionOrMethod()) 68 return false; 69 70 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 71 switch (ND->getFormalLinkage()) { 72 case NoLinkage: 73 case InternalLinkage: 74 return true; 75 case VisibleNoLinkage: 76 case UniqueExternalLinkage: 77 case ModuleInternalLinkage: 78 llvm_unreachable("Not a sema linkage"); 79 case ModuleLinkage: 80 case ExternalLinkage: 81 return false; 82 } 83 } 84 85 return true; 86 } 87 88 SymbolInfo index::getSymbolInfo(const Decl *D) { 89 assert(D); 90 SymbolInfo Info; 91 Info.Kind = SymbolKind::Unknown; 92 Info.SubKind = SymbolSubKind::None; 93 Info.Properties = SymbolPropertySet(); 94 Info.Lang = SymbolLanguage::C; 95 96 if (isFunctionLocalSymbol(D)) { 97 Info.Properties |= (SymbolPropertySet)SymbolProperty::Local; 98 } 99 100 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 101 switch (TD->getTagKind()) { 102 case TTK_Struct: 103 Info.Kind = SymbolKind::Struct; break; 104 case TTK_Union: 105 Info.Kind = SymbolKind::Union; break; 106 case TTK_Class: 107 Info.Kind = SymbolKind::Class; 108 Info.Lang = SymbolLanguage::CXX; 109 break; 110 case TTK_Interface: 111 Info.Kind = SymbolKind::Protocol; 112 Info.Lang = SymbolLanguage::CXX; 113 break; 114 case TTK_Enum: 115 Info.Kind = SymbolKind::Enum; break; 116 } 117 118 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) { 119 if (!CXXRec->isCLike()) { 120 Info.Lang = SymbolLanguage::CXX; 121 if (CXXRec->getDescribedClassTemplate()) { 122 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 123 } 124 } 125 } 126 127 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 128 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 129 Info.Properties |= 130 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization; 131 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 132 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 133 Info.Properties |= 134 (SymbolPropertySet)SymbolProperty::TemplateSpecialization; 135 } 136 137 } else if (auto *VD = dyn_cast<VarDecl>(D)) { 138 Info.Kind = SymbolKind::Variable; 139 if (isa<ParmVarDecl>(D)) { 140 Info.Kind = SymbolKind::Parameter; 141 } else if (isa<CXXRecordDecl>(D->getDeclContext())) { 142 Info.Kind = SymbolKind::StaticProperty; 143 Info.Lang = SymbolLanguage::CXX; 144 } 145 146 if (isa<VarTemplatePartialSpecializationDecl>(D)) { 147 Info.Lang = SymbolLanguage::CXX; 148 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 149 Info.Properties |= 150 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization; 151 } else if (isa<VarTemplateSpecializationDecl>(D)) { 152 Info.Lang = SymbolLanguage::CXX; 153 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 154 Info.Properties |= 155 (SymbolPropertySet)SymbolProperty::TemplateSpecialization; 156 } else if (VD->getDescribedVarTemplate()) { 157 Info.Lang = SymbolLanguage::CXX; 158 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 159 } 160 161 } else { 162 switch (D->getKind()) { 163 case Decl::Import: 164 Info.Kind = SymbolKind::Module; 165 break; 166 case Decl::Typedef: 167 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C 168 case Decl::Function: 169 Info.Kind = SymbolKind::Function; 170 break; 171 case Decl::Field: 172 Info.Kind = SymbolKind::Field; 173 if (const CXXRecordDecl * 174 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) { 175 if (!CXXRec->isCLike()) 176 Info.Lang = SymbolLanguage::CXX; 177 } 178 break; 179 case Decl::EnumConstant: 180 Info.Kind = SymbolKind::EnumConstant; break; 181 case Decl::ObjCInterface: 182 case Decl::ObjCImplementation: { 183 Info.Kind = SymbolKind::Class; 184 Info.Lang = SymbolLanguage::ObjC; 185 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D); 186 if (!ClsD) 187 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface(); 188 if (isUnitTestCase(ClsD)) 189 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest; 190 break; 191 } 192 case Decl::ObjCProtocol: 193 Info.Kind = SymbolKind::Protocol; 194 Info.Lang = SymbolLanguage::ObjC; 195 break; 196 case Decl::ObjCCategory: 197 case Decl::ObjCCategoryImpl: { 198 Info.Kind = SymbolKind::Extension; 199 Info.Lang = SymbolLanguage::ObjC; 200 const ObjCInterfaceDecl *ClsD = nullptr; 201 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D)) 202 ClsD = CatD->getClassInterface(); 203 else 204 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface(); 205 if (isUnitTestCase(ClsD)) 206 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest; 207 break; 208 } 209 case Decl::ObjCMethod: { 210 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D); 211 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod; 212 if (MD->isPropertyAccessor()) { 213 if (MD->param_size()) 214 Info.SubKind = SymbolSubKind::AccessorSetter; 215 else 216 Info.SubKind = SymbolSubKind::AccessorGetter; 217 } 218 Info.Lang = SymbolLanguage::ObjC; 219 if (isUnitTest(MD)) 220 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest; 221 if (D->hasAttr<IBActionAttr>()) 222 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated; 223 break; 224 } 225 case Decl::ObjCProperty: 226 Info.Kind = SymbolKind::InstanceProperty; 227 Info.Lang = SymbolLanguage::ObjC; 228 checkForIBOutlets(D, Info.Properties); 229 if (auto *Annot = D->getAttr<AnnotateAttr>()) { 230 if (Annot->getAnnotation() == "gk_inspectable") 231 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable; 232 } 233 break; 234 case Decl::ObjCIvar: 235 Info.Kind = SymbolKind::Field; 236 Info.Lang = SymbolLanguage::ObjC; 237 checkForIBOutlets(D, Info.Properties); 238 break; 239 case Decl::Namespace: 240 Info.Kind = SymbolKind::Namespace; 241 Info.Lang = SymbolLanguage::CXX; 242 break; 243 case Decl::NamespaceAlias: 244 Info.Kind = SymbolKind::NamespaceAlias; 245 Info.Lang = SymbolLanguage::CXX; 246 break; 247 case Decl::CXXConstructor: { 248 Info.Kind = SymbolKind::Constructor; 249 Info.Lang = SymbolLanguage::CXX; 250 auto *CD = cast<CXXConstructorDecl>(D); 251 if (CD->isCopyConstructor()) 252 Info.SubKind = SymbolSubKind::CXXCopyConstructor; 253 else if (CD->isMoveConstructor()) 254 Info.SubKind = SymbolSubKind::CXXMoveConstructor; 255 break; 256 } 257 case Decl::CXXDestructor: 258 Info.Kind = SymbolKind::Destructor; 259 Info.Lang = SymbolLanguage::CXX; 260 break; 261 case Decl::CXXConversion: 262 Info.Kind = SymbolKind::ConversionFunction; 263 Info.Lang = SymbolLanguage::CXX; 264 break; 265 case Decl::CXXMethod: { 266 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 267 if (MD->isStatic()) 268 Info.Kind = SymbolKind::StaticMethod; 269 else 270 Info.Kind = SymbolKind::InstanceMethod; 271 Info.Lang = SymbolLanguage::CXX; 272 break; 273 } 274 case Decl::ClassTemplate: 275 Info.Kind = SymbolKind::Class; 276 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 277 Info.Lang = SymbolLanguage::CXX; 278 break; 279 case Decl::FunctionTemplate: 280 Info.Kind = SymbolKind::Function; 281 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 282 Info.Lang = SymbolLanguage::CXX; 283 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 284 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 285 if (isa<CXXConstructorDecl>(MD)) 286 Info.Kind = SymbolKind::Constructor; 287 else if (isa<CXXDestructorDecl>(MD)) 288 Info.Kind = SymbolKind::Destructor; 289 else if (isa<CXXConversionDecl>(MD)) 290 Info.Kind = SymbolKind::ConversionFunction; 291 else { 292 if (MD->isStatic()) 293 Info.Kind = SymbolKind::StaticMethod; 294 else 295 Info.Kind = SymbolKind::InstanceMethod; 296 } 297 } 298 break; 299 case Decl::TypeAliasTemplate: 300 Info.Kind = SymbolKind::TypeAlias; 301 Info.Lang = SymbolLanguage::CXX; 302 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 303 break; 304 case Decl::TypeAlias: 305 Info.Kind = SymbolKind::TypeAlias; 306 Info.Lang = SymbolLanguage::CXX; 307 break; 308 case Decl::UnresolvedUsingTypename: 309 Info.Kind = SymbolKind::Using; 310 Info.SubKind = SymbolSubKind::UsingTypename; 311 Info.Lang = SymbolLanguage::CXX; 312 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 313 break; 314 case Decl::UnresolvedUsingValue: 315 Info.Kind = SymbolKind::Using; 316 Info.SubKind = SymbolSubKind::UsingValue; 317 Info.Lang = SymbolLanguage::CXX; 318 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 319 break; 320 case Decl::Binding: 321 Info.Kind = SymbolKind::Variable; 322 Info.Lang = SymbolLanguage::CXX; 323 break; 324 default: 325 break; 326 } 327 } 328 329 if (Info.Kind == SymbolKind::Unknown) 330 return Info; 331 332 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 333 if (FD->getTemplatedKind() == 334 FunctionDecl::TK_FunctionTemplateSpecialization) { 335 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic; 336 Info.Properties |= 337 (SymbolPropertySet)SymbolProperty::TemplateSpecialization; 338 } 339 } 340 341 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic) 342 Info.Lang = SymbolLanguage::CXX; 343 344 if (auto *attr = D->getExternalSourceSymbolAttr()) { 345 if (attr->getLanguage() == "Swift") 346 Info.Lang = SymbolLanguage::Swift; 347 } 348 349 return Info; 350 } 351 352 SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) { 353 SymbolInfo Info; 354 Info.Kind = SymbolKind::Macro; 355 Info.SubKind = SymbolSubKind::None; 356 Info.Properties = SymbolPropertySet(); 357 Info.Lang = SymbolLanguage::C; 358 return Info; 359 } 360 361 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, 362 llvm::function_ref<bool(SymbolRole)> Fn) { 363 #define APPLY_FOR_ROLE(Role) \ 364 if (Roles & (unsigned)SymbolRole::Role) \ 365 if (!Fn(SymbolRole::Role)) \ 366 return false; 367 368 APPLY_FOR_ROLE(Declaration); 369 APPLY_FOR_ROLE(Definition); 370 APPLY_FOR_ROLE(Reference); 371 APPLY_FOR_ROLE(Read); 372 APPLY_FOR_ROLE(Write); 373 APPLY_FOR_ROLE(Call); 374 APPLY_FOR_ROLE(Dynamic); 375 APPLY_FOR_ROLE(AddressOf); 376 APPLY_FOR_ROLE(Implicit); 377 APPLY_FOR_ROLE(Undefinition); 378 APPLY_FOR_ROLE(RelationChildOf); 379 APPLY_FOR_ROLE(RelationBaseOf); 380 APPLY_FOR_ROLE(RelationOverrideOf); 381 APPLY_FOR_ROLE(RelationReceivedBy); 382 APPLY_FOR_ROLE(RelationCalledBy); 383 APPLY_FOR_ROLE(RelationExtendedBy); 384 APPLY_FOR_ROLE(RelationAccessorOf); 385 APPLY_FOR_ROLE(RelationContainedBy); 386 APPLY_FOR_ROLE(RelationIBTypeOf); 387 APPLY_FOR_ROLE(RelationSpecializationOf); 388 389 #undef APPLY_FOR_ROLE 390 391 return true; 392 } 393 394 void index::applyForEachSymbolRole(SymbolRoleSet Roles, 395 llvm::function_ref<void(SymbolRole)> Fn) { 396 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool { 397 Fn(r); 398 return true; 399 }); 400 } 401 402 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) { 403 bool VisitedOnce = false; 404 applyForEachSymbolRole(Roles, [&](SymbolRole Role) { 405 if (VisitedOnce) 406 OS << ','; 407 else 408 VisitedOnce = true; 409 switch (Role) { 410 case SymbolRole::Declaration: OS << "Decl"; break; 411 case SymbolRole::Definition: OS << "Def"; break; 412 case SymbolRole::Reference: OS << "Ref"; break; 413 case SymbolRole::Read: OS << "Read"; break; 414 case SymbolRole::Write: OS << "Writ"; break; 415 case SymbolRole::Call: OS << "Call"; break; 416 case SymbolRole::Dynamic: OS << "Dyn"; break; 417 case SymbolRole::AddressOf: OS << "Addr"; break; 418 case SymbolRole::Implicit: OS << "Impl"; break; 419 case SymbolRole::Undefinition: OS << "Undef"; break; 420 case SymbolRole::RelationChildOf: OS << "RelChild"; break; 421 case SymbolRole::RelationBaseOf: OS << "RelBase"; break; 422 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break; 423 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break; 424 case SymbolRole::RelationCalledBy: OS << "RelCall"; break; 425 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break; 426 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break; 427 case SymbolRole::RelationContainedBy: OS << "RelCont"; break; 428 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break; 429 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break; 430 } 431 }); 432 } 433 434 bool index::printSymbolName(const Decl *D, const LangOptions &LO, 435 raw_ostream &OS) { 436 if (auto *ND = dyn_cast<NamedDecl>(D)) { 437 PrintingPolicy Policy(LO); 438 // Forward references can have different template argument names. Suppress 439 // the template argument names in constructors to make their name more 440 // stable. 441 Policy.SuppressTemplateArgsInCXXConstructors = true; 442 DeclarationName DeclName = ND->getDeclName(); 443 if (DeclName.isEmpty()) 444 return true; 445 DeclName.print(OS, Policy); 446 return false; 447 } else { 448 return true; 449 } 450 } 451 452 StringRef index::getSymbolKindString(SymbolKind K) { 453 switch (K) { 454 case SymbolKind::Unknown: return "<unknown>"; 455 case SymbolKind::Module: return "module"; 456 case SymbolKind::Namespace: return "namespace"; 457 case SymbolKind::NamespaceAlias: return "namespace-alias"; 458 case SymbolKind::Macro: return "macro"; 459 case SymbolKind::Enum: return "enum"; 460 case SymbolKind::Struct: return "struct"; 461 case SymbolKind::Class: return "class"; 462 case SymbolKind::Protocol: return "protocol"; 463 case SymbolKind::Extension: return "extension"; 464 case SymbolKind::Union: return "union"; 465 case SymbolKind::TypeAlias: return "type-alias"; 466 case SymbolKind::Function: return "function"; 467 case SymbolKind::Variable: return "variable"; 468 case SymbolKind::Field: return "field"; 469 case SymbolKind::EnumConstant: return "enumerator"; 470 case SymbolKind::InstanceMethod: return "instance-method"; 471 case SymbolKind::ClassMethod: return "class-method"; 472 case SymbolKind::StaticMethod: return "static-method"; 473 case SymbolKind::InstanceProperty: return "instance-property"; 474 case SymbolKind::ClassProperty: return "class-property"; 475 case SymbolKind::StaticProperty: return "static-property"; 476 case SymbolKind::Constructor: return "constructor"; 477 case SymbolKind::Destructor: return "destructor"; 478 case SymbolKind::ConversionFunction: return "coversion-func"; 479 case SymbolKind::Parameter: return "param"; 480 case SymbolKind::Using: return "using"; 481 } 482 llvm_unreachable("invalid symbol kind"); 483 } 484 485 StringRef index::getSymbolSubKindString(SymbolSubKind K) { 486 switch (K) { 487 case SymbolSubKind::None: return "<none>"; 488 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor"; 489 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor"; 490 case SymbolSubKind::AccessorGetter: return "acc-get"; 491 case SymbolSubKind::AccessorSetter: return "acc-set"; 492 case SymbolSubKind::UsingTypename: return "using-typename"; 493 case SymbolSubKind::UsingValue: return "using-value"; 494 } 495 llvm_unreachable("invalid symbol subkind"); 496 } 497 498 StringRef index::getSymbolLanguageString(SymbolLanguage K) { 499 switch (K) { 500 case SymbolLanguage::C: return "C"; 501 case SymbolLanguage::ObjC: return "ObjC"; 502 case SymbolLanguage::CXX: return "C++"; 503 case SymbolLanguage::Swift: return "Swift"; 504 } 505 llvm_unreachable("invalid symbol language kind"); 506 } 507 508 void index::applyForEachSymbolProperty(SymbolPropertySet Props, 509 llvm::function_ref<void(SymbolProperty)> Fn) { 510 #define APPLY_FOR_PROPERTY(K) \ 511 if (Props & (SymbolPropertySet)SymbolProperty::K) \ 512 Fn(SymbolProperty::K) 513 514 APPLY_FOR_PROPERTY(Generic); 515 APPLY_FOR_PROPERTY(TemplatePartialSpecialization); 516 APPLY_FOR_PROPERTY(TemplateSpecialization); 517 APPLY_FOR_PROPERTY(UnitTest); 518 APPLY_FOR_PROPERTY(IBAnnotated); 519 APPLY_FOR_PROPERTY(IBOutletCollection); 520 APPLY_FOR_PROPERTY(GKInspectable); 521 APPLY_FOR_PROPERTY(Local); 522 523 #undef APPLY_FOR_PROPERTY 524 } 525 526 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) { 527 bool VisitedOnce = false; 528 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) { 529 if (VisitedOnce) 530 OS << ','; 531 else 532 VisitedOnce = true; 533 switch (Prop) { 534 case SymbolProperty::Generic: OS << "Gen"; break; 535 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break; 536 case SymbolProperty::TemplateSpecialization: OS << "TS"; break; 537 case SymbolProperty::UnitTest: OS << "test"; break; 538 case SymbolProperty::IBAnnotated: OS << "IB"; break; 539 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break; 540 case SymbolProperty::GKInspectable: OS << "GKI"; break; 541 case SymbolProperty::Local: OS << "local"; break; 542 } 543 }); 544 } 545