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