]> granicus.if.org Git - clang/blob - tools/libclang/CIndex.cpp
Add a -gmodules option to the driver and a -dwarf-ext-refs to cc1
[clang] / tools / libclang / CIndex.cpp
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 "CIndexDiagnostic.h"
17 #include "CLog.h"
18 #include "CXCursor.h"
19 #include "CXSourceLocation.h"
20 #include "CXString.h"
21 #include "CXTranslationUnit.h"
22 #include "CXType.h"
23 #include "CursorVisitor.h"
24 #include "clang/AST/Attr.h"
25 #include "clang/AST/Mangle.h"
26 #include "clang/AST/StmtVisitor.h"
27 #include "clang/Basic/Diagnostic.h"
28 #include "clang/Basic/DiagnosticCategories.h"
29 #include "clang/Basic/DiagnosticIDs.h"
30 #include "clang/Basic/TargetInfo.h"
31 #include "clang/Basic/Version.h"
32 #include "clang/Frontend/ASTUnit.h"
33 #include "clang/Frontend/CompilerInstance.h"
34 #include "clang/Frontend/FrontendDiagnostic.h"
35 #include "clang/Index/CommentToXML.h"
36 #include "clang/Lex/HeaderSearch.h"
37 #include "clang/Lex/Lexer.h"
38 #include "clang/Lex/PreprocessingRecord.h"
39 #include "clang/Lex/Preprocessor.h"
40 #include "clang/Serialization/SerializationDiagnostic.h"
41 #include "llvm/ADT/Optional.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/StringSwitch.h"
44 #include "llvm/Config/llvm-config.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/Mangler.h"
47 #include "llvm/Support/Compiler.h"
48 #include "llvm/Support/CrashRecoveryContext.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/ManagedStatic.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/Mutex.h"
53 #include "llvm/Support/Program.h"
54 #include "llvm/Support/SaveAndRestore.h"
55 #include "llvm/Support/Signals.h"
56 #include "llvm/Support/TargetSelect.h"
57 #include "llvm/Support/Threading.h"
58 #include "llvm/Support/Timer.h"
59 #include "llvm/Support/raw_ostream.h"
60
61 #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__)
62 #define USE_DARWIN_THREADS
63 #endif
64
65 #ifdef USE_DARWIN_THREADS
66 #include <pthread.h>
67 #endif
68
69 using namespace clang;
70 using namespace clang::cxcursor;
71 using namespace clang::cxtu;
72 using namespace clang::cxindex;
73
74 CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *AU) {
75   if (!AU)
76     return nullptr;
77   assert(CIdx);
78   CXTranslationUnit D = new CXTranslationUnitImpl();
79   D->CIdx = CIdx;
80   D->TheASTUnit = AU;
81   D->StringPool = new cxstring::CXStringPool();
82   D->Diagnostics = nullptr;
83   D->OverridenCursorsPool = createOverridenCXCursorsPool();
84   D->CommentToXML = nullptr;
85   return D;
86 }
87
88 bool cxtu::isASTReadError(ASTUnit *AU) {
89   for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(),
90                                      DEnd = AU->stored_diag_end();
91        D != DEnd; ++D) {
92     if (D->getLevel() >= DiagnosticsEngine::Error &&
93         DiagnosticIDs::getCategoryNumberForDiag(D->getID()) ==
94             diag::DiagCat_AST_Deserialization_Issue)
95       return true;
96   }
97   return false;
98 }
99
100 cxtu::CXTUOwner::~CXTUOwner() {
101   if (TU)
102     clang_disposeTranslationUnit(TU);
103 }
104
105 /// \brief Compare two source ranges to determine their relative position in
106 /// the translation unit.
107 static RangeComparisonResult RangeCompare(SourceManager &SM,
108                                           SourceRange R1,
109                                           SourceRange R2) {
110   assert(R1.isValid() && "First range is invalid?");
111   assert(R2.isValid() && "Second range is invalid?");
112   if (R1.getEnd() != R2.getBegin() &&
113       SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
114     return RangeBefore;
115   if (R2.getEnd() != R1.getBegin() &&
116       SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
117     return RangeAfter;
118   return RangeOverlap;
119 }
120
121 /// \brief Determine if a source location falls within, before, or after a
122 ///   a given source range.
123 static RangeComparisonResult LocationCompare(SourceManager &SM,
124                                              SourceLocation L, SourceRange R) {
125   assert(R.isValid() && "First range is invalid?");
126   assert(L.isValid() && "Second range is invalid?");
127   if (L == R.getBegin() || L == R.getEnd())
128     return RangeOverlap;
129   if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
130     return RangeBefore;
131   if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
132     return RangeAfter;
133   return RangeOverlap;
134 }
135
136 /// \brief Translate a Clang source range into a CIndex source range.
137 ///
138 /// Clang internally represents ranges where the end location points to the
139 /// start of the token at the end. However, for external clients it is more
140 /// useful to have a CXSourceRange be a proper half-open interval. This routine
141 /// does the appropriate translation.
142 CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
143                                           const LangOptions &LangOpts,
144                                           const CharSourceRange &R) {
145   // We want the last character in this location, so we will adjust the
146   // location accordingly.
147   SourceLocation EndLoc = R.getEnd();
148   if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc))
149     EndLoc = SM.getExpansionRange(EndLoc).second;
150   if (R.isTokenRange() && !EndLoc.isInvalid()) {
151     unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc),
152                                                 SM, LangOpts);
153     EndLoc = EndLoc.getLocWithOffset(Length);
154   }
155
156   CXSourceRange Result = {
157     { &SM, &LangOpts },
158     R.getBegin().getRawEncoding(),
159     EndLoc.getRawEncoding()
160   };
161   return Result;
162 }
163
164 //===----------------------------------------------------------------------===//
165 // Cursor visitor.
166 //===----------------------------------------------------------------------===//
167
168 static SourceRange getRawCursorExtent(CXCursor C);
169 static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
170
171
172 RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
173   return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
174 }
175
176 /// \brief Visit the given cursor and, if requested by the visitor,
177 /// its children.
178 ///
179 /// \param Cursor the cursor to visit.
180 ///
181 /// \param CheckedRegionOfInterest if true, then the caller already checked
182 /// that this cursor is within the region of interest.
183 ///
184 /// \returns true if the visitation should be aborted, false if it
185 /// should continue.
186 bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
187   if (clang_isInvalid(Cursor.kind))
188     return false;
189
190   if (clang_isDeclaration(Cursor.kind)) {
191     const Decl *D = getCursorDecl(Cursor);
192     if (!D) {
193       assert(0 && "Invalid declaration cursor");
194       return true; // abort.
195     }
196     
197     // Ignore implicit declarations, unless it's an objc method because
198     // currently we should report implicit methods for properties when indexing.
199     if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
200       return false;
201   }
202
203   // If we have a range of interest, and this cursor doesn't intersect with it,
204   // we're done.
205   if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
206     SourceRange Range = getRawCursorExtent(Cursor);
207     if (Range.isInvalid() || CompareRegionOfInterest(Range))
208       return false;
209   }
210
211   switch (Visitor(Cursor, Parent, ClientData)) {
212   case CXChildVisit_Break:
213     return true;
214
215   case CXChildVisit_Continue:
216     return false;
217
218   case CXChildVisit_Recurse: {
219     bool ret = VisitChildren(Cursor);
220     if (PostChildrenVisitor)
221       if (PostChildrenVisitor(Cursor, ClientData))
222         return true;
223     return ret;
224   }
225   }
226
227   llvm_unreachable("Invalid CXChildVisitResult!");
228 }
229
230 static bool visitPreprocessedEntitiesInRange(SourceRange R,
231                                              PreprocessingRecord &PPRec,
232                                              CursorVisitor &Visitor) {
233   SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
234   FileID FID;
235   
236   if (!Visitor.shouldVisitIncludedEntities()) {
237     // If the begin/end of the range lie in the same FileID, do the optimization
238     // where we skip preprocessed entities that do not come from the same FileID.
239     FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
240     if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
241       FID = FileID();
242   }
243
244   const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R);
245   return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(),
246                                            PPRec, FID);
247 }
248
249 bool CursorVisitor::visitFileRegion() {
250   if (RegionOfInterest.isInvalid())
251     return false;
252
253   ASTUnit *Unit = cxtu::getASTUnit(TU);
254   SourceManager &SM = Unit->getSourceManager();
255   
256   std::pair<FileID, unsigned>
257     Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), 
258     End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); 
259
260   if (End.first != Begin.first) {
261     // If the end does not reside in the same file, try to recover by
262     // picking the end of the file of begin location.
263     End.first = Begin.first;
264     End.second = SM.getFileIDSize(Begin.first);
265   }
266
267   assert(Begin.first == End.first);
268   if (Begin.second > End.second)
269     return false;
270   
271   FileID File = Begin.first;
272   unsigned Offset = Begin.second;
273   unsigned Length = End.second - Begin.second;
274
275   if (!VisitDeclsOnly && !VisitPreprocessorLast)
276     if (visitPreprocessedEntitiesInRegion())
277       return true; // visitation break.
278
279   if (visitDeclsFromFileRegion(File, Offset, Length))
280     return true; // visitation break.
281
282   if (!VisitDeclsOnly && VisitPreprocessorLast)
283     return visitPreprocessedEntitiesInRegion();
284
285   return false;
286 }
287
288 static bool isInLexicalContext(Decl *D, DeclContext *DC) {
289   if (!DC)
290     return false;
291
292   for (DeclContext *DeclDC = D->getLexicalDeclContext();
293          DeclDC; DeclDC = DeclDC->getLexicalParent()) {
294     if (DeclDC == DC)
295       return true;
296   }
297   return false;
298 }
299
300 bool CursorVisitor::visitDeclsFromFileRegion(FileID File,
301                                              unsigned Offset, unsigned Length) {
302   ASTUnit *Unit = cxtu::getASTUnit(TU);
303   SourceManager &SM = Unit->getSourceManager();
304   SourceRange Range = RegionOfInterest;
305
306   SmallVector<Decl *, 16> Decls;
307   Unit->findFileRegionDecls(File, Offset, Length, Decls);
308
309   // If we didn't find any file level decls for the file, try looking at the
310   // file that it was included from.
311   while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
312     bool Invalid = false;
313     const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
314     if (Invalid)
315       return false;
316
317     SourceLocation Outer;
318     if (SLEntry.isFile())
319       Outer = SLEntry.getFile().getIncludeLoc();
320     else
321       Outer = SLEntry.getExpansion().getExpansionLocStart();
322     if (Outer.isInvalid())
323       return false;
324
325     std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
326     Length = 0;
327     Unit->findFileRegionDecls(File, Offset, Length, Decls);
328   }
329
330   assert(!Decls.empty());
331
332   bool VisitedAtLeastOnce = false;
333   DeclContext *CurDC = nullptr;
334   SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
335   for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
336     Decl *D = *DIt;
337     if (D->getSourceRange().isInvalid())
338       continue;
339
340     if (isInLexicalContext(D, CurDC))
341       continue;
342
343     CurDC = dyn_cast<DeclContext>(D);
344
345     if (TagDecl *TD = dyn_cast<TagDecl>(D))
346       if (!TD->isFreeStanding())
347         continue;
348
349     RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range);
350     if (CompRes == RangeBefore)
351       continue;
352     if (CompRes == RangeAfter)
353       break;
354
355     assert(CompRes == RangeOverlap);
356     VisitedAtLeastOnce = true;
357
358     if (isa<ObjCContainerDecl>(D)) {
359       FileDI_current = &DIt;
360       FileDE_current = DE;
361     } else {
362       FileDI_current = nullptr;
363     }
364
365     if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
366       return true; // visitation break.
367   }
368
369   if (VisitedAtLeastOnce)
370     return false;
371
372   // No Decls overlapped with the range. Move up the lexical context until there
373   // is a context that contains the range or we reach the translation unit
374   // level.
375   DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext()
376                                          : (*(DIt-1))->getLexicalDeclContext();
377
378   while (DC && !DC->isTranslationUnit()) {
379     Decl *D = cast<Decl>(DC);
380     SourceRange CurDeclRange = D->getSourceRange();
381     if (CurDeclRange.isInvalid())
382       break;
383
384     if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
385       if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
386         return true; // visitation break.
387     }
388
389     DC = D->getLexicalDeclContext();
390   }
391
392   return false;
393 }
394
395 bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
396   if (!AU->getPreprocessor().getPreprocessingRecord())
397     return false;
398
399   PreprocessingRecord &PPRec
400     = *AU->getPreprocessor().getPreprocessingRecord();
401   SourceManager &SM = AU->getSourceManager();
402   
403   if (RegionOfInterest.isValid()) {
404     SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
405     SourceLocation B = MappedRange.getBegin();
406     SourceLocation E = MappedRange.getEnd();
407
408     if (AU->isInPreambleFileID(B)) {
409       if (SM.isLoadedSourceLocation(E))
410         return visitPreprocessedEntitiesInRange(SourceRange(B, E),
411                                                  PPRec, *this);
412
413       // Beginning of range lies in the preamble but it also extends beyond
414       // it into the main file. Split the range into 2 parts, one covering
415       // the preamble and another covering the main file. This allows subsequent
416       // calls to visitPreprocessedEntitiesInRange to accept a source range that
417       // lies in the same FileID, allowing it to skip preprocessed entities that
418       // do not come from the same FileID.
419       bool breaked =
420         visitPreprocessedEntitiesInRange(
421                                    SourceRange(B, AU->getEndOfPreambleFileID()),
422                                           PPRec, *this);
423       if (breaked) return true;
424       return visitPreprocessedEntitiesInRange(
425                                     SourceRange(AU->getStartOfMainFileID(), E),
426                                         PPRec, *this);
427     }
428
429     return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
430   }
431
432   bool OnlyLocalDecls
433     = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); 
434   
435   if (OnlyLocalDecls)
436     return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
437                                      PPRec);
438
439   return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
440 }
441
442 template<typename InputIterator>
443 bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
444                                               InputIterator Last,
445                                               PreprocessingRecord &PPRec,
446                                               FileID FID) {
447   for (; First != Last; ++First) {
448     if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
449       continue;
450
451     PreprocessedEntity *PPE = *First;
452     if (!PPE)
453       continue;
454
455     if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
456       if (Visit(MakeMacroExpansionCursor(ME, TU)))
457         return true;
458
459       continue;
460     }
461
462     if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) {
463       if (Visit(MakeMacroDefinitionCursor(MD, TU)))
464         return true;
465
466       continue;
467     }
468     
469     if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
470       if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
471         return true;
472       
473       continue;
474     }
475   }
476
477   return false;
478 }
479
480 /// \brief Visit the children of the given cursor.
481 /// 
482 /// \returns true if the visitation should be aborted, false if it
483 /// should continue.
484 bool CursorVisitor::VisitChildren(CXCursor Cursor) {
485   if (clang_isReference(Cursor.kind) && 
486       Cursor.kind != CXCursor_CXXBaseSpecifier) {
487     // By definition, references have no children.
488     return false;
489   }
490
491   // Set the Parent field to Cursor, then back to its old value once we're
492   // done.
493   SetParentRAII SetParent(Parent, StmtParent, Cursor);
494
495   if (clang_isDeclaration(Cursor.kind)) {
496     Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
497     if (!D)
498       return false;
499
500     return VisitAttributes(D) || Visit(D);
501   }
502
503   if (clang_isStatement(Cursor.kind)) {
504     if (const Stmt *S = getCursorStmt(Cursor))
505       return Visit(S);
506
507     return false;
508   }
509
510   if (clang_isExpression(Cursor.kind)) {
511     if (const Expr *E = getCursorExpr(Cursor))
512       return Visit(E);
513
514     return false;
515   }
516
517   if (clang_isTranslationUnit(Cursor.kind)) {
518     CXTranslationUnit TU = getCursorTU(Cursor);
519     ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
520     
521     int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast };
522     for (unsigned I = 0; I != 2; ++I) {
523       if (VisitOrder[I]) {
524         if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
525             RegionOfInterest.isInvalid()) {
526           for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
527                                         TLEnd = CXXUnit->top_level_end();
528                TL != TLEnd; ++TL) {
529             if (Visit(MakeCXCursor(*TL, TU, RegionOfInterest), true))
530               return true;
531           }
532         } else if (VisitDeclContext(
533                                 CXXUnit->getASTContext().getTranslationUnitDecl()))
534           return true;
535         continue;
536       }
537
538       // Walk the preprocessing record.
539       if (CXXUnit->getPreprocessor().getPreprocessingRecord())
540         visitPreprocessedEntitiesInRegion();
541     }
542     
543     return false;
544   }
545
546   if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
547     if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
548       if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
549         return Visit(BaseTSInfo->getTypeLoc());
550       }
551     }
552   }
553
554   if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
555     const IBOutletCollectionAttr *A =
556       cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
557     if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>())
558       return Visit(cxcursor::MakeCursorObjCClassRef(
559           ObjT->getInterface(),
560           A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU));
561   }
562
563   // If pointing inside a macro definition, check if the token is an identifier
564   // that was ever defined as a macro. In such a case, create a "pseudo" macro
565   // expansion cursor for that token.
566   SourceLocation BeginLoc = RegionOfInterest.getBegin();
567   if (Cursor.kind == CXCursor_MacroDefinition &&
568       BeginLoc == RegionOfInterest.getEnd()) {
569     SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc);
570     const MacroInfo *MI =
571         getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU);
572     if (MacroDefinitionRecord *MacroDef =
573             checkForMacroInMacroDefinition(MI, Loc, TU))
574       return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU));
575   }
576
577   // Nothing to visit at the moment.
578   return false;
579 }
580
581 bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
582   if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
583     if (Visit(TSInfo->getTypeLoc()))
584         return true;
585
586   if (Stmt *Body = B->getBody())
587     return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
588
589   return false;
590 }
591
592 Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
593   if (RegionOfInterest.isValid()) {
594     SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
595     if (Range.isInvalid())
596       return None;
597     
598     switch (CompareRegionOfInterest(Range)) {
599     case RangeBefore:
600       // This declaration comes before the region of interest; skip it.
601       return None;
602
603     case RangeAfter:
604       // This declaration comes after the region of interest; we're done.
605       return false;
606
607     case RangeOverlap:
608       // This declaration overlaps the region of interest; visit it.
609       break;
610     }
611   }
612   return true;
613 }
614
615 bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
616   DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
617
618   // FIXME: Eventually remove.  This part of a hack to support proper
619   // iteration over all Decls contained lexically within an ObjC container.
620   SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
621   SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
622
623   for ( ; I != E; ++I) {
624     Decl *D = *I;
625     if (D->getLexicalDeclContext() != DC)
626       continue;
627     CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
628
629     // Ignore synthesized ivars here, otherwise if we have something like:
630     //   @synthesize prop = _prop;
631     // and '_prop' is not declared, we will encounter a '_prop' ivar before
632     // encountering the 'prop' synthesize declaration and we will think that
633     // we passed the region-of-interest.
634     if (ObjCIvarDecl *ivarD = dyn_cast<ObjCIvarDecl>(D)) {
635       if (ivarD->getSynthesize())
636         continue;
637     }
638
639     // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
640     // declarations is a mismatch with the compiler semantics.
641     if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
642       ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D);
643       if (!ID->isThisDeclarationADefinition())
644         Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
645
646     } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
647       ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
648       if (!PD->isThisDeclarationADefinition())
649         Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
650     }
651
652     const Optional<bool> &V = shouldVisitCursor(Cursor);
653     if (!V.hasValue())
654       continue;
655     if (!V.getValue())
656       return false;
657     if (Visit(Cursor, true))
658       return true;
659   }
660   return false;
661 }
662
663 bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
664   llvm_unreachable("Translation units are visited directly by Visit()");
665 }
666
667 bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
668   if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
669     return Visit(TSInfo->getTypeLoc());
670
671   return false;
672 }
673
674 bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
675   if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
676     return Visit(TSInfo->getTypeLoc());
677
678   return false;
679 }
680
681 bool CursorVisitor::VisitTagDecl(TagDecl *D) {
682   return VisitDeclContext(D);
683 }
684
685 bool CursorVisitor::VisitClassTemplateSpecializationDecl(
686                                           ClassTemplateSpecializationDecl *D) {
687   bool ShouldVisitBody = false;
688   switch (D->getSpecializationKind()) {
689   case TSK_Undeclared:
690   case TSK_ImplicitInstantiation:
691     // Nothing to visit
692     return false;
693       
694   case TSK_ExplicitInstantiationDeclaration:
695   case TSK_ExplicitInstantiationDefinition:
696     break;
697       
698   case TSK_ExplicitSpecialization:
699     ShouldVisitBody = true;
700     break;
701   }
702   
703   // Visit the template arguments used in the specialization.
704   if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
705     TypeLoc TL = SpecType->getTypeLoc();
706     if (TemplateSpecializationTypeLoc TSTLoc =
707             TL.getAs<TemplateSpecializationTypeLoc>()) {
708       for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I)
709         if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I)))
710           return true;
711     }
712   }
713   
714   if (ShouldVisitBody && VisitCXXRecordDecl(D))
715     return true;
716   
717   return false;
718 }
719
720 bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
721                                    ClassTemplatePartialSpecializationDecl *D) {
722   // FIXME: Visit the "outer" template parameter lists on the TagDecl
723   // before visiting these template parameters.
724   if (VisitTemplateParameters(D->getTemplateParameters()))
725     return true;
726
727   // Visit the partial specialization arguments.
728   const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten();
729   const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs();
730   for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I)
731     if (VisitTemplateArgumentLoc(TemplateArgs[I]))
732       return true;
733   
734   return VisitCXXRecordDecl(D);
735 }
736
737 bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
738   // Visit the default argument.
739   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
740     if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
741       if (Visit(DefArg->getTypeLoc()))
742         return true;
743   
744   return false;
745 }
746
747 bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
748   if (Expr *Init = D->getInitExpr())
749     return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
750   return false;
751 }
752
753 bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
754   unsigned NumParamList = DD->getNumTemplateParameterLists();
755   for (unsigned i = 0; i < NumParamList; i++) {
756     TemplateParameterList* Params = DD->getTemplateParameterList(i);
757     if (VisitTemplateParameters(Params))
758       return true;
759   }
760
761   if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
762     if (Visit(TSInfo->getTypeLoc()))
763       return true;
764
765   // Visit the nested-name-specifier, if present.
766   if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
767     if (VisitNestedNameSpecifierLoc(QualifierLoc))
768       return true;
769
770   return false;
771 }
772
773 /// \brief Compare two base or member initializers based on their source order.
774 static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X,
775                                       CXXCtorInitializer *const *Y) {
776   return (*X)->getSourceOrder() - (*Y)->getSourceOrder();
777 }
778
779 bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
780   unsigned NumParamList = ND->getNumTemplateParameterLists();
781   for (unsigned i = 0; i < NumParamList; i++) {
782     TemplateParameterList* Params = ND->getTemplateParameterList(i);
783     if (VisitTemplateParameters(Params))
784       return true;
785   }
786
787   if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
788     // Visit the function declaration's syntactic components in the order
789     // written. This requires a bit of work.
790     TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
791     FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
792     
793     // If we have a function declared directly (without the use of a typedef),
794     // visit just the return type. Otherwise, just visit the function's type
795     // now.
796     if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) ||
797         (!FTL && Visit(TL)))
798       return true;
799     
800     // Visit the nested-name-specifier, if present.
801     if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
802       if (VisitNestedNameSpecifierLoc(QualifierLoc))
803         return true;
804     
805     // Visit the declaration name.
806     if (!isa<CXXDestructorDecl>(ND))
807       if (VisitDeclarationNameInfo(ND->getNameInfo()))
808         return true;
809     
810     // FIXME: Visit explicitly-specified template arguments!
811     
812     // Visit the function parameters, if we have a function type.
813     if (FTL && VisitFunctionTypeLoc(FTL, true))
814       return true;
815     
816     // FIXME: Attributes?
817   }
818   
819   if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
820     if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
821       // Find the initializers that were written in the source.
822       SmallVector<CXXCtorInitializer *, 4> WrittenInits;
823       for (auto *I : Constructor->inits()) {
824         if (!I->isWritten())
825           continue;
826       
827         WrittenInits.push_back(I);
828       }
829       
830       // Sort the initializers in source order
831       llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
832                            &CompareCXXCtorInitializers);
833       
834       // Visit the initializers in source order
835       for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
836         CXXCtorInitializer *Init = WrittenInits[I];
837         if (Init->isAnyMemberInitializer()) {
838           if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
839                                         Init->getMemberLocation(), TU)))
840             return true;
841         } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
842           if (Visit(TInfo->getTypeLoc()))
843             return true;
844         }
845         
846         // Visit the initializer value.
847         if (Expr *Initializer = Init->getInit())
848           if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
849             return true;
850       } 
851     }
852     
853     if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
854       return true;
855   }
856
857   return false;
858 }
859
860 bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
861   if (VisitDeclaratorDecl(D))
862     return true;
863
864   if (Expr *BitWidth = D->getBitWidth())
865     return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
866
867   return false;
868 }
869
870 bool CursorVisitor::VisitVarDecl(VarDecl *D) {
871   if (VisitDeclaratorDecl(D))
872     return true;
873
874   if (Expr *Init = D->getInit())
875     return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
876
877   return false;
878 }
879
880 bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
881   if (VisitDeclaratorDecl(D))
882     return true;
883   
884   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
885     if (Expr *DefArg = D->getDefaultArgument())
886       return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
887   
888   return false;  
889 }
890
891 bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
892   // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
893   // before visiting these template parameters.
894   if (VisitTemplateParameters(D->getTemplateParameters()))
895     return true;
896   
897   return VisitFunctionDecl(D->getTemplatedDecl());
898 }
899
900 bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
901   // FIXME: Visit the "outer" template parameter lists on the TagDecl
902   // before visiting these template parameters.
903   if (VisitTemplateParameters(D->getTemplateParameters()))
904     return true;
905   
906   return VisitCXXRecordDecl(D->getTemplatedDecl());
907 }
908
909 bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
910   if (VisitTemplateParameters(D->getTemplateParameters()))
911     return true;
912   
913   if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
914       VisitTemplateArgumentLoc(D->getDefaultArgument()))
915     return true;
916   
917   return false;
918 }
919
920 bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
921   // Visit the bound, if it's explicit.
922   if (D->hasExplicitBound()) {
923     if (auto TInfo = D->getTypeSourceInfo()) {
924       if (Visit(TInfo->getTypeLoc()))
925         return true;
926     }
927   }
928
929   return false;
930 }
931
932 bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
933   if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo())
934     if (Visit(TSInfo->getTypeLoc()))
935       return true;
936
937   for (const auto *P : ND->params()) {
938     if (Visit(MakeCXCursor(P, TU, RegionOfInterest)))
939       return true;
940   }
941
942   if (ND->isThisDeclarationADefinition() &&
943       Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
944     return true;
945
946   return false;
947 }
948
949 template <typename DeclIt>
950 static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
951                                       SourceManager &SM, SourceLocation EndLoc,
952                                       SmallVectorImpl<Decl *> &Decls) {
953   DeclIt next = *DI_current;
954   while (++next != DE_current) {
955     Decl *D_next = *next;
956     if (!D_next)
957       break;
958     SourceLocation L = D_next->getLocStart();
959     if (!L.isValid())
960       break;
961     if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
962       *DI_current = next;
963       Decls.push_back(D_next);
964       continue;
965     }
966     break;
967   }
968 }
969
970 bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
971   // FIXME: Eventually convert back to just 'VisitDeclContext()'.  Essentially
972   // an @implementation can lexically contain Decls that are not properly
973   // nested in the AST.  When we identify such cases, we need to retrofit
974   // this nesting here.
975   if (!DI_current && !FileDI_current)
976     return VisitDeclContext(D);
977
978   // Scan the Decls that immediately come after the container
979   // in the current DeclContext.  If any fall within the
980   // container's lexical region, stash them into a vector
981   // for later processing.
982   SmallVector<Decl *, 24> DeclsInContainer;
983   SourceLocation EndLoc = D->getSourceRange().getEnd();
984   SourceManager &SM = AU->getSourceManager();
985   if (EndLoc.isValid()) {
986     if (DI_current) {
987       addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
988                                 DeclsInContainer);
989     } else {
990       addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
991                                 DeclsInContainer);
992     }
993   }
994
995   // The common case.
996   if (DeclsInContainer.empty())
997     return VisitDeclContext(D);
998
999   // Get all the Decls in the DeclContext, and sort them with the
1000   // additional ones we've collected.  Then visit them.
1001   for (auto *SubDecl : D->decls()) {
1002     if (!SubDecl || SubDecl->getLexicalDeclContext() != D ||
1003         SubDecl->getLocStart().isInvalid())
1004       continue;
1005     DeclsInContainer.push_back(SubDecl);
1006   }
1007
1008   // Now sort the Decls so that they appear in lexical order.
1009   std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
1010             [&SM](Decl *A, Decl *B) {
1011     SourceLocation L_A = A->getLocStart();
1012     SourceLocation L_B = B->getLocStart();
1013     assert(L_A.isValid() && L_B.isValid());
1014     return SM.isBeforeInTranslationUnit(L_A, L_B);
1015   });
1016
1017   // Now visit the decls.
1018   for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
1019          E = DeclsInContainer.end(); I != E; ++I) {
1020     CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
1021     const Optional<bool> &V = shouldVisitCursor(Cursor);
1022     if (!V.hasValue())
1023       continue;
1024     if (!V.getValue())
1025       return false;
1026     if (Visit(Cursor, true))
1027       return true;
1028   }
1029   return false;
1030 }
1031
1032 bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
1033   if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
1034                                    TU)))
1035     return true;
1036
1037   if (VisitObjCTypeParamList(ND->getTypeParamList()))
1038     return true;
1039
1040   ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
1041   for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
1042          E = ND->protocol_end(); I != E; ++I, ++PL)
1043     if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1044       return true;
1045
1046   return VisitObjCContainerDecl(ND);
1047 }
1048
1049 bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1050   if (!PID->isThisDeclarationADefinition())
1051     return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
1052   
1053   ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
1054   for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
1055        E = PID->protocol_end(); I != E; ++I, ++PL)
1056     if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1057       return true;
1058
1059   return VisitObjCContainerDecl(PID);
1060 }
1061
1062 bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
1063   if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
1064     return true;
1065
1066   // FIXME: This implements a workaround with @property declarations also being
1067   // installed in the DeclContext for the @interface.  Eventually this code
1068   // should be removed.
1069   ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
1070   if (!CDecl || !CDecl->IsClassExtension())
1071     return false;
1072
1073   ObjCInterfaceDecl *ID = CDecl->getClassInterface();
1074   if (!ID)
1075     return false;
1076
1077   IdentifierInfo *PropertyId = PD->getIdentifier();
1078   ObjCPropertyDecl *prevDecl =
1079     ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
1080
1081   if (!prevDecl)
1082     return false;
1083
1084   // Visit synthesized methods since they will be skipped when visiting
1085   // the @interface.
1086   if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1087     if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1088       if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1089         return true;
1090
1091   if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1092     if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1093       if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1094         return true;
1095
1096   return false;
1097 }
1098
1099 bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) {
1100   if (!typeParamList)
1101     return false;
1102
1103   for (auto *typeParam : *typeParamList) {
1104     // Visit the type parameter.
1105     if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest)))
1106       return true;
1107   }
1108
1109   return false;
1110 }
1111
1112 bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1113   if (!D->isThisDeclarationADefinition()) {
1114     // Forward declaration is treated like a reference.
1115     return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
1116   }
1117
1118   // Objective-C type parameters.
1119   if (VisitObjCTypeParamList(D->getTypeParamListAsWritten()))
1120     return true;
1121
1122   // Issue callbacks for super class.
1123   if (D->getSuperClass() &&
1124       Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1125                                         D->getSuperClassLoc(),
1126                                         TU)))
1127     return true;
1128
1129   if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo())
1130     if (Visit(SuperClassTInfo->getTypeLoc()))
1131       return true;
1132
1133   ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1134   for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1135          E = D->protocol_end(); I != E; ++I, ++PL)
1136     if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1137       return true;
1138
1139   return VisitObjCContainerDecl(D);
1140 }
1141
1142 bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1143   return VisitObjCContainerDecl(D);
1144 }
1145
1146 bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1147   // 'ID' could be null when dealing with invalid code.
1148   if (ObjCInterfaceDecl *ID = D->getClassInterface())
1149     if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1150       return true;
1151
1152   return VisitObjCImplDecl(D);
1153 }
1154
1155 bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1156 #if 0
1157   // Issue callbacks for super class.
1158   // FIXME: No source location information!
1159   if (D->getSuperClass() &&
1160       Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1161                                         D->getSuperClassLoc(),
1162                                         TU)))
1163     return true;
1164 #endif
1165
1166   return VisitObjCImplDecl(D);
1167 }
1168
1169 bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1170   if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1171     if (PD->isIvarNameSpecified())
1172       return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1173   
1174   return false;
1175 }
1176
1177 bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1178   return VisitDeclContext(D);
1179 }
1180
1181 bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1182   // Visit nested-name-specifier.
1183   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1184     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1185       return true;
1186   
1187   return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), 
1188                                       D->getTargetNameLoc(), TU));
1189 }
1190
1191 bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1192   // Visit nested-name-specifier.
1193   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1194     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1195       return true;
1196   }
1197   
1198   if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1199     return true;
1200     
1201   return VisitDeclarationNameInfo(D->getNameInfo());
1202 }
1203
1204 bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1205   // Visit nested-name-specifier.
1206   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1207     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1208       return true;
1209
1210   return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1211                                       D->getIdentLocation(), TU));
1212 }
1213
1214 bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1215   // Visit nested-name-specifier.
1216   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1217     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1218       return true;
1219   }
1220
1221   return VisitDeclarationNameInfo(D->getNameInfo());
1222 }
1223
1224 bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1225                                                UnresolvedUsingTypenameDecl *D) {
1226   // Visit nested-name-specifier.
1227   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1228     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1229       return true;
1230   
1231   return false;
1232 }
1233
1234 bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1235   switch (Name.getName().getNameKind()) {
1236   case clang::DeclarationName::Identifier:
1237   case clang::DeclarationName::CXXLiteralOperatorName:
1238   case clang::DeclarationName::CXXOperatorName:
1239   case clang::DeclarationName::CXXUsingDirective:
1240     return false;
1241       
1242   case clang::DeclarationName::CXXConstructorName:
1243   case clang::DeclarationName::CXXDestructorName:
1244   case clang::DeclarationName::CXXConversionFunctionName:
1245     if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1246       return Visit(TSInfo->getTypeLoc());
1247     return false;
1248
1249   case clang::DeclarationName::ObjCZeroArgSelector:
1250   case clang::DeclarationName::ObjCOneArgSelector:
1251   case clang::DeclarationName::ObjCMultiArgSelector:
1252     // FIXME: Per-identifier location info?
1253     return false;
1254   }
1255
1256   llvm_unreachable("Invalid DeclarationName::Kind!");
1257 }
1258
1259 bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, 
1260                                              SourceRange Range) {
1261   // FIXME: This whole routine is a hack to work around the lack of proper
1262   // source information in nested-name-specifiers (PR5791). Since we do have
1263   // a beginning source location, we can visit the first component of the
1264   // nested-name-specifier, if it's a single-token component.
1265   if (!NNS)
1266     return false;
1267   
1268   // Get the first component in the nested-name-specifier.
1269   while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1270     NNS = Prefix;
1271   
1272   switch (NNS->getKind()) {
1273   case NestedNameSpecifier::Namespace:
1274     return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1275                                         TU));
1276
1277   case NestedNameSpecifier::NamespaceAlias:
1278     return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 
1279                                         Range.getBegin(), TU));
1280
1281   case NestedNameSpecifier::TypeSpec: {
1282     // If the type has a form where we know that the beginning of the source
1283     // range matches up with a reference cursor. Visit the appropriate reference
1284     // cursor.
1285     const Type *T = NNS->getAsType();
1286     if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1287       return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1288     if (const TagType *Tag = dyn_cast<TagType>(T))
1289       return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1290     if (const TemplateSpecializationType *TST
1291                                       = dyn_cast<TemplateSpecializationType>(T))
1292       return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1293     break;
1294   }
1295       
1296   case NestedNameSpecifier::TypeSpecWithTemplate:
1297   case NestedNameSpecifier::Global:
1298   case NestedNameSpecifier::Identifier:
1299   case NestedNameSpecifier::Super:
1300     break;      
1301   }
1302   
1303   return false;
1304 }
1305
1306 bool 
1307 CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1308   SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1309   for (; Qualifier; Qualifier = Qualifier.getPrefix())
1310     Qualifiers.push_back(Qualifier);
1311   
1312   while (!Qualifiers.empty()) {
1313     NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1314     NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1315     switch (NNS->getKind()) {
1316     case NestedNameSpecifier::Namespace:
1317       if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), 
1318                                        Q.getLocalBeginLoc(),
1319                                        TU)))
1320         return true;
1321         
1322       break;
1323       
1324     case NestedNameSpecifier::NamespaceAlias:
1325       if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), 
1326                                        Q.getLocalBeginLoc(),
1327                                        TU)))
1328         return true;
1329         
1330       break;
1331         
1332     case NestedNameSpecifier::TypeSpec:
1333     case NestedNameSpecifier::TypeSpecWithTemplate:
1334       if (Visit(Q.getTypeLoc()))
1335         return true;
1336         
1337       break;
1338         
1339     case NestedNameSpecifier::Global:
1340     case NestedNameSpecifier::Identifier:
1341     case NestedNameSpecifier::Super:
1342       break;              
1343     }
1344   }
1345   
1346   return false;
1347 }
1348
1349 bool CursorVisitor::VisitTemplateParameters(
1350                                           const TemplateParameterList *Params) {
1351   if (!Params)
1352     return false;
1353   
1354   for (TemplateParameterList::const_iterator P = Params->begin(),
1355                                           PEnd = Params->end();
1356        P != PEnd; ++P) {
1357     if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
1358       return true;
1359   }
1360   
1361   return false;
1362 }
1363
1364 bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1365   switch (Name.getKind()) {
1366   case TemplateName::Template:
1367     return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1368
1369   case TemplateName::OverloadedTemplate:
1370     // Visit the overloaded template set.
1371     if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1372       return true;
1373
1374     return false;
1375
1376   case TemplateName::DependentTemplate:
1377     // FIXME: Visit nested-name-specifier.
1378     return false;
1379       
1380   case TemplateName::QualifiedTemplate:
1381     // FIXME: Visit nested-name-specifier.
1382     return Visit(MakeCursorTemplateRef(
1383                                   Name.getAsQualifiedTemplateName()->getDecl(), 
1384                                        Loc, TU));
1385
1386   case TemplateName::SubstTemplateTemplateParm:
1387     return Visit(MakeCursorTemplateRef(
1388                          Name.getAsSubstTemplateTemplateParm()->getParameter(),
1389                                        Loc, TU));
1390       
1391   case TemplateName::SubstTemplateTemplateParmPack:
1392     return Visit(MakeCursorTemplateRef(
1393                   Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
1394                                        Loc, TU));
1395   }
1396
1397   llvm_unreachable("Invalid TemplateName::Kind!");
1398 }
1399
1400 bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1401   switch (TAL.getArgument().getKind()) {
1402   case TemplateArgument::Null:
1403   case TemplateArgument::Integral:
1404   case TemplateArgument::Pack:
1405     return false;
1406       
1407   case TemplateArgument::Type:
1408     if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1409       return Visit(TSInfo->getTypeLoc());
1410     return false;
1411       
1412   case TemplateArgument::Declaration:
1413     if (Expr *E = TAL.getSourceDeclExpression())
1414       return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1415     return false;
1416
1417   case TemplateArgument::NullPtr:
1418     if (Expr *E = TAL.getSourceNullPtrExpression())
1419       return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1420     return false;
1421
1422   case TemplateArgument::Expression:
1423     if (Expr *E = TAL.getSourceExpression())
1424       return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1425     return false;
1426   
1427   case TemplateArgument::Template:
1428   case TemplateArgument::TemplateExpansion:
1429     if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1430       return true;
1431       
1432     return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), 
1433                              TAL.getTemplateNameLoc());
1434   }
1435
1436   llvm_unreachable("Invalid TemplateArgument::Kind!");
1437 }
1438
1439 bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1440   return VisitDeclContext(D);
1441 }
1442
1443 bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1444   return Visit(TL.getUnqualifiedLoc());
1445 }
1446
1447 bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1448   ASTContext &Context = AU->getASTContext();
1449
1450   // Some builtin types (such as Objective-C's "id", "sel", and
1451   // "Class") have associated declarations. Create cursors for those.
1452   QualType VisitType;
1453   switch (TL.getTypePtr()->getKind()) {
1454
1455   case BuiltinType::Void:
1456   case BuiltinType::NullPtr:
1457   case BuiltinType::Dependent:
1458   case BuiltinType::OCLImage1d:
1459   case BuiltinType::OCLImage1dArray:
1460   case BuiltinType::OCLImage1dBuffer:
1461   case BuiltinType::OCLImage2d:
1462   case BuiltinType::OCLImage2dArray:
1463   case BuiltinType::OCLImage3d:
1464   case BuiltinType::OCLSampler:
1465   case BuiltinType::OCLEvent:
1466 #define BUILTIN_TYPE(Id, SingletonId)
1467 #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1468 #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1469 #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1470 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1471 #include "clang/AST/BuiltinTypes.def"
1472     break;
1473
1474   case BuiltinType::ObjCId:
1475     VisitType = Context.getObjCIdType();
1476     break;
1477
1478   case BuiltinType::ObjCClass:
1479     VisitType = Context.getObjCClassType();
1480     break;
1481
1482   case BuiltinType::ObjCSel:
1483     VisitType = Context.getObjCSelType();
1484     break;
1485   }
1486
1487   if (!VisitType.isNull()) {
1488     if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1489       return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
1490                                      TU));
1491   }
1492
1493   return false;
1494 }
1495
1496 bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1497   return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
1498 }
1499
1500 bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1501   return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1502 }
1503
1504 bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1505   if (TL.isDefinition())
1506     return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
1507
1508   return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1509 }
1510
1511 bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1512   return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1513 }
1514
1515 bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1516   if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1517     return true;
1518
1519   return false;
1520 }
1521
1522 bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1523   if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1524     return true;
1525
1526   for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) {
1527     if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc()))
1528       return true;
1529   }
1530
1531   for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1532     if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1533                                         TU)))
1534       return true;
1535   }
1536
1537   return false;
1538 }
1539
1540 bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1541   return Visit(TL.getPointeeLoc());
1542 }
1543
1544 bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1545   return Visit(TL.getInnerLoc());
1546 }
1547
1548 bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1549   return Visit(TL.getPointeeLoc());
1550 }
1551
1552 bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1553   return Visit(TL.getPointeeLoc());
1554 }
1555
1556 bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1557   return Visit(TL.getPointeeLoc());
1558 }
1559
1560 bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1561   return Visit(TL.getPointeeLoc());
1562 }
1563
1564 bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1565   return Visit(TL.getPointeeLoc());
1566 }
1567
1568 bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1569   return Visit(TL.getModifiedLoc());
1570 }
1571
1572 bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, 
1573                                          bool SkipResultType) {
1574   if (!SkipResultType && Visit(TL.getReturnLoc()))
1575     return true;
1576
1577   for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I)
1578     if (Decl *D = TL.getParam(I))
1579       if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
1580         return true;
1581
1582   return false;
1583 }
1584
1585 bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1586   if (Visit(TL.getElementLoc()))
1587     return true;
1588
1589   if (Expr *Size = TL.getSizeExpr())
1590     return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
1591
1592   return false;
1593 }
1594
1595 bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
1596   return Visit(TL.getOriginalLoc());
1597 }
1598
1599 bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
1600   return Visit(TL.getOriginalLoc());
1601 }
1602
1603 bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1604                                              TemplateSpecializationTypeLoc TL) {
1605   // Visit the template name.
1606   if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), 
1607                         TL.getTemplateNameLoc()))
1608     return true;
1609   
1610   // Visit the template arguments.
1611   for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1612     if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1613       return true;
1614   
1615   return false;
1616 }
1617
1618 bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1619   return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1620 }
1621
1622 bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1623   if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1624     return Visit(TSInfo->getTypeLoc());
1625
1626   return false;
1627 }
1628
1629 bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1630   if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1631     return Visit(TSInfo->getTypeLoc());
1632
1633   return false;
1634 }
1635
1636 bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1637   if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1638     return true;
1639   
1640   return false;
1641 }
1642
1643 bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1644                                     DependentTemplateSpecializationTypeLoc TL) {
1645   // Visit the nested-name-specifier, if there is one.
1646   if (TL.getQualifierLoc() &&
1647       VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1648     return true;
1649   
1650   // Visit the template arguments.
1651   for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1652     if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1653       return true;
1654
1655   return false;
1656 }
1657
1658 bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1659   if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1660     return true;
1661   
1662   return Visit(TL.getNamedTypeLoc());
1663 }
1664
1665 bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1666   return Visit(TL.getPatternLoc());
1667 }
1668
1669 bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1670   if (Expr *E = TL.getUnderlyingExpr())
1671     return Visit(MakeCXCursor(E, StmtParent, TU));
1672
1673   return false;
1674 }
1675
1676 bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1677   return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1678 }
1679
1680 bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1681   return Visit(TL.getValueLoc());
1682 }
1683
1684 #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \
1685 bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
1686   return Visit##PARENT##Loc(TL); \
1687 }
1688
1689 DEFAULT_TYPELOC_IMPL(Complex, Type)
1690 DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
1691 DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
1692 DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
1693 DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
1694 DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
1695 DEFAULT_TYPELOC_IMPL(Vector, Type)
1696 DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
1697 DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
1698 DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
1699 DEFAULT_TYPELOC_IMPL(Record, TagType)
1700 DEFAULT_TYPELOC_IMPL(Enum, TagType)
1701 DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
1702 DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
1703 DEFAULT_TYPELOC_IMPL(Auto, Type)
1704
1705 bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1706   // Visit the nested-name-specifier, if present.
1707   if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1708     if (VisitNestedNameSpecifierLoc(QualifierLoc))
1709       return true;
1710
1711   if (D->isCompleteDefinition()) {
1712     for (const auto &I : D->bases()) {
1713       if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU)))
1714         return true;
1715     }
1716   }
1717
1718   return VisitTagDecl(D);
1719 }
1720
1721 bool CursorVisitor::VisitAttributes(Decl *D) {
1722   for (const auto *I : D->attrs())
1723     if (Visit(MakeCXCursor(I, D, TU)))
1724         return true;
1725
1726   return false;
1727 }
1728
1729 //===----------------------------------------------------------------------===//
1730 // Data-recursive visitor methods.
1731 //===----------------------------------------------------------------------===//
1732
1733 namespace {
1734 #define DEF_JOB(NAME, DATA, KIND)\
1735 class NAME : public VisitorJob {\
1736 public:\
1737   NAME(const DATA *d, CXCursor parent) : \
1738       VisitorJob(parent, VisitorJob::KIND, d) {} \
1739   static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
1740   const DATA *get() const { return static_cast<const DATA*>(data[0]); }\
1741 };
1742
1743 DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1744 DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1745 DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1746 DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1747 DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, 
1748         ExplicitTemplateArgsVisitKind)
1749 DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1750 DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1751 DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
1752 #undef DEF_JOB
1753
1754 class DeclVisit : public VisitorJob {
1755 public:
1756   DeclVisit(const Decl *D, CXCursor parent, bool isFirst) :
1757     VisitorJob(parent, VisitorJob::DeclVisitKind,
1758                D, isFirst ? (void*) 1 : (void*) nullptr) {}
1759   static bool classof(const VisitorJob *VJ) {
1760     return VJ->getKind() == DeclVisitKind;
1761   }
1762   const Decl *get() const { return static_cast<const Decl *>(data[0]); }
1763   bool isFirst() const { return data[1] != nullptr; }
1764 };
1765 class TypeLocVisit : public VisitorJob {
1766 public:
1767   TypeLocVisit(TypeLoc tl, CXCursor parent) :
1768     VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1769                tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1770
1771   static bool classof(const VisitorJob *VJ) {
1772     return VJ->getKind() == TypeLocVisitKind;
1773   }
1774
1775   TypeLoc get() const { 
1776     QualType T = QualType::getFromOpaquePtr(data[0]);
1777     return TypeLoc(T, const_cast<void *>(data[1]));
1778   }
1779 };
1780
1781 class LabelRefVisit : public VisitorJob {
1782 public:
1783   LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
1784     : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
1785                  labelLoc.getPtrEncoding()) {}
1786   
1787   static bool classof(const VisitorJob *VJ) {
1788     return VJ->getKind() == VisitorJob::LabelRefVisitKind;
1789   }
1790   const LabelDecl *get() const {
1791     return static_cast<const LabelDecl *>(data[0]);
1792   }
1793   SourceLocation getLoc() const { 
1794     return SourceLocation::getFromPtrEncoding(data[1]); }
1795 };
1796   
1797 class NestedNameSpecifierLocVisit : public VisitorJob {
1798 public:
1799   NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
1800     : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
1801                  Qualifier.getNestedNameSpecifier(),
1802                  Qualifier.getOpaqueData()) { }
1803   
1804   static bool classof(const VisitorJob *VJ) {
1805     return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
1806   }
1807   
1808   NestedNameSpecifierLoc get() const {
1809     return NestedNameSpecifierLoc(
1810             const_cast<NestedNameSpecifier *>(
1811               static_cast<const NestedNameSpecifier *>(data[0])),
1812             const_cast<void *>(data[1]));
1813   }
1814 };
1815   
1816 class DeclarationNameInfoVisit : public VisitorJob {
1817 public:
1818   DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
1819     : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
1820   static bool classof(const VisitorJob *VJ) {
1821     return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
1822   }
1823   DeclarationNameInfo get() const {
1824     const Stmt *S = static_cast<const Stmt *>(data[0]);
1825     switch (S->getStmtClass()) {
1826     default:
1827       llvm_unreachable("Unhandled Stmt");
1828     case clang::Stmt::MSDependentExistsStmtClass:
1829       return cast<MSDependentExistsStmt>(S)->getNameInfo();
1830     case Stmt::CXXDependentScopeMemberExprClass:
1831       return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
1832     case Stmt::DependentScopeDeclRefExprClass:
1833       return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
1834     case Stmt::OMPCriticalDirectiveClass:
1835       return cast<OMPCriticalDirective>(S)->getDirectiveName();
1836     }
1837   }
1838 };
1839 class MemberRefVisit : public VisitorJob {
1840 public:
1841   MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
1842     : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
1843                  L.getPtrEncoding()) {}
1844   static bool classof(const VisitorJob *VJ) {
1845     return VJ->getKind() == VisitorJob::MemberRefVisitKind;
1846   }
1847   const FieldDecl *get() const {
1848     return static_cast<const FieldDecl *>(data[0]);
1849   }
1850   SourceLocation getLoc() const {
1851     return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1852   }
1853 };
1854 class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
1855   friend class OMPClauseEnqueue;
1856   VisitorWorkList &WL;
1857   CXCursor Parent;
1858 public:
1859   EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
1860     : WL(wl), Parent(parent) {}
1861
1862   void VisitAddrLabelExpr(const AddrLabelExpr *E);
1863   void VisitBlockExpr(const BlockExpr *B);
1864   void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1865   void VisitCompoundStmt(const CompoundStmt *S);
1866   void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ }
1867   void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
1868   void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
1869   void VisitCXXNewExpr(const CXXNewExpr *E);
1870   void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
1871   void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
1872   void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
1873   void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
1874   void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1875   void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
1876   void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
1877   void VisitCXXCatchStmt(const CXXCatchStmt *S);
1878   void VisitCXXForRangeStmt(const CXXForRangeStmt *S);
1879   void VisitDeclRefExpr(const DeclRefExpr *D);
1880   void VisitDeclStmt(const DeclStmt *S);
1881   void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
1882   void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
1883   void VisitExplicitCastExpr(const ExplicitCastExpr *E);
1884   void VisitForStmt(const ForStmt *FS);
1885   void VisitGotoStmt(const GotoStmt *GS);
1886   void VisitIfStmt(const IfStmt *If);
1887   void VisitInitListExpr(const InitListExpr *IE);
1888   void VisitMemberExpr(const MemberExpr *M);
1889   void VisitOffsetOfExpr(const OffsetOfExpr *E);
1890   void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1891   void VisitObjCMessageExpr(const ObjCMessageExpr *M);
1892   void VisitOverloadExpr(const OverloadExpr *E);
1893   void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
1894   void VisitStmt(const Stmt *S);
1895   void VisitSwitchStmt(const SwitchStmt *S);
1896   void VisitWhileStmt(const WhileStmt *W);
1897   void VisitTypeTraitExpr(const TypeTraitExpr *E);
1898   void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
1899   void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
1900   void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
1901   void VisitVAArgExpr(const VAArgExpr *E);
1902   void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1903   void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
1904   void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
1905   void VisitLambdaExpr(const LambdaExpr *E);
1906   void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
1907   void VisitOMPLoopDirective(const OMPLoopDirective *D);
1908   void VisitOMPParallelDirective(const OMPParallelDirective *D);
1909   void VisitOMPSimdDirective(const OMPSimdDirective *D);
1910   void VisitOMPForDirective(const OMPForDirective *D);
1911   void VisitOMPForSimdDirective(const OMPForSimdDirective *D);
1912   void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
1913   void VisitOMPSectionDirective(const OMPSectionDirective *D);
1914   void VisitOMPSingleDirective(const OMPSingleDirective *D);
1915   void VisitOMPMasterDirective(const OMPMasterDirective *D);
1916   void VisitOMPCriticalDirective(const OMPCriticalDirective *D);
1917   void VisitOMPParallelForDirective(const OMPParallelForDirective *D);
1918   void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D);
1919   void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D);
1920   void VisitOMPTaskDirective(const OMPTaskDirective *D);
1921   void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D);
1922   void VisitOMPBarrierDirective(const OMPBarrierDirective *D);
1923   void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D);
1924   void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D);
1925   void
1926   VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D);
1927   void VisitOMPCancelDirective(const OMPCancelDirective *D);
1928   void VisitOMPFlushDirective(const OMPFlushDirective *D);
1929   void VisitOMPOrderedDirective(const OMPOrderedDirective *D);
1930   void VisitOMPAtomicDirective(const OMPAtomicDirective *D);
1931   void VisitOMPTargetDirective(const OMPTargetDirective *D);
1932   void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D);
1933   void VisitOMPTeamsDirective(const OMPTeamsDirective *D);
1934
1935 private:
1936   void AddDeclarationNameInfo(const Stmt *S);
1937   void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
1938   void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A);
1939   void AddMemberRef(const FieldDecl *D, SourceLocation L);
1940   void AddStmt(const Stmt *S);
1941   void AddDecl(const Decl *D, bool isFirst = true);
1942   void AddTypeLoc(TypeSourceInfo *TI);
1943   void EnqueueChildren(const Stmt *S);
1944   void EnqueueChildren(const OMPClause *S);
1945 };
1946 } // end anonyous namespace
1947
1948 void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
1949   // 'S' should always be non-null, since it comes from the
1950   // statement we are visiting.
1951   WL.push_back(DeclarationNameInfoVisit(S, Parent));
1952 }
1953
1954 void 
1955 EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1956   if (Qualifier)
1957     WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
1958 }
1959
1960 void EnqueueVisitor::AddStmt(const Stmt *S) {
1961   if (S)
1962     WL.push_back(StmtVisit(S, Parent));
1963 }
1964 void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
1965   if (D)
1966     WL.push_back(DeclVisit(D, Parent, isFirst));
1967 }
1968 void EnqueueVisitor::
1969   AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) {
1970   if (A)
1971     WL.push_back(ExplicitTemplateArgsVisit(A, Parent));
1972 }
1973 void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
1974   if (D)
1975     WL.push_back(MemberRefVisit(D, L, Parent));
1976 }
1977 void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
1978   if (TI)
1979     WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
1980  }
1981 void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
1982   unsigned size = WL.size();
1983   for (const Stmt *SubStmt : S->children()) {
1984     AddStmt(SubStmt);
1985   }
1986   if (size == WL.size())
1987     return;
1988   // Now reverse the entries we just added.  This will match the DFS
1989   // ordering performed by the worklist.
1990   VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1991   std::reverse(I, E);
1992 }
1993 namespace {
1994 class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
1995   EnqueueVisitor *Visitor;
1996   /// \brief Process clauses with list of variables.
1997   template <typename T>
1998   void VisitOMPClauseList(T *Node);
1999 public:
2000   OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { }
2001 #define OPENMP_CLAUSE(Name, Class)                                             \
2002   void Visit##Class(const Class *C);
2003 #include "clang/Basic/OpenMPKinds.def"
2004 };
2005
2006 void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
2007   Visitor->AddStmt(C->getCondition());
2008 }
2009
2010 void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) {
2011   Visitor->AddStmt(C->getCondition());
2012 }
2013
2014 void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
2015   Visitor->AddStmt(C->getNumThreads());
2016 }
2017
2018 void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) {
2019   Visitor->AddStmt(C->getSafelen());
2020 }
2021
2022 void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) {
2023   Visitor->AddStmt(C->getSimdlen());
2024 }
2025
2026 void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) {
2027   Visitor->AddStmt(C->getNumForLoops());
2028 }
2029
2030 void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
2031
2032 void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { }
2033
2034 void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) {
2035   Visitor->AddStmt(C->getChunkSize());
2036   Visitor->AddStmt(C->getHelperChunkSize());
2037 }
2038
2039 void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) {
2040   Visitor->AddStmt(C->getNumForLoops());
2041 }
2042
2043 void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {}
2044
2045 void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
2046
2047 void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
2048
2049 void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
2050
2051 void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {}
2052
2053 void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {}
2054
2055 void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
2056
2057 void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
2058
2059 void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) {
2060   Visitor->AddStmt(C->getDevice());
2061 }
2062
2063 template<typename T>
2064 void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
2065   for (const auto *I : Node->varlists()) {
2066     Visitor->AddStmt(I);
2067   }
2068 }
2069
2070 void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
2071   VisitOMPClauseList(C);
2072   for (const auto *E : C->private_copies()) {
2073     Visitor->AddStmt(E);
2074   }
2075 }
2076 void OMPClauseEnqueue::VisitOMPFirstprivateClause(
2077                                         const OMPFirstprivateClause *C) {
2078   VisitOMPClauseList(C);
2079 }
2080 void OMPClauseEnqueue::VisitOMPLastprivateClause(
2081                                         const OMPLastprivateClause *C) {
2082   VisitOMPClauseList(C);
2083   for (auto *E : C->private_copies()) {
2084     Visitor->AddStmt(E);
2085   }
2086   for (auto *E : C->source_exprs()) {
2087     Visitor->AddStmt(E);
2088   }
2089   for (auto *E : C->destination_exprs()) {
2090     Visitor->AddStmt(E);
2091   }
2092   for (auto *E : C->assignment_ops()) {
2093     Visitor->AddStmt(E);
2094   }
2095 }
2096 void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) {
2097   VisitOMPClauseList(C);
2098 }
2099 void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) {
2100   VisitOMPClauseList(C);
2101   for (auto *E : C->lhs_exprs()) {
2102     Visitor->AddStmt(E);
2103   }
2104   for (auto *E : C->rhs_exprs()) {
2105     Visitor->AddStmt(E);
2106   }
2107   for (auto *E : C->reduction_ops()) {
2108     Visitor->AddStmt(E);
2109   }
2110 }
2111 void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) {
2112   VisitOMPClauseList(C);
2113   for (const auto *E : C->privates()) {
2114     Visitor->AddStmt(E);
2115   }
2116   for (const auto *E : C->inits()) {
2117     Visitor->AddStmt(E);
2118   }
2119   for (const auto *E : C->updates()) {
2120     Visitor->AddStmt(E);
2121   }
2122   for (const auto *E : C->finals()) {
2123     Visitor->AddStmt(E);
2124   }
2125   Visitor->AddStmt(C->getStep());
2126   Visitor->AddStmt(C->getCalcStep());
2127 }
2128 void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) {
2129   VisitOMPClauseList(C);
2130   Visitor->AddStmt(C->getAlignment());
2131 }
2132 void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) {
2133   VisitOMPClauseList(C);
2134   for (auto *E : C->source_exprs()) {
2135     Visitor->AddStmt(E);
2136   }
2137   for (auto *E : C->destination_exprs()) {
2138     Visitor->AddStmt(E);
2139   }
2140   for (auto *E : C->assignment_ops()) {
2141     Visitor->AddStmt(E);
2142   }
2143 }
2144 void
2145 OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) {
2146   VisitOMPClauseList(C);
2147   for (auto *E : C->source_exprs()) {
2148     Visitor->AddStmt(E);
2149   }
2150   for (auto *E : C->destination_exprs()) {
2151     Visitor->AddStmt(E);
2152   }
2153   for (auto *E : C->assignment_ops()) {
2154     Visitor->AddStmt(E);
2155   }
2156 }
2157 void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) {
2158   VisitOMPClauseList(C);
2159 }
2160 void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) {
2161   VisitOMPClauseList(C);
2162 }
2163 }
2164
2165 void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
2166   unsigned size = WL.size();
2167   OMPClauseEnqueue Visitor(this);
2168   Visitor.Visit(S);
2169   if (size == WL.size())
2170     return;
2171   // Now reverse the entries we just added.  This will match the DFS
2172   // ordering performed by the worklist.
2173   VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2174   std::reverse(I, E);
2175 }
2176 void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2177   WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
2178 }
2179 void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
2180   AddDecl(B->getBlockDecl());
2181 }
2182 void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2183   EnqueueChildren(E);
2184   AddTypeLoc(E->getTypeSourceInfo());
2185 }
2186 void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
2187   for (auto &I : llvm::reverse(S->body()))
2188     AddStmt(I);
2189 }
2190 void EnqueueVisitor::
2191 VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
2192   AddStmt(S->getSubStmt());
2193   AddDeclarationNameInfo(S);
2194   if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
2195     AddNestedNameSpecifierLoc(QualifierLoc);
2196 }
2197
2198 void EnqueueVisitor::
2199 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
2200   AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2201   AddDeclarationNameInfo(E);
2202   if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2203     AddNestedNameSpecifierLoc(QualifierLoc);
2204   if (!E->isImplicitAccess())
2205     AddStmt(E->getBase());
2206 }
2207 void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
2208   // Enqueue the initializer , if any.
2209   AddStmt(E->getInitializer());
2210   // Enqueue the array size, if any.
2211   AddStmt(E->getArraySize());
2212   // Enqueue the allocated type.
2213   AddTypeLoc(E->getAllocatedTypeSourceInfo());
2214   // Enqueue the placement arguments.
2215   for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
2216     AddStmt(E->getPlacementArg(I-1));
2217 }
2218 void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
2219   for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
2220     AddStmt(CE->getArg(I-1));
2221   AddStmt(CE->getCallee());
2222   AddStmt(CE->getArg(0));
2223 }
2224 void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
2225                                         const CXXPseudoDestructorExpr *E) {
2226   // Visit the name of the type being destroyed.
2227   AddTypeLoc(E->getDestroyedTypeInfo());
2228   // Visit the scope type that looks disturbingly like the nested-name-specifier
2229   // but isn't.
2230   AddTypeLoc(E->getScopeTypeInfo());
2231   // Visit the nested-name-specifier.
2232   if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2233     AddNestedNameSpecifierLoc(QualifierLoc);
2234   // Visit base expression.
2235   AddStmt(E->getBase());
2236 }
2237 void EnqueueVisitor::VisitCXXScalarValueInitExpr(
2238                                         const CXXScalarValueInitExpr *E) {
2239   AddTypeLoc(E->getTypeSourceInfo());
2240 }
2241 void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
2242                                         const CXXTemporaryObjectExpr *E) {
2243   EnqueueChildren(E);
2244   AddTypeLoc(E->getTypeSourceInfo());
2245 }
2246 void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2247   EnqueueChildren(E);
2248   if (E->isTypeOperand())
2249     AddTypeLoc(E->getTypeOperandSourceInfo());
2250 }
2251
2252 void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
2253                                         const CXXUnresolvedConstructExpr *E) {
2254   EnqueueChildren(E);
2255   AddTypeLoc(E->getTypeSourceInfo());
2256 }
2257 void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2258   EnqueueChildren(E);
2259   if (E->isTypeOperand())
2260     AddTypeLoc(E->getTypeOperandSourceInfo());
2261 }
2262
2263 void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
2264   EnqueueChildren(S);
2265   AddDecl(S->getExceptionDecl());
2266 }
2267
2268 void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
2269   AddStmt(S->getBody());
2270   AddStmt(S->getRangeInit());
2271   AddDecl(S->getLoopVariable());
2272 }
2273
2274 void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
2275   if (DR->hasExplicitTemplateArgs()) {
2276     AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
2277   }
2278   WL.push_back(DeclRefExprParts(DR, Parent));
2279 }
2280 void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
2281                                         const DependentScopeDeclRefExpr *E) {
2282   AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2283   AddDeclarationNameInfo(E);
2284   AddNestedNameSpecifierLoc(E->getQualifierLoc());
2285 }
2286 void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
2287   unsigned size = WL.size();
2288   bool isFirst = true;
2289   for (const auto *D : S->decls()) {
2290     AddDecl(D, isFirst);
2291     isFirst = false;
2292   }
2293   if (size == WL.size())
2294     return;
2295   // Now reverse the entries we just added.  This will match the DFS
2296   // ordering performed by the worklist.
2297   VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2298   std::reverse(I, E);
2299 }
2300 void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
2301   AddStmt(E->getInit());
2302   for (DesignatedInitExpr::const_reverse_designators_iterator
2303          D = E->designators_rbegin(), DEnd = E->designators_rend();
2304          D != DEnd; ++D) {
2305     if (D->isFieldDesignator()) {
2306       if (FieldDecl *Field = D->getField())
2307         AddMemberRef(Field, D->getFieldLoc());
2308       continue;
2309     }
2310     if (D->isArrayDesignator()) {
2311       AddStmt(E->getArrayIndex(*D));
2312       continue;
2313     }
2314     assert(D->isArrayRangeDesignator() && "Unknown designator kind");
2315     AddStmt(E->getArrayRangeEnd(*D));
2316     AddStmt(E->getArrayRangeStart(*D));
2317   }
2318 }
2319 void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
2320   EnqueueChildren(E);
2321   AddTypeLoc(E->getTypeInfoAsWritten());
2322 }
2323 void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
2324   AddStmt(FS->getBody());
2325   AddStmt(FS->getInc());
2326   AddStmt(FS->getCond());
2327   AddDecl(FS->getConditionVariable());
2328   AddStmt(FS->getInit());
2329 }
2330 void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
2331   WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
2332 }
2333 void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
2334   AddStmt(If->getElse());
2335   AddStmt(If->getThen());
2336   AddStmt(If->getCond());
2337   AddDecl(If->getConditionVariable());
2338 }
2339 void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
2340   // We care about the syntactic form of the initializer list, only.
2341   if (InitListExpr *Syntactic = IE->getSyntacticForm())
2342     IE = Syntactic;
2343   EnqueueChildren(IE);
2344 }
2345 void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
2346   WL.push_back(MemberExprParts(M, Parent));
2347   
2348   // If the base of the member access expression is an implicit 'this', don't
2349   // visit it.
2350   // FIXME: If we ever want to show these implicit accesses, this will be
2351   // unfortunate. However, clang_getCursor() relies on this behavior.
2352   if (M->isImplicitAccess())
2353     return;
2354
2355   // Ignore base anonymous struct/union fields, otherwise they will shadow the
2356   // real field that that we are interested in.
2357   if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) {
2358     if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) {
2359       if (FD->isAnonymousStructOrUnion()) {
2360         AddStmt(SubME->getBase());
2361         return;
2362       }
2363     }
2364   }
2365
2366   AddStmt(M->getBase());
2367 }
2368 void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2369   AddTypeLoc(E->getEncodedTypeSourceInfo());
2370 }
2371 void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
2372   EnqueueChildren(M);
2373   AddTypeLoc(M->getClassReceiverTypeInfo());
2374 }
2375 void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
2376   // Visit the components of the offsetof expression.
2377   for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
2378     typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
2379     const OffsetOfNode &Node = E->getComponent(I-1);
2380     switch (Node.getKind()) {
2381     case OffsetOfNode::Array:
2382       AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
2383       break;
2384     case OffsetOfNode::Field:
2385       AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
2386       break;
2387     case OffsetOfNode::Identifier:
2388     case OffsetOfNode::Base:
2389       continue;
2390     }
2391   }
2392   // Visit the type into which we're computing the offset.
2393   AddTypeLoc(E->getTypeSourceInfo());
2394 }
2395 void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
2396   AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2397   WL.push_back(OverloadExprParts(E, Parent));
2398 }
2399 void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
2400                                         const UnaryExprOrTypeTraitExpr *E) {
2401   EnqueueChildren(E);
2402   if (E->isArgumentType())
2403     AddTypeLoc(E->getArgumentTypeInfo());
2404 }
2405 void EnqueueVisitor::VisitStmt(const Stmt *S) {
2406   EnqueueChildren(S);
2407 }
2408 void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
2409   AddStmt(S->getBody());
2410   AddStmt(S->getCond());
2411   AddDecl(S->getConditionVariable());
2412 }
2413
2414 void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
2415   AddStmt(W->getBody());
2416   AddStmt(W->getCond());
2417   AddDecl(W->getConditionVariable());
2418 }
2419
2420 void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
2421   for (unsigned I = E->getNumArgs(); I > 0; --I)
2422     AddTypeLoc(E->getArg(I-1));
2423 }
2424
2425 void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
2426   AddTypeLoc(E->getQueriedTypeSourceInfo());
2427 }
2428
2429 void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
2430   EnqueueChildren(E);
2431 }
2432
2433 void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
2434   VisitOverloadExpr(U);
2435   if (!U->isImplicitAccess())
2436     AddStmt(U->getBase());
2437 }
2438 void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
2439   AddStmt(E->getSubExpr());
2440   AddTypeLoc(E->getWrittenTypeInfo());
2441 }
2442 void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
2443   WL.push_back(SizeOfPackExprParts(E, Parent));
2444 }
2445 void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2446   // If the opaque value has a source expression, just transparently
2447   // visit that.  This is useful for (e.g.) pseudo-object expressions.
2448   if (Expr *SourceExpr = E->getSourceExpr())
2449     return Visit(SourceExpr);
2450 }
2451 void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
2452   AddStmt(E->getBody());
2453   WL.push_back(LambdaExprParts(E, Parent));
2454 }
2455 void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
2456   // Treat the expression like its syntactic form.
2457   Visit(E->getSyntacticForm());
2458 }
2459
2460 void EnqueueVisitor::VisitOMPExecutableDirective(
2461   const OMPExecutableDirective *D) {
2462   EnqueueChildren(D);
2463   for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
2464                                        E = D->clauses().end();
2465        I != E; ++I)
2466     EnqueueChildren(*I);
2467 }
2468
2469 void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) {
2470   VisitOMPExecutableDirective(D);
2471 }
2472
2473 void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
2474   VisitOMPExecutableDirective(D);
2475 }
2476
2477 void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) {
2478   VisitOMPLoopDirective(D);
2479 }
2480
2481 void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) {
2482   VisitOMPLoopDirective(D);
2483 }
2484
2485 void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) {
2486   VisitOMPLoopDirective(D);
2487 }
2488
2489 void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
2490   VisitOMPExecutableDirective(D);
2491 }
2492
2493 void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
2494   VisitOMPExecutableDirective(D);
2495 }
2496
2497 void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) {
2498   VisitOMPExecutableDirective(D);
2499 }
2500
2501 void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) {
2502   VisitOMPExecutableDirective(D);
2503 }
2504
2505 void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) {
2506   VisitOMPExecutableDirective(D);
2507   AddDeclarationNameInfo(D);
2508 }
2509
2510 void
2511 EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) {
2512   VisitOMPLoopDirective(D);
2513 }
2514
2515 void EnqueueVisitor::VisitOMPParallelForSimdDirective(
2516     const OMPParallelForSimdDirective *D) {
2517   VisitOMPLoopDirective(D);
2518 }
2519
2520 void EnqueueVisitor::VisitOMPParallelSectionsDirective(
2521     const OMPParallelSectionsDirective *D) {
2522   VisitOMPExecutableDirective(D);
2523 }
2524
2525 void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) {
2526   VisitOMPExecutableDirective(D);
2527 }
2528
2529 void
2530 EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) {
2531   VisitOMPExecutableDirective(D);
2532 }
2533
2534 void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) {
2535   VisitOMPExecutableDirective(D);
2536 }
2537
2538 void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) {
2539   VisitOMPExecutableDirective(D);
2540 }
2541
2542 void EnqueueVisitor::VisitOMPTaskgroupDirective(
2543     const OMPTaskgroupDirective *D) {
2544   VisitOMPExecutableDirective(D);
2545 }
2546
2547 void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) {
2548   VisitOMPExecutableDirective(D);
2549 }
2550
2551 void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) {
2552   VisitOMPExecutableDirective(D);
2553 }
2554
2555 void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) {
2556   VisitOMPExecutableDirective(D);
2557 }
2558
2559 void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) {
2560   VisitOMPExecutableDirective(D);
2561 }
2562
2563 void EnqueueVisitor::VisitOMPTargetDataDirective(const 
2564                                                  OMPTargetDataDirective *D) {
2565   VisitOMPExecutableDirective(D);
2566 }
2567
2568 void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) {
2569   VisitOMPExecutableDirective(D);
2570 }
2571
2572 void EnqueueVisitor::VisitOMPCancellationPointDirective(
2573     const OMPCancellationPointDirective *D) {
2574   VisitOMPExecutableDirective(D);
2575 }
2576
2577 void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) {
2578   VisitOMPExecutableDirective(D);
2579 }
2580
2581 void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
2582   EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
2583 }
2584
2585 bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
2586   if (RegionOfInterest.isValid()) {
2587     SourceRange Range = getRawCursorExtent(C);
2588     if (Range.isInvalid() || CompareRegionOfInterest(Range))
2589       return false;
2590   }
2591   return true;
2592 }
2593
2594 bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
2595   while (!WL.empty()) {
2596     // Dequeue the worklist item.
2597     VisitorJob LI = WL.pop_back_val();
2598
2599     // Set the Parent field, then back to its old value once we're done.
2600     SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
2601   
2602     switch (LI.getKind()) {
2603       case VisitorJob::DeclVisitKind: {
2604         const Decl *D = cast<DeclVisit>(&LI)->get();
2605         if (!D)
2606           continue;
2607
2608         // For now, perform default visitation for Decls.
2609         if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
2610                                cast<DeclVisit>(&LI)->isFirst())))
2611             return true;
2612
2613         continue;
2614       }
2615       case VisitorJob::ExplicitTemplateArgsVisitKind: {
2616         const ASTTemplateArgumentListInfo *ArgList =
2617           cast<ExplicitTemplateArgsVisit>(&LI)->get();
2618         for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
2619                *ArgEnd = Arg + ArgList->NumTemplateArgs;
2620                Arg != ArgEnd; ++Arg) {
2621           if (VisitTemplateArgumentLoc(*Arg))
2622             return true;
2623         }
2624         continue;
2625       }
2626       case VisitorJob::TypeLocVisitKind: {
2627         // Perform default visitation for TypeLocs.
2628         if (Visit(cast<TypeLocVisit>(&LI)->get()))
2629           return true;
2630         continue;
2631       }
2632       case VisitorJob::LabelRefVisitKind: {
2633         const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
2634         if (LabelStmt *stmt = LS->getStmt()) {
2635           if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
2636                                        TU))) {
2637             return true;
2638           }
2639         }
2640         continue;
2641       }
2642
2643       case VisitorJob::NestedNameSpecifierLocVisitKind: {
2644         NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
2645         if (VisitNestedNameSpecifierLoc(V->get()))
2646           return true;
2647         continue;
2648       }
2649         
2650       case VisitorJob::DeclarationNameInfoVisitKind: {
2651         if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
2652                                      ->get()))
2653           return true;
2654         continue;
2655       }
2656       case VisitorJob::MemberRefVisitKind: {
2657         MemberRefVisit *V = cast<MemberRefVisit>(&LI);
2658         if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
2659           return true;
2660         continue;
2661       }
2662       case VisitorJob::StmtVisitKind: {
2663         const Stmt *S = cast<StmtVisit>(&LI)->get();
2664         if (!S)
2665           continue;
2666
2667         // Update the current cursor.
2668         CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
2669         if (!IsInRegionOfInterest(Cursor))
2670           continue;
2671         switch (Visitor(Cursor, Parent, ClientData)) {
2672           case CXChildVisit_Break: return true;
2673           case CXChildVisit_Continue: break;
2674           case CXChildVisit_Recurse:
2675             if (PostChildrenVisitor)
2676               WL.push_back(PostChildrenVisit(nullptr, Cursor));
2677             EnqueueWorkList(WL, S);
2678             break;
2679         }
2680         continue;
2681       }
2682       case VisitorJob::MemberExprPartsKind: {
2683         // Handle the other pieces in the MemberExpr besides the base.
2684         const MemberExpr *M = cast<MemberExprParts>(&LI)->get();
2685         
2686         // Visit the nested-name-specifier
2687         if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
2688           if (VisitNestedNameSpecifierLoc(QualifierLoc))
2689             return true;
2690         
2691         // Visit the declaration name.
2692         if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
2693           return true;
2694         
2695         // Visit the explicitly-specified template arguments, if any.
2696         if (M->hasExplicitTemplateArgs()) {
2697           for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
2698                *ArgEnd = Arg + M->getNumTemplateArgs();
2699                Arg != ArgEnd; ++Arg) {
2700             if (VisitTemplateArgumentLoc(*Arg))
2701               return true;
2702           }
2703         }
2704         continue;
2705       }
2706       case VisitorJob::DeclRefExprPartsKind: {
2707         const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
2708         // Visit nested-name-specifier, if present.
2709         if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
2710           if (VisitNestedNameSpecifierLoc(QualifierLoc))
2711             return true;
2712         // Visit declaration name.
2713         if (VisitDeclarationNameInfo(DR->getNameInfo()))
2714           return true;
2715         continue;
2716       }
2717       case VisitorJob::OverloadExprPartsKind: {
2718         const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
2719         // Visit the nested-name-specifier.
2720         if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
2721           if (VisitNestedNameSpecifierLoc(QualifierLoc))
2722             return true;
2723         // Visit the declaration name.
2724         if (VisitDeclarationNameInfo(O->getNameInfo()))
2725           return true;
2726         // Visit the overloaded declaration reference.
2727         if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
2728           return true;
2729         continue;
2730       }
2731       case VisitorJob::SizeOfPackExprPartsKind: {
2732         const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
2733         NamedDecl *Pack = E->getPack();
2734         if (isa<TemplateTypeParmDecl>(Pack)) {
2735           if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
2736                                       E->getPackLoc(), TU)))
2737             return true;
2738           
2739           continue;
2740         }
2741           
2742         if (isa<TemplateTemplateParmDecl>(Pack)) {
2743           if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
2744                                           E->getPackLoc(), TU)))
2745             return true;
2746           
2747           continue;
2748         }
2749         
2750         // Non-type template parameter packs and function parameter packs are
2751         // treated like DeclRefExpr cursors.
2752         continue;
2753       }
2754         
2755       case VisitorJob::LambdaExprPartsKind: {
2756         // Visit captures.
2757         const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
2758         for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
2759                                        CEnd = E->explicit_capture_end();
2760              C != CEnd; ++C) {
2761           // FIXME: Lambda init-captures.
2762           if (!C->capturesVariable())
2763             continue;
2764
2765           if (Visit(MakeCursorVariableRef(C->getCapturedVar(),
2766                                           C->getLocation(),
2767                                           TU)))
2768             return true;
2769         }
2770         
2771         // Visit parameters and return type, if present.
2772         if (E->hasExplicitParameters() || E->hasExplicitResultType()) {
2773           TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2774           if (E->hasExplicitParameters() && E->hasExplicitResultType()) {
2775             // Visit the whole type.
2776             if (Visit(TL))
2777               return true;
2778           } else if (FunctionProtoTypeLoc Proto =
2779                          TL.getAs<FunctionProtoTypeLoc>()) {
2780             if (E->hasExplicitParameters()) {
2781               // Visit parameters.
2782               for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
2783                 if (Visit(MakeCXCursor(Proto.getParam(I), TU)))
2784                   return true;
2785             } else {
2786               // Visit result type.
2787               if (Visit(Proto.getReturnLoc()))
2788                 return true;
2789             }
2790           }
2791         }
2792         break;
2793       }
2794
2795       case VisitorJob::PostChildrenVisitKind:
2796         if (PostChildrenVisitor(Parent, ClientData))
2797           return true;
2798         break;
2799     }
2800   }
2801   return false;
2802 }
2803
2804 bool CursorVisitor::Visit(const Stmt *S) {
2805   VisitorWorkList *WL = nullptr;
2806   if (!WorkListFreeList.empty()) {
2807     WL = WorkListFreeList.back();
2808     WL->clear();
2809     WorkListFreeList.pop_back();
2810   }
2811   else {
2812     WL = new VisitorWorkList();
2813     WorkListCache.push_back(WL);
2814   }
2815   EnqueueWorkList(*WL, S);
2816   bool result = RunVisitorWorkList(*WL);
2817   WorkListFreeList.push_back(WL);
2818   return result;
2819 }
2820
2821 namespace {
2822 typedef SmallVector<SourceRange, 4> RefNamePieces;
2823 RefNamePieces
2824 buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
2825             const DeclarationNameInfo &NI, const SourceRange &QLoc,
2826             const ASTTemplateArgumentListInfo *TemplateArgs = nullptr) {
2827   const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
2828   const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
2829   const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
2830   
2831   const DeclarationName::NameKind Kind = NI.getName().getNameKind();
2832   
2833   RefNamePieces Pieces;
2834
2835   if (WantQualifier && QLoc.isValid())
2836     Pieces.push_back(QLoc);
2837   
2838   if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
2839     Pieces.push_back(NI.getLoc());
2840   
2841   if (WantTemplateArgs && TemplateArgs)
2842     Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc,
2843                                  TemplateArgs->RAngleLoc));
2844   
2845   if (Kind == DeclarationName::CXXOperatorName) {
2846     Pieces.push_back(SourceLocation::getFromRawEncoding(
2847                        NI.getInfo().CXXOperatorName.BeginOpNameLoc));
2848     Pieces.push_back(SourceLocation::getFromRawEncoding(
2849                        NI.getInfo().CXXOperatorName.EndOpNameLoc));
2850   }
2851   
2852   if (WantSinglePiece) {
2853     SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
2854     Pieces.clear();
2855     Pieces.push_back(R);
2856   }  
2857
2858   return Pieces;  
2859 }
2860 }
2861
2862 //===----------------------------------------------------------------------===//
2863 // Misc. API hooks.
2864 //===----------------------------------------------------------------------===//               
2865
2866 static void fatal_error_handler(void *user_data, const std::string& reason,
2867                                 bool gen_crash_diag) {
2868   // Write the result out to stderr avoiding errs() because raw_ostreams can
2869   // call report_fatal_error.
2870   fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str());
2871   ::abort();
2872 }
2873
2874 namespace {
2875 struct RegisterFatalErrorHandler {
2876   RegisterFatalErrorHandler() {
2877     llvm::install_fatal_error_handler(fatal_error_handler, nullptr);
2878   }
2879 };
2880 }
2881
2882 static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce;
2883
2884 extern "C" {
2885 CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
2886                           int displayDiagnostics) {
2887   // We use crash recovery to make some of our APIs more reliable, implicitly
2888   // enable it.
2889   if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
2890     llvm::CrashRecoveryContext::Enable();
2891
2892   // Look through the managed static to trigger construction of the managed
2893   // static which registers our fatal error handler. This ensures it is only
2894   // registered once.
2895   (void)*RegisterFatalErrorHandlerOnce;
2896
2897   // Initialize targets for clang module support.
2898   llvm::InitializeAllTargets();
2899   llvm::InitializeAllTargetMCs();
2900   llvm::InitializeAllAsmPrinters();
2901   llvm::InitializeAllAsmParsers();
2902
2903   CIndexer *CIdxr = new CIndexer();
2904
2905   if (excludeDeclarationsFromPCH)
2906     CIdxr->setOnlyLocalDecls();
2907   if (displayDiagnostics)
2908     CIdxr->setDisplayDiagnostics();
2909
2910   if (getenv("LIBCLANG_BGPRIO_INDEX"))
2911     CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
2912                                CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
2913   if (getenv("LIBCLANG_BGPRIO_EDIT"))
2914     CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
2915                                CXGlobalOpt_ThreadBackgroundPriorityForEditing);
2916
2917   return CIdxr;
2918 }
2919
2920 void clang_disposeIndex(CXIndex CIdx) {
2921   if (CIdx)
2922     delete static_cast<CIndexer *>(CIdx);
2923 }
2924
2925 void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
2926   if (CIdx)
2927     static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
2928 }
2929
2930 unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
2931   if (CIdx)
2932     return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
2933   return 0;
2934 }
2935
2936 void clang_toggleCrashRecovery(unsigned isEnabled) {
2937   if (isEnabled)
2938     llvm::CrashRecoveryContext::Enable();
2939   else
2940     llvm::CrashRecoveryContext::Disable();
2941 }
2942
2943 CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
2944                                               const char *ast_filename) {
2945   CXTranslationUnit TU;
2946   enum CXErrorCode Result =
2947       clang_createTranslationUnit2(CIdx, ast_filename, &TU);
2948   (void)Result;
2949   assert((TU && Result == CXError_Success) ||
2950          (!TU && Result != CXError_Success));
2951   return TU;
2952 }
2953
2954 enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
2955                                               const char *ast_filename,
2956                                               CXTranslationUnit *out_TU) {
2957   if (out_TU)
2958     *out_TU = nullptr;
2959
2960   if (!CIdx || !ast_filename || !out_TU)
2961     return CXError_InvalidArguments;
2962
2963   LOG_FUNC_SECTION {
2964     *Log << ast_filename;
2965   }
2966
2967   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2968   FileSystemOptions FileSystemOpts;
2969
2970   IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
2971       CompilerInstance::createDiagnostics(new DiagnosticOptions());
2972   std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
2973       ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), Diags,
2974       FileSystemOpts, /*UseDebugInfo=*/false,
2975       CXXIdx->getOnlyLocalDecls(), None,
2976       /*CaptureDiagnostics=*/true,
2977       /*AllowPCHWithCompilerErrors=*/true,
2978       /*UserFilesAreVolatile=*/true);
2979   *out_TU = MakeCXTranslationUnit(CXXIdx, AU.release());
2980   return *out_TU ? CXError_Success : CXError_Failure;
2981 }
2982
2983 unsigned clang_defaultEditingTranslationUnitOptions() {
2984   return CXTranslationUnit_PrecompiledPreamble | 
2985          CXTranslationUnit_CacheCompletionResults;
2986 }
2987
2988 CXTranslationUnit
2989 clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
2990                                           const char *source_filename,
2991                                           int num_command_line_args,
2992                                           const char * const *command_line_args,
2993                                           unsigned num_unsaved_files,
2994                                           struct CXUnsavedFile *unsaved_files) {
2995   unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
2996   return clang_parseTranslationUnit(CIdx, source_filename,
2997                                     command_line_args, num_command_line_args,
2998                                     unsaved_files, num_unsaved_files,
2999                                     Options);
3000 }
3001
3002 static CXErrorCode
3003 clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename,
3004                                 const char *const *command_line_args,
3005                                 int num_command_line_args,
3006                                 ArrayRef<CXUnsavedFile> unsaved_files,
3007                                 unsigned options, CXTranslationUnit *out_TU) {
3008   // Set up the initial return values.
3009   if (out_TU)
3010     *out_TU = nullptr;
3011
3012   // Check arguments.
3013   if (!CIdx || !out_TU)
3014     return CXError_InvalidArguments;
3015
3016   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
3017
3018   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
3019     setThreadBackgroundPriority();
3020
3021   bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
3022   // FIXME: Add a flag for modules.
3023   TranslationUnitKind TUKind
3024     = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
3025   bool CacheCodeCompletionResults
3026     = options & CXTranslationUnit_CacheCompletionResults;
3027   bool IncludeBriefCommentsInCodeCompletion
3028     = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
3029   bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies;
3030   bool ForSerialization = options & CXTranslationUnit_ForSerialization;
3031
3032   // Configure the diagnostics.
3033   IntrusiveRefCntPtr<DiagnosticsEngine>
3034     Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
3035
3036   // Recover resources if we crash before exiting this function.
3037   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
3038     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
3039     DiagCleanup(Diags.get());
3040
3041   std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
3042       new std::vector<ASTUnit::RemappedFile>());
3043
3044   // Recover resources if we crash before exiting this function.
3045   llvm::CrashRecoveryContextCleanupRegistrar<
3046     std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
3047
3048   for (auto &UF : unsaved_files) {
3049     std::unique_ptr<llvm::MemoryBuffer> MB =
3050         llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
3051     RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
3052   }
3053
3054   std::unique_ptr<std::vector<const char *>> Args(
3055       new std::vector<const char *>());
3056
3057   // Recover resources if we crash before exiting this method.
3058   llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
3059     ArgsCleanup(Args.get());
3060
3061   // Since the Clang C library is primarily used by batch tools dealing with
3062   // (often very broken) source code, where spell-checking can have a
3063   // significant negative impact on performance (particularly when 
3064   // precompiled headers are involved), we disable it by default.
3065   // Only do this if we haven't found a spell-checking-related argument.
3066   bool FoundSpellCheckingArgument = false;
3067   for (int I = 0; I != num_command_line_args; ++I) {
3068     if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
3069         strcmp(command_line_args[I], "-fspell-checking") == 0) {
3070       FoundSpellCheckingArgument = true;
3071       break;
3072     }
3073   }
3074   if (!FoundSpellCheckingArgument)
3075     Args->push_back("-fno-spell-checking");
3076   
3077   Args->insert(Args->end(), command_line_args,
3078                command_line_args + num_command_line_args);
3079
3080   // The 'source_filename' argument is optional.  If the caller does not
3081   // specify it then it is assumed that the source file is specified
3082   // in the actual argument list.
3083   // Put the source file after command_line_args otherwise if '-x' flag is
3084   // present it will be unused.
3085   if (source_filename)
3086     Args->push_back(source_filename);
3087
3088   // Do we need the detailed preprocessing record?
3089   if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
3090     Args->push_back("-Xclang");
3091     Args->push_back("-detailed-preprocessing-record");
3092   }
3093   
3094   unsigned NumErrors = Diags->getClient()->getNumErrors();
3095   std::unique_ptr<ASTUnit> ErrUnit;
3096   std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine(
3097       Args->data(), Args->data() + Args->size(),
3098       CXXIdx->getPCHContainerOperations(), Diags,
3099       CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(),
3100       /*CaptureDiagnostics=*/true, *RemappedFiles.get(),
3101       /*RemappedFilesKeepOriginalName=*/true, PrecompilePreamble, TUKind,
3102       CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
3103       /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies,
3104       /*UserFilesAreVolatile=*/true, ForSerialization, &ErrUnit));
3105
3106   // Early failures in LoadFromCommandLine may return with ErrUnit unset.
3107   if (!Unit && !ErrUnit)
3108     return CXError_ASTReadError;
3109
3110   if (NumErrors != Diags->getClient()->getNumErrors()) {
3111     // Make sure to check that 'Unit' is non-NULL.
3112     if (CXXIdx->getDisplayDiagnostics())
3113       printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get());
3114   }
3115
3116   if (isASTReadError(Unit ? Unit.get() : ErrUnit.get()))
3117     return CXError_ASTReadError;
3118
3119   *out_TU = MakeCXTranslationUnit(CXXIdx, Unit.release());
3120   return *out_TU ? CXError_Success : CXError_Failure;
3121 }
3122
3123 CXTranslationUnit
3124 clang_parseTranslationUnit(CXIndex CIdx,
3125                            const char *source_filename,
3126                            const char *const *command_line_args,
3127                            int num_command_line_args,
3128                            struct CXUnsavedFile *unsaved_files,
3129                            unsigned num_unsaved_files,
3130                            unsigned options) {
3131   CXTranslationUnit TU;
3132   enum CXErrorCode Result = clang_parseTranslationUnit2(
3133       CIdx, source_filename, command_line_args, num_command_line_args,
3134       unsaved_files, num_unsaved_files, options, &TU);
3135   (void)Result;
3136   assert((TU && Result == CXError_Success) ||
3137          (!TU && Result != CXError_Success));
3138   return TU;
3139 }
3140
3141 enum CXErrorCode clang_parseTranslationUnit2(
3142     CXIndex CIdx,
3143     const char *source_filename,
3144     const char *const *command_line_args,
3145     int num_command_line_args,
3146     struct CXUnsavedFile *unsaved_files,
3147     unsigned num_unsaved_files,
3148     unsigned options,
3149     CXTranslationUnit *out_TU) {
3150   LOG_FUNC_SECTION {
3151     *Log << source_filename << ": ";
3152     for (int i = 0; i != num_command_line_args; ++i)
3153       *Log << command_line_args[i] << " ";
3154   }
3155
3156   if (num_unsaved_files && !unsaved_files)
3157     return CXError_InvalidArguments;
3158
3159   CXErrorCode result = CXError_Failure;
3160   auto ParseTranslationUnitImpl = [=, &result] {
3161     result = clang_parseTranslationUnit_Impl(
3162         CIdx, source_filename, command_line_args, num_command_line_args,
3163         llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU);
3164   };
3165   llvm::CrashRecoveryContext CRC;
3166
3167   if (!RunSafely(CRC, ParseTranslationUnitImpl)) {
3168     fprintf(stderr, "libclang: crash detected during parsing: {\n");
3169     fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
3170     fprintf(stderr, "  'command_line_args' : [");
3171     for (int i = 0; i != num_command_line_args; ++i) {
3172       if (i)
3173         fprintf(stderr, ", ");
3174       fprintf(stderr, "'%s'", command_line_args[i]);
3175     }
3176     fprintf(stderr, "],\n");
3177     fprintf(stderr, "  'unsaved_files' : [");
3178     for (unsigned i = 0; i != num_unsaved_files; ++i) {
3179       if (i)
3180         fprintf(stderr, ", ");
3181       fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
3182               unsaved_files[i].Length);
3183     }
3184     fprintf(stderr, "],\n");
3185     fprintf(stderr, "  'options' : %d,\n", options);
3186     fprintf(stderr, "}\n");
3187
3188     return CXError_Crashed;
3189   } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
3190     if (CXTranslationUnit *TU = out_TU)
3191       PrintLibclangResourceUsage(*TU);
3192   }
3193
3194   return result;
3195 }
3196
3197 unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
3198   return CXSaveTranslationUnit_None;
3199 }  
3200
3201 static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU,
3202                                                   const char *FileName,
3203                                                   unsigned options) {
3204   CIndexer *CXXIdx = TU->CIdx;
3205   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
3206     setThreadBackgroundPriority();
3207
3208   bool hadError = cxtu::getASTUnit(TU)->Save(FileName);
3209   return hadError ? CXSaveError_Unknown : CXSaveError_None;
3210 }
3211
3212 int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
3213                               unsigned options) {
3214   LOG_FUNC_SECTION {
3215     *Log << TU << ' ' << FileName;
3216   }
3217
3218   if (isNotUsableTU(TU)) {
3219     LOG_BAD_TU(TU);
3220     return CXSaveError_InvalidTU;
3221   }
3222
3223   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
3224   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3225   if (!CXXUnit->hasSema())
3226     return CXSaveError_InvalidTU;
3227
3228   CXSaveError result;
3229   auto SaveTranslationUnitImpl = [=, &result]() {
3230     result = clang_saveTranslationUnit_Impl(TU, FileName, options);
3231   };
3232
3233   if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() ||
3234       getenv("LIBCLANG_NOTHREADS")) {
3235     SaveTranslationUnitImpl();
3236
3237     if (getenv("LIBCLANG_RESOURCE_USAGE"))
3238       PrintLibclangResourceUsage(TU);
3239
3240     return result;
3241   }
3242
3243   // We have an AST that has invalid nodes due to compiler errors.
3244   // Use a crash recovery thread for protection.
3245
3246   llvm::CrashRecoveryContext CRC;
3247
3248   if (!RunSafely(CRC, SaveTranslationUnitImpl)) {
3249     fprintf(stderr, "libclang: crash detected during AST saving: {\n");
3250     fprintf(stderr, "  'filename' : '%s'\n", FileName);
3251     fprintf(stderr, "  'options' : %d,\n", options);
3252     fprintf(stderr, "}\n");
3253
3254     return CXSaveError_Unknown;
3255
3256   } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
3257     PrintLibclangResourceUsage(TU);
3258   }
3259
3260   return result;
3261 }
3262
3263 void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
3264   if (CTUnit) {
3265     // If the translation unit has been marked as unsafe to free, just discard
3266     // it.
3267     ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
3268     if (Unit && Unit->isUnsafeToFree())
3269       return;
3270
3271     delete cxtu::getASTUnit(CTUnit);
3272     delete CTUnit->StringPool;
3273     delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
3274     disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
3275     delete CTUnit->CommentToXML;
3276     delete CTUnit;
3277   }
3278 }
3279
3280 unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
3281   return CXReparse_None;
3282 }
3283
3284 static CXErrorCode
3285 clang_reparseTranslationUnit_Impl(CXTranslationUnit TU,
3286                                   ArrayRef<CXUnsavedFile> unsaved_files,
3287                                   unsigned options) {
3288   // Check arguments.
3289   if (isNotUsableTU(TU)) {
3290     LOG_BAD_TU(TU);
3291     return CXError_InvalidArguments;
3292   }
3293
3294   // Reset the associated diagnostics.
3295   delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
3296   TU->Diagnostics = nullptr;
3297
3298   CIndexer *CXXIdx = TU->CIdx;
3299   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
3300     setThreadBackgroundPriority();
3301
3302   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
3303   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3304
3305   std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
3306       new std::vector<ASTUnit::RemappedFile>());
3307
3308   // Recover resources if we crash before exiting this function.
3309   llvm::CrashRecoveryContextCleanupRegistrar<
3310     std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
3311
3312   for (auto &UF : unsaved_files) {
3313     std::unique_ptr<llvm::MemoryBuffer> MB =
3314         llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
3315     RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
3316   }
3317
3318   if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(),
3319                         *RemappedFiles.get()))
3320     return CXError_Success;
3321   if (isASTReadError(CXXUnit))
3322     return CXError_ASTReadError;
3323   return CXError_Failure;
3324 }
3325
3326 int clang_reparseTranslationUnit(CXTranslationUnit TU,
3327                                  unsigned num_unsaved_files,
3328                                  struct CXUnsavedFile *unsaved_files,
3329                                  unsigned options) {
3330   LOG_FUNC_SECTION {
3331     *Log << TU;
3332   }
3333
3334   if (num_unsaved_files && !unsaved_files)
3335     return CXError_InvalidArguments;
3336
3337   CXErrorCode result;
3338   auto ReparseTranslationUnitImpl = [=, &result]() {
3339     result = clang_reparseTranslationUnit_Impl(
3340         TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options);
3341   };
3342
3343   if (getenv("LIBCLANG_NOTHREADS")) {
3344     ReparseTranslationUnitImpl();
3345     return result;
3346   }
3347
3348   llvm::CrashRecoveryContext CRC;
3349
3350   if (!RunSafely(CRC, ReparseTranslationUnitImpl)) {
3351     fprintf(stderr, "libclang: crash detected during reparsing\n");
3352     cxtu::getASTUnit(TU)->setUnsafeToFree(true);
3353     return CXError_Crashed;
3354   } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
3355     PrintLibclangResourceUsage(TU);
3356
3357   return result;
3358 }
3359
3360
3361 CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
3362   if (isNotUsableTU(CTUnit)) {
3363     LOG_BAD_TU(CTUnit);
3364     return cxstring::createEmpty();
3365   }
3366
3367   ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
3368   return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
3369 }
3370
3371 CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
3372   if (isNotUsableTU(TU)) {
3373     LOG_BAD_TU(TU);
3374     return clang_getNullCursor();
3375   }
3376
3377   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
3378   return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
3379 }
3380
3381 } // end: extern "C"
3382
3383 //===----------------------------------------------------------------------===//
3384 // CXFile Operations.
3385 //===----------------------------------------------------------------------===//
3386
3387 extern "C" {
3388 CXString clang_getFileName(CXFile SFile) {
3389   if (!SFile)
3390     return cxstring::createNull();
3391
3392   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
3393   return cxstring::createRef(FEnt->getName());
3394 }
3395
3396 time_t clang_getFileTime(CXFile SFile) {
3397   if (!SFile)
3398     return 0;
3399
3400   FileEntry *FEnt = static_cast<FileEntry *>(SFile);
3401   return FEnt->getModificationTime();
3402 }
3403
3404 CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
3405   if (isNotUsableTU(TU)) {
3406     LOG_BAD_TU(TU);
3407     return nullptr;
3408   }
3409
3410   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
3411
3412   FileManager &FMgr = CXXUnit->getFileManager();
3413   return const_cast<FileEntry *>(FMgr.getFile(file_name));
3414 }
3415
3416 unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU,
3417                                             CXFile file) {
3418   if (isNotUsableTU(TU)) {
3419     LOG_BAD_TU(TU);
3420     return 0;
3421   }
3422
3423   if (!file)
3424     return 0;
3425
3426   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
3427   FileEntry *FEnt = static_cast<FileEntry *>(file);
3428   return CXXUnit->getPreprocessor().getHeaderSearchInfo()
3429                                           .isFileMultipleIncludeGuarded(FEnt);
3430 }
3431
3432 int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
3433   if (!file || !outID)
3434     return 1;
3435
3436   FileEntry *FEnt = static_cast<FileEntry *>(file);
3437   const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
3438   outID->data[0] = ID.getDevice();
3439   outID->data[1] = ID.getFile();
3440   outID->data[2] = FEnt->getModificationTime();
3441   return 0;
3442 }
3443
3444 int clang_File_isEqual(CXFile file1, CXFile file2) {
3445   if (file1 == file2)
3446     return true;
3447
3448   if (!file1 || !file2)
3449     return false;
3450
3451   FileEntry *FEnt1 = static_cast<FileEntry *>(file1);
3452   FileEntry *FEnt2 = static_cast<FileEntry *>(file2);
3453   return FEnt1->getUniqueID() == FEnt2->getUniqueID();
3454 }
3455
3456 } // end: extern "C"
3457
3458 //===----------------------------------------------------------------------===//
3459 // CXCursor Operations.
3460 //===----------------------------------------------------------------------===//
3461
3462 static const Decl *getDeclFromExpr(const Stmt *E) {
3463   if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
3464     return getDeclFromExpr(CE->getSubExpr());
3465
3466   if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
3467     return RefExpr->getDecl();
3468   if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
3469     return ME->getMemberDecl();
3470   if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
3471     return RE->getDecl();
3472   if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) {
3473     if (PRE->isExplicitProperty())
3474       return PRE->getExplicitProperty();
3475     // It could be messaging both getter and setter as in:
3476     // ++myobj.myprop;
3477     // in which case prefer to associate the setter since it is less obvious
3478     // from inspecting the source that the setter is going to get called.
3479     if (PRE->isMessagingSetter())
3480       return PRE->getImplicitPropertySetter();
3481     return PRE->getImplicitPropertyGetter();
3482   }
3483   if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
3484     return getDeclFromExpr(POE->getSyntacticForm());
3485   if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
3486     if (Expr *Src = OVE->getSourceExpr())
3487       return getDeclFromExpr(Src);
3488       
3489   if (const CallExpr *CE = dyn_cast<CallExpr>(E))
3490     return getDeclFromExpr(CE->getCallee());
3491   if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
3492     if (!CE->isElidable())
3493     return CE->getConstructor();
3494   if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
3495     return OME->getMethodDecl();
3496
3497   if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
3498     return PE->getProtocol();
3499   if (const SubstNonTypeTemplateParmPackExpr *NTTP
3500                               = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
3501     return NTTP->getParameterPack();
3502   if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
3503     if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || 
3504         isa<ParmVarDecl>(SizeOfPack->getPack()))
3505       return SizeOfPack->getPack();
3506
3507   return nullptr;
3508 }
3509
3510 static SourceLocation getLocationFromExpr(const Expr *E) {
3511   if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
3512     return getLocationFromExpr(CE->getSubExpr());
3513
3514   if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
3515     return /*FIXME:*/Msg->getLeftLoc();
3516   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
3517     return DRE->getLocation();
3518   if (const MemberExpr *Member = dyn_cast<MemberExpr>(E))
3519     return Member->getMemberLoc();
3520   if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
3521     return Ivar->getLocation();
3522   if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
3523     return SizeOfPack->getPackLoc();
3524   if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E))
3525     return PropRef->getLocation();
3526   
3527   return E->getLocStart();
3528 }
3529
3530 extern "C" {
3531
3532 unsigned clang_visitChildren(CXCursor parent,
3533                              CXCursorVisitor visitor,
3534                              CXClientData client_data) {
3535   CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
3536                           /*VisitPreprocessorLast=*/false);
3537   return CursorVis.VisitChildren(parent);
3538 }
3539
3540 #ifndef __has_feature
3541 #define __has_feature(x) 0
3542 #endif
3543 #if __has_feature(blocks)
3544 typedef enum CXChildVisitResult 
3545      (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
3546
3547 static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
3548     CXClientData client_data) {
3549   CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
3550   return block(cursor, parent);
3551 }
3552 #else
3553 // If we are compiled with a compiler that doesn't have native blocks support,
3554 // define and call the block manually, so the 
3555 typedef struct _CXChildVisitResult
3556 {
3557         void *isa;
3558         int flags;
3559         int reserved;
3560         enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
3561                                          CXCursor);
3562 } *CXCursorVisitorBlock;
3563
3564 static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
3565     CXClientData client_data) {
3566   CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
3567   return block->invoke(block, cursor, parent);
3568 }
3569 #endif
3570
3571
3572 unsigned clang_visitChildrenWithBlock(CXCursor parent,
3573                                       CXCursorVisitorBlock block) {
3574   return clang_visitChildren(parent, visitWithBlock, block);
3575 }
3576
3577 static CXString getDeclSpelling(const Decl *D) {
3578   if (!D)
3579     return cxstring::createEmpty();
3580
3581   const NamedDecl *ND = dyn_cast<NamedDecl>(D);
3582   if (!ND) {
3583     if (const ObjCPropertyImplDecl *PropImpl =
3584             dyn_cast<ObjCPropertyImplDecl>(D))
3585       if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
3586         return cxstring::createDup(Property->getIdentifier()->getName());
3587     
3588     if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D))
3589       if (Module *Mod = ImportD->getImportedModule())
3590         return cxstring::createDup(Mod->getFullModuleName());
3591
3592     return cxstring::createEmpty();
3593   }
3594   
3595   if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
3596     return cxstring::createDup(OMD->getSelector().getAsString());
3597
3598   if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
3599     // No, this isn't the same as the code below. getIdentifier() is non-virtual
3600     // and returns different names. NamedDecl returns the class name and
3601     // ObjCCategoryImplDecl returns the category name.
3602     return cxstring::createRef(CIMP->getIdentifier()->getNameStart());
3603
3604   if (isa<UsingDirectiveDecl>(D))
3605     return cxstring::createEmpty();
3606   
3607   SmallString<1024> S;
3608   llvm::raw_svector_ostream os(S);
3609   ND->printName(os);
3610   
3611   return cxstring::createDup(os.str());
3612 }
3613
3614 CXString clang_getCursorSpelling(CXCursor C) {
3615   if (clang_isTranslationUnit(C.kind))
3616     return clang_getTranslationUnitSpelling(getCursorTU(C));
3617
3618   if (clang_isReference(C.kind)) {
3619     switch (C.kind) {
3620     case CXCursor_ObjCSuperClassRef: {
3621       const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
3622       return cxstring::createRef(Super->getIdentifier()->getNameStart());
3623     }
3624     case CXCursor_ObjCClassRef: {
3625       const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
3626       return cxstring::createRef(Class->getIdentifier()->getNameStart());
3627     }
3628     case CXCursor_ObjCProtocolRef: {
3629       const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
3630       assert(OID && "getCursorSpelling(): Missing protocol decl");
3631       return cxstring::createRef(OID->getIdentifier()->getNameStart());
3632     }
3633     case CXCursor_CXXBaseSpecifier: {
3634       const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
3635       return cxstring::createDup(B->getType().getAsString());
3636     }
3637     case CXCursor_TypeRef: {
3638       const TypeDecl *Type = getCursorTypeRef(C).first;
3639       assert(Type && "Missing type decl");
3640
3641       return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type).
3642                               getAsString());
3643     }
3644     case CXCursor_TemplateRef: {
3645       const TemplateDecl *Template = getCursorTemplateRef(C).first;
3646       assert(Template && "Missing template decl");
3647       
3648       return cxstring::createDup(Template->getNameAsString());
3649     }
3650         
3651     case CXCursor_NamespaceRef: {
3652       const NamedDecl *NS = getCursorNamespaceRef(C).first;
3653       assert(NS && "Missing namespace decl");
3654       
3655       return cxstring::createDup(NS->getNameAsString());
3656     }
3657
3658     case CXCursor_MemberRef: {
3659       const FieldDecl *Field = getCursorMemberRef(C).first;
3660       assert(Field && "Missing member decl");
3661       
3662       return cxstring::createDup(Field->getNameAsString());
3663     }
3664
3665     case CXCursor_LabelRef: {
3666       const LabelStmt *Label = getCursorLabelRef(C).first;
3667       assert(Label && "Missing label");
3668       
3669       return cxstring::createRef(Label->getName());
3670     }
3671
3672     case CXCursor_OverloadedDeclRef: {
3673       OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
3674       if (const Decl *D = Storage.dyn_cast<const Decl *>()) {
3675         if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
3676           return cxstring::createDup(ND->getNameAsString());
3677         return cxstring::createEmpty();
3678       }
3679       if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
3680         return cxstring::createDup(E->getName().getAsString());
3681       OverloadedTemplateStorage *Ovl
3682         = Storage.get<OverloadedTemplateStorage*>();
3683       if (Ovl->size() == 0)
3684         return cxstring::createEmpty();
3685       return cxstring::createDup((*Ovl->begin())->getNameAsString());
3686     }
3687         
3688     case CXCursor_VariableRef: {
3689       const VarDecl *Var = getCursorVariableRef(C).first;
3690       assert(Var && "Missing variable decl");
3691       
3692       return cxstring::createDup(Var->getNameAsString());
3693     }
3694         
3695     default:
3696       return cxstring::createRef("<not implemented>");
3697     }
3698   }
3699
3700   if (clang_isExpression(C.kind)) {
3701     const Expr *E = getCursorExpr(C);
3702
3703     if (C.kind == CXCursor_ObjCStringLiteral ||
3704         C.kind == CXCursor_StringLiteral) {
3705       const StringLiteral *SLit;
3706       if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) {
3707         SLit = OSL->getString();
3708       } else {
3709         SLit = cast<StringLiteral>(E);
3710       }
3711       SmallString<256> Buf;
3712       llvm::raw_svector_ostream OS(Buf);
3713       SLit->outputString(OS);
3714       return cxstring::createDup(OS.str());
3715     }
3716
3717     const Decl *D = getDeclFromExpr(getCursorExpr(C));
3718     if (D)
3719       return getDeclSpelling(D);
3720     return cxstring::createEmpty();
3721   }
3722
3723   if (clang_isStatement(C.kind)) {
3724     const Stmt *S = getCursorStmt(C);
3725     if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
3726       return cxstring::createRef(Label->getName());
3727
3728     return cxstring::createEmpty();
3729   }
3730   
3731   if (C.kind == CXCursor_MacroExpansion)
3732     return cxstring::createRef(getCursorMacroExpansion(C).getName()
3733                                                            ->getNameStart());
3734
3735   if (C.kind == CXCursor_MacroDefinition)
3736     return cxstring::createRef(getCursorMacroDefinition(C)->getName()
3737                                                            ->getNameStart());
3738
3739   if (C.kind == CXCursor_InclusionDirective)
3740     return cxstring::createDup(getCursorInclusionDirective(C)->getFileName());
3741       
3742   if (clang_isDeclaration(C.kind))
3743     return getDeclSpelling(getCursorDecl(C));
3744
3745   if (C.kind == CXCursor_AnnotateAttr) {
3746     const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
3747     return cxstring::createDup(AA->getAnnotation());
3748   }
3749
3750   if (C.kind == CXCursor_AsmLabelAttr) {
3751     const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
3752     return cxstring::createDup(AA->getLabel());
3753   }
3754
3755   if (C.kind == CXCursor_PackedAttr) {
3756     return cxstring::createRef("packed");
3757   }
3758
3759   return cxstring::createEmpty();
3760 }
3761
3762 CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C,
3763                                                 unsigned pieceIndex,
3764                                                 unsigned options) {
3765   if (clang_Cursor_isNull(C))
3766     return clang_getNullRange();
3767
3768   ASTContext &Ctx = getCursorContext(C);
3769
3770   if (clang_isStatement(C.kind)) {
3771     const Stmt *S = getCursorStmt(C);
3772     if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) {
3773       if (pieceIndex > 0)
3774         return clang_getNullRange();
3775       return cxloc::translateSourceRange(Ctx, Label->getIdentLoc());
3776     }
3777
3778     return clang_getNullRange();
3779   }
3780
3781   if (C.kind == CXCursor_ObjCMessageExpr) {
3782     if (const ObjCMessageExpr *
3783           ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) {
3784       if (pieceIndex >= ME->getNumSelectorLocs())
3785         return clang_getNullRange();
3786       return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex));
3787     }
3788   }
3789
3790   if (C.kind == CXCursor_ObjCInstanceMethodDecl ||
3791       C.kind == CXCursor_ObjCClassMethodDecl) {
3792     if (const ObjCMethodDecl *
3793           MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) {
3794       if (pieceIndex >= MD->getNumSelectorLocs())
3795         return clang_getNullRange();
3796       return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex));
3797     }
3798   }
3799
3800   if (C.kind == CXCursor_ObjCCategoryDecl ||
3801       C.kind == CXCursor_ObjCCategoryImplDecl) {
3802     if (pieceIndex > 0)
3803       return clang_getNullRange();
3804     if (const ObjCCategoryDecl *
3805           CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C)))
3806       return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc());
3807     if (const ObjCCategoryImplDecl *
3808           CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C)))
3809       return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc());
3810   }
3811
3812   if (C.kind == CXCursor_ModuleImportDecl) {
3813     if (pieceIndex > 0)
3814       return clang_getNullRange();
3815     if (const ImportDecl *ImportD =
3816             dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) {
3817       ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs();
3818       if (!Locs.empty())
3819         return cxloc::translateSourceRange(Ctx,
3820                                          SourceRange(Locs.front(), Locs.back()));
3821     }
3822     return clang_getNullRange();
3823   }
3824
3825   if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor ||
3826       C.kind == CXCursor_ConversionFunction) {
3827     if (pieceIndex > 0)
3828       return clang_getNullRange();
3829     if (const FunctionDecl *FD =
3830             dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) {
3831       DeclarationNameInfo FunctionName = FD->getNameInfo();
3832       return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange());
3833     }
3834     return clang_getNullRange();
3835   }
3836
3837   // FIXME: A CXCursor_InclusionDirective should give the location of the
3838   // filename, but we don't keep track of this.
3839
3840   // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation
3841   // but we don't keep track of this.
3842
3843   // FIXME: A CXCursor_AsmLabelAttr should give the location of the label
3844   // but we don't keep track of this.
3845
3846   // Default handling, give the location of the cursor.
3847
3848   if (pieceIndex > 0)
3849     return clang_getNullRange();
3850
3851   CXSourceLocation CXLoc = clang_getCursorLocation(C);
3852   SourceLocation Loc = cxloc::translateSourceLocation(CXLoc);
3853   return cxloc::translateSourceRange(Ctx, Loc);
3854 }
3855
3856 CXString clang_Cursor_getMangling(CXCursor C) {
3857   if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
3858     return cxstring::createEmpty();
3859
3860   // Mangling only works for functions and variables.
3861   const Decl *D = getCursorDecl(C);
3862   if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))
3863     return cxstring::createEmpty();
3864
3865   // First apply frontend mangling.
3866   const NamedDecl *ND = cast<NamedDecl>(D);
3867   ASTContext &Ctx = ND->getASTContext();
3868   std::unique_ptr<MangleContext> MC(Ctx.createMangleContext());
3869
3870   std::string FrontendBuf;
3871   llvm::raw_string_ostream FrontendBufOS(FrontendBuf);
3872   MC->mangleName(ND, FrontendBufOS);
3873
3874   // Now apply backend mangling.
3875   std::unique_ptr<llvm::DataLayout> DL(
3876       new llvm::DataLayout(Ctx.getTargetInfo().getDataLayoutString()));
3877
3878   std::string FinalBuf;
3879   llvm::raw_string_ostream FinalBufOS(FinalBuf);
3880   llvm::Mangler::getNameWithPrefix(FinalBufOS, llvm::Twine(FrontendBufOS.str()),
3881                                    *DL);
3882
3883   return cxstring::createDup(FinalBufOS.str());
3884 }
3885
3886 CXString clang_getCursorDisplayName(CXCursor C) {
3887   if (!clang_isDeclaration(C.kind))
3888     return clang_getCursorSpelling(C);
3889   
3890   const Decl *D = getCursorDecl(C);
3891   if (!D)
3892     return cxstring::createEmpty();
3893
3894   PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
3895   if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
3896     D = FunTmpl->getTemplatedDecl();
3897   
3898   if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
3899     SmallString<64> Str;
3900     llvm::raw_svector_ostream OS(Str);
3901     OS << *Function;
3902     if (Function->getPrimaryTemplate())
3903       OS << "<>";
3904     OS << "(";
3905     for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
3906       if (I)
3907         OS << ", ";
3908       OS << Function->getParamDecl(I)->getType().getAsString(Policy);
3909     }
3910     
3911     if (Function->isVariadic()) {
3912       if (Function->getNumParams())
3913         OS << ", ";
3914       OS << "...";
3915     }
3916     OS << ")";
3917     return cxstring::createDup(OS.str());
3918   }
3919   
3920   if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
3921     SmallString<64> Str;
3922     llvm::raw_svector_ostream OS(Str);
3923     OS << *ClassTemplate;
3924     OS << "<";
3925     TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
3926     for (unsigned I = 0, N = Params->size(); I != N; ++I) {
3927       if (I)
3928         OS << ", ";
3929       
3930       NamedDecl *Param = Params->getParam(I);
3931       if (Param->getIdentifier()) {
3932         OS << Param->getIdentifier()->getName();
3933         continue;
3934       }
3935       
3936       // There is no parameter name, which makes this tricky. Try to come up
3937       // with something useful that isn't too long.
3938       if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3939         OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
3940       else if (NonTypeTemplateParmDecl *NTTP
3941                                     = dyn_cast<NonTypeTemplateParmDecl>(Param))
3942         OS << NTTP->getType().getAsString(Policy);
3943       else
3944         OS << "template<...> class";
3945     }
3946     
3947     OS << ">";
3948     return cxstring::createDup(OS.str());
3949   }
3950   
3951   if (const ClassTemplateSpecializationDecl *ClassSpec
3952                               = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3953     // If the type was explicitly written, use that.
3954     if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
3955       return cxstring::createDup(TSInfo->getType().getAsString(Policy));
3956     
3957     SmallString<128> Str;
3958     llvm::raw_svector_ostream OS(Str);
3959     OS << *ClassSpec;
3960     TemplateSpecializationType::PrintTemplateArgumentList(OS,
3961                                       ClassSpec->getTemplateArgs().data(),
3962                                       ClassSpec->getTemplateArgs().size(),
3963                                                                 Policy);
3964     return cxstring::createDup(OS.str());
3965   }
3966   
3967   return clang_getCursorSpelling(C);
3968 }
3969   
3970 CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
3971   switch (Kind) {
3972   case CXCursor_FunctionDecl:
3973       return cxstring::createRef("FunctionDecl");
3974   case CXCursor_TypedefDecl:
3975       return cxstring::createRef("TypedefDecl");
3976   case CXCursor_EnumDecl:
3977       return cxstring::createRef("EnumDecl");
3978   case CXCursor_EnumConstantDecl:
3979       return cxstring::createRef("EnumConstantDecl");
3980   case CXCursor_StructDecl:
3981       return cxstring::createRef("StructDecl");
3982   case CXCursor_UnionDecl:
3983       return cxstring::createRef("UnionDecl");
3984   case CXCursor_ClassDecl:
3985       return cxstring::createRef("ClassDecl");
3986   case CXCursor_FieldDecl:
3987       return cxstring::createRef("FieldDecl");
3988   case CXCursor_VarDecl:
3989       return cxstring::createRef("VarDecl");
3990   case CXCursor_ParmDecl:
3991       return cxstring::createRef("ParmDecl");
3992   case CXCursor_ObjCInterfaceDecl:
3993       return cxstring::createRef("ObjCInterfaceDecl");
3994   case CXCursor_ObjCCategoryDecl:
3995       return cxstring::createRef("ObjCCategoryDecl");
3996   case CXCursor_ObjCProtocolDecl:
3997       return cxstring::createRef("ObjCProtocolDecl");
3998   case CXCursor_ObjCPropertyDecl:
3999       return cxstring::createRef("ObjCPropertyDecl");
4000   case CXCursor_ObjCIvarDecl:
4001       return cxstring::createRef("ObjCIvarDecl");
4002   case CXCursor_ObjCInstanceMethodDecl:
4003       return cxstring::createRef("ObjCInstanceMethodDecl");
4004   case CXCursor_ObjCClassMethodDecl:
4005       return cxstring::createRef("ObjCClassMethodDecl");
4006   case CXCursor_ObjCImplementationDecl:
4007       return cxstring::createRef("ObjCImplementationDecl");
4008   case CXCursor_ObjCCategoryImplDecl:
4009       return cxstring::createRef("ObjCCategoryImplDecl");
4010   case CXCursor_CXXMethod:
4011       return cxstring::createRef("CXXMethod");
4012   case CXCursor_UnexposedDecl:
4013       return cxstring::createRef("UnexposedDecl");
4014   case CXCursor_ObjCSuperClassRef:
4015       return cxstring::createRef("ObjCSuperClassRef");
4016   case CXCursor_ObjCProtocolRef:
4017       return cxstring::createRef("ObjCProtocolRef");
4018   case CXCursor_ObjCClassRef:
4019       return cxstring::createRef("ObjCClassRef");
4020   case CXCursor_TypeRef:
4021       return cxstring::createRef("TypeRef");
4022   case CXCursor_TemplateRef:
4023       return cxstring::createRef("TemplateRef");
4024   case CXCursor_NamespaceRef:
4025     return cxstring::createRef("NamespaceRef");
4026   case CXCursor_MemberRef:
4027     return cxstring::createRef("MemberRef");
4028   case CXCursor_LabelRef:
4029     return cxstring::createRef("LabelRef");
4030   case CXCursor_OverloadedDeclRef:
4031     return cxstring::createRef("OverloadedDeclRef");
4032   case CXCursor_VariableRef:
4033     return cxstring::createRef("VariableRef");
4034   case CXCursor_IntegerLiteral:
4035       return cxstring::createRef("IntegerLiteral");
4036   case CXCursor_FloatingLiteral:
4037       return cxstring::createRef("FloatingLiteral");
4038   case CXCursor_ImaginaryLiteral:
4039       return cxstring::createRef("ImaginaryLiteral");
4040   case CXCursor_StringLiteral:
4041       return cxstring::createRef("StringLiteral");
4042   case CXCursor_CharacterLiteral:
4043       return cxstring::createRef("CharacterLiteral");
4044   case CXCursor_ParenExpr:
4045       return cxstring::createRef("ParenExpr");
4046   case CXCursor_UnaryOperator:
4047       return cxstring::createRef("UnaryOperator");
4048   case CXCursor_ArraySubscriptExpr:
4049       return cxstring::createRef("ArraySubscriptExpr");
4050   case CXCursor_OMPArraySectionExpr:
4051       return cxstring::createRef("OMPArraySectionExpr");
4052   case CXCursor_BinaryOperator:
4053       return cxstring::createRef("BinaryOperator");
4054   case CXCursor_CompoundAssignOperator:
4055       return cxstring::createRef("CompoundAssignOperator");
4056   case CXCursor_ConditionalOperator:
4057       return cxstring::createRef("ConditionalOperator");
4058   case CXCursor_CStyleCastExpr:
4059       return cxstring::createRef("CStyleCastExpr");
4060   case CXCursor_CompoundLiteralExpr:
4061       return cxstring::createRef("CompoundLiteralExpr");
4062   case CXCursor_InitListExpr:
4063       return cxstring::createRef("InitListExpr");
4064   case CXCursor_AddrLabelExpr:
4065       return cxstring::createRef("AddrLabelExpr");
4066   case CXCursor_StmtExpr:
4067       return cxstring::createRef("StmtExpr");
4068   case CXCursor_GenericSelectionExpr:
4069       return cxstring::createRef("GenericSelectionExpr");
4070   case CXCursor_GNUNullExpr:
4071       return cxstring::createRef("GNUNullExpr");
4072   case CXCursor_CXXStaticCastExpr:
4073       return cxstring::createRef("CXXStaticCastExpr");
4074   case CXCursor_CXXDynamicCastExpr:
4075       return cxstring::createRef("CXXDynamicCastExpr");
4076   case CXCursor_CXXReinterpretCastExpr:
4077       return cxstring::createRef("CXXReinterpretCastExpr");
4078   case CXCursor_CXXConstCastExpr:
4079       return cxstring::createRef("CXXConstCastExpr");
4080   case CXCursor_CXXFunctionalCastExpr:
4081       return cxstring::createRef("CXXFunctionalCastExpr");
4082   case CXCursor_CXXTypeidExpr:
4083       return cxstring::createRef("CXXTypeidExpr");
4084   case CXCursor_CXXBoolLiteralExpr:
4085       return cxstring::createRef("CXXBoolLiteralExpr");
4086   case CXCursor_CXXNullPtrLiteralExpr:
4087       return cxstring::createRef("CXXNullPtrLiteralExpr");
4088   case CXCursor_CXXThisExpr:
4089       return cxstring::createRef("CXXThisExpr");
4090   case CXCursor_CXXThrowExpr:
4091       return cxstring::createRef("CXXThrowExpr");
4092   case CXCursor_CXXNewExpr:
4093       return cxstring::createRef("CXXNewExpr");
4094   case CXCursor_CXXDeleteExpr:
4095       return cxstring::createRef("CXXDeleteExpr");
4096   case CXCursor_UnaryExpr:
4097       return cxstring::createRef("UnaryExpr");
4098   case CXCursor_ObjCStringLiteral:
4099       return cxstring::createRef("ObjCStringLiteral");
4100   case CXCursor_ObjCBoolLiteralExpr:
4101       return cxstring::createRef("ObjCBoolLiteralExpr");
4102   case CXCursor_ObjCSelfExpr:
4103       return cxstring::createRef("ObjCSelfExpr");
4104   case CXCursor_ObjCEncodeExpr:
4105       return cxstring::createRef("ObjCEncodeExpr");
4106   case CXCursor_ObjCSelectorExpr:
4107       return cxstring::createRef("ObjCSelectorExpr");
4108   case CXCursor_ObjCProtocolExpr:
4109       return cxstring::createRef("ObjCProtocolExpr");
4110   case CXCursor_ObjCBridgedCastExpr:
4111       return cxstring::createRef("ObjCBridgedCastExpr");
4112   case CXCursor_BlockExpr:
4113       return cxstring::createRef("BlockExpr");
4114   case CXCursor_PackExpansionExpr:
4115       return cxstring::createRef("PackExpansionExpr");
4116   case CXCursor_SizeOfPackExpr:
4117       return cxstring::createRef("SizeOfPackExpr");
4118   case CXCursor_LambdaExpr:
4119     return cxstring::createRef("LambdaExpr");
4120   case CXCursor_UnexposedExpr:
4121       return cxstring::createRef("UnexposedExpr");
4122   case CXCursor_DeclRefExpr:
4123       return cxstring::createRef("DeclRefExpr");
4124   case CXCursor_MemberRefExpr:
4125       return cxstring::createRef("MemberRefExpr");
4126   case CXCursor_CallExpr:
4127       return cxstring::createRef("CallExpr");
4128   case CXCursor_ObjCMessageExpr:
4129       return cxstring::createRef("ObjCMessageExpr");
4130   case CXCursor_UnexposedStmt:
4131       return cxstring::createRef("UnexposedStmt");
4132   case CXCursor_DeclStmt:
4133       return cxstring::createRef("DeclStmt");
4134   case CXCursor_LabelStmt:
4135       return cxstring::createRef("LabelStmt");
4136   case CXCursor_CompoundStmt:
4137       return cxstring::createRef("CompoundStmt");
4138   case CXCursor_CaseStmt:
4139       return cxstring::createRef("CaseStmt");
4140   case CXCursor_DefaultStmt:
4141       return cxstring::createRef("DefaultStmt");
4142   case CXCursor_IfStmt:
4143       return cxstring::createRef("IfStmt");
4144   case CXCursor_SwitchStmt:
4145       return cxstring::createRef("SwitchStmt");
4146   case CXCursor_WhileStmt:
4147       return cxstring::createRef("WhileStmt");
4148   case CXCursor_DoStmt:
4149       return cxstring::createRef("DoStmt");
4150   case CXCursor_ForStmt:
4151       return cxstring::createRef("ForStmt");
4152   case CXCursor_GotoStmt:
4153       return cxstring::createRef("GotoStmt");
4154   case CXCursor_IndirectGotoStmt:
4155       return cxstring::createRef("IndirectGotoStmt");
4156   case CXCursor_ContinueStmt:
4157       return cxstring::createRef("ContinueStmt");
4158   case CXCursor_BreakStmt:
4159       return cxstring::createRef("BreakStmt");
4160   case CXCursor_ReturnStmt:
4161       return cxstring::createRef("ReturnStmt");
4162   case CXCursor_GCCAsmStmt:
4163       return cxstring::createRef("GCCAsmStmt");
4164   case CXCursor_MSAsmStmt:
4165       return cxstring::createRef("MSAsmStmt");
4166   case CXCursor_ObjCAtTryStmt:
4167       return cxstring::createRef("ObjCAtTryStmt");
4168   case CXCursor_ObjCAtCatchStmt:
4169       return cxstring::createRef("ObjCAtCatchStmt");
4170   case CXCursor_ObjCAtFinallyStmt:
4171       return cxstring::createRef("ObjCAtFinallyStmt");
4172   case CXCursor_ObjCAtThrowStmt:
4173       return cxstring::createRef("ObjCAtThrowStmt");
4174   case CXCursor_ObjCAtSynchronizedStmt:
4175       return cxstring::createRef("ObjCAtSynchronizedStmt");
4176   case CXCursor_ObjCAutoreleasePoolStmt:
4177       return cxstring::createRef("ObjCAutoreleasePoolStmt");
4178   case CXCursor_ObjCForCollectionStmt:
4179       return cxstring::createRef("ObjCForCollectionStmt");
4180   case CXCursor_CXXCatchStmt:
4181       return cxstring::createRef("CXXCatchStmt");
4182   case CXCursor_CXXTryStmt:
4183       return cxstring::createRef("CXXTryStmt");
4184   case CXCursor_CXXForRangeStmt:
4185       return cxstring::createRef("CXXForRangeStmt");
4186   case CXCursor_SEHTryStmt:
4187       return cxstring::createRef("SEHTryStmt");
4188   case CXCursor_SEHExceptStmt:
4189       return cxstring::createRef("SEHExceptStmt");
4190   case CXCursor_SEHFinallyStmt:
4191       return cxstring::createRef("SEHFinallyStmt");
4192   case CXCursor_SEHLeaveStmt:
4193       return cxstring::createRef("SEHLeaveStmt");
4194   case CXCursor_NullStmt:
4195       return cxstring::createRef("NullStmt");
4196   case CXCursor_InvalidFile:
4197       return cxstring::createRef("InvalidFile");
4198   case CXCursor_InvalidCode:
4199     return cxstring::createRef("InvalidCode");
4200   case CXCursor_NoDeclFound:
4201       return cxstring::createRef("NoDeclFound");
4202   case CXCursor_NotImplemented:
4203       return cxstring::createRef("NotImplemented");
4204   case CXCursor_TranslationUnit:
4205       return cxstring::createRef("TranslationUnit");
4206   case CXCursor_UnexposedAttr:
4207       return cxstring::createRef("UnexposedAttr");
4208   case CXCursor_IBActionAttr:
4209       return cxstring::createRef("attribute(ibaction)");
4210   case CXCursor_IBOutletAttr:
4211      return cxstring::createRef("attribute(iboutlet)");
4212   case CXCursor_IBOutletCollectionAttr:
4213       return cxstring::createRef("attribute(iboutletcollection)");
4214   case CXCursor_CXXFinalAttr:
4215       return cxstring::createRef("attribute(final)");
4216   case CXCursor_CXXOverrideAttr:
4217       return cxstring::createRef("attribute(override)");
4218   case CXCursor_AnnotateAttr:
4219     return cxstring::createRef("attribute(annotate)");
4220   case CXCursor_AsmLabelAttr:
4221     return cxstring::createRef("asm label");
4222   case CXCursor_PackedAttr:
4223     return cxstring::createRef("attribute(packed)");
4224   case CXCursor_PureAttr:
4225     return cxstring::createRef("attribute(pure)");
4226   case CXCursor_ConstAttr:
4227     return cxstring::createRef("attribute(const)");
4228   case CXCursor_NoDuplicateAttr:
4229     return cxstring::createRef("attribute(noduplicate)");
4230   case CXCursor_CUDAConstantAttr:
4231     return cxstring::createRef("attribute(constant)");
4232   case CXCursor_CUDADeviceAttr:
4233     return cxstring::createRef("attribute(device)");
4234   case CXCursor_CUDAGlobalAttr:
4235     return cxstring::createRef("attribute(global)");
4236   case CXCursor_CUDAHostAttr:
4237     return cxstring::createRef("attribute(host)");
4238   case CXCursor_CUDASharedAttr:
4239     return cxstring::createRef("attribute(shared)");
4240   case CXCursor_PreprocessingDirective:
4241     return cxstring::createRef("preprocessing directive");
4242   case CXCursor_MacroDefinition:
4243     return cxstring::createRef("macro definition");
4244   case CXCursor_MacroExpansion:
4245     return cxstring::createRef("macro expansion");
4246   case CXCursor_InclusionDirective:
4247     return cxstring::createRef("inclusion directive");
4248   case CXCursor_Namespace:
4249     return cxstring::createRef("Namespace");
4250   case CXCursor_LinkageSpec:
4251     return cxstring::createRef("LinkageSpec");
4252   case CXCursor_CXXBaseSpecifier:
4253     return cxstring::createRef("C++ base class specifier");
4254   case CXCursor_Constructor:
4255     return cxstring::createRef("CXXConstructor");
4256   case CXCursor_Destructor:
4257     return cxstring::createRef("CXXDestructor");
4258   case CXCursor_ConversionFunction:
4259     return cxstring::createRef("CXXConversion");
4260   case CXCursor_TemplateTypeParameter:
4261     return cxstring::createRef("TemplateTypeParameter");
4262   case CXCursor_NonTypeTemplateParameter:
4263     return cxstring::createRef("NonTypeTemplateParameter");
4264   case CXCursor_TemplateTemplateParameter:
4265     return cxstring::createRef("TemplateTemplateParameter");
4266   case CXCursor_FunctionTemplate:
4267     return cxstring::createRef("FunctionTemplate");
4268   case CXCursor_ClassTemplate:
4269     return cxstring::createRef("ClassTemplate");
4270   case CXCursor_ClassTemplatePartialSpecialization:
4271     return cxstring::createRef("ClassTemplatePartialSpecialization");
4272   case CXCursor_NamespaceAlias:
4273     return cxstring::createRef("NamespaceAlias");
4274   case CXCursor_UsingDirective:
4275     return cxstring::createRef("UsingDirective");
4276   case CXCursor_UsingDeclaration:
4277     return cxstring::createRef("UsingDeclaration");
4278   case CXCursor_TypeAliasDecl:
4279     return cxstring::createRef("TypeAliasDecl");
4280   case CXCursor_ObjCSynthesizeDecl:
4281     return cxstring::createRef("ObjCSynthesizeDecl");
4282   case CXCursor_ObjCDynamicDecl:
4283     return cxstring::createRef("ObjCDynamicDecl");
4284   case CXCursor_CXXAccessSpecifier:
4285     return cxstring::createRef("CXXAccessSpecifier");
4286   case CXCursor_ModuleImportDecl:
4287     return cxstring::createRef("ModuleImport");
4288   case CXCursor_OMPParallelDirective:
4289     return cxstring::createRef("OMPParallelDirective");
4290   case CXCursor_OMPSimdDirective:
4291     return cxstring::createRef("OMPSimdDirective");
4292   case CXCursor_OMPForDirective:
4293     return cxstring::createRef("OMPForDirective");
4294   case CXCursor_OMPForSimdDirective:
4295     return cxstring::createRef("OMPForSimdDirective");
4296   case CXCursor_OMPSectionsDirective:
4297     return cxstring::createRef("OMPSectionsDirective");
4298   case CXCursor_OMPSectionDirective:
4299     return cxstring::createRef("OMPSectionDirective");
4300   case CXCursor_OMPSingleDirective:
4301     return cxstring::createRef("OMPSingleDirective");
4302   case CXCursor_OMPMasterDirective:
4303     return cxstring::createRef("OMPMasterDirective");
4304   case CXCursor_OMPCriticalDirective:
4305     return cxstring::createRef("OMPCriticalDirective");
4306   case CXCursor_OMPParallelForDirective:
4307     return cxstring::createRef("OMPParallelForDirective");
4308   case CXCursor_OMPParallelForSimdDirective:
4309     return cxstring::createRef("OMPParallelForSimdDirective");
4310   case CXCursor_OMPParallelSectionsDirective:
4311     return cxstring::createRef("OMPParallelSectionsDirective");
4312   case CXCursor_OMPTaskDirective:
4313     return cxstring::createRef("OMPTaskDirective");
4314   case CXCursor_OMPTaskyieldDirective:
4315     return cxstring::createRef("OMPTaskyieldDirective");
4316   case CXCursor_OMPBarrierDirective:
4317     return cxstring::createRef("OMPBarrierDirective");
4318   case CXCursor_OMPTaskwaitDirective:
4319     return cxstring::createRef("OMPTaskwaitDirective");
4320   case CXCursor_OMPTaskgroupDirective:
4321     return cxstring::createRef("OMPTaskgroupDirective");
4322   case CXCursor_OMPFlushDirective:
4323     return cxstring::createRef("OMPFlushDirective");
4324   case CXCursor_OMPOrderedDirective:
4325     return cxstring::createRef("OMPOrderedDirective");
4326   case CXCursor_OMPAtomicDirective:
4327     return cxstring::createRef("OMPAtomicDirective");
4328   case CXCursor_OMPTargetDirective:
4329     return cxstring::createRef("OMPTargetDirective");
4330   case CXCursor_OMPTargetDataDirective:
4331     return cxstring::createRef("OMPTargetDataDirective");
4332   case CXCursor_OMPTeamsDirective:
4333     return cxstring::createRef("OMPTeamsDirective");
4334   case CXCursor_OMPCancellationPointDirective:
4335     return cxstring::createRef("OMPCancellationPointDirective");
4336   case CXCursor_OMPCancelDirective:
4337     return cxstring::createRef("OMPCancelDirective");
4338   case CXCursor_OverloadCandidate:
4339       return cxstring::createRef("OverloadCandidate");
4340   }
4341
4342   llvm_unreachable("Unhandled CXCursorKind");
4343 }
4344
4345 struct GetCursorData {
4346   SourceLocation TokenBeginLoc;
4347   bool PointsAtMacroArgExpansion;
4348   bool VisitedObjCPropertyImplDecl;
4349   SourceLocation VisitedDeclaratorDeclStartLoc;
4350   CXCursor &BestCursor;
4351
4352   GetCursorData(SourceManager &SM,
4353                 SourceLocation tokenBegin, CXCursor &outputCursor)
4354     : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
4355     PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
4356     VisitedObjCPropertyImplDecl = false;
4357   }
4358 };
4359
4360 static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
4361                                                 CXCursor parent,
4362                                                 CXClientData client_data) {
4363   GetCursorData *Data = static_cast<GetCursorData *>(client_data);
4364   CXCursor *BestCursor = &Data->BestCursor;
4365
4366   // If we point inside a macro argument we should provide info of what the
4367   // token is so use the actual cursor, don't replace it with a macro expansion
4368   // cursor.
4369   if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
4370     return CXChildVisit_Recurse;
4371   
4372   if (clang_isDeclaration(cursor.kind)) {
4373     // Avoid having the implicit methods override the property decls.
4374     if (const ObjCMethodDecl *MD
4375           = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
4376       if (MD->isImplicit())
4377         return CXChildVisit_Break;
4378
4379     } else if (const ObjCInterfaceDecl *ID
4380                  = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) {
4381       // Check that when we have multiple @class references in the same line,
4382       // that later ones do not override the previous ones.
4383       // If we have:
4384       // @class Foo, Bar;
4385       // source ranges for both start at '@', so 'Bar' will end up overriding
4386       // 'Foo' even though the cursor location was at 'Foo'.
4387       if (BestCursor->kind == CXCursor_ObjCInterfaceDecl ||
4388           BestCursor->kind == CXCursor_ObjCClassRef)
4389         if (const ObjCInterfaceDecl *PrevID
4390              = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){
4391          if (PrevID != ID &&
4392              !PrevID->isThisDeclarationADefinition() &&
4393              !ID->isThisDeclarationADefinition())
4394            return CXChildVisit_Break;
4395         }
4396
4397     } else if (const DeclaratorDecl *DD
4398                     = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) {
4399       SourceLocation StartLoc = DD->getSourceRange().getBegin();
4400       // Check that when we have multiple declarators in the same line,
4401       // that later ones do not override the previous ones.
4402       // If we have:
4403       // int Foo, Bar;
4404       // source ranges for both start at 'int', so 'Bar' will end up overriding
4405       // 'Foo' even though the cursor location was at 'Foo'.
4406       if (Data->VisitedDeclaratorDeclStartLoc == StartLoc)
4407         return CXChildVisit_Break;
4408       Data->VisitedDeclaratorDeclStartLoc = StartLoc;
4409
4410     } else if (const ObjCPropertyImplDecl *PropImp
4411               = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) {
4412       (void)PropImp;
4413       // Check that when we have multiple @synthesize in the same line,
4414       // that later ones do not override the previous ones.
4415       // If we have:
4416       // @synthesize Foo, Bar;
4417       // source ranges for both start at '@', so 'Bar' will end up overriding
4418       // 'Foo' even though the cursor location was at 'Foo'.
4419       if (Data->VisitedObjCPropertyImplDecl)
4420         return CXChildVisit_Break;
4421       Data->VisitedObjCPropertyImplDecl = true;
4422     }
4423   }
4424
4425   if (clang_isExpression(cursor.kind) &&
4426       clang_isDeclaration(BestCursor->kind)) {
4427     if (const Decl *D = getCursorDecl(*BestCursor)) {
4428       // Avoid having the cursor of an expression replace the declaration cursor
4429       // when the expression source range overlaps the declaration range.
4430       // This can happen for C++ constructor expressions whose range generally
4431       // include the variable declaration, e.g.:
4432       //  MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor.
4433       if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
4434           D->getLocation() == Data->TokenBeginLoc)
4435         return CXChildVisit_Break;
4436     }
4437   }
4438
4439   // If our current best cursor is the construction of a temporary object, 
4440   // don't replace that cursor with a type reference, because we want 
4441   // clang_getCursor() to point at the constructor.
4442   if (clang_isExpression(BestCursor->kind) &&
4443       isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
4444       cursor.kind == CXCursor_TypeRef) {
4445     // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
4446     // as having the actual point on the type reference.
4447     *BestCursor = getTypeRefedCallExprCursor(*BestCursor);
4448     return CXChildVisit_Recurse;
4449   }
4450
4451   // If we already have an Objective-C superclass reference, don't
4452   // update it further.
4453   if (BestCursor->kind == CXCursor_ObjCSuperClassRef)
4454     return CXChildVisit_Break;
4455
4456   *BestCursor = cursor;
4457   return CXChildVisit_Recurse;
4458 }
4459
4460 CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
4461   if (isNotUsableTU(TU)) {
4462     LOG_BAD_TU(TU);
4463     return clang_getNullCursor();
4464   }
4465
4466   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4467   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4468
4469   SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
4470   CXCursor Result = cxcursor::getCursor(TU, SLoc);
4471
4472   LOG_FUNC_SECTION {
4473     CXFile SearchFile;
4474     unsigned SearchLine, SearchColumn;
4475     CXFile ResultFile;
4476     unsigned ResultLine, ResultColumn;
4477     CXString SearchFileName, ResultFileName, KindSpelling, USR;
4478     const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
4479     CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
4480
4481     clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
4482                           nullptr);
4483     clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine,
4484                           &ResultColumn, nullptr);
4485     SearchFileName = clang_getFileName(SearchFile);
4486     ResultFileName = clang_getFileName(ResultFile);
4487     KindSpelling = clang_getCursorKindSpelling(Result.kind);
4488     USR = clang_getCursorUSR(Result);
4489     *Log << llvm::format("(%s:%d:%d) = %s",
4490                    clang_getCString(SearchFileName), SearchLine, SearchColumn,
4491                    clang_getCString(KindSpelling))
4492         << llvm::format("(%s:%d:%d):%s%s",
4493                      clang_getCString(ResultFileName), ResultLine, ResultColumn,
4494                      clang_getCString(USR), IsDef);
4495     clang_disposeString(SearchFileName);
4496     clang_disposeString(ResultFileName);
4497     clang_disposeString(KindSpelling);
4498     clang_disposeString(USR);
4499     
4500     CXCursor Definition = clang_getCursorDefinition(Result);
4501     if (!clang_equalCursors(Definition, clang_getNullCursor())) {
4502       CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
4503       CXString DefinitionKindSpelling
4504                                 = clang_getCursorKindSpelling(Definition.kind);
4505       CXFile DefinitionFile;
4506       unsigned DefinitionLine, DefinitionColumn;
4507       clang_getFileLocation(DefinitionLoc, &DefinitionFile,
4508                             &DefinitionLine, &DefinitionColumn, nullptr);
4509       CXString DefinitionFileName = clang_getFileName(DefinitionFile);
4510       *Log << llvm::format("  -> %s(%s:%d:%d)",
4511                      clang_getCString(DefinitionKindSpelling),
4512                      clang_getCString(DefinitionFileName),
4513                      DefinitionLine, DefinitionColumn);
4514       clang_disposeString(DefinitionFileName);
4515       clang_disposeString(DefinitionKindSpelling);
4516     }
4517   }
4518
4519   return Result;
4520 }
4521
4522 CXCursor clang_getNullCursor(void) {
4523   return MakeCXCursorInvalid(CXCursor_InvalidFile);
4524 }
4525
4526 unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
4527   // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we
4528   // can't set consistently. For example, when visiting a DeclStmt we will set
4529   // it but we don't set it on the result of clang_getCursorDefinition for
4530   // a reference of the same declaration.
4531   // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works
4532   // when visiting a DeclStmt currently, the AST should be enhanced to be able
4533   // to provide that kind of info.
4534   if (clang_isDeclaration(X.kind))
4535     X.data[1] = nullptr;
4536   if (clang_isDeclaration(Y.kind))
4537     Y.data[1] = nullptr;
4538
4539   return X == Y;
4540 }
4541
4542 unsigned clang_hashCursor(CXCursor C) {
4543   unsigned Index = 0;
4544   if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
4545     Index = 1;
4546   
4547   return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue(
4548                                         std::make_pair(C.kind, C.data[Index]));
4549 }
4550
4551 unsigned clang_isInvalid(enum CXCursorKind K) {
4552   return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
4553 }
4554
4555 unsigned clang_isDeclaration(enum CXCursorKind K) {
4556   return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) ||
4557          (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl);
4558 }
4559
4560 unsigned clang_isReference(enum CXCursorKind K) {
4561   return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
4562 }
4563
4564 unsigned clang_isExpression(enum CXCursorKind K) {
4565   return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
4566 }
4567
4568 unsigned clang_isStatement(enum CXCursorKind K) {
4569   return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
4570 }
4571
4572 unsigned clang_isAttribute(enum CXCursorKind K) {
4573     return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
4574 }
4575
4576 unsigned clang_isTranslationUnit(enum CXCursorKind K) {
4577   return K == CXCursor_TranslationUnit;
4578 }
4579
4580 unsigned clang_isPreprocessing(enum CXCursorKind K) {
4581   return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
4582 }
4583   
4584 unsigned clang_isUnexposed(enum CXCursorKind K) {
4585   switch (K) {
4586     case CXCursor_UnexposedDecl:
4587     case CXCursor_UnexposedExpr:
4588     case CXCursor_UnexposedStmt:
4589     case CXCursor_UnexposedAttr:
4590       return true;
4591     default:
4592       return false;
4593   }
4594 }
4595
4596 CXCursorKind clang_getCursorKind(CXCursor C) {
4597   return C.kind;
4598 }
4599
4600 CXSourceLocation clang_getCursorLocation(CXCursor C) {
4601   if (clang_isReference(C.kind)) {
4602     switch (C.kind) {
4603     case CXCursor_ObjCSuperClassRef: {
4604       std::pair<const ObjCInterfaceDecl *, SourceLocation> P
4605         = getCursorObjCSuperClassRef(C);
4606       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4607     }
4608
4609     case CXCursor_ObjCProtocolRef: {
4610       std::pair<const ObjCProtocolDecl *, SourceLocation> P
4611         = getCursorObjCProtocolRef(C);
4612       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4613     }
4614
4615     case CXCursor_ObjCClassRef: {
4616       std::pair<const ObjCInterfaceDecl *, SourceLocation> P
4617         = getCursorObjCClassRef(C);
4618       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4619     }
4620
4621     case CXCursor_TypeRef: {
4622       std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
4623       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4624     }
4625
4626     case CXCursor_TemplateRef: {
4627       std::pair<const TemplateDecl *, SourceLocation> P =
4628           getCursorTemplateRef(C);
4629       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4630     }
4631
4632     case CXCursor_NamespaceRef: {
4633       std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
4634       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4635     }
4636
4637     case CXCursor_MemberRef: {
4638       std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
4639       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4640     }
4641
4642     case CXCursor_VariableRef: {
4643       std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C);
4644       return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
4645     }
4646
4647     case CXCursor_CXXBaseSpecifier: {
4648       const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
4649       if (!BaseSpec)
4650         return clang_getNullLocation();
4651       
4652       if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
4653         return cxloc::translateSourceLocation(getCursorContext(C),
4654                                             TSInfo->getTypeLoc().getBeginLoc());
4655       
4656       return cxloc::translateSourceLocation(getCursorContext(C),
4657                                         BaseSpec->getLocStart());
4658     }
4659
4660     case CXCursor_LabelRef: {
4661       std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
4662       return cxloc::translateSourceLocation(getCursorContext(C), P.second);
4663     }
4664
4665     case CXCursor_OverloadedDeclRef:
4666       return cxloc::translateSourceLocation(getCursorContext(C),
4667                                           getCursorOverloadedDeclRef(C).second);
4668
4669     default:
4670       // FIXME: Need a way to enumerate all non-reference cases.
4671       llvm_unreachable("Missed a reference kind");
4672     }
4673   }
4674
4675   if (clang_isExpression(C.kind))
4676     return cxloc::translateSourceLocation(getCursorContext(C),
4677                                    getLocationFromExpr(getCursorExpr(C)));
4678
4679   if (clang_isStatement(C.kind))
4680     return cxloc::translateSourceLocation(getCursorContext(C),
4681                                           getCursorStmt(C)->getLocStart());
4682
4683   if (C.kind == CXCursor_PreprocessingDirective) {
4684     SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
4685     return cxloc::translateSourceLocation(getCursorContext(C), L);
4686   }
4687
4688   if (C.kind == CXCursor_MacroExpansion) {
4689     SourceLocation L
4690       = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin();
4691     return cxloc::translateSourceLocation(getCursorContext(C), L);
4692   }
4693
4694   if (C.kind == CXCursor_MacroDefinition) {
4695     SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
4696     return cxloc::translateSourceLocation(getCursorContext(C), L);
4697   }
4698
4699   if (C.kind == CXCursor_InclusionDirective) {
4700     SourceLocation L
4701       = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
4702     return cxloc::translateSourceLocation(getCursorContext(C), L);
4703   }
4704
4705   if (clang_isAttribute(C.kind)) {
4706     SourceLocation L
4707       = cxcursor::getCursorAttr(C)->getLocation();
4708     return cxloc::translateSourceLocation(getCursorContext(C), L);
4709   }
4710
4711   if (!clang_isDeclaration(C.kind))
4712     return clang_getNullLocation();
4713
4714   const Decl *D = getCursorDecl(C);
4715   if (!D)
4716     return clang_getNullLocation();
4717
4718   SourceLocation Loc = D->getLocation();
4719   // FIXME: Multiple variables declared in a single declaration
4720   // currently lack the information needed to correctly determine their
4721   // ranges when accounting for the type-specifier.  We use context
4722   // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4723   // and if so, whether it is the first decl.
4724   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
4725     if (!cxcursor::isFirstInDeclGroup(C))
4726       Loc = VD->getLocation();
4727   }
4728
4729   // For ObjC methods, give the start location of the method name.
4730   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
4731     Loc = MD->getSelectorStartLoc();
4732
4733   return cxloc::translateSourceLocation(getCursorContext(C), Loc);
4734 }
4735
4736 } // end extern "C"
4737
4738 CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) {
4739   assert(TU);
4740
4741   // Guard against an invalid SourceLocation, or we may assert in one
4742   // of the following calls.
4743   if (SLoc.isInvalid())
4744     return clang_getNullCursor();
4745
4746   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4747
4748   // Translate the given source location to make it point at the beginning of
4749   // the token under the cursor.
4750   SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
4751                                     CXXUnit->getASTContext().getLangOpts());
4752   
4753   CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
4754   if (SLoc.isValid()) {
4755     GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
4756     CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
4757                             /*VisitPreprocessorLast=*/true, 
4758                             /*VisitIncludedEntities=*/false,
4759                             SourceLocation(SLoc));
4760     CursorVis.visitFileRegion();
4761   }
4762
4763   return Result;
4764 }
4765
4766 static SourceRange getRawCursorExtent(CXCursor C) {
4767   if (clang_isReference(C.kind)) {
4768     switch (C.kind) {
4769     case CXCursor_ObjCSuperClassRef:
4770       return  getCursorObjCSuperClassRef(C).second;
4771
4772     case CXCursor_ObjCProtocolRef:
4773       return getCursorObjCProtocolRef(C).second;
4774
4775     case CXCursor_ObjCClassRef:
4776       return getCursorObjCClassRef(C).second;
4777
4778     case CXCursor_TypeRef:
4779       return getCursorTypeRef(C).second;
4780
4781     case CXCursor_TemplateRef:
4782       return getCursorTemplateRef(C).second;
4783
4784     case CXCursor_NamespaceRef:
4785       return getCursorNamespaceRef(C).second;
4786
4787     case CXCursor_MemberRef:
4788       return getCursorMemberRef(C).second;
4789
4790     case CXCursor_CXXBaseSpecifier:
4791       return getCursorCXXBaseSpecifier(C)->getSourceRange();
4792
4793     case CXCursor_LabelRef:
4794       return getCursorLabelRef(C).second;
4795
4796     case CXCursor_OverloadedDeclRef:
4797       return getCursorOverloadedDeclRef(C).second;
4798
4799     case CXCursor_VariableRef:
4800       return getCursorVariableRef(C).second;
4801         
4802     default:
4803       // FIXME: Need a way to enumerate all non-reference cases.
4804       llvm_unreachable("Missed a reference kind");
4805     }
4806   }
4807
4808   if (clang_isExpression(C.kind))
4809     return getCursorExpr(C)->getSourceRange();
4810
4811   if (clang_isStatement(C.kind))
4812     return getCursorStmt(C)->getSourceRange();
4813
4814   if (clang_isAttribute(C.kind))
4815     return getCursorAttr(C)->getRange();
4816
4817   if (C.kind == CXCursor_PreprocessingDirective)
4818     return cxcursor::getCursorPreprocessingDirective(C);
4819
4820   if (C.kind == CXCursor_MacroExpansion) {
4821     ASTUnit *TU = getCursorASTUnit(C);
4822     SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange();
4823     return TU->mapRangeFromPreamble(Range);
4824   }
4825
4826   if (C.kind == CXCursor_MacroDefinition) {
4827     ASTUnit *TU = getCursorASTUnit(C);
4828     SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
4829     return TU->mapRangeFromPreamble(Range);
4830   }
4831
4832   if (C.kind == CXCursor_InclusionDirective) {
4833     ASTUnit *TU = getCursorASTUnit(C);
4834     SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange();
4835     return TU->mapRangeFromPreamble(Range);
4836   }
4837
4838   if (C.kind == CXCursor_TranslationUnit) {
4839     ASTUnit *TU = getCursorASTUnit(C);
4840     FileID MainID = TU->getSourceManager().getMainFileID();
4841     SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID);
4842     SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID);
4843     return SourceRange(Start, End);
4844   }
4845
4846   if (clang_isDeclaration(C.kind)) {
4847     const Decl *D = cxcursor::getCursorDecl(C);
4848     if (!D)
4849       return SourceRange();
4850
4851     SourceRange R = D->getSourceRange();
4852     // FIXME: Multiple variables declared in a single declaration
4853     // currently lack the information needed to correctly determine their
4854     // ranges when accounting for the type-specifier.  We use context
4855     // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4856     // and if so, whether it is the first decl.
4857     if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
4858       if (!cxcursor::isFirstInDeclGroup(C))
4859         R.setBegin(VD->getLocation());
4860     }
4861     return R;
4862   }
4863   return SourceRange();
4864 }
4865
4866 /// \brief Retrieves the "raw" cursor extent, which is then extended to include
4867 /// the decl-specifier-seq for declarations.
4868 static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
4869   if (clang_isDeclaration(C.kind)) {
4870     const Decl *D = cxcursor::getCursorDecl(C);
4871     if (!D)
4872       return SourceRange();
4873
4874     SourceRange R = D->getSourceRange();
4875
4876     // Adjust the start of the location for declarations preceded by
4877     // declaration specifiers.
4878     SourceLocation StartLoc;
4879     if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
4880       if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
4881         StartLoc = TI->getTypeLoc().getLocStart();
4882     } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
4883       if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
4884         StartLoc = TI->getTypeLoc().getLocStart();
4885     }
4886
4887     if (StartLoc.isValid() && R.getBegin().isValid() &&
4888         SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
4889       R.setBegin(StartLoc);
4890
4891     // FIXME: Multiple variables declared in a single declaration
4892     // currently lack the information needed to correctly determine their
4893     // ranges when accounting for the type-specifier.  We use context
4894     // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
4895     // and if so, whether it is the first decl.
4896     if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
4897       if (!cxcursor::isFirstInDeclGroup(C))
4898         R.setBegin(VD->getLocation());
4899     }
4900
4901     return R;    
4902   }
4903   
4904   return getRawCursorExtent(C);
4905 }
4906
4907 extern "C" {
4908
4909 CXSourceRange clang_getCursorExtent(CXCursor C) {
4910   SourceRange R = getRawCursorExtent(C);
4911   if (R.isInvalid())
4912     return clang_getNullRange();
4913
4914   return cxloc::translateSourceRange(getCursorContext(C), R);
4915 }
4916
4917 CXCursor clang_getCursorReferenced(CXCursor C) {
4918   if (clang_isInvalid(C.kind))
4919     return clang_getNullCursor();
4920
4921   CXTranslationUnit tu = getCursorTU(C);
4922   if (clang_isDeclaration(C.kind)) {
4923     const Decl *D = getCursorDecl(C);
4924     if (!D)
4925       return clang_getNullCursor();
4926     if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
4927       return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
4928     if (const ObjCPropertyImplDecl *PropImpl =
4929             dyn_cast<ObjCPropertyImplDecl>(D))
4930       if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
4931         return MakeCXCursor(Property, tu);
4932     
4933     return C;
4934   }
4935   
4936   if (clang_isExpression(C.kind)) {
4937     const Expr *E = getCursorExpr(C);
4938     const Decl *D = getDeclFromExpr(E);
4939     if (D) {
4940       CXCursor declCursor = MakeCXCursor(D, tu);
4941       declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C),
4942                                                declCursor);
4943       return declCursor;
4944     }
4945     
4946     if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
4947       return MakeCursorOverloadedDeclRef(Ovl, tu);
4948         
4949     return clang_getNullCursor();
4950   }
4951
4952   if (clang_isStatement(C.kind)) {
4953     const Stmt *S = getCursorStmt(C);
4954     if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
4955       if (LabelDecl *label = Goto->getLabel())
4956         if (LabelStmt *labelS = label->getStmt())
4957         return MakeCXCursor(labelS, getCursorDecl(C), tu);
4958
4959     return clang_getNullCursor();
4960   }
4961
4962   if (C.kind == CXCursor_MacroExpansion) {
4963     if (const MacroDefinitionRecord *Def =
4964             getCursorMacroExpansion(C).getDefinition())
4965       return MakeMacroDefinitionCursor(Def, tu);
4966   }
4967
4968   if (!clang_isReference(C.kind))
4969     return clang_getNullCursor();
4970
4971   switch (C.kind) {
4972     case CXCursor_ObjCSuperClassRef:
4973       return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
4974
4975     case CXCursor_ObjCProtocolRef: {
4976       const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first;
4977       if (const ObjCProtocolDecl *Def = Prot->getDefinition())
4978         return MakeCXCursor(Def, tu);
4979
4980       return MakeCXCursor(Prot, tu);
4981     }
4982
4983     case CXCursor_ObjCClassRef: {
4984       const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
4985       if (const ObjCInterfaceDecl *Def = Class->getDefinition())
4986         return MakeCXCursor(Def, tu);
4987
4988       return MakeCXCursor(Class, tu);
4989     }
4990
4991     case CXCursor_TypeRef:
4992       return MakeCXCursor(getCursorTypeRef(C).first, tu );
4993
4994     case CXCursor_TemplateRef:
4995       return MakeCXCursor(getCursorTemplateRef(C).first, tu );
4996
4997     case CXCursor_NamespaceRef:
4998       return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
4999
5000     case CXCursor_MemberRef:
5001       return MakeCXCursor(getCursorMemberRef(C).first, tu );
5002
5003     case CXCursor_CXXBaseSpecifier: {
5004       const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
5005       return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
5006                                                          tu ));
5007     }
5008
5009     case CXCursor_LabelRef:
5010       // FIXME: We end up faking the "parent" declaration here because we
5011       // don't want to make CXCursor larger.
5012       return MakeCXCursor(getCursorLabelRef(C).first,
5013                           cxtu::getASTUnit(tu)->getASTContext()
5014                               .getTranslationUnitDecl(),
5015                           tu);
5016
5017     case CXCursor_OverloadedDeclRef:
5018       return C;
5019       
5020     case CXCursor_VariableRef:
5021       return MakeCXCursor(getCursorVariableRef(C).first, tu);
5022
5023     default:
5024       // We would prefer to enumerate all non-reference cursor kinds here.
5025       llvm_unreachable("Unhandled reference cursor kind");
5026   }
5027 }
5028
5029 CXCursor clang_getCursorDefinition(CXCursor C) {
5030   if (clang_isInvalid(C.kind))
5031     return clang_getNullCursor();
5032
5033   CXTranslationUnit TU = getCursorTU(C);
5034
5035   bool WasReference = false;
5036   if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
5037     C = clang_getCursorReferenced(C);
5038     WasReference = true;
5039   }
5040
5041   if (C.kind == CXCursor_MacroExpansion)
5042     return clang_getCursorReferenced(C);
5043
5044   if (!clang_isDeclaration(C.kind))
5045     return clang_getNullCursor();
5046
5047   const Decl *D = getCursorDecl(C);
5048   if (!D)
5049     return clang_getNullCursor();
5050
5051   switch (D->getKind()) {
5052   // Declaration kinds that don't really separate the notions of
5053   // declaration and definition.
5054   case Decl::Namespace:
5055   case Decl::Typedef:
5056   case Decl::TypeAlias:
5057   case Decl::TypeAliasTemplate:
5058   case Decl::TemplateTypeParm:
5059   case Decl::EnumConstant:
5060   case Decl::Field:
5061   case Decl::MSProperty:
5062   case Decl::IndirectField:
5063   case Decl::ObjCIvar:
5064   case Decl::ObjCAtDefsField:
5065   case Decl::ImplicitParam:
5066   case Decl::ParmVar:
5067   case Decl::NonTypeTemplateParm:
5068   case Decl::TemplateTemplateParm:
5069   case Decl::ObjCCategoryImpl:
5070   case Decl::ObjCImplementation:
5071   case Decl::AccessSpec:
5072   case Decl::LinkageSpec:
5073   case Decl::ObjCPropertyImpl:
5074   case Decl::FileScopeAsm:
5075   case Decl::StaticAssert:
5076   case Decl::Block:
5077   case Decl::Captured:
5078   case Decl::Label:  // FIXME: Is this right??
5079   case Decl::ClassScopeFunctionSpecialization:
5080   case Decl::Import:
5081   case Decl::OMPThreadPrivate:
5082   case Decl::ObjCTypeParam:
5083     return C;
5084
5085   // Declaration kinds that don't make any sense here, but are
5086   // nonetheless harmless.
5087   case Decl::Empty:
5088   case Decl::TranslationUnit:
5089   case Decl::ExternCContext:
5090     break;
5091
5092   // Declaration kinds for which the definition is not resolvable.
5093   case Decl::UnresolvedUsingTypename:
5094   case Decl::UnresolvedUsingValue:
5095     break;
5096
5097   case Decl::UsingDirective:
5098     return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
5099                         TU);
5100
5101   case Decl::NamespaceAlias:
5102     return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
5103
5104   case Decl::Enum:
5105   case Decl::Record:
5106   case Decl::CXXRecord:
5107   case Decl::ClassTemplateSpecialization:
5108   case Decl::ClassTemplatePartialSpecialization:
5109     if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
5110       return MakeCXCursor(Def, TU);
5111     return clang_getNullCursor();
5112
5113   case Decl::Function:
5114   case Decl::CXXMethod:
5115   case Decl::CXXConstructor:
5116   case Decl::CXXDestructor:
5117   case Decl::CXXConversion: {
5118     const FunctionDecl *Def = nullptr;
5119     if (cast<FunctionDecl>(D)->getBody(Def))
5120       return MakeCXCursor(Def, TU);
5121     return clang_getNullCursor();
5122   }
5123
5124   case Decl::Var:
5125   case Decl::VarTemplateSpecialization:
5126   case Decl::VarTemplatePartialSpecialization: {
5127     // Ask the variable if it has a definition.
5128     if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition())
5129       return MakeCXCursor(Def, TU);
5130     return clang_getNullCursor();
5131   }
5132
5133   case Decl::FunctionTemplate: {
5134     const FunctionDecl *Def = nullptr;
5135     if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
5136       return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
5137     return clang_getNullCursor();
5138   }
5139
5140   case Decl::ClassTemplate: {
5141     if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
5142                                                             ->getDefinition())
5143       return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
5144                           TU);
5145     return clang_getNullCursor();
5146   }
5147
5148   case Decl::VarTemplate: {
5149     if (VarDecl *Def =
5150             cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition())
5151       return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU);
5152     return clang_getNullCursor();
5153   }
5154
5155   case Decl::Using:
5156     return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), 
5157                                        D->getLocation(), TU);
5158
5159   case Decl::UsingShadow:
5160     return clang_getCursorDefinition(
5161                        MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
5162                                     TU));
5163
5164   case Decl::ObjCMethod: {
5165     const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
5166     if (Method->isThisDeclarationADefinition())
5167       return C;
5168
5169     // Dig out the method definition in the associated
5170     // @implementation, if we have it.
5171     // FIXME: The ASTs should make finding the definition easier.
5172     if (const ObjCInterfaceDecl *Class
5173                        = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
5174       if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
5175         if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
5176                                                   Method->isInstanceMethod()))
5177           if (Def->isThisDeclarationADefinition())
5178             return MakeCXCursor(Def, TU);
5179
5180     return clang_getNullCursor();
5181   }
5182
5183   case Decl::ObjCCategory:
5184     if (ObjCCategoryImplDecl *Impl
5185                                = cast<ObjCCategoryDecl>(D)->getImplementation())
5186       return MakeCXCursor(Impl, TU);
5187     return clang_getNullCursor();
5188
5189   case Decl::ObjCProtocol:
5190     if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition())
5191       return MakeCXCursor(Def, TU);
5192     return clang_getNullCursor();
5193
5194   case Decl::ObjCInterface: {
5195     // There are two notions of a "definition" for an Objective-C
5196     // class: the interface and its implementation. When we resolved a
5197     // reference to an Objective-C class, produce the @interface as
5198     // the definition; when we were provided with the interface,
5199     // produce the @implementation as the definition.
5200     const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D);
5201     if (WasReference) {
5202       if (const ObjCInterfaceDecl *Def = IFace->getDefinition())
5203         return MakeCXCursor(Def, TU);
5204     } else if (ObjCImplementationDecl *Impl = IFace->getImplementation())
5205       return MakeCXCursor(Impl, TU);
5206     return clang_getNullCursor();
5207   }
5208
5209   case Decl::ObjCProperty:
5210     // FIXME: We don't really know where to find the
5211     // ObjCPropertyImplDecls that implement this property.
5212     return clang_getNullCursor();
5213
5214   case Decl::ObjCCompatibleAlias:
5215     if (const ObjCInterfaceDecl *Class
5216           = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
5217       if (const ObjCInterfaceDecl *Def = Class->getDefinition())
5218         return MakeCXCursor(Def, TU);
5219
5220     return clang_getNullCursor();
5221
5222   case Decl::Friend:
5223     if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
5224       return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
5225     return clang_getNullCursor();
5226
5227   case Decl::FriendTemplate:
5228     if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
5229       return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
5230     return clang_getNullCursor();
5231   }
5232
5233   return clang_getNullCursor();
5234 }
5235
5236 unsigned clang_isCursorDefinition(CXCursor C) {
5237   if (!clang_isDeclaration(C.kind))
5238     return 0;
5239
5240   return clang_getCursorDefinition(C) == C;
5241 }
5242
5243 CXCursor clang_getCanonicalCursor(CXCursor C) {
5244   if (!clang_isDeclaration(C.kind))
5245     return C;
5246   
5247   if (const Decl *D = getCursorDecl(C)) {
5248     if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D))
5249       if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
5250         return MakeCXCursor(CatD, getCursorTU(C));
5251
5252     if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5253       if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
5254         return MakeCXCursor(IFD, getCursorTU(C));
5255
5256     return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
5257   }
5258   
5259   return C;
5260 }
5261
5262 int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) {
5263   return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first;
5264 }
5265   
5266 unsigned clang_getNumOverloadedDecls(CXCursor C) {
5267   if (C.kind != CXCursor_OverloadedDeclRef)
5268     return 0;
5269   
5270   OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
5271   if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
5272     return E->getNumDecls();
5273   
5274   if (OverloadedTemplateStorage *S
5275                               = Storage.dyn_cast<OverloadedTemplateStorage*>())
5276     return S->size();
5277   
5278   const Decl *D = Storage.get<const Decl *>();
5279   if (const UsingDecl *Using = dyn_cast<UsingDecl>(D))
5280     return Using->shadow_size();
5281   
5282   return 0;
5283 }
5284
5285 CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
5286   if (cursor.kind != CXCursor_OverloadedDeclRef)
5287     return clang_getNullCursor();
5288
5289   if (index >= clang_getNumOverloadedDecls(cursor))
5290     return clang_getNullCursor();
5291   
5292   CXTranslationUnit TU = getCursorTU(cursor);
5293   OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
5294   if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>())
5295     return MakeCXCursor(E->decls_begin()[index], TU);
5296   
5297   if (OverloadedTemplateStorage *S
5298                               = Storage.dyn_cast<OverloadedTemplateStorage*>())
5299     return MakeCXCursor(S->begin()[index], TU);
5300   
5301   const Decl *D = Storage.get<const Decl *>();
5302   if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
5303     // FIXME: This is, unfortunately, linear time.
5304     UsingDecl::shadow_iterator Pos = Using->shadow_begin();
5305     std::advance(Pos, index);
5306     return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
5307   }
5308   
5309   return clang_getNullCursor();
5310 }
5311   
5312 void clang_getDefinitionSpellingAndExtent(CXCursor C,
5313                                           const char **startBuf,
5314                                           const char **endBuf,
5315                                           unsigned *startLine,
5316                                           unsigned *startColumn,
5317                                           unsigned *endLine,
5318                                           unsigned *endColumn) {
5319   assert(getCursorDecl(C) && "CXCursor has null decl");
5320   const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C));
5321   CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
5322
5323   SourceManager &SM = FD->getASTContext().getSourceManager();
5324   *startBuf = SM.getCharacterData(Body->getLBracLoc());
5325   *endBuf = SM.getCharacterData(Body->getRBracLoc());
5326   *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
5327   *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
5328   *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
5329   *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
5330 }
5331
5332
5333 CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
5334                                                 unsigned PieceIndex) {
5335   RefNamePieces Pieces;
5336   
5337   switch (C.kind) {
5338   case CXCursor_MemberRefExpr:
5339     if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C)))
5340       Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(),
5341                            E->getQualifierLoc().getSourceRange());
5342     break;
5343   
5344   case CXCursor_DeclRefExpr:
5345     if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C)))
5346       Pieces = buildPieces(NameFlags, false, E->getNameInfo(), 
5347                            E->getQualifierLoc().getSourceRange(),
5348                            E->getOptionalExplicitTemplateArgs());
5349     break;
5350     
5351   case CXCursor_CallExpr:
5352     if (const CXXOperatorCallExpr *OCE = 
5353         dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) {
5354       const Expr *Callee = OCE->getCallee();
5355       if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee))
5356         Callee = ICE->getSubExpr();
5357
5358       if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee))
5359         Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(),
5360                              DRE->getQualifierLoc().getSourceRange());
5361     }
5362     break;
5363     
5364   default:
5365     break;
5366   }
5367
5368   if (Pieces.empty()) {
5369     if (PieceIndex == 0)
5370       return clang_getCursorExtent(C);
5371   } else if (PieceIndex < Pieces.size()) {
5372       SourceRange R = Pieces[PieceIndex];
5373       if (R.isValid())
5374         return cxloc::translateSourceRange(getCursorContext(C), R);
5375   }
5376   
5377   return clang_getNullRange();
5378 }
5379
5380 void clang_enableStackTraces(void) {
5381   llvm::sys::PrintStackTraceOnErrorSignal();
5382 }
5383
5384 void clang_executeOnThread(void (*fn)(void*), void *user_data,
5385                            unsigned stack_size) {
5386   llvm::llvm_execute_on_thread(fn, user_data, stack_size);
5387 }
5388
5389 } // end: extern "C"
5390
5391 //===----------------------------------------------------------------------===//
5392 // Token-based Operations.
5393 //===----------------------------------------------------------------------===//
5394
5395 /* CXToken layout:
5396  *   int_data[0]: a CXTokenKind
5397  *   int_data[1]: starting token location
5398  *   int_data[2]: token length
5399  *   int_data[3]: reserved
5400  *   ptr_data: for identifiers and keywords, an IdentifierInfo*.
5401  *   otherwise unused.
5402  */
5403 extern "C" {
5404
5405 CXTokenKind clang_getTokenKind(CXToken CXTok) {
5406   return static_cast<CXTokenKind>(CXTok.int_data[0]);
5407 }
5408
5409 CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
5410   switch (clang_getTokenKind(CXTok)) {
5411   case CXToken_Identifier:
5412   case CXToken_Keyword:
5413     // We know we have an IdentifierInfo*, so use that.
5414     return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data)
5415                             ->getNameStart());
5416
5417   case CXToken_Literal: {
5418     // We have stashed the starting pointer in the ptr_data field. Use it.
5419     const char *Text = static_cast<const char *>(CXTok.ptr_data);
5420     return cxstring::createDup(StringRef(Text, CXTok.int_data[2]));
5421   }
5422
5423   case CXToken_Punctuation:
5424   case CXToken_Comment:
5425     break;
5426   }
5427
5428   if (isNotUsableTU(TU)) {
5429     LOG_BAD_TU(TU);
5430     return cxstring::createEmpty();
5431   }
5432
5433   // We have to find the starting buffer pointer the hard way, by
5434   // deconstructing the source location.
5435   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5436   if (!CXXUnit)
5437     return cxstring::createEmpty();
5438
5439   SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
5440   std::pair<FileID, unsigned> LocInfo
5441     = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
5442   bool Invalid = false;
5443   StringRef Buffer
5444     = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
5445   if (Invalid)
5446     return cxstring::createEmpty();
5447
5448   return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
5449 }
5450
5451 CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
5452   if (isNotUsableTU(TU)) {
5453     LOG_BAD_TU(TU);
5454     return clang_getNullLocation();
5455   }
5456
5457   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5458   if (!CXXUnit)
5459     return clang_getNullLocation();
5460
5461   return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
5462                         SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
5463 }
5464
5465 CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
5466   if (isNotUsableTU(TU)) {
5467     LOG_BAD_TU(TU);
5468     return clang_getNullRange();
5469   }
5470
5471   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5472   if (!CXXUnit)
5473     return clang_getNullRange();
5474
5475   return cxloc::translateSourceRange(CXXUnit->getASTContext(),
5476                         SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
5477 }
5478
5479 static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
5480                       SmallVectorImpl<CXToken> &CXTokens) {
5481   SourceManager &SourceMgr = CXXUnit->getSourceManager();
5482   std::pair<FileID, unsigned> BeginLocInfo
5483     = SourceMgr.getDecomposedSpellingLoc(Range.getBegin());
5484   std::pair<FileID, unsigned> EndLocInfo
5485     = SourceMgr.getDecomposedSpellingLoc(Range.getEnd());
5486
5487   // Cannot tokenize across files.
5488   if (BeginLocInfo.first != EndLocInfo.first)
5489     return;
5490
5491   // Create a lexer
5492   bool Invalid = false;
5493   StringRef Buffer
5494     = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
5495   if (Invalid)
5496     return;
5497   
5498   Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
5499             CXXUnit->getASTContext().getLangOpts(),
5500             Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
5501   Lex.SetCommentRetentionState(true);
5502
5503   // Lex tokens until we hit the end of the range.
5504   const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
5505   Token Tok;
5506   bool previousWasAt = false;
5507   do {
5508     // Lex the next token
5509     Lex.LexFromRawLexer(Tok);
5510     if (Tok.is(tok::eof))
5511       break;
5512
5513     // Initialize the CXToken.
5514     CXToken CXTok;
5515
5516     //   - Common fields
5517     CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
5518     CXTok.int_data[2] = Tok.getLength();
5519     CXTok.int_data[3] = 0;
5520
5521     //   - Kind-specific fields
5522     if (Tok.isLiteral()) {
5523       CXTok.int_data[0] = CXToken_Literal;
5524       CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData());
5525     } else if (Tok.is(tok::raw_identifier)) {
5526       // Lookup the identifier to determine whether we have a keyword.
5527       IdentifierInfo *II
5528         = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
5529
5530       if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
5531         CXTok.int_data[0] = CXToken_Keyword;
5532       }
5533       else {
5534         CXTok.int_data[0] = Tok.is(tok::identifier)
5535           ? CXToken_Identifier
5536           : CXToken_Keyword;
5537       }
5538       CXTok.ptr_data = II;
5539     } else if (Tok.is(tok::comment)) {
5540       CXTok.int_data[0] = CXToken_Comment;
5541       CXTok.ptr_data = nullptr;
5542     } else {
5543       CXTok.int_data[0] = CXToken_Punctuation;
5544       CXTok.ptr_data = nullptr;
5545     }
5546     CXTokens.push_back(CXTok);
5547     previousWasAt = Tok.is(tok::at);
5548   } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
5549 }
5550
5551 void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
5552                     CXToken **Tokens, unsigned *NumTokens) {
5553   LOG_FUNC_SECTION {
5554     *Log << TU << ' ' << Range;
5555   }
5556
5557   if (Tokens)
5558     *Tokens = nullptr;
5559   if (NumTokens)
5560     *NumTokens = 0;
5561
5562   if (isNotUsableTU(TU)) {
5563     LOG_BAD_TU(TU);
5564     return;
5565   }
5566
5567   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5568   if (!CXXUnit || !Tokens || !NumTokens)
5569     return;
5570
5571   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
5572   
5573   SourceRange R = cxloc::translateCXSourceRange(Range);
5574   if (R.isInvalid())
5575     return;
5576
5577   SmallVector<CXToken, 32> CXTokens;
5578   getTokens(CXXUnit, R, CXTokens);
5579
5580   if (CXTokens.empty())
5581     return;
5582
5583   *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
5584   memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
5585   *NumTokens = CXTokens.size();
5586 }
5587
5588 void clang_disposeTokens(CXTranslationUnit TU,
5589                          CXToken *Tokens, unsigned NumTokens) {
5590   free(Tokens);
5591 }
5592
5593 } // end: extern "C"
5594
5595 //===----------------------------------------------------------------------===//
5596 // Token annotation APIs.
5597 //===----------------------------------------------------------------------===//
5598
5599 static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
5600                                                      CXCursor parent,
5601                                                      CXClientData client_data);
5602 static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
5603                                               CXClientData client_data);
5604
5605 namespace {
5606 class AnnotateTokensWorker {
5607   CXToken *Tokens;
5608   CXCursor *Cursors;
5609   unsigned NumTokens;
5610   unsigned TokIdx;
5611   unsigned PreprocessingTokIdx;
5612   CursorVisitor AnnotateVis;
5613   SourceManager &SrcMgr;
5614   bool HasContextSensitiveKeywords;
5615
5616   struct PostChildrenInfo {
5617     CXCursor Cursor;
5618     SourceRange CursorRange;
5619     unsigned BeforeReachingCursorIdx;
5620     unsigned BeforeChildrenTokenIdx;
5621   };
5622   SmallVector<PostChildrenInfo, 8> PostChildrenInfos;
5623
5624   CXToken &getTok(unsigned Idx) {
5625     assert(Idx < NumTokens);
5626     return Tokens[Idx];
5627   }
5628   const CXToken &getTok(unsigned Idx) const {
5629     assert(Idx < NumTokens);
5630     return Tokens[Idx];
5631   }
5632   bool MoreTokens() const { return TokIdx < NumTokens; }
5633   unsigned NextToken() const { return TokIdx; }
5634   void AdvanceToken() { ++TokIdx; }
5635   SourceLocation GetTokenLoc(unsigned tokI) {
5636     return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]);
5637   }
5638   bool isFunctionMacroToken(unsigned tokI) const {
5639     return getTok(tokI).int_data[3] != 0;
5640   }
5641   SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
5642     return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]);
5643   }
5644
5645   void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
5646   bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
5647                                              SourceRange);
5648
5649 public:
5650   AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens,
5651                        CXTranslationUnit TU, SourceRange RegionOfInterest)
5652     : Tokens(tokens), Cursors(cursors),
5653       NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
5654       AnnotateVis(TU,
5655                   AnnotateTokensVisitor, this,
5656                   /*VisitPreprocessorLast=*/true,
5657                   /*VisitIncludedEntities=*/false,
5658                   RegionOfInterest,
5659                   /*VisitDeclsOnly=*/false,
5660                   AnnotateTokensPostChildrenVisitor),
5661       SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()),
5662       HasContextSensitiveKeywords(false) { }
5663
5664   void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
5665   enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
5666   bool postVisitChildren(CXCursor cursor);
5667   void AnnotateTokens();
5668   
5669   /// \brief Determine whether the annotator saw any cursors that have 
5670   /// context-sensitive keywords.
5671   bool hasContextSensitiveKeywords() const {
5672     return HasContextSensitiveKeywords;
5673   }
5674
5675   ~AnnotateTokensWorker() {
5676     assert(PostChildrenInfos.empty());
5677   }
5678 };
5679 }
5680
5681 void AnnotateTokensWorker::AnnotateTokens() {
5682   // Walk the AST within the region of interest, annotating tokens
5683   // along the way.
5684   AnnotateVis.visitFileRegion();
5685 }
5686
5687 static inline void updateCursorAnnotation(CXCursor &Cursor,
5688                                           const CXCursor &updateC) {
5689   if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind))
5690     return;
5691   Cursor = updateC;
5692 }
5693
5694 /// \brief It annotates and advances tokens with a cursor until the comparison
5695 //// between the cursor location and the source range is the same as
5696 /// \arg compResult.
5697 ///
5698 /// Pass RangeBefore to annotate tokens with a cursor until a range is reached.
5699 /// Pass RangeOverlap to annotate tokens inside a range.
5700 void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC,
5701                                                RangeComparisonResult compResult,
5702                                                SourceRange range) {
5703   while (MoreTokens()) {
5704     const unsigned I = NextToken();
5705     if (isFunctionMacroToken(I))
5706       if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range))
5707         return;
5708
5709     SourceLocation TokLoc = GetTokenLoc(I);
5710     if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
5711       updateCursorAnnotation(Cursors[I], updateC);
5712       AdvanceToken();
5713       continue;
5714     }
5715     break;
5716   }
5717 }
5718
5719 /// \brief Special annotation handling for macro argument tokens.
5720 /// \returns true if it advanced beyond all macro tokens, false otherwise.
5721 bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
5722                                                CXCursor updateC,
5723                                                RangeComparisonResult compResult,
5724                                                SourceRange range) {
5725   assert(MoreTokens());
5726   assert(isFunctionMacroToken(NextToken()) &&
5727          "Should be called only for macro arg tokens");
5728
5729   // This works differently than annotateAndAdvanceTokens; because expanded
5730   // macro arguments can have arbitrary translation-unit source order, we do not
5731   // advance the token index one by one until a token fails the range test.
5732   // We only advance once past all of the macro arg tokens if all of them
5733   // pass the range test. If one of them fails we keep the token index pointing
5734   // at the start of the macro arg tokens so that the failing token will be
5735   // annotated by a subsequent annotation try.
5736
5737   bool atLeastOneCompFail = false;
5738   
5739   unsigned I = NextToken();
5740   for (; I < NumTokens && isFunctionMacroToken(I); ++I) {
5741     SourceLocation TokLoc = getFunctionMacroTokenLoc(I);
5742     if (TokLoc.isFileID())
5743       continue; // not macro arg token, it's parens or comma.
5744     if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
5745       if (clang_isInvalid(clang_getCursorKind(Cursors[I])))
5746         Cursors[I] = updateC;
5747     } else
5748       atLeastOneCompFail = true;
5749   }
5750
5751   if (atLeastOneCompFail)
5752     return false;
5753
5754   TokIdx = I; // All of the tokens were handled, advance beyond all of them.
5755   return true;
5756 }
5757
5758 enum CXChildVisitResult
5759 AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {  
5760   SourceRange cursorRange = getRawCursorExtent(cursor);
5761   if (cursorRange.isInvalid())
5762     return CXChildVisit_Recurse;
5763       
5764   if (!HasContextSensitiveKeywords) {
5765     // Objective-C properties can have context-sensitive keywords.
5766     if (cursor.kind == CXCursor_ObjCPropertyDecl) {
5767       if (const ObjCPropertyDecl *Property
5768                   = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor)))
5769         HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0;
5770     }
5771     // Objective-C methods can have context-sensitive keywords.
5772     else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl ||
5773              cursor.kind == CXCursor_ObjCClassMethodDecl) {
5774       if (const ObjCMethodDecl *Method
5775             = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
5776         if (Method->getObjCDeclQualifier())
5777           HasContextSensitiveKeywords = true;
5778         else {
5779           for (const auto *P : Method->params()) {
5780             if (P->getObjCDeclQualifier()) {
5781               HasContextSensitiveKeywords = true;
5782               break;
5783             }
5784           }
5785         }
5786       }
5787     }    
5788     // C++ methods can have context-sensitive keywords.
5789     else if (cursor.kind == CXCursor_CXXMethod) {
5790       if (const CXXMethodDecl *Method
5791                   = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) {
5792         if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>())
5793           HasContextSensitiveKeywords = true;
5794       }
5795     }
5796     // C++ classes can have context-sensitive keywords.
5797     else if (cursor.kind == CXCursor_StructDecl ||
5798              cursor.kind == CXCursor_ClassDecl ||
5799              cursor.kind == CXCursor_ClassTemplate ||
5800              cursor.kind == CXCursor_ClassTemplatePartialSpecialization) {
5801       if (const Decl *D = getCursorDecl(cursor))
5802         if (D->hasAttr<FinalAttr>())
5803           HasContextSensitiveKeywords = true;
5804     }
5805   }
5806
5807   // Don't override a property annotation with its getter/setter method.
5808   if (cursor.kind == CXCursor_ObjCInstanceMethodDecl &&
5809       parent.kind == CXCursor_ObjCPropertyDecl)
5810     return CXChildVisit_Continue;
5811   
5812   if (clang_isPreprocessing(cursor.kind)) {    
5813     // Items in the preprocessing record are kept separate from items in
5814     // declarations, so we keep a separate token index.
5815     unsigned SavedTokIdx = TokIdx;
5816     TokIdx = PreprocessingTokIdx;
5817
5818     // Skip tokens up until we catch up to the beginning of the preprocessing
5819     // entry.
5820     while (MoreTokens()) {
5821       const unsigned I = NextToken();
5822       SourceLocation TokLoc = GetTokenLoc(I);
5823       switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
5824       case RangeBefore:
5825         AdvanceToken();
5826         continue;
5827       case RangeAfter:
5828       case RangeOverlap:
5829         break;
5830       }
5831       break;
5832     }
5833     
5834     // Look at all of the tokens within this range.
5835     while (MoreTokens()) {
5836       const unsigned I = NextToken();
5837       SourceLocation TokLoc = GetTokenLoc(I);
5838       switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
5839       case RangeBefore:
5840         llvm_unreachable("Infeasible");
5841       case RangeAfter:
5842         break;
5843       case RangeOverlap:
5844         // For macro expansions, just note where the beginning of the macro
5845         // expansion occurs.
5846         if (cursor.kind == CXCursor_MacroExpansion) {
5847           if (TokLoc == cursorRange.getBegin())
5848             Cursors[I] = cursor;
5849           AdvanceToken();
5850           break;
5851         }
5852         // We may have already annotated macro names inside macro definitions.
5853         if (Cursors[I].kind != CXCursor_MacroExpansion)
5854           Cursors[I] = cursor;
5855         AdvanceToken();
5856         continue;
5857       }
5858       break;
5859     }
5860
5861     // Save the preprocessing token index; restore the non-preprocessing
5862     // token index.
5863     PreprocessingTokIdx = TokIdx;
5864     TokIdx = SavedTokIdx;
5865     return CXChildVisit_Recurse;
5866   }
5867
5868   if (cursorRange.isInvalid())
5869     return CXChildVisit_Continue;
5870
5871   unsigned BeforeReachingCursorIdx = NextToken();
5872   const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
5873   const enum CXCursorKind K = clang_getCursorKind(parent);
5874   const CXCursor updateC =
5875     (clang_isInvalid(K) || K == CXCursor_TranslationUnit ||
5876      // Attributes are annotated out-of-order, skip tokens until we reach it.
5877      clang_isAttribute(cursor.kind))
5878      ? clang_getNullCursor() : parent;
5879
5880   annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange);
5881
5882   // Avoid having the cursor of an expression "overwrite" the annotation of the
5883   // variable declaration that it belongs to.
5884   // This can happen for C++ constructor expressions whose range generally
5885   // include the variable declaration, e.g.:
5886   //  MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor.
5887   if (clang_isExpression(cursorK) && MoreTokens()) {
5888     const Expr *E = getCursorExpr(cursor);
5889     if (const Decl *D = getCursorParentDecl(cursor)) {
5890       const unsigned I = NextToken();
5891       if (E->getLocStart().isValid() && D->getLocation().isValid() &&
5892           E->getLocStart() == D->getLocation() &&
5893           E->getLocStart() == GetTokenLoc(I)) {
5894         updateCursorAnnotation(Cursors[I], updateC);
5895         AdvanceToken();
5896       }
5897     }
5898   }
5899
5900   // Before recursing into the children keep some state that we are going
5901   // to use in the AnnotateTokensWorker::postVisitChildren callback to do some
5902   // extra work after the child nodes are visited.
5903   // Note that we don't call VisitChildren here to avoid traversing statements
5904   // code-recursively which can blow the stack.
5905
5906   PostChildrenInfo Info;
5907   Info.Cursor = cursor;
5908   Info.CursorRange = cursorRange;
5909   Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx;
5910   Info.BeforeChildrenTokenIdx = NextToken();
5911   PostChildrenInfos.push_back(Info);
5912
5913   return CXChildVisit_Recurse;
5914 }
5915
5916 bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) {
5917   if (PostChildrenInfos.empty())
5918     return false;
5919   const PostChildrenInfo &Info = PostChildrenInfos.back();
5920   if (!clang_equalCursors(Info.Cursor, cursor))
5921     return false;
5922
5923   const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx;
5924   const unsigned AfterChildren = NextToken();
5925   SourceRange cursorRange = Info.CursorRange;
5926
5927   // Scan the tokens that are at the end of the cursor, but are not captured
5928   // but the child cursors.
5929   annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange);
5930
5931   // Scan the tokens that are at the beginning of the cursor, but are not
5932   // capture by the child cursors.
5933   for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
5934     if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
5935       break;
5936
5937     Cursors[I] = cursor;
5938   }
5939
5940   // Attributes are annotated out-of-order, rewind TokIdx to when we first
5941   // encountered the attribute cursor.
5942   if (clang_isAttribute(cursor.kind))
5943     TokIdx = Info.BeforeReachingCursorIdx;
5944
5945   PostChildrenInfos.pop_back();
5946   return false;
5947 }
5948
5949 static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
5950                                                      CXCursor parent,
5951                                                      CXClientData client_data) {
5952   return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
5953 }
5954
5955 static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
5956                                               CXClientData client_data) {
5957   return static_cast<AnnotateTokensWorker*>(client_data)->
5958                                                       postVisitChildren(cursor);
5959 }
5960
5961 namespace {
5962
5963 /// \brief Uses the macro expansions in the preprocessing record to find
5964 /// and mark tokens that are macro arguments. This info is used by the
5965 /// AnnotateTokensWorker.
5966 class MarkMacroArgTokensVisitor {
5967   SourceManager &SM;
5968   CXToken *Tokens;
5969   unsigned NumTokens;
5970   unsigned CurIdx;
5971   
5972 public:
5973   MarkMacroArgTokensVisitor(SourceManager &SM,
5974                             CXToken *tokens, unsigned numTokens)
5975     : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { }
5976
5977   CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {
5978     if (cursor.kind != CXCursor_MacroExpansion)
5979       return CXChildVisit_Continue;
5980
5981     SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange();
5982     if (macroRange.getBegin() == macroRange.getEnd())
5983       return CXChildVisit_Continue; // it's not a function macro.
5984
5985     for (; CurIdx < NumTokens; ++CurIdx) {
5986       if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx),
5987                                         macroRange.getBegin()))
5988         break;
5989     }
5990     
5991     if (CurIdx == NumTokens)
5992       return CXChildVisit_Break;
5993
5994     for (; CurIdx < NumTokens; ++CurIdx) {
5995       SourceLocation tokLoc = getTokenLoc(CurIdx);
5996       if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd()))
5997         break;
5998
5999       setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc));
6000     }
6001
6002     if (CurIdx == NumTokens)
6003       return CXChildVisit_Break;
6004
6005     return CXChildVisit_Continue;
6006   }
6007
6008 private:
6009   CXToken &getTok(unsigned Idx) {
6010     assert(Idx < NumTokens);
6011     return Tokens[Idx];
6012   }
6013   const CXToken &getTok(unsigned Idx) const {
6014     assert(Idx < NumTokens);
6015     return Tokens[Idx];
6016   }
6017
6018   SourceLocation getTokenLoc(unsigned tokI) {
6019     return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]);
6020   }
6021
6022   void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
6023     // The third field is reserved and currently not used. Use it here
6024     // to mark macro arg expanded tokens with their expanded locations.
6025     getTok(tokI).int_data[3] = loc.getRawEncoding();
6026   }
6027 };
6028
6029 } // end anonymous namespace
6030
6031 static CXChildVisitResult
6032 MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
6033                                   CXClientData client_data) {
6034   return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor,
6035                                                                      parent);
6036 }
6037
6038 /// \brief Used by \c annotatePreprocessorTokens.
6039 /// \returns true if lexing was finished, false otherwise.
6040 static bool lexNext(Lexer &Lex, Token &Tok,
6041                    unsigned &NextIdx, unsigned NumTokens) {
6042   if (NextIdx >= NumTokens)
6043     return true;
6044
6045   ++NextIdx;
6046   Lex.LexFromRawLexer(Tok);
6047   if (Tok.is(tok::eof))
6048     return true;
6049
6050   return false;
6051 }
6052
6053 static void annotatePreprocessorTokens(CXTranslationUnit TU,
6054                                        SourceRange RegionOfInterest,
6055                                        CXCursor *Cursors,
6056                                        CXToken *Tokens,
6057                                        unsigned NumTokens) {
6058   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
6059
6060   Preprocessor &PP = CXXUnit->getPreprocessor();
6061   SourceManager &SourceMgr = CXXUnit->getSourceManager();
6062   std::pair<FileID, unsigned> BeginLocInfo
6063     = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin());
6064   std::pair<FileID, unsigned> EndLocInfo
6065     = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd());
6066
6067   if (BeginLocInfo.first != EndLocInfo.first)
6068     return;
6069
6070   StringRef Buffer;
6071   bool Invalid = false;
6072   Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
6073   if (Buffer.empty() || Invalid)
6074     return;
6075
6076   Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
6077             CXXUnit->getASTContext().getLangOpts(),
6078             Buffer.begin(), Buffer.data() + BeginLocInfo.second,
6079             Buffer.end());
6080   Lex.SetCommentRetentionState(true);
6081   
6082   unsigned NextIdx = 0;
6083   // Lex tokens in raw mode until we hit the end of the range, to avoid
6084   // entering #includes or expanding macros.
6085   while (true) {
6086     Token Tok;
6087     if (lexNext(Lex, Tok, NextIdx, NumTokens))
6088       break;
6089     unsigned TokIdx = NextIdx-1;
6090     assert(Tok.getLocation() ==
6091              SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1]));
6092     
6093   reprocess:
6094     if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
6095       // We have found a preprocessing directive. Annotate the tokens
6096       // appropriately.
6097       //
6098       // FIXME: Some simple tests here could identify macro definitions and
6099       // #undefs, to provide specific cursor kinds for those.
6100
6101       SourceLocation BeginLoc = Tok.getLocation();
6102       if (lexNext(Lex, Tok, NextIdx, NumTokens))
6103         break;
6104
6105       MacroInfo *MI = nullptr;
6106       if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") {
6107         if (lexNext(Lex, Tok, NextIdx, NumTokens))
6108           break;
6109
6110         if (Tok.is(tok::raw_identifier)) {
6111           IdentifierInfo &II =
6112               PP.getIdentifierTable().get(Tok.getRawIdentifier());
6113           SourceLocation MappedTokLoc =
6114               CXXUnit->mapLocationToPreamble(Tok.getLocation());
6115           MI = getMacroInfo(II, MappedTokLoc, TU);
6116         }
6117       }
6118
6119       bool finished = false;
6120       do {
6121         if (lexNext(Lex, Tok, NextIdx, NumTokens)) {
6122           finished = true;
6123           break;
6124         }
6125         // If we are in a macro definition, check if the token was ever a
6126         // macro name and annotate it if that's the case.
6127         if (MI) {
6128           SourceLocation SaveLoc = Tok.getLocation();
6129           Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc));
6130           MacroDefinitionRecord *MacroDef =
6131               checkForMacroInMacroDefinition(MI, Tok, TU);
6132           Tok.setLocation(SaveLoc);
6133           if (MacroDef)
6134             Cursors[NextIdx - 1] =
6135                 MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU);
6136         }
6137       } while (!Tok.isAtStartOfLine());
6138
6139       unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2;
6140       assert(TokIdx <= LastIdx);
6141       SourceLocation EndLoc =
6142           SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]);
6143       CXCursor Cursor =
6144           MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU);
6145
6146       for (; TokIdx <= LastIdx; ++TokIdx)
6147         updateCursorAnnotation(Cursors[TokIdx], Cursor);
6148       
6149       if (finished)
6150         break;
6151       goto reprocess;
6152     }
6153   }
6154 }
6155
6156 // This gets run a separate thread to avoid stack blowout.
6157 static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit,
6158                                      CXToken *Tokens, unsigned NumTokens,
6159                                      CXCursor *Cursors) {
6160   CIndexer *CXXIdx = TU->CIdx;
6161   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
6162     setThreadBackgroundPriority();
6163
6164   // Determine the region of interest, which contains all of the tokens.
6165   SourceRange RegionOfInterest;
6166   RegionOfInterest.setBegin(
6167     cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
6168   RegionOfInterest.setEnd(
6169     cxloc::translateSourceLocation(clang_getTokenLocation(TU,
6170                                                          Tokens[NumTokens-1])));
6171
6172   // Relex the tokens within the source range to look for preprocessing
6173   // directives.
6174   annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens);
6175
6176   // If begin location points inside a macro argument, set it to the expansion
6177   // location so we can have the full context when annotating semantically.
6178   {
6179     SourceManager &SM = CXXUnit->getSourceManager();
6180     SourceLocation Loc =
6181         SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin());
6182     if (Loc.isMacroID())
6183       RegionOfInterest.setBegin(SM.getExpansionLoc(Loc));
6184   }
6185
6186   if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
6187     // Search and mark tokens that are macro argument expansions.
6188     MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(),
6189                                       Tokens, NumTokens);
6190     CursorVisitor MacroArgMarker(TU,
6191                                  MarkMacroArgTokensVisitorDelegate, &Visitor,
6192                                  /*VisitPreprocessorLast=*/true,
6193                                  /*VisitIncludedEntities=*/false,
6194                                  RegionOfInterest);
6195     MacroArgMarker.visitPreprocessedEntitiesInRegion();
6196   }
6197   
6198   // Annotate all of the source locations in the region of interest that map to
6199   // a specific cursor.
6200   AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest);
6201   
6202   // FIXME: We use a ridiculous stack size here because the data-recursion
6203   // algorithm uses a large stack frame than the non-data recursive version,
6204   // and AnnotationTokensWorker currently transforms the data-recursion
6205   // algorithm back into a traditional recursion by explicitly calling
6206   // VisitChildren().  We will need to remove this explicit recursive call.
6207   W.AnnotateTokens();
6208
6209   // If we ran into any entities that involve context-sensitive keywords,
6210   // take another pass through the tokens to mark them as such.
6211   if (W.hasContextSensitiveKeywords()) {
6212     for (unsigned I = 0; I != NumTokens; ++I) {
6213       if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier)
6214         continue;
6215       
6216       if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) {
6217         IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
6218         if (const ObjCPropertyDecl *Property
6219             = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) {
6220           if (Property->getPropertyAttributesAsWritten() != 0 &&
6221               llvm::StringSwitch<bool>(II->getName())
6222               .Case("readonly", true)
6223               .Case("assign", true)
6224               .Case("unsafe_unretained", true)
6225               .Case("readwrite", true)
6226               .Case("retain", true)
6227               .Case("copy", true)
6228               .Case("nonatomic", true)
6229               .Case("atomic", true)
6230               .Case("getter", true)
6231               .Case("setter", true)
6232               .Case("strong", true)
6233               .Case("weak", true)
6234               .Default(false))
6235             Tokens[I].int_data[0] = CXToken_Keyword;
6236         }
6237         continue;
6238       }
6239       
6240       if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl ||
6241           Cursors[I].kind == CXCursor_ObjCClassMethodDecl) {
6242         IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
6243         if (llvm::StringSwitch<bool>(II->getName())
6244             .Case("in", true)
6245             .Case("out", true)
6246             .Case("inout", true)
6247             .Case("oneway", true)
6248             .Case("bycopy", true)
6249             .Case("byref", true)
6250             .Default(false))
6251           Tokens[I].int_data[0] = CXToken_Keyword;
6252         continue;
6253       }
6254
6255       if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
6256           Cursors[I].kind == CXCursor_CXXOverrideAttr) {
6257         Tokens[I].int_data[0] = CXToken_Keyword;
6258         continue;
6259       }
6260     }
6261   }
6262 }
6263
6264 extern "C" {
6265
6266 void clang_annotateTokens(CXTranslationUnit TU,
6267                           CXToken *Tokens, unsigned NumTokens,
6268                           CXCursor *Cursors) {
6269   if (isNotUsableTU(TU)) {
6270     LOG_BAD_TU(TU);
6271     return;
6272   }
6273   if (NumTokens == 0 || !Tokens || !Cursors) {
6274     LOG_FUNC_SECTION { *Log << "<null input>"; }
6275     return;
6276   }
6277
6278   LOG_FUNC_SECTION {
6279     *Log << TU << ' ';
6280     CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]);
6281     CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]);
6282     *Log << clang_getRange(bloc, eloc);
6283   }
6284
6285   // Any token we don't specifically annotate will have a NULL cursor.
6286   CXCursor C = clang_getNullCursor();
6287   for (unsigned I = 0; I != NumTokens; ++I)
6288     Cursors[I] = C;
6289
6290   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
6291   if (!CXXUnit)
6292     return;
6293
6294   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
6295
6296   auto AnnotateTokensImpl = [=]() {
6297     clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors);
6298   };
6299   llvm::CrashRecoveryContext CRC;
6300   if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) {
6301     fprintf(stderr, "libclang: crash detected while annotating tokens\n");
6302   }
6303 }
6304
6305 } // end: extern "C"
6306
6307 //===----------------------------------------------------------------------===//
6308 // Operations for querying linkage of a cursor.
6309 //===----------------------------------------------------------------------===//
6310
6311 extern "C" {
6312 CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
6313   if (!clang_isDeclaration(cursor.kind))
6314     return CXLinkage_Invalid;
6315
6316   const Decl *D = cxcursor::getCursorDecl(cursor);
6317   if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
6318     switch (ND->getLinkageInternal()) {
6319       case NoLinkage:
6320       case VisibleNoLinkage: return CXLinkage_NoLinkage;
6321       case InternalLinkage: return CXLinkage_Internal;
6322       case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
6323       case ExternalLinkage: return CXLinkage_External;
6324     };
6325
6326   return CXLinkage_Invalid;
6327 }
6328 } // end: extern "C"
6329
6330 //===----------------------------------------------------------------------===//
6331 // Operations for querying language of a cursor.
6332 //===----------------------------------------------------------------------===//
6333
6334 static CXLanguageKind getDeclLanguage(const Decl *D) {
6335   if (!D)
6336     return CXLanguage_C;
6337
6338   switch (D->getKind()) {
6339     default:
6340       break;
6341     case Decl::ImplicitParam:
6342     case Decl::ObjCAtDefsField:
6343     case Decl::ObjCCategory:
6344     case Decl::ObjCCategoryImpl:
6345     case Decl::ObjCCompatibleAlias:
6346     case Decl::ObjCImplementation:
6347     case Decl::ObjCInterface:
6348     case Decl::ObjCIvar:
6349     case Decl::ObjCMethod:
6350     case Decl::ObjCProperty:
6351     case Decl::ObjCPropertyImpl:
6352     case Decl::ObjCProtocol:
6353     case Decl::ObjCTypeParam:
6354       return CXLanguage_ObjC;
6355     case Decl::CXXConstructor:
6356     case Decl::CXXConversion:
6357     case Decl::CXXDestructor:
6358     case Decl::CXXMethod:
6359     case Decl::CXXRecord:
6360     case Decl::ClassTemplate:
6361     case Decl::ClassTemplatePartialSpecialization:
6362     case Decl::ClassTemplateSpecialization:
6363     case Decl::Friend:
6364     case Decl::FriendTemplate:
6365     case Decl::FunctionTemplate:
6366     case Decl::LinkageSpec:
6367     case Decl::Namespace:
6368     case Decl::NamespaceAlias:
6369     case Decl::NonTypeTemplateParm:
6370     case Decl::StaticAssert:
6371     case Decl::TemplateTemplateParm:
6372     case Decl::TemplateTypeParm:
6373     case Decl::UnresolvedUsingTypename:
6374     case Decl::UnresolvedUsingValue:
6375     case Decl::Using:
6376     case Decl::UsingDirective:
6377     case Decl::UsingShadow:
6378       return CXLanguage_CPlusPlus;
6379   }
6380
6381   return CXLanguage_C;
6382 }
6383
6384 extern "C" {
6385
6386 static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) {
6387   if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())
6388     return CXAvailability_Available;
6389   
6390   switch (D->getAvailability()) {
6391   case AR_Available:
6392   case AR_NotYetIntroduced:
6393     if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D))
6394       return getCursorAvailabilityForDecl(
6395           cast<Decl>(EnumConst->getDeclContext()));
6396     return CXAvailability_Available;
6397
6398   case AR_Deprecated:
6399     return CXAvailability_Deprecated;
6400
6401   case AR_Unavailable:
6402     return CXAvailability_NotAvailable;
6403   }
6404
6405   llvm_unreachable("Unknown availability kind!");
6406 }
6407
6408 enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
6409   if (clang_isDeclaration(cursor.kind))
6410     if (const Decl *D = cxcursor::getCursorDecl(cursor))
6411       return getCursorAvailabilityForDecl(D);
6412
6413   return CXAvailability_Available;
6414 }
6415
6416 static CXVersion convertVersion(VersionTuple In) {
6417   CXVersion Out = { -1, -1, -1 };
6418   if (In.empty())
6419     return Out;
6420
6421   Out.Major = In.getMajor();
6422   
6423   Optional<unsigned> Minor = In.getMinor();
6424   if (Minor.hasValue())
6425     Out.Minor = *Minor;
6426   else
6427     return Out;
6428
6429   Optional<unsigned> Subminor = In.getSubminor();
6430   if (Subminor.hasValue())
6431     Out.Subminor = *Subminor;
6432   
6433   return Out;
6434 }
6435
6436 static int getCursorPlatformAvailabilityForDecl(const Decl *D,
6437                                                 int *always_deprecated,
6438                                                 CXString *deprecated_message,
6439                                                 int *always_unavailable,
6440                                                 CXString *unavailable_message,
6441                                            CXPlatformAvailability *availability,
6442                                                 int availability_size) {
6443   bool HadAvailAttr = false;
6444   int N = 0;
6445   for (auto A : D->attrs()) {
6446     if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
6447       HadAvailAttr = true;
6448       if (always_deprecated)
6449         *always_deprecated = 1;
6450       if (deprecated_message) {
6451         clang_disposeString(*deprecated_message);
6452         *deprecated_message = cxstring::createDup(Deprecated->getMessage());
6453       }
6454       continue;
6455     }
6456     
6457     if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) {
6458       HadAvailAttr = true;
6459       if (always_unavailable)
6460         *always_unavailable = 1;
6461       if (unavailable_message) {
6462         clang_disposeString(*unavailable_message);
6463         *unavailable_message = cxstring::createDup(Unavailable->getMessage());
6464       }
6465       continue;
6466     }
6467     
6468     if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) {
6469       HadAvailAttr = true;
6470       if (N < availability_size) {
6471         availability[N].Platform
6472           = cxstring::createDup(Avail->getPlatform()->getName());
6473         availability[N].Introduced = convertVersion(Avail->getIntroduced());
6474         availability[N].Deprecated = convertVersion(Avail->getDeprecated());
6475         availability[N].Obsoleted = convertVersion(Avail->getObsoleted());
6476         availability[N].Unavailable = Avail->getUnavailable();
6477         availability[N].Message = cxstring::createDup(Avail->getMessage());
6478       }
6479       ++N;
6480     }
6481   }
6482
6483   if (!HadAvailAttr)
6484     if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D))
6485       return getCursorPlatformAvailabilityForDecl(
6486                                         cast<Decl>(EnumConst->getDeclContext()),
6487                                                   always_deprecated,
6488                                                   deprecated_message,
6489                                                   always_unavailable,
6490                                                   unavailable_message,
6491                                                   availability,
6492                                                   availability_size);
6493   
6494   return N;
6495 }
6496
6497 int clang_getCursorPlatformAvailability(CXCursor cursor,
6498                                         int *always_deprecated,
6499                                         CXString *deprecated_message,
6500                                         int *always_unavailable,
6501                                         CXString *unavailable_message,
6502                                         CXPlatformAvailability *availability,
6503                                         int availability_size) {
6504   if (always_deprecated)
6505     *always_deprecated = 0;
6506   if (deprecated_message)
6507     *deprecated_message = cxstring::createEmpty();
6508   if (always_unavailable)
6509     *always_unavailable = 0;
6510   if (unavailable_message)
6511     *unavailable_message = cxstring::createEmpty();
6512
6513   if (!clang_isDeclaration(cursor.kind))
6514     return 0;
6515
6516   const Decl *D = cxcursor::getCursorDecl(cursor);
6517   if (!D)
6518     return 0;
6519
6520   return getCursorPlatformAvailabilityForDecl(D, always_deprecated,
6521                                               deprecated_message,
6522                                               always_unavailable,
6523                                               unavailable_message,
6524                                               availability,
6525                                               availability_size);
6526 }
6527   
6528 void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) {
6529   clang_disposeString(availability->Platform);
6530   clang_disposeString(availability->Message);
6531 }
6532
6533 CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
6534   if (clang_isDeclaration(cursor.kind))
6535     return getDeclLanguage(cxcursor::getCursorDecl(cursor));
6536
6537   return CXLanguage_Invalid;
6538 }
6539
6540  /// \brief If the given cursor is the "templated" declaration
6541  /// descibing a class or function template, return the class or
6542  /// function template.
6543 static const Decl *maybeGetTemplateCursor(const Decl *D) {
6544   if (!D)
6545     return nullptr;
6546
6547   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
6548     if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
6549       return FunTmpl;
6550
6551   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
6552     if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
6553       return ClassTmpl;
6554
6555   return D;
6556 }
6557
6558
6559 enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) {
6560   StorageClass sc = SC_None;
6561   const Decl *D = getCursorDecl(C);
6562   if (D) {
6563     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
6564       sc = FD->getStorageClass();
6565     } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
6566       sc = VD->getStorageClass();
6567     } else {
6568       return CX_SC_Invalid;
6569     }
6570   } else {
6571     return CX_SC_Invalid;
6572   }
6573   switch (sc) {
6574   case SC_None:
6575     return CX_SC_None;
6576   case SC_Extern:
6577     return CX_SC_Extern;
6578   case SC_Static:
6579     return CX_SC_Static;
6580   case SC_PrivateExtern:
6581     return CX_SC_PrivateExtern;
6582   case SC_OpenCLWorkGroupLocal:
6583     return CX_SC_OpenCLWorkGroupLocal;
6584   case SC_Auto:
6585     return CX_SC_Auto;
6586   case SC_Register:
6587     return CX_SC_Register;
6588   }
6589   llvm_unreachable("Unhandled storage class!");
6590 }
6591
6592 CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
6593   if (clang_isDeclaration(cursor.kind)) {
6594     if (const Decl *D = getCursorDecl(cursor)) {
6595       const DeclContext *DC = D->getDeclContext();
6596       if (!DC)
6597         return clang_getNullCursor();
6598
6599       return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 
6600                           getCursorTU(cursor));
6601     }
6602   }
6603   
6604   if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
6605     if (const Decl *D = getCursorDecl(cursor))
6606       return MakeCXCursor(D, getCursorTU(cursor));
6607   }
6608   
6609   return clang_getNullCursor();
6610 }
6611
6612 CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
6613   if (clang_isDeclaration(cursor.kind)) {
6614     if (const Decl *D = getCursorDecl(cursor)) {
6615       const DeclContext *DC = D->getLexicalDeclContext();
6616       if (!DC)
6617         return clang_getNullCursor();
6618
6619       return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), 
6620                           getCursorTU(cursor));
6621     }
6622   }
6623
6624   // FIXME: Note that we can't easily compute the lexical context of a 
6625   // statement or expression, so we return nothing.
6626   return clang_getNullCursor();
6627 }
6628
6629 CXFile clang_getIncludedFile(CXCursor cursor) {
6630   if (cursor.kind != CXCursor_InclusionDirective)
6631     return nullptr;
6632
6633   const InclusionDirective *ID = getCursorInclusionDirective(cursor);
6634   return const_cast<FileEntry *>(ID->getFile());
6635 }
6636
6637 unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
6638   if (C.kind != CXCursor_ObjCPropertyDecl)
6639     return CXObjCPropertyAttr_noattr;
6640
6641   unsigned Result = CXObjCPropertyAttr_noattr;
6642   const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C));
6643   ObjCPropertyDecl::PropertyAttributeKind Attr =
6644       PD->getPropertyAttributesAsWritten();
6645
6646 #define SET_CXOBJCPROP_ATTR(A) \
6647   if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \
6648     Result |= CXObjCPropertyAttr_##A
6649   SET_CXOBJCPROP_ATTR(readonly);
6650   SET_CXOBJCPROP_ATTR(getter);
6651   SET_CXOBJCPROP_ATTR(assign);
6652   SET_CXOBJCPROP_ATTR(readwrite);
6653   SET_CXOBJCPROP_ATTR(retain);
6654   SET_CXOBJCPROP_ATTR(copy);
6655   SET_CXOBJCPROP_ATTR(nonatomic);
6656   SET_CXOBJCPROP_ATTR(setter);
6657   SET_CXOBJCPROP_ATTR(atomic);
6658   SET_CXOBJCPROP_ATTR(weak);
6659   SET_CXOBJCPROP_ATTR(strong);
6660   SET_CXOBJCPROP_ATTR(unsafe_unretained);
6661 #undef SET_CXOBJCPROP_ATTR
6662
6663   return Result;
6664 }
6665
6666 unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) {
6667   if (!clang_isDeclaration(C.kind))
6668     return CXObjCDeclQualifier_None;
6669
6670   Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None;
6671   const Decl *D = getCursorDecl(C);
6672   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6673     QT = MD->getObjCDeclQualifier();
6674   else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
6675     QT = PD->getObjCDeclQualifier();
6676   if (QT == Decl::OBJC_TQ_None)
6677     return CXObjCDeclQualifier_None;
6678
6679   unsigned Result = CXObjCDeclQualifier_None;
6680   if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In;
6681   if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout;
6682   if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out;
6683   if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy;
6684   if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref;
6685   if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway;
6686
6687   return Result;
6688 }
6689
6690 unsigned clang_Cursor_isObjCOptional(CXCursor C) {
6691   if (!clang_isDeclaration(C.kind))
6692     return 0;
6693
6694   const Decl *D = getCursorDecl(C);
6695   if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
6696     return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional;
6697   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6698     return MD->getImplementationControl() == ObjCMethodDecl::Optional;
6699
6700   return 0;
6701 }
6702
6703 unsigned clang_Cursor_isVariadic(CXCursor C) {
6704   if (!clang_isDeclaration(C.kind))
6705     return 0;
6706
6707   const Decl *D = getCursorDecl(C);
6708   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
6709     return FD->isVariadic();
6710   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
6711     return MD->isVariadic();
6712
6713   return 0;
6714 }
6715
6716 CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
6717   if (!clang_isDeclaration(C.kind))
6718     return clang_getNullRange();
6719
6720   const Decl *D = getCursorDecl(C);
6721   ASTContext &Context = getCursorContext(C);
6722   const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6723   if (!RC)
6724     return clang_getNullRange();
6725
6726   return cxloc::translateSourceRange(Context, RC->getSourceRange());
6727 }
6728
6729 CXString clang_Cursor_getRawCommentText(CXCursor C) {
6730   if (!clang_isDeclaration(C.kind))
6731     return cxstring::createNull();
6732
6733   const Decl *D = getCursorDecl(C);
6734   ASTContext &Context = getCursorContext(C);
6735   const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6736   StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) :
6737                            StringRef();
6738
6739   // Don't duplicate the string because RawText points directly into source
6740   // code.
6741   return cxstring::createRef(RawText);
6742 }
6743
6744 CXString clang_Cursor_getBriefCommentText(CXCursor C) {
6745   if (!clang_isDeclaration(C.kind))
6746     return cxstring::createNull();
6747
6748   const Decl *D = getCursorDecl(C);
6749   const ASTContext &Context = getCursorContext(C);
6750   const RawComment *RC = Context.getRawCommentForAnyRedecl(D);
6751
6752   if (RC) {
6753     StringRef BriefText = RC->getBriefText(Context);
6754
6755     // Don't duplicate the string because RawComment ensures that this memory
6756     // will not go away.
6757     return cxstring::createRef(BriefText);
6758   }
6759
6760   return cxstring::createNull();
6761 }
6762
6763 CXModule clang_Cursor_getModule(CXCursor C) {
6764   if (C.kind == CXCursor_ModuleImportDecl) {
6765     if (const ImportDecl *ImportD =
6766             dyn_cast_or_null<ImportDecl>(getCursorDecl(C)))
6767       return ImportD->getImportedModule();
6768   }
6769
6770   return nullptr;
6771 }
6772
6773 CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) {
6774   if (isNotUsableTU(TU)) {
6775     LOG_BAD_TU(TU);
6776     return nullptr;
6777   }
6778   if (!File)
6779     return nullptr;
6780   FileEntry *FE = static_cast<FileEntry *>(File);
6781   
6782   ASTUnit &Unit = *cxtu::getASTUnit(TU);
6783   HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo();
6784   ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE);
6785   
6786   return Header.getModule();
6787 }
6788
6789 CXFile clang_Module_getASTFile(CXModule CXMod) {
6790   if (!CXMod)
6791     return nullptr;
6792   Module *Mod = static_cast<Module*>(CXMod);
6793   return const_cast<FileEntry *>(Mod->getASTFile());
6794 }
6795
6796 CXModule clang_Module_getParent(CXModule CXMod) {
6797   if (!CXMod)
6798     return nullptr;
6799   Module *Mod = static_cast<Module*>(CXMod);
6800   return Mod->Parent;
6801 }
6802
6803 CXString clang_Module_getName(CXModule CXMod) {
6804   if (!CXMod)
6805     return cxstring::createEmpty();
6806   Module *Mod = static_cast<Module*>(CXMod);
6807   return cxstring::createDup(Mod->Name);
6808 }
6809
6810 CXString clang_Module_getFullName(CXModule CXMod) {
6811   if (!CXMod)
6812     return cxstring::createEmpty();
6813   Module *Mod = static_cast<Module*>(CXMod);
6814   return cxstring::createDup(Mod->getFullModuleName());
6815 }
6816
6817 int clang_Module_isSystem(CXModule CXMod) {
6818   if (!CXMod)
6819     return 0;
6820   Module *Mod = static_cast<Module*>(CXMod);
6821   return Mod->IsSystem;
6822 }
6823
6824 unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
6825                                             CXModule CXMod) {
6826   if (isNotUsableTU(TU)) {
6827     LOG_BAD_TU(TU);
6828     return 0;
6829   }
6830   if (!CXMod)
6831     return 0;
6832   Module *Mod = static_cast<Module*>(CXMod);
6833   FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
6834   ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
6835   return TopHeaders.size();
6836 }
6837
6838 CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
6839                                       CXModule CXMod, unsigned Index) {
6840   if (isNotUsableTU(TU)) {
6841     LOG_BAD_TU(TU);
6842     return nullptr;
6843   }
6844   if (!CXMod)
6845     return nullptr;
6846   Module *Mod = static_cast<Module*>(CXMod);
6847   FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
6848
6849   ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
6850   if (Index < TopHeaders.size())
6851     return const_cast<FileEntry *>(TopHeaders[Index]);
6852
6853   return nullptr;
6854 }
6855
6856 } // end: extern "C"
6857
6858 //===----------------------------------------------------------------------===//
6859 // C++ AST instrospection.
6860 //===----------------------------------------------------------------------===//
6861
6862 extern "C" {
6863 unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
6864   if (!clang_isDeclaration(C.kind))
6865     return 0;
6866
6867   const Decl *D = cxcursor::getCursorDecl(C);
6868   const CXXMethodDecl *Method =
6869       D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
6870   return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0;
6871 }
6872
6873 unsigned clang_CXXMethod_isConst(CXCursor C) {
6874   if (!clang_isDeclaration(C.kind))
6875     return 0;
6876
6877   const Decl *D = cxcursor::getCursorDecl(C);
6878   const CXXMethodDecl *Method =
6879       D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
6880   return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0;
6881 }
6882
6883 unsigned clang_CXXMethod_isStatic(CXCursor C) {
6884   if (!clang_isDeclaration(C.kind))
6885     return 0;
6886   
6887   const Decl *D = cxcursor::getCursorDecl(C);
6888   const CXXMethodDecl *Method =
6889       D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
6890   return (Method && Method->isStatic()) ? 1 : 0;
6891 }
6892
6893 unsigned clang_CXXMethod_isVirtual(CXCursor C) {
6894   if (!clang_isDeclaration(C.kind))
6895     return 0;
6896   
6897   const Decl *D = cxcursor::getCursorDecl(C);
6898   const CXXMethodDecl *Method =
6899       D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr;
6900   return (Method && Method->isVirtual()) ? 1 : 0;
6901 }
6902 } // end: extern "C"
6903
6904 //===----------------------------------------------------------------------===//
6905 // Attribute introspection.
6906 //===----------------------------------------------------------------------===//
6907
6908 extern "C" {
6909 CXType clang_getIBOutletCollectionType(CXCursor C) {
6910   if (C.kind != CXCursor_IBOutletCollectionAttr)
6911     return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
6912   
6913   const IBOutletCollectionAttr *A =
6914     cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
6915   
6916   return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));  
6917 }
6918 } // end: extern "C"
6919
6920 //===----------------------------------------------------------------------===//
6921 // Inspecting memory usage.
6922 //===----------------------------------------------------------------------===//
6923
6924 typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
6925
6926 static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
6927                                               enum CXTUResourceUsageKind k,
6928                                               unsigned long amount) {
6929   CXTUResourceUsageEntry entry = { k, amount };
6930   entries.push_back(entry);
6931 }
6932
6933 extern "C" {
6934
6935 const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
6936   const char *str = "";
6937   switch (kind) {
6938     case CXTUResourceUsage_AST:
6939       str = "ASTContext: expressions, declarations, and types"; 
6940       break;
6941     case CXTUResourceUsage_Identifiers:
6942       str = "ASTContext: identifiers";
6943       break;
6944     case CXTUResourceUsage_Selectors:
6945       str = "ASTContext: selectors";
6946       break;
6947     case CXTUResourceUsage_GlobalCompletionResults:
6948       str = "Code completion: cached global results";
6949       break;
6950     case CXTUResourceUsage_SourceManagerContentCache:
6951       str = "SourceManager: content cache allocator";
6952       break;
6953     case CXTUResourceUsage_AST_SideTables:
6954       str = "ASTContext: side tables";
6955       break;
6956     case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
6957       str = "SourceManager: malloc'ed memory buffers";
6958       break;
6959     case CXTUResourceUsage_SourceManager_Membuffer_MMap:
6960       str = "SourceManager: mmap'ed memory buffers";
6961       break;
6962     case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
6963       str = "ExternalASTSource: malloc'ed memory buffers";
6964       break;
6965     case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
6966       str = "ExternalASTSource: mmap'ed memory buffers";
6967       break;
6968     case CXTUResourceUsage_Preprocessor:
6969       str = "Preprocessor: malloc'ed memory";
6970       break;
6971     case CXTUResourceUsage_PreprocessingRecord:
6972       str = "Preprocessor: PreprocessingRecord";
6973       break;
6974     case CXTUResourceUsage_SourceManager_DataStructures:
6975       str = "SourceManager: data structures and tables";
6976       break;
6977     case CXTUResourceUsage_Preprocessor_HeaderSearch:
6978       str = "Preprocessor: header search tables";
6979       break;
6980   }
6981   return str;
6982 }
6983
6984 CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
6985   if (isNotUsableTU(TU)) {
6986     LOG_BAD_TU(TU);
6987     CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr };
6988     return usage;
6989   }
6990   
6991   ASTUnit *astUnit = cxtu::getASTUnit(TU);
6992   std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries());
6993   ASTContext &astContext = astUnit->getASTContext();
6994   
6995   // How much memory is used by AST nodes and types?
6996   createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST,
6997     (unsigned long) astContext.getASTAllocatedMemory());
6998
6999   // How much memory is used by identifiers?
7000   createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers,
7001     (unsigned long) astContext.Idents.getAllocator().getTotalMemory());
7002
7003   // How much memory is used for selectors?
7004   createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors,
7005     (unsigned long) astContext.Selectors.getTotalMemory());
7006   
7007   // How much memory is used by ASTContext's side tables?
7008   createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables,
7009     (unsigned long) astContext.getSideTableAllocatedMemory());
7010   
7011   // How much memory is used for caching global code completion results?
7012   unsigned long completionBytes = 0;
7013   if (GlobalCodeCompletionAllocator *completionAllocator =
7014       astUnit->getCachedCompletionAllocator().get()) {
7015     completionBytes = completionAllocator->getTotalMemory();
7016   }
7017   createCXTUResourceUsageEntry(*entries,
7018                                CXTUResourceUsage_GlobalCompletionResults,
7019                                completionBytes);
7020   
7021   // How much memory is being used by SourceManager's content cache?
7022   createCXTUResourceUsageEntry(*entries,
7023           CXTUResourceUsage_SourceManagerContentCache,
7024           (unsigned long) astContext.getSourceManager().getContentCacheSize());
7025   
7026   // How much memory is being used by the MemoryBuffer's in SourceManager?
7027   const SourceManager::MemoryBufferSizes &srcBufs =
7028     astUnit->getSourceManager().getMemoryBufferSizes();
7029   
7030   createCXTUResourceUsageEntry(*entries,
7031                                CXTUResourceUsage_SourceManager_Membuffer_Malloc,
7032                                (unsigned long) srcBufs.malloc_bytes);
7033   createCXTUResourceUsageEntry(*entries,
7034                                CXTUResourceUsage_SourceManager_Membuffer_MMap,
7035                                (unsigned long) srcBufs.mmap_bytes);
7036   createCXTUResourceUsageEntry(*entries,
7037                                CXTUResourceUsage_SourceManager_DataStructures,
7038                                (unsigned long) astContext.getSourceManager()
7039                                 .getDataStructureSizes());
7040   
7041   // How much memory is being used by the ExternalASTSource?
7042   if (ExternalASTSource *esrc = astContext.getExternalSource()) {
7043     const ExternalASTSource::MemoryBufferSizes &sizes =
7044       esrc->getMemoryBufferSizes();
7045     
7046     createCXTUResourceUsageEntry(*entries,
7047       CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc,
7048                                  (unsigned long) sizes.malloc_bytes);
7049     createCXTUResourceUsageEntry(*entries,
7050       CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
7051                                  (unsigned long) sizes.mmap_bytes);
7052   }
7053   
7054   // How much memory is being used by the Preprocessor?
7055   Preprocessor &pp = astUnit->getPreprocessor();
7056   createCXTUResourceUsageEntry(*entries,
7057                                CXTUResourceUsage_Preprocessor,
7058                                pp.getTotalMemory());
7059   
7060   if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
7061     createCXTUResourceUsageEntry(*entries,
7062                                  CXTUResourceUsage_PreprocessingRecord,
7063                                  pRec->getTotalMemory());    
7064   }
7065   
7066   createCXTUResourceUsageEntry(*entries,
7067                                CXTUResourceUsage_Preprocessor_HeaderSearch,
7068                                pp.getHeaderSearchInfo().getTotalMemory());
7069
7070   CXTUResourceUsage usage = { (void*) entries.get(),
7071                             (unsigned) entries->size(),
7072                             !entries->empty() ? &(*entries)[0] : nullptr };
7073   entries.release();
7074   return usage;
7075 }
7076
7077 void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
7078   if (usage.data)
7079     delete (MemUsageEntries*) usage.data;
7080 }
7081
7082 CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) {
7083   CXSourceRangeList *skipped = new CXSourceRangeList;
7084   skipped->count = 0;
7085   skipped->ranges = nullptr;
7086
7087   if (isNotUsableTU(TU)) {
7088     LOG_BAD_TU(TU);
7089     return skipped;
7090   }
7091
7092   if (!file)
7093     return skipped;
7094
7095   ASTUnit *astUnit = cxtu::getASTUnit(TU);
7096   PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord();
7097   if (!ppRec)
7098     return skipped;
7099
7100   ASTContext &Ctx = astUnit->getASTContext();
7101   SourceManager &sm = Ctx.getSourceManager();
7102   FileEntry *fileEntry = static_cast<FileEntry *>(file);
7103   FileID wantedFileID = sm.translateFile(fileEntry);
7104
7105   const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges();
7106   std::vector<SourceRange> wantedRanges;
7107   for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end();
7108        i != ei; ++i) {
7109     if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID)
7110       wantedRanges.push_back(*i);
7111   }
7112
7113   skipped->count = wantedRanges.size();
7114   skipped->ranges = new CXSourceRange[skipped->count];
7115   for (unsigned i = 0, ei = skipped->count; i != ei; ++i)
7116     skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]);
7117
7118   return skipped;
7119 }
7120
7121 void clang_disposeSourceRangeList(CXSourceRangeList *ranges) {
7122   if (ranges) {
7123     delete[] ranges->ranges;
7124     delete ranges;
7125   }
7126 }
7127
7128 } // end extern "C"
7129
7130 void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {
7131   CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU);
7132   for (unsigned I = 0; I != Usage.numEntries; ++I)
7133     fprintf(stderr, "  %s: %lu\n", 
7134             clang_getTUResourceUsageName(Usage.entries[I].kind),
7135             Usage.entries[I].amount);
7136   
7137   clang_disposeCXTUResourceUsage(Usage);
7138 }
7139
7140 //===----------------------------------------------------------------------===//
7141 // Misc. utility functions.
7142 //===----------------------------------------------------------------------===//
7143
7144 /// Default to using an 8 MB stack size on "safety" threads.
7145 static unsigned SafetyStackThreadSize = 8 << 20;
7146
7147 namespace clang {
7148
7149 bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn,
7150                unsigned Size) {
7151   if (!Size)
7152     Size = GetSafetyThreadStackSize();
7153   if (Size)
7154     return CRC.RunSafelyOnThread(Fn, Size);
7155   return CRC.RunSafely(Fn);
7156 }
7157
7158 unsigned GetSafetyThreadStackSize() {
7159   return SafetyStackThreadSize;
7160 }
7161
7162 void SetSafetyThreadStackSize(unsigned Value) {
7163   SafetyStackThreadSize = Value;
7164 }
7165
7166 }
7167
7168 void clang::setThreadBackgroundPriority() {
7169   if (getenv("LIBCLANG_BGPRIO_DISABLE"))
7170     return;
7171
7172 #ifdef USE_DARWIN_THREADS
7173   setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
7174 #endif
7175 }
7176
7177 void cxindex::printDiagsToStderr(ASTUnit *Unit) {
7178   if (!Unit)
7179     return;
7180
7181   for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), 
7182                                   DEnd = Unit->stored_diag_end();
7183        D != DEnd; ++D) {
7184     CXStoredDiagnostic Diag(*D, Unit->getLangOpts());
7185     CXString Msg = clang_formatDiagnostic(&Diag,
7186                                 clang_defaultDiagnosticDisplayOptions());
7187     fprintf(stderr, "%s\n", clang_getCString(Msg));
7188     clang_disposeString(Msg);
7189   }
7190 #ifdef LLVM_ON_WIN32
7191   // On Windows, force a flush, since there may be multiple copies of
7192   // stderr and stdout in the file system, all with different buffers
7193   // but writing to the same device.
7194   fflush(stderr);
7195 #endif
7196 }
7197
7198 MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II,
7199                                  SourceLocation MacroDefLoc,
7200                                  CXTranslationUnit TU){
7201   if (MacroDefLoc.isInvalid() || !TU)
7202     return nullptr;
7203   if (!II.hadMacroDefinition())
7204     return nullptr;
7205
7206   ASTUnit *Unit = cxtu::getASTUnit(TU);
7207   Preprocessor &PP = Unit->getPreprocessor();
7208   MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II);
7209   if (MD) {
7210     for (MacroDirective::DefInfo
7211            Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) {
7212       if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc())
7213         return Def.getMacroInfo();
7214     }
7215   }
7216
7217   return nullptr;
7218 }
7219
7220 const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef,
7221                                        CXTranslationUnit TU) {
7222   if (!MacroDef || !TU)
7223     return nullptr;
7224   const IdentifierInfo *II = MacroDef->getName();
7225   if (!II)
7226     return nullptr;
7227
7228   return getMacroInfo(*II, MacroDef->getLocation(), TU);
7229 }
7230
7231 MacroDefinitionRecord *
7232 cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok,
7233                                         CXTranslationUnit TU) {
7234   if (!MI || !TU)
7235     return nullptr;
7236   if (Tok.isNot(tok::raw_identifier))
7237     return nullptr;
7238
7239   if (MI->getNumTokens() == 0)
7240     return nullptr;
7241   SourceRange DefRange(MI->getReplacementToken(0).getLocation(),
7242                        MI->getDefinitionEndLoc());
7243   ASTUnit *Unit = cxtu::getASTUnit(TU);
7244
7245   // Check that the token is inside the definition and not its argument list.
7246   SourceManager &SM = Unit->getSourceManager();
7247   if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin()))
7248     return nullptr;
7249   if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation()))
7250     return nullptr;
7251
7252   Preprocessor &PP = Unit->getPreprocessor();
7253   PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
7254   if (!PPRec)
7255     return nullptr;
7256
7257   IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier());
7258   if (!II.hadMacroDefinition())
7259     return nullptr;
7260
7261   // Check that the identifier is not one of the macro arguments.
7262   if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end())
7263     return nullptr;
7264
7265   MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II);
7266   if (!InnerMD)
7267     return nullptr;
7268
7269   return PPRec->findMacroDefinition(InnerMD->getMacroInfo());
7270 }
7271
7272 MacroDefinitionRecord *
7273 cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc,
7274                                         CXTranslationUnit TU) {
7275   if (Loc.isInvalid() || !MI || !TU)
7276     return nullptr;
7277
7278   if (MI->getNumTokens() == 0)
7279     return nullptr;
7280   ASTUnit *Unit = cxtu::getASTUnit(TU);
7281   Preprocessor &PP = Unit->getPreprocessor();
7282   if (!PP.getPreprocessingRecord())
7283     return nullptr;
7284   Loc = Unit->getSourceManager().getSpellingLoc(Loc);
7285   Token Tok;
7286   if (PP.getRawToken(Loc, Tok))
7287     return nullptr;
7288
7289   return checkForMacroInMacroDefinition(MI, Tok, TU);
7290 }
7291
7292 extern "C" {
7293
7294 CXString clang_getClangVersion() {
7295   return cxstring::createDup(getClangFullVersion());
7296 }
7297
7298 } // end: extern "C"
7299
7300 Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) {
7301   if (TU) {
7302     if (ASTUnit *Unit = cxtu::getASTUnit(TU)) {
7303       LogOS << '<' << Unit->getMainFileName() << '>';
7304       if (Unit->isMainFileAST())
7305         LogOS << " (" << Unit->getASTFileName() << ')';
7306       return *this;
7307     }
7308   } else {
7309     LogOS << "<NULL TU>";
7310   }
7311   return *this;
7312 }
7313
7314 Logger &cxindex::Logger::operator<<(const FileEntry *FE) {
7315   *this << FE->getName();
7316   return *this;
7317 }
7318
7319 Logger &cxindex::Logger::operator<<(CXCursor cursor) {
7320   CXString cursorName = clang_getCursorDisplayName(cursor);
7321   *this << cursorName << "@" << clang_getCursorLocation(cursor);
7322   clang_disposeString(cursorName);
7323   return *this;
7324 }
7325
7326 Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) {
7327   CXFile File;
7328   unsigned Line, Column;
7329   clang_getFileLocation(Loc, &File, &Line, &Column, nullptr);
7330   CXString FileName = clang_getFileName(File);
7331   *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column);
7332   clang_disposeString(FileName);
7333   return *this;
7334 }
7335
7336 Logger &cxindex::Logger::operator<<(CXSourceRange range) {
7337   CXSourceLocation BLoc = clang_getRangeStart(range);
7338   CXSourceLocation ELoc = clang_getRangeEnd(range);
7339
7340   CXFile BFile;
7341   unsigned BLine, BColumn;
7342   clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr);
7343
7344   CXFile EFile;
7345   unsigned ELine, EColumn;
7346   clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr);
7347
7348   CXString BFileName = clang_getFileName(BFile);
7349   if (BFile == EFile) {
7350     *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName),
7351                          BLine, BColumn, ELine, EColumn);
7352   } else {
7353     CXString EFileName = clang_getFileName(EFile);
7354     *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName),
7355                           BLine, BColumn)
7356           << llvm::format("%s:%d:%d]", clang_getCString(EFileName),
7357                           ELine, EColumn);
7358     clang_disposeString(EFileName);
7359   }
7360   clang_disposeString(BFileName);
7361   return *this;
7362 }
7363
7364 Logger &cxindex::Logger::operator<<(CXString Str) {
7365   *this << clang_getCString(Str);
7366   return *this;
7367 }
7368
7369 Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) {
7370   LogOS << Fmt;
7371   return *this;
7372 }
7373
7374 static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex;
7375
7376 cxindex::Logger::~Logger() {
7377   llvm::sys::ScopedLock L(*LoggingMutex);
7378
7379   static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();
7380
7381   raw_ostream &OS = llvm::errs();
7382   OS << "[libclang:" << Name << ':';
7383
7384 #ifdef USE_DARWIN_THREADS
7385   // TODO: Portability.
7386   mach_port_t tid = pthread_mach_thread_np(pthread_self());
7387   OS << tid << ':';
7388 #endif
7389
7390   llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime();
7391   OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime());
7392   OS << Msg << '\n';
7393
7394   if (Trace) {
7395     llvm::sys::PrintStackTrace(OS);
7396     OS << "--------------------------------------------------\n";
7397   }
7398 }