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