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