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