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