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