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