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