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