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