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