CIndex.cpp revision ed8bef44c0545fd55a78715606f8d733f6498b21
1//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// 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// This file implements the main API hooks in the Clang-C Source Indexing 11// library. 12// 13//===----------------------------------------------------------------------===// 14 15#include "CIndexer.h" 16#include "CXCursor.h" 17#include "CXTranslationUnit.h" 18#include "CXString.h" 19#include "CXType.h" 20#include "CXSourceLocation.h" 21#include "CIndexDiagnostic.h" 22#include "CursorVisitor.h" 23 24#include "clang/Basic/Version.h" 25 26#include "clang/AST/StmtVisitor.h" 27#include "clang/Basic/Diagnostic.h" 28#include "clang/Frontend/ASTUnit.h" 29#include "clang/Frontend/CompilerInstance.h" 30#include "clang/Frontend/FrontendDiagnostic.h" 31#include "clang/Lex/Lexer.h" 32#include "clang/Lex/HeaderSearch.h" 33#include "clang/Lex/PreprocessingRecord.h" 34#include "clang/Lex/Preprocessor.h" 35#include "llvm/ADT/STLExtras.h" 36#include "llvm/ADT/Optional.h" 37#include "llvm/ADT/StringSwitch.h" 38#include "clang/Analysis/Support/SaveAndRestore.h" 39#include "llvm/Support/CrashRecoveryContext.h" 40#include "llvm/Support/PrettyStackTrace.h" 41#include "llvm/Support/MemoryBuffer.h" 42#include "llvm/Support/raw_ostream.h" 43#include "llvm/Support/Timer.h" 44#include "llvm/Support/Mutex.h" 45#include "llvm/Support/Program.h" 46#include "llvm/Support/Signals.h" 47#include "llvm/Support/Threading.h" 48#include "llvm/Support/Compiler.h" 49 50using namespace clang; 51using namespace clang::cxcursor; 52using namespace clang::cxstring; 53using namespace clang::cxtu; 54 55CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) { 56 if (!TU) 57 return 0; 58 CXTranslationUnit D = new CXTranslationUnitImpl(); 59 D->TUData = TU; 60 D->StringPool = createCXStringPool(); 61 D->Diagnostics = 0; 62 return D; 63} 64 65cxtu::CXTUOwner::~CXTUOwner() { 66 if (TU) 67 clang_disposeTranslationUnit(TU); 68} 69 70/// \brief Compare two source ranges to determine their relative position in 71/// the translation unit. 72static RangeComparisonResult RangeCompare(SourceManager &SM, 73 SourceRange R1, 74 SourceRange R2) { 75 assert(R1.isValid() && "First range is invalid?"); 76 assert(R2.isValid() && "Second range is invalid?"); 77 if (R1.getEnd() != R2.getBegin() && 78 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) 79 return RangeBefore; 80 if (R2.getEnd() != R1.getBegin() && 81 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) 82 return RangeAfter; 83 return RangeOverlap; 84} 85 86/// \brief Determine if a source location falls within, before, or after a 87/// a given source range. 88static RangeComparisonResult LocationCompare(SourceManager &SM, 89 SourceLocation L, SourceRange R) { 90 assert(R.isValid() && "First range is invalid?"); 91 assert(L.isValid() && "Second range is invalid?"); 92 if (L == R.getBegin() || L == R.getEnd()) 93 return RangeOverlap; 94 if (SM.isBeforeInTranslationUnit(L, R.getBegin())) 95 return RangeBefore; 96 if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) 97 return RangeAfter; 98 return RangeOverlap; 99} 100 101/// \brief Translate a Clang source range into a CIndex source range. 102/// 103/// Clang internally represents ranges where the end location points to the 104/// start of the token at the end. However, for external clients it is more 105/// useful to have a CXSourceRange be a proper half-open interval. This routine 106/// does the appropriate translation. 107CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, 108 const LangOptions &LangOpts, 109 const CharSourceRange &R) { 110 // We want the last character in this location, so we will adjust the 111 // location accordingly. 112 SourceLocation EndLoc = R.getEnd(); 113 if (EndLoc.isValid() && EndLoc.isMacroID()) 114 EndLoc = SM.getExpansionRange(EndLoc).second; 115 if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { 116 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); 117 EndLoc = EndLoc.getLocWithOffset(Length); 118 } 119 120 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, 121 R.getBegin().getRawEncoding(), 122 EndLoc.getRawEncoding() }; 123 return Result; 124} 125 126//===----------------------------------------------------------------------===// 127// Cursor visitor. 128//===----------------------------------------------------------------------===// 129 130static SourceRange getRawCursorExtent(CXCursor C); 131static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); 132 133 134RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { 135 return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); 136} 137 138/// \brief Visit the given cursor and, if requested by the visitor, 139/// its children. 140/// 141/// \param Cursor the cursor to visit. 142/// 143/// \param CheckRegionOfInterest if true, then the caller already checked that 144/// this cursor is within the region of interest. 145/// 146/// \returns true if the visitation should be aborted, false if it 147/// should continue. 148bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { 149 if (clang_isInvalid(Cursor.kind)) 150 return false; 151 152 if (clang_isDeclaration(Cursor.kind)) { 153 Decl *D = getCursorDecl(Cursor); 154 assert(D && "Invalid declaration cursor"); 155 // Ignore implicit declarations, unless it's an objc method because 156 // currently we should report implicit methods for properties when indexing. 157 if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) 158 return false; 159 } 160 161 // If we have a range of interest, and this cursor doesn't intersect with it, 162 // we're done. 163 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { 164 SourceRange Range = getRawCursorExtent(Cursor); 165 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 166 return false; 167 } 168 169 switch (Visitor(Cursor, Parent, ClientData)) { 170 case CXChildVisit_Break: 171 return true; 172 173 case CXChildVisit_Continue: 174 return false; 175 176 case CXChildVisit_Recurse: 177 return VisitChildren(Cursor); 178 } 179 180 return false; 181} 182 183static bool visitPreprocessedEntitiesInRange(SourceRange R, 184 PreprocessingRecord &PPRec, 185 CursorVisitor &Visitor) { 186 SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); 187 FileID FID; 188 189 if (!Visitor.shouldVisitIncludedEntities()) { 190 // If the begin/end of the range lie in the same FileID, do the optimization 191 // where we skip preprocessed entities that do not come from the same FileID. 192 FID = SM.getFileID(R.getBegin()); 193 if (FID != SM.getFileID(R.getEnd())) 194 FID = FileID(); 195 } 196 197 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 198 Entities = PPRec.getPreprocessedEntitiesInRange(R); 199 return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, 200 PPRec, FID); 201} 202 203void CursorVisitor::visitFileRegion() { 204 if (RegionOfInterest.isInvalid()) 205 return; 206 207 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); 208 SourceManager &SM = Unit->getSourceManager(); 209 210 std::pair<FileID, unsigned> 211 Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), 212 End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); 213 214 if (End.first != Begin.first) { 215 // If the end does not reside in the same file, try to recover by 216 // picking the end of the file of begin location. 217 End.first = Begin.first; 218 End.second = SM.getFileIDSize(Begin.first); 219 } 220 221 assert(Begin.first == End.first); 222 if (Begin.second > End.second) 223 return; 224 225 FileID File = Begin.first; 226 unsigned Offset = Begin.second; 227 unsigned Length = End.second - Begin.second; 228 229 if (!VisitPreprocessorLast && 230 Unit->getPreprocessor().getPreprocessingRecord()) 231 visitPreprocessedEntitiesInRegion(); 232 233 visitDeclsFromFileRegion(File, Offset, Length); 234 235 if (VisitPreprocessorLast && 236 Unit->getPreprocessor().getPreprocessingRecord()) 237 visitPreprocessedEntitiesInRegion(); 238} 239 240static bool isInLexicalContext(Decl *D, DeclContext *DC) { 241 if (!DC) 242 return false; 243 244 for (DeclContext *DeclDC = D->getLexicalDeclContext(); 245 DeclDC; DeclDC = DeclDC->getLexicalParent()) { 246 if (DeclDC == DC) 247 return true; 248 } 249 return false; 250} 251 252void CursorVisitor::visitDeclsFromFileRegion(FileID File, 253 unsigned Offset, unsigned Length) { 254 ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); 255 SourceManager &SM = Unit->getSourceManager(); 256 SourceRange Range = RegionOfInterest; 257 258 SmallVector<Decl *, 16> Decls; 259 Unit->findFileRegionDecls(File, Offset, Length, Decls); 260 261 // If we didn't find any file level decls for the file, try looking at the 262 // file that it was included from. 263 while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { 264 bool Invalid = false; 265 const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); 266 if (Invalid) 267 return; 268 269 SourceLocation Outer; 270 if (SLEntry.isFile()) 271 Outer = SLEntry.getFile().getIncludeLoc(); 272 else 273 Outer = SLEntry.getExpansion().getExpansionLocStart(); 274 if (Outer.isInvalid()) 275 return; 276 277 llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); 278 Length = 0; 279 Unit->findFileRegionDecls(File, Offset, Length, Decls); 280 } 281 282 assert(!Decls.empty()); 283 284 bool VisitedAtLeastOnce = false; 285 DeclContext *CurDC = 0; 286 SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); 287 for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { 288 Decl *D = *DIt; 289 if (D->getSourceRange().isInvalid()) 290 continue; 291 292 if (isInLexicalContext(D, CurDC)) 293 continue; 294 295 CurDC = dyn_cast<DeclContext>(D); 296 297 // We handle forward decls via ObjCClassDecl. 298 if (ObjCInterfaceDecl *InterD = dyn_cast<ObjCInterfaceDecl>(D)) { 299 if (InterD->isForwardDecl()) 300 continue; 301 // An interface that started as a forward decl may have changed location 302 // because its @interface was parsed. 303 if (InterD->isInitiallyForwardDecl() && 304 !SM.isInFileID(SM.getFileLoc(InterD->getLocation()), File)) 305 continue; 306 } 307 308 if (TagDecl *TD = dyn_cast<TagDecl>(D)) 309 if (!TD->isFreeStanding()) 310 continue; 311 312 RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); 313 if (CompRes == RangeBefore) 314 continue; 315 if (CompRes == RangeAfter) 316 break; 317 318 assert(CompRes == RangeOverlap); 319 VisitedAtLeastOnce = true; 320 321 if (isa<ObjCContainerDecl>(D)) { 322 FileDI_current = &DIt; 323 FileDE_current = DE; 324 } else { 325 FileDI_current = 0; 326 } 327 328 if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) 329 break; 330 } 331 332 if (VisitedAtLeastOnce) 333 return; 334 335 // No Decls overlapped with the range. Move up the lexical context until there 336 // is a context that contains the range or we reach the translation unit 337 // level. 338 DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() 339 : (*(DIt-1))->getLexicalDeclContext(); 340 341 while (DC && !DC->isTranslationUnit()) { 342 Decl *D = cast<Decl>(DC); 343 SourceRange CurDeclRange = D->getSourceRange(); 344 if (CurDeclRange.isInvalid()) 345 break; 346 347 if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { 348 Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); 349 break; 350 } 351 352 DC = D->getLexicalDeclContext(); 353 } 354} 355 356bool CursorVisitor::visitPreprocessedEntitiesInRegion() { 357 PreprocessingRecord &PPRec 358 = *AU->getPreprocessor().getPreprocessingRecord(); 359 SourceManager &SM = AU->getSourceManager(); 360 361 if (RegionOfInterest.isValid()) { 362 SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); 363 SourceLocation B = MappedRange.getBegin(); 364 SourceLocation E = MappedRange.getEnd(); 365 366 if (AU->isInPreambleFileID(B)) { 367 if (SM.isLoadedSourceLocation(E)) 368 return visitPreprocessedEntitiesInRange(SourceRange(B, E), 369 PPRec, *this); 370 371 // Beginning of range lies in the preamble but it also extends beyond 372 // it into the main file. Split the range into 2 parts, one covering 373 // the preamble and another covering the main file. This allows subsequent 374 // calls to visitPreprocessedEntitiesInRange to accept a source range that 375 // lies in the same FileID, allowing it to skip preprocessed entities that 376 // do not come from the same FileID. 377 bool breaked = 378 visitPreprocessedEntitiesInRange( 379 SourceRange(B, AU->getEndOfPreambleFileID()), 380 PPRec, *this); 381 if (breaked) return true; 382 return visitPreprocessedEntitiesInRange( 383 SourceRange(AU->getStartOfMainFileID(), E), 384 PPRec, *this); 385 } 386 387 return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); 388 } 389 390 bool OnlyLocalDecls 391 = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); 392 393 if (OnlyLocalDecls) 394 return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), 395 PPRec); 396 397 return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); 398} 399 400template<typename InputIterator> 401bool CursorVisitor::visitPreprocessedEntities(InputIterator First, 402 InputIterator Last, 403 PreprocessingRecord &PPRec, 404 FileID FID) { 405 for (; First != Last; ++First) { 406 if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) 407 continue; 408 409 PreprocessedEntity *PPE = *First; 410 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { 411 if (Visit(MakeMacroExpansionCursor(ME, TU))) 412 return true; 413 414 continue; 415 } 416 417 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { 418 if (Visit(MakeMacroDefinitionCursor(MD, TU))) 419 return true; 420 421 continue; 422 } 423 424 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { 425 if (Visit(MakeInclusionDirectiveCursor(ID, TU))) 426 return true; 427 428 continue; 429 } 430 } 431 432 return false; 433} 434 435/// \brief Visit the children of the given cursor. 436/// 437/// \returns true if the visitation should be aborted, false if it 438/// should continue. 439bool CursorVisitor::VisitChildren(CXCursor Cursor) { 440 if (clang_isReference(Cursor.kind) && 441 Cursor.kind != CXCursor_CXXBaseSpecifier) { 442 // By definition, references have no children. 443 return false; 444 } 445 446 // Set the Parent field to Cursor, then back to its old value once we're 447 // done. 448 SetParentRAII SetParent(Parent, StmtParent, Cursor); 449 450 if (clang_isDeclaration(Cursor.kind)) { 451 Decl *D = getCursorDecl(Cursor); 452 if (!D) 453 return false; 454 455 return VisitAttributes(D) || Visit(D); 456 } 457 458 if (clang_isStatement(Cursor.kind)) { 459 if (Stmt *S = getCursorStmt(Cursor)) 460 return Visit(S); 461 462 return false; 463 } 464 465 if (clang_isExpression(Cursor.kind)) { 466 if (Expr *E = getCursorExpr(Cursor)) 467 return Visit(E); 468 469 return false; 470 } 471 472 if (clang_isTranslationUnit(Cursor.kind)) { 473 CXTranslationUnit tu = getCursorTU(Cursor); 474 ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); 475 476 int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; 477 for (unsigned I = 0; I != 2; ++I) { 478 if (VisitOrder[I]) { 479 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && 480 RegionOfInterest.isInvalid()) { 481 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), 482 TLEnd = CXXUnit->top_level_end(); 483 TL != TLEnd; ++TL) { 484 if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) 485 return true; 486 } 487 } else if (VisitDeclContext( 488 CXXUnit->getASTContext().getTranslationUnitDecl())) 489 return true; 490 continue; 491 } 492 493 // Walk the preprocessing record. 494 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) 495 visitPreprocessedEntitiesInRegion(); 496 } 497 498 return false; 499 } 500 501 if (Cursor.kind == CXCursor_CXXBaseSpecifier) { 502 if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { 503 if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { 504 return Visit(BaseTSInfo->getTypeLoc()); 505 } 506 } 507 } 508 509 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { 510 IBOutletCollectionAttr *A = 511 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); 512 if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) 513 return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), 514 A->getInterfaceLoc(), TU)); 515 } 516 517 // Nothing to visit at the moment. 518 return false; 519} 520 521bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { 522 if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) 523 if (Visit(TSInfo->getTypeLoc())) 524 return true; 525 526 if (Stmt *Body = B->getBody()) 527 return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); 528 529 return false; 530} 531 532llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { 533 if (RegionOfInterest.isValid()) { 534 SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); 535 if (Range.isInvalid()) 536 return llvm::Optional<bool>(); 537 538 switch (CompareRegionOfInterest(Range)) { 539 case RangeBefore: 540 // This declaration comes before the region of interest; skip it. 541 return llvm::Optional<bool>(); 542 543 case RangeAfter: 544 // This declaration comes after the region of interest; we're done. 545 return false; 546 547 case RangeOverlap: 548 // This declaration overlaps the region of interest; visit it. 549 break; 550 } 551 } 552 return true; 553} 554 555bool CursorVisitor::VisitDeclContext(DeclContext *DC) { 556 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); 557 558 // FIXME: Eventually remove. This part of a hack to support proper 559 // iteration over all Decls contained lexically within an ObjC container. 560 SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); 561 SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); 562 563 for ( ; I != E; ++I) { 564 Decl *D = *I; 565 if (D->getLexicalDeclContext() != DC) 566 continue; 567 CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); 568 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); 569 if (!V.hasValue()) 570 continue; 571 if (!V.getValue()) 572 return false; 573 if (Visit(Cursor, true)) 574 return true; 575 } 576 return false; 577} 578 579bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 580 llvm_unreachable("Translation units are visited directly by Visit()"); 581 return false; 582} 583 584bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { 585 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) 586 return Visit(TSInfo->getTypeLoc()); 587 588 return false; 589} 590 591bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { 592 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) 593 return Visit(TSInfo->getTypeLoc()); 594 595 return false; 596} 597 598bool CursorVisitor::VisitTagDecl(TagDecl *D) { 599 return VisitDeclContext(D); 600} 601 602bool CursorVisitor::VisitClassTemplateSpecializationDecl( 603 ClassTemplateSpecializationDecl *D) { 604 bool ShouldVisitBody = false; 605 switch (D->getSpecializationKind()) { 606 case TSK_Undeclared: 607 case TSK_ImplicitInstantiation: 608 // Nothing to visit 609 return false; 610 611 case TSK_ExplicitInstantiationDeclaration: 612 case TSK_ExplicitInstantiationDefinition: 613 break; 614 615 case TSK_ExplicitSpecialization: 616 ShouldVisitBody = true; 617 break; 618 } 619 620 // Visit the template arguments used in the specialization. 621 if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { 622 TypeLoc TL = SpecType->getTypeLoc(); 623 if (TemplateSpecializationTypeLoc *TSTLoc 624 = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { 625 for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) 626 if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) 627 return true; 628 } 629 } 630 631 if (ShouldVisitBody && VisitCXXRecordDecl(D)) 632 return true; 633 634 return false; 635} 636 637bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( 638 ClassTemplatePartialSpecializationDecl *D) { 639 // FIXME: Visit the "outer" template parameter lists on the TagDecl 640 // before visiting these template parameters. 641 if (VisitTemplateParameters(D->getTemplateParameters())) 642 return true; 643 644 // Visit the partial specialization arguments. 645 const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); 646 for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) 647 if (VisitTemplateArgumentLoc(TemplateArgs[I])) 648 return true; 649 650 return VisitCXXRecordDecl(D); 651} 652 653bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { 654 // Visit the default argument. 655 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 656 if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) 657 if (Visit(DefArg->getTypeLoc())) 658 return true; 659 660 return false; 661} 662 663bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { 664 if (Expr *Init = D->getInitExpr()) 665 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); 666 return false; 667} 668 669bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { 670 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) 671 if (Visit(TSInfo->getTypeLoc())) 672 return true; 673 674 // Visit the nested-name-specifier, if present. 675 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) 676 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 677 return true; 678 679 return false; 680} 681 682/// \brief Compare two base or member initializers based on their source order. 683static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { 684 CXXCtorInitializer const * const *X 685 = static_cast<CXXCtorInitializer const * const *>(Xp); 686 CXXCtorInitializer const * const *Y 687 = static_cast<CXXCtorInitializer const * const *>(Yp); 688 689 if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) 690 return -1; 691 else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) 692 return 1; 693 else 694 return 0; 695} 696 697bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { 698 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { 699 // Visit the function declaration's syntactic components in the order 700 // written. This requires a bit of work. 701 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); 702 FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); 703 704 // If we have a function declared directly (without the use of a typedef), 705 // visit just the return type. Otherwise, just visit the function's type 706 // now. 707 if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || 708 (!FTL && Visit(TL))) 709 return true; 710 711 // Visit the nested-name-specifier, if present. 712 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) 713 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 714 return true; 715 716 // Visit the declaration name. 717 if (VisitDeclarationNameInfo(ND->getNameInfo())) 718 return true; 719 720 // FIXME: Visit explicitly-specified template arguments! 721 722 // Visit the function parameters, if we have a function type. 723 if (FTL && VisitFunctionTypeLoc(*FTL, true)) 724 return true; 725 726 // FIXME: Attributes? 727 } 728 729 if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { 730 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { 731 // Find the initializers that were written in the source. 732 SmallVector<CXXCtorInitializer *, 4> WrittenInits; 733 for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), 734 IEnd = Constructor->init_end(); 735 I != IEnd; ++I) { 736 if (!(*I)->isWritten()) 737 continue; 738 739 WrittenInits.push_back(*I); 740 } 741 742 // Sort the initializers in source order 743 llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), 744 &CompareCXXCtorInitializers); 745 746 // Visit the initializers in source order 747 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { 748 CXXCtorInitializer *Init = WrittenInits[I]; 749 if (Init->isAnyMemberInitializer()) { 750 if (Visit(MakeCursorMemberRef(Init->getAnyMember(), 751 Init->getMemberLocation(), TU))) 752 return true; 753 } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { 754 if (Visit(TInfo->getTypeLoc())) 755 return true; 756 } 757 758 // Visit the initializer value. 759 if (Expr *Initializer = Init->getInit()) 760 if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) 761 return true; 762 } 763 } 764 765 if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) 766 return true; 767 } 768 769 return false; 770} 771 772bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { 773 if (VisitDeclaratorDecl(D)) 774 return true; 775 776 if (Expr *BitWidth = D->getBitWidth()) 777 return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); 778 779 return false; 780} 781 782bool CursorVisitor::VisitVarDecl(VarDecl *D) { 783 if (VisitDeclaratorDecl(D)) 784 return true; 785 786 if (Expr *Init = D->getInit()) 787 return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); 788 789 return false; 790} 791 792bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { 793 if (VisitDeclaratorDecl(D)) 794 return true; 795 796 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 797 if (Expr *DefArg = D->getDefaultArgument()) 798 return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); 799 800 return false; 801} 802 803bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 804 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl 805 // before visiting these template parameters. 806 if (VisitTemplateParameters(D->getTemplateParameters())) 807 return true; 808 809 return VisitFunctionDecl(D->getTemplatedDecl()); 810} 811 812bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { 813 // FIXME: Visit the "outer" template parameter lists on the TagDecl 814 // before visiting these template parameters. 815 if (VisitTemplateParameters(D->getTemplateParameters())) 816 return true; 817 818 return VisitCXXRecordDecl(D->getTemplatedDecl()); 819} 820 821bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { 822 if (VisitTemplateParameters(D->getTemplateParameters())) 823 return true; 824 825 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && 826 VisitTemplateArgumentLoc(D->getDefaultArgument())) 827 return true; 828 829 return false; 830} 831 832bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { 833 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) 834 if (Visit(TSInfo->getTypeLoc())) 835 return true; 836 837 for (ObjCMethodDecl::param_iterator P = ND->param_begin(), 838 PEnd = ND->param_end(); 839 P != PEnd; ++P) { 840 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) 841 return true; 842 } 843 844 if (ND->isThisDeclarationADefinition() && 845 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) 846 return true; 847 848 return false; 849} 850 851template <typename DeclIt> 852static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, 853 SourceManager &SM, SourceLocation EndLoc, 854 SmallVectorImpl<Decl *> &Decls) { 855 DeclIt next = *DI_current; 856 while (++next != DE_current) { 857 Decl *D_next = *next; 858 if (!D_next) 859 break; 860 SourceLocation L = D_next->getLocStart(); 861 if (!L.isValid()) 862 break; 863 if (SM.isBeforeInTranslationUnit(L, EndLoc)) { 864 *DI_current = next; 865 Decls.push_back(D_next); 866 continue; 867 } 868 break; 869 } 870} 871 872namespace { 873 struct ContainerDeclsSort { 874 SourceManager &SM; 875 ContainerDeclsSort(SourceManager &sm) : SM(sm) {} 876 bool operator()(Decl *A, Decl *B) { 877 SourceLocation L_A = A->getLocStart(); 878 SourceLocation L_B = B->getLocStart(); 879 assert(L_A.isValid() && L_B.isValid()); 880 return SM.isBeforeInTranslationUnit(L_A, L_B); 881 } 882 }; 883} 884 885bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { 886 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially 887 // an @implementation can lexically contain Decls that are not properly 888 // nested in the AST. When we identify such cases, we need to retrofit 889 // this nesting here. 890 if (!DI_current && !FileDI_current) 891 return VisitDeclContext(D); 892 893 // Scan the Decls that immediately come after the container 894 // in the current DeclContext. If any fall within the 895 // container's lexical region, stash them into a vector 896 // for later processing. 897 SmallVector<Decl *, 24> DeclsInContainer; 898 SourceLocation EndLoc = D->getSourceRange().getEnd(); 899 SourceManager &SM = AU->getSourceManager(); 900 if (EndLoc.isValid()) { 901 if (DI_current) { 902 addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, 903 DeclsInContainer); 904 } else { 905 addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, 906 DeclsInContainer); 907 } 908 } 909 910 // The common case. 911 if (DeclsInContainer.empty()) 912 return VisitDeclContext(D); 913 914 // Get all the Decls in the DeclContext, and sort them with the 915 // additional ones we've collected. Then visit them. 916 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); 917 I!=E; ++I) { 918 Decl *subDecl = *I; 919 if (!subDecl || subDecl->getLexicalDeclContext() != D || 920 subDecl->getLocStart().isInvalid()) 921 continue; 922 DeclsInContainer.push_back(subDecl); 923 } 924 925 // Now sort the Decls so that they appear in lexical order. 926 std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), 927 ContainerDeclsSort(SM)); 928 929 // Now visit the decls. 930 for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), 931 E = DeclsInContainer.end(); I != E; ++I) { 932 CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); 933 const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); 934 if (!V.hasValue()) 935 continue; 936 if (!V.getValue()) 937 return false; 938 if (Visit(Cursor, true)) 939 return true; 940 } 941 return false; 942} 943 944bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { 945 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), 946 TU))) 947 return true; 948 949 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); 950 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), 951 E = ND->protocol_end(); I != E; ++I, ++PL) 952 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 953 return true; 954 955 return VisitObjCContainerDecl(ND); 956} 957 958bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 959 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); 960 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), 961 E = PID->protocol_end(); I != E; ++I, ++PL) 962 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 963 return true; 964 965 return VisitObjCContainerDecl(PID); 966} 967 968bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { 969 if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) 970 return true; 971 972 // FIXME: This implements a workaround with @property declarations also being 973 // installed in the DeclContext for the @interface. Eventually this code 974 // should be removed. 975 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); 976 if (!CDecl || !CDecl->IsClassExtension()) 977 return false; 978 979 ObjCInterfaceDecl *ID = CDecl->getClassInterface(); 980 if (!ID) 981 return false; 982 983 IdentifierInfo *PropertyId = PD->getIdentifier(); 984 ObjCPropertyDecl *prevDecl = 985 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); 986 987 if (!prevDecl) 988 return false; 989 990 // Visit synthesized methods since they will be skipped when visiting 991 // the @interface. 992 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) 993 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) 994 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) 995 return true; 996 997 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) 998 if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) 999 if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) 1000 return true; 1001 1002 return false; 1003} 1004 1005bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 1006 // Issue callbacks for super class. 1007 if (D->getSuperClass() && 1008 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 1009 D->getSuperClassLoc(), 1010 TU))) 1011 return true; 1012 1013 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); 1014 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), 1015 E = D->protocol_end(); I != E; ++I, ++PL) 1016 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 1017 return true; 1018 1019 return VisitObjCContainerDecl(D); 1020} 1021 1022bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { 1023 return VisitObjCContainerDecl(D); 1024} 1025 1026bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 1027 // 'ID' could be null when dealing with invalid code. 1028 if (ObjCInterfaceDecl *ID = D->getClassInterface()) 1029 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) 1030 return true; 1031 1032 return VisitObjCImplDecl(D); 1033} 1034 1035bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 1036#if 0 1037 // Issue callbacks for super class. 1038 // FIXME: No source location information! 1039 if (D->getSuperClass() && 1040 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 1041 D->getSuperClassLoc(), 1042 TU))) 1043 return true; 1044#endif 1045 1046 return VisitObjCImplDecl(D); 1047} 1048 1049bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { 1050 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); 1051 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), 1052 E = D->protocol_end(); 1053 I != E; ++I, ++PL) 1054 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 1055 return true; 1056 1057 return false; 1058} 1059 1060bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { 1061 if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), 1062 D->getForwardDecl()->getLocation(), TU))) 1063 return true; 1064 return false; 1065} 1066 1067bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { 1068 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) 1069 return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); 1070 1071 return false; 1072} 1073 1074bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { 1075 return VisitDeclContext(D); 1076} 1077 1078bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 1079 // Visit nested-name-specifier. 1080 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1081 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1082 return true; 1083 1084 return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), 1085 D->getTargetNameLoc(), TU)); 1086} 1087 1088bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { 1089 // Visit nested-name-specifier. 1090 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { 1091 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1092 return true; 1093 } 1094 1095 if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) 1096 return true; 1097 1098 return VisitDeclarationNameInfo(D->getNameInfo()); 1099} 1100 1101bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 1102 // Visit nested-name-specifier. 1103 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1104 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1105 return true; 1106 1107 return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), 1108 D->getIdentLocation(), TU)); 1109} 1110 1111bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1112 // Visit nested-name-specifier. 1113 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { 1114 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1115 return true; 1116 } 1117 1118 return VisitDeclarationNameInfo(D->getNameInfo()); 1119} 1120 1121bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( 1122 UnresolvedUsingTypenameDecl *D) { 1123 // Visit nested-name-specifier. 1124 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1125 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1126 return true; 1127 1128 return false; 1129} 1130 1131bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { 1132 switch (Name.getName().getNameKind()) { 1133 case clang::DeclarationName::Identifier: 1134 case clang::DeclarationName::CXXLiteralOperatorName: 1135 case clang::DeclarationName::CXXOperatorName: 1136 case clang::DeclarationName::CXXUsingDirective: 1137 return false; 1138 1139 case clang::DeclarationName::CXXConstructorName: 1140 case clang::DeclarationName::CXXDestructorName: 1141 case clang::DeclarationName::CXXConversionFunctionName: 1142 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) 1143 return Visit(TSInfo->getTypeLoc()); 1144 return false; 1145 1146 case clang::DeclarationName::ObjCZeroArgSelector: 1147 case clang::DeclarationName::ObjCOneArgSelector: 1148 case clang::DeclarationName::ObjCMultiArgSelector: 1149 // FIXME: Per-identifier location info? 1150 return false; 1151 } 1152 1153 return false; 1154} 1155 1156bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, 1157 SourceRange Range) { 1158 // FIXME: This whole routine is a hack to work around the lack of proper 1159 // source information in nested-name-specifiers (PR5791). Since we do have 1160 // a beginning source location, we can visit the first component of the 1161 // nested-name-specifier, if it's a single-token component. 1162 if (!NNS) 1163 return false; 1164 1165 // Get the first component in the nested-name-specifier. 1166 while (NestedNameSpecifier *Prefix = NNS->getPrefix()) 1167 NNS = Prefix; 1168 1169 switch (NNS->getKind()) { 1170 case NestedNameSpecifier::Namespace: 1171 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), 1172 TU)); 1173 1174 case NestedNameSpecifier::NamespaceAlias: 1175 return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 1176 Range.getBegin(), TU)); 1177 1178 case NestedNameSpecifier::TypeSpec: { 1179 // If the type has a form where we know that the beginning of the source 1180 // range matches up with a reference cursor. Visit the appropriate reference 1181 // cursor. 1182 const Type *T = NNS->getAsType(); 1183 if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) 1184 return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); 1185 if (const TagType *Tag = dyn_cast<TagType>(T)) 1186 return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); 1187 if (const TemplateSpecializationType *TST 1188 = dyn_cast<TemplateSpecializationType>(T)) 1189 return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); 1190 break; 1191 } 1192 1193 case NestedNameSpecifier::TypeSpecWithTemplate: 1194 case NestedNameSpecifier::Global: 1195 case NestedNameSpecifier::Identifier: 1196 break; 1197 } 1198 1199 return false; 1200} 1201 1202bool 1203CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { 1204 SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; 1205 for (; Qualifier; Qualifier = Qualifier.getPrefix()) 1206 Qualifiers.push_back(Qualifier); 1207 1208 while (!Qualifiers.empty()) { 1209 NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); 1210 NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); 1211 switch (NNS->getKind()) { 1212 case NestedNameSpecifier::Namespace: 1213 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), 1214 Q.getLocalBeginLoc(), 1215 TU))) 1216 return true; 1217 1218 break; 1219 1220 case NestedNameSpecifier::NamespaceAlias: 1221 if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 1222 Q.getLocalBeginLoc(), 1223 TU))) 1224 return true; 1225 1226 break; 1227 1228 case NestedNameSpecifier::TypeSpec: 1229 case NestedNameSpecifier::TypeSpecWithTemplate: 1230 if (Visit(Q.getTypeLoc())) 1231 return true; 1232 1233 break; 1234 1235 case NestedNameSpecifier::Global: 1236 case NestedNameSpecifier::Identifier: 1237 break; 1238 } 1239 } 1240 1241 return false; 1242} 1243 1244bool CursorVisitor::VisitTemplateParameters( 1245 const TemplateParameterList *Params) { 1246 if (!Params) 1247 return false; 1248 1249 for (TemplateParameterList::const_iterator P = Params->begin(), 1250 PEnd = Params->end(); 1251 P != PEnd; ++P) { 1252 if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) 1253 return true; 1254 } 1255 1256 return false; 1257} 1258 1259bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { 1260 switch (Name.getKind()) { 1261 case TemplateName::Template: 1262 return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); 1263 1264 case TemplateName::OverloadedTemplate: 1265 // Visit the overloaded template set. 1266 if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) 1267 return true; 1268 1269 return false; 1270 1271 case TemplateName::DependentTemplate: 1272 // FIXME: Visit nested-name-specifier. 1273 return false; 1274 1275 case TemplateName::QualifiedTemplate: 1276 // FIXME: Visit nested-name-specifier. 1277 return Visit(MakeCursorTemplateRef( 1278 Name.getAsQualifiedTemplateName()->getDecl(), 1279 Loc, TU)); 1280 1281 case TemplateName::SubstTemplateTemplateParm: 1282 return Visit(MakeCursorTemplateRef( 1283 Name.getAsSubstTemplateTemplateParm()->getParameter(), 1284 Loc, TU)); 1285 1286 case TemplateName::SubstTemplateTemplateParmPack: 1287 return Visit(MakeCursorTemplateRef( 1288 Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), 1289 Loc, TU)); 1290 } 1291 1292 return false; 1293} 1294 1295bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { 1296 switch (TAL.getArgument().getKind()) { 1297 case TemplateArgument::Null: 1298 case TemplateArgument::Integral: 1299 case TemplateArgument::Pack: 1300 return false; 1301 1302 case TemplateArgument::Type: 1303 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) 1304 return Visit(TSInfo->getTypeLoc()); 1305 return false; 1306 1307 case TemplateArgument::Declaration: 1308 if (Expr *E = TAL.getSourceDeclExpression()) 1309 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); 1310 return false; 1311 1312 case TemplateArgument::Expression: 1313 if (Expr *E = TAL.getSourceExpression()) 1314 return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); 1315 return false; 1316 1317 case TemplateArgument::Template: 1318 case TemplateArgument::TemplateExpansion: 1319 if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) 1320 return true; 1321 1322 return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), 1323 TAL.getTemplateNameLoc()); 1324 } 1325 1326 return false; 1327} 1328 1329bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 1330 return VisitDeclContext(D); 1331} 1332 1333bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 1334 return Visit(TL.getUnqualifiedLoc()); 1335} 1336 1337bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 1338 ASTContext &Context = AU->getASTContext(); 1339 1340 // Some builtin types (such as Objective-C's "id", "sel", and 1341 // "Class") have associated declarations. Create cursors for those. 1342 QualType VisitType; 1343 switch (TL.getTypePtr()->getKind()) { 1344 1345 case BuiltinType::Void: 1346 case BuiltinType::NullPtr: 1347 case BuiltinType::Dependent: 1348#define BUILTIN_TYPE(Id, SingletonId) 1349#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 1350#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: 1351#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: 1352#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: 1353#include "clang/AST/BuiltinTypes.def" 1354 break; 1355 1356 case BuiltinType::ObjCId: 1357 VisitType = Context.getObjCIdType(); 1358 break; 1359 1360 case BuiltinType::ObjCClass: 1361 VisitType = Context.getObjCClassType(); 1362 break; 1363 1364 case BuiltinType::ObjCSel: 1365 VisitType = Context.getObjCSelType(); 1366 break; 1367 } 1368 1369 if (!VisitType.isNull()) { 1370 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) 1371 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), 1372 TU)); 1373 } 1374 1375 return false; 1376} 1377 1378bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 1379 return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); 1380} 1381 1382bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 1383 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1384} 1385 1386bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { 1387 if (TL.isDefinition()) 1388 return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); 1389 1390 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1391} 1392 1393bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 1394 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1395} 1396 1397bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 1398 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) 1399 return true; 1400 1401 return false; 1402} 1403 1404bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 1405 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) 1406 return true; 1407 1408 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { 1409 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), 1410 TU))) 1411 return true; 1412 } 1413 1414 return false; 1415} 1416 1417bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 1418 return Visit(TL.getPointeeLoc()); 1419} 1420 1421bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { 1422 return Visit(TL.getInnerLoc()); 1423} 1424 1425bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { 1426 return Visit(TL.getPointeeLoc()); 1427} 1428 1429bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 1430 return Visit(TL.getPointeeLoc()); 1431} 1432 1433bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 1434 return Visit(TL.getPointeeLoc()); 1435} 1436 1437bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 1438 return Visit(TL.getPointeeLoc()); 1439} 1440 1441bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 1442 return Visit(TL.getPointeeLoc()); 1443} 1444 1445bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 1446 return Visit(TL.getModifiedLoc()); 1447} 1448 1449bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, 1450 bool SkipResultType) { 1451 if (!SkipResultType && Visit(TL.getResultLoc())) 1452 return true; 1453 1454 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1455 if (Decl *D = TL.getArg(I)) 1456 if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) 1457 return true; 1458 1459 return false; 1460} 1461 1462bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { 1463 if (Visit(TL.getElementLoc())) 1464 return true; 1465 1466 if (Expr *Size = TL.getSizeExpr()) 1467 return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); 1468 1469 return false; 1470} 1471 1472bool CursorVisitor::VisitTemplateSpecializationTypeLoc( 1473 TemplateSpecializationTypeLoc TL) { 1474 // Visit the template name. 1475 if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), 1476 TL.getTemplateNameLoc())) 1477 return true; 1478 1479 // Visit the template arguments. 1480 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1481 if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) 1482 return true; 1483 1484 return false; 1485} 1486 1487bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 1488 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); 1489} 1490 1491bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 1492 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) 1493 return Visit(TSInfo->getTypeLoc()); 1494 1495 return false; 1496} 1497 1498bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 1499 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) 1500 return Visit(TSInfo->getTypeLoc()); 1501 1502 return false; 1503} 1504 1505bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 1506 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1507 return true; 1508 1509 return false; 1510} 1511 1512bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( 1513 DependentTemplateSpecializationTypeLoc TL) { 1514 // Visit the nested-name-specifier, if there is one. 1515 if (TL.getQualifierLoc() && 1516 VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1517 return true; 1518 1519 // Visit the template arguments. 1520 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 1521 if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) 1522 return true; 1523 1524 return false; 1525} 1526 1527bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 1528 if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) 1529 return true; 1530 1531 return Visit(TL.getNamedTypeLoc()); 1532} 1533 1534bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 1535 return Visit(TL.getPatternLoc()); 1536} 1537 1538bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 1539 if (Expr *E = TL.getUnderlyingExpr()) 1540 return Visit(MakeCXCursor(E, StmtParent, TU)); 1541 1542 return false; 1543} 1544 1545bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 1546 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 1547} 1548 1549bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 1550 return Visit(TL.getValueLoc()); 1551} 1552 1553#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ 1554bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ 1555 return Visit##PARENT##Loc(TL); \ 1556} 1557 1558DEFAULT_TYPELOC_IMPL(Complex, Type) 1559DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) 1560DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) 1561DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) 1562DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) 1563DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) 1564DEFAULT_TYPELOC_IMPL(Vector, Type) 1565DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) 1566DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) 1567DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) 1568DEFAULT_TYPELOC_IMPL(Record, TagType) 1569DEFAULT_TYPELOC_IMPL(Enum, TagType) 1570DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) 1571DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) 1572DEFAULT_TYPELOC_IMPL(Auto, Type) 1573 1574bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { 1575 // Visit the nested-name-specifier, if present. 1576 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) 1577 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 1578 return true; 1579 1580 if (D->isCompleteDefinition()) { 1581 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), 1582 E = D->bases_end(); I != E; ++I) { 1583 if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) 1584 return true; 1585 } 1586 } 1587 1588 return VisitTagDecl(D); 1589} 1590 1591bool CursorVisitor::VisitAttributes(Decl *D) { 1592 for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); 1593 i != e; ++i) 1594 if (Visit(MakeCXCursor(*i, D, TU))) 1595 return true; 1596 1597 return false; 1598} 1599 1600//===----------------------------------------------------------------------===// 1601// Data-recursive visitor methods. 1602//===----------------------------------------------------------------------===// 1603 1604namespace { 1605#define DEF_JOB(NAME, DATA, KIND)\ 1606class NAME : public VisitorJob {\ 1607public:\ 1608 NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ 1609 static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ 1610 DATA *get() const { return static_cast<DATA*>(data[0]); }\ 1611}; 1612 1613DEF_JOB(StmtVisit, Stmt, StmtVisitKind) 1614DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) 1615DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) 1616DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) 1617DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, 1618 ExplicitTemplateArgsVisitKind) 1619DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) 1620#undef DEF_JOB 1621 1622class DeclVisit : public VisitorJob { 1623public: 1624 DeclVisit(Decl *d, CXCursor parent, bool isFirst) : 1625 VisitorJob(parent, VisitorJob::DeclVisitKind, 1626 d, isFirst ? (void*) 1 : (void*) 0) {} 1627 static bool classof(const VisitorJob *VJ) { 1628 return VJ->getKind() == DeclVisitKind; 1629 } 1630 Decl *get() const { return static_cast<Decl*>(data[0]); } 1631 bool isFirst() const { return data[1] ? true : false; } 1632}; 1633class TypeLocVisit : public VisitorJob { 1634public: 1635 TypeLocVisit(TypeLoc tl, CXCursor parent) : 1636 VisitorJob(parent, VisitorJob::TypeLocVisitKind, 1637 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} 1638 1639 static bool classof(const VisitorJob *VJ) { 1640 return VJ->getKind() == TypeLocVisitKind; 1641 } 1642 1643 TypeLoc get() const { 1644 QualType T = QualType::getFromOpaquePtr(data[0]); 1645 return TypeLoc(T, data[1]); 1646 } 1647}; 1648 1649class LabelRefVisit : public VisitorJob { 1650public: 1651 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) 1652 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, 1653 labelLoc.getPtrEncoding()) {} 1654 1655 static bool classof(const VisitorJob *VJ) { 1656 return VJ->getKind() == VisitorJob::LabelRefVisitKind; 1657 } 1658 LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } 1659 SourceLocation getLoc() const { 1660 return SourceLocation::getFromPtrEncoding(data[1]); } 1661}; 1662 1663class NestedNameSpecifierLocVisit : public VisitorJob { 1664public: 1665 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) 1666 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, 1667 Qualifier.getNestedNameSpecifier(), 1668 Qualifier.getOpaqueData()) { } 1669 1670 static bool classof(const VisitorJob *VJ) { 1671 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; 1672 } 1673 1674 NestedNameSpecifierLoc get() const { 1675 return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), 1676 data[1]); 1677 } 1678}; 1679 1680class DeclarationNameInfoVisit : public VisitorJob { 1681public: 1682 DeclarationNameInfoVisit(Stmt *S, CXCursor parent) 1683 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} 1684 static bool classof(const VisitorJob *VJ) { 1685 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; 1686 } 1687 DeclarationNameInfo get() const { 1688 Stmt *S = static_cast<Stmt*>(data[0]); 1689 switch (S->getStmtClass()) { 1690 default: 1691 llvm_unreachable("Unhandled Stmt"); 1692 case clang::Stmt::MSDependentExistsStmtClass: 1693 return cast<MSDependentExistsStmt>(S)->getNameInfo(); 1694 case Stmt::CXXDependentScopeMemberExprClass: 1695 return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); 1696 case Stmt::DependentScopeDeclRefExprClass: 1697 return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); 1698 } 1699 } 1700}; 1701class MemberRefVisit : public VisitorJob { 1702public: 1703 MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) 1704 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, 1705 L.getPtrEncoding()) {} 1706 static bool classof(const VisitorJob *VJ) { 1707 return VJ->getKind() == VisitorJob::MemberRefVisitKind; 1708 } 1709 FieldDecl *get() const { 1710 return static_cast<FieldDecl*>(data[0]); 1711 } 1712 SourceLocation getLoc() const { 1713 return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); 1714 } 1715}; 1716class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { 1717 VisitorWorkList &WL; 1718 CXCursor Parent; 1719public: 1720 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) 1721 : WL(wl), Parent(parent) {} 1722 1723 void VisitAddrLabelExpr(AddrLabelExpr *E); 1724 void VisitBlockExpr(BlockExpr *B); 1725 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 1726 void VisitCompoundStmt(CompoundStmt *S); 1727 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } 1728 void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); 1729 void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); 1730 void VisitCXXNewExpr(CXXNewExpr *E); 1731 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1732 void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); 1733 void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); 1734 void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); 1735 void VisitCXXTypeidExpr(CXXTypeidExpr *E); 1736 void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); 1737 void VisitCXXUuidofExpr(CXXUuidofExpr *E); 1738 void VisitDeclRefExpr(DeclRefExpr *D); 1739 void VisitDeclStmt(DeclStmt *S); 1740 void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); 1741 void VisitDesignatedInitExpr(DesignatedInitExpr *E); 1742 void VisitExplicitCastExpr(ExplicitCastExpr *E); 1743 void VisitForStmt(ForStmt *FS); 1744 void VisitGotoStmt(GotoStmt *GS); 1745 void VisitIfStmt(IfStmt *If); 1746 void VisitInitListExpr(InitListExpr *IE); 1747 void VisitMemberExpr(MemberExpr *M); 1748 void VisitOffsetOfExpr(OffsetOfExpr *E); 1749 void VisitObjCEncodeExpr(ObjCEncodeExpr *E); 1750 void VisitObjCMessageExpr(ObjCMessageExpr *M); 1751 void VisitOverloadExpr(OverloadExpr *E); 1752 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 1753 void VisitStmt(Stmt *S); 1754 void VisitSwitchStmt(SwitchStmt *S); 1755 void VisitWhileStmt(WhileStmt *W); 1756 void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); 1757 void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); 1758 void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); 1759 void VisitExpressionTraitExpr(ExpressionTraitExpr *E); 1760 void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); 1761 void VisitVAArgExpr(VAArgExpr *E); 1762 void VisitSizeOfPackExpr(SizeOfPackExpr *E); 1763 void VisitPseudoObjectExpr(PseudoObjectExpr *E); 1764 void VisitOpaqueValueExpr(OpaqueValueExpr *E); 1765 1766private: 1767 void AddDeclarationNameInfo(Stmt *S); 1768 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); 1769 void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); 1770 void AddMemberRef(FieldDecl *D, SourceLocation L); 1771 void AddStmt(Stmt *S); 1772 void AddDecl(Decl *D, bool isFirst = true); 1773 void AddTypeLoc(TypeSourceInfo *TI); 1774 void EnqueueChildren(Stmt *S); 1775}; 1776} // end anonyous namespace 1777 1778void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { 1779 // 'S' should always be non-null, since it comes from the 1780 // statement we are visiting. 1781 WL.push_back(DeclarationNameInfoVisit(S, Parent)); 1782} 1783 1784void 1785EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { 1786 if (Qualifier) 1787 WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); 1788} 1789 1790void EnqueueVisitor::AddStmt(Stmt *S) { 1791 if (S) 1792 WL.push_back(StmtVisit(S, Parent)); 1793} 1794void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { 1795 if (D) 1796 WL.push_back(DeclVisit(D, Parent, isFirst)); 1797} 1798void EnqueueVisitor:: 1799 AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { 1800 if (A) 1801 WL.push_back(ExplicitTemplateArgsVisit( 1802 const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); 1803} 1804void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { 1805 if (D) 1806 WL.push_back(MemberRefVisit(D, L, Parent)); 1807} 1808void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { 1809 if (TI) 1810 WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); 1811 } 1812void EnqueueVisitor::EnqueueChildren(Stmt *S) { 1813 unsigned size = WL.size(); 1814 for (Stmt::child_range Child = S->children(); Child; ++Child) { 1815 AddStmt(*Child); 1816 } 1817 if (size == WL.size()) 1818 return; 1819 // Now reverse the entries we just added. This will match the DFS 1820 // ordering performed by the worklist. 1821 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); 1822 std::reverse(I, E); 1823} 1824void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { 1825 WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); 1826} 1827void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { 1828 AddDecl(B->getBlockDecl()); 1829} 1830void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 1831 EnqueueChildren(E); 1832 AddTypeLoc(E->getTypeSourceInfo()); 1833} 1834void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { 1835 for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), 1836 E = S->body_rend(); I != E; ++I) { 1837 AddStmt(*I); 1838 } 1839} 1840void EnqueueVisitor:: 1841VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { 1842 AddStmt(S->getSubStmt()); 1843 AddDeclarationNameInfo(S); 1844 if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) 1845 AddNestedNameSpecifierLoc(QualifierLoc); 1846} 1847 1848void EnqueueVisitor:: 1849VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { 1850 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 1851 AddDeclarationNameInfo(E); 1852 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) 1853 AddNestedNameSpecifierLoc(QualifierLoc); 1854 if (!E->isImplicitAccess()) 1855 AddStmt(E->getBase()); 1856} 1857void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { 1858 // Enqueue the initializer or constructor arguments. 1859 for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) 1860 AddStmt(E->getConstructorArg(I-1)); 1861 // Enqueue the array size, if any. 1862 AddStmt(E->getArraySize()); 1863 // Enqueue the allocated type. 1864 AddTypeLoc(E->getAllocatedTypeSourceInfo()); 1865 // Enqueue the placement arguments. 1866 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) 1867 AddStmt(E->getPlacementArg(I-1)); 1868} 1869void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { 1870 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) 1871 AddStmt(CE->getArg(I-1)); 1872 AddStmt(CE->getCallee()); 1873 AddStmt(CE->getArg(0)); 1874} 1875void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1876 // Visit the name of the type being destroyed. 1877 AddTypeLoc(E->getDestroyedTypeInfo()); 1878 // Visit the scope type that looks disturbingly like the nested-name-specifier 1879 // but isn't. 1880 AddTypeLoc(E->getScopeTypeInfo()); 1881 // Visit the nested-name-specifier. 1882 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) 1883 AddNestedNameSpecifierLoc(QualifierLoc); 1884 // Visit base expression. 1885 AddStmt(E->getBase()); 1886} 1887void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 1888 AddTypeLoc(E->getTypeSourceInfo()); 1889} 1890void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { 1891 EnqueueChildren(E); 1892 AddTypeLoc(E->getTypeSourceInfo()); 1893} 1894void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { 1895 EnqueueChildren(E); 1896 if (E->isTypeOperand()) 1897 AddTypeLoc(E->getTypeOperandSourceInfo()); 1898} 1899 1900void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr 1901 *E) { 1902 EnqueueChildren(E); 1903 AddTypeLoc(E->getTypeSourceInfo()); 1904} 1905void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { 1906 EnqueueChildren(E); 1907 if (E->isTypeOperand()) 1908 AddTypeLoc(E->getTypeOperandSourceInfo()); 1909} 1910void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { 1911 if (DR->hasExplicitTemplateArgs()) { 1912 AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); 1913 } 1914 WL.push_back(DeclRefExprParts(DR, Parent)); 1915} 1916void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { 1917 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 1918 AddDeclarationNameInfo(E); 1919 AddNestedNameSpecifierLoc(E->getQualifierLoc()); 1920} 1921void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { 1922 unsigned size = WL.size(); 1923 bool isFirst = true; 1924 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 1925 D != DEnd; ++D) { 1926 AddDecl(*D, isFirst); 1927 isFirst = false; 1928 } 1929 if (size == WL.size()) 1930 return; 1931 // Now reverse the entries we just added. This will match the DFS 1932 // ordering performed by the worklist. 1933 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); 1934 std::reverse(I, E); 1935} 1936void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { 1937 AddStmt(E->getInit()); 1938 typedef DesignatedInitExpr::Designator Designator; 1939 for (DesignatedInitExpr::reverse_designators_iterator 1940 D = E->designators_rbegin(), DEnd = E->designators_rend(); 1941 D != DEnd; ++D) { 1942 if (D->isFieldDesignator()) { 1943 if (FieldDecl *Field = D->getField()) 1944 AddMemberRef(Field, D->getFieldLoc()); 1945 continue; 1946 } 1947 if (D->isArrayDesignator()) { 1948 AddStmt(E->getArrayIndex(*D)); 1949 continue; 1950 } 1951 assert(D->isArrayRangeDesignator() && "Unknown designator kind"); 1952 AddStmt(E->getArrayRangeEnd(*D)); 1953 AddStmt(E->getArrayRangeStart(*D)); 1954 } 1955} 1956void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { 1957 EnqueueChildren(E); 1958 AddTypeLoc(E->getTypeInfoAsWritten()); 1959} 1960void EnqueueVisitor::VisitForStmt(ForStmt *FS) { 1961 AddStmt(FS->getBody()); 1962 AddStmt(FS->getInc()); 1963 AddStmt(FS->getCond()); 1964 AddDecl(FS->getConditionVariable()); 1965 AddStmt(FS->getInit()); 1966} 1967void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { 1968 WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); 1969} 1970void EnqueueVisitor::VisitIfStmt(IfStmt *If) { 1971 AddStmt(If->getElse()); 1972 AddStmt(If->getThen()); 1973 AddStmt(If->getCond()); 1974 AddDecl(If->getConditionVariable()); 1975} 1976void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { 1977 // We care about the syntactic form of the initializer list, only. 1978 if (InitListExpr *Syntactic = IE->getSyntacticForm()) 1979 IE = Syntactic; 1980 EnqueueChildren(IE); 1981} 1982void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { 1983 WL.push_back(MemberExprParts(M, Parent)); 1984 1985 // If the base of the member access expression is an implicit 'this', don't 1986 // visit it. 1987 // FIXME: If we ever want to show these implicit accesses, this will be 1988 // unfortunate. However, clang_getCursor() relies on this behavior. 1989 if (!M->isImplicitAccess()) 1990 AddStmt(M->getBase()); 1991} 1992void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 1993 AddTypeLoc(E->getEncodedTypeSourceInfo()); 1994} 1995void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { 1996 EnqueueChildren(M); 1997 AddTypeLoc(M->getClassReceiverTypeInfo()); 1998} 1999void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { 2000 // Visit the components of the offsetof expression. 2001 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { 2002 typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; 2003 const OffsetOfNode &Node = E->getComponent(I-1); 2004 switch (Node.getKind()) { 2005 case OffsetOfNode::Array: 2006 AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); 2007 break; 2008 case OffsetOfNode::Field: 2009 AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); 2010 break; 2011 case OffsetOfNode::Identifier: 2012 case OffsetOfNode::Base: 2013 continue; 2014 } 2015 } 2016 // Visit the type into which we're computing the offset. 2017 AddTypeLoc(E->getTypeSourceInfo()); 2018} 2019void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { 2020 AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); 2021 WL.push_back(OverloadExprParts(E, Parent)); 2022} 2023void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( 2024 UnaryExprOrTypeTraitExpr *E) { 2025 EnqueueChildren(E); 2026 if (E->isArgumentType()) 2027 AddTypeLoc(E->getArgumentTypeInfo()); 2028} 2029void EnqueueVisitor::VisitStmt(Stmt *S) { 2030 EnqueueChildren(S); 2031} 2032void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { 2033 AddStmt(S->getBody()); 2034 AddStmt(S->getCond()); 2035 AddDecl(S->getConditionVariable()); 2036} 2037 2038void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { 2039 AddStmt(W->getBody()); 2040 AddStmt(W->getCond()); 2041 AddDecl(W->getConditionVariable()); 2042} 2043 2044void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 2045 AddTypeLoc(E->getQueriedTypeSourceInfo()); 2046} 2047 2048void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 2049 AddTypeLoc(E->getRhsTypeSourceInfo()); 2050 AddTypeLoc(E->getLhsTypeSourceInfo()); 2051} 2052 2053void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 2054 AddTypeLoc(E->getQueriedTypeSourceInfo()); 2055} 2056 2057void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 2058 EnqueueChildren(E); 2059} 2060 2061void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { 2062 VisitOverloadExpr(U); 2063 if (!U->isImplicitAccess()) 2064 AddStmt(U->getBase()); 2065} 2066void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { 2067 AddStmt(E->getSubExpr()); 2068 AddTypeLoc(E->getWrittenTypeInfo()); 2069} 2070void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2071 WL.push_back(SizeOfPackExprParts(E, Parent)); 2072} 2073void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) { 2074 // If the opaque value has a source expression, just transparently 2075 // visit that. This is useful for (e.g.) pseudo-object expressions. 2076 if (Expr *SourceExpr = E->getSourceExpr()) 2077 return Visit(SourceExpr); 2078 AddStmt(E); 2079} 2080void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) { 2081 // Treat the expression like its syntactic form. 2082 Visit(E->getSyntacticForm()); 2083} 2084 2085void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { 2086 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); 2087} 2088 2089bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { 2090 if (RegionOfInterest.isValid()) { 2091 SourceRange Range = getRawCursorExtent(C); 2092 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 2093 return false; 2094 } 2095 return true; 2096} 2097 2098bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { 2099 while (!WL.empty()) { 2100 // Dequeue the worklist item. 2101 VisitorJob LI = WL.back(); 2102 WL.pop_back(); 2103 2104 // Set the Parent field, then back to its old value once we're done. 2105 SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); 2106 2107 switch (LI.getKind()) { 2108 case VisitorJob::DeclVisitKind: { 2109 Decl *D = cast<DeclVisit>(&LI)->get(); 2110 if (!D) 2111 continue; 2112 2113 // For now, perform default visitation for Decls. 2114 if (Visit(MakeCXCursor(D, TU, RegionOfInterest, 2115 cast<DeclVisit>(&LI)->isFirst()))) 2116 return true; 2117 2118 continue; 2119 } 2120 case VisitorJob::ExplicitTemplateArgsVisitKind: { 2121 const ASTTemplateArgumentListInfo *ArgList = 2122 cast<ExplicitTemplateArgsVisit>(&LI)->get(); 2123 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), 2124 *ArgEnd = Arg + ArgList->NumTemplateArgs; 2125 Arg != ArgEnd; ++Arg) { 2126 if (VisitTemplateArgumentLoc(*Arg)) 2127 return true; 2128 } 2129 continue; 2130 } 2131 case VisitorJob::TypeLocVisitKind: { 2132 // Perform default visitation for TypeLocs. 2133 if (Visit(cast<TypeLocVisit>(&LI)->get())) 2134 return true; 2135 continue; 2136 } 2137 case VisitorJob::LabelRefVisitKind: { 2138 LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); 2139 if (LabelStmt *stmt = LS->getStmt()) { 2140 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), 2141 TU))) { 2142 return true; 2143 } 2144 } 2145 continue; 2146 } 2147 2148 case VisitorJob::NestedNameSpecifierLocVisitKind: { 2149 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); 2150 if (VisitNestedNameSpecifierLoc(V->get())) 2151 return true; 2152 continue; 2153 } 2154 2155 case VisitorJob::DeclarationNameInfoVisitKind: { 2156 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) 2157 ->get())) 2158 return true; 2159 continue; 2160 } 2161 case VisitorJob::MemberRefVisitKind: { 2162 MemberRefVisit *V = cast<MemberRefVisit>(&LI); 2163 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) 2164 return true; 2165 continue; 2166 } 2167 case VisitorJob::StmtVisitKind: { 2168 Stmt *S = cast<StmtVisit>(&LI)->get(); 2169 if (!S) 2170 continue; 2171 2172 // Update the current cursor. 2173 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); 2174 if (!IsInRegionOfInterest(Cursor)) 2175 continue; 2176 switch (Visitor(Cursor, Parent, ClientData)) { 2177 case CXChildVisit_Break: return true; 2178 case CXChildVisit_Continue: break; 2179 case CXChildVisit_Recurse: 2180 EnqueueWorkList(WL, S); 2181 break; 2182 } 2183 continue; 2184 } 2185 case VisitorJob::MemberExprPartsKind: { 2186 // Handle the other pieces in the MemberExpr besides the base. 2187 MemberExpr *M = cast<MemberExprParts>(&LI)->get(); 2188 2189 // Visit the nested-name-specifier 2190 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) 2191 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2192 return true; 2193 2194 // Visit the declaration name. 2195 if (VisitDeclarationNameInfo(M->getMemberNameInfo())) 2196 return true; 2197 2198 // Visit the explicitly-specified template arguments, if any. 2199 if (M->hasExplicitTemplateArgs()) { 2200 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), 2201 *ArgEnd = Arg + M->getNumTemplateArgs(); 2202 Arg != ArgEnd; ++Arg) { 2203 if (VisitTemplateArgumentLoc(*Arg)) 2204 return true; 2205 } 2206 } 2207 continue; 2208 } 2209 case VisitorJob::DeclRefExprPartsKind: { 2210 DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); 2211 // Visit nested-name-specifier, if present. 2212 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) 2213 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2214 return true; 2215 // Visit declaration name. 2216 if (VisitDeclarationNameInfo(DR->getNameInfo())) 2217 return true; 2218 continue; 2219 } 2220 case VisitorJob::OverloadExprPartsKind: { 2221 OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); 2222 // Visit the nested-name-specifier. 2223 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) 2224 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2225 return true; 2226 // Visit the declaration name. 2227 if (VisitDeclarationNameInfo(O->getNameInfo())) 2228 return true; 2229 // Visit the overloaded declaration reference. 2230 if (Visit(MakeCursorOverloadedDeclRef(O, TU))) 2231 return true; 2232 continue; 2233 } 2234 case VisitorJob::SizeOfPackExprPartsKind: { 2235 SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); 2236 NamedDecl *Pack = E->getPack(); 2237 if (isa<TemplateTypeParmDecl>(Pack)) { 2238 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), 2239 E->getPackLoc(), TU))) 2240 return true; 2241 2242 continue; 2243 } 2244 2245 if (isa<TemplateTemplateParmDecl>(Pack)) { 2246 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), 2247 E->getPackLoc(), TU))) 2248 return true; 2249 2250 continue; 2251 } 2252 2253 // Non-type template parameter packs and function parameter packs are 2254 // treated like DeclRefExpr cursors. 2255 continue; 2256 } 2257 } 2258 } 2259 return false; 2260} 2261 2262bool CursorVisitor::Visit(Stmt *S) { 2263 VisitorWorkList *WL = 0; 2264 if (!WorkListFreeList.empty()) { 2265 WL = WorkListFreeList.back(); 2266 WL->clear(); 2267 WorkListFreeList.pop_back(); 2268 } 2269 else { 2270 WL = new VisitorWorkList(); 2271 WorkListCache.push_back(WL); 2272 } 2273 EnqueueWorkList(*WL, S); 2274 bool result = RunVisitorWorkList(*WL); 2275 WorkListFreeList.push_back(WL); 2276 return result; 2277} 2278 2279namespace { 2280typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; 2281RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, 2282 const DeclarationNameInfo &NI, 2283 const SourceRange &QLoc, 2284 const ASTTemplateArgumentListInfo *TemplateArgs = 0){ 2285 const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; 2286 const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; 2287 const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; 2288 2289 const DeclarationName::NameKind Kind = NI.getName().getNameKind(); 2290 2291 RefNamePieces Pieces; 2292 2293 if (WantQualifier && QLoc.isValid()) 2294 Pieces.push_back(QLoc); 2295 2296 if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) 2297 Pieces.push_back(NI.getLoc()); 2298 2299 if (WantTemplateArgs && TemplateArgs) 2300 Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, 2301 TemplateArgs->RAngleLoc)); 2302 2303 if (Kind == DeclarationName::CXXOperatorName) { 2304 Pieces.push_back(SourceLocation::getFromRawEncoding( 2305 NI.getInfo().CXXOperatorName.BeginOpNameLoc)); 2306 Pieces.push_back(SourceLocation::getFromRawEncoding( 2307 NI.getInfo().CXXOperatorName.EndOpNameLoc)); 2308 } 2309 2310 if (WantSinglePiece) { 2311 SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); 2312 Pieces.clear(); 2313 Pieces.push_back(R); 2314 } 2315 2316 return Pieces; 2317} 2318} 2319 2320//===----------------------------------------------------------------------===// 2321// Misc. API hooks. 2322//===----------------------------------------------------------------------===// 2323 2324static llvm::sys::Mutex EnableMultithreadingMutex; 2325static bool EnabledMultithreading; 2326 2327extern "C" { 2328CXIndex clang_createIndex(int excludeDeclarationsFromPCH, 2329 int displayDiagnostics) { 2330 // Disable pretty stack trace functionality, which will otherwise be a very 2331 // poor citizen of the world and set up all sorts of signal handlers. 2332 llvm::DisablePrettyStackTrace = true; 2333 2334 // We use crash recovery to make some of our APIs more reliable, implicitly 2335 // enable it. 2336 llvm::CrashRecoveryContext::Enable(); 2337 2338 // Enable support for multithreading in LLVM. 2339 { 2340 llvm::sys::ScopedLock L(EnableMultithreadingMutex); 2341 if (!EnabledMultithreading) { 2342 llvm::llvm_start_multithreaded(); 2343 EnabledMultithreading = true; 2344 } 2345 } 2346 2347 CIndexer *CIdxr = new CIndexer(); 2348 if (excludeDeclarationsFromPCH) 2349 CIdxr->setOnlyLocalDecls(); 2350 if (displayDiagnostics) 2351 CIdxr->setDisplayDiagnostics(); 2352 return CIdxr; 2353} 2354 2355void clang_disposeIndex(CXIndex CIdx) { 2356 if (CIdx) 2357 delete static_cast<CIndexer *>(CIdx); 2358} 2359 2360void clang_toggleCrashRecovery(unsigned isEnabled) { 2361 if (isEnabled) 2362 llvm::CrashRecoveryContext::Enable(); 2363 else 2364 llvm::CrashRecoveryContext::Disable(); 2365} 2366 2367CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, 2368 const char *ast_filename) { 2369 if (!CIdx) 2370 return 0; 2371 2372 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2373 FileSystemOptions FileSystemOpts; 2374 FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); 2375 2376 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; 2377 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, 2378 CXXIdx->getOnlyLocalDecls(), 2379 0, 0, true); 2380 return MakeCXTranslationUnit(TU); 2381} 2382 2383unsigned clang_defaultEditingTranslationUnitOptions() { 2384 return CXTranslationUnit_PrecompiledPreamble | 2385 CXTranslationUnit_CacheCompletionResults; 2386} 2387 2388CXTranslationUnit 2389clang_createTranslationUnitFromSourceFile(CXIndex CIdx, 2390 const char *source_filename, 2391 int num_command_line_args, 2392 const char * const *command_line_args, 2393 unsigned num_unsaved_files, 2394 struct CXUnsavedFile *unsaved_files) { 2395 unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | 2396 CXTranslationUnit_NestedMacroExpansions; 2397 return clang_parseTranslationUnit(CIdx, source_filename, 2398 command_line_args, num_command_line_args, 2399 unsaved_files, num_unsaved_files, 2400 Options); 2401} 2402 2403struct ParseTranslationUnitInfo { 2404 CXIndex CIdx; 2405 const char *source_filename; 2406 const char *const *command_line_args; 2407 int num_command_line_args; 2408 struct CXUnsavedFile *unsaved_files; 2409 unsigned num_unsaved_files; 2410 unsigned options; 2411 CXTranslationUnit result; 2412}; 2413static void clang_parseTranslationUnit_Impl(void *UserData) { 2414 ParseTranslationUnitInfo *PTUI = 2415 static_cast<ParseTranslationUnitInfo*>(UserData); 2416 CXIndex CIdx = PTUI->CIdx; 2417 const char *source_filename = PTUI->source_filename; 2418 const char * const *command_line_args = PTUI->command_line_args; 2419 int num_command_line_args = PTUI->num_command_line_args; 2420 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; 2421 unsigned num_unsaved_files = PTUI->num_unsaved_files; 2422 unsigned options = PTUI->options; 2423 PTUI->result = 0; 2424 2425 if (!CIdx) 2426 return; 2427 2428 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2429 2430 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; 2431 // FIXME: Add a flag for modules. 2432 TranslationUnitKind TUKind 2433 = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; 2434 bool CacheCodeCompetionResults 2435 = options & CXTranslationUnit_CacheCompletionResults; 2436 2437 // Configure the diagnostics. 2438 DiagnosticOptions DiagOpts; 2439 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> 2440 Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, 2441 command_line_args)); 2442 2443 // Recover resources if we crash before exiting this function. 2444 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 2445 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 2446 DiagCleanup(Diags.getPtr()); 2447 2448 llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > 2449 RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); 2450 2451 // Recover resources if we crash before exiting this function. 2452 llvm::CrashRecoveryContextCleanupRegistrar< 2453 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); 2454 2455 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2456 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2457 const llvm::MemoryBuffer *Buffer 2458 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2459 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, 2460 Buffer)); 2461 } 2462 2463 llvm::OwningPtr<std::vector<const char *> > 2464 Args(new std::vector<const char*>()); 2465 2466 // Recover resources if we crash before exiting this method. 2467 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > 2468 ArgsCleanup(Args.get()); 2469 2470 // Since the Clang C library is primarily used by batch tools dealing with 2471 // (often very broken) source code, where spell-checking can have a 2472 // significant negative impact on performance (particularly when 2473 // precompiled headers are involved), we disable it by default. 2474 // Only do this if we haven't found a spell-checking-related argument. 2475 bool FoundSpellCheckingArgument = false; 2476 for (int I = 0; I != num_command_line_args; ++I) { 2477 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || 2478 strcmp(command_line_args[I], "-fspell-checking") == 0) { 2479 FoundSpellCheckingArgument = true; 2480 break; 2481 } 2482 } 2483 if (!FoundSpellCheckingArgument) 2484 Args->push_back("-fno-spell-checking"); 2485 2486 Args->insert(Args->end(), command_line_args, 2487 command_line_args + num_command_line_args); 2488 2489 // The 'source_filename' argument is optional. If the caller does not 2490 // specify it then it is assumed that the source file is specified 2491 // in the actual argument list. 2492 // Put the source file after command_line_args otherwise if '-x' flag is 2493 // present it will be unused. 2494 if (source_filename) 2495 Args->push_back(source_filename); 2496 2497 // Do we need the detailed preprocessing record? 2498 bool NestedMacroExpansions = false; 2499 if (options & CXTranslationUnit_DetailedPreprocessingRecord) { 2500 Args->push_back("-Xclang"); 2501 Args->push_back("-detailed-preprocessing-record"); 2502 NestedMacroExpansions 2503 = (options & CXTranslationUnit_NestedMacroExpansions); 2504 } 2505 2506 unsigned NumErrors = Diags->getClient()->getNumErrors(); 2507 llvm::OwningPtr<ASTUnit> Unit( 2508 ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 2509 /* vector::data() not portable */, 2510 Args->size() ? (&(*Args)[0] + Args->size()) :0, 2511 Diags, 2512 CXXIdx->getClangResourcesPath(), 2513 CXXIdx->getOnlyLocalDecls(), 2514 /*CaptureDiagnostics=*/true, 2515 RemappedFiles->size() ? &(*RemappedFiles)[0]:0, 2516 RemappedFiles->size(), 2517 /*RemappedFilesKeepOriginalName=*/true, 2518 PrecompilePreamble, 2519 TUKind, 2520 CacheCodeCompetionResults, 2521 NestedMacroExpansions)); 2522 2523 if (NumErrors != Diags->getClient()->getNumErrors()) { 2524 // Make sure to check that 'Unit' is non-NULL. 2525 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { 2526 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), 2527 DEnd = Unit->stored_diag_end(); 2528 D != DEnd; ++D) { 2529 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); 2530 CXString Msg = clang_formatDiagnostic(&Diag, 2531 clang_defaultDiagnosticDisplayOptions()); 2532 fprintf(stderr, "%s\n", clang_getCString(Msg)); 2533 clang_disposeString(Msg); 2534 } 2535#ifdef LLVM_ON_WIN32 2536 // On Windows, force a flush, since there may be multiple copies of 2537 // stderr and stdout in the file system, all with different buffers 2538 // but writing to the same device. 2539 fflush(stderr); 2540#endif 2541 } 2542 } 2543 2544 PTUI->result = MakeCXTranslationUnit(Unit.take()); 2545} 2546CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, 2547 const char *source_filename, 2548 const char * const *command_line_args, 2549 int num_command_line_args, 2550 struct CXUnsavedFile *unsaved_files, 2551 unsigned num_unsaved_files, 2552 unsigned options) { 2553 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, 2554 num_command_line_args, unsaved_files, 2555 num_unsaved_files, options, 0 }; 2556 llvm::CrashRecoveryContext CRC; 2557 2558 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { 2559 fprintf(stderr, "libclang: crash detected during parsing: {\n"); 2560 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); 2561 fprintf(stderr, " 'command_line_args' : ["); 2562 for (int i = 0; i != num_command_line_args; ++i) { 2563 if (i) 2564 fprintf(stderr, ", "); 2565 fprintf(stderr, "'%s'", command_line_args[i]); 2566 } 2567 fprintf(stderr, "],\n"); 2568 fprintf(stderr, " 'unsaved_files' : ["); 2569 for (unsigned i = 0; i != num_unsaved_files; ++i) { 2570 if (i) 2571 fprintf(stderr, ", "); 2572 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, 2573 unsaved_files[i].Length); 2574 } 2575 fprintf(stderr, "],\n"); 2576 fprintf(stderr, " 'options' : %d,\n", options); 2577 fprintf(stderr, "}\n"); 2578 2579 return 0; 2580 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { 2581 PrintLibclangResourceUsage(PTUI.result); 2582 } 2583 2584 return PTUI.result; 2585} 2586 2587unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { 2588 return CXSaveTranslationUnit_None; 2589} 2590 2591int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, 2592 unsigned options) { 2593 if (!TU) 2594 return CXSaveError_InvalidTU; 2595 2596 CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); 2597 if (getenv("LIBCLANG_RESOURCE_USAGE")) 2598 PrintLibclangResourceUsage(TU); 2599 return result; 2600} 2601 2602void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { 2603 if (CTUnit) { 2604 // If the translation unit has been marked as unsafe to free, just discard 2605 // it. 2606 if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) 2607 return; 2608 2609 delete static_cast<ASTUnit *>(CTUnit->TUData); 2610 disposeCXStringPool(CTUnit->StringPool); 2611 delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); 2612 delete CTUnit; 2613 } 2614} 2615 2616unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { 2617 return CXReparse_None; 2618} 2619 2620struct ReparseTranslationUnitInfo { 2621 CXTranslationUnit TU; 2622 unsigned num_unsaved_files; 2623 struct CXUnsavedFile *unsaved_files; 2624 unsigned options; 2625 int result; 2626}; 2627 2628static void clang_reparseTranslationUnit_Impl(void *UserData) { 2629 ReparseTranslationUnitInfo *RTUI = 2630 static_cast<ReparseTranslationUnitInfo*>(UserData); 2631 CXTranslationUnit TU = RTUI->TU; 2632 2633 // Reset the associated diagnostics. 2634 delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); 2635 TU->Diagnostics = 0; 2636 2637 unsigned num_unsaved_files = RTUI->num_unsaved_files; 2638 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; 2639 unsigned options = RTUI->options; 2640 (void) options; 2641 RTUI->result = 1; 2642 2643 if (!TU) 2644 return; 2645 2646 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 2647 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2648 2649 llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > 2650 RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); 2651 2652 // Recover resources if we crash before exiting this function. 2653 llvm::CrashRecoveryContextCleanupRegistrar< 2654 std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); 2655 2656 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2657 StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2658 const llvm::MemoryBuffer *Buffer 2659 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2660 RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, 2661 Buffer)); 2662 } 2663 2664 if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, 2665 RemappedFiles->size())) 2666 RTUI->result = 0; 2667} 2668 2669int clang_reparseTranslationUnit(CXTranslationUnit TU, 2670 unsigned num_unsaved_files, 2671 struct CXUnsavedFile *unsaved_files, 2672 unsigned options) { 2673 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, 2674 options, 0 }; 2675 2676 if (getenv("LIBCLANG_NOTHREADS")) { 2677 clang_reparseTranslationUnit_Impl(&RTUI); 2678 return RTUI.result; 2679 } 2680 2681 llvm::CrashRecoveryContext CRC; 2682 2683 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { 2684 fprintf(stderr, "libclang: crash detected during reparsing\n"); 2685 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 2686 return 1; 2687 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) 2688 PrintLibclangResourceUsage(TU); 2689 2690 return RTUI.result; 2691} 2692 2693 2694CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { 2695 if (!CTUnit) 2696 return createCXString(""); 2697 2698 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); 2699 return createCXString(CXXUnit->getOriginalSourceFileName(), true); 2700} 2701 2702CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { 2703 CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; 2704 return Result; 2705} 2706 2707} // end: extern "C" 2708 2709//===----------------------------------------------------------------------===// 2710// CXFile Operations. 2711//===----------------------------------------------------------------------===// 2712 2713extern "C" { 2714CXString clang_getFileName(CXFile SFile) { 2715 if (!SFile) 2716 return createCXString((const char*)NULL); 2717 2718 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2719 return createCXString(FEnt->getName()); 2720} 2721 2722time_t clang_getFileTime(CXFile SFile) { 2723 if (!SFile) 2724 return 0; 2725 2726 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2727 return FEnt->getModificationTime(); 2728} 2729 2730CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { 2731 if (!tu) 2732 return 0; 2733 2734 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2735 2736 FileManager &FMgr = CXXUnit->getFileManager(); 2737 return const_cast<FileEntry *>(FMgr.getFile(file_name)); 2738} 2739 2740unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { 2741 if (!tu || !file) 2742 return 0; 2743 2744 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2745 FileEntry *FEnt = static_cast<FileEntry *>(file); 2746 return CXXUnit->getPreprocessor().getHeaderSearchInfo() 2747 .isFileMultipleIncludeGuarded(FEnt); 2748} 2749 2750} // end: extern "C" 2751 2752//===----------------------------------------------------------------------===// 2753// CXCursor Operations. 2754//===----------------------------------------------------------------------===// 2755 2756static Decl *getDeclFromExpr(Stmt *E) { 2757 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) 2758 return getDeclFromExpr(CE->getSubExpr()); 2759 2760 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) 2761 return RefExpr->getDecl(); 2762 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) 2763 return RefExpr->getDecl(); 2764 if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) 2765 return ME->getMemberDecl(); 2766 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) 2767 return RE->getDecl(); 2768 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) 2769 return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; 2770 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) 2771 return getDeclFromExpr(POE->getSyntacticForm()); 2772 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) 2773 if (Expr *Src = OVE->getSourceExpr()) 2774 return getDeclFromExpr(Src); 2775 2776 if (CallExpr *CE = dyn_cast<CallExpr>(E)) 2777 return getDeclFromExpr(CE->getCallee()); 2778 if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) 2779 if (!CE->isElidable()) 2780 return CE->getConstructor(); 2781 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) 2782 return OME->getMethodDecl(); 2783 2784 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) 2785 return PE->getProtocol(); 2786 if (SubstNonTypeTemplateParmPackExpr *NTTP 2787 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) 2788 return NTTP->getParameterPack(); 2789 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2790 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || 2791 isa<ParmVarDecl>(SizeOfPack->getPack())) 2792 return SizeOfPack->getPack(); 2793 2794 return 0; 2795} 2796 2797static SourceLocation getLocationFromExpr(Expr *E) { 2798 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) 2799 return getLocationFromExpr(CE->getSubExpr()); 2800 2801 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) 2802 return /*FIXME:*/Msg->getLeftLoc(); 2803 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 2804 return DRE->getLocation(); 2805 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) 2806 return RefExpr->getLocation(); 2807 if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) 2808 return Member->getMemberLoc(); 2809 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) 2810 return Ivar->getLocation(); 2811 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2812 return SizeOfPack->getPackLoc(); 2813 2814 return E->getLocStart(); 2815} 2816 2817extern "C" { 2818 2819unsigned clang_visitChildren(CXCursor parent, 2820 CXCursorVisitor visitor, 2821 CXClientData client_data) { 2822 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, 2823 /*VisitPreprocessorLast=*/false); 2824 return CursorVis.VisitChildren(parent); 2825} 2826 2827#ifndef __has_feature 2828#define __has_feature(x) 0 2829#endif 2830#if __has_feature(blocks) 2831typedef enum CXChildVisitResult 2832 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); 2833 2834static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 2835 CXClientData client_data) { 2836 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 2837 return block(cursor, parent); 2838} 2839#else 2840// If we are compiled with a compiler that doesn't have native blocks support, 2841// define and call the block manually, so the 2842typedef struct _CXChildVisitResult 2843{ 2844 void *isa; 2845 int flags; 2846 int reserved; 2847 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, 2848 CXCursor); 2849} *CXCursorVisitorBlock; 2850 2851static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 2852 CXClientData client_data) { 2853 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 2854 return block->invoke(block, cursor, parent); 2855} 2856#endif 2857 2858 2859unsigned clang_visitChildrenWithBlock(CXCursor parent, 2860 CXCursorVisitorBlock block) { 2861 return clang_visitChildren(parent, visitWithBlock, block); 2862} 2863 2864static CXString getDeclSpelling(Decl *D) { 2865 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); 2866 if (!ND) { 2867 if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) 2868 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 2869 return createCXString(Property->getIdentifier()->getName()); 2870 2871 return createCXString(""); 2872 } 2873 2874 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) 2875 return createCXString(OMD->getSelector().getAsString()); 2876 2877 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) 2878 // No, this isn't the same as the code below. getIdentifier() is non-virtual 2879 // and returns different names. NamedDecl returns the class name and 2880 // ObjCCategoryImplDecl returns the category name. 2881 return createCXString(CIMP->getIdentifier()->getNameStart()); 2882 2883 if (isa<UsingDirectiveDecl>(D)) 2884 return createCXString(""); 2885 2886 llvm::SmallString<1024> S; 2887 llvm::raw_svector_ostream os(S); 2888 ND->printName(os); 2889 2890 return createCXString(os.str()); 2891} 2892 2893CXString clang_getCursorSpelling(CXCursor C) { 2894 if (clang_isTranslationUnit(C.kind)) 2895 return clang_getTranslationUnitSpelling( 2896 static_cast<CXTranslationUnit>(C.data[2])); 2897 2898 if (clang_isReference(C.kind)) { 2899 switch (C.kind) { 2900 case CXCursor_ObjCSuperClassRef: { 2901 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; 2902 return createCXString(Super->getIdentifier()->getNameStart()); 2903 } 2904 case CXCursor_ObjCClassRef: { 2905 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; 2906 return createCXString(Class->getIdentifier()->getNameStart()); 2907 } 2908 case CXCursor_ObjCProtocolRef: { 2909 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; 2910 assert(OID && "getCursorSpelling(): Missing protocol decl"); 2911 return createCXString(OID->getIdentifier()->getNameStart()); 2912 } 2913 case CXCursor_CXXBaseSpecifier: { 2914 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); 2915 return createCXString(B->getType().getAsString()); 2916 } 2917 case CXCursor_TypeRef: { 2918 TypeDecl *Type = getCursorTypeRef(C).first; 2919 assert(Type && "Missing type decl"); 2920 2921 return createCXString(getCursorContext(C).getTypeDeclType(Type). 2922 getAsString()); 2923 } 2924 case CXCursor_TemplateRef: { 2925 TemplateDecl *Template = getCursorTemplateRef(C).first; 2926 assert(Template && "Missing template decl"); 2927 2928 return createCXString(Template->getNameAsString()); 2929 } 2930 2931 case CXCursor_NamespaceRef: { 2932 NamedDecl *NS = getCursorNamespaceRef(C).first; 2933 assert(NS && "Missing namespace decl"); 2934 2935 return createCXString(NS->getNameAsString()); 2936 } 2937 2938 case CXCursor_MemberRef: { 2939 FieldDecl *Field = getCursorMemberRef(C).first; 2940 assert(Field && "Missing member decl"); 2941 2942 return createCXString(Field->getNameAsString()); 2943 } 2944 2945 case CXCursor_LabelRef: { 2946 LabelStmt *Label = getCursorLabelRef(C).first; 2947 assert(Label && "Missing label"); 2948 2949 return createCXString(Label->getName()); 2950 } 2951 2952 case CXCursor_OverloadedDeclRef: { 2953 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 2954 if (Decl *D = Storage.dyn_cast<Decl *>()) { 2955 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 2956 return createCXString(ND->getNameAsString()); 2957 return createCXString(""); 2958 } 2959 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 2960 return createCXString(E->getName().getAsString()); 2961 OverloadedTemplateStorage *Ovl 2962 = Storage.get<OverloadedTemplateStorage*>(); 2963 if (Ovl->size() == 0) 2964 return createCXString(""); 2965 return createCXString((*Ovl->begin())->getNameAsString()); 2966 } 2967 2968 default: 2969 return createCXString("<not implemented>"); 2970 } 2971 } 2972 2973 if (clang_isExpression(C.kind)) { 2974 Decl *D = getDeclFromExpr(getCursorExpr(C)); 2975 if (D) 2976 return getDeclSpelling(D); 2977 return createCXString(""); 2978 } 2979 2980 if (clang_isStatement(C.kind)) { 2981 Stmt *S = getCursorStmt(C); 2982 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 2983 return createCXString(Label->getName()); 2984 2985 return createCXString(""); 2986 } 2987 2988 if (C.kind == CXCursor_MacroExpansion) 2989 return createCXString(getCursorMacroExpansion(C)->getName() 2990 ->getNameStart()); 2991 2992 if (C.kind == CXCursor_MacroDefinition) 2993 return createCXString(getCursorMacroDefinition(C)->getName() 2994 ->getNameStart()); 2995 2996 if (C.kind == CXCursor_InclusionDirective) 2997 return createCXString(getCursorInclusionDirective(C)->getFileName()); 2998 2999 if (clang_isDeclaration(C.kind)) 3000 return getDeclSpelling(getCursorDecl(C)); 3001 3002 if (C.kind == CXCursor_AnnotateAttr) { 3003 AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); 3004 return createCXString(AA->getAnnotation()); 3005 } 3006 3007 return createCXString(""); 3008} 3009 3010CXString clang_getCursorDisplayName(CXCursor C) { 3011 if (!clang_isDeclaration(C.kind)) 3012 return clang_getCursorSpelling(C); 3013 3014 Decl *D = getCursorDecl(C); 3015 if (!D) 3016 return createCXString(""); 3017 3018 PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); 3019 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) 3020 D = FunTmpl->getTemplatedDecl(); 3021 3022 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 3023 llvm::SmallString<64> Str; 3024 llvm::raw_svector_ostream OS(Str); 3025 OS << Function->getNameAsString(); 3026 if (Function->getPrimaryTemplate()) 3027 OS << "<>"; 3028 OS << "("; 3029 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { 3030 if (I) 3031 OS << ", "; 3032 OS << Function->getParamDecl(I)->getType().getAsString(Policy); 3033 } 3034 3035 if (Function->isVariadic()) { 3036 if (Function->getNumParams()) 3037 OS << ", "; 3038 OS << "..."; 3039 } 3040 OS << ")"; 3041 return createCXString(OS.str()); 3042 } 3043 3044 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { 3045 llvm::SmallString<64> Str; 3046 llvm::raw_svector_ostream OS(Str); 3047 OS << ClassTemplate->getNameAsString(); 3048 OS << "<"; 3049 TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); 3050 for (unsigned I = 0, N = Params->size(); I != N; ++I) { 3051 if (I) 3052 OS << ", "; 3053 3054 NamedDecl *Param = Params->getParam(I); 3055 if (Param->getIdentifier()) { 3056 OS << Param->getIdentifier()->getName(); 3057 continue; 3058 } 3059 3060 // There is no parameter name, which makes this tricky. Try to come up 3061 // with something useful that isn't too long. 3062 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 3063 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); 3064 else if (NonTypeTemplateParmDecl *NTTP 3065 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 3066 OS << NTTP->getType().getAsString(Policy); 3067 else 3068 OS << "template<...> class"; 3069 } 3070 3071 OS << ">"; 3072 return createCXString(OS.str()); 3073 } 3074 3075 if (ClassTemplateSpecializationDecl *ClassSpec 3076 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 3077 // If the type was explicitly written, use that. 3078 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) 3079 return createCXString(TSInfo->getType().getAsString(Policy)); 3080 3081 llvm::SmallString<64> Str; 3082 llvm::raw_svector_ostream OS(Str); 3083 OS << ClassSpec->getNameAsString(); 3084 OS << TemplateSpecializationType::PrintTemplateArgumentList( 3085 ClassSpec->getTemplateArgs().data(), 3086 ClassSpec->getTemplateArgs().size(), 3087 Policy); 3088 return createCXString(OS.str()); 3089 } 3090 3091 return clang_getCursorSpelling(C); 3092} 3093 3094CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { 3095 switch (Kind) { 3096 case CXCursor_FunctionDecl: 3097 return createCXString("FunctionDecl"); 3098 case CXCursor_TypedefDecl: 3099 return createCXString("TypedefDecl"); 3100 case CXCursor_EnumDecl: 3101 return createCXString("EnumDecl"); 3102 case CXCursor_EnumConstantDecl: 3103 return createCXString("EnumConstantDecl"); 3104 case CXCursor_StructDecl: 3105 return createCXString("StructDecl"); 3106 case CXCursor_UnionDecl: 3107 return createCXString("UnionDecl"); 3108 case CXCursor_ClassDecl: 3109 return createCXString("ClassDecl"); 3110 case CXCursor_FieldDecl: 3111 return createCXString("FieldDecl"); 3112 case CXCursor_VarDecl: 3113 return createCXString("VarDecl"); 3114 case CXCursor_ParmDecl: 3115 return createCXString("ParmDecl"); 3116 case CXCursor_ObjCInterfaceDecl: 3117 return createCXString("ObjCInterfaceDecl"); 3118 case CXCursor_ObjCCategoryDecl: 3119 return createCXString("ObjCCategoryDecl"); 3120 case CXCursor_ObjCProtocolDecl: 3121 return createCXString("ObjCProtocolDecl"); 3122 case CXCursor_ObjCPropertyDecl: 3123 return createCXString("ObjCPropertyDecl"); 3124 case CXCursor_ObjCIvarDecl: 3125 return createCXString("ObjCIvarDecl"); 3126 case CXCursor_ObjCInstanceMethodDecl: 3127 return createCXString("ObjCInstanceMethodDecl"); 3128 case CXCursor_ObjCClassMethodDecl: 3129 return createCXString("ObjCClassMethodDecl"); 3130 case CXCursor_ObjCImplementationDecl: 3131 return createCXString("ObjCImplementationDecl"); 3132 case CXCursor_ObjCCategoryImplDecl: 3133 return createCXString("ObjCCategoryImplDecl"); 3134 case CXCursor_CXXMethod: 3135 return createCXString("CXXMethod"); 3136 case CXCursor_UnexposedDecl: 3137 return createCXString("UnexposedDecl"); 3138 case CXCursor_ObjCSuperClassRef: 3139 return createCXString("ObjCSuperClassRef"); 3140 case CXCursor_ObjCProtocolRef: 3141 return createCXString("ObjCProtocolRef"); 3142 case CXCursor_ObjCClassRef: 3143 return createCXString("ObjCClassRef"); 3144 case CXCursor_TypeRef: 3145 return createCXString("TypeRef"); 3146 case CXCursor_TemplateRef: 3147 return createCXString("TemplateRef"); 3148 case CXCursor_NamespaceRef: 3149 return createCXString("NamespaceRef"); 3150 case CXCursor_MemberRef: 3151 return createCXString("MemberRef"); 3152 case CXCursor_LabelRef: 3153 return createCXString("LabelRef"); 3154 case CXCursor_OverloadedDeclRef: 3155 return createCXString("OverloadedDeclRef"); 3156 case CXCursor_IntegerLiteral: 3157 return createCXString("IntegerLiteral"); 3158 case CXCursor_FloatingLiteral: 3159 return createCXString("FloatingLiteral"); 3160 case CXCursor_ImaginaryLiteral: 3161 return createCXString("ImaginaryLiteral"); 3162 case CXCursor_StringLiteral: 3163 return createCXString("StringLiteral"); 3164 case CXCursor_CharacterLiteral: 3165 return createCXString("CharacterLiteral"); 3166 case CXCursor_ParenExpr: 3167 return createCXString("ParenExpr"); 3168 case CXCursor_UnaryOperator: 3169 return createCXString("UnaryOperator"); 3170 case CXCursor_ArraySubscriptExpr: 3171 return createCXString("ArraySubscriptExpr"); 3172 case CXCursor_BinaryOperator: 3173 return createCXString("BinaryOperator"); 3174 case CXCursor_CompoundAssignOperator: 3175 return createCXString("CompoundAssignOperator"); 3176 case CXCursor_ConditionalOperator: 3177 return createCXString("ConditionalOperator"); 3178 case CXCursor_CStyleCastExpr: 3179 return createCXString("CStyleCastExpr"); 3180 case CXCursor_CompoundLiteralExpr: 3181 return createCXString("CompoundLiteralExpr"); 3182 case CXCursor_InitListExpr: 3183 return createCXString("InitListExpr"); 3184 case CXCursor_AddrLabelExpr: 3185 return createCXString("AddrLabelExpr"); 3186 case CXCursor_StmtExpr: 3187 return createCXString("StmtExpr"); 3188 case CXCursor_GenericSelectionExpr: 3189 return createCXString("GenericSelectionExpr"); 3190 case CXCursor_GNUNullExpr: 3191 return createCXString("GNUNullExpr"); 3192 case CXCursor_CXXStaticCastExpr: 3193 return createCXString("CXXStaticCastExpr"); 3194 case CXCursor_CXXDynamicCastExpr: 3195 return createCXString("CXXDynamicCastExpr"); 3196 case CXCursor_CXXReinterpretCastExpr: 3197 return createCXString("CXXReinterpretCastExpr"); 3198 case CXCursor_CXXConstCastExpr: 3199 return createCXString("CXXConstCastExpr"); 3200 case CXCursor_CXXFunctionalCastExpr: 3201 return createCXString("CXXFunctionalCastExpr"); 3202 case CXCursor_CXXTypeidExpr: 3203 return createCXString("CXXTypeidExpr"); 3204 case CXCursor_CXXBoolLiteralExpr: 3205 return createCXString("CXXBoolLiteralExpr"); 3206 case CXCursor_CXXNullPtrLiteralExpr: 3207 return createCXString("CXXNullPtrLiteralExpr"); 3208 case CXCursor_CXXThisExpr: 3209 return createCXString("CXXThisExpr"); 3210 case CXCursor_CXXThrowExpr: 3211 return createCXString("CXXThrowExpr"); 3212 case CXCursor_CXXNewExpr: 3213 return createCXString("CXXNewExpr"); 3214 case CXCursor_CXXDeleteExpr: 3215 return createCXString("CXXDeleteExpr"); 3216 case CXCursor_UnaryExpr: 3217 return createCXString("UnaryExpr"); 3218 case CXCursor_ObjCStringLiteral: 3219 return createCXString("ObjCStringLiteral"); 3220 case CXCursor_ObjCEncodeExpr: 3221 return createCXString("ObjCEncodeExpr"); 3222 case CXCursor_ObjCSelectorExpr: 3223 return createCXString("ObjCSelectorExpr"); 3224 case CXCursor_ObjCProtocolExpr: 3225 return createCXString("ObjCProtocolExpr"); 3226 case CXCursor_ObjCBridgedCastExpr: 3227 return createCXString("ObjCBridgedCastExpr"); 3228 case CXCursor_BlockExpr: 3229 return createCXString("BlockExpr"); 3230 case CXCursor_PackExpansionExpr: 3231 return createCXString("PackExpansionExpr"); 3232 case CXCursor_SizeOfPackExpr: 3233 return createCXString("SizeOfPackExpr"); 3234 case CXCursor_UnexposedExpr: 3235 return createCXString("UnexposedExpr"); 3236 case CXCursor_DeclRefExpr: 3237 return createCXString("DeclRefExpr"); 3238 case CXCursor_MemberRefExpr: 3239 return createCXString("MemberRefExpr"); 3240 case CXCursor_CallExpr: 3241 return createCXString("CallExpr"); 3242 case CXCursor_ObjCMessageExpr: 3243 return createCXString("ObjCMessageExpr"); 3244 case CXCursor_UnexposedStmt: 3245 return createCXString("UnexposedStmt"); 3246 case CXCursor_DeclStmt: 3247 return createCXString("DeclStmt"); 3248 case CXCursor_LabelStmt: 3249 return createCXString("LabelStmt"); 3250 case CXCursor_CompoundStmt: 3251 return createCXString("CompoundStmt"); 3252 case CXCursor_CaseStmt: 3253 return createCXString("CaseStmt"); 3254 case CXCursor_DefaultStmt: 3255 return createCXString("DefaultStmt"); 3256 case CXCursor_IfStmt: 3257 return createCXString("IfStmt"); 3258 case CXCursor_SwitchStmt: 3259 return createCXString("SwitchStmt"); 3260 case CXCursor_WhileStmt: 3261 return createCXString("WhileStmt"); 3262 case CXCursor_DoStmt: 3263 return createCXString("DoStmt"); 3264 case CXCursor_ForStmt: 3265 return createCXString("ForStmt"); 3266 case CXCursor_GotoStmt: 3267 return createCXString("GotoStmt"); 3268 case CXCursor_IndirectGotoStmt: 3269 return createCXString("IndirectGotoStmt"); 3270 case CXCursor_ContinueStmt: 3271 return createCXString("ContinueStmt"); 3272 case CXCursor_BreakStmt: 3273 return createCXString("BreakStmt"); 3274 case CXCursor_ReturnStmt: 3275 return createCXString("ReturnStmt"); 3276 case CXCursor_AsmStmt: 3277 return createCXString("AsmStmt"); 3278 case CXCursor_ObjCAtTryStmt: 3279 return createCXString("ObjCAtTryStmt"); 3280 case CXCursor_ObjCAtCatchStmt: 3281 return createCXString("ObjCAtCatchStmt"); 3282 case CXCursor_ObjCAtFinallyStmt: 3283 return createCXString("ObjCAtFinallyStmt"); 3284 case CXCursor_ObjCAtThrowStmt: 3285 return createCXString("ObjCAtThrowStmt"); 3286 case CXCursor_ObjCAtSynchronizedStmt: 3287 return createCXString("ObjCAtSynchronizedStmt"); 3288 case CXCursor_ObjCAutoreleasePoolStmt: 3289 return createCXString("ObjCAutoreleasePoolStmt"); 3290 case CXCursor_ObjCForCollectionStmt: 3291 return createCXString("ObjCForCollectionStmt"); 3292 case CXCursor_CXXCatchStmt: 3293 return createCXString("CXXCatchStmt"); 3294 case CXCursor_CXXTryStmt: 3295 return createCXString("CXXTryStmt"); 3296 case CXCursor_CXXForRangeStmt: 3297 return createCXString("CXXForRangeStmt"); 3298 case CXCursor_SEHTryStmt: 3299 return createCXString("SEHTryStmt"); 3300 case CXCursor_SEHExceptStmt: 3301 return createCXString("SEHExceptStmt"); 3302 case CXCursor_SEHFinallyStmt: 3303 return createCXString("SEHFinallyStmt"); 3304 case CXCursor_NullStmt: 3305 return createCXString("NullStmt"); 3306 case CXCursor_InvalidFile: 3307 return createCXString("InvalidFile"); 3308 case CXCursor_InvalidCode: 3309 return createCXString("InvalidCode"); 3310 case CXCursor_NoDeclFound: 3311 return createCXString("NoDeclFound"); 3312 case CXCursor_NotImplemented: 3313 return createCXString("NotImplemented"); 3314 case CXCursor_TranslationUnit: 3315 return createCXString("TranslationUnit"); 3316 case CXCursor_UnexposedAttr: 3317 return createCXString("UnexposedAttr"); 3318 case CXCursor_IBActionAttr: 3319 return createCXString("attribute(ibaction)"); 3320 case CXCursor_IBOutletAttr: 3321 return createCXString("attribute(iboutlet)"); 3322 case CXCursor_IBOutletCollectionAttr: 3323 return createCXString("attribute(iboutletcollection)"); 3324 case CXCursor_CXXFinalAttr: 3325 return createCXString("attribute(final)"); 3326 case CXCursor_CXXOverrideAttr: 3327 return createCXString("attribute(override)"); 3328 case CXCursor_AnnotateAttr: 3329 return createCXString("attribute(annotate)"); 3330 case CXCursor_PreprocessingDirective: 3331 return createCXString("preprocessing directive"); 3332 case CXCursor_MacroDefinition: 3333 return createCXString("macro definition"); 3334 case CXCursor_MacroExpansion: 3335 return createCXString("macro expansion"); 3336 case CXCursor_InclusionDirective: 3337 return createCXString("inclusion directive"); 3338 case CXCursor_Namespace: 3339 return createCXString("Namespace"); 3340 case CXCursor_LinkageSpec: 3341 return createCXString("LinkageSpec"); 3342 case CXCursor_CXXBaseSpecifier: 3343 return createCXString("C++ base class specifier"); 3344 case CXCursor_Constructor: 3345 return createCXString("CXXConstructor"); 3346 case CXCursor_Destructor: 3347 return createCXString("CXXDestructor"); 3348 case CXCursor_ConversionFunction: 3349 return createCXString("CXXConversion"); 3350 case CXCursor_TemplateTypeParameter: 3351 return createCXString("TemplateTypeParameter"); 3352 case CXCursor_NonTypeTemplateParameter: 3353 return createCXString("NonTypeTemplateParameter"); 3354 case CXCursor_TemplateTemplateParameter: 3355 return createCXString("TemplateTemplateParameter"); 3356 case CXCursor_FunctionTemplate: 3357 return createCXString("FunctionTemplate"); 3358 case CXCursor_ClassTemplate: 3359 return createCXString("ClassTemplate"); 3360 case CXCursor_ClassTemplatePartialSpecialization: 3361 return createCXString("ClassTemplatePartialSpecialization"); 3362 case CXCursor_NamespaceAlias: 3363 return createCXString("NamespaceAlias"); 3364 case CXCursor_UsingDirective: 3365 return createCXString("UsingDirective"); 3366 case CXCursor_UsingDeclaration: 3367 return createCXString("UsingDeclaration"); 3368 case CXCursor_TypeAliasDecl: 3369 return createCXString("TypeAliasDecl"); 3370 case CXCursor_ObjCSynthesizeDecl: 3371 return createCXString("ObjCSynthesizeDecl"); 3372 case CXCursor_ObjCDynamicDecl: 3373 return createCXString("ObjCDynamicDecl"); 3374 case CXCursor_CXXAccessSpecifier: 3375 return createCXString("CXXAccessSpecifier"); 3376 } 3377 3378 llvm_unreachable("Unhandled CXCursorKind"); 3379 return createCXString((const char*) 0); 3380} 3381 3382struct GetCursorData { 3383 SourceLocation TokenBeginLoc; 3384 bool PointsAtMacroArgExpansion; 3385 CXCursor &BestCursor; 3386 3387 GetCursorData(SourceManager &SM, 3388 SourceLocation tokenBegin, CXCursor &outputCursor) 3389 : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { 3390 PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); 3391 } 3392}; 3393 3394static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, 3395 CXCursor parent, 3396 CXClientData client_data) { 3397 GetCursorData *Data = static_cast<GetCursorData *>(client_data); 3398 CXCursor *BestCursor = &Data->BestCursor; 3399 3400 // If we point inside a macro argument we should provide info of what the 3401 // token is so use the actual cursor, don't replace it with a macro expansion 3402 // cursor. 3403 if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) 3404 return CXChildVisit_Recurse; 3405 3406 if (clang_isDeclaration(cursor.kind)) { 3407 // Avoid having the implicit methods override the property decls. 3408 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCursorDecl(cursor))) 3409 if (MD->isImplicit()) 3410 return CXChildVisit_Break; 3411 } 3412 3413 if (clang_isExpression(cursor.kind) && 3414 clang_isDeclaration(BestCursor->kind)) { 3415 Decl *D = getCursorDecl(*BestCursor); 3416 3417 // Avoid having the cursor of an expression replace the declaration cursor 3418 // when the expression source range overlaps the declaration range. 3419 // This can happen for C++ constructor expressions whose range generally 3420 // include the variable declaration, e.g.: 3421 // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. 3422 if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && 3423 D->getLocation() == Data->TokenBeginLoc) 3424 return CXChildVisit_Break; 3425 } 3426 3427 // If our current best cursor is the construction of a temporary object, 3428 // don't replace that cursor with a type reference, because we want 3429 // clang_getCursor() to point at the constructor. 3430 if (clang_isExpression(BestCursor->kind) && 3431 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && 3432 cursor.kind == CXCursor_TypeRef) { 3433 // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it 3434 // as having the actual point on the type reference. 3435 *BestCursor = getTypeRefedCallExprCursor(*BestCursor); 3436 return CXChildVisit_Recurse; 3437 } 3438 3439 *BestCursor = cursor; 3440 return CXChildVisit_Recurse; 3441} 3442 3443CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { 3444 if (!TU) 3445 return clang_getNullCursor(); 3446 3447 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 3448 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 3449 3450 SourceLocation SLoc = cxloc::translateSourceLocation(Loc); 3451 CXCursor Result = cxcursor::getCursor(TU, SLoc); 3452 3453 bool Logging = getenv("LIBCLANG_LOGGING"); 3454 if (Logging) { 3455 CXFile SearchFile; 3456 unsigned SearchLine, SearchColumn; 3457 CXFile ResultFile; 3458 unsigned ResultLine, ResultColumn; 3459 CXString SearchFileName, ResultFileName, KindSpelling, USR; 3460 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; 3461 CXSourceLocation ResultLoc = clang_getCursorLocation(Result); 3462 3463 clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); 3464 clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, 3465 &ResultColumn, 0); 3466 SearchFileName = clang_getFileName(SearchFile); 3467 ResultFileName = clang_getFileName(ResultFile); 3468 KindSpelling = clang_getCursorKindSpelling(Result.kind); 3469 USR = clang_getCursorUSR(Result); 3470 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n", 3471 clang_getCString(SearchFileName), SearchLine, SearchColumn, 3472 clang_getCString(KindSpelling), 3473 clang_getCString(ResultFileName), ResultLine, ResultColumn, 3474 clang_getCString(USR), IsDef); 3475 clang_disposeString(SearchFileName); 3476 clang_disposeString(ResultFileName); 3477 clang_disposeString(KindSpelling); 3478 clang_disposeString(USR); 3479 3480 CXCursor Definition = clang_getCursorDefinition(Result); 3481 if (!clang_equalCursors(Definition, clang_getNullCursor())) { 3482 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); 3483 CXString DefinitionKindSpelling 3484 = clang_getCursorKindSpelling(Definition.kind); 3485 CXFile DefinitionFile; 3486 unsigned DefinitionLine, DefinitionColumn; 3487 clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, 3488 &DefinitionLine, &DefinitionColumn, 0); 3489 CXString DefinitionFileName = clang_getFileName(DefinitionFile); 3490 fprintf(stderr, " -> %s(%s:%d:%d)\n", 3491 clang_getCString(DefinitionKindSpelling), 3492 clang_getCString(DefinitionFileName), 3493 DefinitionLine, DefinitionColumn); 3494 clang_disposeString(DefinitionFileName); 3495 clang_disposeString(DefinitionKindSpelling); 3496 } 3497 } 3498 3499 return Result; 3500} 3501 3502CXCursor clang_getNullCursor(void) { 3503 return MakeCXCursorInvalid(CXCursor_InvalidFile); 3504} 3505 3506unsigned clang_equalCursors(CXCursor X, CXCursor Y) { 3507 return X == Y; 3508} 3509 3510unsigned clang_hashCursor(CXCursor C) { 3511 unsigned Index = 0; 3512 if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) 3513 Index = 1; 3514 3515 return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( 3516 std::make_pair(C.kind, C.data[Index])); 3517} 3518 3519unsigned clang_isInvalid(enum CXCursorKind K) { 3520 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; 3521} 3522 3523unsigned clang_isDeclaration(enum CXCursorKind K) { 3524 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; 3525} 3526 3527unsigned clang_isReference(enum CXCursorKind K) { 3528 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; 3529} 3530 3531unsigned clang_isExpression(enum CXCursorKind K) { 3532 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; 3533} 3534 3535unsigned clang_isStatement(enum CXCursorKind K) { 3536 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; 3537} 3538 3539unsigned clang_isAttribute(enum CXCursorKind K) { 3540 return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; 3541} 3542 3543unsigned clang_isTranslationUnit(enum CXCursorKind K) { 3544 return K == CXCursor_TranslationUnit; 3545} 3546 3547unsigned clang_isPreprocessing(enum CXCursorKind K) { 3548 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; 3549} 3550 3551unsigned clang_isUnexposed(enum CXCursorKind K) { 3552 switch (K) { 3553 case CXCursor_UnexposedDecl: 3554 case CXCursor_UnexposedExpr: 3555 case CXCursor_UnexposedStmt: 3556 case CXCursor_UnexposedAttr: 3557 return true; 3558 default: 3559 return false; 3560 } 3561} 3562 3563CXCursorKind clang_getCursorKind(CXCursor C) { 3564 return C.kind; 3565} 3566 3567CXSourceLocation clang_getCursorLocation(CXCursor C) { 3568 if (clang_isReference(C.kind)) { 3569 switch (C.kind) { 3570 case CXCursor_ObjCSuperClassRef: { 3571 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3572 = getCursorObjCSuperClassRef(C); 3573 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3574 } 3575 3576 case CXCursor_ObjCProtocolRef: { 3577 std::pair<ObjCProtocolDecl *, SourceLocation> P 3578 = getCursorObjCProtocolRef(C); 3579 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3580 } 3581 3582 case CXCursor_ObjCClassRef: { 3583 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3584 = getCursorObjCClassRef(C); 3585 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3586 } 3587 3588 case CXCursor_TypeRef: { 3589 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); 3590 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3591 } 3592 3593 case CXCursor_TemplateRef: { 3594 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); 3595 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3596 } 3597 3598 case CXCursor_NamespaceRef: { 3599 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); 3600 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3601 } 3602 3603 case CXCursor_MemberRef: { 3604 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); 3605 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3606 } 3607 3608 case CXCursor_CXXBaseSpecifier: { 3609 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); 3610 if (!BaseSpec) 3611 return clang_getNullLocation(); 3612 3613 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) 3614 return cxloc::translateSourceLocation(getCursorContext(C), 3615 TSInfo->getTypeLoc().getBeginLoc()); 3616 3617 return cxloc::translateSourceLocation(getCursorContext(C), 3618 BaseSpec->getSourceRange().getBegin()); 3619 } 3620 3621 case CXCursor_LabelRef: { 3622 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); 3623 return cxloc::translateSourceLocation(getCursorContext(C), P.second); 3624 } 3625 3626 case CXCursor_OverloadedDeclRef: 3627 return cxloc::translateSourceLocation(getCursorContext(C), 3628 getCursorOverloadedDeclRef(C).second); 3629 3630 default: 3631 // FIXME: Need a way to enumerate all non-reference cases. 3632 llvm_unreachable("Missed a reference kind"); 3633 } 3634 } 3635 3636 if (clang_isExpression(C.kind)) 3637 return cxloc::translateSourceLocation(getCursorContext(C), 3638 getLocationFromExpr(getCursorExpr(C))); 3639 3640 if (clang_isStatement(C.kind)) 3641 return cxloc::translateSourceLocation(getCursorContext(C), 3642 getCursorStmt(C)->getLocStart()); 3643 3644 if (C.kind == CXCursor_PreprocessingDirective) { 3645 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); 3646 return cxloc::translateSourceLocation(getCursorContext(C), L); 3647 } 3648 3649 if (C.kind == CXCursor_MacroExpansion) { 3650 SourceLocation L 3651 = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); 3652 return cxloc::translateSourceLocation(getCursorContext(C), L); 3653 } 3654 3655 if (C.kind == CXCursor_MacroDefinition) { 3656 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); 3657 return cxloc::translateSourceLocation(getCursorContext(C), L); 3658 } 3659 3660 if (C.kind == CXCursor_InclusionDirective) { 3661 SourceLocation L 3662 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); 3663 return cxloc::translateSourceLocation(getCursorContext(C), L); 3664 } 3665 3666 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) 3667 return clang_getNullLocation(); 3668 3669 Decl *D = getCursorDecl(C); 3670 SourceLocation Loc = D->getLocation(); 3671 // FIXME: Multiple variables declared in a single declaration 3672 // currently lack the information needed to correctly determine their 3673 // ranges when accounting for the type-specifier. We use context 3674 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3675 // and if so, whether it is the first decl. 3676 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3677 if (!cxcursor::isFirstInDeclGroup(C)) 3678 Loc = VD->getLocation(); 3679 } 3680 3681 return cxloc::translateSourceLocation(getCursorContext(C), Loc); 3682} 3683 3684} // end extern "C" 3685 3686CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { 3687 assert(TU); 3688 3689 // Guard against an invalid SourceLocation, or we may assert in one 3690 // of the following calls. 3691 if (SLoc.isInvalid()) 3692 return clang_getNullCursor(); 3693 3694 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 3695 3696 // Translate the given source location to make it point at the beginning of 3697 // the token under the cursor. 3698 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), 3699 CXXUnit->getASTContext().getLangOptions()); 3700 3701 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); 3702 if (SLoc.isValid()) { 3703 GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); 3704 CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, 3705 /*VisitPreprocessorLast=*/true, 3706 /*VisitIncludedEntities=*/false, 3707 SourceLocation(SLoc)); 3708 CursorVis.visitFileRegion(); 3709 } 3710 3711 return Result; 3712} 3713 3714static SourceRange getRawCursorExtent(CXCursor C) { 3715 if (clang_isReference(C.kind)) { 3716 switch (C.kind) { 3717 case CXCursor_ObjCSuperClassRef: 3718 return getCursorObjCSuperClassRef(C).second; 3719 3720 case CXCursor_ObjCProtocolRef: 3721 return getCursorObjCProtocolRef(C).second; 3722 3723 case CXCursor_ObjCClassRef: 3724 return getCursorObjCClassRef(C).second; 3725 3726 case CXCursor_TypeRef: 3727 return getCursorTypeRef(C).second; 3728 3729 case CXCursor_TemplateRef: 3730 return getCursorTemplateRef(C).second; 3731 3732 case CXCursor_NamespaceRef: 3733 return getCursorNamespaceRef(C).second; 3734 3735 case CXCursor_MemberRef: 3736 return getCursorMemberRef(C).second; 3737 3738 case CXCursor_CXXBaseSpecifier: 3739 return getCursorCXXBaseSpecifier(C)->getSourceRange(); 3740 3741 case CXCursor_LabelRef: 3742 return getCursorLabelRef(C).second; 3743 3744 case CXCursor_OverloadedDeclRef: 3745 return getCursorOverloadedDeclRef(C).second; 3746 3747 default: 3748 // FIXME: Need a way to enumerate all non-reference cases. 3749 llvm_unreachable("Missed a reference kind"); 3750 } 3751 } 3752 3753 if (clang_isExpression(C.kind)) 3754 return getCursorExpr(C)->getSourceRange(); 3755 3756 if (clang_isStatement(C.kind)) 3757 return getCursorStmt(C)->getSourceRange(); 3758 3759 if (clang_isAttribute(C.kind)) 3760 return getCursorAttr(C)->getRange(); 3761 3762 if (C.kind == CXCursor_PreprocessingDirective) 3763 return cxcursor::getCursorPreprocessingDirective(C); 3764 3765 if (C.kind == CXCursor_MacroExpansion) { 3766 ASTUnit *TU = getCursorASTUnit(C); 3767 SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); 3768 return TU->mapRangeFromPreamble(Range); 3769 } 3770 3771 if (C.kind == CXCursor_MacroDefinition) { 3772 ASTUnit *TU = getCursorASTUnit(C); 3773 SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); 3774 return TU->mapRangeFromPreamble(Range); 3775 } 3776 3777 if (C.kind == CXCursor_InclusionDirective) { 3778 ASTUnit *TU = getCursorASTUnit(C); 3779 SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); 3780 return TU->mapRangeFromPreamble(Range); 3781 } 3782 3783 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 3784 Decl *D = cxcursor::getCursorDecl(C); 3785 SourceRange R = D->getSourceRange(); 3786 // FIXME: Multiple variables declared in a single declaration 3787 // currently lack the information needed to correctly determine their 3788 // ranges when accounting for the type-specifier. We use context 3789 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3790 // and if so, whether it is the first decl. 3791 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3792 if (!cxcursor::isFirstInDeclGroup(C)) 3793 R.setBegin(VD->getLocation()); 3794 } 3795 return R; 3796 } 3797 return SourceRange(); 3798} 3799 3800/// \brief Retrieves the "raw" cursor extent, which is then extended to include 3801/// the decl-specifier-seq for declarations. 3802static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { 3803 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 3804 Decl *D = cxcursor::getCursorDecl(C); 3805 SourceRange R = D->getSourceRange(); 3806 3807 // Adjust the start of the location for declarations preceded by 3808 // declaration specifiers. 3809 SourceLocation StartLoc; 3810 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 3811 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 3812 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 3813 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { 3814 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 3815 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 3816 } 3817 3818 if (StartLoc.isValid() && R.getBegin().isValid() && 3819 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) 3820 R.setBegin(StartLoc); 3821 3822 // FIXME: Multiple variables declared in a single declaration 3823 // currently lack the information needed to correctly determine their 3824 // ranges when accounting for the type-specifier. We use context 3825 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3826 // and if so, whether it is the first decl. 3827 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3828 if (!cxcursor::isFirstInDeclGroup(C)) 3829 R.setBegin(VD->getLocation()); 3830 } 3831 3832 return R; 3833 } 3834 3835 return getRawCursorExtent(C); 3836} 3837 3838extern "C" { 3839 3840CXSourceRange clang_getCursorExtent(CXCursor C) { 3841 SourceRange R = getRawCursorExtent(C); 3842 if (R.isInvalid()) 3843 return clang_getNullRange(); 3844 3845 return cxloc::translateSourceRange(getCursorContext(C), R); 3846} 3847 3848CXCursor clang_getCursorReferenced(CXCursor C) { 3849 if (clang_isInvalid(C.kind)) 3850 return clang_getNullCursor(); 3851 3852 CXTranslationUnit tu = getCursorTU(C); 3853 if (clang_isDeclaration(C.kind)) { 3854 Decl *D = getCursorDecl(C); 3855 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 3856 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); 3857 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) 3858 return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); 3859 if (ObjCForwardProtocolDecl *Protocols 3860 = dyn_cast<ObjCForwardProtocolDecl>(D)) 3861 return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); 3862 if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) 3863 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 3864 return MakeCXCursor(Property, tu); 3865 3866 return C; 3867 } 3868 3869 if (clang_isExpression(C.kind)) { 3870 Expr *E = getCursorExpr(C); 3871 Decl *D = getDeclFromExpr(E); 3872 if (D) { 3873 CXCursor declCursor = MakeCXCursor(D, tu); 3874 declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), 3875 declCursor); 3876 return declCursor; 3877 } 3878 3879 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) 3880 return MakeCursorOverloadedDeclRef(Ovl, tu); 3881 3882 return clang_getNullCursor(); 3883 } 3884 3885 if (clang_isStatement(C.kind)) { 3886 Stmt *S = getCursorStmt(C); 3887 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) 3888 if (LabelDecl *label = Goto->getLabel()) 3889 if (LabelStmt *labelS = label->getStmt()) 3890 return MakeCXCursor(labelS, getCursorDecl(C), tu); 3891 3892 return clang_getNullCursor(); 3893 } 3894 3895 if (C.kind == CXCursor_MacroExpansion) { 3896 if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) 3897 return MakeMacroDefinitionCursor(Def, tu); 3898 } 3899 3900 if (!clang_isReference(C.kind)) 3901 return clang_getNullCursor(); 3902 3903 switch (C.kind) { 3904 case CXCursor_ObjCSuperClassRef: 3905 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); 3906 3907 case CXCursor_ObjCProtocolRef: { 3908 return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); 3909 3910 case CXCursor_ObjCClassRef: 3911 return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); 3912 3913 case CXCursor_TypeRef: 3914 return MakeCXCursor(getCursorTypeRef(C).first, tu ); 3915 3916 case CXCursor_TemplateRef: 3917 return MakeCXCursor(getCursorTemplateRef(C).first, tu ); 3918 3919 case CXCursor_NamespaceRef: 3920 return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); 3921 3922 case CXCursor_MemberRef: 3923 return MakeCXCursor(getCursorMemberRef(C).first, tu ); 3924 3925 case CXCursor_CXXBaseSpecifier: { 3926 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); 3927 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), 3928 tu )); 3929 } 3930 3931 case CXCursor_LabelRef: 3932 // FIXME: We end up faking the "parent" declaration here because we 3933 // don't want to make CXCursor larger. 3934 return MakeCXCursor(getCursorLabelRef(C).first, 3935 static_cast<ASTUnit*>(tu->TUData)->getASTContext() 3936 .getTranslationUnitDecl(), 3937 tu); 3938 3939 case CXCursor_OverloadedDeclRef: 3940 return C; 3941 3942 default: 3943 // We would prefer to enumerate all non-reference cursor kinds here. 3944 llvm_unreachable("Unhandled reference cursor kind"); 3945 break; 3946 } 3947 } 3948 3949 return clang_getNullCursor(); 3950} 3951 3952CXCursor clang_getCursorDefinition(CXCursor C) { 3953 if (clang_isInvalid(C.kind)) 3954 return clang_getNullCursor(); 3955 3956 CXTranslationUnit TU = getCursorTU(C); 3957 3958 bool WasReference = false; 3959 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { 3960 C = clang_getCursorReferenced(C); 3961 WasReference = true; 3962 } 3963 3964 if (C.kind == CXCursor_MacroExpansion) 3965 return clang_getCursorReferenced(C); 3966 3967 if (!clang_isDeclaration(C.kind)) 3968 return clang_getNullCursor(); 3969 3970 Decl *D = getCursorDecl(C); 3971 if (!D) 3972 return clang_getNullCursor(); 3973 3974 switch (D->getKind()) { 3975 // Declaration kinds that don't really separate the notions of 3976 // declaration and definition. 3977 case Decl::Namespace: 3978 case Decl::Typedef: 3979 case Decl::TypeAlias: 3980 case Decl::TypeAliasTemplate: 3981 case Decl::TemplateTypeParm: 3982 case Decl::EnumConstant: 3983 case Decl::Field: 3984 case Decl::IndirectField: 3985 case Decl::ObjCIvar: 3986 case Decl::ObjCAtDefsField: 3987 case Decl::ImplicitParam: 3988 case Decl::ParmVar: 3989 case Decl::NonTypeTemplateParm: 3990 case Decl::TemplateTemplateParm: 3991 case Decl::ObjCCategoryImpl: 3992 case Decl::ObjCImplementation: 3993 case Decl::AccessSpec: 3994 case Decl::LinkageSpec: 3995 case Decl::ObjCPropertyImpl: 3996 case Decl::FileScopeAsm: 3997 case Decl::StaticAssert: 3998 case Decl::Block: 3999 case Decl::Label: // FIXME: Is this right?? 4000 case Decl::ClassScopeFunctionSpecialization: 4001 return C; 4002 4003 // Declaration kinds that don't make any sense here, but are 4004 // nonetheless harmless. 4005 case Decl::TranslationUnit: 4006 break; 4007 4008 // Declaration kinds for which the definition is not resolvable. 4009 case Decl::UnresolvedUsingTypename: 4010 case Decl::UnresolvedUsingValue: 4011 break; 4012 4013 case Decl::UsingDirective: 4014 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), 4015 TU); 4016 4017 case Decl::NamespaceAlias: 4018 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); 4019 4020 case Decl::Enum: 4021 case Decl::Record: 4022 case Decl::CXXRecord: 4023 case Decl::ClassTemplateSpecialization: 4024 case Decl::ClassTemplatePartialSpecialization: 4025 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) 4026 return MakeCXCursor(Def, TU); 4027 return clang_getNullCursor(); 4028 4029 case Decl::Function: 4030 case Decl::CXXMethod: 4031 case Decl::CXXConstructor: 4032 case Decl::CXXDestructor: 4033 case Decl::CXXConversion: { 4034 const FunctionDecl *Def = 0; 4035 if (cast<FunctionDecl>(D)->getBody(Def)) 4036 return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); 4037 return clang_getNullCursor(); 4038 } 4039 4040 case Decl::Var: { 4041 // Ask the variable if it has a definition. 4042 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) 4043 return MakeCXCursor(Def, TU); 4044 return clang_getNullCursor(); 4045 } 4046 4047 case Decl::FunctionTemplate: { 4048 const FunctionDecl *Def = 0; 4049 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) 4050 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); 4051 return clang_getNullCursor(); 4052 } 4053 4054 case Decl::ClassTemplate: { 4055 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() 4056 ->getDefinition()) 4057 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), 4058 TU); 4059 return clang_getNullCursor(); 4060 } 4061 4062 case Decl::Using: 4063 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), 4064 D->getLocation(), TU); 4065 4066 case Decl::UsingShadow: 4067 return clang_getCursorDefinition( 4068 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), 4069 TU)); 4070 4071 case Decl::ObjCMethod: { 4072 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); 4073 if (Method->isThisDeclarationADefinition()) 4074 return C; 4075 4076 // Dig out the method definition in the associated 4077 // @implementation, if we have it. 4078 // FIXME: The ASTs should make finding the definition easier. 4079 if (ObjCInterfaceDecl *Class 4080 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) 4081 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) 4082 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), 4083 Method->isInstanceMethod())) 4084 if (Def->isThisDeclarationADefinition()) 4085 return MakeCXCursor(Def, TU); 4086 4087 return clang_getNullCursor(); 4088 } 4089 4090 case Decl::ObjCCategory: 4091 if (ObjCCategoryImplDecl *Impl 4092 = cast<ObjCCategoryDecl>(D)->getImplementation()) 4093 return MakeCXCursor(Impl, TU); 4094 return clang_getNullCursor(); 4095 4096 case Decl::ObjCProtocol: 4097 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) 4098 return C; 4099 return clang_getNullCursor(); 4100 4101 case Decl::ObjCInterface: 4102 // There are two notions of a "definition" for an Objective-C 4103 // class: the interface and its implementation. When we resolved a 4104 // reference to an Objective-C class, produce the @interface as 4105 // the definition; when we were provided with the interface, 4106 // produce the @implementation as the definition. 4107 if (WasReference) { 4108 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) 4109 return C; 4110 } else if (ObjCImplementationDecl *Impl 4111 = cast<ObjCInterfaceDecl>(D)->getImplementation()) 4112 return MakeCXCursor(Impl, TU); 4113 return clang_getNullCursor(); 4114 4115 case Decl::ObjCProperty: 4116 // FIXME: We don't really know where to find the 4117 // ObjCPropertyImplDecls that implement this property. 4118 return clang_getNullCursor(); 4119 4120 case Decl::ObjCCompatibleAlias: 4121 if (ObjCInterfaceDecl *Class 4122 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) 4123 if (!Class->isForwardDecl()) 4124 return MakeCXCursor(Class, TU); 4125 4126 return clang_getNullCursor(); 4127 4128 case Decl::ObjCForwardProtocol: 4129 return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), 4130 D->getLocation(), TU); 4131 4132 case Decl::ObjCClass: 4133 return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), 4134 TU); 4135 4136 case Decl::Friend: 4137 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) 4138 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 4139 return clang_getNullCursor(); 4140 4141 case Decl::FriendTemplate: 4142 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) 4143 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 4144 return clang_getNullCursor(); 4145 } 4146 4147 return clang_getNullCursor(); 4148} 4149 4150unsigned clang_isCursorDefinition(CXCursor C) { 4151 if (!clang_isDeclaration(C.kind)) 4152 return 0; 4153 4154 return clang_getCursorDefinition(C) == C; 4155} 4156 4157CXCursor clang_getCanonicalCursor(CXCursor C) { 4158 if (!clang_isDeclaration(C.kind)) 4159 return C; 4160 4161 if (Decl *D = getCursorDecl(C)) { 4162 if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) 4163 if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) 4164 return MakeCXCursor(CatD, getCursorTU(C)); 4165 4166 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 4167 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) 4168 return MakeCXCursor(IFD, getCursorTU(C)); 4169 4170 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); 4171 } 4172 4173 return C; 4174} 4175 4176unsigned clang_getNumOverloadedDecls(CXCursor C) { 4177 if (C.kind != CXCursor_OverloadedDeclRef) 4178 return 0; 4179 4180 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 4181 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 4182 return E->getNumDecls(); 4183 4184 if (OverloadedTemplateStorage *S 4185 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 4186 return S->size(); 4187 4188 Decl *D = Storage.get<Decl*>(); 4189 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 4190 return Using->shadow_size(); 4191 if (isa<ObjCClassDecl>(D)) 4192 return 1; 4193 if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) 4194 return Protocols->protocol_size(); 4195 4196 return 0; 4197} 4198 4199CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { 4200 if (cursor.kind != CXCursor_OverloadedDeclRef) 4201 return clang_getNullCursor(); 4202 4203 if (index >= clang_getNumOverloadedDecls(cursor)) 4204 return clang_getNullCursor(); 4205 4206 CXTranslationUnit TU = getCursorTU(cursor); 4207 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; 4208 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 4209 return MakeCXCursor(E->decls_begin()[index], TU); 4210 4211 if (OverloadedTemplateStorage *S 4212 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 4213 return MakeCXCursor(S->begin()[index], TU); 4214 4215 Decl *D = Storage.get<Decl*>(); 4216 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { 4217 // FIXME: This is, unfortunately, linear time. 4218 UsingDecl::shadow_iterator Pos = Using->shadow_begin(); 4219 std::advance(Pos, index); 4220 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); 4221 } 4222 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) 4223 return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); 4224 if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) 4225 return MakeCXCursor(Protocols->protocol_begin()[index], TU); 4226 4227 return clang_getNullCursor(); 4228} 4229 4230void clang_getDefinitionSpellingAndExtent(CXCursor C, 4231 const char **startBuf, 4232 const char **endBuf, 4233 unsigned *startLine, 4234 unsigned *startColumn, 4235 unsigned *endLine, 4236 unsigned *endColumn) { 4237 assert(getCursorDecl(C) && "CXCursor has null decl"); 4238 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); 4239 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); 4240 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); 4241 4242 SourceManager &SM = FD->getASTContext().getSourceManager(); 4243 *startBuf = SM.getCharacterData(Body->getLBracLoc()); 4244 *endBuf = SM.getCharacterData(Body->getRBracLoc()); 4245 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); 4246 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); 4247 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); 4248 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); 4249} 4250 4251 4252CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, 4253 unsigned PieceIndex) { 4254 RefNamePieces Pieces; 4255 4256 switch (C.kind) { 4257 case CXCursor_MemberRefExpr: 4258 if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) 4259 Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), 4260 E->getQualifierLoc().getSourceRange()); 4261 break; 4262 4263 case CXCursor_DeclRefExpr: 4264 if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) 4265 Pieces = buildPieces(NameFlags, false, E->getNameInfo(), 4266 E->getQualifierLoc().getSourceRange(), 4267 E->getExplicitTemplateArgsOpt()); 4268 break; 4269 4270 case CXCursor_CallExpr: 4271 if (CXXOperatorCallExpr *OCE = 4272 dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { 4273 Expr *Callee = OCE->getCallee(); 4274 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) 4275 Callee = ICE->getSubExpr(); 4276 4277 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) 4278 Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), 4279 DRE->getQualifierLoc().getSourceRange()); 4280 } 4281 break; 4282 4283 default: 4284 break; 4285 } 4286 4287 if (Pieces.empty()) { 4288 if (PieceIndex == 0) 4289 return clang_getCursorExtent(C); 4290 } else if (PieceIndex < Pieces.size()) { 4291 SourceRange R = Pieces[PieceIndex]; 4292 if (R.isValid()) 4293 return cxloc::translateSourceRange(getCursorContext(C), R); 4294 } 4295 4296 return clang_getNullRange(); 4297} 4298 4299void clang_enableStackTraces(void) { 4300 llvm::sys::PrintStackTraceOnErrorSignal(); 4301} 4302 4303void clang_executeOnThread(void (*fn)(void*), void *user_data, 4304 unsigned stack_size) { 4305 llvm::llvm_execute_on_thread(fn, user_data, stack_size); 4306} 4307 4308} // end: extern "C" 4309 4310//===----------------------------------------------------------------------===// 4311// Token-based Operations. 4312//===----------------------------------------------------------------------===// 4313 4314/* CXToken layout: 4315 * int_data[0]: a CXTokenKind 4316 * int_data[1]: starting token location 4317 * int_data[2]: token length 4318 * int_data[3]: reserved 4319 * ptr_data: for identifiers and keywords, an IdentifierInfo*. 4320 * otherwise unused. 4321 */ 4322extern "C" { 4323 4324CXTokenKind clang_getTokenKind(CXToken CXTok) { 4325 return static_cast<CXTokenKind>(CXTok.int_data[0]); 4326} 4327 4328CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { 4329 switch (clang_getTokenKind(CXTok)) { 4330 case CXToken_Identifier: 4331 case CXToken_Keyword: 4332 // We know we have an IdentifierInfo*, so use that. 4333 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) 4334 ->getNameStart()); 4335 4336 case CXToken_Literal: { 4337 // We have stashed the starting pointer in the ptr_data field. Use it. 4338 const char *Text = static_cast<const char *>(CXTok.ptr_data); 4339 return createCXString(StringRef(Text, CXTok.int_data[2])); 4340 } 4341 4342 case CXToken_Punctuation: 4343 case CXToken_Comment: 4344 break; 4345 } 4346 4347 // We have to find the starting buffer pointer the hard way, by 4348 // deconstructing the source location. 4349 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4350 if (!CXXUnit) 4351 return createCXString(""); 4352 4353 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); 4354 std::pair<FileID, unsigned> LocInfo 4355 = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); 4356 bool Invalid = false; 4357 StringRef Buffer 4358 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); 4359 if (Invalid) 4360 return createCXString(""); 4361 4362 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); 4363} 4364 4365CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { 4366 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4367 if (!CXXUnit) 4368 return clang_getNullLocation(); 4369 4370 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), 4371 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4372} 4373 4374CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { 4375 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4376 if (!CXXUnit) 4377 return clang_getNullRange(); 4378 4379 return cxloc::translateSourceRange(CXXUnit->getASTContext(), 4380 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4381} 4382 4383static void getTokens(ASTUnit *CXXUnit, SourceRange Range, 4384 SmallVectorImpl<CXToken> &CXTokens) { 4385 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 4386 std::pair<FileID, unsigned> BeginLocInfo 4387 = SourceMgr.getDecomposedLoc(Range.getBegin()); 4388 std::pair<FileID, unsigned> EndLocInfo 4389 = SourceMgr.getDecomposedLoc(Range.getEnd()); 4390 4391 // Cannot tokenize across files. 4392 if (BeginLocInfo.first != EndLocInfo.first) 4393 return; 4394 4395 // Create a lexer 4396 bool Invalid = false; 4397 StringRef Buffer 4398 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 4399 if (Invalid) 4400 return; 4401 4402 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 4403 CXXUnit->getASTContext().getLangOptions(), 4404 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); 4405 Lex.SetCommentRetentionState(true); 4406 4407 // Lex tokens until we hit the end of the range. 4408 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; 4409 Token Tok; 4410 bool previousWasAt = false; 4411 do { 4412 // Lex the next token 4413 Lex.LexFromRawLexer(Tok); 4414 if (Tok.is(tok::eof)) 4415 break; 4416 4417 // Initialize the CXToken. 4418 CXToken CXTok; 4419 4420 // - Common fields 4421 CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); 4422 CXTok.int_data[2] = Tok.getLength(); 4423 CXTok.int_data[3] = 0; 4424 4425 // - Kind-specific fields 4426 if (Tok.isLiteral()) { 4427 CXTok.int_data[0] = CXToken_Literal; 4428 CXTok.ptr_data = (void *)Tok.getLiteralData(); 4429 } else if (Tok.is(tok::raw_identifier)) { 4430 // Lookup the identifier to determine whether we have a keyword. 4431 IdentifierInfo *II 4432 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); 4433 4434 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { 4435 CXTok.int_data[0] = CXToken_Keyword; 4436 } 4437 else { 4438 CXTok.int_data[0] = Tok.is(tok::identifier) 4439 ? CXToken_Identifier 4440 : CXToken_Keyword; 4441 } 4442 CXTok.ptr_data = II; 4443 } else if (Tok.is(tok::comment)) { 4444 CXTok.int_data[0] = CXToken_Comment; 4445 CXTok.ptr_data = 0; 4446 } else { 4447 CXTok.int_data[0] = CXToken_Punctuation; 4448 CXTok.ptr_data = 0; 4449 } 4450 CXTokens.push_back(CXTok); 4451 previousWasAt = Tok.is(tok::at); 4452 } while (Lex.getBufferLocation() <= EffectiveBufferEnd); 4453} 4454 4455void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, 4456 CXToken **Tokens, unsigned *NumTokens) { 4457 if (Tokens) 4458 *Tokens = 0; 4459 if (NumTokens) 4460 *NumTokens = 0; 4461 4462 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4463 if (!CXXUnit || !Tokens || !NumTokens) 4464 return; 4465 4466 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 4467 4468 SourceRange R = cxloc::translateCXSourceRange(Range); 4469 if (R.isInvalid()) 4470 return; 4471 4472 SmallVector<CXToken, 32> CXTokens; 4473 getTokens(CXXUnit, R, CXTokens); 4474 4475 if (CXTokens.empty()) 4476 return; 4477 4478 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); 4479 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); 4480 *NumTokens = CXTokens.size(); 4481} 4482 4483void clang_disposeTokens(CXTranslationUnit TU, 4484 CXToken *Tokens, unsigned NumTokens) { 4485 free(Tokens); 4486} 4487 4488} // end: extern "C" 4489 4490//===----------------------------------------------------------------------===// 4491// Token annotation APIs. 4492//===----------------------------------------------------------------------===// 4493 4494typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; 4495static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 4496 CXCursor parent, 4497 CXClientData client_data); 4498namespace { 4499class AnnotateTokensWorker { 4500 AnnotateTokensData &Annotated; 4501 CXToken *Tokens; 4502 CXCursor *Cursors; 4503 unsigned NumTokens; 4504 unsigned TokIdx; 4505 unsigned PreprocessingTokIdx; 4506 CursorVisitor AnnotateVis; 4507 SourceManager &SrcMgr; 4508 bool HasContextSensitiveKeywords; 4509 4510 bool MoreTokens() const { return TokIdx < NumTokens; } 4511 unsigned NextToken() const { return TokIdx; } 4512 void AdvanceToken() { ++TokIdx; } 4513 SourceLocation GetTokenLoc(unsigned tokI) { 4514 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 4515 } 4516 bool isFunctionMacroToken(unsigned tokI) const { 4517 return Tokens[tokI].int_data[3] != 0; 4518 } 4519 SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { 4520 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); 4521 } 4522 4523 void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); 4524 void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, 4525 SourceRange); 4526 4527public: 4528 AnnotateTokensWorker(AnnotateTokensData &annotated, 4529 CXToken *tokens, CXCursor *cursors, unsigned numTokens, 4530 CXTranslationUnit tu, SourceRange RegionOfInterest) 4531 : Annotated(annotated), Tokens(tokens), Cursors(cursors), 4532 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), 4533 AnnotateVis(tu, 4534 AnnotateTokensVisitor, this, 4535 /*VisitPreprocessorLast=*/true, 4536 /*VisitIncludedEntities=*/false, 4537 RegionOfInterest), 4538 SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), 4539 HasContextSensitiveKeywords(false) { } 4540 4541 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } 4542 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); 4543 void AnnotateTokens(); 4544 4545 /// \brief Determine whether the annotator saw any cursors that have 4546 /// context-sensitive keywords. 4547 bool hasContextSensitiveKeywords() const { 4548 return HasContextSensitiveKeywords; 4549 } 4550}; 4551} 4552 4553void AnnotateTokensWorker::AnnotateTokens() { 4554 // Walk the AST within the region of interest, annotating tokens 4555 // along the way. 4556 AnnotateVis.visitFileRegion(); 4557 4558 for (unsigned I = 0 ; I < TokIdx ; ++I) { 4559 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4560 if (Pos != Annotated.end() && 4561 (clang_isInvalid(Cursors[I].kind) || 4562 Pos->second.kind != CXCursor_PreprocessingDirective)) 4563 Cursors[I] = Pos->second; 4564 } 4565 4566 // Finish up annotating any tokens left. 4567 if (!MoreTokens()) 4568 return; 4569 4570 const CXCursor &C = clang_getNullCursor(); 4571 for (unsigned I = TokIdx ; I < NumTokens ; ++I) { 4572 if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind)) 4573 continue; 4574 4575 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4576 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; 4577 } 4578} 4579 4580/// \brief It annotates and advances tokens with a cursor until the comparison 4581//// between the cursor location and the source range is the same as 4582/// \arg compResult. 4583/// 4584/// Pass RangeBefore to annotate tokens with a cursor until a range is reached. 4585/// Pass RangeOverlap to annotate tokens inside a range. 4586void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, 4587 RangeComparisonResult compResult, 4588 SourceRange range) { 4589 while (MoreTokens()) { 4590 const unsigned I = NextToken(); 4591 if (isFunctionMacroToken(I)) 4592 return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); 4593 4594 SourceLocation TokLoc = GetTokenLoc(I); 4595 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { 4596 Cursors[I] = updateC; 4597 AdvanceToken(); 4598 continue; 4599 } 4600 break; 4601 } 4602} 4603 4604/// \brief Special annotation handling for macro argument tokens. 4605void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( 4606 CXCursor updateC, 4607 RangeComparisonResult compResult, 4608 SourceRange range) { 4609 assert(MoreTokens()); 4610 assert(isFunctionMacroToken(NextToken()) && 4611 "Should be called only for macro arg tokens"); 4612 4613 // This works differently than annotateAndAdvanceTokens; because expanded 4614 // macro arguments can have arbitrary translation-unit source order, we do not 4615 // advance the token index one by one until a token fails the range test. 4616 // We only advance once past all of the macro arg tokens if all of them 4617 // pass the range test. If one of them fails we keep the token index pointing 4618 // at the start of the macro arg tokens so that the failing token will be 4619 // annotated by a subsequent annotation try. 4620 4621 bool atLeastOneCompFail = false; 4622 4623 unsigned I = NextToken(); 4624 for (; I < NumTokens && isFunctionMacroToken(I); ++I) { 4625 SourceLocation TokLoc = getFunctionMacroTokenLoc(I); 4626 if (TokLoc.isFileID()) 4627 continue; // not macro arg token, it's parens or comma. 4628 if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { 4629 if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) 4630 Cursors[I] = updateC; 4631 } else 4632 atLeastOneCompFail = true; 4633 } 4634 4635 if (!atLeastOneCompFail) 4636 TokIdx = I; // All of the tokens were handled, advance beyond all of them. 4637} 4638 4639enum CXChildVisitResult 4640AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { 4641 CXSourceLocation Loc = clang_getCursorLocation(cursor); 4642 SourceRange cursorRange = getRawCursorExtent(cursor); 4643 if (cursorRange.isInvalid()) 4644 return CXChildVisit_Recurse; 4645 4646 if (!HasContextSensitiveKeywords) { 4647 // Objective-C properties can have context-sensitive keywords. 4648 if (cursor.kind == CXCursor_ObjCPropertyDecl) { 4649 if (ObjCPropertyDecl *Property 4650 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) 4651 HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; 4652 } 4653 // Objective-C methods can have context-sensitive keywords. 4654 else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || 4655 cursor.kind == CXCursor_ObjCClassMethodDecl) { 4656 if (ObjCMethodDecl *Method 4657 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { 4658 if (Method->getObjCDeclQualifier()) 4659 HasContextSensitiveKeywords = true; 4660 else { 4661 for (ObjCMethodDecl::param_iterator P = Method->param_begin(), 4662 PEnd = Method->param_end(); 4663 P != PEnd; ++P) { 4664 if ((*P)->getObjCDeclQualifier()) { 4665 HasContextSensitiveKeywords = true; 4666 break; 4667 } 4668 } 4669 } 4670 } 4671 } 4672 // C++ methods can have context-sensitive keywords. 4673 else if (cursor.kind == CXCursor_CXXMethod) { 4674 if (CXXMethodDecl *Method 4675 = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { 4676 if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) 4677 HasContextSensitiveKeywords = true; 4678 } 4679 } 4680 // C++ classes can have context-sensitive keywords. 4681 else if (cursor.kind == CXCursor_StructDecl || 4682 cursor.kind == CXCursor_ClassDecl || 4683 cursor.kind == CXCursor_ClassTemplate || 4684 cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { 4685 if (Decl *D = getCursorDecl(cursor)) 4686 if (D->hasAttr<FinalAttr>()) 4687 HasContextSensitiveKeywords = true; 4688 } 4689 } 4690 4691 if (clang_isPreprocessing(cursor.kind)) { 4692 // For macro expansions, just note where the beginning of the macro 4693 // expansion occurs. 4694 if (cursor.kind == CXCursor_MacroExpansion) { 4695 Annotated[Loc.int_data] = cursor; 4696 return CXChildVisit_Recurse; 4697 } 4698 4699 // Items in the preprocessing record are kept separate from items in 4700 // declarations, so we keep a separate token index. 4701 unsigned SavedTokIdx = TokIdx; 4702 TokIdx = PreprocessingTokIdx; 4703 4704 // Skip tokens up until we catch up to the beginning of the preprocessing 4705 // entry. 4706 while (MoreTokens()) { 4707 const unsigned I = NextToken(); 4708 SourceLocation TokLoc = GetTokenLoc(I); 4709 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4710 case RangeBefore: 4711 AdvanceToken(); 4712 continue; 4713 case RangeAfter: 4714 case RangeOverlap: 4715 break; 4716 } 4717 break; 4718 } 4719 4720 // Look at all of the tokens within this range. 4721 while (MoreTokens()) { 4722 const unsigned I = NextToken(); 4723 SourceLocation TokLoc = GetTokenLoc(I); 4724 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4725 case RangeBefore: 4726 llvm_unreachable("Infeasible"); 4727 case RangeAfter: 4728 break; 4729 case RangeOverlap: 4730 Cursors[I] = cursor; 4731 AdvanceToken(); 4732 continue; 4733 } 4734 break; 4735 } 4736 4737 // Save the preprocessing token index; restore the non-preprocessing 4738 // token index. 4739 PreprocessingTokIdx = TokIdx; 4740 TokIdx = SavedTokIdx; 4741 return CXChildVisit_Recurse; 4742 } 4743 4744 if (cursorRange.isInvalid()) 4745 return CXChildVisit_Continue; 4746 4747 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); 4748 4749 // Adjust the annotated range based specific declarations. 4750 const enum CXCursorKind cursorK = clang_getCursorKind(cursor); 4751 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { 4752 Decl *D = cxcursor::getCursorDecl(cursor); 4753 4754 SourceLocation StartLoc; 4755 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 4756 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 4757 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 4758 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { 4759 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 4760 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 4761 } 4762 4763 if (StartLoc.isValid() && L.isValid() && 4764 SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) 4765 cursorRange.setBegin(StartLoc); 4766 } 4767 4768 // If the location of the cursor occurs within a macro instantiation, record 4769 // the spelling location of the cursor in our annotation map. We can then 4770 // paper over the token labelings during a post-processing step to try and 4771 // get cursor mappings for tokens that are the *arguments* of a macro 4772 // instantiation. 4773 if (L.isMacroID()) { 4774 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); 4775 // Only invalidate the old annotation if it isn't part of a preprocessing 4776 // directive. Here we assume that the default construction of CXCursor 4777 // results in CXCursor.kind being an initialized value (i.e., 0). If 4778 // this isn't the case, we can fix by doing lookup + insertion. 4779 4780 CXCursor &oldC = Annotated[rawEncoding]; 4781 if (!clang_isPreprocessing(oldC.kind)) 4782 oldC = cursor; 4783 } 4784 4785 const enum CXCursorKind K = clang_getCursorKind(parent); 4786 const CXCursor updateC = 4787 (clang_isInvalid(K) || K == CXCursor_TranslationUnit) 4788 ? clang_getNullCursor() : parent; 4789 4790 annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); 4791 4792 // Avoid having the cursor of an expression "overwrite" the annotation of the 4793 // variable declaration that it belongs to. 4794 // This can happen for C++ constructor expressions whose range generally 4795 // include the variable declaration, e.g.: 4796 // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. 4797 if (clang_isExpression(cursorK)) { 4798 Expr *E = getCursorExpr(cursor); 4799 if (Decl *D = getCursorParentDecl(cursor)) { 4800 const unsigned I = NextToken(); 4801 if (E->getLocStart().isValid() && D->getLocation().isValid() && 4802 E->getLocStart() == D->getLocation() && 4803 E->getLocStart() == GetTokenLoc(I)) { 4804 Cursors[I] = updateC; 4805 AdvanceToken(); 4806 } 4807 } 4808 } 4809 4810 // Visit children to get their cursor information. 4811 const unsigned BeforeChildren = NextToken(); 4812 VisitChildren(cursor); 4813 const unsigned AfterChildren = NextToken(); 4814 4815 // Scan the tokens that are at the end of the cursor, but are not captured 4816 // but the child cursors. 4817 annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); 4818 4819 // Scan the tokens that are at the beginning of the cursor, but are not 4820 // capture by the child cursors. 4821 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { 4822 if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) 4823 break; 4824 4825 Cursors[I] = cursor; 4826 } 4827 4828 return CXChildVisit_Continue; 4829} 4830 4831static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 4832 CXCursor parent, 4833 CXClientData client_data) { 4834 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); 4835} 4836 4837namespace { 4838 4839/// \brief Uses the macro expansions in the preprocessing record to find 4840/// and mark tokens that are macro arguments. This info is used by the 4841/// AnnotateTokensWorker. 4842class MarkMacroArgTokensVisitor { 4843 SourceManager &SM; 4844 CXToken *Tokens; 4845 unsigned NumTokens; 4846 unsigned CurIdx; 4847 4848public: 4849 MarkMacroArgTokensVisitor(SourceManager &SM, 4850 CXToken *tokens, unsigned numTokens) 4851 : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } 4852 4853 CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { 4854 if (cursor.kind != CXCursor_MacroExpansion) 4855 return CXChildVisit_Continue; 4856 4857 SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); 4858 if (macroRange.getBegin() == macroRange.getEnd()) 4859 return CXChildVisit_Continue; // it's not a function macro. 4860 4861 for (; CurIdx < NumTokens; ++CurIdx) { 4862 if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), 4863 macroRange.getBegin())) 4864 break; 4865 } 4866 4867 if (CurIdx == NumTokens) 4868 return CXChildVisit_Break; 4869 4870 for (; CurIdx < NumTokens; ++CurIdx) { 4871 SourceLocation tokLoc = getTokenLoc(CurIdx); 4872 if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) 4873 break; 4874 4875 setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); 4876 } 4877 4878 if (CurIdx == NumTokens) 4879 return CXChildVisit_Break; 4880 4881 return CXChildVisit_Continue; 4882 } 4883 4884private: 4885 SourceLocation getTokenLoc(unsigned tokI) { 4886 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 4887 } 4888 4889 void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { 4890 // The third field is reserved and currently not used. Use it here 4891 // to mark macro arg expanded tokens with their expanded locations. 4892 Tokens[tokI].int_data[3] = loc.getRawEncoding(); 4893 } 4894}; 4895 4896} // end anonymous namespace 4897 4898static CXChildVisitResult 4899MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, 4900 CXClientData client_data) { 4901 return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, 4902 parent); 4903} 4904 4905namespace { 4906 struct clang_annotateTokens_Data { 4907 CXTranslationUnit TU; 4908 ASTUnit *CXXUnit; 4909 CXToken *Tokens; 4910 unsigned NumTokens; 4911 CXCursor *Cursors; 4912 }; 4913} 4914 4915static void annotatePreprocessorTokens(CXTranslationUnit TU, 4916 SourceRange RegionOfInterest, 4917 AnnotateTokensData &Annotated) { 4918 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4919 4920 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 4921 std::pair<FileID, unsigned> BeginLocInfo 4922 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); 4923 std::pair<FileID, unsigned> EndLocInfo 4924 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); 4925 4926 if (BeginLocInfo.first != EndLocInfo.first) 4927 return; 4928 4929 StringRef Buffer; 4930 bool Invalid = false; 4931 Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 4932 if (Buffer.empty() || Invalid) 4933 return; 4934 4935 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 4936 CXXUnit->getASTContext().getLangOptions(), 4937 Buffer.begin(), Buffer.data() + BeginLocInfo.second, 4938 Buffer.end()); 4939 Lex.SetCommentRetentionState(true); 4940 4941 // Lex tokens in raw mode until we hit the end of the range, to avoid 4942 // entering #includes or expanding macros. 4943 while (true) { 4944 Token Tok; 4945 Lex.LexFromRawLexer(Tok); 4946 4947 reprocess: 4948 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { 4949 // We have found a preprocessing directive. Gobble it up so that we 4950 // don't see it while preprocessing these tokens later, but keep track 4951 // of all of the token locations inside this preprocessing directive so 4952 // that we can annotate them appropriately. 4953 // 4954 // FIXME: Some simple tests here could identify macro definitions and 4955 // #undefs, to provide specific cursor kinds for those. 4956 SmallVector<SourceLocation, 32> Locations; 4957 do { 4958 Locations.push_back(Tok.getLocation()); 4959 Lex.LexFromRawLexer(Tok); 4960 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); 4961 4962 using namespace cxcursor; 4963 CXCursor Cursor 4964 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), 4965 Locations.back()), 4966 TU); 4967 for (unsigned I = 0, N = Locations.size(); I != N; ++I) { 4968 Annotated[Locations[I].getRawEncoding()] = Cursor; 4969 } 4970 4971 if (Tok.isAtStartOfLine()) 4972 goto reprocess; 4973 4974 continue; 4975 } 4976 4977 if (Tok.is(tok::eof)) 4978 break; 4979 } 4980} 4981 4982// This gets run a separate thread to avoid stack blowout. 4983static void clang_annotateTokensImpl(void *UserData) { 4984 CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; 4985 ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; 4986 CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; 4987 const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; 4988 CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; 4989 4990 // Determine the region of interest, which contains all of the tokens. 4991 SourceRange RegionOfInterest; 4992 RegionOfInterest.setBegin( 4993 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); 4994 RegionOfInterest.setEnd( 4995 cxloc::translateSourceLocation(clang_getTokenLocation(TU, 4996 Tokens[NumTokens-1]))); 4997 4998 // A mapping from the source locations found when re-lexing or traversing the 4999 // region of interest to the corresponding cursors. 5000 AnnotateTokensData Annotated; 5001 5002 // Relex the tokens within the source range to look for preprocessing 5003 // directives. 5004 annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); 5005 5006 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { 5007 // Search and mark tokens that are macro argument expansions. 5008 MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), 5009 Tokens, NumTokens); 5010 CursorVisitor MacroArgMarker(TU, 5011 MarkMacroArgTokensVisitorDelegate, &Visitor, 5012 /*VisitPreprocessorLast=*/true, 5013 /*VisitIncludedEntities=*/false, 5014 RegionOfInterest); 5015 MacroArgMarker.visitPreprocessedEntitiesInRegion(); 5016 } 5017 5018 // Annotate all of the source locations in the region of interest that map to 5019 // a specific cursor. 5020 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, 5021 TU, RegionOfInterest); 5022 5023 // FIXME: We use a ridiculous stack size here because the data-recursion 5024 // algorithm uses a large stack frame than the non-data recursive version, 5025 // and AnnotationTokensWorker currently transforms the data-recursion 5026 // algorithm back into a traditional recursion by explicitly calling 5027 // VisitChildren(). We will need to remove this explicit recursive call. 5028 W.AnnotateTokens(); 5029 5030 // If we ran into any entities that involve context-sensitive keywords, 5031 // take another pass through the tokens to mark them as such. 5032 if (W.hasContextSensitiveKeywords()) { 5033 for (unsigned I = 0; I != NumTokens; ++I) { 5034 if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) 5035 continue; 5036 5037 if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { 5038 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 5039 if (ObjCPropertyDecl *Property 5040 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { 5041 if (Property->getPropertyAttributesAsWritten() != 0 && 5042 llvm::StringSwitch<bool>(II->getName()) 5043 .Case("readonly", true) 5044 .Case("assign", true) 5045 .Case("unsafe_unretained", true) 5046 .Case("readwrite", true) 5047 .Case("retain", true) 5048 .Case("copy", true) 5049 .Case("nonatomic", true) 5050 .Case("atomic", true) 5051 .Case("getter", true) 5052 .Case("setter", true) 5053 .Case("strong", true) 5054 .Case("weak", true) 5055 .Default(false)) 5056 Tokens[I].int_data[0] = CXToken_Keyword; 5057 } 5058 continue; 5059 } 5060 5061 if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || 5062 Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { 5063 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 5064 if (llvm::StringSwitch<bool>(II->getName()) 5065 .Case("in", true) 5066 .Case("out", true) 5067 .Case("inout", true) 5068 .Case("oneway", true) 5069 .Case("bycopy", true) 5070 .Case("byref", true) 5071 .Default(false)) 5072 Tokens[I].int_data[0] = CXToken_Keyword; 5073 continue; 5074 } 5075 5076 if (Cursors[I].kind == CXCursor_CXXFinalAttr || 5077 Cursors[I].kind == CXCursor_CXXOverrideAttr) { 5078 Tokens[I].int_data[0] = CXToken_Keyword; 5079 continue; 5080 } 5081 } 5082 } 5083} 5084 5085extern "C" { 5086 5087void clang_annotateTokens(CXTranslationUnit TU, 5088 CXToken *Tokens, unsigned NumTokens, 5089 CXCursor *Cursors) { 5090 5091 if (NumTokens == 0 || !Tokens || !Cursors) 5092 return; 5093 5094 // Any token we don't specifically annotate will have a NULL cursor. 5095 CXCursor C = clang_getNullCursor(); 5096 for (unsigned I = 0; I != NumTokens; ++I) 5097 Cursors[I] = C; 5098 5099 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 5100 if (!CXXUnit) 5101 return; 5102 5103 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 5104 5105 clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; 5106 llvm::CrashRecoveryContext CRC; 5107 if (!RunSafely(CRC, clang_annotateTokensImpl, &data, 5108 GetSafetyThreadStackSize() * 2)) { 5109 fprintf(stderr, "libclang: crash detected while annotating tokens\n"); 5110 } 5111} 5112 5113} // end: extern "C" 5114 5115//===----------------------------------------------------------------------===// 5116// Operations for querying linkage of a cursor. 5117//===----------------------------------------------------------------------===// 5118 5119extern "C" { 5120CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { 5121 if (!clang_isDeclaration(cursor.kind)) 5122 return CXLinkage_Invalid; 5123 5124 Decl *D = cxcursor::getCursorDecl(cursor); 5125 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) 5126 switch (ND->getLinkage()) { 5127 case NoLinkage: return CXLinkage_NoLinkage; 5128 case InternalLinkage: return CXLinkage_Internal; 5129 case UniqueExternalLinkage: return CXLinkage_UniqueExternal; 5130 case ExternalLinkage: return CXLinkage_External; 5131 }; 5132 5133 return CXLinkage_Invalid; 5134} 5135} // end: extern "C" 5136 5137//===----------------------------------------------------------------------===// 5138// Operations for querying language of a cursor. 5139//===----------------------------------------------------------------------===// 5140 5141static CXLanguageKind getDeclLanguage(const Decl *D) { 5142 switch (D->getKind()) { 5143 default: 5144 break; 5145 case Decl::ImplicitParam: 5146 case Decl::ObjCAtDefsField: 5147 case Decl::ObjCCategory: 5148 case Decl::ObjCCategoryImpl: 5149 case Decl::ObjCClass: 5150 case Decl::ObjCCompatibleAlias: 5151 case Decl::ObjCForwardProtocol: 5152 case Decl::ObjCImplementation: 5153 case Decl::ObjCInterface: 5154 case Decl::ObjCIvar: 5155 case Decl::ObjCMethod: 5156 case Decl::ObjCProperty: 5157 case Decl::ObjCPropertyImpl: 5158 case Decl::ObjCProtocol: 5159 return CXLanguage_ObjC; 5160 case Decl::CXXConstructor: 5161 case Decl::CXXConversion: 5162 case Decl::CXXDestructor: 5163 case Decl::CXXMethod: 5164 case Decl::CXXRecord: 5165 case Decl::ClassTemplate: 5166 case Decl::ClassTemplatePartialSpecialization: 5167 case Decl::ClassTemplateSpecialization: 5168 case Decl::Friend: 5169 case Decl::FriendTemplate: 5170 case Decl::FunctionTemplate: 5171 case Decl::LinkageSpec: 5172 case Decl::Namespace: 5173 case Decl::NamespaceAlias: 5174 case Decl::NonTypeTemplateParm: 5175 case Decl::StaticAssert: 5176 case Decl::TemplateTemplateParm: 5177 case Decl::TemplateTypeParm: 5178 case Decl::UnresolvedUsingTypename: 5179 case Decl::UnresolvedUsingValue: 5180 case Decl::Using: 5181 case Decl::UsingDirective: 5182 case Decl::UsingShadow: 5183 return CXLanguage_CPlusPlus; 5184 } 5185 5186 return CXLanguage_C; 5187} 5188 5189extern "C" { 5190 5191enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { 5192 if (clang_isDeclaration(cursor.kind)) 5193 if (Decl *D = cxcursor::getCursorDecl(cursor)) { 5194 if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) 5195 return CXAvailability_Available; 5196 5197 switch (D->getAvailability()) { 5198 case AR_Available: 5199 case AR_NotYetIntroduced: 5200 return CXAvailability_Available; 5201 5202 case AR_Deprecated: 5203 return CXAvailability_Deprecated; 5204 5205 case AR_Unavailable: 5206 return CXAvailability_NotAvailable; 5207 } 5208 } 5209 5210 return CXAvailability_Available; 5211} 5212 5213CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { 5214 if (clang_isDeclaration(cursor.kind)) 5215 return getDeclLanguage(cxcursor::getCursorDecl(cursor)); 5216 5217 return CXLanguage_Invalid; 5218} 5219 5220 /// \brief If the given cursor is the "templated" declaration 5221 /// descibing a class or function template, return the class or 5222 /// function template. 5223static Decl *maybeGetTemplateCursor(Decl *D) { 5224 if (!D) 5225 return 0; 5226 5227 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 5228 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) 5229 return FunTmpl; 5230 5231 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) 5232 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) 5233 return ClassTmpl; 5234 5235 return D; 5236} 5237 5238CXCursor clang_getCursorSemanticParent(CXCursor cursor) { 5239 if (clang_isDeclaration(cursor.kind)) { 5240 if (Decl *D = getCursorDecl(cursor)) { 5241 DeclContext *DC = D->getDeclContext(); 5242 if (!DC) 5243 return clang_getNullCursor(); 5244 5245 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 5246 getCursorTU(cursor)); 5247 } 5248 } 5249 5250 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { 5251 if (Decl *D = getCursorDecl(cursor)) 5252 return MakeCXCursor(D, getCursorTU(cursor)); 5253 } 5254 5255 return clang_getNullCursor(); 5256} 5257 5258CXCursor clang_getCursorLexicalParent(CXCursor cursor) { 5259 if (clang_isDeclaration(cursor.kind)) { 5260 if (Decl *D = getCursorDecl(cursor)) { 5261 DeclContext *DC = D->getLexicalDeclContext(); 5262 if (!DC) 5263 return clang_getNullCursor(); 5264 5265 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 5266 getCursorTU(cursor)); 5267 } 5268 } 5269 5270 // FIXME: Note that we can't easily compute the lexical context of a 5271 // statement or expression, so we return nothing. 5272 return clang_getNullCursor(); 5273} 5274 5275void clang_getOverriddenCursors(CXCursor cursor, 5276 CXCursor **overridden, 5277 unsigned *num_overridden) { 5278 if (overridden) 5279 *overridden = 0; 5280 if (num_overridden) 5281 *num_overridden = 0; 5282 if (!overridden || !num_overridden) 5283 return; 5284 5285 SmallVector<CXCursor, 8> Overridden; 5286 cxcursor::getOverriddenCursors(cursor, Overridden); 5287 5288 // Don't allocate memory if we have no overriden cursors. 5289 if (Overridden.size() == 0) 5290 return; 5291 5292 *num_overridden = Overridden.size(); 5293 *overridden = new CXCursor [Overridden.size()]; 5294 std::copy(Overridden.begin(), Overridden.end(), *overridden); 5295} 5296 5297void clang_disposeOverriddenCursors(CXCursor *overridden) { 5298 delete [] overridden; 5299} 5300 5301CXFile clang_getIncludedFile(CXCursor cursor) { 5302 if (cursor.kind != CXCursor_InclusionDirective) 5303 return 0; 5304 5305 InclusionDirective *ID = getCursorInclusionDirective(cursor); 5306 return (void *)ID->getFile(); 5307} 5308 5309} // end: extern "C" 5310 5311 5312//===----------------------------------------------------------------------===// 5313// C++ AST instrospection. 5314//===----------------------------------------------------------------------===// 5315 5316extern "C" { 5317unsigned clang_CXXMethod_isStatic(CXCursor C) { 5318 if (!clang_isDeclaration(C.kind)) 5319 return 0; 5320 5321 CXXMethodDecl *Method = 0; 5322 Decl *D = cxcursor::getCursorDecl(C); 5323 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) 5324 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 5325 else 5326 Method = dyn_cast_or_null<CXXMethodDecl>(D); 5327 return (Method && Method->isStatic()) ? 1 : 0; 5328} 5329 5330unsigned clang_CXXMethod_isVirtual(CXCursor C) { 5331 if (!clang_isDeclaration(C.kind)) 5332 return 0; 5333 5334 CXXMethodDecl *Method = 0; 5335 Decl *D = cxcursor::getCursorDecl(C); 5336 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) 5337 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 5338 else 5339 Method = dyn_cast_or_null<CXXMethodDecl>(D); 5340 return (Method && Method->isVirtual()) ? 1 : 0; 5341} 5342} // end: extern "C" 5343 5344//===----------------------------------------------------------------------===// 5345// Attribute introspection. 5346//===----------------------------------------------------------------------===// 5347 5348extern "C" { 5349CXType clang_getIBOutletCollectionType(CXCursor C) { 5350 if (C.kind != CXCursor_IBOutletCollectionAttr) 5351 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); 5352 5353 IBOutletCollectionAttr *A = 5354 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); 5355 5356 return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); 5357} 5358} // end: extern "C" 5359 5360//===----------------------------------------------------------------------===// 5361// Inspecting memory usage. 5362//===----------------------------------------------------------------------===// 5363 5364typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; 5365 5366static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, 5367 enum CXTUResourceUsageKind k, 5368 unsigned long amount) { 5369 CXTUResourceUsageEntry entry = { k, amount }; 5370 entries.push_back(entry); 5371} 5372 5373extern "C" { 5374 5375const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { 5376 const char *str = ""; 5377 switch (kind) { 5378 case CXTUResourceUsage_AST: 5379 str = "ASTContext: expressions, declarations, and types"; 5380 break; 5381 case CXTUResourceUsage_Identifiers: 5382 str = "ASTContext: identifiers"; 5383 break; 5384 case CXTUResourceUsage_Selectors: 5385 str = "ASTContext: selectors"; 5386 break; 5387 case CXTUResourceUsage_GlobalCompletionResults: 5388 str = "Code completion: cached global results"; 5389 break; 5390 case CXTUResourceUsage_SourceManagerContentCache: 5391 str = "SourceManager: content cache allocator"; 5392 break; 5393 case CXTUResourceUsage_AST_SideTables: 5394 str = "ASTContext: side tables"; 5395 break; 5396 case CXTUResourceUsage_SourceManager_Membuffer_Malloc: 5397 str = "SourceManager: malloc'ed memory buffers"; 5398 break; 5399 case CXTUResourceUsage_SourceManager_Membuffer_MMap: 5400 str = "SourceManager: mmap'ed memory buffers"; 5401 break; 5402 case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: 5403 str = "ExternalASTSource: malloc'ed memory buffers"; 5404 break; 5405 case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: 5406 str = "ExternalASTSource: mmap'ed memory buffers"; 5407 break; 5408 case CXTUResourceUsage_Preprocessor: 5409 str = "Preprocessor: malloc'ed memory"; 5410 break; 5411 case CXTUResourceUsage_PreprocessingRecord: 5412 str = "Preprocessor: PreprocessingRecord"; 5413 break; 5414 case CXTUResourceUsage_SourceManager_DataStructures: 5415 str = "SourceManager: data structures and tables"; 5416 break; 5417 case CXTUResourceUsage_Preprocessor_HeaderSearch: 5418 str = "Preprocessor: header search tables"; 5419 break; 5420 } 5421 return str; 5422} 5423 5424CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { 5425 if (!TU) { 5426 CXTUResourceUsage usage = { (void*) 0, 0, 0 }; 5427 return usage; 5428 } 5429 5430 ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); 5431 llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); 5432 ASTContext &astContext = astUnit->getASTContext(); 5433 5434 // How much memory is used by AST nodes and types? 5435 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, 5436 (unsigned long) astContext.getASTAllocatedMemory()); 5437 5438 // How much memory is used by identifiers? 5439 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, 5440 (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); 5441 5442 // How much memory is used for selectors? 5443 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, 5444 (unsigned long) astContext.Selectors.getTotalMemory()); 5445 5446 // How much memory is used by ASTContext's side tables? 5447 createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, 5448 (unsigned long) astContext.getSideTableAllocatedMemory()); 5449 5450 // How much memory is used for caching global code completion results? 5451 unsigned long completionBytes = 0; 5452 if (GlobalCodeCompletionAllocator *completionAllocator = 5453 astUnit->getCachedCompletionAllocator().getPtr()) { 5454 completionBytes = completionAllocator->getTotalMemory(); 5455 } 5456 createCXTUResourceUsageEntry(*entries, 5457 CXTUResourceUsage_GlobalCompletionResults, 5458 completionBytes); 5459 5460 // How much memory is being used by SourceManager's content cache? 5461 createCXTUResourceUsageEntry(*entries, 5462 CXTUResourceUsage_SourceManagerContentCache, 5463 (unsigned long) astContext.getSourceManager().getContentCacheSize()); 5464 5465 // How much memory is being used by the MemoryBuffer's in SourceManager? 5466 const SourceManager::MemoryBufferSizes &srcBufs = 5467 astUnit->getSourceManager().getMemoryBufferSizes(); 5468 5469 createCXTUResourceUsageEntry(*entries, 5470 CXTUResourceUsage_SourceManager_Membuffer_Malloc, 5471 (unsigned long) srcBufs.malloc_bytes); 5472 createCXTUResourceUsageEntry(*entries, 5473 CXTUResourceUsage_SourceManager_Membuffer_MMap, 5474 (unsigned long) srcBufs.mmap_bytes); 5475 createCXTUResourceUsageEntry(*entries, 5476 CXTUResourceUsage_SourceManager_DataStructures, 5477 (unsigned long) astContext.getSourceManager() 5478 .getDataStructureSizes()); 5479 5480 // How much memory is being used by the ExternalASTSource? 5481 if (ExternalASTSource *esrc = astContext.getExternalSource()) { 5482 const ExternalASTSource::MemoryBufferSizes &sizes = 5483 esrc->getMemoryBufferSizes(); 5484 5485 createCXTUResourceUsageEntry(*entries, 5486 CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, 5487 (unsigned long) sizes.malloc_bytes); 5488 createCXTUResourceUsageEntry(*entries, 5489 CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, 5490 (unsigned long) sizes.mmap_bytes); 5491 } 5492 5493 // How much memory is being used by the Preprocessor? 5494 Preprocessor &pp = astUnit->getPreprocessor(); 5495 createCXTUResourceUsageEntry(*entries, 5496 CXTUResourceUsage_Preprocessor, 5497 pp.getTotalMemory()); 5498 5499 if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { 5500 createCXTUResourceUsageEntry(*entries, 5501 CXTUResourceUsage_PreprocessingRecord, 5502 pRec->getTotalMemory()); 5503 } 5504 5505 createCXTUResourceUsageEntry(*entries, 5506 CXTUResourceUsage_Preprocessor_HeaderSearch, 5507 pp.getHeaderSearchInfo().getTotalMemory()); 5508 5509 CXTUResourceUsage usage = { (void*) entries.get(), 5510 (unsigned) entries->size(), 5511 entries->size() ? &(*entries)[0] : 0 }; 5512 entries.take(); 5513 return usage; 5514} 5515 5516void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { 5517 if (usage.data) 5518 delete (MemUsageEntries*) usage.data; 5519} 5520 5521} // end extern "C" 5522 5523void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { 5524 CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); 5525 for (unsigned I = 0; I != Usage.numEntries; ++I) 5526 fprintf(stderr, " %s: %lu\n", 5527 clang_getTUResourceUsageName(Usage.entries[I].kind), 5528 Usage.entries[I].amount); 5529 5530 clang_disposeCXTUResourceUsage(Usage); 5531} 5532 5533//===----------------------------------------------------------------------===// 5534// Misc. utility functions. 5535//===----------------------------------------------------------------------===// 5536 5537/// Default to using an 8 MB stack size on "safety" threads. 5538static unsigned SafetyStackThreadSize = 8 << 20; 5539 5540namespace clang { 5541 5542bool RunSafely(llvm::CrashRecoveryContext &CRC, 5543 void (*Fn)(void*), void *UserData, 5544 unsigned Size) { 5545 if (!Size) 5546 Size = GetSafetyThreadStackSize(); 5547 if (Size) 5548 return CRC.RunSafelyOnThread(Fn, UserData, Size); 5549 return CRC.RunSafely(Fn, UserData); 5550} 5551 5552unsigned GetSafetyThreadStackSize() { 5553 return SafetyStackThreadSize; 5554} 5555 5556void SetSafetyThreadStackSize(unsigned Value) { 5557 SafetyStackThreadSize = Value; 5558} 5559 5560} 5561 5562extern "C" { 5563 5564CXString clang_getClangVersion() { 5565 return createCXString(getClangFullVersion()); 5566} 5567 5568} // end: extern "C" 5569 5570