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