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