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