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