CIndex.cpp revision 8f06e0e9fec3ca501e5fb129f413adbfc88e82f8
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 "CXSourceLocation.h" 18#include "CIndexDiagnostic.h" 19 20#include "clang/Basic/Version.h" 21 22#include "clang/AST/DeclVisitor.h" 23#include "clang/AST/StmtVisitor.h" 24#include "clang/AST/TypeLocVisitor.h" 25#include "clang/Basic/Diagnostic.h" 26#include "clang/Frontend/ASTUnit.h" 27#include "clang/Frontend/CompilerInstance.h" 28#include "clang/Frontend/FrontendDiagnostic.h" 29#include "clang/Lex/Lexer.h" 30#include "clang/Lex/PreprocessingRecord.h" 31#include "clang/Lex/Preprocessor.h" 32#include "llvm/Support/MemoryBuffer.h" 33#include "llvm/System/Program.h" 34#include "llvm/System/Signals.h" 35 36// Needed to define L_TMPNAM on some systems. 37#include <cstdio> 38 39using namespace clang; 40using namespace clang::cxcursor; 41using namespace clang::cxstring; 42 43//===----------------------------------------------------------------------===// 44// Crash Reporting. 45//===----------------------------------------------------------------------===// 46 47#ifdef __APPLE__ 48#define USE_CRASHTRACER 49#include "clang/Analysis/Support/SaveAndRestore.h" 50// Integrate with crash reporter. 51extern "C" const char *__crashreporter_info__; 52#define NUM_CRASH_STRINGS 32 53static unsigned crashtracer_counter = 0; 54static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 }; 55static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; 56static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; 57 58static unsigned SetCrashTracerInfo(const char *str, 59 llvm::SmallString<1024> &AggStr) { 60 61 unsigned slot = 0; 62 while (crashtracer_strings[slot]) { 63 if (++slot == NUM_CRASH_STRINGS) 64 slot = 0; 65 } 66 crashtracer_strings[slot] = str; 67 crashtracer_counter_id[slot] = ++crashtracer_counter; 68 69 // We need to create an aggregate string because multiple threads 70 // may be in this method at one time. The crash reporter string 71 // will attempt to overapproximate the set of in-flight invocations 72 // of this function. Race conditions can still cause this goal 73 // to not be achieved. 74 { 75 llvm::raw_svector_ostream Out(AggStr); 76 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i) 77 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n'; 78 } 79 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str(); 80 return slot; 81} 82 83static void ResetCrashTracerInfo(unsigned slot) { 84 unsigned max_slot = 0; 85 unsigned max_value = 0; 86 87 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0; 88 89 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i) 90 if (agg_crashtracer_strings[i] && 91 crashtracer_counter_id[i] > max_value) { 92 max_slot = i; 93 max_value = crashtracer_counter_id[i]; 94 } 95 96 __crashreporter_info__ = agg_crashtracer_strings[max_slot]; 97} 98 99namespace { 100class ArgsCrashTracerInfo { 101 llvm::SmallString<1024> CrashString; 102 llvm::SmallString<1024> AggregateString; 103 unsigned crashtracerSlot; 104public: 105 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args) 106 : crashtracerSlot(0) 107 { 108 { 109 llvm::raw_svector_ostream Out(CrashString); 110 Out << "ClangCIndex [" << getClangFullVersion() << "]" 111 << "[createTranslationUnitFromSourceFile]: clang"; 112 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(), 113 E=Args.end(); I!=E; ++I) 114 Out << ' ' << *I; 115 } 116 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(), 117 AggregateString); 118 } 119 120 ~ArgsCrashTracerInfo() { 121 ResetCrashTracerInfo(crashtracerSlot); 122 } 123}; 124} 125#endif 126 127/// \brief The result of comparing two source ranges. 128enum RangeComparisonResult { 129 /// \brief Either the ranges overlap or one of the ranges is invalid. 130 RangeOverlap, 131 132 /// \brief The first range ends before the second range starts. 133 RangeBefore, 134 135 /// \brief The first range starts after the second range ends. 136 RangeAfter 137}; 138 139/// \brief Compare two source ranges to determine their relative position in 140/// the translation unit. 141static RangeComparisonResult RangeCompare(SourceManager &SM, 142 SourceRange R1, 143 SourceRange R2) { 144 assert(R1.isValid() && "First range is invalid?"); 145 assert(R2.isValid() && "Second range is invalid?"); 146 if (R1.getEnd() == R2.getBegin() || 147 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) 148 return RangeBefore; 149 if (R2.getEnd() == R1.getBegin() || 150 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) 151 return RangeAfter; 152 return RangeOverlap; 153} 154 155/// \brief Determine if a source location falls within, before, or after a 156/// a given source range. 157static RangeComparisonResult LocationCompare(SourceManager &SM, 158 SourceLocation L, SourceRange R) { 159 assert(R.isValid() && "First range is invalid?"); 160 assert(L.isValid() && "Second range is invalid?"); 161 if (L == R.getBegin()) 162 return RangeOverlap; 163 if (L == R.getEnd()) 164 return RangeAfter; 165 if (SM.isBeforeInTranslationUnit(L, R.getBegin())) 166 return RangeBefore; 167 if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) 168 return RangeAfter; 169 return RangeOverlap; 170} 171 172/// \brief Translate a Clang source range into a CIndex source range. 173/// 174/// Clang internally represents ranges where the end location points to the 175/// start of the token at the end. However, for external clients it is more 176/// useful to have a CXSourceRange be a proper half-open interval. This routine 177/// does the appropriate translation. 178CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, 179 const LangOptions &LangOpts, 180 SourceRange R) { 181 // We want the last character in this location, so we will adjust the 182 // location accordingly. 183 // FIXME: How do do this with a macro instantiation location? 184 SourceLocation EndLoc = R.getEnd(); 185 if (!EndLoc.isInvalid() && EndLoc.isFileID()) { 186 unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); 187 EndLoc = EndLoc.getFileLocWithOffset(Length); 188 } 189 190 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, 191 R.getBegin().getRawEncoding(), 192 EndLoc.getRawEncoding() }; 193 return Result; 194} 195 196//===----------------------------------------------------------------------===// 197// Cursor visitor. 198//===----------------------------------------------------------------------===// 199 200namespace { 201 202// Cursor visitor. 203class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, 204 public TypeLocVisitor<CursorVisitor, bool>, 205 public StmtVisitor<CursorVisitor, bool> 206{ 207 /// \brief The translation unit we are traversing. 208 ASTUnit *TU; 209 210 /// \brief The parent cursor whose children we are traversing. 211 CXCursor Parent; 212 213 /// \brief The declaration that serves at the parent of any statement or 214 /// expression nodes. 215 Decl *StmtParent; 216 217 /// \brief The visitor function. 218 CXCursorVisitor Visitor; 219 220 /// \brief The opaque client data, to be passed along to the visitor. 221 CXClientData ClientData; 222 223 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on 224 // to the visitor. Declarations with a PCH level greater than this value will 225 // be suppressed. 226 unsigned MaxPCHLevel; 227 228 /// \brief When valid, a source range to which the cursor should restrict 229 /// its search. 230 SourceRange RegionOfInterest; 231 232 using DeclVisitor<CursorVisitor, bool>::Visit; 233 using TypeLocVisitor<CursorVisitor, bool>::Visit; 234 using StmtVisitor<CursorVisitor, bool>::Visit; 235 236 /// \brief Determine whether this particular source range comes before, comes 237 /// after, or overlaps the region of interest. 238 /// 239 /// \param R a half-open source range retrieved from the abstract syntax tree. 240 RangeComparisonResult CompareRegionOfInterest(SourceRange R); 241 242public: 243 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData, 244 unsigned MaxPCHLevel, 245 SourceRange RegionOfInterest = SourceRange()) 246 : TU(TU), Visitor(Visitor), ClientData(ClientData), 247 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest) 248 { 249 Parent.kind = CXCursor_NoDeclFound; 250 Parent.data[0] = 0; 251 Parent.data[1] = 0; 252 Parent.data[2] = 0; 253 StmtParent = 0; 254 } 255 256 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); 257 258 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 259 getPreprocessedEntities(); 260 261 bool VisitChildren(CXCursor Parent); 262 263 // Declaration visitors 264 bool VisitAttributes(Decl *D); 265 bool VisitBlockDecl(BlockDecl *B); 266 bool VisitDeclContext(DeclContext *DC); 267 bool VisitTranslationUnitDecl(TranslationUnitDecl *D); 268 bool VisitTypedefDecl(TypedefDecl *D); 269 bool VisitTagDecl(TagDecl *D); 270 bool VisitEnumConstantDecl(EnumConstantDecl *D); 271 bool VisitDeclaratorDecl(DeclaratorDecl *DD); 272 bool VisitFunctionDecl(FunctionDecl *ND); 273 bool VisitFieldDecl(FieldDecl *D); 274 bool VisitVarDecl(VarDecl *); 275 bool VisitObjCMethodDecl(ObjCMethodDecl *ND); 276 bool VisitObjCContainerDecl(ObjCContainerDecl *D); 277 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); 278 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); 279 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 280 bool VisitObjCImplDecl(ObjCImplDecl *D); 281 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 282 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); 283 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations, 284 // etc. 285 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. 286 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); 287 bool VisitObjCClassDecl(ObjCClassDecl *D); 288 bool VisitNamespaceDecl(NamespaceDecl *D); 289 290 // Type visitors 291 // FIXME: QualifiedTypeLoc doesn't provide any location information 292 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL); 293 bool VisitTypedefTypeLoc(TypedefTypeLoc TL); 294 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); 295 bool VisitTagTypeLoc(TagTypeLoc TL); 296 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information 297 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); 298 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); 299 bool VisitPointerTypeLoc(PointerTypeLoc TL); 300 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL); 301 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL); 302 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL); 303 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL); 304 bool VisitFunctionTypeLoc(FunctionTypeLoc TL); 305 bool VisitArrayTypeLoc(ArrayTypeLoc TL); 306 // FIXME: Implement for TemplateSpecializationTypeLoc 307 // FIXME: Implement visitors here when the unimplemented TypeLocs get 308 // implemented 309 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL); 310 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL); 311 312 // Statement visitors 313 bool VisitStmt(Stmt *S); 314 bool VisitDeclStmt(DeclStmt *S); 315 // FIXME: LabelStmt label? 316 bool VisitIfStmt(IfStmt *S); 317 bool VisitSwitchStmt(SwitchStmt *S); 318 bool VisitWhileStmt(WhileStmt *S); 319 bool VisitForStmt(ForStmt *S); 320 321 // Expression visitors 322 bool VisitBlockExpr(BlockExpr *B); 323 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 324 bool VisitExplicitCastExpr(ExplicitCastExpr *E); 325 bool VisitObjCMessageExpr(ObjCMessageExpr *E); 326 bool VisitObjCEncodeExpr(ObjCEncodeExpr *E); 327 bool VisitOffsetOfExpr(OffsetOfExpr *E); 328 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); 329}; 330 331} // end anonymous namespace 332 333RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { 334 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest); 335} 336 337/// \brief Visit the given cursor and, if requested by the visitor, 338/// its children. 339/// 340/// \param Cursor the cursor to visit. 341/// 342/// \param CheckRegionOfInterest if true, then the caller already checked that 343/// this cursor is within the region of interest. 344/// 345/// \returns true if the visitation should be aborted, false if it 346/// should continue. 347bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { 348 if (clang_isInvalid(Cursor.kind)) 349 return false; 350 351 if (clang_isDeclaration(Cursor.kind)) { 352 Decl *D = getCursorDecl(Cursor); 353 assert(D && "Invalid declaration cursor"); 354 if (D->getPCHLevel() > MaxPCHLevel) 355 return false; 356 357 if (D->isImplicit()) 358 return false; 359 } 360 361 // If we have a range of interest, and this cursor doesn't intersect with it, 362 // we're done. 363 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { 364 SourceRange Range = 365 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor)); 366 if (Range.isInvalid() || CompareRegionOfInterest(Range)) 367 return false; 368 } 369 370 switch (Visitor(Cursor, Parent, ClientData)) { 371 case CXChildVisit_Break: 372 return true; 373 374 case CXChildVisit_Continue: 375 return false; 376 377 case CXChildVisit_Recurse: 378 return VisitChildren(Cursor); 379 } 380 381 return false; 382} 383 384std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 385CursorVisitor::getPreprocessedEntities() { 386 PreprocessingRecord &PPRec 387 = *TU->getPreprocessor().getPreprocessingRecord(); 388 389 bool OnlyLocalDecls 390 = !TU->isMainFileAST() && TU->getOnlyLocalDecls(); 391 392 // There is no region of interest; we have to walk everything. 393 if (RegionOfInterest.isInvalid()) 394 return std::make_pair(PPRec.begin(OnlyLocalDecls), 395 PPRec.end(OnlyLocalDecls)); 396 397 // Find the file in which the region of interest lands. 398 SourceManager &SM = TU->getSourceManager(); 399 std::pair<FileID, unsigned> Begin 400 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin()); 401 std::pair<FileID, unsigned> End 402 = SM.getDecomposedInstantiationLoc(RegionOfInterest.getEnd()); 403 404 // The region of interest spans files; we have to walk everything. 405 if (Begin.first != End.first) 406 return std::make_pair(PPRec.begin(OnlyLocalDecls), 407 PPRec.end(OnlyLocalDecls)); 408 409 ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap 410 = TU->getPreprocessedEntitiesByFile(); 411 if (ByFileMap.empty()) { 412 // Build the mapping from files to sets of preprocessed entities. 413 for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls), 414 EEnd = PPRec.end(OnlyLocalDecls); 415 E != EEnd; ++E) { 416 std::pair<FileID, unsigned> P 417 = SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin()); 418 ByFileMap[P.first].push_back(*E); 419 } 420 } 421 422 return std::make_pair(ByFileMap[Begin.first].begin(), 423 ByFileMap[Begin.first].end()); 424} 425 426/// \brief Visit the children of the given cursor. 427/// 428/// \returns true if the visitation should be aborted, false if it 429/// should continue. 430bool CursorVisitor::VisitChildren(CXCursor Cursor) { 431 if (clang_isReference(Cursor.kind)) { 432 // By definition, references have no children. 433 return false; 434 } 435 436 // Set the Parent field to Cursor, then back to its old value once we're 437 // done. 438 class SetParentRAII { 439 CXCursor &Parent; 440 Decl *&StmtParent; 441 CXCursor OldParent; 442 443 public: 444 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) 445 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) 446 { 447 Parent = NewParent; 448 if (clang_isDeclaration(Parent.kind)) 449 StmtParent = getCursorDecl(Parent); 450 } 451 452 ~SetParentRAII() { 453 Parent = OldParent; 454 if (clang_isDeclaration(Parent.kind)) 455 StmtParent = getCursorDecl(Parent); 456 } 457 } SetParent(Parent, StmtParent, Cursor); 458 459 if (clang_isDeclaration(Cursor.kind)) { 460 Decl *D = getCursorDecl(Cursor); 461 assert(D && "Invalid declaration cursor"); 462 return VisitAttributes(D) || Visit(D); 463 } 464 465 if (clang_isStatement(Cursor.kind)) 466 return Visit(getCursorStmt(Cursor)); 467 if (clang_isExpression(Cursor.kind)) 468 return Visit(getCursorExpr(Cursor)); 469 470 if (clang_isTranslationUnit(Cursor.kind)) { 471 ASTUnit *CXXUnit = getCursorASTUnit(Cursor); 472 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && 473 RegionOfInterest.isInvalid()) { 474 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); 475 for (std::vector<Decl*>::const_iterator it = TLDs.begin(), 476 ie = TLDs.end(); it != ie; ++it) { 477 if (Visit(MakeCXCursor(*it, CXXUnit), true)) 478 return true; 479 } 480 } else if (VisitDeclContext( 481 CXXUnit->getASTContext().getTranslationUnitDecl())) 482 return true; 483 484 // Walk the preprocessing record. 485 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { 486 // FIXME: Once we have the ability to deserialize a preprocessing record, 487 // do so. 488 PreprocessingRecord::iterator E, EEnd; 489 for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) { 490 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { 491 if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit))) 492 return true; 493 494 continue; 495 } 496 497 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { 498 if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit))) 499 return true; 500 501 continue; 502 } 503 } 504 } 505 return false; 506 } 507 508 // Nothing to visit at the moment. 509 return false; 510} 511 512bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { 513 for (BlockDecl::param_iterator I=B->param_begin(), E=B->param_end(); I!=E;++I) 514 if (Decl *D = *I) 515 if (Visit(D)) 516 return true; 517 518 return Visit(MakeCXCursor(B->getBody(), StmtParent, TU)); 519} 520 521bool CursorVisitor::VisitDeclContext(DeclContext *DC) { 522 for (DeclContext::decl_iterator 523 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { 524 525 CXCursor Cursor = MakeCXCursor(*I, TU); 526 527 if (RegionOfInterest.isValid()) { 528 SourceRange Range = 529 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor)); 530 if (Range.isInvalid()) 531 continue; 532 533 switch (CompareRegionOfInterest(Range)) { 534 case RangeBefore: 535 // This declaration comes before the region of interest; skip it. 536 continue; 537 538 case RangeAfter: 539 // This declaration comes after the region of interest; we're done. 540 return false; 541 542 case RangeOverlap: 543 // This declaration overlaps the region of interest; visit it. 544 break; 545 } 546 } 547 548 if (Visit(Cursor, true)) 549 return true; 550 } 551 552 return false; 553} 554 555bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 556 llvm_unreachable("Translation units are visited directly by Visit()"); 557 return false; 558} 559 560bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { 561 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) 562 return Visit(TSInfo->getTypeLoc()); 563 564 return false; 565} 566 567bool CursorVisitor::VisitTagDecl(TagDecl *D) { 568 return VisitDeclContext(D); 569} 570 571bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { 572 if (Expr *Init = D->getInitExpr()) 573 return Visit(MakeCXCursor(Init, StmtParent, TU)); 574 return false; 575} 576 577bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { 578 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) 579 if (Visit(TSInfo->getTypeLoc())) 580 return true; 581 582 return false; 583} 584 585bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { 586 if (VisitDeclaratorDecl(ND)) 587 return true; 588 589 if (ND->isThisDeclarationADefinition() && 590 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) 591 return true; 592 593 return false; 594} 595 596bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { 597 if (VisitDeclaratorDecl(D)) 598 return true; 599 600 if (Expr *BitWidth = D->getBitWidth()) 601 return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); 602 603 return false; 604} 605 606bool CursorVisitor::VisitVarDecl(VarDecl *D) { 607 if (VisitDeclaratorDecl(D)) 608 return true; 609 610 if (Expr *Init = D->getInit()) 611 return Visit(MakeCXCursor(Init, StmtParent, TU)); 612 613 return false; 614} 615 616bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { 617 if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) 618 if (Visit(TSInfo->getTypeLoc())) 619 return true; 620 621 for (ObjCMethodDecl::param_iterator P = ND->param_begin(), 622 PEnd = ND->param_end(); 623 P != PEnd; ++P) { 624 if (Visit(MakeCXCursor(*P, TU))) 625 return true; 626 } 627 628 if (ND->isThisDeclarationADefinition() && 629 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) 630 return true; 631 632 return false; 633} 634 635bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { 636 return VisitDeclContext(D); 637} 638 639bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { 640 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), 641 TU))) 642 return true; 643 644 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); 645 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), 646 E = ND->protocol_end(); I != E; ++I, ++PL) 647 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 648 return true; 649 650 return VisitObjCContainerDecl(ND); 651} 652 653bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 654 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); 655 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), 656 E = PID->protocol_end(); I != E; ++I, ++PL) 657 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 658 return true; 659 660 return VisitObjCContainerDecl(PID); 661} 662 663bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { 664 // Issue callbacks for super class. 665 if (D->getSuperClass() && 666 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 667 D->getSuperClassLoc(), 668 TU))) 669 return true; 670 671 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); 672 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), 673 E = D->protocol_end(); I != E; ++I, ++PL) 674 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 675 return true; 676 677 return VisitObjCContainerDecl(D); 678} 679 680bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { 681 return VisitObjCContainerDecl(D); 682} 683 684bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { 685 // 'ID' could be null when dealing with invalid code. 686 if (ObjCInterfaceDecl *ID = D->getClassInterface()) 687 if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) 688 return true; 689 690 return VisitObjCImplDecl(D); 691} 692 693bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { 694#if 0 695 // Issue callbacks for super class. 696 // FIXME: No source location information! 697 if (D->getSuperClass() && 698 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), 699 D->getSuperClassLoc(), 700 TU))) 701 return true; 702#endif 703 704 return VisitObjCImplDecl(D); 705} 706 707bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { 708 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); 709 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), 710 E = D->protocol_end(); 711 I != E; ++I, ++PL) 712 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) 713 return true; 714 715 return false; 716} 717 718bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { 719 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C) 720 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU))) 721 return true; 722 723 return false; 724} 725 726bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { 727 return VisitDeclContext(D); 728} 729 730bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 731 ASTContext &Context = TU->getASTContext(); 732 733 // Some builtin types (such as Objective-C's "id", "sel", and 734 // "Class") have associated declarations. Create cursors for those. 735 QualType VisitType; 736 switch (TL.getType()->getAs<BuiltinType>()->getKind()) { 737 case BuiltinType::Void: 738 case BuiltinType::Bool: 739 case BuiltinType::Char_U: 740 case BuiltinType::UChar: 741 case BuiltinType::Char16: 742 case BuiltinType::Char32: 743 case BuiltinType::UShort: 744 case BuiltinType::UInt: 745 case BuiltinType::ULong: 746 case BuiltinType::ULongLong: 747 case BuiltinType::UInt128: 748 case BuiltinType::Char_S: 749 case BuiltinType::SChar: 750 case BuiltinType::WChar: 751 case BuiltinType::Short: 752 case BuiltinType::Int: 753 case BuiltinType::Long: 754 case BuiltinType::LongLong: 755 case BuiltinType::Int128: 756 case BuiltinType::Float: 757 case BuiltinType::Double: 758 case BuiltinType::LongDouble: 759 case BuiltinType::NullPtr: 760 case BuiltinType::Overload: 761 case BuiltinType::Dependent: 762 break; 763 764 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor? 765 break; 766 767 case BuiltinType::ObjCId: 768 VisitType = Context.getObjCIdType(); 769 break; 770 771 case BuiltinType::ObjCClass: 772 VisitType = Context.getObjCClassType(); 773 break; 774 775 case BuiltinType::ObjCSel: 776 VisitType = Context.getObjCSelType(); 777 break; 778 } 779 780 if (!VisitType.isNull()) { 781 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) 782 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), 783 TU)); 784 } 785 786 return false; 787} 788 789bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 790 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU)); 791} 792 793bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 794 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 795} 796 797bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { 798 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); 799} 800 801bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 802 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) 803 return true; 804 805 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { 806 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), 807 TU))) 808 return true; 809 } 810 811 return false; 812} 813 814bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 815 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc())) 816 return true; 817 818 if (TL.hasProtocolsAsWritten()) { 819 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { 820 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), 821 TL.getProtocolLoc(I), 822 TU))) 823 return true; 824 } 825 } 826 827 return false; 828} 829 830bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { 831 return Visit(TL.getPointeeLoc()); 832} 833 834bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 835 return Visit(TL.getPointeeLoc()); 836} 837 838bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 839 return Visit(TL.getPointeeLoc()); 840} 841 842bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 843 return Visit(TL.getPointeeLoc()); 844} 845 846bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 847 return Visit(TL.getPointeeLoc()); 848} 849 850bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 851 if (Visit(TL.getResultLoc())) 852 return true; 853 854 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) 855 if (Decl *D = TL.getArg(I)) 856 if (Visit(MakeCXCursor(D, TU))) 857 return true; 858 859 return false; 860} 861 862bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { 863 if (Visit(TL.getElementLoc())) 864 return true; 865 866 if (Expr *Size = TL.getSizeExpr()) 867 return Visit(MakeCXCursor(Size, StmtParent, TU)); 868 869 return false; 870} 871 872bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 873 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); 874} 875 876bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 877 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) 878 return Visit(TSInfo->getTypeLoc()); 879 880 return false; 881} 882 883bool CursorVisitor::VisitStmt(Stmt *S) { 884 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end(); 885 Child != ChildEnd; ++Child) { 886 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU))) 887 return true; 888 } 889 890 return false; 891} 892 893bool CursorVisitor::VisitDeclStmt(DeclStmt *S) { 894 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 895 D != DEnd; ++D) { 896 if (*D && Visit(MakeCXCursor(*D, TU))) 897 return true; 898 } 899 900 return false; 901} 902 903bool CursorVisitor::VisitIfStmt(IfStmt *S) { 904 if (VarDecl *Var = S->getConditionVariable()) { 905 if (Visit(MakeCXCursor(Var, TU))) 906 return true; 907 } 908 909 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) 910 return true; 911 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU))) 912 return true; 913 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU))) 914 return true; 915 916 return false; 917} 918 919bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) { 920 if (VarDecl *Var = S->getConditionVariable()) { 921 if (Visit(MakeCXCursor(Var, TU))) 922 return true; 923 } 924 925 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) 926 return true; 927 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) 928 return true; 929 930 return false; 931} 932 933bool CursorVisitor::VisitWhileStmt(WhileStmt *S) { 934 if (VarDecl *Var = S->getConditionVariable()) { 935 if (Visit(MakeCXCursor(Var, TU))) 936 return true; 937 } 938 939 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) 940 return true; 941 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) 942 return true; 943 944 return false; 945} 946 947bool CursorVisitor::VisitForStmt(ForStmt *S) { 948 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU))) 949 return true; 950 if (VarDecl *Var = S->getConditionVariable()) { 951 if (Visit(MakeCXCursor(Var, TU))) 952 return true; 953 } 954 955 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) 956 return true; 957 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU))) 958 return true; 959 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) 960 return true; 961 962 return false; 963} 964 965bool CursorVisitor::VisitBlockExpr(BlockExpr *B) { 966 return Visit(B->getBlockDecl()); 967} 968 969bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { 970 // FIXME: Visit fields as well? 971 if (Visit(E->getTypeSourceInfo()->getTypeLoc())) 972 return true; 973 974 return VisitExpr(E); 975} 976 977bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { 978 if (E->isArgumentType()) { 979 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo()) 980 return Visit(TSInfo->getTypeLoc()); 981 982 return false; 983 } 984 985 return VisitExpr(E); 986} 987 988bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { 989 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten()) 990 if (Visit(TSInfo->getTypeLoc())) 991 return true; 992 993 return VisitCastExpr(E); 994} 995 996bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 997 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo()) 998 if (Visit(TSInfo->getTypeLoc())) 999 return true; 1000 1001 return VisitExpr(E); 1002} 1003 1004bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) { 1005 if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo()) 1006 if (Visit(TSInfo->getTypeLoc())) 1007 return true; 1008 1009 return VisitExpr(E); 1010} 1011 1012bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 1013 return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc()); 1014} 1015 1016 1017bool CursorVisitor::VisitAttributes(Decl *D) { 1018 for (const Attr *A = D->getAttrs(); A; A = A->getNext()) 1019 if (Visit(MakeCXCursor(A, D, TU))) 1020 return true; 1021 1022 return false; 1023} 1024 1025extern "C" { 1026CXIndex clang_createIndex(int excludeDeclarationsFromPCH, 1027 int displayDiagnostics) { 1028 CIndexer *CIdxr = new CIndexer(); 1029 if (excludeDeclarationsFromPCH) 1030 CIdxr->setOnlyLocalDecls(); 1031 if (displayDiagnostics) 1032 CIdxr->setDisplayDiagnostics(); 1033 return CIdxr; 1034} 1035 1036void clang_disposeIndex(CXIndex CIdx) { 1037 if (CIdx) 1038 delete static_cast<CIndexer *>(CIdx); 1039} 1040 1041void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { 1042 if (CIdx) { 1043 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 1044 CXXIdx->setUseExternalASTGeneration(value); 1045 } 1046} 1047 1048CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, 1049 const char *ast_filename) { 1050 if (!CIdx) 1051 return 0; 1052 1053 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 1054 1055 llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 1056 return ASTUnit::LoadFromPCHFile(ast_filename, Diags, 1057 CXXIdx->getOnlyLocalDecls(), 1058 0, 0, true); 1059} 1060 1061CXTranslationUnit 1062clang_createTranslationUnitFromSourceFile(CXIndex CIdx, 1063 const char *source_filename, 1064 int num_command_line_args, 1065 const char **command_line_args, 1066 unsigned num_unsaved_files, 1067 struct CXUnsavedFile *unsaved_files) { 1068 if (!CIdx) 1069 return 0; 1070 1071 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 1072 1073 // Configure the diagnostics. 1074 DiagnosticOptions DiagOpts; 1075 llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 1076 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 1077 1078 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 1079 for (unsigned I = 0; I != num_unsaved_files; ++I) { 1080 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 1081 const llvm::MemoryBuffer *Buffer 1082 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 1083 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 1084 Buffer)); 1085 } 1086 1087 if (!CXXIdx->getUseExternalASTGeneration()) { 1088 llvm::SmallVector<const char *, 16> Args; 1089 1090 // The 'source_filename' argument is optional. If the caller does not 1091 // specify it then it is assumed that the source file is specified 1092 // in the actual argument list. 1093 if (source_filename) 1094 Args.push_back(source_filename); 1095 Args.insert(Args.end(), command_line_args, 1096 command_line_args + num_command_line_args); 1097 Args.push_back("-Xclang"); 1098 Args.push_back("-detailed-preprocessing-record"); 1099 unsigned NumErrors = Diags->getNumErrors(); 1100 1101#ifdef USE_CRASHTRACER 1102 ArgsCrashTracerInfo ACTI(Args); 1103#endif 1104 1105 llvm::OwningPtr<ASTUnit> Unit( 1106 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), 1107 Diags, 1108 CXXIdx->getClangResourcesPath(), 1109 CXXIdx->getOnlyLocalDecls(), 1110 RemappedFiles.data(), 1111 RemappedFiles.size(), 1112 /*CaptureDiagnostics=*/true)); 1113 1114 // FIXME: Until we have broader testing, just drop the entire AST if we 1115 // encountered an error. 1116 if (NumErrors != Diags->getNumErrors()) { 1117 // Make sure to check that 'Unit' is non-NULL. 1118 if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { 1119 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), 1120 DEnd = Unit->stored_diag_end(); 1121 D != DEnd; ++D) { 1122 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); 1123 CXString Msg = clang_formatDiagnostic(&Diag, 1124 clang_defaultDiagnosticDisplayOptions()); 1125 fprintf(stderr, "%s\n", clang_getCString(Msg)); 1126 clang_disposeString(Msg); 1127 } 1128#ifdef LLVM_ON_WIN32 1129 // On Windows, force a flush, since there may be multiple copies of 1130 // stderr and stdout in the file system, all with different buffers 1131 // but writing to the same device. 1132 fflush(stderr); 1133#endif 1134 } 1135 } 1136 1137 return Unit.take(); 1138 } 1139 1140 // Build up the arguments for invoking 'clang'. 1141 std::vector<const char *> argv; 1142 1143 // First add the complete path to the 'clang' executable. 1144 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); 1145 argv.push_back(ClangPath.c_str()); 1146 1147 // Add the '-emit-ast' option as our execution mode for 'clang'. 1148 argv.push_back("-emit-ast"); 1149 1150 // The 'source_filename' argument is optional. If the caller does not 1151 // specify it then it is assumed that the source file is specified 1152 // in the actual argument list. 1153 if (source_filename) 1154 argv.push_back(source_filename); 1155 1156 // Generate a temporary name for the AST file. 1157 argv.push_back("-o"); 1158 char astTmpFile[L_tmpnam]; 1159 argv.push_back(tmpnam(astTmpFile)); 1160 1161 // Remap any unsaved files to temporary files. 1162 std::vector<llvm::sys::Path> TemporaryFiles; 1163 std::vector<std::string> RemapArgs; 1164 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 1165 return 0; 1166 1167 // The pointers into the elements of RemapArgs are stable because we 1168 // won't be adding anything to RemapArgs after this point. 1169 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 1170 argv.push_back(RemapArgs[i].c_str()); 1171 1172 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 1173 for (int i = 0; i < num_command_line_args; ++i) 1174 if (const char *arg = command_line_args[i]) { 1175 if (strcmp(arg, "-o") == 0) { 1176 ++i; // Also skip the matching argument. 1177 continue; 1178 } 1179 if (strcmp(arg, "-emit-ast") == 0 || 1180 strcmp(arg, "-c") == 0 || 1181 strcmp(arg, "-fsyntax-only") == 0) { 1182 continue; 1183 } 1184 1185 // Keep the argument. 1186 argv.push_back(arg); 1187 } 1188 1189 // Generate a temporary name for the diagnostics file. 1190 char tmpFileResults[L_tmpnam]; 1191 char *tmpResultsFileName = tmpnam(tmpFileResults); 1192 llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 1193 TemporaryFiles.push_back(DiagnosticsFile); 1194 argv.push_back("-fdiagnostics-binary"); 1195 1196 argv.push_back("-Xclang"); 1197 argv.push_back("-detailed-preprocessing-record"); 1198 1199 // Add the null terminator. 1200 argv.push_back(NULL); 1201 1202 // Invoke 'clang'. 1203 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 1204 // on Unix or NUL (Windows). 1205 std::string ErrMsg; 1206 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile, 1207 NULL }; 1208 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 1209 /* redirects */ &Redirects[0], 1210 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); 1211 1212 if (!ErrMsg.empty()) { 1213 std::string AllArgs; 1214 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 1215 I != E; ++I) { 1216 AllArgs += ' '; 1217 if (*I) 1218 AllArgs += *I; 1219 } 1220 1221 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 1222 } 1223 1224 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, Diags, 1225 CXXIdx->getOnlyLocalDecls(), 1226 RemappedFiles.data(), 1227 RemappedFiles.size(), 1228 /*CaptureDiagnostics=*/true); 1229 if (ATU) { 1230 LoadSerializedDiagnostics(DiagnosticsFile, 1231 num_unsaved_files, unsaved_files, 1232 ATU->getFileManager(), 1233 ATU->getSourceManager(), 1234 ATU->getStoredDiagnostics()); 1235 } else if (CXXIdx->getDisplayDiagnostics()) { 1236 // We failed to load the ASTUnit, but we can still deserialize the 1237 // diagnostics and emit them. 1238 FileManager FileMgr; 1239 Diagnostic Diag; 1240 SourceManager SourceMgr(Diag); 1241 // FIXME: Faked LangOpts! 1242 LangOptions LangOpts; 1243 llvm::SmallVector<StoredDiagnostic, 4> Diags; 1244 LoadSerializedDiagnostics(DiagnosticsFile, 1245 num_unsaved_files, unsaved_files, 1246 FileMgr, SourceMgr, Diags); 1247 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(), 1248 DEnd = Diags.end(); 1249 D != DEnd; ++D) { 1250 CXStoredDiagnostic Diag(*D, LangOpts); 1251 CXString Msg = clang_formatDiagnostic(&Diag, 1252 clang_defaultDiagnosticDisplayOptions()); 1253 fprintf(stderr, "%s\n", clang_getCString(Msg)); 1254 clang_disposeString(Msg); 1255 } 1256 1257#ifdef LLVM_ON_WIN32 1258 // On Windows, force a flush, since there may be multiple copies of 1259 // stderr and stdout in the file system, all with different buffers 1260 // but writing to the same device. 1261 fflush(stderr); 1262#endif 1263 } 1264 1265 if (ATU) { 1266 // Make the translation unit responsible for destroying all temporary files. 1267 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) 1268 ATU->addTemporaryFile(TemporaryFiles[i]); 1269 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName())); 1270 } else { 1271 // Destroy all of the temporary files now; they can't be referenced any 1272 // longer. 1273 llvm::sys::Path(astTmpFile).eraseFromDisk(); 1274 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) 1275 TemporaryFiles[i].eraseFromDisk(); 1276 } 1277 1278 return ATU; 1279} 1280 1281void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { 1282 if (CTUnit) 1283 delete static_cast<ASTUnit *>(CTUnit); 1284} 1285 1286CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { 1287 if (!CTUnit) 1288 return createCXString(""); 1289 1290 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); 1291 return createCXString(CXXUnit->getOriginalSourceFileName(), true); 1292} 1293 1294CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { 1295 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; 1296 return Result; 1297} 1298 1299} // end: extern "C" 1300 1301//===----------------------------------------------------------------------===// 1302// CXSourceLocation and CXSourceRange Operations. 1303//===----------------------------------------------------------------------===// 1304 1305extern "C" { 1306CXSourceLocation clang_getNullLocation() { 1307 CXSourceLocation Result = { { 0, 0 }, 0 }; 1308 return Result; 1309} 1310 1311unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { 1312 return (loc1.ptr_data[0] == loc2.ptr_data[0] && 1313 loc1.ptr_data[1] == loc2.ptr_data[1] && 1314 loc1.int_data == loc2.int_data); 1315} 1316 1317CXSourceLocation clang_getLocation(CXTranslationUnit tu, 1318 CXFile file, 1319 unsigned line, 1320 unsigned column) { 1321 if (!tu || !file) 1322 return clang_getNullLocation(); 1323 1324 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); 1325 SourceLocation SLoc 1326 = CXXUnit->getSourceManager().getLocation( 1327 static_cast<const FileEntry *>(file), 1328 line, column); 1329 1330 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); 1331} 1332 1333CXSourceRange clang_getNullRange() { 1334 CXSourceRange Result = { { 0, 0 }, 0, 0 }; 1335 return Result; 1336} 1337 1338CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { 1339 if (begin.ptr_data[0] != end.ptr_data[0] || 1340 begin.ptr_data[1] != end.ptr_data[1]) 1341 return clang_getNullRange(); 1342 1343 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, 1344 begin.int_data, end.int_data }; 1345 return Result; 1346} 1347 1348void clang_getInstantiationLocation(CXSourceLocation location, 1349 CXFile *file, 1350 unsigned *line, 1351 unsigned *column, 1352 unsigned *offset) { 1353 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 1354 1355 if (!location.ptr_data[0] || Loc.isInvalid()) { 1356 if (file) 1357 *file = 0; 1358 if (line) 1359 *line = 0; 1360 if (column) 1361 *column = 0; 1362 if (offset) 1363 *offset = 0; 1364 return; 1365 } 1366 1367 const SourceManager &SM = 1368 *static_cast<const SourceManager*>(location.ptr_data[0]); 1369 SourceLocation InstLoc = SM.getInstantiationLoc(Loc); 1370 1371 if (file) 1372 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); 1373 if (line) 1374 *line = SM.getInstantiationLineNumber(InstLoc); 1375 if (column) 1376 *column = SM.getInstantiationColumnNumber(InstLoc); 1377 if (offset) 1378 *offset = SM.getDecomposedLoc(InstLoc).second; 1379} 1380 1381CXSourceLocation clang_getRangeStart(CXSourceRange range) { 1382 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 1383 range.begin_int_data }; 1384 return Result; 1385} 1386 1387CXSourceLocation clang_getRangeEnd(CXSourceRange range) { 1388 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 1389 range.end_int_data }; 1390 return Result; 1391} 1392 1393} // end: extern "C" 1394 1395//===----------------------------------------------------------------------===// 1396// CXFile Operations. 1397//===----------------------------------------------------------------------===// 1398 1399extern "C" { 1400CXString clang_getFileName(CXFile SFile) { 1401 if (!SFile) 1402 return createCXString(NULL); 1403 1404 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 1405 return createCXString(FEnt->getName()); 1406} 1407 1408time_t clang_getFileTime(CXFile SFile) { 1409 if (!SFile) 1410 return 0; 1411 1412 FileEntry *FEnt = static_cast<FileEntry *>(SFile); 1413 return FEnt->getModificationTime(); 1414} 1415 1416CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { 1417 if (!tu) 1418 return 0; 1419 1420 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); 1421 1422 FileManager &FMgr = CXXUnit->getFileManager(); 1423 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name)); 1424 return const_cast<FileEntry *>(File); 1425} 1426 1427} // end: extern "C" 1428 1429//===----------------------------------------------------------------------===// 1430// CXCursor Operations. 1431//===----------------------------------------------------------------------===// 1432 1433static Decl *getDeclFromExpr(Stmt *E) { 1434 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) 1435 return RefExpr->getDecl(); 1436 if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) 1437 return ME->getMemberDecl(); 1438 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) 1439 return RE->getDecl(); 1440 1441 if (CallExpr *CE = dyn_cast<CallExpr>(E)) 1442 return getDeclFromExpr(CE->getCallee()); 1443 if (CastExpr *CE = dyn_cast<CastExpr>(E)) 1444 return getDeclFromExpr(CE->getSubExpr()); 1445 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) 1446 return OME->getMethodDecl(); 1447 1448 return 0; 1449} 1450 1451static SourceLocation getLocationFromExpr(Expr *E) { 1452 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) 1453 return /*FIXME:*/Msg->getLeftLoc(); 1454 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 1455 return DRE->getLocation(); 1456 if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) 1457 return Member->getMemberLoc(); 1458 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) 1459 return Ivar->getLocation(); 1460 return E->getLocStart(); 1461} 1462 1463extern "C" { 1464 1465unsigned clang_visitChildren(CXCursor parent, 1466 CXCursorVisitor visitor, 1467 CXClientData client_data) { 1468 ASTUnit *CXXUnit = getCursorASTUnit(parent); 1469 1470 unsigned PCHLevel = Decl::MaxPCHLevel; 1471 1472 // Set the PCHLevel to filter out unwanted decls if requested. 1473 if (CXXUnit->getOnlyLocalDecls()) { 1474 PCHLevel = 0; 1475 1476 // If the main input was an AST, bump the level. 1477 if (CXXUnit->isMainFileAST()) 1478 ++PCHLevel; 1479 } 1480 1481 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel); 1482 return CursorVis.VisitChildren(parent); 1483} 1484 1485static CXString getDeclSpelling(Decl *D) { 1486 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); 1487 if (!ND) 1488 return createCXString(""); 1489 1490 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) 1491 return createCXString(OMD->getSelector().getAsString()); 1492 1493 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) 1494 // No, this isn't the same as the code below. getIdentifier() is non-virtual 1495 // and returns different names. NamedDecl returns the class name and 1496 // ObjCCategoryImplDecl returns the category name. 1497 return createCXString(CIMP->getIdentifier()->getNameStart()); 1498 1499 if (ND->getIdentifier()) 1500 return createCXString(ND->getIdentifier()->getNameStart()); 1501 1502 return createCXString(""); 1503} 1504 1505CXString clang_getCursorSpelling(CXCursor C) { 1506 if (clang_isTranslationUnit(C.kind)) 1507 return clang_getTranslationUnitSpelling(C.data[2]); 1508 1509 if (clang_isReference(C.kind)) { 1510 switch (C.kind) { 1511 case CXCursor_ObjCSuperClassRef: { 1512 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; 1513 return createCXString(Super->getIdentifier()->getNameStart()); 1514 } 1515 case CXCursor_ObjCClassRef: { 1516 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; 1517 return createCXString(Class->getIdentifier()->getNameStart()); 1518 } 1519 case CXCursor_ObjCProtocolRef: { 1520 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; 1521 assert(OID && "getCursorSpelling(): Missing protocol decl"); 1522 return createCXString(OID->getIdentifier()->getNameStart()); 1523 } 1524 case CXCursor_TypeRef: { 1525 TypeDecl *Type = getCursorTypeRef(C).first; 1526 assert(Type && "Missing type decl"); 1527 1528 return createCXString(getCursorContext(C).getTypeDeclType(Type). 1529 getAsString()); 1530 } 1531 1532 default: 1533 return createCXString("<not implemented>"); 1534 } 1535 } 1536 1537 if (clang_isExpression(C.kind)) { 1538 Decl *D = getDeclFromExpr(getCursorExpr(C)); 1539 if (D) 1540 return getDeclSpelling(D); 1541 return createCXString(""); 1542 } 1543 1544 if (C.kind == CXCursor_MacroInstantiation) 1545 return createCXString(getCursorMacroInstantiation(C)->getName() 1546 ->getNameStart()); 1547 1548 if (C.kind == CXCursor_MacroDefinition) 1549 return createCXString(getCursorMacroDefinition(C)->getName() 1550 ->getNameStart()); 1551 1552 if (clang_isDeclaration(C.kind)) 1553 return getDeclSpelling(getCursorDecl(C)); 1554 1555 return createCXString(""); 1556} 1557 1558CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { 1559 switch (Kind) { 1560 case CXCursor_FunctionDecl: 1561 return createCXString("FunctionDecl"); 1562 case CXCursor_TypedefDecl: 1563 return createCXString("TypedefDecl"); 1564 case CXCursor_EnumDecl: 1565 return createCXString("EnumDecl"); 1566 case CXCursor_EnumConstantDecl: 1567 return createCXString("EnumConstantDecl"); 1568 case CXCursor_StructDecl: 1569 return createCXString("StructDecl"); 1570 case CXCursor_UnionDecl: 1571 return createCXString("UnionDecl"); 1572 case CXCursor_ClassDecl: 1573 return createCXString("ClassDecl"); 1574 case CXCursor_FieldDecl: 1575 return createCXString("FieldDecl"); 1576 case CXCursor_VarDecl: 1577 return createCXString("VarDecl"); 1578 case CXCursor_ParmDecl: 1579 return createCXString("ParmDecl"); 1580 case CXCursor_ObjCInterfaceDecl: 1581 return createCXString("ObjCInterfaceDecl"); 1582 case CXCursor_ObjCCategoryDecl: 1583 return createCXString("ObjCCategoryDecl"); 1584 case CXCursor_ObjCProtocolDecl: 1585 return createCXString("ObjCProtocolDecl"); 1586 case CXCursor_ObjCPropertyDecl: 1587 return createCXString("ObjCPropertyDecl"); 1588 case CXCursor_ObjCIvarDecl: 1589 return createCXString("ObjCIvarDecl"); 1590 case CXCursor_ObjCInstanceMethodDecl: 1591 return createCXString("ObjCInstanceMethodDecl"); 1592 case CXCursor_ObjCClassMethodDecl: 1593 return createCXString("ObjCClassMethodDecl"); 1594 case CXCursor_ObjCImplementationDecl: 1595 return createCXString("ObjCImplementationDecl"); 1596 case CXCursor_ObjCCategoryImplDecl: 1597 return createCXString("ObjCCategoryImplDecl"); 1598 case CXCursor_CXXMethod: 1599 return createCXString("CXXMethod"); 1600 case CXCursor_UnexposedDecl: 1601 return createCXString("UnexposedDecl"); 1602 case CXCursor_ObjCSuperClassRef: 1603 return createCXString("ObjCSuperClassRef"); 1604 case CXCursor_ObjCProtocolRef: 1605 return createCXString("ObjCProtocolRef"); 1606 case CXCursor_ObjCClassRef: 1607 return createCXString("ObjCClassRef"); 1608 case CXCursor_TypeRef: 1609 return createCXString("TypeRef"); 1610 case CXCursor_UnexposedExpr: 1611 return createCXString("UnexposedExpr"); 1612 case CXCursor_BlockExpr: 1613 return createCXString("BlockExpr"); 1614 case CXCursor_DeclRefExpr: 1615 return createCXString("DeclRefExpr"); 1616 case CXCursor_MemberRefExpr: 1617 return createCXString("MemberRefExpr"); 1618 case CXCursor_CallExpr: 1619 return createCXString("CallExpr"); 1620 case CXCursor_ObjCMessageExpr: 1621 return createCXString("ObjCMessageExpr"); 1622 case CXCursor_UnexposedStmt: 1623 return createCXString("UnexposedStmt"); 1624 case CXCursor_InvalidFile: 1625 return createCXString("InvalidFile"); 1626 case CXCursor_InvalidCode: 1627 return createCXString("InvalidCode"); 1628 case CXCursor_NoDeclFound: 1629 return createCXString("NoDeclFound"); 1630 case CXCursor_NotImplemented: 1631 return createCXString("NotImplemented"); 1632 case CXCursor_TranslationUnit: 1633 return createCXString("TranslationUnit"); 1634 case CXCursor_UnexposedAttr: 1635 return createCXString("UnexposedAttr"); 1636 case CXCursor_IBActionAttr: 1637 return createCXString("attribute(ibaction)"); 1638 case CXCursor_IBOutletAttr: 1639 return createCXString("attribute(iboutlet)"); 1640 case CXCursor_PreprocessingDirective: 1641 return createCXString("preprocessing directive"); 1642 case CXCursor_MacroDefinition: 1643 return createCXString("macro definition"); 1644 case CXCursor_MacroInstantiation: 1645 return createCXString("macro instantiation"); 1646 case CXCursor_Namespace: 1647 return createCXString("Namespace"); 1648 } 1649 1650 llvm_unreachable("Unhandled CXCursorKind"); 1651 return createCXString(NULL); 1652} 1653 1654enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, 1655 CXCursor parent, 1656 CXClientData client_data) { 1657 CXCursor *BestCursor = static_cast<CXCursor *>(client_data); 1658 *BestCursor = cursor; 1659 return CXChildVisit_Recurse; 1660} 1661 1662CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { 1663 if (!TU) 1664 return clang_getNullCursor(); 1665 1666 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 1667 1668 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 1669 1670 SourceLocation SLoc = cxloc::translateSourceLocation(Loc); 1671 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); 1672 if (SLoc.isValid()) { 1673 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1)); 1674 1675 // FIXME: Would be great to have a "hint" cursor, then walk from that 1676 // hint cursor upward until we find a cursor whose source range encloses 1677 // the region of interest, rather than starting from the translation unit. 1678 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit); 1679 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, 1680 Decl::MaxPCHLevel, RegionOfInterest); 1681 CursorVis.VisitChildren(Parent); 1682 } 1683 return Result; 1684} 1685 1686CXCursor clang_getNullCursor(void) { 1687 return MakeCXCursorInvalid(CXCursor_InvalidFile); 1688} 1689 1690unsigned clang_equalCursors(CXCursor X, CXCursor Y) { 1691 return X == Y; 1692} 1693 1694unsigned clang_isInvalid(enum CXCursorKind K) { 1695 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; 1696} 1697 1698unsigned clang_isDeclaration(enum CXCursorKind K) { 1699 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; 1700} 1701 1702unsigned clang_isReference(enum CXCursorKind K) { 1703 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; 1704} 1705 1706unsigned clang_isExpression(enum CXCursorKind K) { 1707 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; 1708} 1709 1710unsigned clang_isStatement(enum CXCursorKind K) { 1711 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; 1712} 1713 1714unsigned clang_isTranslationUnit(enum CXCursorKind K) { 1715 return K == CXCursor_TranslationUnit; 1716} 1717 1718unsigned clang_isPreprocessing(enum CXCursorKind K) { 1719 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; 1720} 1721 1722unsigned clang_isUnexposed(enum CXCursorKind K) { 1723 switch (K) { 1724 case CXCursor_UnexposedDecl: 1725 case CXCursor_UnexposedExpr: 1726 case CXCursor_UnexposedStmt: 1727 case CXCursor_UnexposedAttr: 1728 return true; 1729 default: 1730 return false; 1731 } 1732} 1733 1734CXCursorKind clang_getCursorKind(CXCursor C) { 1735 return C.kind; 1736} 1737 1738CXSourceLocation clang_getCursorLocation(CXCursor C) { 1739 if (clang_isReference(C.kind)) { 1740 switch (C.kind) { 1741 case CXCursor_ObjCSuperClassRef: { 1742 std::pair<ObjCInterfaceDecl *, SourceLocation> P 1743 = getCursorObjCSuperClassRef(C); 1744 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 1745 } 1746 1747 case CXCursor_ObjCProtocolRef: { 1748 std::pair<ObjCProtocolDecl *, SourceLocation> P 1749 = getCursorObjCProtocolRef(C); 1750 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 1751 } 1752 1753 case CXCursor_ObjCClassRef: { 1754 std::pair<ObjCInterfaceDecl *, SourceLocation> P 1755 = getCursorObjCClassRef(C); 1756 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 1757 } 1758 1759 case CXCursor_TypeRef: { 1760 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); 1761 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); 1762 } 1763 1764 default: 1765 // FIXME: Need a way to enumerate all non-reference cases. 1766 llvm_unreachable("Missed a reference kind"); 1767 } 1768 } 1769 1770 if (clang_isExpression(C.kind)) 1771 return cxloc::translateSourceLocation(getCursorContext(C), 1772 getLocationFromExpr(getCursorExpr(C))); 1773 1774 if (C.kind == CXCursor_PreprocessingDirective) { 1775 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); 1776 return cxloc::translateSourceLocation(getCursorContext(C), L); 1777 } 1778 1779 if (C.kind == CXCursor_MacroInstantiation) { 1780 SourceLocation L 1781 = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin(); 1782 return cxloc::translateSourceLocation(getCursorContext(C), L); 1783 } 1784 1785 if (C.kind == CXCursor_MacroDefinition) { 1786 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); 1787 return cxloc::translateSourceLocation(getCursorContext(C), L); 1788 } 1789 1790 if (!getCursorDecl(C)) 1791 return clang_getNullLocation(); 1792 1793 Decl *D = getCursorDecl(C); 1794 SourceLocation Loc = D->getLocation(); 1795 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) 1796 Loc = Class->getClassLoc(); 1797 return cxloc::translateSourceLocation(getCursorContext(C), Loc); 1798} 1799 1800CXSourceRange clang_getCursorExtent(CXCursor C) { 1801 if (clang_isReference(C.kind)) { 1802 switch (C.kind) { 1803 case CXCursor_ObjCSuperClassRef: { 1804 std::pair<ObjCInterfaceDecl *, SourceLocation> P 1805 = getCursorObjCSuperClassRef(C); 1806 return cxloc::translateSourceRange(P.first->getASTContext(), P.second); 1807 } 1808 1809 case CXCursor_ObjCProtocolRef: { 1810 std::pair<ObjCProtocolDecl *, SourceLocation> P 1811 = getCursorObjCProtocolRef(C); 1812 return cxloc::translateSourceRange(P.first->getASTContext(), P.second); 1813 } 1814 1815 case CXCursor_ObjCClassRef: { 1816 std::pair<ObjCInterfaceDecl *, SourceLocation> P 1817 = getCursorObjCClassRef(C); 1818 1819 return cxloc::translateSourceRange(P.first->getASTContext(), P.second); 1820 } 1821 1822 case CXCursor_TypeRef: { 1823 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); 1824 return cxloc::translateSourceRange(P.first->getASTContext(), P.second); 1825 } 1826 1827 default: 1828 // FIXME: Need a way to enumerate all non-reference cases. 1829 llvm_unreachable("Missed a reference kind"); 1830 } 1831 } 1832 1833 if (clang_isExpression(C.kind)) 1834 return cxloc::translateSourceRange(getCursorContext(C), 1835 getCursorExpr(C)->getSourceRange()); 1836 1837 if (clang_isStatement(C.kind)) 1838 return cxloc::translateSourceRange(getCursorContext(C), 1839 getCursorStmt(C)->getSourceRange()); 1840 1841 if (C.kind == CXCursor_PreprocessingDirective) { 1842 SourceRange R = cxcursor::getCursorPreprocessingDirective(C); 1843 return cxloc::translateSourceRange(getCursorContext(C), R); 1844 } 1845 1846 if (C.kind == CXCursor_MacroInstantiation) { 1847 SourceRange R = cxcursor::getCursorMacroInstantiation(C)->getSourceRange(); 1848 return cxloc::translateSourceRange(getCursorContext(C), R); 1849 } 1850 1851 if (C.kind == CXCursor_MacroDefinition) { 1852 SourceRange R = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); 1853 return cxloc::translateSourceRange(getCursorContext(C), R); 1854 } 1855 1856 if (!getCursorDecl(C)) 1857 return clang_getNullRange(); 1858 1859 Decl *D = getCursorDecl(C); 1860 return cxloc::translateSourceRange(getCursorContext(C), D->getSourceRange()); 1861} 1862 1863CXCursor clang_getCursorReferenced(CXCursor C) { 1864 if (clang_isInvalid(C.kind)) 1865 return clang_getNullCursor(); 1866 1867 ASTUnit *CXXUnit = getCursorASTUnit(C); 1868 if (clang_isDeclaration(C.kind)) 1869 return C; 1870 1871 if (clang_isExpression(C.kind)) { 1872 Decl *D = getDeclFromExpr(getCursorExpr(C)); 1873 if (D) 1874 return MakeCXCursor(D, CXXUnit); 1875 return clang_getNullCursor(); 1876 } 1877 1878 if (C.kind == CXCursor_MacroInstantiation) { 1879 if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition()) 1880 return MakeMacroDefinitionCursor(Def, CXXUnit); 1881 } 1882 1883 if (!clang_isReference(C.kind)) 1884 return clang_getNullCursor(); 1885 1886 switch (C.kind) { 1887 case CXCursor_ObjCSuperClassRef: 1888 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit); 1889 1890 case CXCursor_ObjCProtocolRef: { 1891 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit); 1892 1893 case CXCursor_ObjCClassRef: 1894 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit); 1895 1896 case CXCursor_TypeRef: 1897 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit); 1898 1899 default: 1900 // We would prefer to enumerate all non-reference cursor kinds here. 1901 llvm_unreachable("Unhandled reference cursor kind"); 1902 break; 1903 } 1904 } 1905 1906 return clang_getNullCursor(); 1907} 1908 1909CXCursor clang_getCursorDefinition(CXCursor C) { 1910 if (clang_isInvalid(C.kind)) 1911 return clang_getNullCursor(); 1912 1913 ASTUnit *CXXUnit = getCursorASTUnit(C); 1914 1915 bool WasReference = false; 1916 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { 1917 C = clang_getCursorReferenced(C); 1918 WasReference = true; 1919 } 1920 1921 if (C.kind == CXCursor_MacroInstantiation) 1922 return clang_getCursorReferenced(C); 1923 1924 if (!clang_isDeclaration(C.kind)) 1925 return clang_getNullCursor(); 1926 1927 Decl *D = getCursorDecl(C); 1928 if (!D) 1929 return clang_getNullCursor(); 1930 1931 switch (D->getKind()) { 1932 // Declaration kinds that don't really separate the notions of 1933 // declaration and definition. 1934 case Decl::Namespace: 1935 case Decl::Typedef: 1936 case Decl::TemplateTypeParm: 1937 case Decl::EnumConstant: 1938 case Decl::Field: 1939 case Decl::ObjCIvar: 1940 case Decl::ObjCAtDefsField: 1941 case Decl::ImplicitParam: 1942 case Decl::ParmVar: 1943 case Decl::NonTypeTemplateParm: 1944 case Decl::TemplateTemplateParm: 1945 case Decl::ObjCCategoryImpl: 1946 case Decl::ObjCImplementation: 1947 case Decl::LinkageSpec: 1948 case Decl::ObjCPropertyImpl: 1949 case Decl::FileScopeAsm: 1950 case Decl::StaticAssert: 1951 case Decl::Block: 1952 return C; 1953 1954 // Declaration kinds that don't make any sense here, but are 1955 // nonetheless harmless. 1956 case Decl::TranslationUnit: 1957 break; 1958 1959 // Declaration kinds for which the definition is not resolvable. 1960 case Decl::UnresolvedUsingTypename: 1961 case Decl::UnresolvedUsingValue: 1962 break; 1963 1964 case Decl::UsingDirective: 1965 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), 1966 CXXUnit); 1967 1968 case Decl::NamespaceAlias: 1969 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit); 1970 1971 case Decl::Enum: 1972 case Decl::Record: 1973 case Decl::CXXRecord: 1974 case Decl::ClassTemplateSpecialization: 1975 case Decl::ClassTemplatePartialSpecialization: 1976 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) 1977 return MakeCXCursor(Def, CXXUnit); 1978 return clang_getNullCursor(); 1979 1980 case Decl::Function: 1981 case Decl::CXXMethod: 1982 case Decl::CXXConstructor: 1983 case Decl::CXXDestructor: 1984 case Decl::CXXConversion: { 1985 const FunctionDecl *Def = 0; 1986 if (cast<FunctionDecl>(D)->getBody(Def)) 1987 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit); 1988 return clang_getNullCursor(); 1989 } 1990 1991 case Decl::Var: { 1992 // Ask the variable if it has a definition. 1993 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) 1994 return MakeCXCursor(Def, CXXUnit); 1995 return clang_getNullCursor(); 1996 } 1997 1998 case Decl::FunctionTemplate: { 1999 const FunctionDecl *Def = 0; 2000 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) 2001 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit); 2002 return clang_getNullCursor(); 2003 } 2004 2005 case Decl::ClassTemplate: { 2006 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() 2007 ->getDefinition()) 2008 return MakeCXCursor( 2009 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), 2010 CXXUnit); 2011 return clang_getNullCursor(); 2012 } 2013 2014 case Decl::Using: { 2015 UsingDecl *Using = cast<UsingDecl>(D); 2016 CXCursor Def = clang_getNullCursor(); 2017 for (UsingDecl::shadow_iterator S = Using->shadow_begin(), 2018 SEnd = Using->shadow_end(); 2019 S != SEnd; ++S) { 2020 if (Def != clang_getNullCursor()) { 2021 // FIXME: We have no way to return multiple results. 2022 return clang_getNullCursor(); 2023 } 2024 2025 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(), 2026 CXXUnit)); 2027 } 2028 2029 return Def; 2030 } 2031 2032 case Decl::UsingShadow: 2033 return clang_getCursorDefinition( 2034 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), 2035 CXXUnit)); 2036 2037 case Decl::ObjCMethod: { 2038 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); 2039 if (Method->isThisDeclarationADefinition()) 2040 return C; 2041 2042 // Dig out the method definition in the associated 2043 // @implementation, if we have it. 2044 // FIXME: The ASTs should make finding the definition easier. 2045 if (ObjCInterfaceDecl *Class 2046 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) 2047 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) 2048 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), 2049 Method->isInstanceMethod())) 2050 if (Def->isThisDeclarationADefinition()) 2051 return MakeCXCursor(Def, CXXUnit); 2052 2053 return clang_getNullCursor(); 2054 } 2055 2056 case Decl::ObjCCategory: 2057 if (ObjCCategoryImplDecl *Impl 2058 = cast<ObjCCategoryDecl>(D)->getImplementation()) 2059 return MakeCXCursor(Impl, CXXUnit); 2060 return clang_getNullCursor(); 2061 2062 case Decl::ObjCProtocol: 2063 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) 2064 return C; 2065 return clang_getNullCursor(); 2066 2067 case Decl::ObjCInterface: 2068 // There are two notions of a "definition" for an Objective-C 2069 // class: the interface and its implementation. When we resolved a 2070 // reference to an Objective-C class, produce the @interface as 2071 // the definition; when we were provided with the interface, 2072 // produce the @implementation as the definition. 2073 if (WasReference) { 2074 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) 2075 return C; 2076 } else if (ObjCImplementationDecl *Impl 2077 = cast<ObjCInterfaceDecl>(D)->getImplementation()) 2078 return MakeCXCursor(Impl, CXXUnit); 2079 return clang_getNullCursor(); 2080 2081 case Decl::ObjCProperty: 2082 // FIXME: We don't really know where to find the 2083 // ObjCPropertyImplDecls that implement this property. 2084 return clang_getNullCursor(); 2085 2086 case Decl::ObjCCompatibleAlias: 2087 if (ObjCInterfaceDecl *Class 2088 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) 2089 if (!Class->isForwardDecl()) 2090 return MakeCXCursor(Class, CXXUnit); 2091 2092 return clang_getNullCursor(); 2093 2094 case Decl::ObjCForwardProtocol: { 2095 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D); 2096 if (Forward->protocol_size() == 1) 2097 return clang_getCursorDefinition( 2098 MakeCXCursor(*Forward->protocol_begin(), 2099 CXXUnit)); 2100 2101 // FIXME: Cannot return multiple definitions. 2102 return clang_getNullCursor(); 2103 } 2104 2105 case Decl::ObjCClass: { 2106 ObjCClassDecl *Class = cast<ObjCClassDecl>(D); 2107 if (Class->size() == 1) { 2108 ObjCInterfaceDecl *IFace = Class->begin()->getInterface(); 2109 if (!IFace->isForwardDecl()) 2110 return MakeCXCursor(IFace, CXXUnit); 2111 return clang_getNullCursor(); 2112 } 2113 2114 // FIXME: Cannot return multiple definitions. 2115 return clang_getNullCursor(); 2116 } 2117 2118 case Decl::Friend: 2119 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) 2120 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); 2121 return clang_getNullCursor(); 2122 2123 case Decl::FriendTemplate: 2124 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) 2125 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); 2126 return clang_getNullCursor(); 2127 } 2128 2129 return clang_getNullCursor(); 2130} 2131 2132unsigned clang_isCursorDefinition(CXCursor C) { 2133 if (!clang_isDeclaration(C.kind)) 2134 return 0; 2135 2136 return clang_getCursorDefinition(C) == C; 2137} 2138 2139void clang_getDefinitionSpellingAndExtent(CXCursor C, 2140 const char **startBuf, 2141 const char **endBuf, 2142 unsigned *startLine, 2143 unsigned *startColumn, 2144 unsigned *endLine, 2145 unsigned *endColumn) { 2146 assert(getCursorDecl(C) && "CXCursor has null decl"); 2147 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); 2148 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); 2149 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); 2150 2151 SourceManager &SM = FD->getASTContext().getSourceManager(); 2152 *startBuf = SM.getCharacterData(Body->getLBracLoc()); 2153 *endBuf = SM.getCharacterData(Body->getRBracLoc()); 2154 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); 2155 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); 2156 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); 2157 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); 2158} 2159 2160void clang_enableStackTraces(void) { 2161 llvm::sys::PrintStackTraceOnErrorSignal(); 2162} 2163 2164} // end: extern "C" 2165 2166//===----------------------------------------------------------------------===// 2167// Token-based Operations. 2168//===----------------------------------------------------------------------===// 2169 2170/* CXToken layout: 2171 * int_data[0]: a CXTokenKind 2172 * int_data[1]: starting token location 2173 * int_data[2]: token length 2174 * int_data[3]: reserved 2175 * ptr_data: for identifiers and keywords, an IdentifierInfo*. 2176 * otherwise unused. 2177 */ 2178extern "C" { 2179 2180CXTokenKind clang_getTokenKind(CXToken CXTok) { 2181 return static_cast<CXTokenKind>(CXTok.int_data[0]); 2182} 2183 2184CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { 2185 switch (clang_getTokenKind(CXTok)) { 2186 case CXToken_Identifier: 2187 case CXToken_Keyword: 2188 // We know we have an IdentifierInfo*, so use that. 2189 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) 2190 ->getNameStart()); 2191 2192 case CXToken_Literal: { 2193 // We have stashed the starting pointer in the ptr_data field. Use it. 2194 const char *Text = static_cast<const char *>(CXTok.ptr_data); 2195 return createCXString(llvm::StringRef(Text, CXTok.int_data[2])); 2196 } 2197 2198 case CXToken_Punctuation: 2199 case CXToken_Comment: 2200 break; 2201 } 2202 2203 // We have to find the starting buffer pointer the hard way, by 2204 // deconstructing the source location. 2205 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 2206 if (!CXXUnit) 2207 return createCXString(""); 2208 2209 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); 2210 std::pair<FileID, unsigned> LocInfo 2211 = CXXUnit->getSourceManager().getDecomposedLoc(Loc); 2212 bool Invalid = false; 2213 llvm::StringRef Buffer 2214 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); 2215 if (Invalid) 2216 return createCXString(""); 2217 2218 return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); 2219} 2220 2221CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { 2222 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 2223 if (!CXXUnit) 2224 return clang_getNullLocation(); 2225 2226 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), 2227 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 2228} 2229 2230CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { 2231 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 2232 if (!CXXUnit) 2233 return clang_getNullRange(); 2234 2235 return cxloc::translateSourceRange(CXXUnit->getASTContext(), 2236 SourceLocation::getFromRawEncoding(CXTok.int_data[1])); 2237} 2238 2239void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, 2240 CXToken **Tokens, unsigned *NumTokens) { 2241 if (Tokens) 2242 *Tokens = 0; 2243 if (NumTokens) 2244 *NumTokens = 0; 2245 2246 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 2247 if (!CXXUnit || !Tokens || !NumTokens) 2248 return; 2249 2250 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2251 2252 SourceRange R = cxloc::translateCXSourceRange(Range); 2253 if (R.isInvalid()) 2254 return; 2255 2256 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 2257 std::pair<FileID, unsigned> BeginLocInfo 2258 = SourceMgr.getDecomposedLoc(R.getBegin()); 2259 std::pair<FileID, unsigned> EndLocInfo 2260 = SourceMgr.getDecomposedLoc(R.getEnd()); 2261 2262 // Cannot tokenize across files. 2263 if (BeginLocInfo.first != EndLocInfo.first) 2264 return; 2265 2266 // Create a lexer 2267 bool Invalid = false; 2268 llvm::StringRef Buffer 2269 = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); 2270 if (Invalid) 2271 return; 2272 2273 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 2274 CXXUnit->getASTContext().getLangOptions(), 2275 Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); 2276 Lex.SetCommentRetentionState(true); 2277 2278 // Lex tokens until we hit the end of the range. 2279 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; 2280 llvm::SmallVector<CXToken, 32> CXTokens; 2281 Token Tok; 2282 do { 2283 // Lex the next token 2284 Lex.LexFromRawLexer(Tok); 2285 if (Tok.is(tok::eof)) 2286 break; 2287 2288 // Initialize the CXToken. 2289 CXToken CXTok; 2290 2291 // - Common fields 2292 CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); 2293 CXTok.int_data[2] = Tok.getLength(); 2294 CXTok.int_data[3] = 0; 2295 2296 // - Kind-specific fields 2297 if (Tok.isLiteral()) { 2298 CXTok.int_data[0] = CXToken_Literal; 2299 CXTok.ptr_data = (void *)Tok.getLiteralData(); 2300 } else if (Tok.is(tok::identifier)) { 2301 // Lookup the identifier to determine whether we have a keyword. 2302 std::pair<FileID, unsigned> LocInfo 2303 = SourceMgr.getDecomposedLoc(Tok.getLocation()); 2304 bool Invalid = false; 2305 llvm::StringRef Buf 2306 = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); 2307 if (Invalid) 2308 return; 2309 2310 const char *StartPos = Buf.data() + LocInfo.second; 2311 IdentifierInfo *II 2312 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos); 2313 2314 if (II->getObjCKeywordID() != tok::objc_not_keyword) { 2315 CXTok.int_data[0] = CXToken_Keyword; 2316 } 2317 else { 2318 CXTok.int_data[0] = II->getTokenID() == tok::identifier? 2319 CXToken_Identifier 2320 : CXToken_Keyword; 2321 } 2322 CXTok.ptr_data = II; 2323 } else if (Tok.is(tok::comment)) { 2324 CXTok.int_data[0] = CXToken_Comment; 2325 CXTok.ptr_data = 0; 2326 } else { 2327 CXTok.int_data[0] = CXToken_Punctuation; 2328 CXTok.ptr_data = 0; 2329 } 2330 CXTokens.push_back(CXTok); 2331 } while (Lex.getBufferLocation() <= EffectiveBufferEnd); 2332 2333 if (CXTokens.empty()) 2334 return; 2335 2336 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); 2337 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); 2338 *NumTokens = CXTokens.size(); 2339} 2340 2341void clang_disposeTokens(CXTranslationUnit TU, 2342 CXToken *Tokens, unsigned NumTokens) { 2343 free(Tokens); 2344} 2345 2346} // end: extern "C" 2347 2348//===----------------------------------------------------------------------===// 2349// Token annotation APIs. 2350//===----------------------------------------------------------------------===// 2351 2352typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; 2353static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 2354 CXCursor parent, 2355 CXClientData client_data); 2356namespace { 2357class AnnotateTokensWorker { 2358 AnnotateTokensData &Annotated; 2359 CXToken *Tokens; 2360 CXCursor *Cursors; 2361 unsigned NumTokens; 2362 unsigned TokIdx; 2363 CursorVisitor AnnotateVis; 2364 SourceManager &SrcMgr; 2365 2366 bool MoreTokens() const { return TokIdx < NumTokens; } 2367 unsigned NextToken() const { return TokIdx; } 2368 void AdvanceToken() { ++TokIdx; } 2369 SourceLocation GetTokenLoc(unsigned tokI) { 2370 return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); 2371 } 2372 2373public: 2374 AnnotateTokensWorker(AnnotateTokensData &annotated, 2375 CXToken *tokens, CXCursor *cursors, unsigned numTokens, 2376 ASTUnit *CXXUnit, SourceRange RegionOfInterest) 2377 : Annotated(annotated), Tokens(tokens), Cursors(cursors), 2378 NumTokens(numTokens), TokIdx(0), 2379 AnnotateVis(CXXUnit, AnnotateTokensVisitor, this, 2380 Decl::MaxPCHLevel, RegionOfInterest), 2381 SrcMgr(CXXUnit->getSourceManager()) {} 2382 2383 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } 2384 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); 2385 void AnnotateTokens(CXCursor parent); 2386}; 2387} 2388 2389void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { 2390 // Walk the AST within the region of interest, annotating tokens 2391 // along the way. 2392 VisitChildren(parent); 2393 2394 for (unsigned I = 0 ; I < TokIdx ; ++I) { 2395 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 2396 if (Pos != Annotated.end()) 2397 Cursors[I] = Pos->second; 2398 } 2399 2400 // Finish up annotating any tokens left. 2401 if (!MoreTokens()) 2402 return; 2403 2404 const CXCursor &C = clang_getNullCursor(); 2405 for (unsigned I = TokIdx ; I < NumTokens ; ++I) { 2406 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); 2407 Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; 2408 } 2409} 2410 2411enum CXChildVisitResult 2412AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { 2413 CXSourceLocation Loc = clang_getCursorLocation(cursor); 2414 // We can always annotate a preprocessing directive/macro instantiation. 2415 if (clang_isPreprocessing(cursor.kind)) { 2416 Annotated[Loc.int_data] = cursor; 2417 return CXChildVisit_Recurse; 2418 } 2419 2420 CXSourceRange cursorExtent = clang_getCursorExtent(cursor); 2421 SourceRange cursorRange = cxloc::translateCXSourceRange(cursorExtent); 2422 2423 if (cursorRange.isInvalid()) 2424 return CXChildVisit_Continue; 2425 2426 SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); 2427 2428 const enum CXCursorKind K = clang_getCursorKind(parent); 2429 const CXCursor updateC = 2430 (clang_isInvalid(K) || K == CXCursor_TranslationUnit || 2431 L.isMacroID()) 2432 ? clang_getNullCursor() : parent; 2433 2434 while (MoreTokens()) { 2435 const unsigned I = NextToken(); 2436 SourceLocation TokLoc = GetTokenLoc(I); 2437 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 2438 case RangeBefore: 2439 Cursors[I] = updateC; 2440 AdvanceToken(); 2441 continue; 2442 case RangeAfter: 2443 return CXChildVisit_Continue; 2444 case RangeOverlap: 2445 break; 2446 } 2447 break; 2448 } 2449 2450 // Visit children to get their cursor information. 2451 const unsigned BeforeChildren = NextToken(); 2452 VisitChildren(cursor); 2453 const unsigned AfterChildren = NextToken(); 2454 2455 // Adjust 'Last' to the last token within the extent of the cursor. 2456 while (MoreTokens()) { 2457 const unsigned I = NextToken(); 2458 SourceLocation TokLoc = GetTokenLoc(I); 2459 switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { 2460 case RangeBefore: 2461 assert(0 && "Infeasible"); 2462 case RangeAfter: 2463 break; 2464 case RangeOverlap: 2465 Cursors[I] = updateC; 2466 AdvanceToken(); 2467 continue; 2468 } 2469 break; 2470 } 2471 const unsigned Last = NextToken(); 2472 2473 // Scan the tokens that are at the beginning of the cursor, but are not 2474 // capture by the child cursors. 2475 2476 // For AST elements within macros, rely on a post-annotate pass to 2477 // to correctly annotate the tokens with cursors. Otherwise we can 2478 // get confusing results of having tokens that map to cursors that really 2479 // are expanded by an instantiation. 2480 if (L.isMacroID()) 2481 cursor = clang_getNullCursor(); 2482 2483 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { 2484 if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) 2485 break; 2486 Cursors[I] = cursor; 2487 } 2488 // Scan the tokens that are at the end of the cursor, but are not captured 2489 // but the child cursors. 2490 for (unsigned I = AfterChildren; I != Last; ++I) 2491 Cursors[I] = cursor; 2492 2493 TokIdx = Last; 2494 return CXChildVisit_Continue; 2495} 2496 2497static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, 2498 CXCursor parent, 2499 CXClientData client_data) { 2500 return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); 2501} 2502 2503extern "C" { 2504 2505void clang_annotateTokens(CXTranslationUnit TU, 2506 CXToken *Tokens, unsigned NumTokens, 2507 CXCursor *Cursors) { 2508 2509 if (NumTokens == 0 || !Tokens || !Cursors) 2510 return; 2511 2512 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); 2513 if (!CXXUnit) { 2514 // Any token we don't specifically annotate will have a NULL cursor. 2515 const CXCursor &C = clang_getNullCursor(); 2516 for (unsigned I = 0; I != NumTokens; ++I) 2517 Cursors[I] = C; 2518 return; 2519 } 2520 2521 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 2522 2523 // Determine the region of interest, which contains all of the tokens. 2524 SourceRange RegionOfInterest; 2525 RegionOfInterest.setBegin(cxloc::translateSourceLocation( 2526 clang_getTokenLocation(TU, Tokens[0]))); 2527 2528 SourceLocation End 2529 = cxloc::translateSourceLocation(clang_getTokenLocation(TU, 2530 Tokens[NumTokens - 1])); 2531 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End)); 2532 2533 // A mapping from the source locations found when re-lexing or traversing the 2534 // region of interest to the corresponding cursors. 2535 AnnotateTokensData Annotated; 2536 2537 // Relex the tokens within the source range to look for preprocessing 2538 // directives. 2539 SourceManager &SourceMgr = CXXUnit->getSourceManager(); 2540 std::pair<FileID, unsigned> BeginLocInfo 2541 = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); 2542 std::pair<FileID, unsigned> EndLocInfo 2543 = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); 2544 2545 llvm::StringRef Buffer; 2546 bool Invalid = false; 2547 if (BeginLocInfo.first == EndLocInfo.first && 2548 ((Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid)),true) && 2549 !Invalid) { 2550 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), 2551 CXXUnit->getASTContext().getLangOptions(), 2552 Buffer.begin(), Buffer.data() + BeginLocInfo.second, 2553 Buffer.end()); 2554 Lex.SetCommentRetentionState(true); 2555 2556 // Lex tokens in raw mode until we hit the end of the range, to avoid 2557 // entering #includes or expanding macros. 2558 while (true) { 2559 Token Tok; 2560 Lex.LexFromRawLexer(Tok); 2561 2562 reprocess: 2563 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { 2564 // We have found a preprocessing directive. Gobble it up so that we 2565 // don't see it while preprocessing these tokens later, but keep track of 2566 // all of the token locations inside this preprocessing directive so that 2567 // we can annotate them appropriately. 2568 // 2569 // FIXME: Some simple tests here could identify macro definitions and 2570 // #undefs, to provide specific cursor kinds for those. 2571 std::vector<SourceLocation> Locations; 2572 do { 2573 Locations.push_back(Tok.getLocation()); 2574 Lex.LexFromRawLexer(Tok); 2575 } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); 2576 2577 using namespace cxcursor; 2578 CXCursor Cursor 2579 = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), 2580 Locations.back()), 2581 CXXUnit); 2582 for (unsigned I = 0, N = Locations.size(); I != N; ++I) { 2583 Annotated[Locations[I].getRawEncoding()] = Cursor; 2584 } 2585 2586 if (Tok.isAtStartOfLine()) 2587 goto reprocess; 2588 2589 continue; 2590 } 2591 2592 if (Tok.is(tok::eof)) 2593 break; 2594 } 2595 } 2596 2597 // Annotate all of the source locations in the region of interest that map to 2598 // a specific cursor. 2599 AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, 2600 CXXUnit, RegionOfInterest); 2601 W.AnnotateTokens(clang_getTranslationUnitCursor(CXXUnit)); 2602} 2603} // end: extern "C" 2604 2605//===----------------------------------------------------------------------===// 2606// Operations for querying linkage of a cursor. 2607//===----------------------------------------------------------------------===// 2608 2609extern "C" { 2610CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { 2611 if (!clang_isDeclaration(cursor.kind)) 2612 return CXLinkage_Invalid; 2613 2614 Decl *D = cxcursor::getCursorDecl(cursor); 2615 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) 2616 switch (ND->getLinkage()) { 2617 case NoLinkage: return CXLinkage_NoLinkage; 2618 case InternalLinkage: return CXLinkage_Internal; 2619 case UniqueExternalLinkage: return CXLinkage_UniqueExternal; 2620 case ExternalLinkage: return CXLinkage_External; 2621 }; 2622 2623 return CXLinkage_Invalid; 2624} 2625} // end: extern "C" 2626 2627//===----------------------------------------------------------------------===// 2628// Operations for querying language of a cursor. 2629//===----------------------------------------------------------------------===// 2630 2631static CXLanguageKind getDeclLanguage(const Decl *D) { 2632 switch (D->getKind()) { 2633 default: 2634 break; 2635 case Decl::ImplicitParam: 2636 case Decl::ObjCAtDefsField: 2637 case Decl::ObjCCategory: 2638 case Decl::ObjCCategoryImpl: 2639 case Decl::ObjCClass: 2640 case Decl::ObjCCompatibleAlias: 2641 case Decl::ObjCForwardProtocol: 2642 case Decl::ObjCImplementation: 2643 case Decl::ObjCInterface: 2644 case Decl::ObjCIvar: 2645 case Decl::ObjCMethod: 2646 case Decl::ObjCProperty: 2647 case Decl::ObjCPropertyImpl: 2648 case Decl::ObjCProtocol: 2649 return CXLanguage_ObjC; 2650 case Decl::CXXConstructor: 2651 case Decl::CXXConversion: 2652 case Decl::CXXDestructor: 2653 case Decl::CXXMethod: 2654 case Decl::CXXRecord: 2655 case Decl::ClassTemplate: 2656 case Decl::ClassTemplatePartialSpecialization: 2657 case Decl::ClassTemplateSpecialization: 2658 case Decl::Friend: 2659 case Decl::FriendTemplate: 2660 case Decl::FunctionTemplate: 2661 case Decl::LinkageSpec: 2662 case Decl::Namespace: 2663 case Decl::NamespaceAlias: 2664 case Decl::NonTypeTemplateParm: 2665 case Decl::StaticAssert: 2666 case Decl::TemplateTemplateParm: 2667 case Decl::TemplateTypeParm: 2668 case Decl::UnresolvedUsingTypename: 2669 case Decl::UnresolvedUsingValue: 2670 case Decl::Using: 2671 case Decl::UsingDirective: 2672 case Decl::UsingShadow: 2673 return CXLanguage_CPlusPlus; 2674 } 2675 2676 return CXLanguage_C; 2677} 2678 2679extern "C" { 2680CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { 2681 if (clang_isDeclaration(cursor.kind)) 2682 return getDeclLanguage(cxcursor::getCursorDecl(cursor)); 2683 2684 return CXLanguage_Invalid; 2685} 2686} // end: extern "C" 2687 2688//===----------------------------------------------------------------------===// 2689// CXString Operations. 2690//===----------------------------------------------------------------------===// 2691 2692extern "C" { 2693const char *clang_getCString(CXString string) { 2694 return string.Spelling; 2695} 2696 2697void clang_disposeString(CXString string) { 2698 if (string.MustFreeString && string.Spelling) 2699 free((void*)string.Spelling); 2700} 2701 2702} // end: extern "C" 2703 2704namespace clang { namespace cxstring { 2705CXString createCXString(const char *String, bool DupString){ 2706 CXString Str; 2707 if (DupString) { 2708 Str.Spelling = strdup(String); 2709 Str.MustFreeString = 1; 2710 } else { 2711 Str.Spelling = String; 2712 Str.MustFreeString = 0; 2713 } 2714 return Str; 2715} 2716 2717CXString createCXString(llvm::StringRef String, bool DupString) { 2718 CXString Result; 2719 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) { 2720 char *Spelling = (char *)malloc(String.size() + 1); 2721 memmove(Spelling, String.data(), String.size()); 2722 Spelling[String.size()] = 0; 2723 Result.Spelling = Spelling; 2724 Result.MustFreeString = 1; 2725 } else { 2726 Result.Spelling = String.data(); 2727 Result.MustFreeString = 0; 2728 } 2729 return Result; 2730} 2731}} 2732 2733//===----------------------------------------------------------------------===// 2734// Misc. utility functions. 2735//===----------------------------------------------------------------------===// 2736 2737extern "C" { 2738 2739CXString clang_getClangVersion() { 2740 return createCXString(getClangFullVersion()); 2741} 2742 2743} // end: extern "C" 2744