CIndex.cpp revision 06dec892b5300b43263d25c5476b506c9d6cfbad
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 VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); 1753 void VisitStmt(Stmt *S); 1754 void VisitSwitchStmt(SwitchStmt *S); 1755 void VisitWhileStmt(WhileStmt *W); 1756 void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); 1757 void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); 1758 void 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.getSourceRange().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::VisitUnaryExprOrTypeTraitExpr( 2018 UnaryExprOrTypeTraitExpr *E) { 2019 EnqueueChildren(E); 2020 if (E->isArgumentType()) 2021 AddTypeLoc(E->getArgumentTypeInfo()); 2022} 2023void EnqueueVisitor::VisitStmt(Stmt *S) { 2024 EnqueueChildren(S); 2025} 2026void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { 2027 AddStmt(S->getBody()); 2028 AddStmt(S->getCond()); 2029 AddDecl(S->getConditionVariable()); 2030} 2031 2032void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { 2033 AddStmt(W->getBody()); 2034 AddStmt(W->getCond()); 2035 AddDecl(W->getConditionVariable()); 2036} 2037void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 2038 AddTypeLoc(E->getQueriedTypeSourceInfo()); 2039} 2040 2041void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 2042 AddTypeLoc(E->getRhsTypeSourceInfo()); 2043 AddTypeLoc(E->getLhsTypeSourceInfo()); 2044} 2045 2046void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { 2047 VisitOverloadExpr(U); 2048 if (!U->isImplicitAccess()) 2049 AddStmt(U->getBase()); 2050} 2051void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { 2052 AddStmt(E->getSubExpr()); 2053 AddTypeLoc(E->getWrittenTypeInfo()); 2054} 2055void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 2056 WL.push_back(SizeOfPackExprParts(E, Parent)); 2057} 2058 2059void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { 2060 EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S); 2061} 2062 2063bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { 2064 if (RegionOfInterest.isValid()) { 2065 SourceRange Range = getRawCursorExtent(C); 2066 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 2067 return false; 2068 } 2069 return true; 2070} 2071 2072bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { 2073 while (!WL.empty()) { 2074 // Dequeue the worklist item. 2075 VisitorJob LI = WL.back(); 2076 WL.pop_back(); 2077 2078 // Set the Parent field, then back to its old value once we're done. 2079 SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); 2080 2081 switch (LI.getKind()) { 2082 case VisitorJob::DeclVisitKind: { 2083 Decl *D = cast<DeclVisit>(&LI)->get(); 2084 if (!D) 2085 continue; 2086 2087 // For now, perform default visitation for Decls. 2088 if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst()))) 2089 return true; 2090 2091 continue; 2092 } 2093 case VisitorJob::ExplicitTemplateArgsVisitKind: { 2094 const ExplicitTemplateArgumentList *ArgList = 2095 cast<ExplicitTemplateArgsVisit>(&LI)->get(); 2096 for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), 2097 *ArgEnd = Arg + ArgList->NumTemplateArgs; 2098 Arg != ArgEnd; ++Arg) { 2099 if (VisitTemplateArgumentLoc(*Arg)) 2100 return true; 2101 } 2102 continue; 2103 } 2104 case VisitorJob::TypeLocVisitKind: { 2105 // Perform default visitation for TypeLocs. 2106 if (Visit(cast<TypeLocVisit>(&LI)->get())) 2107 return true; 2108 continue; 2109 } 2110 case VisitorJob::LabelRefVisitKind: { 2111 LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); 2112 if (LabelStmt *stmt = LS->getStmt()) { 2113 if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), 2114 TU))) { 2115 return true; 2116 } 2117 } 2118 continue; 2119 } 2120 2121 case VisitorJob::NestedNameSpecifierVisitKind: { 2122 NestedNameSpecifierVisit *V = cast<NestedNameSpecifierVisit>(&LI); 2123 if (VisitNestedNameSpecifier(V->get(), V->getSourceRange())) 2124 return true; 2125 continue; 2126 } 2127 2128 case VisitorJob::NestedNameSpecifierLocVisitKind: { 2129 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); 2130 if (VisitNestedNameSpecifierLoc(V->get())) 2131 return true; 2132 continue; 2133 } 2134 2135 case VisitorJob::DeclarationNameInfoVisitKind: { 2136 if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) 2137 ->get())) 2138 return true; 2139 continue; 2140 } 2141 case VisitorJob::MemberRefVisitKind: { 2142 MemberRefVisit *V = cast<MemberRefVisit>(&LI); 2143 if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) 2144 return true; 2145 continue; 2146 } 2147 case VisitorJob::StmtVisitKind: { 2148 Stmt *S = cast<StmtVisit>(&LI)->get(); 2149 if (!S) 2150 continue; 2151 2152 // Update the current cursor. 2153 CXCursor Cursor = MakeCXCursor(S, StmtParent, TU); 2154 if (!IsInRegionOfInterest(Cursor)) 2155 continue; 2156 switch (Visitor(Cursor, Parent, ClientData)) { 2157 case CXChildVisit_Break: return true; 2158 case CXChildVisit_Continue: break; 2159 case CXChildVisit_Recurse: 2160 EnqueueWorkList(WL, S); 2161 break; 2162 } 2163 continue; 2164 } 2165 case VisitorJob::MemberExprPartsKind: { 2166 // Handle the other pieces in the MemberExpr besides the base. 2167 MemberExpr *M = cast<MemberExprParts>(&LI)->get(); 2168 2169 // Visit the nested-name-specifier 2170 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) 2171 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2172 return true; 2173 2174 // Visit the declaration name. 2175 if (VisitDeclarationNameInfo(M->getMemberNameInfo())) 2176 return true; 2177 2178 // Visit the explicitly-specified template arguments, if any. 2179 if (M->hasExplicitTemplateArgs()) { 2180 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), 2181 *ArgEnd = Arg + M->getNumTemplateArgs(); 2182 Arg != ArgEnd; ++Arg) { 2183 if (VisitTemplateArgumentLoc(*Arg)) 2184 return true; 2185 } 2186 } 2187 continue; 2188 } 2189 case VisitorJob::DeclRefExprPartsKind: { 2190 DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); 2191 // Visit nested-name-specifier, if present. 2192 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) 2193 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2194 return true; 2195 // Visit declaration name. 2196 if (VisitDeclarationNameInfo(DR->getNameInfo())) 2197 return true; 2198 continue; 2199 } 2200 case VisitorJob::OverloadExprPartsKind: { 2201 OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); 2202 // Visit the nested-name-specifier. 2203 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) 2204 if (VisitNestedNameSpecifierLoc(QualifierLoc)) 2205 return true; 2206 // Visit the declaration name. 2207 if (VisitDeclarationNameInfo(O->getNameInfo())) 2208 return true; 2209 // Visit the overloaded declaration reference. 2210 if (Visit(MakeCursorOverloadedDeclRef(O, TU))) 2211 return true; 2212 continue; 2213 } 2214 case VisitorJob::SizeOfPackExprPartsKind: { 2215 SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); 2216 NamedDecl *Pack = E->getPack(); 2217 if (isa<TemplateTypeParmDecl>(Pack)) { 2218 if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), 2219 E->getPackLoc(), TU))) 2220 return true; 2221 2222 continue; 2223 } 2224 2225 if (isa<TemplateTemplateParmDecl>(Pack)) { 2226 if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), 2227 E->getPackLoc(), TU))) 2228 return true; 2229 2230 continue; 2231 } 2232 2233 // Non-type template parameter packs and function parameter packs are 2234 // treated like DeclRefExpr cursors. 2235 continue; 2236 } 2237 } 2238 } 2239 return false; 2240} 2241 2242bool CursorVisitor::Visit(Stmt *S) { 2243 VisitorWorkList *WL = 0; 2244 if (!WorkListFreeList.empty()) { 2245 WL = WorkListFreeList.back(); 2246 WL->clear(); 2247 WorkListFreeList.pop_back(); 2248 } 2249 else { 2250 WL = new VisitorWorkList(); 2251 WorkListCache.push_back(WL); 2252 } 2253 EnqueueWorkList(*WL, S); 2254 bool result = RunVisitorWorkList(*WL); 2255 WorkListFreeList.push_back(WL); 2256 return result; 2257} 2258 2259//===----------------------------------------------------------------------===// 2260// Misc. API hooks. 2261//===----------------------------------------------------------------------===// 2262 2263static llvm::sys::Mutex EnableMultithreadingMutex; 2264static bool EnabledMultithreading; 2265 2266extern "C" { 2267CXIndex clang_createIndex(int excludeDeclarationsFromPCH, 2268 int displayDiagnostics) { 2269 // Disable pretty stack trace functionality, which will otherwise be a very 2270 // poor citizen of the world and set up all sorts of signal handlers. 2271 llvm::DisablePrettyStackTrace = true; 2272 2273 // We use crash recovery to make some of our APIs more reliable, implicitly 2274 // enable it. 2275 llvm::CrashRecoveryContext::Enable(); 2276 2277 // Enable support for multithreading in LLVM. 2278 { 2279 llvm::sys::ScopedLock L(EnableMultithreadingMutex); 2280 if (!EnabledMultithreading) { 2281 llvm::llvm_start_multithreaded(); 2282 EnabledMultithreading = true; 2283 } 2284 } 2285 2286 CIndexer *CIdxr = new CIndexer(); 2287 if (excludeDeclarationsFromPCH) 2288 CIdxr->setOnlyLocalDecls(); 2289 if (displayDiagnostics) 2290 CIdxr->setDisplayDiagnostics(); 2291 return CIdxr; 2292} 2293 2294void clang_disposeIndex(CXIndex CIdx) { 2295 if (CIdx) 2296 delete static_cast<CIndexer *>(CIdx); 2297} 2298 2299CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, 2300 const char *ast_filename) { 2301 if (!CIdx) 2302 return 0; 2303 2304 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2305 FileSystemOptions FileSystemOpts; 2306 FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); 2307 2308 llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 2309 ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, 2310 CXXIdx->getOnlyLocalDecls(), 2311 0, 0, true); 2312 return MakeCXTranslationUnit(TU); 2313} 2314 2315unsigned clang_defaultEditingTranslationUnitOptions() { 2316 return CXTranslationUnit_PrecompiledPreamble | 2317 CXTranslationUnit_CacheCompletionResults | 2318 CXTranslationUnit_CXXPrecompiledPreamble; 2319} 2320 2321CXTranslationUnit 2322clang_createTranslationUnitFromSourceFile(CXIndex CIdx, 2323 const char *source_filename, 2324 int num_command_line_args, 2325 const char * const *command_line_args, 2326 unsigned num_unsaved_files, 2327 struct CXUnsavedFile *unsaved_files) { 2328 return clang_parseTranslationUnit(CIdx, source_filename, 2329 command_line_args, num_command_line_args, 2330 unsaved_files, num_unsaved_files, 2331 CXTranslationUnit_DetailedPreprocessingRecord); 2332} 2333 2334struct ParseTranslationUnitInfo { 2335 CXIndex CIdx; 2336 const char *source_filename; 2337 const char *const *command_line_args; 2338 int num_command_line_args; 2339 struct CXUnsavedFile *unsaved_files; 2340 unsigned num_unsaved_files; 2341 unsigned options; 2342 CXTranslationUnit result; 2343}; 2344static void clang_parseTranslationUnit_Impl(void *UserData) { 2345 ParseTranslationUnitInfo *PTUI = 2346 static_cast<ParseTranslationUnitInfo*>(UserData); 2347 CXIndex CIdx = PTUI->CIdx; 2348 const char *source_filename = PTUI->source_filename; 2349 const char * const *command_line_args = PTUI->command_line_args; 2350 int num_command_line_args = PTUI->num_command_line_args; 2351 struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; 2352 unsigned num_unsaved_files = PTUI->num_unsaved_files; 2353 unsigned options = PTUI->options; 2354 PTUI->result = 0; 2355 2356 if (!CIdx) 2357 return; 2358 2359 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 2360 2361 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; 2362 bool CompleteTranslationUnit 2363 = ((options & CXTranslationUnit_Incomplete) == 0); 2364 bool CacheCodeCompetionResults 2365 = options & CXTranslationUnit_CacheCompletionResults; 2366 bool CXXPrecompilePreamble 2367 = options & CXTranslationUnit_CXXPrecompiledPreamble; 2368 bool CXXChainedPCH 2369 = options & CXTranslationUnit_CXXChainedPCH; 2370 2371 // Configure the diagnostics. 2372 DiagnosticOptions DiagOpts; 2373 llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 2374 Diags = CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, 2375 command_line_args); 2376 2377 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 2378 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2379 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2380 const llvm::MemoryBuffer *Buffer 2381 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2382 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 2383 Buffer)); 2384 } 2385 2386 llvm::SmallVector<const char *, 16> Args; 2387 2388 // The 'source_filename' argument is optional. If the caller does not 2389 // specify it then it is assumed that the source file is specified 2390 // in the actual argument list. 2391 if (source_filename) 2392 Args.push_back(source_filename); 2393 2394 // Since the Clang C library is primarily used by batch tools dealing with 2395 // (often very broken) source code, where spell-checking can have a 2396 // significant negative impact on performance (particularly when 2397 // precompiled headers are involved), we disable it by default. 2398 // Only do this if we haven't found a spell-checking-related argument. 2399 bool FoundSpellCheckingArgument = false; 2400 for (int I = 0; I != num_command_line_args; ++I) { 2401 if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || 2402 strcmp(command_line_args[I], "-fspell-checking") == 0) { 2403 FoundSpellCheckingArgument = true; 2404 break; 2405 } 2406 } 2407 if (!FoundSpellCheckingArgument) 2408 Args.push_back("-fno-spell-checking"); 2409 2410 Args.insert(Args.end(), command_line_args, 2411 command_line_args + num_command_line_args); 2412 2413 // Do we need the detailed preprocessing record? 2414 if (options & CXTranslationUnit_DetailedPreprocessingRecord) { 2415 Args.push_back("-Xclang"); 2416 Args.push_back("-detailed-preprocessing-record"); 2417 } 2418 2419 unsigned NumErrors = Diags->getClient()->getNumErrors(); 2420 llvm::OwningPtr<ASTUnit> Unit( 2421 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), 2422 Diags, 2423 CXXIdx->getClangResourcesPath(), 2424 CXXIdx->getOnlyLocalDecls(), 2425 /*CaptureDiagnostics=*/true, 2426 RemappedFiles.data(), 2427 RemappedFiles.size(), 2428 /*RemappedFilesKeepOriginalName=*/true, 2429 PrecompilePreamble, 2430 CompleteTranslationUnit, 2431 CacheCodeCompetionResults, 2432 CXXPrecompilePreamble, 2433 CXXChainedPCH)); 2434 2435 if (NumErrors != Diags->getClient()->getNumErrors()) { 2436 // Make sure to check that 'Unit' is non-NULL. 2437 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { 2438 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), 2439 DEnd = Unit->stored_diag_end(); 2440 D != DEnd; ++D) { 2441 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); 2442 CXString Msg = clang_formatDiagnostic(&Diag, 2443 clang_defaultDiagnosticDisplayOptions()); 2444 fprintf(stderr, "%s\n", clang_getCString(Msg)); 2445 clang_disposeString(Msg); 2446 } 2447#ifdef LLVM_ON_WIN32 2448 // On Windows, force a flush, since there may be multiple copies of 2449 // stderr and stdout in the file system, all with different buffers 2450 // but writing to the same device. 2451 fflush(stderr); 2452#endif 2453 } 2454 } 2455 2456 PTUI->result = MakeCXTranslationUnit(Unit.take()); 2457} 2458CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, 2459 const char *source_filename, 2460 const char * const *command_line_args, 2461 int num_command_line_args, 2462 struct CXUnsavedFile *unsaved_files, 2463 unsigned num_unsaved_files, 2464 unsigned options) { 2465 ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, 2466 num_command_line_args, unsaved_files, 2467 num_unsaved_files, options, 0 }; 2468 llvm::CrashRecoveryContext CRC; 2469 2470 if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { 2471 fprintf(stderr, "libclang: crash detected during parsing: {\n"); 2472 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); 2473 fprintf(stderr, " 'command_line_args' : ["); 2474 for (int i = 0; i != num_command_line_args; ++i) { 2475 if (i) 2476 fprintf(stderr, ", "); 2477 fprintf(stderr, "'%s'", command_line_args[i]); 2478 } 2479 fprintf(stderr, "],\n"); 2480 fprintf(stderr, " 'unsaved_files' : ["); 2481 for (unsigned i = 0; i != num_unsaved_files; ++i) { 2482 if (i) 2483 fprintf(stderr, ", "); 2484 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, 2485 unsaved_files[i].Length); 2486 } 2487 fprintf(stderr, "],\n"); 2488 fprintf(stderr, " 'options' : %d,\n", options); 2489 fprintf(stderr, "}\n"); 2490 2491 return 0; 2492 } 2493 2494 return PTUI.result; 2495} 2496 2497unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { 2498 return CXSaveTranslationUnit_None; 2499} 2500 2501int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, 2502 unsigned options) { 2503 if (!TU) 2504 return 1; 2505 2506 return static_cast<ASTUnit *>(TU->TUData)->Save(FileName); 2507} 2508 2509void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { 2510 if (CTUnit) { 2511 // If the translation unit has been marked as unsafe to free, just discard 2512 // it. 2513 if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) 2514 return; 2515 2516 delete static_cast<ASTUnit *>(CTUnit->TUData); 2517 disposeCXStringPool(CTUnit->StringPool); 2518 delete CTUnit; 2519 } 2520} 2521 2522unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { 2523 return CXReparse_None; 2524} 2525 2526struct ReparseTranslationUnitInfo { 2527 CXTranslationUnit TU; 2528 unsigned num_unsaved_files; 2529 struct CXUnsavedFile *unsaved_files; 2530 unsigned options; 2531 int result; 2532}; 2533 2534static void clang_reparseTranslationUnit_Impl(void *UserData) { 2535 ReparseTranslationUnitInfo *RTUI = 2536 static_cast<ReparseTranslationUnitInfo*>(UserData); 2537 CXTranslationUnit TU = RTUI->TU; 2538 unsigned num_unsaved_files = RTUI->num_unsaved_files; 2539 struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; 2540 unsigned options = RTUI->options; 2541 (void) options; 2542 RTUI->result = 1; 2543 2544 if (!TU) 2545 return; 2546 2547 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 2548 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2549 2550 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 2551 for (unsigned I = 0; I != num_unsaved_files; ++I) { 2552 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 2553 const llvm::MemoryBuffer *Buffer 2554 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 2555 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 2556 Buffer)); 2557 } 2558 2559 if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size())) 2560 RTUI->result = 0; 2561} 2562 2563int clang_reparseTranslationUnit(CXTranslationUnit TU, 2564 unsigned num_unsaved_files, 2565 struct CXUnsavedFile *unsaved_files, 2566 unsigned options) { 2567 ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, 2568 options, 0 }; 2569 llvm::CrashRecoveryContext CRC; 2570 2571 if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { 2572 fprintf(stderr, "libclang: crash detected during reparsing\n"); 2573 static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 2574 return 1; 2575 } 2576 2577 2578 return RTUI.result; 2579} 2580 2581 2582CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { 2583 if (!CTUnit) 2584 return createCXString(""); 2585 2586 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); 2587 return createCXString(CXXUnit->getOriginalSourceFileName(), true); 2588} 2589 2590CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { 2591 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; 2592 return Result; 2593} 2594 2595} // end: extern "C" 2596 2597//===----------------------------------------------------------------------===// 2598// CXSourceLocation and CXSourceRange Operations. 2599//===----------------------------------------------------------------------===// 2600 2601extern "C" { 2602CXSourceLocation clang_getNullLocation() { 2603 CXSourceLocation Result = { { 0, 0 }, 0 }; 2604 return Result; 2605} 2606 2607unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { 2608 return (loc1.ptr_data[0] == loc2.ptr_data[0] && 2609 loc1.ptr_data[1] == loc2.ptr_data[1] && 2610 loc1.int_data == loc2.int_data); 2611} 2612 2613CXSourceLocation clang_getLocation(CXTranslationUnit tu, 2614 CXFile file, 2615 unsigned line, 2616 unsigned column) { 2617 if (!tu || !file) 2618 return clang_getNullLocation(); 2619 2620 bool Logging = ::getenv("LIBCLANG_LOGGING"); 2621 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2622 const FileEntry *File = static_cast<const FileEntry *>(file); 2623 SourceLocation SLoc 2624 = CXXUnit->getSourceManager().getLocation(File, line, column); 2625 if (SLoc.isInvalid()) { 2626 if (Logging) 2627 llvm::errs() << "clang_getLocation(\"" << File->getName() 2628 << "\", " << line << ", " << column << ") = invalid\n"; 2629 return clang_getNullLocation(); 2630 } 2631 2632 if (Logging) 2633 llvm::errs() << "clang_getLocation(\"" << File->getName() 2634 << "\", " << line << ", " << column << ") = " 2635 << SLoc.getRawEncoding() << "\n"; 2636 2637 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); 2638} 2639 2640CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu, 2641 CXFile file, 2642 unsigned offset) { 2643 if (!tu || !file) 2644 return clang_getNullLocation(); 2645 2646 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2647 SourceLocation Start 2648 = CXXUnit->getSourceManager().getLocation( 2649 static_cast<const FileEntry *>(file), 2650 1, 1); 2651 if (Start.isInvalid()) return clang_getNullLocation(); 2652 2653 SourceLocation SLoc = Start.getFileLocWithOffset(offset); 2654 2655 if (SLoc.isInvalid()) return clang_getNullLocation(); 2656 2657 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); 2658} 2659 2660CXSourceRange clang_getNullRange() { 2661 CXSourceRange Result = { { 0, 0 }, 0, 0 }; 2662 return Result; 2663} 2664 2665CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { 2666 if (begin.ptr_data[0] != end.ptr_data[0] || 2667 begin.ptr_data[1] != end.ptr_data[1]) 2668 return clang_getNullRange(); 2669 2670 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, 2671 begin.int_data, end.int_data }; 2672 return Result; 2673} 2674 2675void clang_getInstantiationLocation(CXSourceLocation location, 2676 CXFile *file, 2677 unsigned *line, 2678 unsigned *column, 2679 unsigned *offset) { 2680 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 2681 2682 if (!location.ptr_data[0] || Loc.isInvalid()) { 2683 if (file) 2684 *file = 0; 2685 if (line) 2686 *line = 0; 2687 if (column) 2688 *column = 0; 2689 if (offset) 2690 *offset = 0; 2691 return; 2692 } 2693 2694 const SourceManager &SM = 2695 *static_cast<const SourceManager*>(location.ptr_data[0]); 2696 SourceLocation InstLoc = SM.getInstantiationLoc(Loc); 2697 2698 if (file) 2699 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); 2700 if (line) 2701 *line = SM.getInstantiationLineNumber(InstLoc); 2702 if (column) 2703 *column = SM.getInstantiationColumnNumber(InstLoc); 2704 if (offset) 2705 *offset = SM.getDecomposedLoc(InstLoc).second; 2706} 2707 2708void clang_getSpellingLocation(CXSourceLocation location, 2709 CXFile *file, 2710 unsigned *line, 2711 unsigned *column, 2712 unsigned *offset) { 2713 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 2714 2715 if (!location.ptr_data[0] || Loc.isInvalid()) { 2716 if (file) 2717 *file = 0; 2718 if (line) 2719 *line = 0; 2720 if (column) 2721 *column = 0; 2722 if (offset) 2723 *offset = 0; 2724 return; 2725 } 2726 2727 const SourceManager &SM = 2728 *static_cast<const SourceManager*>(location.ptr_data[0]); 2729 SourceLocation SpellLoc = Loc; 2730 if (SpellLoc.isMacroID()) { 2731 SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc); 2732 if (SimpleSpellingLoc.isFileID() && 2733 SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first)) 2734 SpellLoc = SimpleSpellingLoc; 2735 else 2736 SpellLoc = SM.getInstantiationLoc(SpellLoc); 2737 } 2738 2739 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); 2740 FileID FID = LocInfo.first; 2741 unsigned FileOffset = LocInfo.second; 2742 2743 if (file) 2744 *file = (void *)SM.getFileEntryForID(FID); 2745 if (line) 2746 *line = SM.getLineNumber(FID, FileOffset); 2747 if (column) 2748 *column = SM.getColumnNumber(FID, FileOffset); 2749 if (offset) 2750 *offset = FileOffset; 2751} 2752 2753CXSourceLocation clang_getRangeStart(CXSourceRange range) { 2754 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 2755 range.begin_int_data }; 2756 return Result; 2757} 2758 2759CXSourceLocation clang_getRangeEnd(CXSourceRange range) { 2760 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 2761 range.end_int_data }; 2762 return Result; 2763} 2764 2765} // end: extern "C" 2766 2767//===----------------------------------------------------------------------===// 2768// CXFile Operations. 2769//===----------------------------------------------------------------------===// 2770 2771extern "C" { 2772CXString clang_getFileName(CXFile SFile) { 2773 if (!SFile) 2774 return createCXString((const char*)NULL); 2775 2776 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2777 return createCXString(FEnt->getName()); 2778} 2779 2780time_t clang_getFileTime(CXFile SFile) { 2781 if (!SFile) 2782 return 0; 2783 2784 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 2785 return FEnt->getModificationTime(); 2786} 2787 2788CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { 2789 if (!tu) 2790 return 0; 2791 2792 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); 2793 2794 FileManager &FMgr = CXXUnit->getFileManager(); 2795 return const_cast<FileEntry *>(FMgr.getFile(file_name)); 2796} 2797 2798} // end: extern "C" 2799 2800//===----------------------------------------------------------------------===// 2801// CXCursor Operations. 2802//===----------------------------------------------------------------------===// 2803 2804static Decl *getDeclFromExpr(Stmt *E) { 2805 if (CastExpr *CE = dyn_cast<CastExpr>(E)) 2806 return getDeclFromExpr(CE->getSubExpr()); 2807 2808 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) 2809 return RefExpr->getDecl(); 2810 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) 2811 return RefExpr->getDecl(); 2812 if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) 2813 return ME->getMemberDecl(); 2814 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) 2815 return RE->getDecl(); 2816 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) 2817 return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; 2818 2819 if (CallExpr *CE = dyn_cast<CallExpr>(E)) 2820 return getDeclFromExpr(CE->getCallee()); 2821 if (CXXConstructExpr *CE = llvm::dyn_cast<CXXConstructExpr>(E)) 2822 if (!CE->isElidable()) 2823 return CE->getConstructor(); 2824 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) 2825 return OME->getMethodDecl(); 2826 2827 if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) 2828 return PE->getProtocol(); 2829 if (SubstNonTypeTemplateParmPackExpr *NTTP 2830 = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) 2831 return NTTP->getParameterPack(); 2832 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2833 if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || 2834 isa<ParmVarDecl>(SizeOfPack->getPack())) 2835 return SizeOfPack->getPack(); 2836 2837 return 0; 2838} 2839 2840static SourceLocation getLocationFromExpr(Expr *E) { 2841 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) 2842 return /*FIXME:*/Msg->getLeftLoc(); 2843 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 2844 return DRE->getLocation(); 2845 if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) 2846 return RefExpr->getLocation(); 2847 if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) 2848 return Member->getMemberLoc(); 2849 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) 2850 return Ivar->getLocation(); 2851 if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) 2852 return SizeOfPack->getPackLoc(); 2853 2854 return E->getLocStart(); 2855} 2856 2857extern "C" { 2858 2859unsigned clang_visitChildren(CXCursor parent, 2860 CXCursorVisitor visitor, 2861 CXClientData client_data) { 2862 CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, 2863 getCursorASTUnit(parent)->getMaxPCHLevel()); 2864 return CursorVis.VisitChildren(parent); 2865} 2866 2867#ifndef __has_feature 2868#define __has_feature(x) 0 2869#endif 2870#if __has_feature(blocks) 2871typedef enum CXChildVisitResult 2872 (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); 2873 2874static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 2875 CXClientData client_data) { 2876 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 2877 return block(cursor, parent); 2878} 2879#else 2880// If we are compiled with a compiler that doesn't have native blocks support, 2881// define and call the block manually, so the 2882typedef struct _CXChildVisitResult 2883{ 2884 void *isa; 2885 int flags; 2886 int reserved; 2887 enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, 2888 CXCursor); 2889} *CXCursorVisitorBlock; 2890 2891static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, 2892 CXClientData client_data) { 2893 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; 2894 return block->invoke(block, cursor, parent); 2895} 2896#endif 2897 2898 2899unsigned clang_visitChildrenWithBlock(CXCursor parent, 2900 CXCursorVisitorBlock block) { 2901 return clang_visitChildren(parent, visitWithBlock, block); 2902} 2903 2904static CXString getDeclSpelling(Decl *D) { 2905 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); 2906 if (!ND) { 2907 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D)) 2908 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 2909 return createCXString(Property->getIdentifier()->getName()); 2910 2911 return createCXString(""); 2912 } 2913 2914 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) 2915 return createCXString(OMD->getSelector().getAsString()); 2916 2917 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) 2918 // No, this isn't the same as the code below. getIdentifier() is non-virtual 2919 // and returns different names. NamedDecl returns the class name and 2920 // ObjCCategoryImplDecl returns the category name. 2921 return createCXString(CIMP->getIdentifier()->getNameStart()); 2922 2923 if (isa<UsingDirectiveDecl>(D)) 2924 return createCXString(""); 2925 2926 llvm::SmallString<1024> S; 2927 llvm::raw_svector_ostream os(S); 2928 ND->printName(os); 2929 2930 return createCXString(os.str()); 2931} 2932 2933CXString clang_getCursorSpelling(CXCursor C) { 2934 if (clang_isTranslationUnit(C.kind)) 2935 return clang_getTranslationUnitSpelling( 2936 static_cast<CXTranslationUnit>(C.data[2])); 2937 2938 if (clang_isReference(C.kind)) { 2939 switch (C.kind) { 2940 case CXCursor_ObjCSuperClassRef: { 2941 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; 2942 return createCXString(Super->getIdentifier()->getNameStart()); 2943 } 2944 case CXCursor_ObjCClassRef: { 2945 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; 2946 return createCXString(Class->getIdentifier()->getNameStart()); 2947 } 2948 case CXCursor_ObjCProtocolRef: { 2949 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; 2950 assert(OID && "getCursorSpelling(): Missing protocol decl"); 2951 return createCXString(OID->getIdentifier()->getNameStart()); 2952 } 2953 case CXCursor_CXXBaseSpecifier: { 2954 CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); 2955 return createCXString(B->getType().getAsString()); 2956 } 2957 case CXCursor_TypeRef: { 2958 TypeDecl *Type = getCursorTypeRef(C).first; 2959 assert(Type && "Missing type decl"); 2960 2961 return createCXString(getCursorContext(C).getTypeDeclType(Type). 2962 getAsString()); 2963 } 2964 case CXCursor_TemplateRef: { 2965 TemplateDecl *Template = getCursorTemplateRef(C).first; 2966 assert(Template && "Missing template decl"); 2967 2968 return createCXString(Template->getNameAsString()); 2969 } 2970 2971 case CXCursor_NamespaceRef: { 2972 NamedDecl *NS = getCursorNamespaceRef(C).first; 2973 assert(NS && "Missing namespace decl"); 2974 2975 return createCXString(NS->getNameAsString()); 2976 } 2977 2978 case CXCursor_MemberRef: { 2979 FieldDecl *Field = getCursorMemberRef(C).first; 2980 assert(Field && "Missing member decl"); 2981 2982 return createCXString(Field->getNameAsString()); 2983 } 2984 2985 case CXCursor_LabelRef: { 2986 LabelStmt *Label = getCursorLabelRef(C).first; 2987 assert(Label && "Missing label"); 2988 2989 return createCXString(Label->getName()); 2990 } 2991 2992 case CXCursor_OverloadedDeclRef: { 2993 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 2994 if (Decl *D = Storage.dyn_cast<Decl *>()) { 2995 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) 2996 return createCXString(ND->getNameAsString()); 2997 return createCXString(""); 2998 } 2999 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 3000 return createCXString(E->getName().getAsString()); 3001 OverloadedTemplateStorage *Ovl 3002 = Storage.get<OverloadedTemplateStorage*>(); 3003 if (Ovl->size() == 0) 3004 return createCXString(""); 3005 return createCXString((*Ovl->begin())->getNameAsString()); 3006 } 3007 3008 default: 3009 return createCXString("<not implemented>"); 3010 } 3011 } 3012 3013 if (clang_isExpression(C.kind)) { 3014 Decl *D = getDeclFromExpr(getCursorExpr(C)); 3015 if (D) 3016 return getDeclSpelling(D); 3017 return createCXString(""); 3018 } 3019 3020 if (clang_isStatement(C.kind)) { 3021 Stmt *S = getCursorStmt(C); 3022 if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 3023 return createCXString(Label->getName()); 3024 3025 return createCXString(""); 3026 } 3027 3028 if (C.kind == CXCursor_MacroInstantiation) 3029 return createCXString(getCursorMacroInstantiation(C)->getName() 3030 ->getNameStart()); 3031 3032 if (C.kind == CXCursor_MacroDefinition) 3033 return createCXString(getCursorMacroDefinition(C)->getName() 3034 ->getNameStart()); 3035 3036 if (C.kind == CXCursor_InclusionDirective) 3037 return createCXString(getCursorInclusionDirective(C)->getFileName()); 3038 3039 if (clang_isDeclaration(C.kind)) 3040 return getDeclSpelling(getCursorDecl(C)); 3041 3042 return createCXString(""); 3043} 3044 3045CXString clang_getCursorDisplayName(CXCursor C) { 3046 if (!clang_isDeclaration(C.kind)) 3047 return clang_getCursorSpelling(C); 3048 3049 Decl *D = getCursorDecl(C); 3050 if (!D) 3051 return createCXString(""); 3052 3053 PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy; 3054 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) 3055 D = FunTmpl->getTemplatedDecl(); 3056 3057 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 3058 llvm::SmallString<64> Str; 3059 llvm::raw_svector_ostream OS(Str); 3060 OS << Function->getNameAsString(); 3061 if (Function->getPrimaryTemplate()) 3062 OS << "<>"; 3063 OS << "("; 3064 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { 3065 if (I) 3066 OS << ", "; 3067 OS << Function->getParamDecl(I)->getType().getAsString(Policy); 3068 } 3069 3070 if (Function->isVariadic()) { 3071 if (Function->getNumParams()) 3072 OS << ", "; 3073 OS << "..."; 3074 } 3075 OS << ")"; 3076 return createCXString(OS.str()); 3077 } 3078 3079 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { 3080 llvm::SmallString<64> Str; 3081 llvm::raw_svector_ostream OS(Str); 3082 OS << ClassTemplate->getNameAsString(); 3083 OS << "<"; 3084 TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); 3085 for (unsigned I = 0, N = Params->size(); I != N; ++I) { 3086 if (I) 3087 OS << ", "; 3088 3089 NamedDecl *Param = Params->getParam(I); 3090 if (Param->getIdentifier()) { 3091 OS << Param->getIdentifier()->getName(); 3092 continue; 3093 } 3094 3095 // There is no parameter name, which makes this tricky. Try to come up 3096 // with something useful that isn't too long. 3097 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 3098 OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); 3099 else if (NonTypeTemplateParmDecl *NTTP 3100 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 3101 OS << NTTP->getType().getAsString(Policy); 3102 else 3103 OS << "template<...> class"; 3104 } 3105 3106 OS << ">"; 3107 return createCXString(OS.str()); 3108 } 3109 3110 if (ClassTemplateSpecializationDecl *ClassSpec 3111 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 3112 // If the type was explicitly written, use that. 3113 if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) 3114 return createCXString(TSInfo->getType().getAsString(Policy)); 3115 3116 llvm::SmallString<64> Str; 3117 llvm::raw_svector_ostream OS(Str); 3118 OS << ClassSpec->getNameAsString(); 3119 OS << TemplateSpecializationType::PrintTemplateArgumentList( 3120 ClassSpec->getTemplateArgs().data(), 3121 ClassSpec->getTemplateArgs().size(), 3122 Policy); 3123 return createCXString(OS.str()); 3124 } 3125 3126 return clang_getCursorSpelling(C); 3127} 3128 3129CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { 3130 switch (Kind) { 3131 case CXCursor_FunctionDecl: 3132 return createCXString("FunctionDecl"); 3133 case CXCursor_TypedefDecl: 3134 return createCXString("TypedefDecl"); 3135 case CXCursor_EnumDecl: 3136 return createCXString("EnumDecl"); 3137 case CXCursor_EnumConstantDecl: 3138 return createCXString("EnumConstantDecl"); 3139 case CXCursor_StructDecl: 3140 return createCXString("StructDecl"); 3141 case CXCursor_UnionDecl: 3142 return createCXString("UnionDecl"); 3143 case CXCursor_ClassDecl: 3144 return createCXString("ClassDecl"); 3145 case CXCursor_FieldDecl: 3146 return createCXString("FieldDecl"); 3147 case CXCursor_VarDecl: 3148 return createCXString("VarDecl"); 3149 case CXCursor_ParmDecl: 3150 return createCXString("ParmDecl"); 3151 case CXCursor_ObjCInterfaceDecl: 3152 return createCXString("ObjCInterfaceDecl"); 3153 case CXCursor_ObjCCategoryDecl: 3154 return createCXString("ObjCCategoryDecl"); 3155 case CXCursor_ObjCProtocolDecl: 3156 return createCXString("ObjCProtocolDecl"); 3157 case CXCursor_ObjCPropertyDecl: 3158 return createCXString("ObjCPropertyDecl"); 3159 case CXCursor_ObjCIvarDecl: 3160 return createCXString("ObjCIvarDecl"); 3161 case CXCursor_ObjCInstanceMethodDecl: 3162 return createCXString("ObjCInstanceMethodDecl"); 3163 case CXCursor_ObjCClassMethodDecl: 3164 return createCXString("ObjCClassMethodDecl"); 3165 case CXCursor_ObjCImplementationDecl: 3166 return createCXString("ObjCImplementationDecl"); 3167 case CXCursor_ObjCCategoryImplDecl: 3168 return createCXString("ObjCCategoryImplDecl"); 3169 case CXCursor_CXXMethod: 3170 return createCXString("CXXMethod"); 3171 case CXCursor_UnexposedDecl: 3172 return createCXString("UnexposedDecl"); 3173 case CXCursor_ObjCSuperClassRef: 3174 return createCXString("ObjCSuperClassRef"); 3175 case CXCursor_ObjCProtocolRef: 3176 return createCXString("ObjCProtocolRef"); 3177 case CXCursor_ObjCClassRef: 3178 return createCXString("ObjCClassRef"); 3179 case CXCursor_TypeRef: 3180 return createCXString("TypeRef"); 3181 case CXCursor_TemplateRef: 3182 return createCXString("TemplateRef"); 3183 case CXCursor_NamespaceRef: 3184 return createCXString("NamespaceRef"); 3185 case CXCursor_MemberRef: 3186 return createCXString("MemberRef"); 3187 case CXCursor_LabelRef: 3188 return createCXString("LabelRef"); 3189 case CXCursor_OverloadedDeclRef: 3190 return createCXString("OverloadedDeclRef"); 3191 case CXCursor_UnexposedExpr: 3192 return createCXString("UnexposedExpr"); 3193 case CXCursor_BlockExpr: 3194 return createCXString("BlockExpr"); 3195 case CXCursor_DeclRefExpr: 3196 return createCXString("DeclRefExpr"); 3197 case CXCursor_MemberRefExpr: 3198 return createCXString("MemberRefExpr"); 3199 case CXCursor_CallExpr: 3200 return createCXString("CallExpr"); 3201 case CXCursor_ObjCMessageExpr: 3202 return createCXString("ObjCMessageExpr"); 3203 case CXCursor_UnexposedStmt: 3204 return createCXString("UnexposedStmt"); 3205 case CXCursor_LabelStmt: 3206 return createCXString("LabelStmt"); 3207 case CXCursor_InvalidFile: 3208 return createCXString("InvalidFile"); 3209 case CXCursor_InvalidCode: 3210 return createCXString("InvalidCode"); 3211 case CXCursor_NoDeclFound: 3212 return createCXString("NoDeclFound"); 3213 case CXCursor_NotImplemented: 3214 return createCXString("NotImplemented"); 3215 case CXCursor_TranslationUnit: 3216 return createCXString("TranslationUnit"); 3217 case CXCursor_UnexposedAttr: 3218 return createCXString("UnexposedAttr"); 3219 case CXCursor_IBActionAttr: 3220 return createCXString("attribute(ibaction)"); 3221 case CXCursor_IBOutletAttr: 3222 return createCXString("attribute(iboutlet)"); 3223 case CXCursor_IBOutletCollectionAttr: 3224 return createCXString("attribute(iboutletcollection)"); 3225 case CXCursor_PreprocessingDirective: 3226 return createCXString("preprocessing directive"); 3227 case CXCursor_MacroDefinition: 3228 return createCXString("macro definition"); 3229 case CXCursor_MacroInstantiation: 3230 return createCXString("macro instantiation"); 3231 case CXCursor_InclusionDirective: 3232 return createCXString("inclusion directive"); 3233 case CXCursor_Namespace: 3234 return createCXString("Namespace"); 3235 case CXCursor_LinkageSpec: 3236 return createCXString("LinkageSpec"); 3237 case CXCursor_CXXBaseSpecifier: 3238 return createCXString("C++ base class specifier"); 3239 case CXCursor_Constructor: 3240 return createCXString("CXXConstructor"); 3241 case CXCursor_Destructor: 3242 return createCXString("CXXDestructor"); 3243 case CXCursor_ConversionFunction: 3244 return createCXString("CXXConversion"); 3245 case CXCursor_TemplateTypeParameter: 3246 return createCXString("TemplateTypeParameter"); 3247 case CXCursor_NonTypeTemplateParameter: 3248 return createCXString("NonTypeTemplateParameter"); 3249 case CXCursor_TemplateTemplateParameter: 3250 return createCXString("TemplateTemplateParameter"); 3251 case CXCursor_FunctionTemplate: 3252 return createCXString("FunctionTemplate"); 3253 case CXCursor_ClassTemplate: 3254 return createCXString("ClassTemplate"); 3255 case CXCursor_ClassTemplatePartialSpecialization: 3256 return createCXString("ClassTemplatePartialSpecialization"); 3257 case CXCursor_NamespaceAlias: 3258 return createCXString("NamespaceAlias"); 3259 case CXCursor_UsingDirective: 3260 return createCXString("UsingDirective"); 3261 case CXCursor_UsingDeclaration: 3262 return createCXString("UsingDeclaration"); 3263 } 3264 3265 llvm_unreachable("Unhandled CXCursorKind"); 3266 return createCXString((const char*) 0); 3267} 3268 3269enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, 3270 CXCursor parent, 3271 CXClientData client_data) { 3272 CXCursor *BestCursor = static_cast<CXCursor *>(client_data); 3273 3274 // If our current best cursor is the construction of a temporary object, 3275 // don't replace that cursor with a type reference, because we want 3276 // clang_getCursor() to point at the constructor. 3277 if (clang_isExpression(BestCursor->kind) && 3278 isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && 3279 cursor.kind == CXCursor_TypeRef) 3280 return CXChildVisit_Recurse; 3281 3282 // Don't override a preprocessing cursor with another preprocessing 3283 // cursor; we want the outermost preprocessing cursor. 3284 if (clang_isPreprocessing(cursor.kind) && 3285 clang_isPreprocessing(BestCursor->kind)) 3286 return CXChildVisit_Recurse; 3287 3288 *BestCursor = cursor; 3289 return CXChildVisit_Recurse; 3290} 3291 3292CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { 3293 if (!TU) 3294 return clang_getNullCursor(); 3295 3296 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 3297 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 3298 3299 // Translate the given source location to make it point at the beginning of 3300 // the token under the cursor. 3301 SourceLocation SLoc = cxloc::translateSourceLocation(Loc); 3302 3303 // Guard against an invalid SourceLocation, or we may assert in one 3304 // of the following calls. 3305 if (SLoc.isInvalid()) 3306 return clang_getNullCursor(); 3307 3308 bool Logging = getenv("LIBCLANG_LOGGING"); 3309 SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), 3310 CXXUnit->getASTContext().getLangOptions()); 3311 3312 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); 3313 if (SLoc.isValid()) { 3314 // FIXME: Would be great to have a "hint" cursor, then walk from that 3315 // hint cursor upward until we find a cursor whose source range encloses 3316 // the region of interest, rather than starting from the translation unit. 3317 CXCursor Parent = clang_getTranslationUnitCursor(TU); 3318 CursorVisitor CursorVis(TU, GetCursorVisitor, &Result, 3319 Decl::MaxPCHLevel, SourceLocation(SLoc)); 3320 CursorVis.VisitChildren(Parent); 3321 } 3322 3323 if (Logging) { 3324 CXFile SearchFile; 3325 unsigned SearchLine, SearchColumn; 3326 CXFile ResultFile; 3327 unsigned ResultLine, ResultColumn; 3328 CXString SearchFileName, ResultFileName, KindSpelling, USR; 3329 const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; 3330 CXSourceLocation ResultLoc = clang_getCursorLocation(Result); 3331 3332 clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 3333 0); 3334 clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine, 3335 &ResultColumn, 0); 3336 SearchFileName = clang_getFileName(SearchFile); 3337 ResultFileName = clang_getFileName(ResultFile); 3338 KindSpelling = clang_getCursorKindSpelling(Result.kind); 3339 USR = clang_getCursorUSR(Result); 3340 fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n", 3341 clang_getCString(SearchFileName), SearchLine, SearchColumn, 3342 clang_getCString(KindSpelling), 3343 clang_getCString(ResultFileName), ResultLine, ResultColumn, 3344 clang_getCString(USR), IsDef); 3345 clang_disposeString(SearchFileName); 3346 clang_disposeString(ResultFileName); 3347 clang_disposeString(KindSpelling); 3348 clang_disposeString(USR); 3349 3350 CXCursor Definition = clang_getCursorDefinition(Result); 3351 if (!clang_equalCursors(Definition, clang_getNullCursor())) { 3352 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); 3353 CXString DefinitionKindSpelling 3354 = clang_getCursorKindSpelling(Definition.kind); 3355 CXFile DefinitionFile; 3356 unsigned DefinitionLine, DefinitionColumn; 3357 clang_getInstantiationLocation(DefinitionLoc, &DefinitionFile, 3358 &DefinitionLine, &DefinitionColumn, 0); 3359 CXString DefinitionFileName = clang_getFileName(DefinitionFile); 3360 fprintf(stderr, " -> %s(%s:%d:%d)\n", 3361 clang_getCString(DefinitionKindSpelling), 3362 clang_getCString(DefinitionFileName), 3363 DefinitionLine, DefinitionColumn); 3364 clang_disposeString(DefinitionFileName); 3365 clang_disposeString(DefinitionKindSpelling); 3366 } 3367 } 3368 3369 return Result; 3370} 3371 3372CXCursor clang_getNullCursor(void) { 3373 return MakeCXCursorInvalid(CXCursor_InvalidFile); 3374} 3375 3376unsigned clang_equalCursors(CXCursor X, CXCursor Y) { 3377 return X == Y; 3378} 3379 3380unsigned clang_hashCursor(CXCursor C) { 3381 unsigned Index = 0; 3382 if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) 3383 Index = 1; 3384 3385 return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( 3386 std::make_pair(C.kind, C.data[Index])); 3387} 3388 3389unsigned clang_isInvalid(enum CXCursorKind K) { 3390 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; 3391} 3392 3393unsigned clang_isDeclaration(enum CXCursorKind K) { 3394 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; 3395} 3396 3397unsigned clang_isReference(enum CXCursorKind K) { 3398 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; 3399} 3400 3401unsigned clang_isExpression(enum CXCursorKind K) { 3402 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; 3403} 3404 3405unsigned clang_isStatement(enum CXCursorKind K) { 3406 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; 3407} 3408 3409unsigned clang_isTranslationUnit(enum CXCursorKind K) { 3410 return K == CXCursor_TranslationUnit; 3411} 3412 3413unsigned clang_isPreprocessing(enum CXCursorKind K) { 3414 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; 3415} 3416 3417unsigned clang_isUnexposed(enum CXCursorKind K) { 3418 switch (K) { 3419 case CXCursor_UnexposedDecl: 3420 case CXCursor_UnexposedExpr: 3421 case CXCursor_UnexposedStmt: 3422 case CXCursor_UnexposedAttr: 3423 return true; 3424 default: 3425 return false; 3426 } 3427} 3428 3429CXCursorKind clang_getCursorKind(CXCursor C) { 3430 return C.kind; 3431} 3432 3433CXSourceLocation clang_getCursorLocation(CXCursor C) { 3434 if (clang_isReference(C.kind)) { 3435 switch (C.kind) { 3436 case CXCursor_ObjCSuperClassRef: { 3437 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3438 = getCursorObjCSuperClassRef(C); 3439 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3440 } 3441 3442 case CXCursor_ObjCProtocolRef: { 3443 std::pair<ObjCProtocolDecl *, SourceLocation> P 3444 = getCursorObjCProtocolRef(C); 3445 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3446 } 3447 3448 case CXCursor_ObjCClassRef: { 3449 std::pair<ObjCInterfaceDecl *, SourceLocation> P 3450 = getCursorObjCClassRef(C); 3451 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3452 } 3453 3454 case CXCursor_TypeRef: { 3455 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); 3456 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3457 } 3458 3459 case CXCursor_TemplateRef: { 3460 std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); 3461 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3462 } 3463 3464 case CXCursor_NamespaceRef: { 3465 std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); 3466 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3467 } 3468 3469 case CXCursor_MemberRef: { 3470 std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); 3471 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 3472 } 3473 3474 case CXCursor_CXXBaseSpecifier: { 3475 CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); 3476 if (!BaseSpec) 3477 return clang_getNullLocation(); 3478 3479 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) 3480 return cxloc::translateSourceLocation(getCursorContext(C), 3481 TSInfo->getTypeLoc().getBeginLoc()); 3482 3483 return cxloc::translateSourceLocation(getCursorContext(C), 3484 BaseSpec->getSourceRange().getBegin()); 3485 } 3486 3487 case CXCursor_LabelRef: { 3488 std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); 3489 return cxloc::translateSourceLocation(getCursorContext(C), P.second); 3490 } 3491 3492 case CXCursor_OverloadedDeclRef: 3493 return cxloc::translateSourceLocation(getCursorContext(C), 3494 getCursorOverloadedDeclRef(C).second); 3495 3496 default: 3497 // FIXME: Need a way to enumerate all non-reference cases. 3498 llvm_unreachable("Missed a reference kind"); 3499 } 3500 } 3501 3502 if (clang_isExpression(C.kind)) 3503 return cxloc::translateSourceLocation(getCursorContext(C), 3504 getLocationFromExpr(getCursorExpr(C))); 3505 3506 if (clang_isStatement(C.kind)) 3507 return cxloc::translateSourceLocation(getCursorContext(C), 3508 getCursorStmt(C)->getLocStart()); 3509 3510 if (C.kind == CXCursor_PreprocessingDirective) { 3511 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); 3512 return cxloc::translateSourceLocation(getCursorContext(C), L); 3513 } 3514 3515 if (C.kind == CXCursor_MacroInstantiation) { 3516 SourceLocation L 3517 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin(); 3518 return cxloc::translateSourceLocation(getCursorContext(C), L); 3519 } 3520 3521 if (C.kind == CXCursor_MacroDefinition) { 3522 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); 3523 return cxloc::translateSourceLocation(getCursorContext(C), L); 3524 } 3525 3526 if (C.kind == CXCursor_InclusionDirective) { 3527 SourceLocation L 3528 = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); 3529 return cxloc::translateSourceLocation(getCursorContext(C), L); 3530 } 3531 3532 if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) 3533 return clang_getNullLocation(); 3534 3535 Decl *D = getCursorDecl(C); 3536 SourceLocation Loc = D->getLocation(); 3537 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) 3538 Loc = Class->getClassLoc(); 3539 // FIXME: Multiple variables declared in a single declaration 3540 // currently lack the information needed to correctly determine their 3541 // ranges when accounting for the type-specifier. We use context 3542 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3543 // and if so, whether it is the first decl. 3544 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3545 if (!cxcursor::isFirstInDeclGroup(C)) 3546 Loc = VD->getLocation(); 3547 } 3548 3549 return cxloc::translateSourceLocation(getCursorContext(C), Loc); 3550} 3551 3552} // end extern "C" 3553 3554static SourceRange getRawCursorExtent(CXCursor C) { 3555 if (clang_isReference(C.kind)) { 3556 switch (C.kind) { 3557 case CXCursor_ObjCSuperClassRef: 3558 return getCursorObjCSuperClassRef(C).second; 3559 3560 case CXCursor_ObjCProtocolRef: 3561 return getCursorObjCProtocolRef(C).second; 3562 3563 case CXCursor_ObjCClassRef: 3564 return getCursorObjCClassRef(C).second; 3565 3566 case CXCursor_TypeRef: 3567 return getCursorTypeRef(C).second; 3568 3569 case CXCursor_TemplateRef: 3570 return getCursorTemplateRef(C).second; 3571 3572 case CXCursor_NamespaceRef: 3573 return getCursorNamespaceRef(C).second; 3574 3575 case CXCursor_MemberRef: 3576 return getCursorMemberRef(C).second; 3577 3578 case CXCursor_CXXBaseSpecifier: 3579 return getCursorCXXBaseSpecifier(C)->getSourceRange(); 3580 3581 case CXCursor_LabelRef: 3582 return getCursorLabelRef(C).second; 3583 3584 case CXCursor_OverloadedDeclRef: 3585 return getCursorOverloadedDeclRef(C).second; 3586 3587 default: 3588 // FIXME: Need a way to enumerate all non-reference cases. 3589 llvm_unreachable("Missed a reference kind"); 3590 } 3591 } 3592 3593 if (clang_isExpression(C.kind)) 3594 return getCursorExpr(C)->getSourceRange(); 3595 3596 if (clang_isStatement(C.kind)) 3597 return getCursorStmt(C)->getSourceRange(); 3598 3599 if (C.kind == CXCursor_PreprocessingDirective) 3600 return cxcursor::getCursorPreprocessingDirective(C); 3601 3602 if (C.kind == CXCursor_MacroInstantiation) 3603 return cxcursor::getCursorMacroInstantiation(C)->getSourceRange(); 3604 3605 if (C.kind == CXCursor_MacroDefinition) 3606 return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); 3607 3608 if (C.kind == CXCursor_InclusionDirective) 3609 return cxcursor::getCursorInclusionDirective(C)->getSourceRange(); 3610 3611 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 3612 Decl *D = cxcursor::getCursorDecl(C); 3613 SourceRange R = D->getSourceRange(); 3614 // FIXME: Multiple variables declared in a single declaration 3615 // currently lack the information needed to correctly determine their 3616 // ranges when accounting for the type-specifier. We use context 3617 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3618 // and if so, whether it is the first decl. 3619 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3620 if (!cxcursor::isFirstInDeclGroup(C)) 3621 R.setBegin(VD->getLocation()); 3622 } 3623 return R; 3624 } 3625 return SourceRange(); 3626} 3627 3628/// \brief Retrieves the "raw" cursor extent, which is then extended to include 3629/// the decl-specifier-seq for declarations. 3630static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { 3631 if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { 3632 Decl *D = cxcursor::getCursorDecl(C); 3633 SourceRange R = D->getSourceRange(); 3634 3635 // Adjust the start of the location for declarations preceded by 3636 // declaration specifiers. 3637 SourceLocation StartLoc; 3638 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 3639 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 3640 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 3641 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { 3642 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 3643 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 3644 } 3645 3646 if (StartLoc.isValid() && R.getBegin().isValid() && 3647 SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) 3648 R.setBegin(StartLoc); 3649 3650 // FIXME: Multiple variables declared in a single declaration 3651 // currently lack the information needed to correctly determine their 3652 // ranges when accounting for the type-specifier. We use context 3653 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, 3654 // and if so, whether it is the first decl. 3655 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 3656 if (!cxcursor::isFirstInDeclGroup(C)) 3657 R.setBegin(VD->getLocation()); 3658 } 3659 3660 return R; 3661 } 3662 3663 return getRawCursorExtent(C); 3664} 3665 3666extern "C" { 3667 3668CXSourceRange clang_getCursorExtent(CXCursor C) { 3669 SourceRange R = getRawCursorExtent(C); 3670 if (R.isInvalid()) 3671 return clang_getNullRange(); 3672 3673 return cxloc::translateSourceRange(getCursorContext(C), R); 3674} 3675 3676CXCursor clang_getCursorReferenced(CXCursor C) { 3677 if (clang_isInvalid(C.kind)) 3678 return clang_getNullCursor(); 3679 3680 CXTranslationUnit tu = getCursorTU(C); 3681 if (clang_isDeclaration(C.kind)) { 3682 Decl *D = getCursorDecl(C); 3683 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 3684 return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); 3685 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) 3686 return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); 3687 if (ObjCForwardProtocolDecl *Protocols 3688 = dyn_cast<ObjCForwardProtocolDecl>(D)) 3689 return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); 3690 if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D)) 3691 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) 3692 return MakeCXCursor(Property, tu); 3693 3694 return C; 3695 } 3696 3697 if (clang_isExpression(C.kind)) { 3698 Expr *E = getCursorExpr(C); 3699 Decl *D = getDeclFromExpr(E); 3700 if (D) 3701 return MakeCXCursor(D, tu); 3702 3703 if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) 3704 return MakeCursorOverloadedDeclRef(Ovl, tu); 3705 3706 return clang_getNullCursor(); 3707 } 3708 3709 if (clang_isStatement(C.kind)) { 3710 Stmt *S = getCursorStmt(C); 3711 if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) 3712 return MakeCXCursor(Goto->getLabel()->getStmt(), getCursorDecl(C), tu); 3713 3714 return clang_getNullCursor(); 3715 } 3716 3717 if (C.kind == CXCursor_MacroInstantiation) { 3718 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition()) 3719 return MakeMacroDefinitionCursor(Def, tu); 3720 } 3721 3722 if (!clang_isReference(C.kind)) 3723 return clang_getNullCursor(); 3724 3725 switch (C.kind) { 3726 case CXCursor_ObjCSuperClassRef: 3727 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); 3728 3729 case CXCursor_ObjCProtocolRef: { 3730 return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); 3731 3732 case CXCursor_ObjCClassRef: 3733 return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); 3734 3735 case CXCursor_TypeRef: 3736 return MakeCXCursor(getCursorTypeRef(C).first, tu ); 3737 3738 case CXCursor_TemplateRef: 3739 return MakeCXCursor(getCursorTemplateRef(C).first, tu ); 3740 3741 case CXCursor_NamespaceRef: 3742 return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); 3743 3744 case CXCursor_MemberRef: 3745 return MakeCXCursor(getCursorMemberRef(C).first, tu ); 3746 3747 case CXCursor_CXXBaseSpecifier: { 3748 CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); 3749 return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), 3750 tu )); 3751 } 3752 3753 case CXCursor_LabelRef: 3754 // FIXME: We end up faking the "parent" declaration here because we 3755 // don't want to make CXCursor larger. 3756 return MakeCXCursor(getCursorLabelRef(C).first, 3757 static_cast<ASTUnit*>(tu->TUData)->getASTContext() 3758 .getTranslationUnitDecl(), 3759 tu); 3760 3761 case CXCursor_OverloadedDeclRef: 3762 return C; 3763 3764 default: 3765 // We would prefer to enumerate all non-reference cursor kinds here. 3766 llvm_unreachable("Unhandled reference cursor kind"); 3767 break; 3768 } 3769 } 3770 3771 return clang_getNullCursor(); 3772} 3773 3774CXCursor clang_getCursorDefinition(CXCursor C) { 3775 if (clang_isInvalid(C.kind)) 3776 return clang_getNullCursor(); 3777 3778 CXTranslationUnit TU = getCursorTU(C); 3779 3780 bool WasReference = false; 3781 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { 3782 C = clang_getCursorReferenced(C); 3783 WasReference = true; 3784 } 3785 3786 if (C.kind == CXCursor_MacroInstantiation) 3787 return clang_getCursorReferenced(C); 3788 3789 if (!clang_isDeclaration(C.kind)) 3790 return clang_getNullCursor(); 3791 3792 Decl *D = getCursorDecl(C); 3793 if (!D) 3794 return clang_getNullCursor(); 3795 3796 switch (D->getKind()) { 3797 // Declaration kinds that don't really separate the notions of 3798 // declaration and definition. 3799 case Decl::Namespace: 3800 case Decl::Typedef: 3801 case Decl::TemplateTypeParm: 3802 case Decl::EnumConstant: 3803 case Decl::Field: 3804 case Decl::IndirectField: 3805 case Decl::ObjCIvar: 3806 case Decl::ObjCAtDefsField: 3807 case Decl::ImplicitParam: 3808 case Decl::ParmVar: 3809 case Decl::NonTypeTemplateParm: 3810 case Decl::TemplateTemplateParm: 3811 case Decl::ObjCCategoryImpl: 3812 case Decl::ObjCImplementation: 3813 case Decl::AccessSpec: 3814 case Decl::LinkageSpec: 3815 case Decl::ObjCPropertyImpl: 3816 case Decl::FileScopeAsm: 3817 case Decl::StaticAssert: 3818 case Decl::Block: 3819 case Decl::Label: // FIXME: Is this right?? 3820 return C; 3821 3822 // Declaration kinds that don't make any sense here, but are 3823 // nonetheless harmless. 3824 case Decl::TranslationUnit: 3825 break; 3826 3827 // Declaration kinds for which the definition is not resolvable. 3828 case Decl::UnresolvedUsingTypename: 3829 case Decl::UnresolvedUsingValue: 3830 break; 3831 3832 case Decl::UsingDirective: 3833 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), 3834 TU); 3835 3836 case Decl::NamespaceAlias: 3837 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); 3838 3839 case Decl::Enum: 3840 case Decl::Record: 3841 case Decl::CXXRecord: 3842 case Decl::ClassTemplateSpecialization: 3843 case Decl::ClassTemplatePartialSpecialization: 3844 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) 3845 return MakeCXCursor(Def, TU); 3846 return clang_getNullCursor(); 3847 3848 case Decl::Function: 3849 case Decl::CXXMethod: 3850 case Decl::CXXConstructor: 3851 case Decl::CXXDestructor: 3852 case Decl::CXXConversion: { 3853 const FunctionDecl *Def = 0; 3854 if (cast<FunctionDecl>(D)->getBody(Def)) 3855 return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); 3856 return clang_getNullCursor(); 3857 } 3858 3859 case Decl::Var: { 3860 // Ask the variable if it has a definition. 3861 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) 3862 return MakeCXCursor(Def, TU); 3863 return clang_getNullCursor(); 3864 } 3865 3866 case Decl::FunctionTemplate: { 3867 const FunctionDecl *Def = 0; 3868 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) 3869 return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); 3870 return clang_getNullCursor(); 3871 } 3872 3873 case Decl::ClassTemplate: { 3874 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() 3875 ->getDefinition()) 3876 return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), 3877 TU); 3878 return clang_getNullCursor(); 3879 } 3880 3881 case Decl::Using: 3882 return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), 3883 D->getLocation(), TU); 3884 3885 case Decl::UsingShadow: 3886 return clang_getCursorDefinition( 3887 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), 3888 TU)); 3889 3890 case Decl::ObjCMethod: { 3891 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); 3892 if (Method->isThisDeclarationADefinition()) 3893 return C; 3894 3895 // Dig out the method definition in the associated 3896 // @implementation, if we have it. 3897 // FIXME: The ASTs should make finding the definition easier. 3898 if (ObjCInterfaceDecl *Class 3899 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) 3900 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) 3901 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), 3902 Method->isInstanceMethod())) 3903 if (Def->isThisDeclarationADefinition()) 3904 return MakeCXCursor(Def, TU); 3905 3906 return clang_getNullCursor(); 3907 } 3908 3909 case Decl::ObjCCategory: 3910 if (ObjCCategoryImplDecl *Impl 3911 = cast<ObjCCategoryDecl>(D)->getImplementation()) 3912 return MakeCXCursor(Impl, TU); 3913 return clang_getNullCursor(); 3914 3915 case Decl::ObjCProtocol: 3916 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) 3917 return C; 3918 return clang_getNullCursor(); 3919 3920 case Decl::ObjCInterface: 3921 // There are two notions of a "definition" for an Objective-C 3922 // class: the interface and its implementation. When we resolved a 3923 // reference to an Objective-C class, produce the @interface as 3924 // the definition; when we were provided with the interface, 3925 // produce the @implementation as the definition. 3926 if (WasReference) { 3927 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) 3928 return C; 3929 } else if (ObjCImplementationDecl *Impl 3930 = cast<ObjCInterfaceDecl>(D)->getImplementation()) 3931 return MakeCXCursor(Impl, TU); 3932 return clang_getNullCursor(); 3933 3934 case Decl::ObjCProperty: 3935 // FIXME: We don't really know where to find the 3936 // ObjCPropertyImplDecls that implement this property. 3937 return clang_getNullCursor(); 3938 3939 case Decl::ObjCCompatibleAlias: 3940 if (ObjCInterfaceDecl *Class 3941 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) 3942 if (!Class->isForwardDecl()) 3943 return MakeCXCursor(Class, TU); 3944 3945 return clang_getNullCursor(); 3946 3947 case Decl::ObjCForwardProtocol: 3948 return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), 3949 D->getLocation(), TU); 3950 3951 case Decl::ObjCClass: 3952 return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), 3953 TU); 3954 3955 case Decl::Friend: 3956 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) 3957 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 3958 return clang_getNullCursor(); 3959 3960 case Decl::FriendTemplate: 3961 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) 3962 return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); 3963 return clang_getNullCursor(); 3964 } 3965 3966 return clang_getNullCursor(); 3967} 3968 3969unsigned clang_isCursorDefinition(CXCursor C) { 3970 if (!clang_isDeclaration(C.kind)) 3971 return 0; 3972 3973 return clang_getCursorDefinition(C) == C; 3974} 3975 3976CXCursor clang_getCanonicalCursor(CXCursor C) { 3977 if (!clang_isDeclaration(C.kind)) 3978 return C; 3979 3980 if (Decl *D = getCursorDecl(C)) 3981 return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); 3982 3983 return C; 3984} 3985 3986unsigned clang_getNumOverloadedDecls(CXCursor C) { 3987 if (C.kind != CXCursor_OverloadedDeclRef) 3988 return 0; 3989 3990 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; 3991 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 3992 return E->getNumDecls(); 3993 3994 if (OverloadedTemplateStorage *S 3995 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 3996 return S->size(); 3997 3998 Decl *D = Storage.get<Decl*>(); 3999 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) 4000 return Using->shadow_size(); 4001 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) 4002 return Classes->size(); 4003 if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) 4004 return Protocols->protocol_size(); 4005 4006 return 0; 4007} 4008 4009CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { 4010 if (cursor.kind != CXCursor_OverloadedDeclRef) 4011 return clang_getNullCursor(); 4012 4013 if (index >= clang_getNumOverloadedDecls(cursor)) 4014 return clang_getNullCursor(); 4015 4016 CXTranslationUnit TU = getCursorTU(cursor); 4017 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; 4018 if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) 4019 return MakeCXCursor(E->decls_begin()[index], TU); 4020 4021 if (OverloadedTemplateStorage *S 4022 = Storage.dyn_cast<OverloadedTemplateStorage*>()) 4023 return MakeCXCursor(S->begin()[index], TU); 4024 4025 Decl *D = Storage.get<Decl*>(); 4026 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { 4027 // FIXME: This is, unfortunately, linear time. 4028 UsingDecl::shadow_iterator Pos = Using->shadow_begin(); 4029 std::advance(Pos, index); 4030 return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); 4031 } 4032 4033 if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) 4034 return MakeCXCursor(Classes->begin()[index].getInterface(), TU); 4035 4036 if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) 4037 return MakeCXCursor(Protocols->protocol_begin()[index], TU); 4038 4039 return clang_getNullCursor(); 4040} 4041 4042void clang_getDefinitionSpellingAndExtent(CXCursor C, 4043 const char **startBuf, 4044 const char **endBuf, 4045 unsigned *startLine, 4046 unsigned *startColumn, 4047 unsigned *endLine, 4048 unsigned *endColumn) { 4049 assert(getCursorDecl(C) && "CXCursor has null decl"); 4050 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); 4051 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); 4052 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); 4053 4054 SourceManager &SM = FD->getASTContext().getSourceManager(); 4055 *startBuf = SM.getCharacterData(Body->getLBracLoc()); 4056 *endBuf = SM.getCharacterData(Body->getRBracLoc()); 4057 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); 4058 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); 4059 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); 4060 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); 4061} 4062 4063void clang_enableStackTraces(void) { 4064 llvm::sys::PrintStackTraceOnErrorSignal(); 4065} 4066 4067void clang_executeOnThread(void (*fn)(void*), void *user_data, 4068 unsigned stack_size) { 4069 llvm::llvm_execute_on_thread(fn, user_data, stack_size); 4070} 4071 4072} // end: extern "C" 4073 4074//===----------------------------------------------------------------------===// 4075// Token-based Operations. 4076//===----------------------------------------------------------------------===// 4077 4078/* CXToken layout: 4079 * int_data[0]: a CXTokenKind 4080 * int_data[1]: starting token location 4081 * int_data[2]: token length 4082 * int_data[3]: reserved 4083 * ptr_data: for identifiers and keywords, an IdentifierInfo*. 4084 * otherwise unused. 4085 */ 4086extern "C" { 4087 4088CXTokenKind clang_getTokenKind(CXToken CXTok) { 4089 return static_cast<CXTokenKind>(CXTok.int_data[0]); 4090} 4091 4092CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { 4093 switch (clang_getTokenKind(CXTok)) { 4094 case CXToken_Identifier: 4095 case CXToken_Keyword: 4096 // We know we have an IdentifierInfo*, so use that. 4097 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) 4098 ->getNameStart()); 4099 4100 case CXToken_Literal: { 4101 // We have stashed the starting pointer in the ptr_data field. Use it. 4102 const char *Text = static_cast<const char *>(CXTok.ptr_data); 4103 return createCXString(llvm::StringRef(Text, CXTok.int_data[2])); 4104 } 4105 4106 case CXToken_Punctuation: 4107 case CXToken_Comment: 4108 break; 4109 } 4110 4111 // We have to find the starting buffer pointer the hard way, by 4112 // deconstructing the source location. 4113 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4114 if (!CXXUnit) 4115 return createCXString(""); 4116 4117 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); 4118 std::pair<FileID, unsigned> LocInfo 4119 = CXXUnit->getSourceManager().getDecomposedLoc(Loc); 4120 bool Invalid = false; 4121 llvm::StringRef Buffer 4122 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); 4123 if (Invalid) 4124 return createCXString(""); 4125 4126 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); 4127} 4128 4129CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { 4130 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4131 if (!CXXUnit) 4132 return clang_getNullLocation(); 4133 4134 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), 4135 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4136} 4137 4138CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { 4139 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4140 if (!CXXUnit) 4141 return clang_getNullRange(); 4142 4143 return cxloc::translateSourceRange(CXXUnit->getASTContext(), 4144 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 4145} 4146 4147void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, 4148 CXToken **Tokens, unsigned *NumTokens) { 4149 if (Tokens) 4150 *Tokens = 0; 4151 if (NumTokens) 4152 *NumTokens = 0; 4153 4154 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4155 if (!CXXUnit || !Tokens || !NumTokens) 4156 return; 4157 4158 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 4159 4160 SourceRange R = cxloc::translateCXSourceRange(Range); 4161 if (R.isInvalid()) 4162 return; 4163 4164 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 4165 std::pair<FileID, unsigned> BeginLocInfo 4166 = SourceMgr.getDecomposedLoc(R.getBegin()); 4167 std::pair<FileID, unsigned> EndLocInfo 4168 = SourceMgr.getDecomposedLoc(R.getEnd()); 4169 4170 // Cannot tokenize across files. 4171 if (BeginLocInfo.first != EndLocInfo.first) 4172 return; 4173 4174 // Create a lexer 4175 bool Invalid = false; 4176 llvm::StringRef Buffer 4177 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 4178 if (Invalid) 4179 return; 4180 4181 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 4182 CXXUnit->getASTContext().getLangOptions(), 4183 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); 4184 Lex.SetCommentRetentionState(true); 4185 4186 // Lex tokens until we hit the end of the range. 4187 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; 4188 llvm::SmallVector<CXToken, 32> CXTokens; 4189 Token Tok; 4190 bool previousWasAt = false; 4191 do { 4192 // Lex the next token 4193 Lex.LexFromRawLexer(Tok); 4194 if (Tok.is(tok::eof)) 4195 break; 4196 4197 // Initialize the CXToken. 4198 CXToken CXTok; 4199 4200 // - Common fields 4201 CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); 4202 CXTok.int_data[2] = Tok.getLength(); 4203 CXTok.int_data[3] = 0; 4204 4205 // - Kind-specific fields 4206 if (Tok.isLiteral()) { 4207 CXTok.int_data[0] = CXToken_Literal; 4208 CXTok.ptr_data = (void *)Tok.getLiteralData(); 4209 } else if (Tok.is(tok::raw_identifier)) { 4210 // Lookup the identifier to determine whether we have a keyword. 4211 IdentifierInfo *II 4212 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); 4213 4214 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { 4215 CXTok.int_data[0] = CXToken_Keyword; 4216 } 4217 else { 4218 CXTok.int_data[0] = Tok.is(tok::identifier) 4219 ? CXToken_Identifier 4220 : CXToken_Keyword; 4221 } 4222 CXTok.ptr_data = II; 4223 } else if (Tok.is(tok::comment)) { 4224 CXTok.int_data[0] = CXToken_Comment; 4225 CXTok.ptr_data = 0; 4226 } else { 4227 CXTok.int_data[0] = CXToken_Punctuation; 4228 CXTok.ptr_data = 0; 4229 } 4230 CXTokens.push_back(CXTok); 4231 previousWasAt = Tok.is(tok::at); 4232 } while (Lex.getBufferLocation() <= EffectiveBufferEnd); 4233 4234 if (CXTokens.empty()) 4235 return; 4236 4237 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); 4238 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); 4239 *NumTokens = CXTokens.size(); 4240} 4241 4242void clang_disposeTokens(CXTranslationUnit TU, 4243 CXToken *Tokens, unsigned NumTokens) { 4244 free(Tokens); 4245} 4246 4247} // end: extern "C" 4248 4249//===----------------------------------------------------------------------===// 4250// Token annotation APIs. 4251//===----------------------------------------------------------------------===// 4252 4253typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; 4254static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 4255 CXCursor parent, 4256 CXClientData client_data); 4257namespace { 4258class AnnotateTokensWorker { 4259 AnnotateTokensData &Annotated; 4260 CXToken *Tokens; 4261 CXCursor *Cursors; 4262 unsigned NumTokens; 4263 unsigned TokIdx; 4264 unsigned PreprocessingTokIdx; 4265 CursorVisitor AnnotateVis; 4266 SourceManager &SrcMgr; 4267 bool HasContextSensitiveKeywords; 4268 4269 bool MoreTokens() const { return TokIdx < NumTokens; } 4270 unsigned NextToken() const { return TokIdx; } 4271 void AdvanceToken() { ++TokIdx; } 4272 SourceLocation GetTokenLoc(unsigned tokI) { 4273 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 4274 } 4275 4276public: 4277 AnnotateTokensWorker(AnnotateTokensData &annotated, 4278 CXToken *tokens, CXCursor *cursors, unsigned numTokens, 4279 CXTranslationUnit tu, SourceRange RegionOfInterest) 4280 : Annotated(annotated), Tokens(tokens), Cursors(cursors), 4281 NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), 4282 AnnotateVis(tu, 4283 AnnotateTokensVisitor, this, 4284 Decl::MaxPCHLevel, RegionOfInterest), 4285 SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), 4286 HasContextSensitiveKeywords(false) { } 4287 4288 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } 4289 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); 4290 void AnnotateTokens(CXCursor parent); 4291 void AnnotateTokens() { 4292 AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); 4293 } 4294 4295 /// \brief Determine whether the annotator saw any cursors that have 4296 /// context-sensitive keywords. 4297 bool hasContextSensitiveKeywords() const { 4298 return HasContextSensitiveKeywords; 4299 } 4300}; 4301} 4302 4303void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { 4304 // Walk the AST within the region of interest, annotating tokens 4305 // along the way. 4306 VisitChildren(parent); 4307 4308 for (unsigned I = 0 ; I < TokIdx ; ++I) { 4309 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4310 if (Pos != Annotated.end() && 4311 (clang_isInvalid(Cursors[I].kind) || 4312 Pos->second.kind != CXCursor_PreprocessingDirective)) 4313 Cursors[I] = Pos->second; 4314 } 4315 4316 // Finish up annotating any tokens left. 4317 if (!MoreTokens()) 4318 return; 4319 4320 const CXCursor &C = clang_getNullCursor(); 4321 for (unsigned I = TokIdx ; I < NumTokens ; ++I) { 4322 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 4323 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; 4324 } 4325} 4326 4327enum CXChildVisitResult 4328AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { 4329 CXSourceLocation Loc = clang_getCursorLocation(cursor); 4330 SourceRange cursorRange = getRawCursorExtent(cursor); 4331 if (cursorRange.isInvalid()) 4332 return CXChildVisit_Recurse; 4333 4334 if (!HasContextSensitiveKeywords) { 4335 // Objective-C properties can have context-sensitive keywords. 4336 if (cursor.kind == CXCursor_ObjCPropertyDecl) { 4337 if (ObjCPropertyDecl *Property 4338 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) 4339 HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; 4340 } 4341 // Objective-C methods can have context-sensitive keywords. 4342 else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || 4343 cursor.kind == CXCursor_ObjCClassMethodDecl) { 4344 if (ObjCMethodDecl *Method 4345 = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { 4346 if (Method->getObjCDeclQualifier()) 4347 HasContextSensitiveKeywords = true; 4348 else { 4349 for (ObjCMethodDecl::param_iterator P = Method->param_begin(), 4350 PEnd = Method->param_end(); 4351 P != PEnd; ++P) { 4352 if ((*P)->getObjCDeclQualifier()) { 4353 HasContextSensitiveKeywords = true; 4354 break; 4355 } 4356 } 4357 } 4358 } 4359 } 4360 // C++ methods can have context-sensitive keywords. 4361 else if (cursor.kind == CXCursor_CXXMethod) { 4362 if (CXXMethodDecl *Method 4363 = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { 4364 if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) 4365 HasContextSensitiveKeywords = true; 4366 } 4367 } 4368 // C++ classes can have context-sensitive keywords. 4369 else if (cursor.kind == CXCursor_StructDecl || 4370 cursor.kind == CXCursor_ClassDecl || 4371 cursor.kind == CXCursor_ClassTemplate || 4372 cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { 4373 if (Decl *D = getCursorDecl(cursor)) 4374 if (D->hasAttr<FinalAttr>()) 4375 HasContextSensitiveKeywords = true; 4376 } 4377 } 4378 4379 if (clang_isPreprocessing(cursor.kind)) { 4380 // For macro instantiations, just note where the beginning of the macro 4381 // instantiation occurs. 4382 if (cursor.kind == CXCursor_MacroInstantiation) { 4383 Annotated[Loc.int_data] = cursor; 4384 return CXChildVisit_Recurse; 4385 } 4386 4387 // Items in the preprocessing record are kept separate from items in 4388 // declarations, so we keep a separate token index. 4389 unsigned SavedTokIdx = TokIdx; 4390 TokIdx = PreprocessingTokIdx; 4391 4392 // Skip tokens up until we catch up to the beginning of the preprocessing 4393 // entry. 4394 while (MoreTokens()) { 4395 const unsigned I = NextToken(); 4396 SourceLocation TokLoc = GetTokenLoc(I); 4397 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4398 case RangeBefore: 4399 AdvanceToken(); 4400 continue; 4401 case RangeAfter: 4402 case RangeOverlap: 4403 break; 4404 } 4405 break; 4406 } 4407 4408 // Look at all of the tokens within this range. 4409 while (MoreTokens()) { 4410 const unsigned I = NextToken(); 4411 SourceLocation TokLoc = GetTokenLoc(I); 4412 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4413 case RangeBefore: 4414 assert(0 && "Infeasible"); 4415 case RangeAfter: 4416 break; 4417 case RangeOverlap: 4418 Cursors[I] = cursor; 4419 AdvanceToken(); 4420 continue; 4421 } 4422 break; 4423 } 4424 4425 // Save the preprocessing token index; restore the non-preprocessing 4426 // token index. 4427 PreprocessingTokIdx = TokIdx; 4428 TokIdx = SavedTokIdx; 4429 return CXChildVisit_Recurse; 4430 } 4431 4432 if (cursorRange.isInvalid()) 4433 return CXChildVisit_Continue; 4434 4435 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); 4436 4437 // Adjust the annotated range based specific declarations. 4438 const enum CXCursorKind cursorK = clang_getCursorKind(cursor); 4439 if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { 4440 Decl *D = cxcursor::getCursorDecl(cursor); 4441 // Don't visit synthesized ObjC methods, since they have no syntatic 4442 // representation in the source. 4443 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { 4444 if (MD->isSynthesized()) 4445 return CXChildVisit_Continue; 4446 } 4447 4448 SourceLocation StartLoc; 4449 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { 4450 if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) 4451 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 4452 } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { 4453 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) 4454 StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); 4455 } 4456 4457 if (StartLoc.isValid() && L.isValid() && 4458 SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) 4459 cursorRange.setBegin(StartLoc); 4460 } 4461 4462 // If the location of the cursor occurs within a macro instantiation, record 4463 // the spelling location of the cursor in our annotation map. We can then 4464 // paper over the token labelings during a post-processing step to try and 4465 // get cursor mappings for tokens that are the *arguments* of a macro 4466 // instantiation. 4467 if (L.isMacroID()) { 4468 unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); 4469 // Only invalidate the old annotation if it isn't part of a preprocessing 4470 // directive. Here we assume that the default construction of CXCursor 4471 // results in CXCursor.kind being an initialized value (i.e., 0). If 4472 // this isn't the case, we can fix by doing lookup + insertion. 4473 4474 CXCursor &oldC = Annotated[rawEncoding]; 4475 if (!clang_isPreprocessing(oldC.kind)) 4476 oldC = cursor; 4477 } 4478 4479 const enum CXCursorKind K = clang_getCursorKind(parent); 4480 const CXCursor updateC = 4481 (clang_isInvalid(K) || K == CXCursor_TranslationUnit) 4482 ? clang_getNullCursor() : parent; 4483 4484 while (MoreTokens()) { 4485 const unsigned I = NextToken(); 4486 SourceLocation TokLoc = GetTokenLoc(I); 4487 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4488 case RangeBefore: 4489 Cursors[I] = updateC; 4490 AdvanceToken(); 4491 continue; 4492 case RangeAfter: 4493 case RangeOverlap: 4494 break; 4495 } 4496 break; 4497 } 4498 4499 // Visit children to get their cursor information. 4500 const unsigned BeforeChildren = NextToken(); 4501 VisitChildren(cursor); 4502 const unsigned AfterChildren = NextToken(); 4503 4504 // Adjust 'Last' to the last token within the extent of the cursor. 4505 while (MoreTokens()) { 4506 const unsigned I = NextToken(); 4507 SourceLocation TokLoc = GetTokenLoc(I); 4508 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 4509 case RangeBefore: 4510 assert(0 && "Infeasible"); 4511 case RangeAfter: 4512 break; 4513 case RangeOverlap: 4514 Cursors[I] = updateC; 4515 AdvanceToken(); 4516 continue; 4517 } 4518 break; 4519 } 4520 const unsigned Last = NextToken(); 4521 4522 // Scan the tokens that are at the beginning of the cursor, but are not 4523 // capture by the child cursors. 4524 4525 // For AST elements within macros, rely on a post-annotate pass to 4526 // to correctly annotate the tokens with cursors. Otherwise we can 4527 // get confusing results of having tokens that map to cursors that really 4528 // are expanded by an instantiation. 4529 if (L.isMacroID()) 4530 cursor = clang_getNullCursor(); 4531 4532 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { 4533 if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) 4534 break; 4535 4536 Cursors[I] = cursor; 4537 } 4538 // Scan the tokens that are at the end of the cursor, but are not captured 4539 // but the child cursors. 4540 for (unsigned I = AfterChildren; I != Last; ++I) 4541 Cursors[I] = cursor; 4542 4543 TokIdx = Last; 4544 return CXChildVisit_Continue; 4545} 4546 4547static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 4548 CXCursor parent, 4549 CXClientData client_data) { 4550 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); 4551} 4552 4553// This gets run a separate thread to avoid stack blowout. 4554static void runAnnotateTokensWorker(void *UserData) { 4555 ((AnnotateTokensWorker*)UserData)->AnnotateTokens(); 4556} 4557 4558extern "C" { 4559 4560void clang_annotateTokens(CXTranslationUnit TU, 4561 CXToken *Tokens, unsigned NumTokens, 4562 CXCursor *Cursors) { 4563 4564 if (NumTokens == 0 || !Tokens || !Cursors) 4565 return; 4566 4567 // Any token we don't specifically annotate will have a NULL cursor. 4568 CXCursor C = clang_getNullCursor(); 4569 for (unsigned I = 0; I != NumTokens; ++I) 4570 Cursors[I] = C; 4571 4572 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); 4573 if (!CXXUnit) 4574 return; 4575 4576 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 4577 4578 // Determine the region of interest, which contains all of the tokens. 4579 SourceRange RegionOfInterest; 4580 RegionOfInterest.setBegin(cxloc::translateSourceLocation( 4581 clang_getTokenLocation(TU, Tokens[0]))); 4582 RegionOfInterest.setEnd(cxloc::translateSourceLocation( 4583 clang_getTokenLocation(TU, 4584 Tokens[NumTokens - 1]))); 4585 4586 // A mapping from the source locations found when re-lexing or traversing the 4587 // region of interest to the corresponding cursors. 4588 AnnotateTokensData Annotated; 4589 4590 // Relex the tokens within the source range to look for preprocessing 4591 // directives. 4592 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 4593 std::pair<FileID, unsigned> BeginLocInfo 4594 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); 4595 std::pair<FileID, unsigned> EndLocInfo 4596 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); 4597 4598 llvm::StringRef Buffer; 4599 bool Invalid = false; 4600 if (BeginLocInfo.first == EndLocInfo.first && 4601 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && 4602 !Invalid) { 4603 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 4604 CXXUnit->getASTContext().getLangOptions(), 4605 Buffer.begin(), Buffer.data() + BeginLocInfo.second, 4606 Buffer.end()); 4607 Lex.SetCommentRetentionState(true); 4608 4609 // Lex tokens in raw mode until we hit the end of the range, to avoid 4610 // entering #includes or expanding macros. 4611 while (true) { 4612 Token Tok; 4613 Lex.LexFromRawLexer(Tok); 4614 4615 reprocess: 4616 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { 4617 // We have found a preprocessing directive. Gobble it up so that we 4618 // don't see it while preprocessing these tokens later, but keep track 4619 // of all of the token locations inside this preprocessing directive so 4620 // that we can annotate them appropriately. 4621 // 4622 // FIXME: Some simple tests here could identify macro definitions and 4623 // #undefs, to provide specific cursor kinds for those. 4624 std::vector<SourceLocation> Locations; 4625 do { 4626 Locations.push_back(Tok.getLocation()); 4627 Lex.LexFromRawLexer(Tok); 4628 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); 4629 4630 using namespace cxcursor; 4631 CXCursor Cursor 4632 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), 4633 Locations.back()), 4634 TU); 4635 for (unsigned I = 0, N = Locations.size(); I != N; ++I) { 4636 Annotated[Locations[I].getRawEncoding()] = Cursor; 4637 } 4638 4639 if (Tok.isAtStartOfLine()) 4640 goto reprocess; 4641 4642 continue; 4643 } 4644 4645 if (Tok.is(tok::eof)) 4646 break; 4647 } 4648 } 4649 4650 // Annotate all of the source locations in the region of interest that map to 4651 // a specific cursor. 4652 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, 4653 TU, RegionOfInterest); 4654 4655 // Run the worker within a CrashRecoveryContext. 4656 // FIXME: We use a ridiculous stack size here because the data-recursion 4657 // algorithm uses a large stack frame than the non-data recursive version, 4658 // and AnnotationTokensWorker currently transforms the data-recursion 4659 // algorithm back into a traditional recursion by explicitly calling 4660 // VisitChildren(). We will need to remove this explicit recursive call. 4661 llvm::CrashRecoveryContext CRC; 4662 if (!RunSafely(CRC, runAnnotateTokensWorker, &W, 4663 GetSafetyThreadStackSize() * 2)) { 4664 fprintf(stderr, "libclang: crash detected while annotating tokens\n"); 4665 } 4666 4667 // If we ran into any entities that involve context-sensitive keywords, 4668 // take another pass through the tokens to mark them as such. 4669 if (W.hasContextSensitiveKeywords()) { 4670 for (unsigned I = 0; I != NumTokens; ++I) { 4671 if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) 4672 continue; 4673 4674 if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { 4675 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 4676 if (ObjCPropertyDecl *Property 4677 = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { 4678 if (Property->getPropertyAttributesAsWritten() != 0 && 4679 llvm::StringSwitch<bool>(II->getName()) 4680 .Case("readonly", true) 4681 .Case("assign", true) 4682 .Case("readwrite", true) 4683 .Case("retain", true) 4684 .Case("copy", true) 4685 .Case("nonatomic", true) 4686 .Case("atomic", true) 4687 .Case("getter", true) 4688 .Case("setter", true) 4689 .Default(false)) 4690 Tokens[I].int_data[0] = CXToken_Keyword; 4691 } 4692 continue; 4693 } 4694 4695 if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || 4696 Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { 4697 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 4698 if (llvm::StringSwitch<bool>(II->getName()) 4699 .Case("in", true) 4700 .Case("out", true) 4701 .Case("inout", true) 4702 .Case("oneway", true) 4703 .Case("bycopy", true) 4704 .Case("byref", true) 4705 .Default(false)) 4706 Tokens[I].int_data[0] = CXToken_Keyword; 4707 continue; 4708 } 4709 4710 if (Cursors[I].kind == CXCursor_CXXMethod) { 4711 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 4712 if (CXXMethodDecl *Method 4713 = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(Cursors[I]))) { 4714 if ((Method->hasAttr<FinalAttr>() || 4715 Method->hasAttr<OverrideAttr>()) && 4716 Method->getLocation().getRawEncoding() != Tokens[I].int_data[1] && 4717 llvm::StringSwitch<bool>(II->getName()) 4718 .Case("final", true) 4719 .Case("override", true) 4720 .Default(false)) 4721 Tokens[I].int_data[0] = CXToken_Keyword; 4722 } 4723 continue; 4724 } 4725 4726 if (Cursors[I].kind == CXCursor_ClassDecl || 4727 Cursors[I].kind == CXCursor_StructDecl || 4728 Cursors[I].kind == CXCursor_ClassTemplate) { 4729 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); 4730 if (II->getName() == "final") { 4731 // We have to be careful with 'final', since it could be the name 4732 // of a member class rather than the context-sensitive keyword. 4733 // So, check whether the cursor associated with this 4734 Decl *D = getCursorDecl(Cursors[I]); 4735 if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(D)) { 4736 if ((Record->hasAttr<FinalAttr>()) && 4737 Record->getIdentifier() != II) 4738 Tokens[I].int_data[0] = CXToken_Keyword; 4739 } else if (ClassTemplateDecl *ClassTemplate 4740 = dyn_cast_or_null<ClassTemplateDecl>(D)) { 4741 CXXRecordDecl *Record = ClassTemplate->getTemplatedDecl(); 4742 if ((Record->hasAttr<FinalAttr>()) && 4743 Record->getIdentifier() != II) 4744 Tokens[I].int_data[0] = CXToken_Keyword; 4745 } 4746 } 4747 continue; 4748 } 4749 } 4750 } 4751} 4752} // end: extern "C" 4753 4754//===----------------------------------------------------------------------===// 4755// Operations for querying linkage of a cursor. 4756//===----------------------------------------------------------------------===// 4757 4758extern "C" { 4759CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { 4760 if (!clang_isDeclaration(cursor.kind)) 4761 return CXLinkage_Invalid; 4762 4763 Decl *D = cxcursor::getCursorDecl(cursor); 4764 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) 4765 switch (ND->getLinkage()) { 4766 case NoLinkage: return CXLinkage_NoLinkage; 4767 case InternalLinkage: return CXLinkage_Internal; 4768 case UniqueExternalLinkage: return CXLinkage_UniqueExternal; 4769 case ExternalLinkage: return CXLinkage_External; 4770 }; 4771 4772 return CXLinkage_Invalid; 4773} 4774} // end: extern "C" 4775 4776//===----------------------------------------------------------------------===// 4777// Operations for querying language of a cursor. 4778//===----------------------------------------------------------------------===// 4779 4780static CXLanguageKind getDeclLanguage(const Decl *D) { 4781 switch (D->getKind()) { 4782 default: 4783 break; 4784 case Decl::ImplicitParam: 4785 case Decl::ObjCAtDefsField: 4786 case Decl::ObjCCategory: 4787 case Decl::ObjCCategoryImpl: 4788 case Decl::ObjCClass: 4789 case Decl::ObjCCompatibleAlias: 4790 case Decl::ObjCForwardProtocol: 4791 case Decl::ObjCImplementation: 4792 case Decl::ObjCInterface: 4793 case Decl::ObjCIvar: 4794 case Decl::ObjCMethod: 4795 case Decl::ObjCProperty: 4796 case Decl::ObjCPropertyImpl: 4797 case Decl::ObjCProtocol: 4798 return CXLanguage_ObjC; 4799 case Decl::CXXConstructor: 4800 case Decl::CXXConversion: 4801 case Decl::CXXDestructor: 4802 case Decl::CXXMethod: 4803 case Decl::CXXRecord: 4804 case Decl::ClassTemplate: 4805 case Decl::ClassTemplatePartialSpecialization: 4806 case Decl::ClassTemplateSpecialization: 4807 case Decl::Friend: 4808 case Decl::FriendTemplate: 4809 case Decl::FunctionTemplate: 4810 case Decl::LinkageSpec: 4811 case Decl::Namespace: 4812 case Decl::NamespaceAlias: 4813 case Decl::NonTypeTemplateParm: 4814 case Decl::StaticAssert: 4815 case Decl::TemplateTemplateParm: 4816 case Decl::TemplateTypeParm: 4817 case Decl::UnresolvedUsingTypename: 4818 case Decl::UnresolvedUsingValue: 4819 case Decl::Using: 4820 case Decl::UsingDirective: 4821 case Decl::UsingShadow: 4822 return CXLanguage_CPlusPlus; 4823 } 4824 4825 return CXLanguage_C; 4826} 4827 4828extern "C" { 4829 4830enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { 4831 if (clang_isDeclaration(cursor.kind)) 4832 if (Decl *D = cxcursor::getCursorDecl(cursor)) { 4833 if (D->hasAttr<UnavailableAttr>() || 4834 (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())) 4835 return CXAvailability_Available; 4836 4837 if (D->hasAttr<DeprecatedAttr>()) 4838 return CXAvailability_Deprecated; 4839 } 4840 4841 return CXAvailability_Available; 4842} 4843 4844CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { 4845 if (clang_isDeclaration(cursor.kind)) 4846 return getDeclLanguage(cxcursor::getCursorDecl(cursor)); 4847 4848 return CXLanguage_Invalid; 4849} 4850 4851 /// \brief If the given cursor is the "templated" declaration 4852 /// descibing a class or function template, return the class or 4853 /// function template. 4854static Decl *maybeGetTemplateCursor(Decl *D) { 4855 if (!D) 4856 return 0; 4857 4858 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 4859 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) 4860 return FunTmpl; 4861 4862 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) 4863 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) 4864 return ClassTmpl; 4865 4866 return D; 4867} 4868 4869CXCursor clang_getCursorSemanticParent(CXCursor cursor) { 4870 if (clang_isDeclaration(cursor.kind)) { 4871 if (Decl *D = getCursorDecl(cursor)) { 4872 DeclContext *DC = D->getDeclContext(); 4873 if (!DC) 4874 return clang_getNullCursor(); 4875 4876 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 4877 getCursorTU(cursor)); 4878 } 4879 } 4880 4881 if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { 4882 if (Decl *D = getCursorDecl(cursor)) 4883 return MakeCXCursor(D, getCursorTU(cursor)); 4884 } 4885 4886 return clang_getNullCursor(); 4887} 4888 4889CXCursor clang_getCursorLexicalParent(CXCursor cursor) { 4890 if (clang_isDeclaration(cursor.kind)) { 4891 if (Decl *D = getCursorDecl(cursor)) { 4892 DeclContext *DC = D->getLexicalDeclContext(); 4893 if (!DC) 4894 return clang_getNullCursor(); 4895 4896 return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 4897 getCursorTU(cursor)); 4898 } 4899 } 4900 4901 // FIXME: Note that we can't easily compute the lexical context of a 4902 // statement or expression, so we return nothing. 4903 return clang_getNullCursor(); 4904} 4905 4906static void CollectOverriddenMethods(DeclContext *Ctx, 4907 ObjCMethodDecl *Method, 4908 llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) { 4909 if (!Ctx) 4910 return; 4911 4912 // If we have a class or category implementation, jump straight to the 4913 // interface. 4914 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx)) 4915 return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods); 4916 4917 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx); 4918 if (!Container) 4919 return; 4920 4921 // Check whether we have a matching method at this level. 4922 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), 4923 Method->isInstanceMethod())) 4924 if (Method != Overridden) { 4925 // We found an override at this level; there is no need to look 4926 // into other protocols or categories. 4927 Methods.push_back(Overridden); 4928 return; 4929 } 4930 4931 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { 4932 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), 4933 PEnd = Protocol->protocol_end(); 4934 P != PEnd; ++P) 4935 CollectOverriddenMethods(*P, Method, Methods); 4936 } 4937 4938 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { 4939 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), 4940 PEnd = Category->protocol_end(); 4941 P != PEnd; ++P) 4942 CollectOverriddenMethods(*P, Method, Methods); 4943 } 4944 4945 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { 4946 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), 4947 PEnd = Interface->protocol_end(); 4948 P != PEnd; ++P) 4949 CollectOverriddenMethods(*P, Method, Methods); 4950 4951 for (ObjCCategoryDecl *Category = Interface->getCategoryList(); 4952 Category; Category = Category->getNextClassCategory()) 4953 CollectOverriddenMethods(Category, Method, Methods); 4954 4955 // We only look into the superclass if we haven't found anything yet. 4956 if (Methods.empty()) 4957 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) 4958 return CollectOverriddenMethods(Super, Method, Methods); 4959 } 4960} 4961 4962void clang_getOverriddenCursors(CXCursor cursor, 4963 CXCursor **overridden, 4964 unsigned *num_overridden) { 4965 if (overridden) 4966 *overridden = 0; 4967 if (num_overridden) 4968 *num_overridden = 0; 4969 if (!overridden || !num_overridden) 4970 return; 4971 4972 if (!clang_isDeclaration(cursor.kind)) 4973 return; 4974 4975 Decl *D = getCursorDecl(cursor); 4976 if (!D) 4977 return; 4978 4979 // Handle C++ member functions. 4980 CXTranslationUnit TU = getCursorTU(cursor); 4981 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { 4982 *num_overridden = CXXMethod->size_overridden_methods(); 4983 if (!*num_overridden) 4984 return; 4985 4986 *overridden = new CXCursor [*num_overridden]; 4987 unsigned I = 0; 4988 for (CXXMethodDecl::method_iterator 4989 M = CXXMethod->begin_overridden_methods(), 4990 MEnd = CXXMethod->end_overridden_methods(); 4991 M != MEnd; (void)++M, ++I) 4992 (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU); 4993 return; 4994 } 4995 4996 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); 4997 if (!Method) 4998 return; 4999 5000 // Handle Objective-C methods. 5001 llvm::SmallVector<ObjCMethodDecl *, 4> Methods; 5002 CollectOverriddenMethods(Method->getDeclContext(), Method, Methods); 5003 5004 if (Methods.empty()) 5005 return; 5006 5007 *num_overridden = Methods.size(); 5008 *overridden = new CXCursor [Methods.size()]; 5009 for (unsigned I = 0, N = Methods.size(); I != N; ++I) 5010 (*overridden)[I] = MakeCXCursor(Methods[I], TU); 5011} 5012 5013void clang_disposeOverriddenCursors(CXCursor *overridden) { 5014 delete [] overridden; 5015} 5016 5017CXFile clang_getIncludedFile(CXCursor cursor) { 5018 if (cursor.kind != CXCursor_InclusionDirective) 5019 return 0; 5020 5021 InclusionDirective *ID = getCursorInclusionDirective(cursor); 5022 return (void *)ID->getFile(); 5023} 5024 5025} // end: extern "C" 5026 5027 5028//===----------------------------------------------------------------------===// 5029// C++ AST instrospection. 5030//===----------------------------------------------------------------------===// 5031 5032extern "C" { 5033unsigned clang_CXXMethod_isStatic(CXCursor C) { 5034 if (!clang_isDeclaration(C.kind)) 5035 return 0; 5036 5037 CXXMethodDecl *Method = 0; 5038 Decl *D = cxcursor::getCursorDecl(C); 5039 if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) 5040 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 5041 else 5042 Method = dyn_cast_or_null<CXXMethodDecl>(D); 5043 return (Method && Method->isStatic()) ? 1 : 0; 5044} 5045 5046} // end: extern "C" 5047 5048//===----------------------------------------------------------------------===// 5049// Attribute introspection. 5050//===----------------------------------------------------------------------===// 5051 5052extern "C" { 5053CXType clang_getIBOutletCollectionType(CXCursor C) { 5054 if (C.kind != CXCursor_IBOutletCollectionAttr) 5055 return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); 5056 5057 IBOutletCollectionAttr *A = 5058 cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); 5059 5060 return cxtype::MakeCXType(A->getInterFace(), cxcursor::getCursorTU(C)); 5061} 5062} // end: extern "C" 5063 5064//===----------------------------------------------------------------------===// 5065// Misc. utility functions. 5066//===----------------------------------------------------------------------===// 5067 5068/// Default to using an 8 MB stack size on "safety" threads. 5069static unsigned SafetyStackThreadSize = 8 << 20; 5070 5071namespace clang { 5072 5073bool RunSafely(llvm::CrashRecoveryContext &CRC, 5074 void (*Fn)(void*), void *UserData, 5075 unsigned Size) { 5076 if (!Size) 5077 Size = GetSafetyThreadStackSize(); 5078 if (Size) 5079 return CRC.RunSafelyOnThread(Fn, UserData, Size); 5080 return CRC.RunSafely(Fn, UserData); 5081} 5082 5083unsigned GetSafetyThreadStackSize() { 5084 return SafetyStackThreadSize; 5085} 5086 5087void SetSafetyThreadStackSize(unsigned Value) { 5088 SafetyStackThreadSize = Value; 5089} 5090 5091} 5092 5093extern "C" { 5094 5095CXString clang_getClangVersion() { 5096 return createCXString(getClangFullVersion()); 5097} 5098 5099} // end: extern "C" 5100