1 //===- CIndexHigh.cpp - Higher level API functions ------------------------===// 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 "IndexingContext.h" 11 #include "CXTranslationUnit.h" 12 #include "CIndexDiagnostic.h" 13 14 #include "clang/Frontend/ASTUnit.h" 15 #include "clang/AST/DeclCXX.h" 16 #include "clang/AST/DeclTemplate.h" 17 18 using namespace clang; 19 using namespace cxindex; 20 using namespace cxcursor; 21 22 IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo( 23 const ObjCProtocolList &ProtList, 24 IndexingContext &IdxCtx, 25 ScratchAlloc &SA) { 26 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin(); 27 for (ObjCInterfaceDecl::protocol_iterator 28 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) { 29 SourceLocation Loc = *LI; 30 ObjCProtocolDecl *PD = *I; 31 ProtEntities.push_back(EntityInfo()); 32 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA); 33 CXIdxObjCProtocolRefInfo ProtInfo = { 0, 34 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU), 35 IdxCtx.getIndexLoc(Loc) }; 36 ProtInfos.push_back(ProtInfo); 37 38 if (IdxCtx.shouldSuppressRefs()) 39 IdxCtx.markEntityOccurrenceInFile(PD, Loc); 40 } 41 42 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 43 ProtInfos[i].protocol = &ProtEntities[i]; 44 45 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i) 46 Prots.push_back(&ProtInfos[i]); 47 } 48 49 50 IBOutletCollectionInfo::IBOutletCollectionInfo( 51 const IBOutletCollectionInfo &other) 52 : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) { 53 54 IBCollInfo.attrInfo = this; 55 IBCollInfo.classCursor = other.IBCollInfo.classCursor; 56 IBCollInfo.classLoc = other.IBCollInfo.classLoc; 57 if (other.IBCollInfo.objcClass) { 58 ClassInfo = other.ClassInfo; 59 IBCollInfo.objcClass = &ClassInfo; 60 } else 61 IBCollInfo.objcClass = 0; 62 } 63 64 AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx) 65 : SA(IdxCtx), ref_cnt(0) { 66 67 if (!D->hasAttrs()) 68 return; 69 70 for (AttrVec::const_iterator AttrI = D->attr_begin(), AttrE = D->attr_end(); 71 AttrI != AttrE; ++AttrI) { 72 const Attr *A = *AttrI; 73 CXCursor C = MakeCXCursor(A, const_cast<Decl *>(D), IdxCtx.CXTU); 74 CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation()); 75 switch (C.kind) { 76 default: 77 Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A)); 78 break; 79 case CXCursor_IBActionAttr: 80 Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A)); 81 break; 82 case CXCursor_IBOutletAttr: 83 Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A)); 84 break; 85 case CXCursor_IBOutletCollectionAttr: 86 IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A)); 87 break; 88 } 89 } 90 91 for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) { 92 IBOutletCollectionInfo &IBInfo = IBCollAttrs[i]; 93 CXAttrs.push_back(&IBInfo); 94 95 const IBOutletCollectionAttr * 96 IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A); 97 IBInfo.IBCollInfo.attrInfo = &IBInfo; 98 IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(IBAttr->getInterfaceLoc()); 99 IBInfo.IBCollInfo.objcClass = 0; 100 IBInfo.IBCollInfo.classCursor = clang_getNullCursor(); 101 QualType Ty = IBAttr->getInterface(); 102 if (const ObjCInterfaceType *InterTy = Ty->getAs<ObjCInterfaceType>()) { 103 if (const ObjCInterfaceDecl *InterD = InterTy->getInterface()) { 104 IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA); 105 IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo; 106 IBInfo.IBCollInfo.classCursor = MakeCursorObjCClassRef(InterD, 107 IBAttr->getInterfaceLoc(), IdxCtx.CXTU); 108 } 109 } 110 } 111 112 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) 113 CXAttrs.push_back(&Attrs[i]); 114 } 115 116 IntrusiveRefCntPtr<AttrListInfo> 117 AttrListInfo::create(const Decl *D, IndexingContext &IdxCtx) { 118 ScratchAlloc SA(IdxCtx); 119 AttrListInfo *attrs = SA.allocate<AttrListInfo>(); 120 return new (attrs) AttrListInfo(D, IdxCtx); 121 } 122 123 IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D, 124 IndexingContext &IdxCtx, 125 ScratchAlloc &SA) { 126 for (CXXRecordDecl::base_class_const_iterator 127 I = D->bases_begin(), E = D->bases_end(); I != E; ++I) { 128 const CXXBaseSpecifier &Base = *I; 129 BaseEntities.push_back(EntityInfo()); 130 const NamedDecl *BaseD = 0; 131 QualType T = Base.getType(); 132 SourceLocation Loc = getBaseLoc(Base); 133 134 if (const TypedefType *TDT = T->getAs<TypedefType>()) { 135 BaseD = TDT->getDecl(); 136 } else if (const TemplateSpecializationType * 137 TST = T->getAs<TemplateSpecializationType>()) { 138 BaseD = TST->getTemplateName().getAsTemplateDecl(); 139 } else if (const RecordType *RT = T->getAs<RecordType>()) { 140 BaseD = RT->getDecl(); 141 } 142 143 if (BaseD) 144 IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA); 145 CXIdxBaseClassInfo BaseInfo = { 0, 146 MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU), 147 IdxCtx.getIndexLoc(Loc) }; 148 BaseInfos.push_back(BaseInfo); 149 } 150 151 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) { 152 if (BaseEntities[i].name && BaseEntities[i].USR) 153 BaseInfos[i].base = &BaseEntities[i]; 154 } 155 156 for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) 157 CXBases.push_back(&BaseInfos[i]); 158 } 159 160 SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc( 161 const CXXBaseSpecifier &Base) const { 162 SourceLocation Loc = Base.getSourceRange().getBegin(); 163 TypeLoc TL; 164 if (Base.getTypeSourceInfo()) 165 TL = Base.getTypeSourceInfo()->getTypeLoc(); 166 if (TL.isNull()) 167 return Loc; 168 169 if (const QualifiedTypeLoc *QL = dyn_cast<QualifiedTypeLoc>(&TL)) 170 TL = QL->getUnqualifiedLoc(); 171 172 if (const ElaboratedTypeLoc *EL = dyn_cast<ElaboratedTypeLoc>(&TL)) 173 return EL->getNamedTypeLoc().getBeginLoc(); 174 if (const DependentNameTypeLoc *DL = dyn_cast<DependentNameTypeLoc>(&TL)) 175 return DL->getNameLoc(); 176 if (const DependentTemplateSpecializationTypeLoc * 177 DTL = dyn_cast<DependentTemplateSpecializationTypeLoc>(&TL)) 178 return DTL->getTemplateNameLoc(); 179 180 return Loc; 181 } 182 183 const char *ScratchAlloc::toCStr(StringRef Str) { 184 if (Str.empty()) 185 return ""; 186 if (Str.data()[Str.size()] == '\0') 187 return Str.data(); 188 return copyCStr(Str); 189 } 190 191 const char *ScratchAlloc::copyCStr(StringRef Str) { 192 char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1); 193 std::uninitialized_copy(Str.begin(), Str.end(), buf); 194 buf[Str.size()] = '\0'; 195 return buf; 196 } 197 198 void IndexingContext::setASTContext(ASTContext &ctx) { 199 Ctx = &ctx; 200 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx); 201 } 202 203 void IndexingContext::setPreprocessor(Preprocessor &PP) { 204 static_cast<ASTUnit*>(CXTU->TUData)->setPreprocessor(&PP); 205 } 206 207 bool IndexingContext::shouldAbort() { 208 if (!CB.abortQuery) 209 return false; 210 return CB.abortQuery(ClientData, 0); 211 } 212 213 void IndexingContext::enteredMainFile(const FileEntry *File) { 214 if (File && CB.enteredMainFile) { 215 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0); 216 FileMap[File] = idxFile; 217 } 218 } 219 220 void IndexingContext::ppIncludedFile(SourceLocation hashLoc, 221 StringRef filename, 222 const FileEntry *File, 223 bool isImport, bool isAngled) { 224 if (!CB.ppIncludedFile) 225 return; 226 227 ScratchAlloc SA(*this); 228 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), 229 SA.toCStr(filename), 230 (CXFile)File, 231 isImport, isAngled }; 232 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); 233 FileMap[File] = idxFile; 234 } 235 236 void IndexingContext::startedTranslationUnit() { 237 CXIdxClientContainer idxCont = 0; 238 if (CB.startedTranslationUnit) 239 idxCont = CB.startedTranslationUnit(ClientData, 0); 240 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont); 241 } 242 243 void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) { 244 if (!CB.diagnostic) 245 return; 246 247 CB.diagnostic(ClientData, CXDiagSet, 0); 248 } 249 250 bool IndexingContext::handleDecl(const NamedDecl *D, 251 SourceLocation Loc, CXCursor Cursor, 252 DeclInfo &DInfo, 253 const DeclContext *LexicalDC) { 254 if (!CB.indexDeclaration || !D) 255 return false; 256 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 257 return false; 258 259 ScratchAlloc SA(*this); 260 getEntityInfo(D, DInfo.EntInfo, SA); 261 if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR) 262 || Loc.isInvalid()) 263 return false; 264 265 if (!LexicalDC) 266 LexicalDC = D->getLexicalDeclContext(); 267 268 if (shouldSuppressRefs()) 269 markEntityOccurrenceInFile(D, Loc); 270 271 DInfo.entityInfo = &DInfo.EntInfo; 272 DInfo.cursor = Cursor; 273 DInfo.loc = getIndexLoc(Loc); 274 DInfo.isImplicit = D->isImplicit(); 275 276 DInfo.attributes = DInfo.EntInfo.attributes; 277 DInfo.numAttributes = DInfo.EntInfo.numAttributes; 278 279 getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer); 280 DInfo.semanticContainer = &DInfo.SemanticContainer; 281 282 if (LexicalDC == D->getDeclContext()) { 283 DInfo.lexicalContainer = &DInfo.SemanticContainer; 284 } else if (isTemplateImplicitInstantiation(D)) { 285 // Implicit instantiations have the lexical context of where they were 286 // instantiated first. We choose instead the semantic context because: 287 // 1) at the time that we see the instantiation we have not seen the 288 // function where it occurred yet. 289 // 2) the lexical context of the first instantiation is not useful 290 // information anyway. 291 DInfo.lexicalContainer = &DInfo.SemanticContainer; 292 } else { 293 getContainerInfo(LexicalDC, DInfo.LexicalContainer); 294 DInfo.lexicalContainer = &DInfo.LexicalContainer; 295 } 296 297 if (DInfo.isContainer) { 298 getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer); 299 DInfo.declAsContainer = &DInfo.DeclAsContainer; 300 } 301 302 CB.indexDeclaration(ClientData, &DInfo); 303 return true; 304 } 305 306 bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D, 307 SourceLocation Loc, CXCursor Cursor, 308 ObjCContainerDeclInfo &ContDInfo) { 309 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo; 310 return handleDecl(D, Loc, Cursor, ContDInfo); 311 } 312 313 bool IndexingContext::handleFunction(const FunctionDecl *D) { 314 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 315 D->isThisDeclarationADefinition()); 316 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 317 } 318 319 bool IndexingContext::handleVar(const VarDecl *D) { 320 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 321 /*isContainer=*/false); 322 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 323 } 324 325 bool IndexingContext::handleField(const FieldDecl *D) { 326 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 327 /*isContainer=*/false); 328 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 329 } 330 331 bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) { 332 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true, 333 /*isContainer=*/false); 334 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 335 } 336 337 bool IndexingContext::handleTagDecl(const TagDecl *D) { 338 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D)) 339 return handleCXXRecordDecl(CXXRD, D); 340 341 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(), 342 D->isThisDeclarationADefinition()); 343 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 344 } 345 346 bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) { 347 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true, 348 /*isContainer=*/false); 349 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 350 } 351 352 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) { 353 // For @class forward declarations, suppress them the same way as references. 354 if (!D->isThisDeclarationADefinition()) { 355 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation())) 356 return false; // already occurred. 357 358 // FIXME: This seems like the wrong definition for redeclaration. 359 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl(); 360 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration, 361 /*isImplementation=*/false); 362 return handleObjCContainer(D, D->getLocation(), 363 MakeCursorObjCClassRef(D, D->getLocation(), 364 CXTU), 365 ContDInfo); 366 } 367 368 ScratchAlloc SA(*this); 369 370 CXIdxBaseClassInfo BaseClass; 371 EntityInfo BaseEntity; 372 BaseClass.cursor = clang_getNullCursor(); 373 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) { 374 getEntityInfo(SuperD, BaseEntity, SA); 375 SourceLocation SuperLoc = D->getSuperClassLoc(); 376 BaseClass.base = &BaseEntity; 377 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU); 378 BaseClass.loc = getIndexLoc(SuperLoc); 379 380 if (shouldSuppressRefs()) 381 markEntityOccurrenceInFile(SuperD, SuperLoc); 382 } 383 384 ObjCProtocolList EmptyProtoList; 385 ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition() 386 ? D->getReferencedProtocols() 387 : EmptyProtoList, 388 *this, SA); 389 390 ObjCInterfaceDeclInfo InterInfo(D); 391 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 392 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo; 393 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0; 394 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo; 395 396 return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo); 397 } 398 399 bool IndexingContext::handleObjCImplementation( 400 const ObjCImplementationDecl *D) { 401 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false, 402 /*isRedeclaration=*/true, 403 /*isImplementation=*/true); 404 return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo); 405 } 406 407 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) { 408 if (!D->isThisDeclarationADefinition()) { 409 if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation())) 410 return false; // already occurred. 411 412 // FIXME: This seems like the wrong definition for redeclaration. 413 bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl(); 414 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, 415 isRedeclaration, 416 /*isImplementation=*/false); 417 return handleObjCContainer(D, D->getLocation(), 418 MakeCursorObjCProtocolRef(D, D->getLocation(), 419 CXTU), 420 ContDInfo); 421 } 422 423 ScratchAlloc SA(*this); 424 ObjCProtocolList EmptyProtoList; 425 ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition() 426 ? D->getReferencedProtocols() 427 : EmptyProtoList, 428 *this, SA); 429 430 ObjCProtocolDeclInfo ProtInfo(D); 431 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo(); 432 433 return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo); 434 } 435 436 bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) { 437 ScratchAlloc SA(*this); 438 439 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false); 440 EntityInfo ClassEntity; 441 const ObjCInterfaceDecl *IFaceD = D->getClassInterface(); 442 SourceLocation ClassLoc = D->getLocation(); 443 SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc 444 : D->getCategoryNameLoc(); 445 getEntityInfo(IFaceD, ClassEntity, SA); 446 447 if (shouldSuppressRefs()) 448 markEntityOccurrenceInFile(IFaceD, ClassLoc); 449 450 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA); 451 452 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 453 if (IFaceD) { 454 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 455 CatDInfo.ObjCCatDeclInfo.classCursor = 456 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 457 } else { 458 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 459 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 460 } 461 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 462 CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo(); 463 CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo; 464 465 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 466 } 467 468 bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) { 469 ScratchAlloc SA(*this); 470 471 const ObjCCategoryDecl *CatD = D->getCategoryDecl(); 472 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true); 473 EntityInfo ClassEntity; 474 const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface(); 475 SourceLocation ClassLoc = D->getLocation(); 476 SourceLocation CategoryLoc = D->getCategoryNameLoc(); 477 getEntityInfo(IFaceD, ClassEntity, SA); 478 479 if (shouldSuppressRefs()) 480 markEntityOccurrenceInFile(IFaceD, ClassLoc); 481 482 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo; 483 if (IFaceD) { 484 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity; 485 CatDInfo.ObjCCatDeclInfo.classCursor = 486 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU); 487 } else { 488 CatDInfo.ObjCCatDeclInfo.objcClass = 0; 489 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor(); 490 } 491 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc); 492 CatDInfo.ObjCCatDeclInfo.protocols = 0; 493 494 return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo); 495 } 496 497 bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) { 498 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(), 499 D->isThisDeclarationADefinition()); 500 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 501 } 502 503 bool IndexingContext::handleSynthesizedObjCProperty( 504 const ObjCPropertyImplDecl *D) { 505 ObjCPropertyDecl *PD = D->getPropertyDecl(); 506 return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext()); 507 } 508 509 bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D, 510 SourceLocation Loc, 511 const DeclContext *LexicalDC) { 512 DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true, 513 /*isContainer=*/false); 514 return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC); 515 } 516 517 bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) { 518 ScratchAlloc SA(*this); 519 520 ObjCPropertyDeclInfo DInfo; 521 EntityInfo GetterEntity; 522 EntityInfo SetterEntity; 523 524 DInfo.ObjCPropDeclInfo.declInfo = &DInfo; 525 526 if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) { 527 getEntityInfo(Getter, GetterEntity, SA); 528 DInfo.ObjCPropDeclInfo.getter = &GetterEntity; 529 } else { 530 DInfo.ObjCPropDeclInfo.getter = 0; 531 } 532 if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) { 533 getEntityInfo(Setter, SetterEntity, SA); 534 DInfo.ObjCPropDeclInfo.setter = &SetterEntity; 535 } else { 536 DInfo.ObjCPropDeclInfo.setter = 0; 537 } 538 539 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 540 } 541 542 bool IndexingContext::handleNamespace(const NamespaceDecl *D) { 543 DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(), 544 /*isDefinition=*/true, 545 /*isContainer=*/true); 546 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 547 } 548 549 bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) { 550 return handleCXXRecordDecl(D->getTemplatedDecl(), D); 551 } 552 553 bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) { 554 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 555 /*isDefinition=*/D->isThisDeclarationADefinition(), 556 /*isContainer=*/D->isThisDeclarationADefinition()); 557 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 558 } 559 560 bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) { 561 DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(), 562 /*isDefinition=*/true, /*isContainer=*/false); 563 return handleDecl(D, D->getLocation(), getCursor(D), DInfo); 564 } 565 566 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 567 const NamedDecl *Parent, 568 const DeclContext *DC, 569 const Expr *E, 570 CXIdxEntityRefKind Kind) { 571 if (!D) 572 return false; 573 574 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E), 575 const_cast<Decl*>(cast<Decl>(DC)), CXTU) 576 : getRefCursor(D, Loc); 577 return handleReference(D, Loc, Cursor, Parent, DC, E, Kind); 578 } 579 580 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, 581 CXCursor Cursor, 582 const NamedDecl *Parent, 583 const DeclContext *DC, 584 const Expr *E, 585 CXIdxEntityRefKind Kind) { 586 if (!CB.indexEntityReference) 587 return false; 588 589 if (!D) 590 return false; 591 if (Loc.isInvalid()) 592 return false; 593 if (!shouldIndexFunctionLocalSymbols() && D->getParentFunctionOrMethod()) 594 return false; 595 if (isNotFromSourceFile(D->getLocation())) 596 return false; 597 if (D->isImplicit() && shouldIgnoreIfImplicit(D)) 598 return false; 599 600 if (shouldSuppressRefs()) { 601 if (markEntityOccurrenceInFile(D, Loc)) 602 return false; // already occurred. 603 } 604 605 ScratchAlloc SA(*this); 606 EntityInfo RefEntity, ParentEntity; 607 getEntityInfo(D, RefEntity, SA); 608 if (!RefEntity.USR) 609 return false; 610 611 getEntityInfo(Parent, ParentEntity, SA); 612 613 ContainerInfo Container; 614 getContainerInfo(DC, Container); 615 616 CXIdxEntityRefInfo Info = { Kind, 617 Cursor, 618 getIndexLoc(Loc), 619 &RefEntity, 620 Parent ? &ParentEntity : 0, 621 &Container }; 622 CB.indexEntityReference(ClientData, &Info); 623 return true; 624 } 625 626 bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const { 627 if (Loc.isInvalid()) 628 return true; 629 SourceManager &SM = Ctx->getSourceManager(); 630 SourceLocation FileLoc = SM.getFileLoc(Loc); 631 FileID FID = SM.getFileID(FileLoc); 632 return SM.getFileEntryForID(FID) == 0; 633 } 634 635 void IndexingContext::addContainerInMap(const DeclContext *DC, 636 CXIdxClientContainer container) { 637 if (!DC) 638 return; 639 640 ContainerMapTy::iterator I = ContainerMap.find(DC); 641 if (I == ContainerMap.end()) { 642 if (container) 643 ContainerMap[DC] = container; 644 return; 645 } 646 // Allow changing the container of a previously seen DeclContext so we 647 // can handle invalid user code, like a function re-definition. 648 if (container) 649 I->second = container; 650 else 651 ContainerMap.erase(I); 652 } 653 654 CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const { 655 if (!D) 656 return 0; 657 EntityMapTy::const_iterator I = EntityMap.find(D); 658 if (I == EntityMap.end()) 659 return 0; 660 return I->second; 661 } 662 663 void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) { 664 if (!D) 665 return; 666 EntityMap[D] = client; 667 } 668 669 bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD, 670 const NamedDecl *OrigD) { 671 if (RD->isThisDeclarationADefinition()) { 672 ScratchAlloc SA(*this); 673 CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 674 /*isDefinition=*/RD->isThisDeclarationADefinition()); 675 CXXBasesListInfo BaseList(RD, *this, SA); 676 CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo; 677 CXXDInfo.CXXClassInfo.bases = BaseList.getBases(); 678 CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases(); 679 680 if (shouldSuppressRefs()) { 681 // Go through bases and mark them as referenced. 682 for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) { 683 const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i]; 684 if (baseInfo->base) { 685 const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl; 686 SourceLocation 687 Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data); 688 markEntityOccurrenceInFile(BaseD, Loc); 689 } 690 } 691 } 692 693 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo); 694 } 695 696 DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(), 697 /*isDefinition=*/RD->isThisDeclarationADefinition(), 698 /*isContainer=*/RD->isThisDeclarationADefinition()); 699 return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo); 700 } 701 702 bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D, 703 SourceLocation Loc) { 704 if (!D || Loc.isInvalid()) 705 return true; 706 707 SourceManager &SM = Ctx->getSourceManager(); 708 D = getEntityDecl(D); 709 710 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc)); 711 FileID FID = LocInfo.first; 712 if (FID.isInvalid()) 713 return true; 714 715 const FileEntry *FE = SM.getFileEntryForID(FID); 716 if (!FE) 717 return true; 718 RefFileOccurence RefOccur(FE, D); 719 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool> 720 res = RefFileOccurences.insert(RefOccur); 721 if (!res.second) 722 return true; // already in map. 723 724 return false; 725 } 726 727 const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const { 728 assert(D); 729 D = cast<NamedDecl>(D->getCanonicalDecl()); 730 731 if (const ObjCImplementationDecl * 732 ImplD = dyn_cast<ObjCImplementationDecl>(D)) { 733 return getEntityDecl(ImplD->getClassInterface()); 734 735 } else if (const ObjCCategoryImplDecl * 736 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) { 737 return getEntityDecl(CatImplD->getCategoryDecl()); 738 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 739 if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate()) 740 return getEntityDecl(TemplD); 741 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { 742 if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate()) 743 return getEntityDecl(TemplD); 744 } 745 746 return D; 747 } 748 749 const DeclContext * 750 IndexingContext::getEntityContainer(const Decl *D) const { 751 const DeclContext *DC = dyn_cast<DeclContext>(D); 752 if (DC) 753 return DC; 754 755 if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) { 756 DC = ClassTempl->getTemplatedDecl(); 757 } if (const FunctionTemplateDecl * 758 FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) { 759 DC = FuncTempl->getTemplatedDecl(); 760 } 761 762 return DC; 763 } 764 765 CXIdxClientContainer 766 IndexingContext::getClientContainerForDC(const DeclContext *DC) const { 767 if (!DC) 768 return 0; 769 770 ContainerMapTy::const_iterator I = ContainerMap.find(DC); 771 if (I == ContainerMap.end()) 772 return 0; 773 774 return I->second; 775 } 776 777 CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) { 778 if (!File) 779 return 0; 780 781 FileMapTy::iterator FI = FileMap.find(File); 782 if (FI != FileMap.end()) 783 return FI->second; 784 785 return 0; 786 } 787 788 CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const { 789 CXIdxLoc idxLoc = { {0, 0}, 0 }; 790 if (Loc.isInvalid()) 791 return idxLoc; 792 793 idxLoc.ptr_data[0] = (void*)this; 794 idxLoc.int_data = Loc.getRawEncoding(); 795 return idxLoc; 796 } 797 798 void IndexingContext::translateLoc(SourceLocation Loc, 799 CXIdxClientFile *indexFile, CXFile *file, 800 unsigned *line, unsigned *column, 801 unsigned *offset) { 802 if (Loc.isInvalid()) 803 return; 804 805 SourceManager &SM = Ctx->getSourceManager(); 806 Loc = SM.getFileLoc(Loc); 807 808 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 809 FileID FID = LocInfo.first; 810 unsigned FileOffset = LocInfo.second; 811 812 if (FID.isInvalid()) 813 return; 814 815 const FileEntry *FE = SM.getFileEntryForID(FID); 816 if (indexFile) 817 *indexFile = getIndexFile(FE); 818 if (file) 819 *file = (void *)FE; 820 if (line) 821 *line = SM.getLineNumber(FID, FileOffset); 822 if (column) 823 *column = SM.getColumnNumber(FID, FileOffset); 824 if (offset) 825 *offset = FileOffset; 826 } 827 828 void IndexingContext::getEntityInfo(const NamedDecl *D, 829 EntityInfo &EntityInfo, 830 ScratchAlloc &SA) { 831 if (!D) 832 return; 833 834 D = getEntityDecl(D); 835 EntityInfo.cursor = getCursor(D); 836 EntityInfo.Dcl = D; 837 EntityInfo.IndexCtx = this; 838 EntityInfo.kind = CXIdxEntity_Unexposed; 839 EntityInfo.templateKind = CXIdxEntity_NonTemplate; 840 EntityInfo.lang = CXIdxEntityLang_C; 841 842 if (D->hasAttrs()) { 843 EntityInfo.AttrList = AttrListInfo::create(D, *this); 844 EntityInfo.attributes = EntityInfo.AttrList->getAttrs(); 845 EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs(); 846 } 847 848 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { 849 switch (TD->getTagKind()) { 850 case TTK_Struct: 851 EntityInfo.kind = CXIdxEntity_Struct; break; 852 case TTK_Union: 853 EntityInfo.kind = CXIdxEntity_Union; break; 854 case TTK_Class: 855 EntityInfo.kind = CXIdxEntity_CXXClass; 856 EntityInfo.lang = CXIdxEntityLang_CXX; 857 break; 858 case TTK_Interface: 859 EntityInfo.kind = CXIdxEntity_CXXInterface; 860 EntityInfo.lang = CXIdxEntityLang_CXX; 861 break; 862 case TTK_Enum: 863 EntityInfo.kind = CXIdxEntity_Enum; break; 864 } 865 866 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) 867 if (!CXXRec->isCLike()) 868 EntityInfo.lang = CXIdxEntityLang_CXX; 869 870 if (isa<ClassTemplatePartialSpecializationDecl>(D)) { 871 EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization; 872 } else if (isa<ClassTemplateSpecializationDecl>(D)) { 873 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 874 } 875 876 } else { 877 switch (D->getKind()) { 878 case Decl::Typedef: 879 EntityInfo.kind = CXIdxEntity_Typedef; break; 880 case Decl::Function: 881 EntityInfo.kind = CXIdxEntity_Function; 882 break; 883 case Decl::ParmVar: 884 EntityInfo.kind = CXIdxEntity_Variable; 885 break; 886 case Decl::Var: 887 EntityInfo.kind = CXIdxEntity_Variable; 888 if (isa<CXXRecordDecl>(D->getDeclContext())) { 889 EntityInfo.kind = CXIdxEntity_CXXStaticVariable; 890 EntityInfo.lang = CXIdxEntityLang_CXX; 891 } 892 break; 893 case Decl::Field: 894 EntityInfo.kind = CXIdxEntity_Field; 895 if (const CXXRecordDecl * 896 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) { 897 // FIXME: isPOD check is not sufficient, a POD can contain methods, 898 // we want a isCStructLike check. 899 if (!CXXRec->isPOD()) 900 EntityInfo.lang = CXIdxEntityLang_CXX; 901 } 902 break; 903 case Decl::EnumConstant: 904 EntityInfo.kind = CXIdxEntity_EnumConstant; break; 905 case Decl::ObjCInterface: 906 EntityInfo.kind = CXIdxEntity_ObjCClass; 907 EntityInfo.lang = CXIdxEntityLang_ObjC; 908 break; 909 case Decl::ObjCProtocol: 910 EntityInfo.kind = CXIdxEntity_ObjCProtocol; 911 EntityInfo.lang = CXIdxEntityLang_ObjC; 912 break; 913 case Decl::ObjCCategory: 914 EntityInfo.kind = CXIdxEntity_ObjCCategory; 915 EntityInfo.lang = CXIdxEntityLang_ObjC; 916 break; 917 case Decl::ObjCMethod: 918 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) 919 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod; 920 else 921 EntityInfo.kind = CXIdxEntity_ObjCClassMethod; 922 EntityInfo.lang = CXIdxEntityLang_ObjC; 923 break; 924 case Decl::ObjCProperty: 925 EntityInfo.kind = CXIdxEntity_ObjCProperty; 926 EntityInfo.lang = CXIdxEntityLang_ObjC; 927 break; 928 case Decl::ObjCIvar: 929 EntityInfo.kind = CXIdxEntity_ObjCIvar; 930 EntityInfo.lang = CXIdxEntityLang_ObjC; 931 break; 932 case Decl::Namespace: 933 EntityInfo.kind = CXIdxEntity_CXXNamespace; 934 EntityInfo.lang = CXIdxEntityLang_CXX; 935 break; 936 case Decl::NamespaceAlias: 937 EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias; 938 EntityInfo.lang = CXIdxEntityLang_CXX; 939 break; 940 case Decl::CXXConstructor: 941 EntityInfo.kind = CXIdxEntity_CXXConstructor; 942 EntityInfo.lang = CXIdxEntityLang_CXX; 943 break; 944 case Decl::CXXDestructor: 945 EntityInfo.kind = CXIdxEntity_CXXDestructor; 946 EntityInfo.lang = CXIdxEntityLang_CXX; 947 break; 948 case Decl::CXXConversion: 949 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 950 EntityInfo.lang = CXIdxEntityLang_CXX; 951 break; 952 case Decl::CXXMethod: { 953 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); 954 if (MD->isStatic()) 955 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 956 else 957 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 958 EntityInfo.lang = CXIdxEntityLang_CXX; 959 break; 960 } 961 case Decl::ClassTemplate: 962 EntityInfo.kind = CXIdxEntity_CXXClass; 963 EntityInfo.templateKind = CXIdxEntity_Template; 964 break; 965 case Decl::FunctionTemplate: 966 EntityInfo.kind = CXIdxEntity_Function; 967 EntityInfo.templateKind = CXIdxEntity_Template; 968 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>( 969 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) { 970 if (isa<CXXConstructorDecl>(MD)) 971 EntityInfo.kind = CXIdxEntity_CXXConstructor; 972 else if (isa<CXXDestructorDecl>(MD)) 973 EntityInfo.kind = CXIdxEntity_CXXDestructor; 974 else if (isa<CXXConversionDecl>(MD)) 975 EntityInfo.kind = CXIdxEntity_CXXConversionFunction; 976 else { 977 if (MD->isStatic()) 978 EntityInfo.kind = CXIdxEntity_CXXStaticMethod; 979 else 980 EntityInfo.kind = CXIdxEntity_CXXInstanceMethod; 981 } 982 } 983 break; 984 case Decl::TypeAliasTemplate: 985 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 986 EntityInfo.templateKind = CXIdxEntity_Template; 987 break; 988 case Decl::TypeAlias: 989 EntityInfo.kind = CXIdxEntity_CXXTypeAlias; 990 EntityInfo.lang = CXIdxEntityLang_CXX; 991 break; 992 default: 993 break; 994 } 995 } 996 997 if (EntityInfo.kind == CXIdxEntity_Unexposed) 998 return; 999 1000 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1001 if (FD->getTemplatedKind() == 1002 FunctionDecl::TK_FunctionTemplateSpecialization) 1003 EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization; 1004 } 1005 1006 if (EntityInfo.templateKind != CXIdxEntity_NonTemplate) 1007 EntityInfo.lang = CXIdxEntityLang_CXX; 1008 1009 if (IdentifierInfo *II = D->getIdentifier()) { 1010 EntityInfo.name = SA.toCStr(II->getName()); 1011 1012 } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) { 1013 EntityInfo.name = 0; // anonymous tag/field/namespace. 1014 1015 } else { 1016 SmallString<256> StrBuf; 1017 { 1018 llvm::raw_svector_ostream OS(StrBuf); 1019 D->printName(OS); 1020 } 1021 EntityInfo.name = SA.copyCStr(StrBuf.str()); 1022 } 1023 1024 { 1025 SmallString<512> StrBuf; 1026 bool Ignore = getDeclCursorUSR(D, StrBuf); 1027 if (Ignore) { 1028 EntityInfo.USR = 0; 1029 } else { 1030 EntityInfo.USR = SA.copyCStr(StrBuf.str()); 1031 } 1032 } 1033 } 1034 1035 void IndexingContext::getContainerInfo(const DeclContext *DC, 1036 ContainerInfo &ContInfo) { 1037 ContInfo.cursor = getCursor(cast<Decl>(DC)); 1038 ContInfo.DC = DC; 1039 ContInfo.IndexCtx = this; 1040 } 1041 1042 CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) { 1043 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D)) 1044 return MakeCursorTypeRef(TD, Loc, CXTU); 1045 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) 1046 return MakeCursorObjCClassRef(ID, Loc, CXTU); 1047 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) 1048 return MakeCursorObjCProtocolRef(PD, Loc, CXTU); 1049 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) 1050 return MakeCursorTemplateRef(Template, Loc, CXTU); 1051 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D)) 1052 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 1053 if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D)) 1054 return MakeCursorNamespaceRef(Namespace, Loc, CXTU); 1055 if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) 1056 return MakeCursorMemberRef(Field, Loc, CXTU); 1057 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) 1058 return MakeCursorVariableRef(Var, Loc, CXTU); 1059 1060 return clang_getNullCursor(); 1061 } 1062 1063 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) { 1064 if (isa<ObjCInterfaceDecl>(D)) 1065 return false; 1066 if (isa<ObjCCategoryDecl>(D)) 1067 return false; 1068 if (isa<ObjCIvarDecl>(D)) 1069 return false; 1070 if (isa<ObjCMethodDecl>(D)) 1071 return false; 1072 return true; 1073 } 1074 1075 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) { 1076 if (const ClassTemplateSpecializationDecl * 1077 SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 1078 return SD->getSpecializationKind() == TSK_ImplicitInstantiation; 1079 } 1080 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1081 return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation; 1082 } 1083 return false; 1084 } 1085