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