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