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