1//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the main API hooks in the Clang-C Source Indexing
10// library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIndexDiagnostic.h"
15#include "CIndexer.h"
16#include "CLog.h"
17#include "CXCursor.h"
18#include "CXFile.h"
19#include "CXSourceLocation.h"
20#include "CXString.h"
21#include "CXTranslationUnit.h"
22#include "CXType.h"
23#include "CursorVisitor.h"
24#include "clang-c/FatalErrorHandler.h"
25#include "clang/AST/Attr.h"
26#include "clang/AST/AttrVisitor.h"
27#include "clang/AST/DeclObjCCommon.h"
28#include "clang/AST/Expr.h"
29#include "clang/AST/ExprCXX.h"
30#include "clang/AST/Mangle.h"
31#include "clang/AST/OpenACCClause.h"
32#include "clang/AST/OpenMPClause.h"
33#include "clang/AST/OperationKinds.h"
34#include "clang/AST/StmtVisitor.h"
35#include "clang/Basic/Diagnostic.h"
36#include "clang/Basic/DiagnosticCategories.h"
37#include "clang/Basic/DiagnosticIDs.h"
38#include "clang/Basic/Stack.h"
39#include "clang/Basic/TargetInfo.h"
40#include "clang/Basic/Version.h"
41#include "clang/Driver/CreateASTUnitFromArgs.h"
42#include "clang/Frontend/ASTUnit.h"
43#include "clang/Frontend/CompilerInstance.h"
44#include "clang/Index/CommentToXML.h"
45#include "clang/Lex/HeaderSearch.h"
46#include "clang/Lex/Lexer.h"
47#include "clang/Lex/PreprocessingRecord.h"
48#include "clang/Lex/Preprocessor.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/StringSwitch.h"
51#include "llvm/Config/llvm-config.h"
52#include "llvm/Support/Compiler.h"
53#include "llvm/Support/CrashRecoveryContext.h"
54#include "llvm/Support/Format.h"
55#include "llvm/Support/ManagedStatic.h"
56#include "llvm/Support/MemoryBuffer.h"
57#include "llvm/Support/Program.h"
58#include "llvm/Support/SaveAndRestore.h"
59#include "llvm/Support/Signals.h"
60#include "llvm/Support/TargetSelect.h"
61#include "llvm/Support/Threading.h"
62#include "llvm/Support/Timer.h"
63#include "llvm/Support/VirtualFileSystem.h"
64#include "llvm/Support/raw_ostream.h"
65#include "llvm/Support/thread.h"
66#include <mutex>
67#include <optional>
68
69#if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__)
70#define USE_DARWIN_THREADS
71#endif
72
73#ifdef USE_DARWIN_THREADS
74#include <pthread.h>
75#endif
76
77using namespace clang;
78using namespace clang::cxcursor;
79using namespace clang::cxtu;
80using namespace clang::cxindex;
81
82CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx,
83 std::unique_ptr<ASTUnit> AU) {
84 if (!AU)
85 return nullptr;
86 assert(CIdx);
87 CXTranslationUnit D = new CXTranslationUnitImpl();
88 D->CIdx = CIdx;
89 D->TheASTUnit = AU.release();
90 D->StringPool = new cxstring::CXStringPool();
91 D->Diagnostics = nullptr;
92 D->OverridenCursorsPool = createOverridenCXCursorsPool();
93 D->CommentToXML = nullptr;
94 D->ParsingOptions = 0;
95 D->Arguments = {};
96 return D;
97}
98
99bool cxtu::isASTReadError(ASTUnit *AU) {
100 for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(),
101 DEnd = AU->stored_diag_end();
102 D != DEnd; ++D) {
103 if (D->getLevel() >= DiagnosticsEngine::Error &&
104 DiagnosticIDs::getCategoryNumberForDiag(DiagID: D->getID()) ==
105 diag::DiagCat_AST_Deserialization_Issue)
106 return true;
107 }
108 return false;
109}
110
111cxtu::CXTUOwner::~CXTUOwner() {
112 if (TU)
113 clang_disposeTranslationUnit(TU);
114}
115
116/// Compare two source ranges to determine their relative position in
117/// the translation unit.
118static RangeComparisonResult RangeCompare(SourceManager &SM, SourceRange R1,
119 SourceRange R2) {
120 assert(R1.isValid() && "First range is invalid?");
121 assert(R2.isValid() && "Second range is invalid?");
122 if (R1.getEnd() != R2.getBegin() &&
123 SM.isBeforeInTranslationUnit(LHS: R1.getEnd(), RHS: R2.getBegin()))
124 return RangeBefore;
125 if (R2.getEnd() != R1.getBegin() &&
126 SM.isBeforeInTranslationUnit(LHS: R2.getEnd(), RHS: R1.getBegin()))
127 return RangeAfter;
128 return RangeOverlap;
129}
130
131/// Determine if a source location falls within, before, or after a
132/// a given source range.
133static RangeComparisonResult LocationCompare(SourceManager &SM,
134 SourceLocation L, SourceRange R) {
135 assert(R.isValid() && "First range is invalid?");
136 assert(L.isValid() && "Second range is invalid?");
137 if (L == R.getBegin() || L == R.getEnd())
138 return RangeOverlap;
139 if (SM.isBeforeInTranslationUnit(LHS: L, RHS: R.getBegin()))
140 return RangeBefore;
141 if (SM.isBeforeInTranslationUnit(LHS: R.getEnd(), RHS: L))
142 return RangeAfter;
143 return RangeOverlap;
144}
145
146/// Translate a Clang source range into a CIndex source range.
147///
148/// Clang internally represents ranges where the end location points to the
149/// start of the token at the end. However, for external clients it is more
150/// useful to have a CXSourceRange be a proper half-open interval. This routine
151/// does the appropriate translation.
152CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
153 const LangOptions &LangOpts,
154 const CharSourceRange &R) {
155 // We want the last character in this location, so we will adjust the
156 // location accordingly.
157 SourceLocation EndLoc = R.getEnd();
158 bool IsTokenRange = R.isTokenRange();
159 if (EndLoc.isValid() && EndLoc.isMacroID() &&
160 !SM.isMacroArgExpansion(Loc: EndLoc)) {
161 CharSourceRange Expansion = SM.getExpansionRange(Loc: EndLoc);
162 EndLoc = Expansion.getEnd();
163 IsTokenRange = Expansion.isTokenRange();
164 }
165 if (IsTokenRange && EndLoc.isValid()) {
166 unsigned Length =
167 Lexer::MeasureTokenLength(Loc: SM.getSpellingLoc(Loc: EndLoc), SM, LangOpts);
168 EndLoc = EndLoc.getLocWithOffset(Offset: Length);
169 }
170
171 CXSourceRange Result = {
172 .ptr_data: {&SM, &LangOpts}, .begin_int_data: R.getBegin().getRawEncoding(), .end_int_data: EndLoc.getRawEncoding()};
173 return Result;
174}
175
176CharSourceRange cxloc::translateCXRangeToCharRange(CXSourceRange R) {
177 return CharSourceRange::getCharRange(
178 B: SourceLocation::getFromRawEncoding(Encoding: R.begin_int_data),
179 E: SourceLocation::getFromRawEncoding(Encoding: R.end_int_data));
180}
181
182//===----------------------------------------------------------------------===//
183// Cursor visitor.
184//===----------------------------------------------------------------------===//
185
186static SourceRange getRawCursorExtent(CXCursor C);
187static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
188
189RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
190 return RangeCompare(SM&: AU->getSourceManager(), R1: R, R2: RegionOfInterest);
191}
192
193/// Visit the given cursor and, if requested by the visitor,
194/// its children.
195///
196/// \param Cursor the cursor to visit.
197///
198/// \param CheckedRegionOfInterest if true, then the caller already checked
199/// that this cursor is within the region of interest.
200///
201/// \returns true if the visitation should be aborted, false if it
202/// should continue.
203bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
204 if (clang_isInvalid(Cursor.kind))
205 return false;
206
207 if (clang_isDeclaration(Cursor.kind)) {
208 const Decl *D = getCursorDecl(Cursor);
209 if (!D) {
210 assert(0 && "Invalid declaration cursor");
211 return true; // abort.
212 }
213
214 // Ignore implicit declarations, unless it's an objc method because
215 // currently we should report implicit methods for properties when indexing.
216 if (D->isImplicit() && !isa<ObjCMethodDecl>(Val: D))
217 return false;
218 }
219
220 // If we have a range of interest, and this cursor doesn't intersect with it,
221 // we're done.
222 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
223 SourceRange Range = getRawCursorExtent(C: Cursor);
224 if (Range.isInvalid() || CompareRegionOfInterest(R: Range))
225 return false;
226 }
227
228 switch (Visitor(Cursor, Parent, ClientData)) {
229 case CXChildVisit_Break:
230 return true;
231
232 case CXChildVisit_Continue:
233 return false;
234
235 case CXChildVisit_Recurse: {
236 bool ret = VisitChildren(Parent: Cursor);
237 if (PostChildrenVisitor)
238 if (PostChildrenVisitor(Cursor, ClientData))
239 return true;
240 return ret;
241 }
242 }
243
244 llvm_unreachable("Invalid CXChildVisitResult!");
245}
246
247static bool visitPreprocessedEntitiesInRange(SourceRange R,
248 PreprocessingRecord &PPRec,
249 CursorVisitor &Visitor) {
250 SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
251 FileID FID;
252
253 if (!Visitor.shouldVisitIncludedEntities()) {
254 // If the begin/end of the range lie in the same FileID, do the optimization
255 // where we skip preprocessed entities that do not come from the same
256 // FileID.
257 FID = SM.getFileID(SpellingLoc: SM.getFileLoc(Loc: R.getBegin()));
258 if (FID != SM.getFileID(SpellingLoc: SM.getFileLoc(Loc: R.getEnd())))
259 FID = FileID();
260 }
261
262 const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R);
263 return Visitor.visitPreprocessedEntities(First: Entities.begin(), Last: Entities.end(),
264 PPRec, FID);
265}
266
267bool CursorVisitor::visitFileRegion() {
268 if (RegionOfInterest.isInvalid())
269 return false;
270
271 ASTUnit *Unit = cxtu::getASTUnit(TU);
272 SourceManager &SM = Unit->getSourceManager();
273
274 FileIDAndOffset Begin = SM.getDecomposedLoc(
275 Loc: SM.getFileLoc(Loc: RegionOfInterest.getBegin())),
276 End = SM.getDecomposedLoc(
277 Loc: SM.getFileLoc(Loc: RegionOfInterest.getEnd()));
278
279 if (End.first != Begin.first) {
280 // If the end does not reside in the same file, try to recover by
281 // picking the end of the file of begin location.
282 End.first = Begin.first;
283 End.second = SM.getFileIDSize(FID: Begin.first);
284 }
285
286 assert(Begin.first == End.first);
287 if (Begin.second > End.second)
288 return false;
289
290 FileID File = Begin.first;
291 unsigned Offset = Begin.second;
292 unsigned Length = End.second - Begin.second;
293
294 if (!VisitDeclsOnly && !VisitPreprocessorLast)
295 if (visitPreprocessedEntitiesInRegion())
296 return true; // visitation break.
297
298 if (visitDeclsFromFileRegion(File, Offset, Length))
299 return true; // visitation break.
300
301 if (!VisitDeclsOnly && VisitPreprocessorLast)
302 return visitPreprocessedEntitiesInRegion();
303
304 return false;
305}
306
307static bool isInLexicalContext(Decl *D, DeclContext *DC) {
308 if (!DC)
309 return false;
310
311 for (DeclContext *DeclDC = D->getLexicalDeclContext(); DeclDC;
312 DeclDC = DeclDC->getLexicalParent()) {
313 if (DeclDC == DC)
314 return true;
315 }
316 return false;
317}
318
319bool CursorVisitor::visitDeclsFromFileRegion(FileID File, unsigned Offset,
320 unsigned Length) {
321 ASTUnit *Unit = cxtu::getASTUnit(TU);
322 SourceManager &SM = Unit->getSourceManager();
323 SourceRange Range = RegionOfInterest;
324
325 SmallVector<Decl *, 16> Decls;
326 Unit->findFileRegionDecls(File, Offset, Length, Decls);
327
328 // If we didn't find any file level decls for the file, try looking at the
329 // file that it was included from.
330 while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
331 bool Invalid = false;
332 const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(FID: File, Invalid: &Invalid);
333 if (Invalid)
334 return false;
335
336 SourceLocation Outer;
337 if (SLEntry.isFile())
338 Outer = SLEntry.getFile().getIncludeLoc();
339 else
340 Outer = SLEntry.getExpansion().getExpansionLocStart();
341 if (Outer.isInvalid())
342 return false;
343
344 std::tie(args&: File, args&: Offset) = SM.getDecomposedExpansionLoc(Loc: Outer);
345 Length = 0;
346 Unit->findFileRegionDecls(File, Offset, Length, Decls);
347 }
348
349 assert(!Decls.empty());
350
351 bool VisitedAtLeastOnce = false;
352 DeclContext *CurDC = nullptr;
353 SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
354 for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
355 Decl *D = *DIt;
356 if (D->getSourceRange().isInvalid())
357 continue;
358
359 if (isInLexicalContext(D, DC: CurDC))
360 continue;
361
362 CurDC = dyn_cast<DeclContext>(Val: D);
363
364 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D))
365 if (!TD->isFreeStanding())
366 continue;
367
368 RangeComparisonResult CompRes =
369 RangeCompare(SM, R1: D->getSourceRange(), R2: Range);
370 if (CompRes == RangeBefore)
371 continue;
372 if (CompRes == RangeAfter)
373 break;
374
375 assert(CompRes == RangeOverlap);
376 VisitedAtLeastOnce = true;
377
378 if (isa<ObjCContainerDecl>(Val: D)) {
379 FileDI_current = &DIt;
380 FileDE_current = DE;
381 } else {
382 FileDI_current = nullptr;
383 }
384
385 if (Visit(Cursor: MakeCXCursor(D, TU, RegionOfInterest: Range), /*CheckedRegionOfInterest=*/true))
386 return true; // visitation break.
387 }
388
389 if (VisitedAtLeastOnce)
390 return false;
391
392 // No Decls overlapped with the range. Move up the lexical context until there
393 // is a context that contains the range or we reach the translation unit
394 // level.
395 DeclContext *DC = DIt == Decls.begin()
396 ? (*DIt)->getLexicalDeclContext()
397 : (*(DIt - 1))->getLexicalDeclContext();
398
399 while (DC && !DC->isTranslationUnit()) {
400 Decl *D = cast<Decl>(Val: DC);
401 SourceRange CurDeclRange = D->getSourceRange();
402 if (CurDeclRange.isInvalid())
403 break;
404
405 if (RangeCompare(SM, R1: CurDeclRange, R2: Range) == RangeOverlap) {
406 if (Visit(Cursor: MakeCXCursor(D, TU, RegionOfInterest: Range), /*CheckedRegionOfInterest=*/true))
407 return true; // visitation break.
408 }
409
410 DC = D->getLexicalDeclContext();
411 }
412
413 return false;
414}
415
416bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
417 if (!AU->getPreprocessor().getPreprocessingRecord())
418 return false;
419
420 PreprocessingRecord &PPRec = *AU->getPreprocessor().getPreprocessingRecord();
421 SourceManager &SM = AU->getSourceManager();
422
423 if (RegionOfInterest.isValid()) {
424 SourceRange MappedRange = AU->mapRangeToPreamble(R: RegionOfInterest);
425 SourceLocation B = MappedRange.getBegin();
426 SourceLocation E = MappedRange.getEnd();
427
428 if (AU->isInPreambleFileID(Loc: B)) {
429 if (SM.isLoadedSourceLocation(Loc: E))
430 return visitPreprocessedEntitiesInRange(R: SourceRange(B, E), PPRec,
431 Visitor&: *this);
432
433 // Beginning of range lies in the preamble but it also extends beyond
434 // it into the main file. Split the range into 2 parts, one covering
435 // the preamble and another covering the main file. This allows subsequent
436 // calls to visitPreprocessedEntitiesInRange to accept a source range that
437 // lies in the same FileID, allowing it to skip preprocessed entities that
438 // do not come from the same FileID.
439 bool breaked = visitPreprocessedEntitiesInRange(
440 R: SourceRange(B, AU->getEndOfPreambleFileID()), PPRec, Visitor&: *this);
441 if (breaked)
442 return true;
443 return visitPreprocessedEntitiesInRange(
444 R: SourceRange(AU->getStartOfMainFileID(), E), PPRec, Visitor&: *this);
445 }
446
447 return visitPreprocessedEntitiesInRange(R: SourceRange(B, E), PPRec, Visitor&: *this);
448 }
449
450 bool OnlyLocalDecls = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
451
452 if (OnlyLocalDecls)
453 return visitPreprocessedEntities(First: PPRec.local_begin(), Last: PPRec.local_end(),
454 PPRec);
455
456 return visitPreprocessedEntities(First: PPRec.begin(), Last: PPRec.end(), PPRec);
457}
458
459template <typename InputIterator>
460bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
461 InputIterator Last,
462 PreprocessingRecord &PPRec,
463 FileID FID) {
464 for (; First != Last; ++First) {
465 if (!FID.isInvalid() && !PPRec.isEntityInFileID(PPEI: First, FID))
466 continue;
467
468 PreprocessedEntity *PPE = *First;
469 if (!PPE)
470 continue;
471
472 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(Val: PPE)) {
473 if (Visit(Cursor: MakeMacroExpansionCursor(ME, TU)))
474 return true;
475
476 continue;
477 }
478
479 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(Val: PPE)) {
480 if (Visit(Cursor: MakeMacroDefinitionCursor(MD, TU)))
481 return true;
482
483 continue;
484 }
485
486 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(Val: PPE)) {
487 if (Visit(Cursor: MakeInclusionDirectiveCursor(ID, TU)))
488 return true;
489
490 continue;
491 }
492 }
493
494 return false;
495}
496
497/// Visit the children of the given cursor.
498///
499/// \returns true if the visitation should be aborted, false if it
500/// should continue.
501bool CursorVisitor::VisitChildren(CXCursor Cursor) {
502 if (clang_isReference(Cursor.kind) &&
503 Cursor.kind != CXCursor_CXXBaseSpecifier) {
504 // By definition, references have no children.
505 return false;
506 }
507
508 // Set the Parent field to Cursor, then back to its old value once we're
509 // done.
510 SetParentRAII SetParent(Parent, StmtParent, Cursor);
511
512 if (clang_isDeclaration(Cursor.kind)) {
513 Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
514 if (!D)
515 return false;
516
517 return VisitAttributes(D) || Visit(D);
518 }
519
520 if (clang_isStatement(Cursor.kind)) {
521 if (const Stmt *S = getCursorStmt(Cursor))
522 return Visit(S);
523
524 return false;
525 }
526
527 if (clang_isExpression(Cursor.kind)) {
528 if (const Expr *E = getCursorExpr(Cursor))
529 return Visit(S: E);
530
531 return false;
532 }
533
534 if (clang_isTranslationUnit(Cursor.kind)) {
535 CXTranslationUnit TU = getCursorTU(Cursor);
536 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
537
538 int VisitOrder[2] = {VisitPreprocessorLast, !VisitPreprocessorLast};
539 for (unsigned I = 0; I != 2; ++I) {
540 if (VisitOrder[I]) {
541 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
542 RegionOfInterest.isInvalid()) {
543 for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
544 TLEnd = CXXUnit->top_level_end();
545 TL != TLEnd; ++TL) {
546 const std::optional<bool> V = handleDeclForVisitation(D: *TL);
547 if (!V)
548 continue;
549 return *V;
550 }
551 } else if (VisitDeclContext(
552 DC: CXXUnit->getASTContext().getTranslationUnitDecl()))
553 return true;
554 continue;
555 }
556
557 // Walk the preprocessing record.
558 if (CXXUnit->getPreprocessor().getPreprocessingRecord())
559 visitPreprocessedEntitiesInRegion();
560 }
561
562 return false;
563 }
564
565 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
566 if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(C: Cursor)) {
567 if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
568 return Visit(TyLoc: BaseTSInfo->getTypeLoc());
569 }
570 }
571 }
572
573 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
574 const IBOutletCollectionAttr *A =
575 cast<IBOutletCollectionAttr>(Val: cxcursor::getCursorAttr(Cursor));
576 if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>())
577 return Visit(Cursor: cxcursor::MakeCursorObjCClassRef(
578 Class: ObjT->getInterface(),
579 Loc: A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU));
580 }
581
582 if (clang_isAttribute(Cursor.kind)) {
583 if (const Attr *A = getCursorAttr(Cursor))
584 return Visit(A);
585
586 return false;
587 }
588
589 // If pointing inside a macro definition, check if the token is an identifier
590 // that was ever defined as a macro. In such a case, create a "pseudo" macro
591 // expansion cursor for that token.
592 SourceLocation BeginLoc = RegionOfInterest.getBegin();
593 if (Cursor.kind == CXCursor_MacroDefinition &&
594 BeginLoc == RegionOfInterest.getEnd()) {
595 SourceLocation Loc = AU->mapLocationToPreamble(Loc: BeginLoc);
596 const MacroInfo *MI =
597 getMacroInfo(MacroDef: cxcursor::getCursorMacroDefinition(C: Cursor), TU);
598 if (MacroDefinitionRecord *MacroDef =
599 checkForMacroInMacroDefinition(MI, Loc, TU))
600 return Visit(Cursor: cxcursor::MakeMacroExpansionCursor(MacroDef, Loc: BeginLoc, TU));
601 }
602
603 // Nothing to visit at the moment.
604 return false;
605}
606
607bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
608 if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
609 if (Visit(TyLoc: TSInfo->getTypeLoc()))
610 return true;
611
612 if (Stmt *Body = B->getBody())
613 return Visit(Cursor: MakeCXCursor(S: Body, Parent: StmtParent, TU, RegionOfInterest));
614
615 return false;
616}
617
618std::optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
619 if (RegionOfInterest.isValid()) {
620 SourceRange Range = getFullCursorExtent(C: Cursor, SrcMgr&: AU->getSourceManager());
621 if (Range.isInvalid())
622 return std::nullopt;
623
624 switch (CompareRegionOfInterest(R: Range)) {
625 case RangeBefore:
626 // This declaration comes before the region of interest; skip it.
627 return std::nullopt;
628
629 case RangeAfter:
630 // This declaration comes after the region of interest; we're done.
631 return false;
632
633 case RangeOverlap:
634 // This declaration overlaps the region of interest; visit it.
635 break;
636 }
637 }
638 return true;
639}
640
641bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
642 DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
643
644 // FIXME: Eventually remove. This part of a hack to support proper
645 // iteration over all Decls contained lexically within an ObjC container.
646 SaveAndRestore DI_saved(DI_current, &I);
647 SaveAndRestore DE_saved(DE_current, E);
648
649 for (; I != E; ++I) {
650 Decl *D = *I;
651 if (D->getLexicalDeclContext() != DC)
652 continue;
653 // Filter out synthesized property accessor redeclarations.
654 if (isa<ObjCImplDecl>(Val: DC))
655 if (auto *OMD = dyn_cast<ObjCMethodDecl>(Val: D))
656 if (OMD->isSynthesizedAccessorStub())
657 continue;
658 const std::optional<bool> V = handleDeclForVisitation(D);
659 if (!V)
660 continue;
661 return *V;
662 }
663 return false;
664}
665
666std::optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) {
667 CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
668
669 // Ignore synthesized ivars here, otherwise if we have something like:
670 // @synthesize prop = _prop;
671 // and '_prop' is not declared, we will encounter a '_prop' ivar before
672 // encountering the 'prop' synthesize declaration and we will think that
673 // we passed the region-of-interest.
674 if (auto *ivarD = dyn_cast<ObjCIvarDecl>(Val: D)) {
675 if (ivarD->getSynthesize())
676 return std::nullopt;
677 }
678
679 // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
680 // declarations is a mismatch with the compiler semantics.
681 if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
682 auto *ID = cast<ObjCInterfaceDecl>(Val: D);
683 if (!ID->isThisDeclarationADefinition())
684 Cursor = MakeCursorObjCClassRef(Class: ID, Loc: ID->getLocation(), TU);
685
686 } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
687 auto *PD = cast<ObjCProtocolDecl>(Val: D);
688 if (!PD->isThisDeclarationADefinition())
689 Cursor = MakeCursorObjCProtocolRef(Proto: PD, Loc: PD->getLocation(), TU);
690 }
691
692 const std::optional<bool> V = shouldVisitCursor(Cursor);
693 if (!V)
694 return std::nullopt;
695 if (!*V)
696 return false;
697 if (Visit(Cursor, CheckedRegionOfInterest: true))
698 return true;
699 return std::nullopt;
700}
701
702bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
703 llvm_unreachable("Translation units are visited directly by Visit()");
704}
705
706bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
707 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
708 return true;
709
710 return Visit(Cursor: MakeCXCursor(D: D->getTemplatedDecl(), TU, RegionOfInterest));
711}
712
713bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
714 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
715 return Visit(TyLoc: TSInfo->getTypeLoc());
716
717 return false;
718}
719
720bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
721 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
722 return Visit(TyLoc: TSInfo->getTypeLoc());
723
724 return false;
725}
726
727bool CursorVisitor::VisitTagDecl(TagDecl *D) { return VisitDeclContext(DC: D); }
728
729bool CursorVisitor::VisitClassTemplateSpecializationDecl(
730 ClassTemplateSpecializationDecl *D) {
731 bool ShouldVisitBody = false;
732 switch (D->getSpecializationKind()) {
733 case TSK_Undeclared:
734 case TSK_ImplicitInstantiation:
735 // Nothing to visit
736 return false;
737
738 case TSK_ExplicitInstantiationDeclaration:
739 case TSK_ExplicitInstantiationDefinition:
740 break;
741
742 case TSK_ExplicitSpecialization:
743 ShouldVisitBody = true;
744 break;
745 }
746
747 // Visit the template arguments used in the specialization.
748 if (const auto *ArgsWritten = D->getTemplateArgsAsWritten()) {
749 for (const TemplateArgumentLoc &Arg : ArgsWritten->arguments())
750 if (VisitTemplateArgumentLoc(TAL: Arg))
751 return true;
752 }
753
754 return ShouldVisitBody && VisitCXXRecordDecl(D);
755}
756
757bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
758 ClassTemplatePartialSpecializationDecl *D) {
759 // FIXME: Visit the "outer" template parameter lists on the TagDecl
760 // before visiting these template parameters.
761 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
762 return true;
763
764 // Visit the partial specialization arguments.
765 const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten();
766 const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs();
767 for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I)
768 if (VisitTemplateArgumentLoc(TAL: TemplateArgs[I]))
769 return true;
770
771 return VisitCXXRecordDecl(D);
772}
773
774bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
775 if (const auto *TC = D->getTypeConstraint()) {
776 if (VisitTypeConstraint(TC: *TC))
777 return true;
778 }
779
780 // Visit the default argument.
781 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
782 VisitTemplateArgumentLoc(TAL: D->getDefaultArgument()))
783 return true;
784
785 return false;
786}
787
788bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
789 if (Expr *Init = D->getInitExpr())
790 return Visit(Cursor: MakeCXCursor(S: Init, Parent: StmtParent, TU, RegionOfInterest));
791 return false;
792}
793
794bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
795 for (TemplateParameterList *TPL : DD->getTemplateParameterLists())
796 if (VisitTemplateParameters(Params: TPL))
797 return true;
798
799 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
800 if (Visit(TyLoc: TSInfo->getTypeLoc()))
801 return true;
802
803 // Visit the nested-name-specifier, if present.
804 if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
805 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
806 return true;
807
808 return false;
809}
810
811static bool HasTrailingReturnType(FunctionDecl *ND) {
812 const QualType Ty = ND->getType();
813 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
814 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(Val: AFT))
815 return FT->hasTrailingReturn();
816 }
817
818 return false;
819}
820
821/// Compare two base or member initializers based on their source order.
822static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X,
823 CXXCtorInitializer *const *Y) {
824 return (*X)->getSourceOrder() - (*Y)->getSourceOrder();
825}
826
827bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
828 for (TemplateParameterList *TPL : ND->getTemplateParameterLists())
829 if (VisitTemplateParameters(Params: TPL))
830 return true;
831
832 if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
833 // Visit the function declaration's syntactic components in the order
834 // written. This requires a bit of work.
835 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
836 FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
837 const bool HasTrailingRT = HasTrailingReturnType(ND);
838
839 // If we have a function declared directly (without the use of a typedef),
840 // visit just the return type. Otherwise, just visit the function's type
841 // now.
842 if ((FTL && !isa<CXXConversionDecl>(Val: ND) && !HasTrailingRT &&
843 Visit(TyLoc: FTL.getReturnLoc())) ||
844 (!FTL && Visit(TyLoc: TL)))
845 return true;
846
847 // Visit the nested-name-specifier, if present.
848 if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
849 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
850 return true;
851
852 // Visit the declaration name.
853 if (!isa<CXXDestructorDecl>(Val: ND))
854 if (VisitDeclarationNameInfo(Name: ND->getNameInfo()))
855 return true;
856
857 // FIXME: Visit explicitly-specified template arguments!
858
859 // Visit the function parameters, if we have a function type.
860 if (FTL && VisitFunctionTypeLoc(TL: FTL, SkipResultType: true))
861 return true;
862
863 // Visit the function's trailing return type.
864 if (FTL && HasTrailingRT && Visit(TyLoc: FTL.getReturnLoc()))
865 return true;
866
867 // FIXME: Attributes?
868 }
869
870 if (auto *E = ND->getTrailingRequiresClause().ConstraintExpr) {
871 if (Visit(S: E))
872 return true;
873 }
874
875 if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
876 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Val: ND)) {
877 // Find the initializers that were written in the source.
878 SmallVector<CXXCtorInitializer *, 4> WrittenInits;
879 for (auto *I : Constructor->inits()) {
880 if (!I->isWritten())
881 continue;
882
883 WrittenInits.push_back(Elt: I);
884 }
885
886 // Sort the initializers in source order
887 llvm::array_pod_sort(Start: WrittenInits.begin(), End: WrittenInits.end(),
888 Compare: &CompareCXXCtorInitializers);
889
890 // Visit the initializers in source order
891 for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
892 CXXCtorInitializer *Init = WrittenInits[I];
893 if (Init->isAnyMemberInitializer()) {
894 if (Visit(Cursor: MakeCursorMemberRef(Field: Init->getAnyMember(),
895 Loc: Init->getMemberLocation(), TU)))
896 return true;
897 } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
898 if (Visit(TyLoc: TInfo->getTypeLoc()))
899 return true;
900 }
901
902 // Visit the initializer value.
903 if (Expr *Initializer = Init->getInit())
904 if (Visit(Cursor: MakeCXCursor(S: Initializer, Parent: ND, TU, RegionOfInterest)))
905 return true;
906 }
907 }
908
909 if (Visit(Cursor: MakeCXCursor(S: ND->getBody(), Parent: StmtParent, TU, RegionOfInterest)))
910 return true;
911 }
912
913 return false;
914}
915
916bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
917 if (VisitDeclaratorDecl(DD: D))
918 return true;
919
920 if (Expr *BitWidth = D->getBitWidth())
921 return Visit(Cursor: MakeCXCursor(S: BitWidth, Parent: StmtParent, TU, RegionOfInterest));
922
923 if (Expr *Init = D->getInClassInitializer())
924 return Visit(Cursor: MakeCXCursor(S: Init, Parent: StmtParent, TU, RegionOfInterest));
925
926 return false;
927}
928
929bool CursorVisitor::VisitVarDecl(VarDecl *D) {
930 if (VisitDeclaratorDecl(DD: D))
931 return true;
932
933 if (Expr *Init = D->getInit())
934 return Visit(Cursor: MakeCXCursor(S: Init, Parent: StmtParent, TU, RegionOfInterest));
935
936 return false;
937}
938
939bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
940 if (VisitDeclaratorDecl(DD: D))
941 return true;
942
943 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
944 if (D->hasDefaultArgument() &&
945 VisitTemplateArgumentLoc(TAL: D->getDefaultArgument()))
946 return true;
947
948 return false;
949}
950
951bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
952 // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
953 // before visiting these template parameters.
954 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
955 return true;
956
957 auto *FD = D->getTemplatedDecl();
958 return VisitAttributes(D: FD) || VisitFunctionDecl(ND: FD);
959}
960
961bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
962 // FIXME: Visit the "outer" template parameter lists on the TagDecl
963 // before visiting these template parameters.
964 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
965 return true;
966
967 auto *CD = D->getTemplatedDecl();
968 return VisitAttributes(D: CD) || VisitCXXRecordDecl(D: CD);
969}
970
971bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
972 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
973 return true;
974
975 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
976 VisitTemplateArgumentLoc(TAL: D->getDefaultArgument()))
977 return true;
978
979 return false;
980}
981
982bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
983 // Visit the bound, if it's explicit.
984 if (D->hasExplicitBound()) {
985 if (auto TInfo = D->getTypeSourceInfo()) {
986 if (Visit(TyLoc: TInfo->getTypeLoc()))
987 return true;
988 }
989 }
990
991 return false;
992}
993
994bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
995 if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo())
996 if (Visit(TyLoc: TSInfo->getTypeLoc()))
997 return true;
998
999 for (const auto *P : ND->parameters()) {
1000 if (Visit(Cursor: MakeCXCursor(D: P, TU, RegionOfInterest)))
1001 return true;
1002 }
1003
1004 return ND->isThisDeclarationADefinition() &&
1005 Visit(Cursor: MakeCXCursor(S: ND->getBody(), Parent: StmtParent, TU, RegionOfInterest));
1006}
1007
1008template <typename DeclIt>
1009static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
1010 SourceManager &SM, SourceLocation EndLoc,
1011 SmallVectorImpl<Decl *> &Decls) {
1012 DeclIt next = *DI_current;
1013 while (++next != DE_current) {
1014 Decl *D_next = *next;
1015 if (!D_next)
1016 break;
1017 SourceLocation L = D_next->getBeginLoc();
1018 if (!L.isValid())
1019 break;
1020 if (SM.isBeforeInTranslationUnit(LHS: L, RHS: EndLoc)) {
1021 *DI_current = next;
1022 Decls.push_back(Elt: D_next);
1023 continue;
1024 }
1025 break;
1026 }
1027}
1028
1029bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
1030 // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
1031 // an @implementation can lexically contain Decls that are not properly
1032 // nested in the AST. When we identify such cases, we need to retrofit
1033 // this nesting here.
1034 if (!DI_current && !FileDI_current)
1035 return VisitDeclContext(DC: D);
1036
1037 // Scan the Decls that immediately come after the container
1038 // in the current DeclContext. If any fall within the
1039 // container's lexical region, stash them into a vector
1040 // for later processing.
1041 SmallVector<Decl *, 24> DeclsInContainer;
1042 SourceLocation EndLoc = D->getSourceRange().getEnd();
1043 SourceManager &SM = AU->getSourceManager();
1044 if (EndLoc.isValid()) {
1045 if (DI_current) {
1046 addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
1047 Decls&: DeclsInContainer);
1048 } else {
1049 addRangedDeclsInContainer(DI_current: FileDI_current, DE_current: FileDE_current, SM, EndLoc,
1050 Decls&: DeclsInContainer);
1051 }
1052 }
1053
1054 // The common case.
1055 if (DeclsInContainer.empty())
1056 return VisitDeclContext(DC: D);
1057
1058 // Get all the Decls in the DeclContext, and sort them with the
1059 // additional ones we've collected. Then visit them.
1060 for (auto *SubDecl : D->decls()) {
1061 if (!SubDecl || SubDecl->getLexicalDeclContext() != D ||
1062 SubDecl->getBeginLoc().isInvalid())
1063 continue;
1064 DeclsInContainer.push_back(Elt: SubDecl);
1065 }
1066
1067 // Now sort the Decls so that they appear in lexical order.
1068 llvm::sort(C&: DeclsInContainer, Comp: [&SM](Decl *A, Decl *B) {
1069 SourceLocation L_A = A->getBeginLoc();
1070 SourceLocation L_B = B->getBeginLoc();
1071 return L_A != L_B
1072 ? SM.isBeforeInTranslationUnit(LHS: L_A, RHS: L_B)
1073 : SM.isBeforeInTranslationUnit(LHS: A->getEndLoc(), RHS: B->getEndLoc());
1074 });
1075
1076 // Now visit the decls.
1077 for (SmallVectorImpl<Decl *>::iterator I = DeclsInContainer.begin(),
1078 E = DeclsInContainer.end();
1079 I != E; ++I) {
1080 CXCursor Cursor = MakeCXCursor(D: *I, TU, RegionOfInterest);
1081 const std::optional<bool> &V = shouldVisitCursor(Cursor);
1082 if (!V)
1083 continue;
1084 if (!*V)
1085 return false;
1086 if (Visit(Cursor, CheckedRegionOfInterest: true))
1087 return true;
1088 }
1089 return false;
1090}
1091
1092bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
1093 if (Visit(Cursor: MakeCursorObjCClassRef(Class: ND->getClassInterface(), Loc: ND->getLocation(),
1094 TU)))
1095 return true;
1096
1097 if (VisitObjCTypeParamList(typeParamList: ND->getTypeParamList()))
1098 return true;
1099
1100 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
1101 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
1102 E = ND->protocol_end();
1103 I != E; ++I, ++PL)
1104 if (Visit(Cursor: MakeCursorObjCProtocolRef(Proto: *I, Loc: *PL, TU)))
1105 return true;
1106
1107 return VisitObjCContainerDecl(D: ND);
1108}
1109
1110bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1111 if (!PID->isThisDeclarationADefinition())
1112 return Visit(Cursor: MakeCursorObjCProtocolRef(Proto: PID, Loc: PID->getLocation(), TU));
1113
1114 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
1115 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
1116 E = PID->protocol_end();
1117 I != E; ++I, ++PL)
1118 if (Visit(Cursor: MakeCursorObjCProtocolRef(Proto: *I, Loc: *PL, TU)))
1119 return true;
1120
1121 return VisitObjCContainerDecl(D: PID);
1122}
1123
1124bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
1125 if (PD->getTypeSourceInfo() && Visit(TyLoc: PD->getTypeSourceInfo()->getTypeLoc()))
1126 return true;
1127
1128 // FIXME: This implements a workaround with @property declarations also being
1129 // installed in the DeclContext for the @interface. Eventually this code
1130 // should be removed.
1131 ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(Val: PD->getDeclContext());
1132 if (!CDecl || !CDecl->IsClassExtension())
1133 return false;
1134
1135 ObjCInterfaceDecl *ID = CDecl->getClassInterface();
1136 if (!ID)
1137 return false;
1138
1139 IdentifierInfo *PropertyId = PD->getIdentifier();
1140 ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
1141 DC: cast<DeclContext>(Val: ID), propertyID: PropertyId, queryKind: PD->getQueryKind());
1142
1143 if (!prevDecl)
1144 return false;
1145
1146 // Visit synthesized methods since they will be skipped when visiting
1147 // the @interface.
1148 if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1149 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1150 if (Visit(Cursor: MakeCXCursor(D: MD, TU, RegionOfInterest)))
1151 return true;
1152
1153 if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1154 if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1155 if (Visit(Cursor: MakeCXCursor(D: MD, TU, RegionOfInterest)))
1156 return true;
1157
1158 return false;
1159}
1160
1161bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) {
1162 if (!typeParamList)
1163 return false;
1164
1165 for (auto *typeParam : *typeParamList) {
1166 // Visit the type parameter.
1167 if (Visit(Cursor: MakeCXCursor(D: typeParam, TU, RegionOfInterest)))
1168 return true;
1169 }
1170
1171 return false;
1172}
1173
1174bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1175 if (!D->isThisDeclarationADefinition()) {
1176 // Forward declaration is treated like a reference.
1177 return Visit(Cursor: MakeCursorObjCClassRef(Class: D, Loc: D->getLocation(), TU));
1178 }
1179
1180 // Objective-C type parameters.
1181 if (VisitObjCTypeParamList(typeParamList: D->getTypeParamListAsWritten()))
1182 return true;
1183
1184 // Issue callbacks for super class.
1185 if (D->getSuperClass() && Visit(Cursor: MakeCursorObjCSuperClassRef(
1186 Super: D->getSuperClass(), Loc: D->getSuperClassLoc(), TU)))
1187 return true;
1188
1189 if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo())
1190 if (Visit(TyLoc: SuperClassTInfo->getTypeLoc()))
1191 return true;
1192
1193 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1194 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1195 E = D->protocol_end();
1196 I != E; ++I, ++PL)
1197 if (Visit(Cursor: MakeCursorObjCProtocolRef(Proto: *I, Loc: *PL, TU)))
1198 return true;
1199
1200 return VisitObjCContainerDecl(D);
1201}
1202
1203bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1204 return VisitObjCContainerDecl(D);
1205}
1206
1207bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1208 // 'ID' could be null when dealing with invalid code.
1209 if (ObjCInterfaceDecl *ID = D->getClassInterface())
1210 if (Visit(Cursor: MakeCursorObjCClassRef(Class: ID, Loc: D->getLocation(), TU)))
1211 return true;
1212
1213 return VisitObjCImplDecl(D);
1214}
1215
1216bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1217#if 0
1218 // Issue callbacks for super class.
1219 // FIXME: No source location information!
1220 if (D->getSuperClass() &&
1221 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1222 D->getSuperClassLoc(),
1223 TU)))
1224 return true;
1225#endif
1226
1227 return VisitObjCImplDecl(D);
1228}
1229
1230bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1231 if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1232 if (PD->isIvarNameSpecified())
1233 return Visit(Cursor: MakeCursorMemberRef(Field: Ivar, Loc: PD->getPropertyIvarDeclLoc(), TU));
1234
1235 return false;
1236}
1237
1238bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1239 return VisitDeclContext(DC: D);
1240}
1241
1242bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1243 // Visit nested-name-specifier.
1244 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1245 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
1246 return true;
1247
1248 return Visit(Cursor: MakeCursorNamespaceRef(NS: D->getAliasedNamespace(),
1249 Loc: D->getTargetNameLoc(), TU));
1250}
1251
1252bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1253 // Visit nested-name-specifier.
1254 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1255 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
1256 return true;
1257 }
1258
1259 if (Visit(Cursor: MakeCursorOverloadedDeclRef(D, Location: D->getLocation(), TU)))
1260 return true;
1261
1262 return VisitDeclarationNameInfo(Name: D->getNameInfo());
1263}
1264
1265bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1266 // Visit nested-name-specifier.
1267 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1268 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
1269 return true;
1270
1271 return Visit(Cursor: MakeCursorNamespaceRef(NS: D->getNominatedNamespaceAsWritten(),
1272 Loc: D->getIdentLocation(), TU));
1273}
1274
1275bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1276 // Visit nested-name-specifier.
1277 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1278 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
1279 return true;
1280 }
1281
1282 return VisitDeclarationNameInfo(Name: D->getNameInfo());
1283}
1284
1285bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1286 UnresolvedUsingTypenameDecl *D) {
1287 // Visit nested-name-specifier.
1288 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1289 if (VisitNestedNameSpecifierLoc(NNS: QualifierLoc))
1290 return true;
1291
1292 return false;
1293}
1294
1295bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) {
1296 if (Visit(Cursor: MakeCXCursor(S: D->getAssertExpr(), Parent: StmtParent, TU, RegionOfInterest)))
1297 return true;
1298 if (auto *Message = D->getMessage())
1299 if (Visit(Cursor: MakeCXCursor(S: Message, Parent: StmtParent, TU, RegionOfInterest)))
1300 return true;
1301 return false;
1302}
1303
1304bool CursorVisitor::VisitFriendDecl(FriendDecl *D) {
1305 if (NamedDecl *FriendD = D->getFriendDecl()) {
1306 if (Visit(Cursor: MakeCXCursor(D: FriendD, TU, RegionOfInterest)))
1307 return true;
1308 } else if (TypeSourceInfo *TI = D->getFriendType()) {
1309 if (Visit(TyLoc: TI->getTypeLoc()))
1310 return true;
1311 }
1312 return false;
1313}
1314
1315bool CursorVisitor::VisitDecompositionDecl(DecompositionDecl *D) {
1316 for (auto *B : D->bindings()) {
1317 if (Visit(Cursor: MakeCXCursor(D: B, TU, RegionOfInterest)))
1318 return true;
1319 }
1320 return VisitVarDecl(D);
1321}
1322
1323bool CursorVisitor::VisitConceptDecl(ConceptDecl *D) {
1324 if (VisitTemplateParameters(Params: D->getTemplateParameters()))
1325 return true;
1326
1327 if (auto *E = D->getConstraintExpr()) {
1328 if (Visit(Cursor: MakeCXCursor(S: E, Parent: D, TU, RegionOfInterest)))
1329 return true;
1330 }
1331 return false;
1332}
1333
1334bool CursorVisitor::VisitTypeConstraint(const TypeConstraint &TC) {
1335 if (TC.getNestedNameSpecifierLoc()) {
1336 if (VisitNestedNameSpecifierLoc(NNS: TC.getNestedNameSpecifierLoc()))
1337 return true;
1338 }
1339 if (TC.getNamedConcept()) {
1340 if (Visit(Cursor: MakeCursorTemplateRef(Template: TC.getNamedConcept(),
1341 Loc: TC.getConceptNameLoc(), TU)))
1342 return true;
1343 }
1344 if (auto Args = TC.getTemplateArgsAsWritten()) {
1345 for (const auto &Arg : Args->arguments()) {
1346 if (VisitTemplateArgumentLoc(TAL: Arg))
1347 return true;
1348 }
1349 }
1350 return false;
1351}
1352
1353bool CursorVisitor::VisitConceptRequirement(const concepts::Requirement &R) {
1354 using namespace concepts;
1355 switch (R.getKind()) {
1356 case Requirement::RK_Type: {
1357 const TypeRequirement &TR = cast<TypeRequirement>(Val: R);
1358 if (!TR.isSubstitutionFailure()) {
1359 if (Visit(TyLoc: TR.getType()->getTypeLoc()))
1360 return true;
1361 }
1362 break;
1363 }
1364 case Requirement::RK_Simple:
1365 case Requirement::RK_Compound: {
1366 const ExprRequirement &ER = cast<ExprRequirement>(Val: R);
1367 if (!ER.isExprSubstitutionFailure()) {
1368 if (Visit(S: ER.getExpr()))
1369 return true;
1370 }
1371 if (ER.getKind() == Requirement::RK_Compound) {
1372 const auto &RTR = ER.getReturnTypeRequirement();
1373 if (RTR.isTypeConstraint()) {
1374 if (const auto *Cons = RTR.getTypeConstraint())
1375 VisitTypeConstraint(TC: *Cons);
1376 }
1377 }
1378 break;
1379 }
1380 case Requirement::RK_Nested: {
1381 const NestedRequirement &NR = cast<NestedRequirement>(Val: R);
1382 if (!NR.hasInvalidConstraint()) {
1383 if (Visit(S: NR.getConstraintExpr()))
1384 return true;
1385 }
1386 break;
1387 }
1388 }
1389 return false;
1390}
1391
1392bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1393 switch (Name.getName().getNameKind()) {
1394 case clang::DeclarationName::Identifier:
1395 case clang::DeclarationName::CXXLiteralOperatorName:
1396 case clang::DeclarationName::CXXDeductionGuideName:
1397 case clang::DeclarationName::CXXOperatorName:
1398 case clang::DeclarationName::CXXUsingDirective:
1399 return false;
1400
1401 case clang::DeclarationName::CXXConstructorName:
1402 case clang::DeclarationName::CXXDestructorName:
1403 case clang::DeclarationName::CXXConversionFunctionName:
1404 if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1405 return Visit(TyLoc: TSInfo->getTypeLoc());
1406 return false;
1407
1408 case clang::DeclarationName::ObjCZeroArgSelector:
1409 case clang::DeclarationName::ObjCOneArgSelector:
1410 case clang::DeclarationName::ObjCMultiArgSelector:
1411 // FIXME: Per-identifier location info?
1412 return false;
1413 }
1414
1415 llvm_unreachable("Invalid DeclarationName::Kind!");
1416}
1417
1418bool CursorVisitor::VisitNestedNameSpecifierLoc(
1419 NestedNameSpecifierLoc Qualifier) {
1420 NestedNameSpecifier NNS = Qualifier.getNestedNameSpecifier();
1421 switch (NNS.getKind()) {
1422 case NestedNameSpecifier::Kind::Namespace: {
1423 auto [Namespace, Prefix] = Qualifier.castAsNamespaceAndPrefix();
1424 if (VisitNestedNameSpecifierLoc(Qualifier: Prefix))
1425 return true;
1426 return Visit(
1427 Cursor: MakeCursorNamespaceRef(NS: Namespace, Loc: Qualifier.getLocalBeginLoc(), TU));
1428 }
1429 case NestedNameSpecifier::Kind::Type:
1430 return Visit(TyLoc: Qualifier.castAsTypeLoc());
1431 case NestedNameSpecifier::Kind::Null:
1432 case NestedNameSpecifier::Kind::Global:
1433 case NestedNameSpecifier::Kind::MicrosoftSuper:
1434 return false;
1435 }
1436 llvm_unreachable("unexpected nested name specifier kind");
1437}
1438
1439bool CursorVisitor::VisitTemplateParameters(
1440 const TemplateParameterList *Params) {
1441 if (!Params)
1442 return false;
1443
1444 for (TemplateParameterList::const_iterator P = Params->begin(),
1445 PEnd = Params->end();
1446 P != PEnd; ++P) {
1447 if (Visit(Cursor: MakeCXCursor(D: *P, TU, RegionOfInterest)))
1448 return true;
1449 }
1450
1451 if (const auto *E = Params->getRequiresClause()) {
1452 if (Visit(Cursor: MakeCXCursor(S: E, Parent: nullptr, TU, RegionOfInterest)))
1453 return true;
1454 }
1455
1456 return false;
1457}
1458
1459bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation NameLoc,
1460 NestedNameSpecifierLoc NNS) {
1461 switch (Name.getKind()) {
1462 case TemplateName::QualifiedTemplate: {
1463 const QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName();
1464 assert(QTN->getQualifier() == NNS.getNestedNameSpecifier());
1465 if (VisitNestedNameSpecifierLoc(Qualifier: NNS))
1466 return true;
1467 return VisitTemplateName(Name: QTN->getUnderlyingTemplate(), NameLoc, /*NNS=*/{});
1468 }
1469 case TemplateName::Template:
1470 case TemplateName::UsingTemplate:
1471 return Visit(Cursor: MakeCursorTemplateRef(Template: Name.getAsTemplateDecl(), Loc: NameLoc, TU));
1472
1473 case TemplateName::OverloadedTemplate:
1474 // Visit the overloaded template set.
1475 if (Visit(Cursor: MakeCursorOverloadedDeclRef(Template: Name, Location: NameLoc, TU)))
1476 return true;
1477
1478 return false;
1479
1480 case TemplateName::AssumedTemplate:
1481 // FIXME: Visit DeclarationName?
1482 return false;
1483
1484 case TemplateName::DependentTemplate: {
1485 assert(Name.getAsDependentTemplateName()->getQualifier() ==
1486 NNS.getNestedNameSpecifier());
1487 return VisitNestedNameSpecifierLoc(Qualifier: NNS);
1488 }
1489
1490 case TemplateName::SubstTemplateTemplateParm:
1491 return Visit(Cursor: MakeCursorTemplateRef(
1492 Template: Name.getAsSubstTemplateTemplateParm()->getParameter(), Loc: NameLoc, TU));
1493
1494 case TemplateName::SubstTemplateTemplateParmPack:
1495 return Visit(Cursor: MakeCursorTemplateRef(
1496 Template: Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), Loc: NameLoc,
1497 TU));
1498
1499 case TemplateName::DeducedTemplate:
1500 llvm_unreachable("DeducedTemplate shouldn't appear in source");
1501 }
1502
1503 llvm_unreachable("Invalid TemplateName::Kind!");
1504}
1505
1506bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1507 switch (TAL.getArgument().getKind()) {
1508 case TemplateArgument::Null:
1509 case TemplateArgument::Integral:
1510 case TemplateArgument::Pack:
1511 return false;
1512
1513 case TemplateArgument::Type:
1514 if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1515 return Visit(TyLoc: TSInfo->getTypeLoc());
1516 return false;
1517
1518 case TemplateArgument::Declaration:
1519 if (Expr *E = TAL.getSourceDeclExpression())
1520 return Visit(Cursor: MakeCXCursor(S: E, Parent: StmtParent, TU, RegionOfInterest));
1521 return false;
1522
1523 case TemplateArgument::StructuralValue:
1524 if (Expr *E = TAL.getSourceStructuralValueExpression())
1525 return Visit(Cursor: MakeCXCursor(S: E, Parent: StmtParent, TU, RegionOfInterest));
1526 return false;
1527
1528 case TemplateArgument::NullPtr:
1529 if (Expr *E = TAL.getSourceNullPtrExpression())
1530 return Visit(Cursor: MakeCXCursor(S: E, Parent: StmtParent, TU, RegionOfInterest));
1531 return false;
1532
1533 case TemplateArgument::Expression:
1534 if (Expr *E = TAL.getSourceExpression())
1535 return Visit(Cursor: MakeCXCursor(S: E, Parent: StmtParent, TU, RegionOfInterest));
1536 return false;
1537
1538 case TemplateArgument::Template:
1539 case TemplateArgument::TemplateExpansion:
1540 return VisitTemplateName(Name: TAL.getArgument().getAsTemplateOrTemplatePattern(),
1541 NameLoc: TAL.getTemplateNameLoc(),
1542 NNS: TAL.getTemplateQualifierLoc());
1543 }
1544
1545 llvm_unreachable("Invalid TemplateArgument::Kind!");
1546}
1547
1548bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1549 return VisitDeclContext(DC: D);
1550}
1551
1552bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1553 return Visit(TyLoc: TL.getUnqualifiedLoc());
1554}
1555
1556bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1557 ASTContext &Context = AU->getASTContext();
1558
1559 // Some builtin types (such as Objective-C's "id", "sel", and
1560 // "Class") have associated declarations. Create cursors for those.
1561 QualType VisitType;
1562 switch (TL.getTypePtr()->getKind()) {
1563
1564 case BuiltinType::Void:
1565 case BuiltinType::NullPtr:
1566 case BuiltinType::Dependent:
1567#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1568 case BuiltinType::Id:
1569#include "clang/Basic/OpenCLImageTypes.def"
1570#define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) case BuiltinType::Id:
1571#include "clang/Basic/OpenCLExtensionTypes.def"
1572 case BuiltinType::OCLSampler:
1573 case BuiltinType::OCLEvent:
1574 case BuiltinType::OCLClkEvent:
1575 case BuiltinType::OCLQueue:
1576 case BuiltinType::OCLReserveID:
1577#define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1578#include "clang/Basic/AArch64ACLETypes.def"
1579#define PPC_VECTOR_TYPE(Name, Id, Size) case BuiltinType::Id:
1580#include "clang/Basic/PPCTypes.def"
1581#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1582#include "clang/Basic/RISCVVTypes.def"
1583#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1584#include "clang/Basic/WebAssemblyReferenceTypes.def"
1585#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
1586#include "clang/Basic/AMDGPUTypes.def"
1587#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1588#include "clang/Basic/HLSLIntangibleTypes.def"
1589#define BUILTIN_TYPE(Id, SingletonId)
1590#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1591#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1592#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1593#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1594#include "clang/AST/BuiltinTypes.def"
1595 break;
1596
1597 case BuiltinType::ObjCId:
1598 VisitType = Context.getObjCIdType();
1599 break;
1600
1601 case BuiltinType::ObjCClass:
1602 VisitType = Context.getObjCClassType();
1603 break;
1604
1605 case BuiltinType::ObjCSel:
1606 VisitType = Context.getObjCSelType();
1607 break;
1608 }
1609
1610 if (!VisitType.isNull()) {
1611 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1612 return Visit(
1613 Cursor: MakeCursorTypeRef(Type: Typedef->getDecl(), Loc: TL.getBuiltinLoc(), TU));
1614 }
1615
1616 return false;
1617}
1618
1619bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1620 if (VisitNestedNameSpecifierLoc(Qualifier: TL.getQualifierLoc()))
1621 return true;
1622
1623 return Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getNameLoc(), TU));
1624}
1625
1626bool CursorVisitor::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
1627 return false;
1628}
1629
1630bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1631 if (VisitNestedNameSpecifierLoc(Qualifier: TL.getQualifierLoc()))
1632 return true;
1633
1634 return Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getNameLoc(), TU));
1635}
1636
1637bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1638 if (VisitNestedNameSpecifierLoc(Qualifier: TL.getQualifierLoc()))
1639 return true;
1640
1641 if (TL.isDefinition())
1642 return Visit(Cursor: MakeCXCursor(D: TL.getDecl(), TU, RegionOfInterest));
1643
1644 return Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getNameLoc(), TU));
1645}
1646
1647bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1648 if (const auto *TC = TL.getDecl()->getTypeConstraint()) {
1649 if (VisitTypeConstraint(TC: *TC))
1650 return true;
1651 }
1652
1653 return Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getNameLoc(), TU));
1654}
1655
1656bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1657 return Visit(Cursor: MakeCursorObjCClassRef(Class: TL.getIFaceDecl(), Loc: TL.getNameLoc(), TU));
1658}
1659
1660bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
1661 if (Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getBeginLoc(), TU)))
1662 return true;
1663 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1664 if (Visit(Cursor: MakeCursorObjCProtocolRef(Proto: TL.getProtocol(i: I), Loc: TL.getProtocolLoc(i: I),
1665 TU)))
1666 return true;
1667 }
1668
1669 return false;
1670}
1671
1672bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1673 if (TL.hasBaseTypeAsWritten() && Visit(TyLoc: TL.getBaseLoc()))
1674 return true;
1675
1676 for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) {
1677 if (Visit(TyLoc: TL.getTypeArgTInfo(i: I)->getTypeLoc()))
1678 return true;
1679 }
1680
1681 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1682 if (Visit(Cursor: MakeCursorObjCProtocolRef(Proto: TL.getProtocol(i: I), Loc: TL.getProtocolLoc(i: I),
1683 TU)))
1684 return true;
1685 }
1686
1687 return false;
1688}
1689
1690bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1691 return Visit(TyLoc: TL.getPointeeLoc());
1692}
1693
1694bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1695 return Visit(TyLoc: TL.getInnerLoc());
1696}
1697
1698bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
1699 return Visit(TyLoc: TL.getInnerLoc());
1700}
1701
1702bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1703 return Visit(TyLoc: TL.getPointeeLoc());
1704}
1705
1706bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1707 return Visit(TyLoc: TL.getPointeeLoc());
1708}
1709
1710bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1711 return Visit(TyLoc: TL.getPointeeLoc());
1712}
1713
1714bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1715 return Visit(TyLoc: TL.getPointeeLoc());
1716}
1717
1718bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1719 return Visit(TyLoc: TL.getPointeeLoc());
1720}
1721
1722bool CursorVisitor::VisitUsingTypeLoc(UsingTypeLoc TL) {
1723 if (VisitNestedNameSpecifierLoc(Qualifier: TL.getQualifierLoc()))
1724 return true;
1725
1726 auto *underlyingDecl = TL.getTypePtr()->getAsTagDecl();
1727 if (underlyingDecl) {
1728 return Visit(Cursor: MakeCursorTypeRef(Type: underlyingDecl, Loc: TL.getNameLoc(), TU));
1729 }
1730 return false;
1731}
1732
1733bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1734 return Visit(TyLoc: TL.getModifiedLoc());
1735}
1736
1737bool CursorVisitor::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
1738 return Visit(TyLoc: TL.getInnerLoc());
1739}
1740
1741bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
1742 return Visit(TyLoc: TL.getWrappedLoc());
1743}
1744
1745bool CursorVisitor::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
1746 return Visit(TyLoc: TL.getWrappedLoc());
1747}
1748
1749bool CursorVisitor::VisitHLSLAttributedResourceTypeLoc(
1750 HLSLAttributedResourceTypeLoc TL) {
1751 return Visit(TyLoc: TL.getWrappedLoc());
1752}
1753
1754bool CursorVisitor::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
1755 // Nothing to do.
1756 return false;
1757}
1758
1759bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1760 bool SkipResultType) {
1761 if (!SkipResultType && Visit(TyLoc: TL.getReturnLoc()))
1762 return true;
1763
1764 for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I)
1765 if (Decl *D = TL.getParam(i: I))
1766 if (Visit(Cursor: MakeCXCursor(D, TU, RegionOfInterest)))
1767 return true;
1768
1769 return false;
1770}
1771
1772bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1773 if (Visit(TyLoc: TL.getElementLoc()))
1774 return true;
1775
1776 if (Expr *Size = TL.getSizeExpr())
1777 return Visit(Cursor: MakeCXCursor(S: Size, Parent: StmtParent, TU, RegionOfInterest));
1778
1779 return false;
1780}
1781
1782bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
1783 return Visit(TyLoc: TL.getOriginalLoc());
1784}
1785
1786bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
1787 return Visit(TyLoc: TL.getOriginalLoc());
1788}
1789
1790bool CursorVisitor::VisitAutoTypeLoc(AutoTypeLoc TL) {
1791
1792 if (TL.isConstrained()) {
1793 if (auto *CR = TL.getConceptReference()) {
1794 if (CR->getNamedConcept()) {
1795 return Visit(Cursor: MakeCursorTemplateRef(Template: CR->getNamedConcept(),
1796 Loc: CR->getConceptNameLoc(), TU));
1797 }
1798 }
1799 }
1800
1801 return false;
1802}
1803
1804bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
1805 DeducedTemplateSpecializationTypeLoc TL) {
1806 if (VisitTemplateName(Name: TL.getTypePtr()->getTemplateName(),
1807 NameLoc: TL.getTemplateNameLoc(), NNS: TL.getQualifierLoc()))
1808 return true;
1809
1810 return false;
1811}
1812
1813bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1814 TemplateSpecializationTypeLoc TL) {
1815 // Visit the template name.
1816 if (VisitTemplateName(Name: TL.getTypePtr()->getTemplateName(),
1817 NameLoc: TL.getTemplateNameLoc(), NNS: TL.getQualifierLoc()))
1818 return true;
1819
1820 // Visit the template arguments.
1821 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1822 if (VisitTemplateArgumentLoc(TAL: TL.getArgLoc(i: I)))
1823 return true;
1824
1825 return false;
1826}
1827
1828bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1829 return Visit(Cursor: MakeCXCursor(S: TL.getUnderlyingExpr(), Parent: StmtParent, TU));
1830}
1831
1832bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1833 if (TypeSourceInfo *TSInfo = TL.getUnmodifiedTInfo())
1834 return Visit(TyLoc: TSInfo->getTypeLoc());
1835
1836 return false;
1837}
1838
1839bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1840 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1841 return Visit(TyLoc: TSInfo->getTypeLoc());
1842
1843 return false;
1844}
1845
1846bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1847 return VisitNestedNameSpecifierLoc(Qualifier: TL.getQualifierLoc());
1848}
1849
1850bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1851 return Visit(TyLoc: TL.getPatternLoc());
1852}
1853
1854bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1855 if (Expr *E = TL.getUnderlyingExpr())
1856 return Visit(Cursor: MakeCXCursor(S: E, Parent: StmtParent, TU));
1857
1858 return false;
1859}
1860
1861bool CursorVisitor::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
1862 if (Visit(TyLoc: TL.getPatternLoc()))
1863 return true;
1864 return Visit(Cursor: MakeCXCursor(S: TL.getIndexExpr(), Parent: StmtParent, TU));
1865}
1866
1867bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1868 return Visit(Cursor: MakeCursorTypeRef(Type: TL.getDecl(), Loc: TL.getNameLoc(), TU));
1869}
1870
1871bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1872 return Visit(TyLoc: TL.getValueLoc());
1873}
1874
1875bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) {
1876 return Visit(TyLoc: TL.getValueLoc());
1877}
1878
1879#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \
1880 bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
1881 return Visit##PARENT##Loc(TL); \
1882 }
1883
1884DEFAULT_TYPELOC_IMPL(Complex, Type)
1885DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
1886DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
1887DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
1888DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
1889DEFAULT_TYPELOC_IMPL(ArrayParameter, ConstantArrayType)
1890DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type)
1891DEFAULT_TYPELOC_IMPL(DependentVector, Type)
1892DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
1893DEFAULT_TYPELOC_IMPL(Vector, Type)
1894DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
1895DEFAULT_TYPELOC_IMPL(ConstantMatrix, MatrixType)
1896DEFAULT_TYPELOC_IMPL(DependentSizedMatrix, MatrixType)
1897DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
1898DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
1899DEFAULT_TYPELOC_IMPL(Record, TagType)
1900DEFAULT_TYPELOC_IMPL(Enum, TagType)
1901DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
1902DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
1903DEFAULT_TYPELOC_IMPL(SubstBuiltinTemplatePack, Type)
1904DEFAULT_TYPELOC_IMPL(BitInt, Type)
1905DEFAULT_TYPELOC_IMPL(DependentBitInt, Type)
1906
1907bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1908 // Visit the nested-name-specifier, if present.
1909 if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1910 if (VisitNestedNameSpecifierLoc(Qualifier: QualifierLoc))
1911 return true;
1912
1913 if (D->isCompleteDefinition()) {
1914 for (const auto &I : D->bases()) {
1915 if (Visit(Cursor: cxcursor::MakeCursorCXXBaseSpecifier(B: &I, TU)))
1916 return true;
1917 }
1918 }
1919
1920 return VisitTagDecl(D);
1921}
1922
1923bool CursorVisitor::VisitAttributes(Decl *D) {
1924 for (const auto *I : D->attrs())
1925 if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes ||
1926 !I->isImplicit()) &&
1927 Visit(Cursor: MakeCXCursor(A: I, Parent: D, TU)))
1928 return true;
1929
1930 return false;
1931}
1932
1933//===----------------------------------------------------------------------===//
1934// Data-recursive visitor methods.
1935//===----------------------------------------------------------------------===//
1936
1937namespace {
1938#define DEF_JOB(NAME, DATA, KIND) \
1939 class NAME : public VisitorJob { \
1940 public: \
1941 NAME(const DATA *d, CXCursor parent) \
1942 : VisitorJob(parent, VisitorJob::KIND, d) {} \
1943 static bool classof(const VisitorJob *VJ) { \
1944 return VJ->getKind() == KIND; \
1945 } \
1946 const DATA *get() const { return static_cast<const DATA *>(data[0]); } \
1947 };
1948
1949DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1950DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1951DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1952DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1953DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1954DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1955DEF_JOB(ConceptSpecializationExprVisit, ConceptSpecializationExpr,
1956 ConceptSpecializationExprVisitKind)
1957DEF_JOB(RequiresExprVisit, RequiresExpr, RequiresExprVisitKind)
1958DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
1959#undef DEF_JOB
1960
1961class ExplicitTemplateArgsVisit : public VisitorJob {
1962public:
1963 ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin,
1964 const TemplateArgumentLoc *End, CXCursor parent)
1965 : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin,
1966 End) {}
1967 static bool classof(const VisitorJob *VJ) {
1968 return VJ->getKind() == ExplicitTemplateArgsVisitKind;
1969 }
1970 const TemplateArgumentLoc *begin() const {
1971 return static_cast<const TemplateArgumentLoc *>(data[0]);
1972 }
1973 const TemplateArgumentLoc *end() {
1974 return static_cast<const TemplateArgumentLoc *>(data[1]);
1975 }
1976};
1977class DeclVisit : public VisitorJob {
1978public:
1979 DeclVisit(const Decl *D, CXCursor parent, bool isFirst)
1980 : VisitorJob(parent, VisitorJob::DeclVisitKind, D,
1981 isFirst ? (void *)1 : (void *)nullptr) {}
1982 static bool classof(const VisitorJob *VJ) {
1983 return VJ->getKind() == DeclVisitKind;
1984 }
1985 const Decl *get() const { return static_cast<const Decl *>(data[0]); }
1986 bool isFirst() const { return data[1] != nullptr; }
1987};
1988class TypeLocVisit : public VisitorJob {
1989public:
1990 TypeLocVisit(TypeLoc tl, CXCursor parent)
1991 : VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1992 tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1993
1994 static bool classof(const VisitorJob *VJ) {
1995 return VJ->getKind() == TypeLocVisitKind;
1996 }
1997
1998 TypeLoc get() const {
1999 QualType T = QualType::getFromOpaquePtr(Ptr: data[0]);
2000 return TypeLoc(T, const_cast<void *>(data[1]));
2001 }
2002};
2003
2004class LabelRefVisit : public VisitorJob {
2005public:
2006 LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
2007 : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
2008 labelLoc.getPtrEncoding()) {}
2009
2010 static bool classof(const VisitorJob *VJ) {
2011 return VJ->getKind() == VisitorJob::LabelRefVisitKind;
2012 }
2013 const LabelDecl *get() const {
2014 return static_cast<const LabelDecl *>(data[0]);
2015 }
2016 SourceLocation getLoc() const {
2017 return SourceLocation::getFromPtrEncoding(Encoding: data[1]);
2018 }
2019};
2020
2021class NestedNameSpecifierLocVisit : public VisitorJob {
2022public:
2023 NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
2024 : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
2025 Qualifier.getNestedNameSpecifier().getAsVoidPointer(),
2026 Qualifier.getOpaqueData()) {}
2027
2028 static bool classof(const VisitorJob *VJ) {
2029 return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
2030 }
2031
2032 NestedNameSpecifierLoc get() const {
2033 return NestedNameSpecifierLoc(
2034 NestedNameSpecifier::getFromVoidPointer(Ptr: data[0]),
2035 const_cast<void *>(data[1]));
2036 }
2037};
2038
2039class DeclarationNameInfoVisit : public VisitorJob {
2040public:
2041 DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
2042 : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
2043 static bool classof(const VisitorJob *VJ) {
2044 return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
2045 }
2046 DeclarationNameInfo get() const {
2047 const Stmt *S = static_cast<const Stmt *>(data[0]);
2048 switch (S->getStmtClass()) {
2049 default:
2050 llvm_unreachable("Unhandled Stmt");
2051 case clang::Stmt::MSDependentExistsStmtClass:
2052 return cast<MSDependentExistsStmt>(Val: S)->getNameInfo();
2053 case Stmt::CXXDependentScopeMemberExprClass:
2054 return cast<CXXDependentScopeMemberExpr>(Val: S)->getMemberNameInfo();
2055 case Stmt::DependentScopeDeclRefExprClass:
2056 return cast<DependentScopeDeclRefExpr>(Val: S)->getNameInfo();
2057 case Stmt::OMPCriticalDirectiveClass:
2058 return cast<OMPCriticalDirective>(Val: S)->getDirectiveName();
2059 }
2060 }
2061};
2062class MemberRefVisit : public VisitorJob {
2063public:
2064 MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
2065 : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
2066 L.getPtrEncoding()) {}
2067 static bool classof(const VisitorJob *VJ) {
2068 return VJ->getKind() == VisitorJob::MemberRefVisitKind;
2069 }
2070 const FieldDecl *get() const {
2071 return static_cast<const FieldDecl *>(data[0]);
2072 }
2073 SourceLocation getLoc() const {
2074 return SourceLocation::getFromRawEncoding(
2075 Encoding: (SourceLocation::UIntTy)(uintptr_t)data[1]);
2076 }
2077};
2078class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void>,
2079 public ConstAttrVisitor<EnqueueVisitor, void> {
2080 friend class OpenACCClauseEnqueue;
2081 friend class OMPClauseEnqueue;
2082 VisitorWorkList &WL;
2083 CXCursor Parent;
2084
2085public:
2086 EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
2087 : WL(wl), Parent(parent) {}
2088
2089 void VisitAddrLabelExpr(const AddrLabelExpr *E);
2090 void VisitBlockExpr(const BlockExpr *B);
2091 void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
2092 void VisitCompoundStmt(const CompoundStmt *S);
2093 void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */
2094 }
2095 void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
2096 void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
2097 void VisitCXXNewExpr(const CXXNewExpr *E);
2098 void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
2099 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
2100 void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
2101 void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
2102 void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2103 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
2104 void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
2105 void VisitCXXCatchStmt(const CXXCatchStmt *S);
2106 void VisitCXXForRangeStmt(const CXXForRangeStmt *S);
2107 void VisitDeclRefExpr(const DeclRefExpr *D);
2108 void VisitDeclStmt(const DeclStmt *S);
2109 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
2110 void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
2111 void VisitExplicitCastExpr(const ExplicitCastExpr *E);
2112 void VisitForStmt(const ForStmt *FS);
2113 void VisitGotoStmt(const GotoStmt *GS);
2114 void VisitIfStmt(const IfStmt *If);
2115 void VisitInitListExpr(const InitListExpr *IE);
2116 void VisitMemberExpr(const MemberExpr *M);
2117 void VisitOffsetOfExpr(const OffsetOfExpr *E);
2118 void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
2119 void VisitObjCMessageExpr(const ObjCMessageExpr *M);
2120 void VisitOverloadExpr(const OverloadExpr *E);
2121 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
2122 void VisitStmt(const Stmt *S);
2123 void VisitSwitchStmt(const SwitchStmt *S);
2124 void VisitWhileStmt(const WhileStmt *W);
2125 void VisitTypeTraitExpr(const TypeTraitExpr *E);
2126 void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
2127 void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
2128 void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
2129 void VisitVAArgExpr(const VAArgExpr *E);
2130 void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
2131 void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
2132 void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
2133 void VisitLambdaExpr(const LambdaExpr *E);
2134 void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
2135 void VisitRequiresExpr(const RequiresExpr *E);
2136 void VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
2137 void VisitOpenACCComputeConstruct(const OpenACCComputeConstruct *D);
2138 void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *D);
2139 void VisitOpenACCCombinedConstruct(const OpenACCCombinedConstruct *D);
2140 void VisitOpenACCDataConstruct(const OpenACCDataConstruct *D);
2141 void VisitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct *D);
2142 void VisitOpenACCExitDataConstruct(const OpenACCExitDataConstruct *D);
2143 void VisitOpenACCHostDataConstruct(const OpenACCHostDataConstruct *D);
2144 void VisitOpenACCWaitConstruct(const OpenACCWaitConstruct *D);
2145 void VisitOpenACCCacheConstruct(const OpenACCCacheConstruct *D);
2146 void VisitOpenACCInitConstruct(const OpenACCInitConstruct *D);
2147 void VisitOpenACCShutdownConstruct(const OpenACCShutdownConstruct *D);
2148 void VisitOpenACCSetConstruct(const OpenACCSetConstruct *D);
2149 void VisitOpenACCUpdateConstruct(const OpenACCUpdateConstruct *D);
2150 void VisitOpenACCAtomicConstruct(const OpenACCAtomicConstruct *D);
2151 void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
2152 void VisitOMPLoopBasedDirective(const OMPLoopBasedDirective *D);
2153 void VisitOMPLoopDirective(const OMPLoopDirective *D);
2154 void VisitOMPParallelDirective(const OMPParallelDirective *D);
2155 void VisitOMPSimdDirective(const OMPSimdDirective *D);
2156 void VisitOMPCanonicalLoopNestTransformationDirective(
2157 const OMPCanonicalLoopNestTransformationDirective *D);
2158 void VisitOMPTileDirective(const OMPTileDirective *D);
2159 void VisitOMPStripeDirective(const OMPStripeDirective *D);
2160 void VisitOMPUnrollDirective(const OMPUnrollDirective *D);
2161 void VisitOMPReverseDirective(const OMPReverseDirective *D);
2162 void VisitOMPInterchangeDirective(const OMPInterchangeDirective *D);
2163 void VisitOMPCanonicalLoopSequenceTransformationDirective(
2164 const OMPCanonicalLoopSequenceTransformationDirective *D);
2165 void VisitOMPFuseDirective(const OMPFuseDirective *D);
2166 void VisitOMPForDirective(const OMPForDirective *D);
2167 void VisitOMPForSimdDirective(const OMPForSimdDirective *D);
2168 void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
2169 void VisitOMPSectionDirective(const OMPSectionDirective *D);
2170 void VisitOMPSingleDirective(const OMPSingleDirective *D);
2171 void VisitOMPMasterDirective(const OMPMasterDirective *D);
2172 void VisitOMPCriticalDirective(const OMPCriticalDirective *D);
2173 void VisitOMPParallelForDirective(const OMPParallelForDirective *D);
2174 void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D);
2175 void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D);
2176 void VisitOMPParallelMaskedDirective(const OMPParallelMaskedDirective *D);
2177 void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D);
2178 void VisitOMPTaskDirective(const OMPTaskDirective *D);
2179 void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D);
2180 void VisitOMPBarrierDirective(const OMPBarrierDirective *D);
2181 void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D);
2182 void VisitOMPAssumeDirective(const OMPAssumeDirective *D);
2183 void VisitOMPErrorDirective(const OMPErrorDirective *D);
2184 void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D);
2185 void
2186 VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D);
2187 void VisitOMPCancelDirective(const OMPCancelDirective *D);
2188 void VisitOMPFlushDirective(const OMPFlushDirective *D);
2189 void VisitOMPDepobjDirective(const OMPDepobjDirective *D);
2190 void VisitOMPScanDirective(const OMPScanDirective *D);
2191 void VisitOMPOrderedDirective(const OMPOrderedDirective *D);
2192 void VisitOMPAtomicDirective(const OMPAtomicDirective *D);
2193 void VisitOMPTargetDirective(const OMPTargetDirective *D);
2194 void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D);
2195 void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D);
2196 void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D);
2197 void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D);
2198 void
2199 VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D);
2200 void VisitOMPTeamsDirective(const OMPTeamsDirective *D);
2201 void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D);
2202 void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D);
2203 void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D);
2204 void VisitOMPMaskedTaskLoopDirective(const OMPMaskedTaskLoopDirective *D);
2205 void
2206 VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D);
2207 void VisitOMPMaskedTaskLoopSimdDirective(
2208 const OMPMaskedTaskLoopSimdDirective *D);
2209 void VisitOMPParallelMasterTaskLoopDirective(
2210 const OMPParallelMasterTaskLoopDirective *D);
2211 void VisitOMPParallelMaskedTaskLoopDirective(
2212 const OMPParallelMaskedTaskLoopDirective *D);
2213 void VisitOMPParallelMasterTaskLoopSimdDirective(
2214 const OMPParallelMasterTaskLoopSimdDirective *D);
2215 void VisitOMPParallelMaskedTaskLoopSimdDirective(
2216 const OMPParallelMaskedTaskLoopSimdDirective *D);
2217 void VisitOMPDistributeDirective(const OMPDistributeDirective *D);
2218 void VisitOMPDistributeParallelForDirective(
2219 const OMPDistributeParallelForDirective *D);
2220 void VisitOMPDistributeParallelForSimdDirective(
2221 const OMPDistributeParallelForSimdDirective *D);
2222 void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D);
2223 void VisitOMPTargetParallelForSimdDirective(
2224 const OMPTargetParallelForSimdDirective *D);
2225 void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D);
2226 void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D);
2227 void VisitOMPTeamsDistributeSimdDirective(
2228 const OMPTeamsDistributeSimdDirective *D);
2229 void VisitOMPTeamsDistributeParallelForSimdDirective(
2230 const OMPTeamsDistributeParallelForSimdDirective *D);
2231 void VisitOMPTeamsDistributeParallelForDirective(
2232 const OMPTeamsDistributeParallelForDirective *D);
2233 void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D);
2234 void VisitOMPTargetTeamsDistributeDirective(
2235 const OMPTargetTeamsDistributeDirective *D);
2236 void VisitOMPTargetTeamsDistributeParallelForDirective(
2237 const OMPTargetTeamsDistributeParallelForDirective *D);
2238 void VisitOMPTargetTeamsDistributeParallelForSimdDirective(
2239 const OMPTargetTeamsDistributeParallelForSimdDirective *D);
2240 void VisitOMPTargetTeamsDistributeSimdDirective(
2241 const OMPTargetTeamsDistributeSimdDirective *D);
2242
2243 // Attributes
2244 void VisitAnnotateAttr(const AnnotateAttr *A);
2245
2246private:
2247 void AddDeclarationNameInfo(const Stmt *S);
2248 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
2249 void AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2250 unsigned NumTemplateArgs);
2251 void AddMemberRef(const FieldDecl *D, SourceLocation L);
2252 void AddStmt(const Stmt *S);
2253 void AddDecl(const Decl *D, bool isFirst = true);
2254 void AddTypeLoc(TypeSourceInfo *TI);
2255 void EnqueueChildren(const Stmt *S);
2256 void EnqueueChildren(const OpenACCClause *S);
2257 void EnqueueChildren(const OMPClause *S);
2258 void EnqueueChildren(const AnnotateAttr *A);
2259};
2260} // namespace
2261
2262void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
2263 // 'S' should always be non-null, since it comes from the
2264 // statement we are visiting.
2265 WL.push_back(Elt: DeclarationNameInfoVisit(S, Parent));
2266}
2267
2268void EnqueueVisitor::AddNestedNameSpecifierLoc(
2269 NestedNameSpecifierLoc Qualifier) {
2270 if (Qualifier)
2271 WL.push_back(Elt: NestedNameSpecifierLocVisit(Qualifier, Parent));
2272}
2273
2274void EnqueueVisitor::AddStmt(const Stmt *S) {
2275 if (S)
2276 WL.push_back(Elt: StmtVisit(S, Parent));
2277}
2278void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
2279 if (D)
2280 WL.push_back(Elt: DeclVisit(D, Parent, isFirst));
2281}
2282void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2283 unsigned NumTemplateArgs) {
2284 WL.push_back(Elt: ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent));
2285}
2286void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
2287 if (D)
2288 WL.push_back(Elt: MemberRefVisit(D, L, Parent));
2289}
2290void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
2291 if (TI)
2292 WL.push_back(Elt: TypeLocVisit(TI->getTypeLoc(), Parent));
2293}
2294void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
2295 unsigned size = WL.size();
2296 for (const Stmt *SubStmt : S->children()) {
2297 AddStmt(S: SubStmt);
2298 }
2299 if (size == WL.size())
2300 return;
2301 // Now reverse the entries we just added. This will match the DFS
2302 // ordering performed by the worklist.
2303 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2304 std::reverse(first: I, last: E);
2305}
2306namespace {
2307class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
2308 EnqueueVisitor *Visitor;
2309 /// Process clauses with list of variables.
2310 template <typename T> void VisitOMPClauseList(T *Node);
2311
2312public:
2313 OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) {}
2314#define GEN_CLANG_CLAUSE_CLASS
2315#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(const Class *C);
2316#include "llvm/Frontend/OpenMP/OMP.inc"
2317 void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C);
2318 void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C);
2319};
2320
2321void OMPClauseEnqueue::VisitOMPClauseWithPreInit(
2322 const OMPClauseWithPreInit *C) {
2323 Visitor->AddStmt(S: C->getPreInitStmt());
2324}
2325
2326void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate(
2327 const OMPClauseWithPostUpdate *C) {
2328 VisitOMPClauseWithPreInit(C);
2329 Visitor->AddStmt(S: C->getPostUpdateExpr());
2330}
2331
2332void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
2333 VisitOMPClauseWithPreInit(C);
2334 Visitor->AddStmt(S: C->getCondition());
2335}
2336
2337void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) {
2338 Visitor->AddStmt(S: C->getCondition());
2339}
2340
2341void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
2342 VisitOMPClauseWithPreInit(C);
2343 Visitor->AddStmt(S: C->getNumThreads());
2344}
2345
2346void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) {
2347 Visitor->AddStmt(S: C->getSafelen());
2348}
2349
2350void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) {
2351 Visitor->AddStmt(S: C->getSimdlen());
2352}
2353
2354void OMPClauseEnqueue::VisitOMPSizesClause(const OMPSizesClause *C) {
2355 for (auto E : C->getSizesRefs())
2356 Visitor->AddStmt(S: E);
2357}
2358
2359void OMPClauseEnqueue::VisitOMPCountsClause(const OMPCountsClause *C) {
2360 for (auto E : C->getCountsRefs())
2361 Visitor->AddStmt(S: E);
2362}
2363
2364void OMPClauseEnqueue::VisitOMPPermutationClause(
2365 const OMPPermutationClause *C) {
2366 for (auto E : C->getArgsRefs())
2367 Visitor->AddStmt(S: E);
2368}
2369
2370void OMPClauseEnqueue::VisitOMPFullClause(const OMPFullClause *C) {}
2371
2372void OMPClauseEnqueue::VisitOMPPartialClause(const OMPPartialClause *C) {
2373 Visitor->AddStmt(S: C->getFactor());
2374}
2375
2376void OMPClauseEnqueue::VisitOMPLoopRangeClause(const OMPLoopRangeClause *C) {
2377 Visitor->AddStmt(S: C->getFirst());
2378 Visitor->AddStmt(S: C->getCount());
2379}
2380
2381void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) {
2382 Visitor->AddStmt(S: C->getAllocator());
2383}
2384
2385void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) {
2386 Visitor->AddStmt(S: C->getNumForLoops());
2387}
2388
2389void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) {}
2390
2391void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) {}
2392
2393void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) {
2394 VisitOMPClauseWithPreInit(C);
2395 Visitor->AddStmt(S: C->getChunkSize());
2396}
2397
2398void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) {
2399 Visitor->AddStmt(S: C->getNumForLoops());
2400}
2401
2402void OMPClauseEnqueue::VisitOMPDetachClause(const OMPDetachClause *C) {
2403 Visitor->AddStmt(S: C->getEventHandler());
2404}
2405
2406void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *C) {
2407 Visitor->AddStmt(S: C->getCondition());
2408}
2409
2410void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
2411
2412void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
2413
2414void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
2415
2416void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {}
2417
2418void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {}
2419
2420void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
2421
2422void OMPClauseEnqueue::VisitOMPCompareClause(const OMPCompareClause *) {}
2423
2424void OMPClauseEnqueue::VisitOMPFailClause(const OMPFailClause *) {}
2425
2426void OMPClauseEnqueue::VisitOMPThreadsetClause(const OMPThreadsetClause *) {}
2427
2428void OMPClauseEnqueue::VisitOMPTransparentClause(
2429 const OMPTransparentClause *C) {
2430 Visitor->AddStmt(S: C->getImpexType());
2431}
2432
2433void OMPClauseEnqueue::VisitOMPAbsentClause(const OMPAbsentClause *) {}
2434
2435void OMPClauseEnqueue::VisitOMPHoldsClause(const OMPHoldsClause *) {}
2436
2437void OMPClauseEnqueue::VisitOMPContainsClause(const OMPContainsClause *) {}
2438
2439void OMPClauseEnqueue::VisitOMPNoOpenMPClause(const OMPNoOpenMPClause *) {}
2440
2441void OMPClauseEnqueue::VisitOMPNoOpenMPRoutinesClause(
2442 const OMPNoOpenMPRoutinesClause *) {}
2443
2444void OMPClauseEnqueue::VisitOMPNoOpenMPConstructsClause(
2445 const OMPNoOpenMPConstructsClause *) {}
2446
2447void OMPClauseEnqueue::VisitOMPNoParallelismClause(
2448 const OMPNoParallelismClause *) {}
2449
2450void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
2451
2452void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {}
2453
2454void OMPClauseEnqueue::VisitOMPAcquireClause(const OMPAcquireClause *) {}
2455
2456void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {}
2457
2458void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {}
2459
2460void OMPClauseEnqueue::VisitOMPWeakClause(const OMPWeakClause *) {}
2461
2462void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {}
2463
2464void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {}
2465
2466void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {}
2467
2468void OMPClauseEnqueue::VisitOMPInitClause(const OMPInitClause *C) {
2469 VisitOMPClauseList(Node: C);
2470 for (const Expr *A : C->attrs())
2471 Visitor->AddStmt(S: A);
2472}
2473
2474void OMPClauseEnqueue::VisitOMPUseClause(const OMPUseClause *C) {
2475 Visitor->AddStmt(S: C->getInteropVar());
2476}
2477
2478void OMPClauseEnqueue::VisitOMPDestroyClause(const OMPDestroyClause *C) {
2479 if (C->getInteropVar())
2480 Visitor->AddStmt(S: C->getInteropVar());
2481}
2482
2483void OMPClauseEnqueue::VisitOMPNovariantsClause(const OMPNovariantsClause *C) {
2484 Visitor->AddStmt(S: C->getCondition());
2485}
2486
2487void OMPClauseEnqueue::VisitOMPNocontextClause(const OMPNocontextClause *C) {
2488 Visitor->AddStmt(S: C->getCondition());
2489}
2490
2491void OMPClauseEnqueue::VisitOMPFilterClause(const OMPFilterClause *C) {
2492 VisitOMPClauseWithPreInit(C);
2493 Visitor->AddStmt(S: C->getThreadID());
2494}
2495
2496void OMPClauseEnqueue::VisitOMPAlignClause(const OMPAlignClause *C) {
2497 Visitor->AddStmt(S: C->getAlignment());
2498}
2499
2500void OMPClauseEnqueue::VisitOMPUnifiedAddressClause(
2501 const OMPUnifiedAddressClause *) {}
2502
2503void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause(
2504 const OMPUnifiedSharedMemoryClause *) {}
2505
2506void OMPClauseEnqueue::VisitOMPReverseOffloadClause(
2507 const OMPReverseOffloadClause *) {}
2508
2509void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause(
2510 const OMPDynamicAllocatorsClause *) {}
2511
2512void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause(
2513 const OMPAtomicDefaultMemOrderClause *) {}
2514
2515void OMPClauseEnqueue::VisitOMPSelfMapsClause(const OMPSelfMapsClause *) {}
2516
2517void OMPClauseEnqueue::VisitOMPAtClause(const OMPAtClause *) {}
2518
2519void OMPClauseEnqueue::VisitOMPSeverityClause(const OMPSeverityClause *) {}
2520
2521void OMPClauseEnqueue::VisitOMPMessageClause(const OMPMessageClause *) {}
2522
2523void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) {
2524 Visitor->AddStmt(S: C->getDevice());
2525}
2526
2527void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) {
2528 if (const Expr *Modifier = C->getModifierExpr())
2529 Visitor->AddStmt(S: Modifier);
2530 VisitOMPClauseList(Node: C);
2531 VisitOMPClauseWithPreInit(C);
2532}
2533
2534void OMPClauseEnqueue::VisitOMPThreadLimitClause(
2535 const OMPThreadLimitClause *C) {
2536 VisitOMPClauseList(Node: C);
2537 VisitOMPClauseWithPreInit(C);
2538}
2539
2540void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) {
2541 Visitor->AddStmt(S: C->getPriority());
2542}
2543
2544void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) {
2545 Visitor->AddStmt(S: C->getGrainsize());
2546}
2547
2548void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) {
2549 Visitor->AddStmt(S: C->getNumTasks());
2550}
2551
2552void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) {
2553 Visitor->AddStmt(S: C->getHint());
2554}
2555
2556template <typename T> void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
2557 for (const auto *I : Node->varlist()) {
2558 Visitor->AddStmt(S: I);
2559 }
2560}
2561
2562void OMPClauseEnqueue::VisitOMPInclusiveClause(const OMPInclusiveClause *C) {
2563 VisitOMPClauseList(Node: C);
2564}
2565void OMPClauseEnqueue::VisitOMPExclusiveClause(const OMPExclusiveClause *C) {
2566 VisitOMPClauseList(Node: C);
2567}
2568void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) {
2569 VisitOMPClauseList(Node: C);
2570 Visitor->AddStmt(S: C->getAllocator());
2571}
2572void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
2573 VisitOMPClauseList(Node: C);
2574 for (const auto *E : C->private_copies()) {
2575 Visitor->AddStmt(S: E);
2576 }
2577}
2578void OMPClauseEnqueue::VisitOMPFirstprivateClause(
2579 const OMPFirstprivateClause *C) {
2580 VisitOMPClauseList(Node: C);
2581 VisitOMPClauseWithPreInit(C);
2582 for (const auto *E : C->private_copies()) {
2583 Visitor->AddStmt(S: E);
2584 }
2585 for (const auto *E : C->inits()) {
2586 Visitor->AddStmt(S: E);
2587 }
2588}
2589void OMPClauseEnqueue::VisitOMPLastprivateClause(
2590 const OMPLastprivateClause *C) {
2591 VisitOMPClauseList(Node: C);
2592 VisitOMPClauseWithPostUpdate(C);
2593 for (auto *E : C->private_copies()) {
2594 Visitor->AddStmt(S: E);
2595 }
2596 for (auto *E : C->source_exprs()) {
2597 Visitor->AddStmt(S: E);
2598 }
2599 for (auto *E : C->destination_exprs()) {
2600 Visitor->AddStmt(S: E);
2601 }
2602 for (auto *E : C->assignment_ops()) {
2603 Visitor->AddStmt(S: E);
2604 }
2605}
2606void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) {
2607 VisitOMPClauseList(Node: C);
2608}
2609void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) {
2610 VisitOMPClauseList(Node: C);
2611 VisitOMPClauseWithPostUpdate(C);
2612 for (auto *E : C->privates()) {
2613 Visitor->AddStmt(S: E);
2614 }
2615 for (auto *E : C->lhs_exprs()) {
2616 Visitor->AddStmt(S: E);
2617 }
2618 for (auto *E : C->rhs_exprs()) {
2619 Visitor->AddStmt(S: E);
2620 }
2621 for (auto *E : C->reduction_ops()) {
2622 Visitor->AddStmt(S: E);
2623 }
2624 if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
2625 for (auto *E : C->copy_ops()) {
2626 Visitor->AddStmt(S: E);
2627 }
2628 for (auto *E : C->copy_array_temps()) {
2629 Visitor->AddStmt(S: E);
2630 }
2631 for (auto *E : C->copy_array_elems()) {
2632 Visitor->AddStmt(S: E);
2633 }
2634 }
2635}
2636void OMPClauseEnqueue::VisitOMPTaskReductionClause(
2637 const OMPTaskReductionClause *C) {
2638 VisitOMPClauseList(Node: C);
2639 VisitOMPClauseWithPostUpdate(C);
2640 for (auto *E : C->privates()) {
2641 Visitor->AddStmt(S: E);
2642 }
2643 for (auto *E : C->lhs_exprs()) {
2644 Visitor->AddStmt(S: E);
2645 }
2646 for (auto *E : C->rhs_exprs()) {
2647 Visitor->AddStmt(S: E);
2648 }
2649 for (auto *E : C->reduction_ops()) {
2650 Visitor->AddStmt(S: E);
2651 }
2652}
2653void OMPClauseEnqueue::VisitOMPInReductionClause(
2654 const OMPInReductionClause *C) {
2655 VisitOMPClauseList(Node: C);
2656 VisitOMPClauseWithPostUpdate(C);
2657 for (auto *E : C->privates()) {
2658 Visitor->AddStmt(S: E);
2659 }
2660 for (auto *E : C->lhs_exprs()) {
2661 Visitor->AddStmt(S: E);
2662 }
2663 for (auto *E : C->rhs_exprs()) {
2664 Visitor->AddStmt(S: E);
2665 }
2666 for (auto *E : C->reduction_ops()) {
2667 Visitor->AddStmt(S: E);
2668 }
2669 for (auto *E : C->taskgroup_descriptors())
2670 Visitor->AddStmt(S: E);
2671}
2672void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) {
2673 VisitOMPClauseList(Node: C);
2674 VisitOMPClauseWithPostUpdate(C);
2675 for (const auto *E : C->privates()) {
2676 Visitor->AddStmt(S: E);
2677 }
2678 for (const auto *E : C->inits()) {
2679 Visitor->AddStmt(S: E);
2680 }
2681 for (const auto *E : C->updates()) {
2682 Visitor->AddStmt(S: E);
2683 }
2684 for (const auto *E : C->finals()) {
2685 Visitor->AddStmt(S: E);
2686 }
2687 Visitor->AddStmt(S: C->getStep());
2688 Visitor->AddStmt(S: C->getCalcStep());
2689}
2690void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) {
2691 VisitOMPClauseList(Node: C);
2692 Visitor->AddStmt(S: C->getAlignment());
2693}
2694void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) {
2695 VisitOMPClauseList(Node: C);
2696 for (auto *E : C->source_exprs()) {
2697 Visitor->AddStmt(S: E);
2698 }
2699 for (auto *E : C->destination_exprs()) {
2700 Visitor->AddStmt(S: E);
2701 }
2702 for (auto *E : C->assignment_ops()) {
2703 Visitor->AddStmt(S: E);
2704 }
2705}
2706void OMPClauseEnqueue::VisitOMPCopyprivateClause(
2707 const OMPCopyprivateClause *C) {
2708 VisitOMPClauseList(Node: C);
2709 for (auto *E : C->source_exprs()) {
2710 Visitor->AddStmt(S: E);
2711 }
2712 for (auto *E : C->destination_exprs()) {
2713 Visitor->AddStmt(S: E);
2714 }
2715 for (auto *E : C->assignment_ops()) {
2716 Visitor->AddStmt(S: E);
2717 }
2718}
2719void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) {
2720 VisitOMPClauseList(Node: C);
2721}
2722void OMPClauseEnqueue::VisitOMPDepobjClause(const OMPDepobjClause *C) {
2723 Visitor->AddStmt(S: C->getDepobj());
2724}
2725void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) {
2726 VisitOMPClauseList(Node: C);
2727}
2728void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) {
2729 VisitOMPClauseList(Node: C);
2730}
2731void OMPClauseEnqueue::VisitOMPDistScheduleClause(
2732 const OMPDistScheduleClause *C) {
2733 VisitOMPClauseWithPreInit(C);
2734 Visitor->AddStmt(S: C->getChunkSize());
2735}
2736void OMPClauseEnqueue::VisitOMPDefaultmapClause(
2737 const OMPDefaultmapClause * /*C*/) {}
2738void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) {
2739 VisitOMPClauseList(Node: C);
2740}
2741void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) {
2742 VisitOMPClauseList(Node: C);
2743}
2744void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(
2745 const OMPUseDevicePtrClause *C) {
2746 VisitOMPClauseList(Node: C);
2747}
2748void OMPClauseEnqueue::VisitOMPUseDeviceAddrClause(
2749 const OMPUseDeviceAddrClause *C) {
2750 VisitOMPClauseList(Node: C);
2751}
2752void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(
2753 const OMPIsDevicePtrClause *C) {
2754 VisitOMPClauseList(Node: C);
2755}
2756void OMPClauseEnqueue::VisitOMPHasDeviceAddrClause(
2757 const OMPHasDeviceAddrClause *C) {
2758 VisitOMPClauseList(Node: C);
2759}
2760void OMPClauseEnqueue::VisitOMPNontemporalClause(
2761 const OMPNontemporalClause *C) {
2762 VisitOMPClauseList(Node: C);
2763 for (const auto *E : C->private_refs())
2764 Visitor->AddStmt(S: E);
2765}
2766void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {}
2767void OMPClauseEnqueue::VisitOMPUsesAllocatorsClause(
2768 const OMPUsesAllocatorsClause *C) {
2769 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
2770 const OMPUsesAllocatorsClause::Data &D = C->getAllocatorData(I);
2771 Visitor->AddStmt(S: D.Allocator);
2772 Visitor->AddStmt(S: D.AllocatorTraits);
2773 }
2774}
2775void OMPClauseEnqueue::VisitOMPAffinityClause(const OMPAffinityClause *C) {
2776 Visitor->AddStmt(S: C->getModifier());
2777 for (const Expr *E : C->varlist())
2778 Visitor->AddStmt(S: E);
2779}
2780void OMPClauseEnqueue::VisitOMPBindClause(const OMPBindClause *C) {}
2781void OMPClauseEnqueue::VisitOMPXDynCGroupMemClause(
2782 const OMPXDynCGroupMemClause *C) {
2783 VisitOMPClauseWithPreInit(C);
2784 Visitor->AddStmt(S: C->getSize());
2785}
2786void OMPClauseEnqueue::VisitOMPDynGroupprivateClause(
2787 const OMPDynGroupprivateClause *C) {
2788 VisitOMPClauseWithPreInit(C);
2789 Visitor->AddStmt(S: C->getSize());
2790}
2791void OMPClauseEnqueue::VisitOMPDoacrossClause(const OMPDoacrossClause *C) {
2792 VisitOMPClauseList(Node: C);
2793}
2794void OMPClauseEnqueue::VisitOMPXAttributeClause(const OMPXAttributeClause *C) {
2795}
2796void OMPClauseEnqueue::VisitOMPXBareClause(const OMPXBareClause *C) {}
2797
2798} // namespace
2799
2800void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
2801 unsigned size = WL.size();
2802 OMPClauseEnqueue Visitor(this);
2803 Visitor.Visit(S);
2804 if (size == WL.size())
2805 return;
2806 // Now reverse the entries we just added. This will match the DFS
2807 // ordering performed by the worklist.
2808 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2809 std::reverse(first: I, last: E);
2810}
2811
2812namespace {
2813class OpenACCClauseEnqueue : public OpenACCClauseVisitor<OpenACCClauseEnqueue> {
2814 EnqueueVisitor &Visitor;
2815
2816public:
2817 OpenACCClauseEnqueue(EnqueueVisitor &V) : Visitor(V) {}
2818
2819 void VisitVarList(const OpenACCClauseWithVarList &C) {
2820 for (Expr *Var : C.getVarList())
2821 Visitor.AddStmt(S: Var);
2822 }
2823
2824#define VISIT_CLAUSE(CLAUSE_NAME) \
2825 void Visit##CLAUSE_NAME##Clause(const OpenACC##CLAUSE_NAME##Clause &C);
2826#include "clang/Basic/OpenACCClauses.def"
2827};
2828
2829void OpenACCClauseEnqueue::VisitDefaultClause(const OpenACCDefaultClause &C) {}
2830void OpenACCClauseEnqueue::VisitIfClause(const OpenACCIfClause &C) {
2831 Visitor.AddStmt(S: C.getConditionExpr());
2832}
2833void OpenACCClauseEnqueue::VisitSelfClause(const OpenACCSelfClause &C) {
2834 if (C.isConditionExprClause()) {
2835 if (C.hasConditionExpr())
2836 Visitor.AddStmt(S: C.getConditionExpr());
2837 } else {
2838 for (Expr *Var : C.getVarList())
2839 Visitor.AddStmt(S: Var);
2840 }
2841}
2842void OpenACCClauseEnqueue::VisitNumWorkersClause(
2843 const OpenACCNumWorkersClause &C) {
2844 Visitor.AddStmt(S: C.getIntExpr());
2845}
2846void OpenACCClauseEnqueue::VisitDeviceNumClause(
2847 const OpenACCDeviceNumClause &C) {
2848 Visitor.AddStmt(S: C.getIntExpr());
2849}
2850void OpenACCClauseEnqueue::VisitDefaultAsyncClause(
2851 const OpenACCDefaultAsyncClause &C) {
2852 Visitor.AddStmt(S: C.getIntExpr());
2853}
2854void OpenACCClauseEnqueue::VisitVectorLengthClause(
2855 const OpenACCVectorLengthClause &C) {
2856 Visitor.AddStmt(S: C.getIntExpr());
2857}
2858void OpenACCClauseEnqueue::VisitNumGangsClause(const OpenACCNumGangsClause &C) {
2859 for (Expr *IE : C.getIntExprs())
2860 Visitor.AddStmt(S: IE);
2861}
2862
2863void OpenACCClauseEnqueue::VisitTileClause(const OpenACCTileClause &C) {
2864 for (Expr *IE : C.getSizeExprs())
2865 Visitor.AddStmt(S: IE);
2866}
2867
2868void OpenACCClauseEnqueue::VisitPrivateClause(const OpenACCPrivateClause &C) {
2869 VisitVarList(C);
2870 for (const OpenACCPrivateRecipe &R : C.getInitRecipes())
2871 Visitor.AddDecl(D: R.AllocaDecl);
2872}
2873
2874void OpenACCClauseEnqueue::VisitHostClause(const OpenACCHostClause &C) {
2875 VisitVarList(C);
2876}
2877
2878void OpenACCClauseEnqueue::VisitDeviceClause(const OpenACCDeviceClause &C) {
2879 VisitVarList(C);
2880}
2881
2882void OpenACCClauseEnqueue::VisitFirstPrivateClause(
2883 const OpenACCFirstPrivateClause &C) {
2884 VisitVarList(C);
2885 for (const OpenACCFirstPrivateRecipe &R : C.getInitRecipes()) {
2886 Visitor.AddDecl(D: R.AllocaDecl);
2887 Visitor.AddDecl(D: R.InitFromTemporary);
2888 }
2889}
2890
2891void OpenACCClauseEnqueue::VisitPresentClause(const OpenACCPresentClause &C) {
2892 VisitVarList(C);
2893}
2894void OpenACCClauseEnqueue::VisitNoCreateClause(const OpenACCNoCreateClause &C) {
2895 VisitVarList(C);
2896}
2897void OpenACCClauseEnqueue::VisitCopyClause(const OpenACCCopyClause &C) {
2898 VisitVarList(C);
2899}
2900void OpenACCClauseEnqueue::VisitLinkClause(const OpenACCLinkClause &C) {
2901 VisitVarList(C);
2902}
2903void OpenACCClauseEnqueue::VisitDeviceResidentClause(
2904 const OpenACCDeviceResidentClause &C) {
2905 VisitVarList(C);
2906}
2907void OpenACCClauseEnqueue::VisitCopyInClause(const OpenACCCopyInClause &C) {
2908 VisitVarList(C);
2909}
2910void OpenACCClauseEnqueue::VisitCopyOutClause(const OpenACCCopyOutClause &C) {
2911 VisitVarList(C);
2912}
2913void OpenACCClauseEnqueue::VisitCreateClause(const OpenACCCreateClause &C) {
2914 VisitVarList(C);
2915}
2916void OpenACCClauseEnqueue::VisitAttachClause(const OpenACCAttachClause &C) {
2917 VisitVarList(C);
2918}
2919
2920void OpenACCClauseEnqueue::VisitDetachClause(const OpenACCDetachClause &C) {
2921 VisitVarList(C);
2922}
2923void OpenACCClauseEnqueue::VisitDeleteClause(const OpenACCDeleteClause &C) {
2924 VisitVarList(C);
2925}
2926
2927void OpenACCClauseEnqueue::VisitUseDeviceClause(
2928 const OpenACCUseDeviceClause &C) {
2929 VisitVarList(C);
2930}
2931
2932void OpenACCClauseEnqueue::VisitDevicePtrClause(
2933 const OpenACCDevicePtrClause &C) {
2934 VisitVarList(C);
2935}
2936void OpenACCClauseEnqueue::VisitAsyncClause(const OpenACCAsyncClause &C) {
2937 if (C.hasIntExpr())
2938 Visitor.AddStmt(S: C.getIntExpr());
2939}
2940
2941void OpenACCClauseEnqueue::VisitWorkerClause(const OpenACCWorkerClause &C) {
2942 if (C.hasIntExpr())
2943 Visitor.AddStmt(S: C.getIntExpr());
2944}
2945
2946void OpenACCClauseEnqueue::VisitVectorClause(const OpenACCVectorClause &C) {
2947 if (C.hasIntExpr())
2948 Visitor.AddStmt(S: C.getIntExpr());
2949}
2950
2951void OpenACCClauseEnqueue::VisitWaitClause(const OpenACCWaitClause &C) {
2952 if (const Expr *DevNumExpr = C.getDevNumExpr())
2953 Visitor.AddStmt(S: DevNumExpr);
2954 for (Expr *QE : C.getQueueIdExprs())
2955 Visitor.AddStmt(S: QE);
2956}
2957void OpenACCClauseEnqueue::VisitDeviceTypeClause(
2958 const OpenACCDeviceTypeClause &C) {}
2959void OpenACCClauseEnqueue::VisitReductionClause(
2960 const OpenACCReductionClause &C) {
2961 VisitVarList(C);
2962 for (const OpenACCReductionRecipe &R : C.getRecipes())
2963 Visitor.AddDecl(D: R.AllocaDecl);
2964}
2965void OpenACCClauseEnqueue::VisitAutoClause(const OpenACCAutoClause &C) {}
2966void OpenACCClauseEnqueue::VisitIndependentClause(
2967 const OpenACCIndependentClause &C) {}
2968void OpenACCClauseEnqueue::VisitSeqClause(const OpenACCSeqClause &C) {}
2969void OpenACCClauseEnqueue::VisitNoHostClause(const OpenACCNoHostClause &C) {}
2970void OpenACCClauseEnqueue::VisitBindClause(const OpenACCBindClause &C) { }
2971void OpenACCClauseEnqueue::VisitFinalizeClause(const OpenACCFinalizeClause &C) {
2972}
2973void OpenACCClauseEnqueue::VisitIfPresentClause(
2974 const OpenACCIfPresentClause &C) {}
2975void OpenACCClauseEnqueue::VisitCollapseClause(const OpenACCCollapseClause &C) {
2976 Visitor.AddStmt(S: C.getLoopCount());
2977}
2978void OpenACCClauseEnqueue::VisitGangClause(const OpenACCGangClause &C) {
2979 for (unsigned I = 0; I < C.getNumExprs(); ++I) {
2980 Visitor.AddStmt(S: C.getExpr(I).second);
2981 }
2982}
2983} // namespace
2984
2985void EnqueueVisitor::EnqueueChildren(const OpenACCClause *C) {
2986 unsigned size = WL.size();
2987 OpenACCClauseEnqueue Visitor(*this);
2988 Visitor.Visit(C);
2989
2990 if (size == WL.size())
2991 return;
2992 // Now reverse the entries we just added. This will match the DFS
2993 // ordering performed by the worklist.
2994 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2995 std::reverse(first: I, last: E);
2996}
2997
2998void EnqueueVisitor::EnqueueChildren(const AnnotateAttr *A) {
2999 unsigned size = WL.size();
3000 for (const Expr *Arg : A->args()) {
3001 VisitStmt(S: Arg);
3002 }
3003 if (size == WL.size())
3004 return;
3005 // Now reverse the entries we just added. This will match the DFS
3006 // ordering performed by the worklist.
3007 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
3008 std::reverse(first: I, last: E);
3009}
3010
3011void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
3012 WL.push_back(Elt: LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
3013}
3014void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
3015 AddDecl(D: B->getBlockDecl());
3016}
3017void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
3018 EnqueueChildren(S: E);
3019 AddTypeLoc(TI: E->getTypeSourceInfo());
3020}
3021void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
3022 for (auto &I : llvm::reverse(C: S->body()))
3023 AddStmt(S: I);
3024}
3025void EnqueueVisitor::VisitMSDependentExistsStmt(
3026 const MSDependentExistsStmt *S) {
3027 AddStmt(S: S->getSubStmt());
3028 AddDeclarationNameInfo(S);
3029 if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
3030 AddNestedNameSpecifierLoc(Qualifier: QualifierLoc);
3031}
3032
3033void EnqueueVisitor::VisitCXXDependentScopeMemberExpr(
3034 const CXXDependentScopeMemberExpr *E) {
3035 if (E->hasExplicitTemplateArgs())
3036 AddExplicitTemplateArgs(A: E->getTemplateArgs(), NumTemplateArgs: E->getNumTemplateArgs());
3037 AddDeclarationNameInfo(S: E);
3038 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
3039 AddNestedNameSpecifierLoc(Qualifier: QualifierLoc);
3040 if (!E->isImplicitAccess())
3041 AddStmt(S: E->getBase());
3042}
3043void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
3044 // Enqueue the initializer , if any.
3045 AddStmt(S: E->getInitializer());
3046 // Enqueue the array size, if any.
3047 AddStmt(S: E->getArraySize().value_or(u: nullptr));
3048 // Enqueue the allocated type.
3049 AddTypeLoc(TI: E->getAllocatedTypeSourceInfo());
3050 // Enqueue the placement arguments.
3051 for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
3052 AddStmt(S: E->getPlacementArg(I: I - 1));
3053}
3054void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
3055 for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
3056 AddStmt(S: CE->getArg(Arg: I - 1));
3057 AddStmt(S: CE->getCallee());
3058 AddStmt(S: CE->getArg(Arg: 0));
3059}
3060void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
3061 const CXXPseudoDestructorExpr *E) {
3062 // Visit the name of the type being destroyed.
3063 AddTypeLoc(TI: E->getDestroyedTypeInfo());
3064 // Visit the scope type that looks disturbingly like the nested-name-specifier
3065 // but isn't.
3066 AddTypeLoc(TI: E->getScopeTypeInfo());
3067 // Visit the nested-name-specifier.
3068 if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
3069 AddNestedNameSpecifierLoc(Qualifier: QualifierLoc);
3070 // Visit base expression.
3071 AddStmt(S: E->getBase());
3072}
3073void EnqueueVisitor::VisitCXXScalarValueInitExpr(
3074 const CXXScalarValueInitExpr *E) {
3075 AddTypeLoc(TI: E->getTypeSourceInfo());
3076}
3077void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
3078 const CXXTemporaryObjectExpr *E) {
3079 EnqueueChildren(S: E);
3080 AddTypeLoc(TI: E->getTypeSourceInfo());
3081}
3082void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
3083 EnqueueChildren(S: E);
3084 if (E->isTypeOperand())
3085 AddTypeLoc(TI: E->getTypeOperandSourceInfo());
3086}
3087
3088void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
3089 const CXXUnresolvedConstructExpr *E) {
3090 EnqueueChildren(S: E);
3091 AddTypeLoc(TI: E->getTypeSourceInfo());
3092}
3093void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
3094 EnqueueChildren(S: E);
3095 if (E->isTypeOperand())
3096 AddTypeLoc(TI: E->getTypeOperandSourceInfo());
3097}
3098
3099void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
3100 EnqueueChildren(S);
3101 AddDecl(D: S->getExceptionDecl());
3102}
3103
3104void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
3105 AddStmt(S: S->getBody());
3106 AddStmt(S: S->getRangeInit());
3107 AddDecl(D: S->getLoopVariable());
3108}
3109
3110void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
3111 if (DR->hasExplicitTemplateArgs())
3112 AddExplicitTemplateArgs(A: DR->getTemplateArgs(), NumTemplateArgs: DR->getNumTemplateArgs());
3113 WL.push_back(Elt: DeclRefExprParts(DR, Parent));
3114}
3115void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
3116 const DependentScopeDeclRefExpr *E) {
3117 if (E->hasExplicitTemplateArgs())
3118 AddExplicitTemplateArgs(A: E->getTemplateArgs(), NumTemplateArgs: E->getNumTemplateArgs());
3119 AddDeclarationNameInfo(S: E);
3120 AddNestedNameSpecifierLoc(Qualifier: E->getQualifierLoc());
3121}
3122void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
3123 unsigned size = WL.size();
3124 bool isFirst = true;
3125 for (const auto *D : S->decls()) {
3126 AddDecl(D, isFirst);
3127 isFirst = false;
3128 }
3129 if (size == WL.size())
3130 return;
3131 // Now reverse the entries we just added. This will match the DFS
3132 // ordering performed by the worklist.
3133 VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
3134 std::reverse(first: I, last: E);
3135}
3136void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
3137 AddStmt(S: E->getInit());
3138 for (const DesignatedInitExpr::Designator &D :
3139 llvm::reverse(C: E->designators())) {
3140 if (D.isFieldDesignator()) {
3141 if (const FieldDecl *Field = D.getFieldDecl())
3142 AddMemberRef(D: Field, L: D.getFieldLoc());
3143 continue;
3144 }
3145 if (D.isArrayDesignator()) {
3146 AddStmt(S: E->getArrayIndex(D));
3147 continue;
3148 }
3149 assert(D.isArrayRangeDesignator() && "Unknown designator kind");
3150 AddStmt(S: E->getArrayRangeEnd(D));
3151 AddStmt(S: E->getArrayRangeStart(D));
3152 }
3153}
3154void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
3155 EnqueueChildren(S: E);
3156 AddTypeLoc(TI: E->getTypeInfoAsWritten());
3157}
3158void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
3159 AddStmt(S: FS->getBody());
3160 AddStmt(S: FS->getInc());
3161 AddStmt(S: FS->getCond());
3162 AddDecl(D: FS->getConditionVariable());
3163 AddStmt(S: FS->getInit());
3164}
3165void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
3166 WL.push_back(Elt: LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
3167}
3168void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
3169 AddStmt(S: If->getElse());
3170 AddStmt(S: If->getThen());
3171 AddStmt(S: If->getCond());
3172 AddStmt(S: If->getInit());
3173 AddDecl(D: If->getConditionVariable());
3174}
3175void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
3176 // We care about the syntactic form of the initializer list, only.
3177 if (InitListExpr *Syntactic = IE->getSyntacticForm())
3178 IE = Syntactic;
3179 EnqueueChildren(S: IE);
3180}
3181void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
3182 WL.push_back(Elt: MemberExprParts(M, Parent));
3183
3184 // If the base of the member access expression is an implicit 'this', don't
3185 // visit it.
3186 // FIXME: If we ever want to show these implicit accesses, this will be
3187 // unfortunate. However, clang_getCursor() relies on this behavior.
3188 if (M->isImplicitAccess())
3189 return;
3190
3191 // Ignore base anonymous struct/union fields, otherwise they will shadow the
3192 // real field that we are interested in.
3193 if (auto *SubME = dyn_cast<MemberExpr>(Val: M->getBase())) {
3194 if (auto *FD = dyn_cast_or_null<FieldDecl>(Val: SubME->getMemberDecl())) {
3195 if (FD->isAnonymousStructOrUnion()) {
3196 AddStmt(S: SubME->getBase());
3197 return;
3198 }
3199 }
3200 }
3201
3202 AddStmt(S: M->getBase());
3203}
3204void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
3205 AddTypeLoc(TI: E->getEncodedTypeSourceInfo());
3206}
3207void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
3208 EnqueueChildren(S: M);
3209 AddTypeLoc(TI: M->getClassReceiverTypeInfo());
3210}
3211void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
3212 // Visit the components of the offsetof expression.
3213 for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
3214 const OffsetOfNode &Node = E->getComponent(Idx: I - 1);
3215 switch (Node.getKind()) {
3216 case OffsetOfNode::Array:
3217 AddStmt(S: E->getIndexExpr(Idx: Node.getArrayExprIndex()));
3218 break;
3219 case OffsetOfNode::Field:
3220 AddMemberRef(D: Node.getField(), L: Node.getSourceRange().getEnd());
3221 break;
3222 case OffsetOfNode::Identifier:
3223 case OffsetOfNode::Base:
3224 continue;
3225 }
3226 }
3227 // Visit the type into which we're computing the offset.
3228 AddTypeLoc(TI: E->getTypeSourceInfo());
3229}
3230void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
3231 if (E->hasExplicitTemplateArgs())
3232 AddExplicitTemplateArgs(A: E->getTemplateArgs(), NumTemplateArgs: E->getNumTemplateArgs());
3233 WL.push_back(Elt: OverloadExprParts(E, Parent));
3234}
3235void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
3236 const UnaryExprOrTypeTraitExpr *E) {
3237 EnqueueChildren(S: E);
3238 if (E->isArgumentType())
3239 AddTypeLoc(TI: E->getArgumentTypeInfo());
3240}
3241void EnqueueVisitor::VisitStmt(const Stmt *S) { EnqueueChildren(S); }
3242void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
3243 AddStmt(S: S->getBody());
3244 AddStmt(S: S->getCond());
3245 AddStmt(S: S->getInit());
3246 AddDecl(D: S->getConditionVariable());
3247}
3248
3249void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
3250 AddStmt(S: W->getBody());
3251 AddStmt(S: W->getCond());
3252 AddDecl(D: W->getConditionVariable());
3253}
3254
3255void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
3256 for (unsigned I = E->getNumArgs(); I > 0; --I)
3257 AddTypeLoc(TI: E->getArg(I: I - 1));
3258}
3259
3260void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
3261 AddTypeLoc(TI: E->getQueriedTypeSourceInfo());
3262}
3263
3264void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
3265 EnqueueChildren(S: E);
3266}
3267
3268void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
3269 VisitOverloadExpr(E: U);
3270 if (!U->isImplicitAccess())
3271 AddStmt(S: U->getBase());
3272}
3273void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
3274 AddStmt(S: E->getSubExpr());
3275 AddTypeLoc(TI: E->getWrittenTypeInfo());
3276}
3277void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
3278 WL.push_back(Elt: SizeOfPackExprParts(E, Parent));
3279}
3280void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
3281 // If the opaque value has a source expression, just transparently
3282 // visit that. This is useful for (e.g.) pseudo-object expressions.
3283 if (Expr *SourceExpr = E->getSourceExpr())
3284 return ConstStmtVisitor::Visit(S: SourceExpr);
3285}
3286void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
3287 AddStmt(S: E->getBody());
3288 WL.push_back(Elt: LambdaExprParts(E, Parent));
3289}
3290void EnqueueVisitor::VisitConceptSpecializationExpr(
3291 const ConceptSpecializationExpr *E) {
3292 WL.push_back(Elt: ConceptSpecializationExprVisit(E, Parent));
3293}
3294void EnqueueVisitor::VisitRequiresExpr(const RequiresExpr *E) {
3295 WL.push_back(Elt: RequiresExprVisit(E, Parent));
3296 for (ParmVarDecl *VD : E->getLocalParameters())
3297 AddDecl(D: VD);
3298}
3299void EnqueueVisitor::VisitCXXParenListInitExpr(const CXXParenListInitExpr *E) {
3300 EnqueueChildren(S: E);
3301}
3302void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
3303 // Treat the expression like its syntactic form.
3304 ConstStmtVisitor::Visit(S: E->getSyntacticForm());
3305}
3306
3307void EnqueueVisitor::VisitOMPExecutableDirective(
3308 const OMPExecutableDirective *D) {
3309 EnqueueChildren(S: D);
3310 for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
3311 E = D->clauses().end();
3312 I != E; ++I)
3313 EnqueueChildren(S: *I);
3314}
3315
3316void EnqueueVisitor::VisitOMPLoopBasedDirective(
3317 const OMPLoopBasedDirective *D) {
3318 VisitOMPExecutableDirective(D);
3319}
3320
3321void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) {
3322 VisitOMPLoopBasedDirective(D);
3323}
3324
3325void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
3326 VisitOMPExecutableDirective(D);
3327}
3328
3329void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) {
3330 VisitOMPLoopDirective(D);
3331}
3332
3333void EnqueueVisitor::VisitOMPCanonicalLoopNestTransformationDirective(
3334 const OMPCanonicalLoopNestTransformationDirective *D) {
3335 VisitOMPLoopBasedDirective(D);
3336}
3337
3338void EnqueueVisitor::VisitOMPTileDirective(const OMPTileDirective *D) {
3339 VisitOMPCanonicalLoopNestTransformationDirective(D);
3340}
3341
3342void EnqueueVisitor::VisitOMPStripeDirective(const OMPStripeDirective *D) {
3343 VisitOMPCanonicalLoopNestTransformationDirective(D);
3344}
3345
3346void EnqueueVisitor::VisitOMPUnrollDirective(const OMPUnrollDirective *D) {
3347 VisitOMPCanonicalLoopNestTransformationDirective(D);
3348}
3349
3350void EnqueueVisitor::VisitOMPReverseDirective(const OMPReverseDirective *D) {
3351 VisitOMPCanonicalLoopNestTransformationDirective(D);
3352}
3353
3354void EnqueueVisitor::VisitOMPInterchangeDirective(
3355 const OMPInterchangeDirective *D) {
3356 VisitOMPCanonicalLoopNestTransformationDirective(D);
3357}
3358
3359void EnqueueVisitor::VisitOMPCanonicalLoopSequenceTransformationDirective(
3360 const OMPCanonicalLoopSequenceTransformationDirective *D) {
3361 VisitOMPExecutableDirective(D);
3362}
3363
3364void EnqueueVisitor::VisitOMPFuseDirective(const OMPFuseDirective *D) {
3365 VisitOMPCanonicalLoopSequenceTransformationDirective(D);
3366}
3367
3368void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) {
3369 VisitOMPLoopDirective(D);
3370}
3371
3372void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) {
3373 VisitOMPLoopDirective(D);
3374}
3375
3376void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
3377 VisitOMPExecutableDirective(D);
3378}
3379
3380void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
3381 VisitOMPExecutableDirective(D);
3382}
3383
3384void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) {
3385 VisitOMPExecutableDirective(D);
3386}
3387
3388void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) {
3389 VisitOMPExecutableDirective(D);
3390}
3391
3392void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) {
3393 VisitOMPExecutableDirective(D);
3394 AddDeclarationNameInfo(S: D);
3395}
3396
3397void EnqueueVisitor::VisitOMPParallelForDirective(
3398 const OMPParallelForDirective *D) {
3399 VisitOMPLoopDirective(D);
3400}
3401
3402void EnqueueVisitor::VisitOMPParallelForSimdDirective(
3403 const OMPParallelForSimdDirective *D) {
3404 VisitOMPLoopDirective(D);
3405}
3406
3407void EnqueueVisitor::VisitOMPParallelMasterDirective(
3408 const OMPParallelMasterDirective *D) {
3409 VisitOMPExecutableDirective(D);
3410}
3411
3412void EnqueueVisitor::VisitOMPParallelMaskedDirective(
3413 const OMPParallelMaskedDirective *D) {
3414 VisitOMPExecutableDirective(D);
3415}
3416
3417void EnqueueVisitor::VisitOMPParallelSectionsDirective(
3418 const OMPParallelSectionsDirective *D) {
3419 VisitOMPExecutableDirective(D);
3420}
3421
3422void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) {
3423 VisitOMPExecutableDirective(D);
3424}
3425
3426void EnqueueVisitor::VisitOMPTaskyieldDirective(
3427 const OMPTaskyieldDirective *D) {
3428 VisitOMPExecutableDirective(D);
3429}
3430
3431void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) {
3432 VisitOMPExecutableDirective(D);
3433}
3434
3435void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) {
3436 VisitOMPExecutableDirective(D);
3437}
3438
3439void EnqueueVisitor::VisitOMPAssumeDirective(const OMPAssumeDirective *D) {
3440 VisitOMPExecutableDirective(D);
3441}
3442
3443void EnqueueVisitor::VisitOMPErrorDirective(const OMPErrorDirective *D) {
3444 VisitOMPExecutableDirective(D);
3445}
3446
3447void EnqueueVisitor::VisitOMPTaskgroupDirective(
3448 const OMPTaskgroupDirective *D) {
3449 VisitOMPExecutableDirective(D);
3450 if (const Expr *E = D->getReductionRef())
3451 VisitStmt(S: E);
3452}
3453
3454void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) {
3455 VisitOMPExecutableDirective(D);
3456}
3457
3458void EnqueueVisitor::VisitOMPDepobjDirective(const OMPDepobjDirective *D) {
3459 VisitOMPExecutableDirective(D);
3460}
3461
3462void EnqueueVisitor::VisitOMPScanDirective(const OMPScanDirective *D) {
3463 VisitOMPExecutableDirective(D);
3464}
3465
3466void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) {
3467 VisitOMPExecutableDirective(D);
3468}
3469
3470void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) {
3471 VisitOMPExecutableDirective(D);
3472}
3473
3474void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) {
3475 VisitOMPExecutableDirective(D);
3476}
3477
3478void EnqueueVisitor::VisitOMPTargetDataDirective(
3479 const OMPTargetDataDirective *D) {
3480 VisitOMPExecutableDirective(D);
3481}
3482
3483void EnqueueVisitor::VisitOMPTargetEnterDataDirective(
3484 const OMPTargetEnterDataDirective *D) {
3485 VisitOMPExecutableDirective(D);
3486}
3487
3488void EnqueueVisitor::VisitOMPTargetExitDataDirective(
3489 const OMPTargetExitDataDirective *D) {
3490 VisitOMPExecutableDirective(D);
3491}
3492
3493void EnqueueVisitor::VisitOMPTargetParallelDirective(
3494 const OMPTargetParallelDirective *D) {
3495 VisitOMPExecutableDirective(D);
3496}
3497
3498void EnqueueVisitor::VisitOMPTargetParallelForDirective(
3499 const OMPTargetParallelForDirective *D) {
3500 VisitOMPLoopDirective(D);
3501}
3502
3503void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) {
3504 VisitOMPExecutableDirective(D);
3505}
3506
3507void EnqueueVisitor::VisitOMPCancellationPointDirective(
3508 const OMPCancellationPointDirective *D) {
3509 VisitOMPExecutableDirective(D);
3510}
3511
3512void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) {
3513 VisitOMPExecutableDirective(D);
3514}
3515
3516void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) {
3517 VisitOMPLoopDirective(D);
3518}
3519
3520void EnqueueVisitor::VisitOMPTaskLoopSimdDirective(
3521 const OMPTaskLoopSimdDirective *D) {
3522 VisitOMPLoopDirective(D);
3523}
3524
3525void EnqueueVisitor::VisitOMPMasterTaskLoopDirective(
3526 const OMPMasterTaskLoopDirective *D) {
3527 VisitOMPLoopDirective(D);
3528}
3529
3530void EnqueueVisitor::VisitOMPMaskedTaskLoopDirective(
3531 const OMPMaskedTaskLoopDirective *D) {
3532 VisitOMPLoopDirective(D);
3533}
3534
3535void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective(
3536 const OMPMasterTaskLoopSimdDirective *D) {
3537 VisitOMPLoopDirective(D);
3538}
3539
3540void EnqueueVisitor::VisitOMPMaskedTaskLoopSimdDirective(
3541 const OMPMaskedTaskLoopSimdDirective *D) {
3542 VisitOMPLoopDirective(D);
3543}
3544
3545void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective(
3546 const OMPParallelMasterTaskLoopDirective *D) {
3547 VisitOMPLoopDirective(D);
3548}
3549
3550void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopDirective(
3551 const OMPParallelMaskedTaskLoopDirective *D) {
3552 VisitOMPLoopDirective(D);
3553}
3554
3555void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective(
3556 const OMPParallelMasterTaskLoopSimdDirective *D) {
3557 VisitOMPLoopDirective(D);
3558}
3559
3560void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopSimdDirective(
3561 const OMPParallelMaskedTaskLoopSimdDirective *D) {
3562 VisitOMPLoopDirective(D);
3563}
3564
3565void EnqueueVisitor::VisitOMPDistributeDirective(
3566 const OMPDistributeDirective *D) {
3567 VisitOMPLoopDirective(D);
3568}
3569
3570void EnqueueVisitor::VisitOMPDistributeParallelForDirective(
3571 const OMPDistributeParallelForDirective *D) {
3572 VisitOMPLoopDirective(D);
3573}
3574
3575void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective(
3576 const OMPDistributeParallelForSimdDirective *D) {
3577 VisitOMPLoopDirective(D);
3578}
3579
3580void EnqueueVisitor::VisitOMPDistributeSimdDirective(
3581 const OMPDistributeSimdDirective *D) {
3582 VisitOMPLoopDirective(D);
3583}
3584
3585void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective(
3586 const OMPTargetParallelForSimdDirective *D) {
3587 VisitOMPLoopDirective(D);
3588}
3589
3590void EnqueueVisitor::VisitOMPTargetSimdDirective(
3591 const OMPTargetSimdDirective *D) {
3592 VisitOMPLoopDirective(D);
3593}
3594
3595void EnqueueVisitor::VisitOMPTeamsDistributeDirective(
3596 const OMPTeamsDistributeDirective *D) {
3597 VisitOMPLoopDirective(D);
3598}
3599
3600void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective(
3601 const OMPTeamsDistributeSimdDirective *D) {
3602 VisitOMPLoopDirective(D);
3603}
3604
3605void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective(
3606 const OMPTeamsDistributeParallelForSimdDirective *D) {
3607 VisitOMPLoopDirective(D);
3608}
3609
3610void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective(
3611 const OMPTeamsDistributeParallelForDirective *D) {
3612 VisitOMPLoopDirective(D);
3613}
3614
3615void EnqueueVisitor::VisitOMPTargetTeamsDirective(
3616 const OMPTargetTeamsDirective *D) {
3617 VisitOMPExecutableDirective(D);
3618}
3619
3620void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective(
3621 const OMPTargetTeamsDistributeDirective *D) {
3622 VisitOMPLoopDirective(D);
3623}
3624
3625void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective(
3626 const OMPTargetTeamsDistributeParallelForDirective *D) {
3627 VisitOMPLoopDirective(D);
3628}
3629
3630void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
3631 const OMPTargetTeamsDistributeParallelForSimdDirective *D) {
3632 VisitOMPLoopDirective(D);
3633}
3634
3635void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective(
3636 const OMPTargetTeamsDistributeSimdDirective *D) {
3637 VisitOMPLoopDirective(D);
3638}
3639
3640void EnqueueVisitor::VisitOpenACCComputeConstruct(
3641 const OpenACCComputeConstruct *C) {
3642 EnqueueChildren(S: C);
3643 for (auto *Clause : C->clauses())
3644 EnqueueChildren(C: Clause);
3645}
3646
3647void EnqueueVisitor::VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *C) {
3648 EnqueueChildren(S: C);
3649 for (auto *Clause : C->clauses())
3650 EnqueueChildren(C: Clause);
3651}
3652
3653void EnqueueVisitor::VisitOpenACCCombinedConstruct(
3654 const OpenACCCombinedConstruct *C) {
3655 EnqueueChildren(S: C);
3656 for (auto *Clause : C->clauses())
3657 EnqueueChildren(C: Clause);
3658}
3659void EnqueueVisitor::VisitOpenACCDataConstruct(const OpenACCDataConstruct *C) {
3660 EnqueueChildren(S: C);
3661 for (auto *Clause : C->clauses())
3662 EnqueueChildren(C: Clause);
3663}
3664void EnqueueVisitor::VisitOpenACCEnterDataConstruct(
3665 const OpenACCEnterDataConstruct *C) {
3666 EnqueueChildren(S: C);
3667 for (auto *Clause : C->clauses())
3668 EnqueueChildren(C: Clause);
3669}
3670void EnqueueVisitor::VisitOpenACCExitDataConstruct(
3671 const OpenACCExitDataConstruct *C) {
3672 EnqueueChildren(S: C);
3673 for (auto *Clause : C->clauses())
3674 EnqueueChildren(C: Clause);
3675}
3676void EnqueueVisitor::VisitOpenACCHostDataConstruct(
3677 const OpenACCHostDataConstruct *C) {
3678 EnqueueChildren(S: C);
3679 for (auto *Clause : C->clauses())
3680 EnqueueChildren(C: Clause);
3681}
3682
3683void EnqueueVisitor::VisitOpenACCWaitConstruct(const OpenACCWaitConstruct *C) {
3684 EnqueueChildren(S: C);
3685 for (auto *Clause : C->clauses())
3686 EnqueueChildren(C: Clause);
3687}
3688
3689void EnqueueVisitor::VisitOpenACCCacheConstruct(
3690 const OpenACCCacheConstruct *C) {
3691 EnqueueChildren(S: C);
3692}
3693
3694void EnqueueVisitor::VisitOpenACCInitConstruct(const OpenACCInitConstruct *C) {
3695 EnqueueChildren(S: C);
3696 for (auto *Clause : C->clauses())
3697 EnqueueChildren(C: Clause);
3698}
3699
3700void EnqueueVisitor::VisitOpenACCShutdownConstruct(
3701 const OpenACCShutdownConstruct *C) {
3702 EnqueueChildren(S: C);
3703 for (auto *Clause : C->clauses())
3704 EnqueueChildren(C: Clause);
3705}
3706
3707void EnqueueVisitor::VisitOpenACCSetConstruct(const OpenACCSetConstruct *C) {
3708 EnqueueChildren(S: C);
3709 for (auto *Clause : C->clauses())
3710 EnqueueChildren(C: Clause);
3711}
3712
3713void EnqueueVisitor::VisitOpenACCUpdateConstruct(
3714 const OpenACCUpdateConstruct *C) {
3715 EnqueueChildren(S: C);
3716 for (auto *Clause : C->clauses())
3717 EnqueueChildren(C: Clause);
3718}
3719
3720void EnqueueVisitor::VisitOpenACCAtomicConstruct(
3721 const OpenACCAtomicConstruct *C) {
3722 EnqueueChildren(S: C);
3723 for (auto *Clause : C->clauses())
3724 EnqueueChildren(C: Clause);
3725}
3726
3727void EnqueueVisitor::VisitAnnotateAttr(const AnnotateAttr *A) {
3728 EnqueueChildren(A);
3729}
3730
3731void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
3732 EnqueueVisitor(WL, MakeCXCursor(S, Parent: StmtParent, TU, RegionOfInterest))
3733 .ConstStmtVisitor::Visit(S);
3734}
3735
3736void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Attr *A) {
3737 // Parent is the attribute itself when this is indirectly called from
3738 // VisitChildren. Because we need to make a CXCursor for A, we need *its*
3739 // parent.
3740 auto AttrCursor = Parent;
3741
3742 // Get the attribute's parent as stored in
3743 // cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent, CXTranslationUnit
3744 // TU)
3745 const Decl *AttrParent = static_cast<const Decl *>(AttrCursor.data[1]);
3746
3747 EnqueueVisitor(WL, MakeCXCursor(A, Parent: AttrParent, TU))
3748 .ConstAttrVisitor::Visit(A);
3749}
3750
3751bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
3752 if (RegionOfInterest.isValid()) {
3753 SourceRange Range = getRawCursorExtent(C);
3754 if (Range.isInvalid() || CompareRegionOfInterest(R: Range))
3755 return false;
3756 }
3757 return true;
3758}
3759
3760bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
3761 while (!WL.empty()) {
3762 // Dequeue the worklist item.
3763 VisitorJob LI = WL.pop_back_val();
3764
3765 // Set the Parent field, then back to its old value once we're done.
3766 SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
3767
3768 switch (LI.getKind()) {
3769 case VisitorJob::DeclVisitKind: {
3770 const Decl *D = cast<DeclVisit>(Val: &LI)->get();
3771 if (!D)
3772 continue;
3773
3774 // For now, perform default visitation for Decls.
3775 if (Visit(Cursor: MakeCXCursor(D, TU, RegionOfInterest,
3776 FirstInDeclGroup: cast<DeclVisit>(Val: &LI)->isFirst())))
3777 return true;
3778
3779 continue;
3780 }
3781 case VisitorJob::ExplicitTemplateArgsVisitKind: {
3782 for (const TemplateArgumentLoc &Arg :
3783 *cast<ExplicitTemplateArgsVisit>(Val: &LI)) {
3784 if (VisitTemplateArgumentLoc(TAL: Arg))
3785 return true;
3786 }
3787 continue;
3788 }
3789 case VisitorJob::TypeLocVisitKind: {
3790 // Perform default visitation for TypeLocs.
3791 if (Visit(TyLoc: cast<TypeLocVisit>(Val: &LI)->get()))
3792 return true;
3793 continue;
3794 }
3795 case VisitorJob::LabelRefVisitKind: {
3796 const LabelDecl *LS = cast<LabelRefVisit>(Val: &LI)->get();
3797 if (LabelStmt *stmt = LS->getStmt()) {
3798 if (Visit(Cursor: MakeCursorLabelRef(Label: stmt, Loc: cast<LabelRefVisit>(Val: &LI)->getLoc(),
3799 TU))) {
3800 return true;
3801 }
3802 }
3803 continue;
3804 }
3805
3806 case VisitorJob::NestedNameSpecifierLocVisitKind: {
3807 NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(Val: &LI);
3808 if (VisitNestedNameSpecifierLoc(Qualifier: V->get()))
3809 return true;
3810 continue;
3811 }
3812
3813 case VisitorJob::DeclarationNameInfoVisitKind: {
3814 if (VisitDeclarationNameInfo(Name: cast<DeclarationNameInfoVisit>(Val: &LI)->get()))
3815 return true;
3816 continue;
3817 }
3818 case VisitorJob::MemberRefVisitKind: {
3819 MemberRefVisit *V = cast<MemberRefVisit>(Val: &LI);
3820 if (Visit(Cursor: MakeCursorMemberRef(Field: V->get(), Loc: V->getLoc(), TU)))
3821 return true;
3822 continue;
3823 }
3824 case VisitorJob::StmtVisitKind: {
3825 const Stmt *S = cast<StmtVisit>(Val: &LI)->get();
3826 if (!S)
3827 continue;
3828
3829 // Update the current cursor.
3830 CXCursor Cursor = MakeCXCursor(S, Parent: StmtParent, TU, RegionOfInterest);
3831 if (!IsInRegionOfInterest(C: Cursor))
3832 continue;
3833 switch (Visitor(Cursor, Parent, ClientData)) {
3834 case CXChildVisit_Break:
3835 return true;
3836 case CXChildVisit_Continue:
3837 break;
3838 case CXChildVisit_Recurse:
3839 if (PostChildrenVisitor)
3840 WL.push_back(Elt: PostChildrenVisit(nullptr, Cursor));
3841 EnqueueWorkList(WL, S);
3842 break;
3843 }
3844 continue;
3845 }
3846 case VisitorJob::MemberExprPartsKind: {
3847 // Handle the other pieces in the MemberExpr besides the base.
3848 const MemberExpr *M = cast<MemberExprParts>(Val: &LI)->get();
3849
3850 // Visit the nested-name-specifier
3851 if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
3852 if (VisitNestedNameSpecifierLoc(Qualifier: QualifierLoc))
3853 return true;
3854
3855 // Visit the declaration name.
3856 if (VisitDeclarationNameInfo(Name: M->getMemberNameInfo()))
3857 return true;
3858
3859 // Visit the explicitly-specified template arguments, if any.
3860 if (M->hasExplicitTemplateArgs()) {
3861 for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
3862 *ArgEnd = Arg + M->getNumTemplateArgs();
3863 Arg != ArgEnd; ++Arg) {
3864 if (VisitTemplateArgumentLoc(TAL: *Arg))
3865 return true;
3866 }
3867 }
3868 continue;
3869 }
3870 case VisitorJob::DeclRefExprPartsKind: {
3871 const DeclRefExpr *DR = cast<DeclRefExprParts>(Val: &LI)->get();
3872 // Visit nested-name-specifier, if present.
3873 if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
3874 if (VisitNestedNameSpecifierLoc(Qualifier: QualifierLoc))
3875 return true;
3876 // Visit declaration name.
3877 if (VisitDeclarationNameInfo(Name: DR->getNameInfo()))
3878 return true;
3879 continue;
3880 }
3881 case VisitorJob::OverloadExprPartsKind: {
3882 const OverloadExpr *O = cast<OverloadExprParts>(Val: &LI)->get();
3883 // Visit the nested-name-specifier.
3884 if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
3885 if (VisitNestedNameSpecifierLoc(Qualifier: QualifierLoc))
3886 return true;
3887 // Visit the declaration name.
3888 if (VisitDeclarationNameInfo(Name: O->getNameInfo()))
3889 return true;
3890 // Visit the overloaded declaration reference.
3891 if (Visit(Cursor: MakeCursorOverloadedDeclRef(E: O, TU)))
3892 return true;
3893 continue;
3894 }
3895 case VisitorJob::SizeOfPackExprPartsKind: {
3896 const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(Val: &LI)->get();
3897 NamedDecl *Pack = E->getPack();
3898 if (isa<TemplateTypeParmDecl>(Val: Pack)) {
3899 if (Visit(Cursor: MakeCursorTypeRef(Type: cast<TemplateTypeParmDecl>(Val: Pack),
3900 Loc: E->getPackLoc(), TU)))
3901 return true;
3902
3903 continue;
3904 }
3905
3906 if (isa<TemplateTemplateParmDecl>(Val: Pack)) {
3907 if (Visit(Cursor: MakeCursorTemplateRef(Template: cast<TemplateTemplateParmDecl>(Val: Pack),
3908 Loc: E->getPackLoc(), TU)))
3909 return true;
3910
3911 continue;
3912 }
3913
3914 // Non-type template parameter packs and function parameter packs are
3915 // treated like DeclRefExpr cursors.
3916 continue;
3917 }
3918
3919 case VisitorJob::LambdaExprPartsKind: {
3920 // Visit non-init captures.
3921 const LambdaExpr *E = cast<LambdaExprParts>(Val: &LI)->get();
3922 for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
3923 CEnd = E->explicit_capture_end();
3924 C != CEnd; ++C) {
3925 if (!C->capturesVariable())
3926 continue;
3927 // TODO: handle structured bindings here ?
3928 if (!isa<VarDecl>(Val: C->getCapturedVar()))
3929 continue;
3930 if (Visit(Cursor: MakeCursorVariableRef(Var: cast<VarDecl>(Val: C->getCapturedVar()),
3931 Loc: C->getLocation(), TU)))
3932 return true;
3933 }
3934 // Visit init captures
3935 for (auto InitExpr : E->capture_inits()) {
3936 if (InitExpr && Visit(S: InitExpr))
3937 return true;
3938 }
3939
3940 TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
3941 // Visit parameters and return type, if present.
3942 if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
3943 if (E->hasExplicitParameters()) {
3944 // Visit parameters.
3945 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I)
3946 if (Visit(Cursor: MakeCXCursor(D: Proto.getParam(i: I), TU)))
3947 return true;
3948 }
3949 if (E->hasExplicitResultType()) {
3950 // Visit result type.
3951 if (Visit(TyLoc: Proto.getReturnLoc()))
3952 return true;
3953 }
3954 }
3955 break;
3956 }
3957
3958 case VisitorJob::ConceptSpecializationExprVisitKind: {
3959 const ConceptSpecializationExpr *E =
3960 cast<ConceptSpecializationExprVisit>(Val: &LI)->get();
3961 if (NestedNameSpecifierLoc QualifierLoc =
3962 E->getNestedNameSpecifierLoc()) {
3963 if (VisitNestedNameSpecifierLoc(Qualifier: QualifierLoc))
3964 return true;
3965 }
3966
3967 if (E->getNamedConcept() &&
3968 Visit(Cursor: MakeCursorTemplateRef(Template: E->getNamedConcept(),
3969 Loc: E->getConceptNameLoc(), TU)))
3970 return true;
3971
3972 if (auto Args = E->getTemplateArgsAsWritten()) {
3973 for (const auto &Arg : Args->arguments()) {
3974 if (VisitTemplateArgumentLoc(TAL: Arg))
3975 return true;
3976 }
3977 }
3978 break;
3979 }
3980
3981 case VisitorJob::RequiresExprVisitKind: {
3982 const RequiresExpr *E = cast<RequiresExprVisit>(Val: &LI)->get();
3983 for (const concepts::Requirement *R : E->getRequirements())
3984 VisitConceptRequirement(R: *R);
3985 break;
3986 }
3987
3988 case VisitorJob::PostChildrenVisitKind:
3989 if (PostChildrenVisitor(Parent, ClientData))
3990 return true;
3991 break;
3992 }
3993 }
3994 return false;
3995}
3996
3997bool CursorVisitor::Visit(const Stmt *S) {
3998 VisitorWorkList *WL = nullptr;
3999 if (!WorkListFreeList.empty()) {
4000 WL = WorkListFreeList.back();
4001 WL->clear();
4002 WorkListFreeList.pop_back();
4003 } else {
4004 WL = new VisitorWorkList();
4005 WorkListCache.push_back(Elt: WL);
4006 }
4007 EnqueueWorkList(WL&: *WL, S);
4008 bool result = RunVisitorWorkList(WL&: *WL);
4009 WorkListFreeList.push_back(Elt: WL);
4010 return result;
4011}
4012
4013bool CursorVisitor::Visit(const Attr *A) {
4014 VisitorWorkList *WL = nullptr;
4015 if (!WorkListFreeList.empty()) {
4016 WL = WorkListFreeList.back();
4017 WL->clear();
4018 WorkListFreeList.pop_back();
4019 } else {
4020 WL = new VisitorWorkList();
4021 WorkListCache.push_back(Elt: WL);
4022 }
4023 EnqueueWorkList(WL&: *WL, A);
4024 bool result = RunVisitorWorkList(WL&: *WL);
4025 WorkListFreeList.push_back(Elt: WL);
4026 return result;
4027}
4028
4029namespace {
4030typedef SmallVector<SourceRange, 4> RefNamePieces;
4031RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
4032 const DeclarationNameInfo &NI, SourceRange QLoc,
4033 const SourceRange *TemplateArgsLoc = nullptr) {
4034 const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
4035 const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
4036 const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
4037
4038 const DeclarationName::NameKind Kind = NI.getName().getNameKind();
4039
4040 RefNamePieces Pieces;
4041
4042 if (WantQualifier && QLoc.isValid())
4043 Pieces.push_back(Elt: QLoc);
4044
4045 if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
4046 Pieces.push_back(Elt: NI.getLoc());
4047
4048 if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid())
4049 Pieces.push_back(Elt: *TemplateArgsLoc);
4050
4051 if (Kind == DeclarationName::CXXOperatorName) {
4052 Pieces.push_back(Elt: NI.getInfo().getCXXOperatorNameBeginLoc());
4053 Pieces.push_back(Elt: NI.getInfo().getCXXOperatorNameEndLoc());
4054 }
4055
4056 if (WantSinglePiece) {
4057 SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
4058 Pieces.clear();
4059 Pieces.push_back(Elt: R);
4060 }
4061
4062 return Pieces;
4063}
4064} // namespace
4065
4066//===----------------------------------------------------------------------===//
4067// Misc. API hooks.
4068//===----------------------------------------------------------------------===//
4069
4070namespace {
4071struct RegisterFatalErrorHandler {
4072 RegisterFatalErrorHandler() {
4073 clang_install_aborting_llvm_fatal_error_handler();
4074 }
4075};
4076} // namespace
4077
4078static llvm::ManagedStatic<RegisterFatalErrorHandler>
4079 RegisterFatalErrorHandlerOnce;
4080
4081static CIndexer *clang_createIndex_Impl(
4082 int excludeDeclarationsFromPCH, int displayDiagnostics,
4083 unsigned char threadBackgroundPriorityForIndexing = CXChoice_Default,
4084 unsigned char threadBackgroundPriorityForEditing = CXChoice_Default) {
4085 // We use crash recovery to make some of our APIs more reliable, implicitly
4086 // enable it.
4087 if (!getenv(name: "LIBCLANG_DISABLE_CRASH_RECOVERY"))
4088 llvm::CrashRecoveryContext::Enable();
4089
4090 // Look through the managed static to trigger construction of the managed
4091 // static which registers our fatal error handler. This ensures it is only
4092 // registered once.
4093 (void)*RegisterFatalErrorHandlerOnce;
4094
4095 // Initialize targets for clang module support.
4096 llvm::InitializeAllTargets();
4097 llvm::InitializeAllTargetMCs();
4098 llvm::InitializeAllAsmPrinters();
4099 llvm::InitializeAllAsmParsers();
4100
4101 CIndexer *CIdxr = new CIndexer();
4102
4103 if (excludeDeclarationsFromPCH)
4104 CIdxr->setOnlyLocalDecls();
4105 if (displayDiagnostics)
4106 CIdxr->setDisplayDiagnostics();
4107
4108 unsigned GlobalOptions = CIdxr->getCXGlobalOptFlags();
4109 const auto updateGlobalOption =
4110 [&GlobalOptions](unsigned char Policy, CXGlobalOptFlags Flag,
4111 const char *EnvironmentVariableName) {
4112 switch (Policy) {
4113 case CXChoice_Enabled:
4114 GlobalOptions |= Flag;
4115 break;
4116 case CXChoice_Disabled:
4117 GlobalOptions &= ~Flag;
4118 break;
4119 case CXChoice_Default:
4120 default: // Fall back to default behavior if Policy is unsupported.
4121 if (getenv(name: EnvironmentVariableName))
4122 GlobalOptions |= Flag;
4123 }
4124 };
4125 updateGlobalOption(threadBackgroundPriorityForIndexing,
4126 CXGlobalOpt_ThreadBackgroundPriorityForIndexing,
4127 "LIBCLANG_BGPRIO_INDEX");
4128 updateGlobalOption(threadBackgroundPriorityForEditing,
4129 CXGlobalOpt_ThreadBackgroundPriorityForEditing,
4130 "LIBCLANG_BGPRIO_EDIT");
4131 CIdxr->setCXGlobalOptFlags(GlobalOptions);
4132
4133 return CIdxr;
4134}
4135
4136CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
4137 int displayDiagnostics) {
4138 return clang_createIndex_Impl(excludeDeclarationsFromPCH, displayDiagnostics);
4139}
4140
4141void clang_disposeIndex(CXIndex CIdx) {
4142 if (CIdx)
4143 delete static_cast<CIndexer *>(CIdx);
4144}
4145
4146CXIndex clang_createIndexWithOptions(const CXIndexOptions *options) {
4147 // Adding new options to struct CXIndexOptions:
4148 // 1. If no other new option has been added in the same libclang version,
4149 // sizeof(CXIndexOptions) must increase for versioning purposes.
4150 // 2. Options should be added at the end of the struct in order to seamlessly
4151 // support older struct versions. If options->Size < sizeof(CXIndexOptions),
4152 // don't attempt to read the missing options and rely on the default values of
4153 // recently added options being reasonable. For example:
4154 // if (options->Size >= offsetof(CXIndexOptions, RecentlyAddedMember))
4155 // do_something(options->RecentlyAddedMember);
4156
4157 // An exception: if a new option is small enough, it can be squeezed into the
4158 // /*Reserved*/ bits in CXIndexOptions. Since the default value of each option
4159 // is guaranteed to be 0 and the callers are advised to zero out the struct,
4160 // programs built against older libclang versions would implicitly set the new
4161 // options to default values, which should keep the behavior of previous
4162 // libclang versions and thus be backward-compatible.
4163
4164 // If options->Size > sizeof(CXIndexOptions), the user may have set an option
4165 // we can't handle, in which case we return nullptr to report failure.
4166 // Replace `!=` with `>` here to support older struct versions. `!=` has the
4167 // advantage of catching more usage bugs and no disadvantages while there is a
4168 // single supported struct version (the initial version).
4169 if (options->Size != sizeof(CXIndexOptions))
4170 return nullptr;
4171 CIndexer *const CIdxr = clang_createIndex_Impl(
4172 excludeDeclarationsFromPCH: options->ExcludeDeclarationsFromPCH, displayDiagnostics: options->DisplayDiagnostics,
4173 threadBackgroundPriorityForIndexing: options->ThreadBackgroundPriorityForIndexing,
4174 threadBackgroundPriorityForEditing: options->ThreadBackgroundPriorityForEditing);
4175 CIdxr->setStorePreamblesInMemory(options->StorePreamblesInMemory);
4176 CIdxr->setPreambleStoragePath(options->PreambleStoragePath);
4177 CIdxr->setInvocationEmissionPath(options->InvocationEmissionPath);
4178 return CIdxr;
4179}
4180
4181void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
4182 if (CIdx)
4183 static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
4184}
4185
4186unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
4187 if (CIdx)
4188 return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
4189 return 0;
4190}
4191
4192void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx,
4193 const char *Path) {
4194 if (CIdx)
4195 static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : "");
4196}
4197
4198void clang_toggleCrashRecovery(unsigned isEnabled) {
4199 if (isEnabled)
4200 llvm::CrashRecoveryContext::Enable();
4201 else
4202 llvm::CrashRecoveryContext::Disable();
4203}
4204
4205CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
4206 const char *ast_filename) {
4207 CXTranslationUnit TU;
4208 enum CXErrorCode Result =
4209 clang_createTranslationUnit2(CIdx, ast_filename, out_TU: &TU);
4210 (void)Result;
4211 assert((TU && Result == CXError_Success) ||
4212 (!TU && Result != CXError_Success));
4213 return TU;
4214}
4215
4216enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
4217 const char *ast_filename,
4218 CXTranslationUnit *out_TU) {
4219 if (out_TU)
4220 *out_TU = nullptr;
4221
4222 if (!CIdx || !ast_filename || !out_TU)
4223 return CXError_InvalidArguments;
4224
4225 LOG_FUNC_SECTION { *Log << ast_filename; }
4226
4227 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
4228 FileSystemOptions FileSystemOpts;
4229 HeaderSearchOptions HSOpts;
4230
4231 auto VFS = llvm::vfs::getRealFileSystem();
4232
4233 auto DiagOpts = std::make_shared<DiagnosticOptions>();
4234 IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
4235 CompilerInstance::createDiagnostics(VFS&: *VFS, Opts&: *DiagOpts);
4236 std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
4237 Filename: ast_filename, PCHContainerRdr: CXXIdx->getPCHContainerOperations()->getRawReader(),
4238 ToLoad: ASTUnit::LoadEverything, VFS, DiagOpts, Diags, FileSystemOpts, HSOpts,
4239 /*LangOpts=*/nullptr, OnlyLocalDecls: CXXIdx->getOnlyLocalDecls(), CaptureDiagnostics: CaptureDiagsKind::All,
4240 /*AllowASTWithCompilerErrors=*/true,
4241 /*UserFilesAreVolatile=*/true);
4242 *out_TU = MakeCXTranslationUnit(CIdx: CXXIdx, AU: std::move(AU));
4243 return *out_TU ? CXError_Success : CXError_Failure;
4244}
4245
4246unsigned clang_defaultEditingTranslationUnitOptions() {
4247 return CXTranslationUnit_PrecompiledPreamble |
4248 CXTranslationUnit_CacheCompletionResults;
4249}
4250
4251CXTranslationUnit clang_createTranslationUnitFromSourceFile(
4252 CXIndex CIdx, const char *source_filename, int num_command_line_args,
4253 const char *const *command_line_args, unsigned num_unsaved_files,
4254 struct CXUnsavedFile *unsaved_files) {
4255 unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
4256 return clang_parseTranslationUnit(CIdx, source_filename, command_line_args,
4257 num_command_line_args, unsaved_files,
4258 num_unsaved_files, options: Options);
4259}
4260
4261static CXErrorCode
4262clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename,
4263 const char *const *command_line_args,
4264 int num_command_line_args,
4265 ArrayRef<CXUnsavedFile> unsaved_files,
4266 unsigned options, CXTranslationUnit *out_TU) {
4267 // Set up the initial return values.
4268 if (out_TU)
4269 *out_TU = nullptr;
4270
4271 // Check arguments.
4272 if (!CIdx || !out_TU)
4273 return CXError_InvalidArguments;
4274
4275 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
4276
4277 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
4278 setThreadBackgroundPriority();
4279
4280 bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
4281 bool CreatePreambleOnFirstParse =
4282 options & CXTranslationUnit_CreatePreambleOnFirstParse;
4283 // FIXME: Add a flag for modules.
4284 TranslationUnitKind TUKind = (options & (CXTranslationUnit_Incomplete |
4285 CXTranslationUnit_SingleFileParse))
4286 ? TU_Prefix
4287 : TU_Complete;
4288 bool CacheCodeCompletionResults =
4289 options & CXTranslationUnit_CacheCompletionResults;
4290 bool IncludeBriefCommentsInCodeCompletion =
4291 options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
4292 bool SingleFileParse = options & CXTranslationUnit_SingleFileParse;
4293 bool ForSerialization = options & CXTranslationUnit_ForSerialization;
4294 bool RetainExcludedCB =
4295 options & CXTranslationUnit_RetainExcludedConditionalBlocks;
4296 SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None;
4297 if (options & CXTranslationUnit_SkipFunctionBodies) {
4298 SkipFunctionBodies =
4299 (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble)
4300 ? SkipFunctionBodiesScope::Preamble
4301 : SkipFunctionBodiesScope::PreambleAndMainFile;
4302 }
4303
4304 // Configure the diagnostics.
4305 std::shared_ptr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(
4306 Argv: llvm::ArrayRef(command_line_args, num_command_line_args));
4307 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
4308 CompilerInstance::createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem(),
4309 Opts&: *DiagOpts));
4310
4311 if (options & CXTranslationUnit_KeepGoing)
4312 Diags->setFatalsAsError(true);
4313
4314 CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
4315 if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
4316 CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
4317
4318 // Recover resources if we crash before exiting this function.
4319 llvm::CrashRecoveryContextCleanupRegistrar<
4320 DiagnosticsEngine,
4321 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
4322 DiagCleanup(Diags.get());
4323
4324 std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
4325 new std::vector<ASTUnit::RemappedFile>());
4326
4327 // Recover resources if we crash before exiting this function.
4328 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
4329 RemappedCleanup(RemappedFiles.get());
4330
4331 for (auto &UF : unsaved_files) {
4332 std::unique_ptr<llvm::MemoryBuffer> MB =
4333 llvm::MemoryBuffer::getMemBufferCopy(InputData: getContents(UF), BufferName: UF.Filename);
4334 RemappedFiles->push_back(x: std::make_pair(x: UF.Filename, y: MB.release()));
4335 }
4336
4337 std::unique_ptr<std::vector<const char *>> Args(
4338 new std::vector<const char *>());
4339
4340 // Recover resources if we crash before exiting this method.
4341 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char *>>
4342 ArgsCleanup(Args.get());
4343
4344 // Since the Clang C library is primarily used by batch tools dealing with
4345 // (often very broken) source code, where spell-checking can have a
4346 // significant negative impact on performance (particularly when
4347 // precompiled headers are involved), we disable it by default.
4348 // Only do this if we haven't found a spell-checking-related argument.
4349 bool FoundSpellCheckingArgument = false;
4350 for (int I = 0; I != num_command_line_args; ++I) {
4351 if (strcmp(s1: command_line_args[I], s2: "-fno-spell-checking") == 0 ||
4352 strcmp(s1: command_line_args[I], s2: "-fspell-checking") == 0) {
4353 FoundSpellCheckingArgument = true;
4354 break;
4355 }
4356 }
4357 Args->insert(position: Args->end(), first: command_line_args,
4358 last: command_line_args + num_command_line_args);
4359
4360 if (!FoundSpellCheckingArgument)
4361 Args->insert(position: Args->begin() + 1, x: "-fno-spell-checking");
4362
4363 // The 'source_filename' argument is optional. If the caller does not
4364 // specify it then it is assumed that the source file is specified
4365 // in the actual argument list.
4366 // Put the source file after command_line_args otherwise if '-x' flag is
4367 // present it will be unused.
4368 if (source_filename)
4369 Args->push_back(x: source_filename);
4370
4371 // Do we need the detailed preprocessing record?
4372 if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
4373 Args->push_back(x: "-Xclang");
4374 Args->push_back(x: "-detailed-preprocessing-record");
4375 }
4376
4377 // Suppress any editor placeholder diagnostics.
4378 Args->push_back(x: "-fallow-editor-placeholders");
4379
4380 unsigned NumErrors = Diags->getClient()->getNumErrors();
4381 std::unique_ptr<ASTUnit> ErrUnit;
4382 // Unless the user specified that they want the preamble on the first parse
4383 // set it up to be created on the first reparse. This makes the first parse
4384 // faster, trading for a slower (first) reparse.
4385 unsigned PrecompilePreambleAfterNParses =
4386 !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse;
4387
4388 LibclangInvocationReporter InvocationReporter(
4389 *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation,
4390 options, llvm::ArrayRef(*Args), /*InvocationArgs=*/{}, unsaved_files);
4391 std::unique_ptr<ASTUnit> Unit = CreateASTUnitFromCommandLine(
4392 ArgBegin: Args->data(), ArgEnd: Args->data() + Args->size(),
4393 PCHContainerOps: CXXIdx->getPCHContainerOperations(), DiagOpts, Diags,
4394 ResourceFilesPath: CXXIdx->getClangResourcesPath(), StorePreamblesInMemory: CXXIdx->getStorePreamblesInMemory(),
4395 PreambleStoragePath: CXXIdx->getPreambleStoragePath(), OnlyLocalDecls: CXXIdx->getOnlyLocalDecls(),
4396 CaptureDiagnostics, RemappedFiles: *RemappedFiles,
4397 /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses,
4398 TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
4399 /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse,
4400 /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedConditionalBlocks: RetainExcludedCB,
4401 ModuleFormat: CXXIdx->getPCHContainerOperations()->getRawReader().getFormats().front(),
4402 ErrAST: &ErrUnit);
4403
4404 // Early failures in LoadFromCommandLine may return with ErrUnit unset.
4405 if (!Unit && !ErrUnit)
4406 return CXError_ASTReadError;
4407
4408 if (NumErrors != Diags->getClient()->getNumErrors()) {
4409 // Make sure to check that 'Unit' is non-NULL.
4410 if (CXXIdx->getDisplayDiagnostics())
4411 printDiagsToStderr(Unit: Unit ? Unit.get() : ErrUnit.get());
4412 }
4413
4414 if (isASTReadError(AU: Unit ? Unit.get() : ErrUnit.get()))
4415 return CXError_ASTReadError;
4416
4417 *out_TU = MakeCXTranslationUnit(CIdx: CXXIdx, AU: std::move(Unit));
4418 if (CXTranslationUnitImpl *TU = *out_TU) {
4419 TU->ParsingOptions = options;
4420 TU->Arguments.reserve(n: Args->size());
4421 for (const char *Arg : *Args)
4422 TU->Arguments.push_back(x: Arg);
4423 return CXError_Success;
4424 }
4425 return CXError_Failure;
4426}
4427
4428CXTranslationUnit
4429clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
4430 const char *const *command_line_args,
4431 int num_command_line_args,
4432 struct CXUnsavedFile *unsaved_files,
4433 unsigned num_unsaved_files, unsigned options) {
4434 CXTranslationUnit TU;
4435 enum CXErrorCode Result = clang_parseTranslationUnit2(
4436 CIdx, source_filename, command_line_args, num_command_line_args,
4437 unsaved_files, num_unsaved_files, options, out_TU: &TU);
4438 (void)Result;
4439 assert((TU && Result == CXError_Success) ||
4440 (!TU && Result != CXError_Success));
4441 return TU;
4442}
4443
4444enum CXErrorCode clang_parseTranslationUnit2(
4445 CXIndex CIdx, const char *source_filename,
4446 const char *const *command_line_args, int num_command_line_args,
4447 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
4448 unsigned options, CXTranslationUnit *out_TU) {
4449 noteBottomOfStack();
4450 SmallVector<const char *, 4> Args;
4451 Args.push_back(Elt: "clang");
4452 Args.append(in_start: command_line_args, in_end: command_line_args + num_command_line_args);
4453 return clang_parseTranslationUnit2FullArgv(
4454 CIdx, source_filename, command_line_args: Args.data(), num_command_line_args: Args.size(), unsaved_files,
4455 num_unsaved_files, options, out_TU);
4456}
4457
4458enum CXErrorCode clang_parseTranslationUnit2FullArgv(
4459 CXIndex CIdx, const char *source_filename,
4460 const char *const *command_line_args, int num_command_line_args,
4461 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
4462 unsigned options, CXTranslationUnit *out_TU) {
4463 LOG_FUNC_SECTION {
4464 *Log << source_filename << ": ";
4465 for (int i = 0; i != num_command_line_args; ++i)
4466 *Log << command_line_args[i] << " ";
4467 }
4468
4469 if (num_unsaved_files && !unsaved_files)
4470 return CXError_InvalidArguments;
4471
4472 CXErrorCode result = CXError_Failure;
4473 auto ParseTranslationUnitImpl = [=, &result] {
4474 noteBottomOfStack();
4475 result = clang_parseTranslationUnit_Impl(
4476 CIdx, source_filename, command_line_args, num_command_line_args,
4477 unsaved_files: llvm::ArrayRef(unsaved_files, num_unsaved_files), options, out_TU);
4478 };
4479
4480 llvm::CrashRecoveryContext CRC;
4481
4482 if (!RunSafely(CRC, Fn: ParseTranslationUnitImpl)) {
4483 fprintf(stderr, format: "libclang: crash detected during parsing: {\n");
4484 fprintf(stderr, format: " 'source_filename' : '%s'\n", source_filename);
4485 fprintf(stderr, format: " 'command_line_args' : [");
4486 for (int i = 0; i != num_command_line_args; ++i) {
4487 if (i)
4488 fprintf(stderr, format: ", ");
4489 fprintf(stderr, format: "'%s'", command_line_args[i]);
4490 }
4491 fprintf(stderr, format: "],\n");
4492 fprintf(stderr, format: " 'unsaved_files' : [");
4493 for (unsigned i = 0; i != num_unsaved_files; ++i) {
4494 if (i)
4495 fprintf(stderr, format: ", ");
4496 fprintf(stderr, format: "('%s', '...', %ld)", unsaved_files[i].Filename,
4497 unsaved_files[i].Length);
4498 }
4499 fprintf(stderr, format: "],\n");
4500 fprintf(stderr, format: " 'options' : %d,\n", options);
4501 fprintf(stderr, format: "}\n");
4502
4503 return CXError_Crashed;
4504 } else if (getenv(name: "LIBCLANG_RESOURCE_USAGE")) {
4505 if (CXTranslationUnit *TU = out_TU)
4506 PrintLibclangResourceUsage(TU: *TU);
4507 }
4508
4509 return result;
4510}
4511
4512CXString clang_Type_getObjCEncoding(CXType CT) {
4513 CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]);
4514 ASTContext &Ctx = getASTUnit(TU: tu)->getASTContext();
4515 std::string encoding;
4516 Ctx.getObjCEncodingForType(T: QualType::getFromOpaquePtr(Ptr: CT.data[0]), S&: encoding);
4517
4518 return cxstring::createDup(String: encoding);
4519}
4520
4521static const IdentifierInfo *getMacroIdentifier(CXCursor C) {
4522 if (C.kind == CXCursor_MacroDefinition) {
4523 if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C))
4524 return MDR->getName();
4525 } else if (C.kind == CXCursor_MacroExpansion) {
4526 MacroExpansionCursor ME = getCursorMacroExpansion(C);
4527 return ME.getName();
4528 }
4529 return nullptr;
4530}
4531
4532unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) {
4533 const IdentifierInfo *II = getMacroIdentifier(C);
4534 if (!II) {
4535 return false;
4536 }
4537 ASTUnit *ASTU = getCursorASTUnit(Cursor: C);
4538 Preprocessor &PP = ASTU->getPreprocessor();
4539 if (const MacroInfo *MI = PP.getMacroInfo(II))
4540 return MI->isFunctionLike();
4541 return false;
4542}
4543
4544unsigned clang_Cursor_isMacroBuiltin(CXCursor C) {
4545 const IdentifierInfo *II = getMacroIdentifier(C);
4546 if (!II) {
4547 return false;
4548 }
4549 ASTUnit *ASTU = getCursorASTUnit(Cursor: C);
4550 Preprocessor &PP = ASTU->getPreprocessor();
4551 if (const MacroInfo *MI = PP.getMacroInfo(II))
4552 return MI->isBuiltinMacro();
4553 return false;
4554}
4555
4556unsigned clang_Cursor_isFunctionInlined(CXCursor C) {
4557 const Decl *D = getCursorDecl(Cursor: C);
4558 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Val: D);
4559 if (!FD) {
4560 return false;
4561 }
4562 return FD->isInlined();
4563}
4564
4565static StringLiteral *getCFSTR_value(CallExpr *callExpr) {
4566 if (callExpr->getNumArgs() != 1) {
4567 return nullptr;
4568 }
4569
4570 StringLiteral *S = nullptr;
4571 auto *arg = callExpr->getArg(Arg: 0);
4572 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
4573 ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg);
4574 auto *subExpr = I->getSubExprAsWritten();
4575
4576 if (subExpr->getStmtClass() != Stmt::StringLiteralClass) {
4577 return nullptr;
4578 }
4579
4580 S = static_cast<StringLiteral *>(I->getSubExprAsWritten());
4581 } else if (arg->getStmtClass() == Stmt::StringLiteralClass) {
4582 S = static_cast<StringLiteral *>(callExpr->getArg(Arg: 0));
4583 } else {
4584 return nullptr;
4585 }
4586 return S;
4587}
4588
4589struct ExprEvalResult {
4590 CXEvalResultKind EvalType;
4591 union {
4592 unsigned long long unsignedVal;
4593 long long intVal;
4594 double floatVal;
4595 char *stringVal;
4596 } EvalData;
4597 bool IsUnsignedInt;
4598 ~ExprEvalResult() {
4599 if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float &&
4600 EvalType != CXEval_Int) {
4601 delete[] EvalData.stringVal;
4602 }
4603 }
4604};
4605
4606void clang_EvalResult_dispose(CXEvalResult E) {
4607 delete static_cast<ExprEvalResult *>(E);
4608}
4609
4610CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) {
4611 if (!E) {
4612 return CXEval_UnExposed;
4613 }
4614 return ((ExprEvalResult *)E)->EvalType;
4615}
4616
4617int clang_EvalResult_getAsInt(CXEvalResult E) {
4618 return clang_EvalResult_getAsLongLong(E);
4619}
4620
4621long long clang_EvalResult_getAsLongLong(CXEvalResult E) {
4622 if (!E) {
4623 return 0;
4624 }
4625 ExprEvalResult *Result = (ExprEvalResult *)E;
4626 if (Result->IsUnsignedInt)
4627 return Result->EvalData.unsignedVal;
4628 return Result->EvalData.intVal;
4629}
4630
4631unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) {
4632 return ((ExprEvalResult *)E)->IsUnsignedInt;
4633}
4634
4635unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) {
4636 if (!E) {
4637 return 0;
4638 }
4639
4640 ExprEvalResult *Result = (ExprEvalResult *)E;
4641 if (Result->IsUnsignedInt)
4642 return Result->EvalData.unsignedVal;
4643 return Result->EvalData.intVal;
4644}
4645
4646double clang_EvalResult_getAsDouble(CXEvalResult E) {
4647 if (!E) {
4648 return 0;
4649 }
4650 return ((ExprEvalResult *)E)->EvalData.floatVal;
4651}
4652
4653const char *clang_EvalResult_getAsStr(CXEvalResult E) {
4654 if (!E) {
4655 return nullptr;
4656 }
4657 return ((ExprEvalResult *)E)->EvalData.stringVal;
4658}
4659
4660static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
4661 Expr::EvalResult ER;
4662 ASTContext &ctx = getCursorContext(Cursor: C);
4663 if (!expr)
4664 return nullptr;
4665
4666 expr = expr->IgnoreParens();
4667 if (expr->isValueDependent())
4668 return nullptr;
4669 if (!expr->EvaluateAsRValue(Result&: ER, Ctx: ctx))
4670 return nullptr;
4671
4672 QualType rettype;
4673 CallExpr *callExpr;
4674 auto result = std::make_unique<ExprEvalResult>();
4675 result->EvalType = CXEval_UnExposed;
4676 result->IsUnsignedInt = false;
4677
4678 if (ER.Val.isInt()) {
4679 result->EvalType = CXEval_Int;
4680
4681 auto &val = ER.Val.getInt();
4682 if (val.isUnsigned()) {
4683 result->IsUnsignedInt = true;
4684 result->EvalData.unsignedVal = val.getZExtValue();
4685 } else {
4686 result->EvalData.intVal = val.getExtValue();
4687 }
4688
4689 return result.release();
4690 }
4691
4692 if (ER.Val.isFloat()) {
4693 llvm::SmallVector<char, 100> Buffer;
4694 ER.Val.getFloat().toString(Str&: Buffer);
4695 result->EvalType = CXEval_Float;
4696 bool ignored;
4697 llvm::APFloat apFloat = ER.Val.getFloat();
4698 apFloat.convert(ToSemantics: llvm::APFloat::IEEEdouble(),
4699 RM: llvm::APFloat::rmNearestTiesToEven, losesInfo: &ignored);
4700 result->EvalData.floatVal = apFloat.convertToDouble();
4701 return result.release();
4702 }
4703
4704 if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
4705 const auto *I = cast<ImplicitCastExpr>(Val: expr);
4706 auto *subExpr = I->getSubExprAsWritten();
4707 if (subExpr->getStmtClass() == Stmt::StringLiteralClass ||
4708 subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) {
4709 const StringLiteral *StrE = nullptr;
4710 const ObjCStringLiteral *ObjCExpr;
4711 ObjCExpr = dyn_cast<ObjCStringLiteral>(Val: subExpr);
4712
4713 if (ObjCExpr) {
4714 StrE = ObjCExpr->getString();
4715 result->EvalType = CXEval_ObjCStrLiteral;
4716 } else {
4717 StrE = cast<StringLiteral>(Val: I->getSubExprAsWritten());
4718 result->EvalType = CXEval_StrLiteral;
4719 }
4720
4721 std::string strRef(StrE->getString().str());
4722 result->EvalData.stringVal = new char[strRef.size() + 1];
4723 strncpy(dest: result->EvalData.stringVal, src: strRef.c_str(), n: strRef.size());
4724 result->EvalData.stringVal[strRef.size()] = '\0';
4725 return result.release();
4726 }
4727 } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass ||
4728 expr->getStmtClass() == Stmt::StringLiteralClass) {
4729 const StringLiteral *StrE = nullptr;
4730 const ObjCStringLiteral *ObjCExpr;
4731 ObjCExpr = dyn_cast<ObjCStringLiteral>(Val: expr);
4732
4733 if (ObjCExpr) {
4734 StrE = ObjCExpr->getString();
4735 result->EvalType = CXEval_ObjCStrLiteral;
4736 } else {
4737 StrE = cast<StringLiteral>(Val: expr);
4738 result->EvalType = CXEval_StrLiteral;
4739 }
4740
4741 std::string strRef(StrE->getString().str());
4742 result->EvalData.stringVal = new char[strRef.size() + 1];
4743 strncpy(dest: result->EvalData.stringVal, src: strRef.c_str(), n: strRef.size());
4744 result->EvalData.stringVal[strRef.size()] = '\0';
4745 return result.release();
4746 }
4747
4748 if (expr->getStmtClass() == Stmt::CStyleCastExprClass) {
4749 CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr);
4750
4751 rettype = CC->getType();
4752 if (rettype.getAsString() == "CFStringRef" &&
4753 CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) {
4754
4755 callExpr = static_cast<CallExpr *>(CC->getSubExpr());
4756 StringLiteral *S = getCFSTR_value(callExpr);
4757 if (S) {
4758 std::string strLiteral(S->getString().str());
4759 result->EvalType = CXEval_CFStr;
4760
4761 result->EvalData.stringVal = new char[strLiteral.size() + 1];
4762 strncpy(dest: result->EvalData.stringVal, src: strLiteral.c_str(),
4763 n: strLiteral.size());
4764 result->EvalData.stringVal[strLiteral.size()] = '\0';
4765 return result.release();
4766 }
4767 }
4768
4769 } else if (expr->getStmtClass() == Stmt::CallExprClass) {
4770 callExpr = static_cast<CallExpr *>(expr);
4771 rettype = callExpr->getCallReturnType(Ctx: ctx);
4772
4773 if (rettype->isVectorType() || callExpr->getNumArgs() > 1)
4774 return nullptr;
4775
4776 if (rettype->isIntegralType(Ctx: ctx) || rettype->isRealFloatingType()) {
4777 if (callExpr->getNumArgs() == 1 &&
4778 !callExpr->getArg(Arg: 0)->getType()->isIntegralType(Ctx: ctx))
4779 return nullptr;
4780 } else if (rettype.getAsString() == "CFStringRef") {
4781
4782 StringLiteral *S = getCFSTR_value(callExpr);
4783 if (S) {
4784 std::string strLiteral(S->getString().str());
4785 result->EvalType = CXEval_CFStr;
4786 result->EvalData.stringVal = new char[strLiteral.size() + 1];
4787 strncpy(dest: result->EvalData.stringVal, src: strLiteral.c_str(),
4788 n: strLiteral.size());
4789 result->EvalData.stringVal[strLiteral.size()] = '\0';
4790 return result.release();
4791 }
4792 }
4793 } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) {
4794 DeclRefExpr *D = static_cast<DeclRefExpr *>(expr);
4795 ValueDecl *V = D->getDecl();
4796 if (V->getKind() == Decl::Function) {
4797 std::string strName = V->getNameAsString();
4798 result->EvalType = CXEval_Other;
4799 result->EvalData.stringVal = new char[strName.size() + 1];
4800 strncpy(dest: result->EvalData.stringVal, src: strName.c_str(), n: strName.size());
4801 result->EvalData.stringVal[strName.size()] = '\0';
4802 return result.release();
4803 }
4804 }
4805
4806 return nullptr;
4807}
4808
4809static const Expr *evaluateDeclExpr(const Decl *D) {
4810 if (!D)
4811 return nullptr;
4812 if (auto *Var = dyn_cast<VarDecl>(Val: D))
4813 return Var->getInit();
4814 else if (auto *Field = dyn_cast<FieldDecl>(Val: D))
4815 return Field->getInClassInitializer();
4816 return nullptr;
4817}
4818
4819static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
4820 assert(CS && "invalid compound statement");
4821 for (auto *bodyIterator : CS->body()) {
4822 if (const auto *E = dyn_cast<Expr>(Val: bodyIterator))
4823 return E;
4824 }
4825 return nullptr;
4826}
4827
4828CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
4829 const Expr *E = nullptr;
4830 if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
4831 E = evaluateCompoundStmtExpr(CS: cast<CompoundStmt>(Val: getCursorStmt(Cursor: C)));
4832 else if (clang_isDeclaration(C.kind))
4833 E = evaluateDeclExpr(D: getCursorDecl(Cursor: C));
4834 else if (clang_isExpression(C.kind))
4835 E = getCursorExpr(Cursor: C);
4836 if (E)
4837 return const_cast<CXEvalResult>(
4838 reinterpret_cast<const void *>(evaluateExpr(expr: const_cast<Expr *>(E), C)));
4839 return nullptr;
4840}
4841
4842unsigned clang_Cursor_hasAttrs(CXCursor C) {
4843 const Decl *D = getCursorDecl(Cursor: C);
4844 if (!D) {
4845 return 0;
4846 }
4847
4848 if (D->hasAttrs()) {
4849 return 1;
4850 }
4851
4852 return 0;
4853}
4854unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
4855 return CXSaveTranslationUnit_None;
4856}
4857
4858static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU,
4859 const char *FileName,
4860 unsigned options) {
4861 CIndexer *CXXIdx = TU->CIdx;
4862 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
4863 setThreadBackgroundPriority();
4864
4865 bool hadError = cxtu::getASTUnit(TU)->Save(File: FileName);
4866 return hadError ? CXSaveError_Unknown : CXSaveError_None;
4867}
4868
4869int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
4870 unsigned options) {
4871 LOG_FUNC_SECTION { *Log << TU << ' ' << FileName; }
4872
4873 if (isNotUsableTU(TU)) {
4874 LOG_BAD_TU(TU);
4875 return CXSaveError_InvalidTU;
4876 }
4877
4878 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4879 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4880 if (!CXXUnit->hasSema())
4881 return CXSaveError_InvalidTU;
4882
4883 CXSaveError result;
4884 auto SaveTranslationUnitImpl = [=, &result]() {
4885 result = clang_saveTranslationUnit_Impl(TU, FileName, options);
4886 };
4887
4888 if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) {
4889 SaveTranslationUnitImpl();
4890
4891 if (getenv(name: "LIBCLANG_RESOURCE_USAGE"))
4892 PrintLibclangResourceUsage(TU);
4893
4894 return result;
4895 }
4896
4897 // We have an AST that has invalid nodes due to compiler errors.
4898 // Use a crash recovery thread for protection.
4899
4900 llvm::CrashRecoveryContext CRC;
4901
4902 if (!RunSafely(CRC, Fn: SaveTranslationUnitImpl)) {
4903 fprintf(stderr, format: "libclang: crash detected during AST saving: {\n");
4904 fprintf(stderr, format: " 'filename' : '%s'\n", FileName);
4905 fprintf(stderr, format: " 'options' : %d,\n", options);
4906 fprintf(stderr, format: "}\n");
4907
4908 return CXSaveError_Unknown;
4909
4910 } else if (getenv(name: "LIBCLANG_RESOURCE_USAGE")) {
4911 PrintLibclangResourceUsage(TU);
4912 }
4913
4914 return result;
4915}
4916
4917void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
4918 if (CTUnit) {
4919 // If the translation unit has been marked as unsafe to free, just discard
4920 // it.
4921 ASTUnit *Unit = cxtu::getASTUnit(TU: CTUnit);
4922 if (Unit && Unit->isUnsafeToFree())
4923 return;
4924
4925 delete cxtu::getASTUnit(TU: CTUnit);
4926 delete CTUnit->StringPool;
4927 delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
4928 disposeOverridenCXCursorsPool(pool: CTUnit->OverridenCursorsPool);
4929 delete CTUnit->CommentToXML;
4930 delete CTUnit;
4931 }
4932}
4933
4934unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) {
4935 if (CTUnit) {
4936 ASTUnit *Unit = cxtu::getASTUnit(TU: CTUnit);
4937
4938 if (Unit && Unit->isUnsafeToFree())
4939 return false;
4940
4941 Unit->ResetForParse();
4942 return true;
4943 }
4944
4945 return false;
4946}
4947
4948unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
4949 return CXReparse_None;
4950}
4951
4952static CXErrorCode
4953clang_reparseTranslationUnit_Impl(CXTranslationUnit TU,
4954 ArrayRef<CXUnsavedFile> unsaved_files,
4955 unsigned options) {
4956 // Check arguments.
4957 if (isNotUsableTU(TU)) {
4958 LOG_BAD_TU(TU);
4959 return CXError_InvalidArguments;
4960 }
4961
4962 // Reset the associated diagnostics.
4963 delete static_cast<CXDiagnosticSetImpl *>(TU->Diagnostics);
4964 TU->Diagnostics = nullptr;
4965
4966 CIndexer *CXXIdx = TU->CIdx;
4967 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForEditing))
4968 setThreadBackgroundPriority();
4969
4970 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4971 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4972
4973 std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
4974 new std::vector<ASTUnit::RemappedFile>());
4975
4976 // Recover resources if we crash before exiting this function.
4977 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
4978 RemappedCleanup(RemappedFiles.get());
4979
4980 for (auto &UF : unsaved_files) {
4981 std::unique_ptr<llvm::MemoryBuffer> MB =
4982 llvm::MemoryBuffer::getMemBufferCopy(InputData: getContents(UF), BufferName: UF.Filename);
4983 RemappedFiles->push_back(x: std::make_pair(x: UF.Filename, y: MB.release()));
4984 }
4985
4986 if (!CXXUnit->Reparse(PCHContainerOps: CXXIdx->getPCHContainerOperations(), RemappedFiles: *RemappedFiles))
4987 return CXError_Success;
4988 if (isASTReadError(AU: CXXUnit))
4989 return CXError_ASTReadError;
4990 return CXError_Failure;
4991}
4992
4993int clang_reparseTranslationUnit(CXTranslationUnit TU,
4994 unsigned num_unsaved_files,
4995 struct CXUnsavedFile *unsaved_files,
4996 unsigned options) {
4997 LOG_FUNC_SECTION { *Log << TU; }
4998
4999 if (num_unsaved_files && !unsaved_files)
5000 return CXError_InvalidArguments;
5001
5002 CXErrorCode result;
5003 auto ReparseTranslationUnitImpl = [=, &result]() {
5004 result = clang_reparseTranslationUnit_Impl(
5005 TU, unsaved_files: llvm::ArrayRef(unsaved_files, num_unsaved_files), options);
5006 };
5007
5008 llvm::CrashRecoveryContext CRC;
5009
5010 if (!RunSafely(CRC, Fn: ReparseTranslationUnitImpl)) {
5011 fprintf(stderr, format: "libclang: crash detected during reparsing\n");
5012 cxtu::getASTUnit(TU)->setUnsafeToFree(true);
5013 return CXError_Crashed;
5014 } else if (getenv(name: "LIBCLANG_RESOURCE_USAGE"))
5015 PrintLibclangResourceUsage(TU);
5016
5017 return result;
5018}
5019
5020CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
5021 if (isNotUsableTU(TU: CTUnit)) {
5022 LOG_BAD_TU(CTUnit);
5023 return cxstring::createEmpty();
5024 }
5025
5026 ASTUnit *CXXUnit = cxtu::getASTUnit(TU: CTUnit);
5027 return cxstring::createDup(String: CXXUnit->getOriginalSourceFileName());
5028}
5029
5030CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
5031 if (isNotUsableTU(TU)) {
5032 LOG_BAD_TU(TU);
5033 return clang_getNullCursor();
5034 }
5035
5036 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5037 return MakeCXCursor(D: CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
5038}
5039
5040CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) {
5041 if (isNotUsableTU(TU: CTUnit)) {
5042 LOG_BAD_TU(CTUnit);
5043 return nullptr;
5044 }
5045
5046 CXTargetInfoImpl *impl = new CXTargetInfoImpl();
5047 impl->TranslationUnit = CTUnit;
5048 return impl;
5049}
5050
5051CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) {
5052 if (!TargetInfo)
5053 return cxstring::createEmpty();
5054
5055 CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
5056 assert(!isNotUsableTU(CTUnit) &&
5057 "Unexpected unusable translation unit in TargetInfo");
5058
5059 ASTUnit *CXXUnit = cxtu::getASTUnit(TU: CTUnit);
5060 std::string Triple =
5061 CXXUnit->getASTContext().getTargetInfo().getTriple().normalize();
5062 return cxstring::createDup(String: Triple);
5063}
5064
5065int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) {
5066 if (!TargetInfo)
5067 return -1;
5068
5069 CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
5070 assert(!isNotUsableTU(CTUnit) &&
5071 "Unexpected unusable translation unit in TargetInfo");
5072
5073 ASTUnit *CXXUnit = cxtu::getASTUnit(TU: CTUnit);
5074 return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth();
5075}
5076
5077void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) {
5078 if (!TargetInfo)
5079 return;
5080
5081 delete TargetInfo;
5082}
5083
5084//===----------------------------------------------------------------------===//
5085// CXFile Operations.
5086//===----------------------------------------------------------------------===//
5087
5088CXString clang_getFileName(CXFile SFile) {
5089 if (!SFile)
5090 return cxstring::createNull();
5091
5092 FileEntryRef FEnt = *cxfile::getFileEntryRef(File: SFile);
5093 return cxstring::createRef(String: FEnt.getName());
5094}
5095
5096time_t clang_getFileTime(CXFile SFile) {
5097 if (!SFile)
5098 return 0;
5099
5100 FileEntryRef FEnt = *cxfile::getFileEntryRef(File: SFile);
5101 return FEnt.getModificationTime();
5102}
5103
5104CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
5105 if (isNotUsableTU(TU)) {
5106 LOG_BAD_TU(TU);
5107 return nullptr;
5108 }
5109
5110 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5111
5112 FileManager &FMgr = CXXUnit->getFileManager();
5113 return cxfile::makeCXFile(FE: FMgr.getOptionalFileRef(Filename: file_name));
5114}
5115
5116const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
5117 size_t *size) {
5118 if (isNotUsableTU(TU)) {
5119 LOG_BAD_TU(TU);
5120 return nullptr;
5121 }
5122
5123 const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
5124 FileID fid = SM.translateFile(SourceFile: *cxfile::getFileEntryRef(File: file));
5125 std::optional<llvm::MemoryBufferRef> buf = SM.getBufferOrNone(FID: fid);
5126 if (!buf) {
5127 if (size)
5128 *size = 0;
5129 return nullptr;
5130 }
5131 if (size)
5132 *size = buf->getBufferSize();
5133 return buf->getBufferStart();
5134}
5135
5136unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
5137 if (isNotUsableTU(TU)) {
5138 LOG_BAD_TU(TU);
5139 return 0;
5140 }
5141
5142 if (!file)
5143 return 0;
5144
5145 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
5146 FileEntryRef FEnt = *cxfile::getFileEntryRef(File: file);
5147 return CXXUnit->getPreprocessor()
5148 .getHeaderSearchInfo()
5149 .isFileMultipleIncludeGuarded(File: FEnt);
5150}
5151
5152int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
5153 if (!file || !outID)
5154 return 1;
5155
5156 FileEntryRef FEnt = *cxfile::getFileEntryRef(File: file);
5157 const llvm::sys::fs::UniqueID &ID = FEnt.getUniqueID();
5158 outID->data[0] = ID.getDevice();
5159 outID->data[1] = ID.getFile();
5160 outID->data[2] = FEnt.getModificationTime();
5161 return 0;
5162}
5163
5164int clang_File_isEqual(CXFile file1, CXFile file2) {
5165 if (file1 == file2)
5166 return true;
5167
5168 if (!file1 || !file2)
5169 return false;
5170
5171 FileEntryRef FEnt1 = *cxfile::getFileEntryRef(File: file1);
5172 FileEntryRef FEnt2 = *cxfile::getFileEntryRef(File: file2);
5173 return FEnt1 == FEnt2;
5174}
5175
5176CXString clang_File_tryGetRealPathName(CXFile SFile) {
5177 if (!SFile)
5178 return cxstring::createNull();
5179
5180 FileEntryRef FEnt = *cxfile::getFileEntryRef(File: SFile);
5181 return cxstring::createRef(String: FEnt.getFileEntry().tryGetRealPathName());
5182}
5183
5184//===----------------------------------------------------------------------===//
5185// CXCursor Operations.
5186//===----------------------------------------------------------------------===//
5187
5188static const Decl *getDeclFromExpr(const Stmt *E) {
5189 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Val: E))
5190 return getDeclFromExpr(E: CE->getSubExpr());
5191
5192 if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(Val: E))
5193 return RefExpr->getDecl();
5194 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
5195 return ME->getMemberDecl();
5196 if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(Val: E))
5197 return RE->getDecl();
5198 if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Val: E)) {
5199 if (PRE->isExplicitProperty())
5200 return PRE->getExplicitProperty();
5201 // It could be messaging both getter and setter as in:
5202 // ++myobj.myprop;
5203 // in which case prefer to associate the setter since it is less obvious
5204 // from inspecting the source that the setter is going to get called.
5205 if (PRE->isMessagingSetter())
5206 return PRE->getImplicitPropertySetter();
5207 return PRE->getImplicitPropertyGetter();
5208 }
5209 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Val: E))
5210 return getDeclFromExpr(E: POE->getSyntacticForm());
5211 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Val: E))
5212 if (Expr *Src = OVE->getSourceExpr())
5213 return getDeclFromExpr(E: Src);
5214
5215 if (const CallExpr *CE = dyn_cast<CallExpr>(Val: E))
5216 return getDeclFromExpr(E: CE->getCallee());
5217 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Val: E))
5218 if (!CE->isElidable())
5219 return CE->getConstructor();
5220 if (const CXXInheritedCtorInitExpr *CE =
5221 dyn_cast<CXXInheritedCtorInitExpr>(Val: E))
5222 return CE->getConstructor();
5223 if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(Val: E))
5224 return OME->getMethodDecl();
5225
5226 if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(Val: E))
5227 return PE->getProtocol();
5228 if (const SubstNonTypeTemplateParmPackExpr *NTTP =
5229 dyn_cast<SubstNonTypeTemplateParmPackExpr>(Val: E))
5230 return NTTP->getParameterPack();
5231 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(Val: E))
5232 if (isa<NonTypeTemplateParmDecl>(Val: SizeOfPack->getPack()) ||
5233 isa<ParmVarDecl>(Val: SizeOfPack->getPack()))
5234 return SizeOfPack->getPack();
5235
5236 return nullptr;
5237}
5238
5239static SourceLocation getLocationFromExpr(const Expr *E) {
5240 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Val: E))
5241 return getLocationFromExpr(E: CE->getSubExpr());
5242
5243 if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(Val: E))
5244 return /*FIXME:*/ Msg->getLeftLoc();
5245 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
5246 return DRE->getLocation();
5247 if (const MemberExpr *Member = dyn_cast<MemberExpr>(Val: E))
5248 return Member->getMemberLoc();
5249 if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(Val: E))
5250 return Ivar->getLocation();
5251 if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(Val: E))
5252 return SizeOfPack->getPackLoc();
5253 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(Val: E))
5254 return PropRef->getLocation();
5255
5256 return E->getBeginLoc();
5257}
5258
5259extern "C" {
5260
5261unsigned clang_visitChildren(CXCursor parent, CXCursorVisitor visitor,
5262 CXClientData client_data) {
5263 CursorVisitor CursorVis(getCursorTU(Cursor: parent), visitor, client_data,
5264 /*VisitPreprocessorLast=*/false);
5265 return CursorVis.VisitChildren(Cursor: parent);
5266}
5267
5268#ifndef __has_feature
5269#define __has_feature(x) 0
5270#endif
5271#if __has_feature(blocks)
5272typedef enum CXChildVisitResult (^CXCursorVisitorBlock)(CXCursor cursor,
5273 CXCursor parent);
5274
5275static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
5276 CXClientData client_data) {
5277 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
5278 return block(cursor, parent);
5279}
5280#else
5281// If we are compiled with a compiler that doesn't have native blocks support,
5282// define and call the block manually, so the
5283typedef struct _CXChildVisitResult {
5284 void *isa;
5285 int flags;
5286 int reserved;
5287 enum CXChildVisitResult (*invoke)(struct _CXChildVisitResult *, CXCursor,
5288 CXCursor);
5289} * CXCursorVisitorBlock;
5290
5291static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
5292 CXClientData client_data) {
5293 CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
5294 return block->invoke(block, cursor, parent);
5295}
5296#endif
5297
5298unsigned clang_visitChildrenWithBlock(CXCursor parent,
5299 CXCursorVisitorBlock block) {
5300 return clang_visitChildren(parent, visitor: visitWithBlock, client_data: block);
5301}
5302
5303static CXString getDeclSpelling(const Decl *D) {
5304 if (!D)
5305 return cxstring::createEmpty();
5306
5307 const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D);
5308 if (!ND) {
5309 if (const ObjCPropertyImplDecl *PropImpl =
5310 dyn_cast<ObjCPropertyImplDecl>(Val: D))
5311 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
5312 return cxstring::createDup(String: Property->getIdentifier()->getName());
5313
5314 if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(Val: D))
5315 if (Module *Mod = ImportD->getImportedModule())
5316 return cxstring::createDup(String: Mod->getFullModuleName());
5317
5318 return cxstring::createEmpty();
5319 }
5320
5321 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(Val: ND))
5322 return cxstring::createDup(String: OMD->getSelector().getAsString());
5323
5324 if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(Val: ND))
5325 // No, this isn't the same as the code below. getIdentifier() is non-virtual
5326 // and returns different names. NamedDecl returns the class name and
5327 // ObjCCategoryImplDecl returns the category name.
5328 return cxstring::createRef(String: CIMP->getIdentifier()->getNameStart());
5329
5330 if (isa<UsingDirectiveDecl>(Val: D))
5331 return cxstring::createEmpty();
5332
5333 SmallString<1024> S;
5334 llvm::raw_svector_ostream os(S);
5335 ND->printName(OS&: os);
5336
5337 return cxstring::createDup(String: os.str());
5338}
5339
5340CXString clang_getCursorSpelling(CXCursor C) {
5341 if (clang_isTranslationUnit(C.kind))
5342 return clang_getTranslationUnitSpelling(CTUnit: getCursorTU(Cursor: C));
5343
5344 if (clang_isReference(C.kind)) {
5345 switch (C.kind) {
5346 case CXCursor_ObjCSuperClassRef: {
5347 const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
5348 return cxstring::createRef(String: Super->getIdentifier()->getNameStart());
5349 }
5350 case CXCursor_ObjCClassRef: {
5351 const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
5352 return cxstring::createRef(String: Class->getIdentifier()->getNameStart());
5353 }
5354 case CXCursor_ObjCProtocolRef: {
5355 const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
5356 assert(OID && "getCursorSpelling(): Missing protocol decl");
5357 return cxstring::createRef(String: OID->getIdentifier()->getNameStart());
5358 }
5359 case CXCursor_CXXBaseSpecifier: {
5360 const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
5361 return cxstring::createDup(String: B->getType().getAsString());
5362 }
5363 case CXCursor_TypeRef: {
5364 const TypeDecl *Type = getCursorTypeRef(C).first;
5365 assert(Type && "Missing type decl");
5366 const ASTContext &Ctx = getCursorContext(Cursor: C);
5367 QualType T = Ctx.getTypeDeclType(Decl: Type);
5368
5369 PrintingPolicy Policy = Ctx.getPrintingPolicy();
5370 Policy.FullyQualifiedName = true;
5371 Policy.SuppressTagKeyword = false;
5372 return cxstring::createDup(String: T.getAsString(Policy));
5373 }
5374 case CXCursor_TemplateRef: {
5375 const TemplateDecl *Template = getCursorTemplateRef(C).first;
5376 assert(Template && "Missing template decl");
5377
5378 return cxstring::createDup(String: Template->getNameAsString());
5379 }
5380
5381 case CXCursor_NamespaceRef: {
5382 const NamedDecl *NS = getCursorNamespaceRef(C).first;
5383 assert(NS && "Missing namespace decl");
5384
5385 return cxstring::createDup(String: NS->getNameAsString());
5386 }
5387
5388 case CXCursor_MemberRef: {
5389 const FieldDecl *Field = getCursorMemberRef(C).first;
5390 assert(Field && "Missing member decl");
5391
5392 return cxstring::createDup(String: Field->getNameAsString());
5393 }
5394
5395 case CXCursor_LabelRef: {
5396 const LabelStmt *Label = getCursorLabelRef(C).first;
5397 assert(Label && "Missing label");
5398
5399 return cxstring::createRef(String: Label->getName());
5400 }
5401
5402 case CXCursor_OverloadedDeclRef: {
5403 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
5404 if (const Decl *D = dyn_cast<const Decl *>(Val&: Storage)) {
5405 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D))
5406 return cxstring::createDup(String: ND->getNameAsString());
5407 return cxstring::createEmpty();
5408 }
5409 if (const OverloadExpr *E = dyn_cast<const OverloadExpr *>(Val&: Storage))
5410 return cxstring::createDup(String: E->getName().getAsString());
5411 OverloadedTemplateStorage *Ovl =
5412 cast<OverloadedTemplateStorage *>(Val&: Storage);
5413 if (Ovl->size() == 0)
5414 return cxstring::createEmpty();
5415 return cxstring::createDup(String: (*Ovl->begin())->getNameAsString());
5416 }
5417
5418 case CXCursor_VariableRef: {
5419 const VarDecl *Var = getCursorVariableRef(C).first;
5420 assert(Var && "Missing variable decl");
5421
5422 return cxstring::createDup(String: Var->getNameAsString());
5423 }
5424
5425 default:
5426 return cxstring::createRef(String: "<not implemented>");
5427 }
5428 }
5429
5430 if (clang_isExpression(C.kind)) {
5431 const Expr *E = getCursorExpr(Cursor: C);
5432
5433 if (C.kind == CXCursor_ObjCStringLiteral ||
5434 C.kind == CXCursor_StringLiteral) {
5435 const StringLiteral *SLit;
5436 if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(Val: E)) {
5437 SLit = OSL->getString();
5438 } else {
5439 SLit = cast<StringLiteral>(Val: E);
5440 }
5441 SmallString<256> Buf;
5442 llvm::raw_svector_ostream OS(Buf);
5443 SLit->outputString(OS);
5444 return cxstring::createDup(String: OS.str());
5445 }
5446
5447 if (C.kind == CXCursor_BinaryOperator ||
5448 C.kind == CXCursor_CompoundAssignOperator) {
5449 return clang_getBinaryOperatorKindSpelling(
5450 kind: clang_getCursorBinaryOperatorKind(cursor: C));
5451 }
5452
5453 const Decl *D = getDeclFromExpr(E: getCursorExpr(Cursor: C));
5454 if (D)
5455 return getDeclSpelling(D);
5456 return cxstring::createEmpty();
5457 }
5458
5459 if (clang_isStatement(C.kind)) {
5460 const Stmt *S = getCursorStmt(Cursor: C);
5461 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(Val: S))
5462 return cxstring::createRef(String: Label->getName());
5463
5464 return cxstring::createEmpty();
5465 }
5466
5467 if (C.kind == CXCursor_MacroExpansion)
5468 return cxstring::createRef(
5469 String: getCursorMacroExpansion(C).getName()->getNameStart());
5470
5471 if (C.kind == CXCursor_MacroDefinition)
5472 return cxstring::createRef(
5473 String: getCursorMacroDefinition(C)->getName()->getNameStart());
5474
5475 if (C.kind == CXCursor_InclusionDirective)
5476 return cxstring::createDup(String: getCursorInclusionDirective(C)->getFileName());
5477
5478 if (clang_isDeclaration(C.kind))
5479 return getDeclSpelling(D: getCursorDecl(Cursor: C));
5480
5481 if (C.kind == CXCursor_AnnotateAttr) {
5482 const AnnotateAttr *AA = cast<AnnotateAttr>(Val: cxcursor::getCursorAttr(Cursor: C));
5483 return cxstring::createDup(String: AA->getAnnotation());
5484 }
5485
5486 if (C.kind == CXCursor_AsmLabelAttr) {
5487 const AsmLabelAttr *AA = cast<AsmLabelAttr>(Val: cxcursor::getCursorAttr(Cursor: C));
5488 return cxstring::createDup(String: AA->getLabel());
5489 }
5490
5491 if (C.kind == CXCursor_PackedAttr) {
5492 return cxstring::createRef(String: "packed");
5493 }
5494
5495 if (C.kind == CXCursor_VisibilityAttr) {
5496 const VisibilityAttr *AA = cast<VisibilityAttr>(Val: cxcursor::getCursorAttr(Cursor: C));
5497 switch (AA->getVisibility()) {
5498 case VisibilityAttr::VisibilityType::Default:
5499 return cxstring::createRef(String: "default");
5500 case VisibilityAttr::VisibilityType::Hidden:
5501 return cxstring::createRef(String: "hidden");
5502 case VisibilityAttr::VisibilityType::Protected:
5503 return cxstring::createRef(String: "protected");
5504 }
5505 llvm_unreachable("unknown visibility type");
5506 }
5507
5508 return cxstring::createEmpty();
5509}
5510
5511CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, unsigned pieceIndex,
5512 unsigned options) {
5513 if (clang_Cursor_isNull(cursor: C))
5514 return clang_getNullRange();
5515
5516 ASTContext &Ctx = getCursorContext(Cursor: C);
5517
5518 if (clang_isStatement(C.kind)) {
5519 const Stmt *S = getCursorStmt(Cursor: C);
5520 if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(Val: S)) {
5521 if (pieceIndex > 0)
5522 return clang_getNullRange();
5523 return cxloc::translateSourceRange(Context&: Ctx, R: Label->getIdentLoc());
5524 }
5525
5526 return clang_getNullRange();
5527 }
5528
5529 if (C.kind == CXCursor_ObjCMessageExpr) {
5530 if (const ObjCMessageExpr *ME =
5531 dyn_cast_or_null<ObjCMessageExpr>(Val: getCursorExpr(Cursor: C))) {
5532 if (pieceIndex >= ME->getNumSelectorLocs())
5533 return clang_getNullRange();
5534 return cxloc::translateSourceRange(Context&: Ctx, R: ME->getSelectorLoc(Index: pieceIndex));
5535 }
5536 }
5537
5538 if (C.kind == CXCursor_ObjCInstanceMethodDecl ||
5539 C.kind == CXCursor_ObjCClassMethodDecl) {
5540 if (const ObjCMethodDecl *MD =
5541 dyn_cast_or_null<ObjCMethodDecl>(Val: getCursorDecl(Cursor: C))) {
5542 if (pieceIndex >= MD->getNumSelectorLocs())
5543 return clang_getNullRange();
5544 return cxloc::translateSourceRange(Context&: Ctx, R: MD->getSelectorLoc(Index: pieceIndex));
5545 }
5546 }
5547
5548 if (C.kind == CXCursor_ObjCCategoryDecl ||
5549 C.kind == CXCursor_ObjCCategoryImplDecl) {
5550 if (pieceIndex > 0)
5551 return clang_getNullRange();
5552 if (const ObjCCategoryDecl *CD =
5553 dyn_cast_or_null<ObjCCategoryDecl>(Val: getCursorDecl(Cursor: C)))
5554 return cxloc::translateSourceRange(Context&: Ctx, R: CD->getCategoryNameLoc());
5555 if (const ObjCCategoryImplDecl *CID =
5556 dyn_cast_or_null<ObjCCategoryImplDecl>(Val: getCursorDecl(Cursor: C)))
5557 return cxloc::translateSourceRange(Context&: Ctx, R: CID->getCategoryNameLoc());
5558 }
5559
5560 if (C.kind == CXCursor_ModuleImportDecl) {
5561 if (pieceIndex > 0)
5562 return clang_getNullRange();
5563 if (const ImportDecl *ImportD =
5564 dyn_cast_or_null<ImportDecl>(Val: getCursorDecl(Cursor: C))) {
5565 ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs();
5566 if (!Locs.empty())
5567 return cxloc::translateSourceRange(
5568 Context&: Ctx, R: SourceRange(Locs.front(), Locs.back()));
5569 }
5570 return clang_getNullRange();
5571 }
5572
5573 if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor ||
5574 C.kind == CXCursor_ConversionFunction ||
5575 C.kind == CXCursor_FunctionDecl) {
5576 if (pieceIndex > 0)
5577 return clang_getNullRange();
5578 if (const FunctionDecl *FD =
5579 dyn_cast_or_null<FunctionDecl>(Val: getCursorDecl(Cursor: C))) {
5580 DeclarationNameInfo FunctionName = FD->getNameInfo();
5581 return cxloc::translateSourceRange(Context&: Ctx, R: FunctionName.getSourceRange());
5582 }
5583 return clang_getNullRange();
5584 }
5585
5586 // FIXME: A CXCursor_InclusionDirective should give the location of the
5587 // filename, but we don't keep track of this.
5588
5589 // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation
5590 // but we don't keep track of this.
5591
5592 // FIXME: A CXCursor_AsmLabelAttr should give the location of the label
5593 // but we don't keep track of this.
5594
5595 // Default handling, give the location of the cursor.
5596
5597 if (pieceIndex > 0)
5598 return clang_getNullRange();
5599
5600 CXSourceLocation CXLoc = clang_getCursorLocation(C);
5601 SourceLocation Loc = cxloc::translateSourceLocation(L: CXLoc);
5602 return cxloc::translateSourceRange(Context&: Ctx, R: Loc);
5603}
5604
5605CXString clang_Cursor_getMangling(CXCursor C) {
5606 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5607 return cxstring::createEmpty();
5608
5609 // Mangling only works for functions and variables.
5610 const Decl *D = getCursorDecl(Cursor: C);
5611 if (!D || !(isa<FunctionDecl>(Val: D) || isa<VarDecl>(Val: D)))
5612 return cxstring::createEmpty();
5613
5614 ASTContext &Ctx = D->getASTContext();
5615 ASTNameGenerator ASTNameGen(Ctx);
5616 return cxstring::createDup(String: ASTNameGen.getName(D));
5617}
5618
5619CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) {
5620 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5621 return nullptr;
5622
5623 const Decl *D = getCursorDecl(Cursor: C);
5624 if (!(isa<CXXRecordDecl>(Val: D) || isa<CXXMethodDecl>(Val: D)))
5625 return nullptr;
5626
5627 ASTContext &Ctx = D->getASTContext();
5628 ASTNameGenerator ASTNameGen(Ctx);
5629 std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D);
5630 return cxstring::createSet(Strings: Manglings);
5631}
5632
5633CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) {
5634 if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
5635 return nullptr;
5636
5637 const Decl *D = getCursorDecl(Cursor: C);
5638 if (!(isa<ObjCInterfaceDecl>(Val: D) || isa<ObjCImplementationDecl>(Val: D)))
5639 return nullptr;
5640
5641 ASTContext &Ctx = D->getASTContext();
5642 ASTNameGenerator ASTNameGen(Ctx);
5643 std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D);
5644 return cxstring::createSet(Strings: Manglings);
5645}
5646
5647CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) {
5648 if (clang_Cursor_isNull(cursor: C))
5649 return nullptr;
5650 return new PrintingPolicy(getCursorContext(Cursor: C).getPrintingPolicy());
5651}
5652
5653void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) {
5654 if (Policy)
5655 delete static_cast<PrintingPolicy *>(Policy);
5656}
5657
5658unsigned
5659clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy,
5660 enum CXPrintingPolicyProperty Property) {
5661 if (!Policy)
5662 return 0;
5663
5664 PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy);
5665 switch (Property) {
5666 case CXPrintingPolicy_Indentation:
5667 return P->Indentation;
5668 case CXPrintingPolicy_SuppressSpecifiers:
5669 return P->SuppressSpecifiers;
5670 case CXPrintingPolicy_SuppressTagKeyword:
5671 return P->SuppressTagKeyword;
5672 case CXPrintingPolicy_IncludeTagDefinition:
5673 return P->IncludeTagDefinition;
5674 case CXPrintingPolicy_SuppressScope:
5675 return P->SuppressScope;
5676 case CXPrintingPolicy_SuppressUnwrittenScope:
5677 return P->SuppressUnwrittenScope;
5678 case CXPrintingPolicy_SuppressInitializers:
5679 return P->SuppressInitializers;
5680 case CXPrintingPolicy_ConstantArraySizeAsWritten:
5681 return P->ConstantArraySizeAsWritten;
5682 case CXPrintingPolicy_AnonymousTagLocations:
5683 return P->AnonymousTagNameStyle ==
5684 llvm::to_underlying(
5685 E: PrintingPolicy::AnonymousTagMode::SourceLocation);
5686 case CXPrintingPolicy_SuppressStrongLifetime:
5687 return P->SuppressStrongLifetime;
5688 case CXPrintingPolicy_SuppressLifetimeQualifiers:
5689 return P->SuppressLifetimeQualifiers;
5690 case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors:
5691 return P->SuppressTemplateArgsInCXXConstructors;
5692 case CXPrintingPolicy_Bool:
5693 return P->Bool;
5694 case CXPrintingPolicy_Restrict:
5695 return P->Restrict;
5696 case CXPrintingPolicy_Alignof:
5697 return P->Alignof;
5698 case CXPrintingPolicy_UnderscoreAlignof:
5699 return P->UnderscoreAlignof;
5700 case CXPrintingPolicy_UseVoidForZeroParams:
5701 return P->UseVoidForZeroParams;
5702 case CXPrintingPolicy_TerseOutput:
5703 return P->TerseOutput;
5704 case CXPrintingPolicy_PolishForDeclaration:
5705 return P->PolishForDeclaration;
5706 case CXPrintingPolicy_Half:
5707 return P->Half;
5708 case CXPrintingPolicy_MSWChar:
5709 return P->MSWChar;
5710 case CXPrintingPolicy_IncludeNewlines:
5711 return P->IncludeNewlines;
5712 case CXPrintingPolicy_MSVCFormatting:
5713 return P->MSVCFormatting;
5714 case CXPrintingPolicy_ConstantsAsWritten:
5715 return P->ConstantsAsWritten;
5716 case CXPrintingPolicy_SuppressImplicitBase:
5717 return P->SuppressImplicitBase;
5718 case CXPrintingPolicy_FullyQualifiedName:
5719 return P->FullyQualifiedName;
5720 }
5721
5722 assert(false && "Invalid CXPrintingPolicyProperty");
5723 return 0;
5724}
5725
5726void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy,
5727 enum CXPrintingPolicyProperty Property,
5728 unsigned Value) {
5729 if (!Policy)
5730 return;
5731
5732 PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy);
5733 switch (Property) {
5734 case CXPrintingPolicy_Indentation:
5735 P->Indentation = Value;
5736 return;
5737 case CXPrintingPolicy_SuppressSpecifiers:
5738 P->SuppressSpecifiers = Value;
5739 return;
5740 case CXPrintingPolicy_SuppressTagKeyword:
5741 P->SuppressTagKeyword = Value;
5742 return;
5743 case CXPrintingPolicy_IncludeTagDefinition:
5744 P->IncludeTagDefinition = Value;
5745 return;
5746 case CXPrintingPolicy_SuppressScope:
5747 P->SuppressScope = Value;
5748 return;
5749 case CXPrintingPolicy_SuppressUnwrittenScope:
5750 P->SuppressUnwrittenScope = Value;
5751 return;
5752 case CXPrintingPolicy_SuppressInitializers:
5753 P->SuppressInitializers = Value;
5754 return;
5755 case CXPrintingPolicy_ConstantArraySizeAsWritten:
5756 P->ConstantArraySizeAsWritten = Value;
5757 return;
5758 case CXPrintingPolicy_AnonymousTagLocations:
5759 P->AnonymousTagNameStyle = llvm::to_underlying(
5760 E: Value ? PrintingPolicy::AnonymousTagMode::SourceLocation
5761 : PrintingPolicy::AnonymousTagMode::Plain);
5762 return;
5763 case CXPrintingPolicy_SuppressStrongLifetime:
5764 P->SuppressStrongLifetime = Value;
5765 return;
5766 case CXPrintingPolicy_SuppressLifetimeQualifiers:
5767 P->SuppressLifetimeQualifiers = Value;
5768 return;
5769 case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors:
5770 P->SuppressTemplateArgsInCXXConstructors = Value;
5771 return;
5772 case CXPrintingPolicy_Bool:
5773 P->Bool = Value;
5774 return;
5775 case CXPrintingPolicy_Restrict:
5776 P->Restrict = Value;
5777 return;
5778 case CXPrintingPolicy_Alignof:
5779 P->Alignof = Value;
5780 return;
5781 case CXPrintingPolicy_UnderscoreAlignof:
5782 P->UnderscoreAlignof = Value;
5783 return;
5784 case CXPrintingPolicy_UseVoidForZeroParams:
5785 P->UseVoidForZeroParams = Value;
5786 return;
5787 case CXPrintingPolicy_TerseOutput:
5788 P->TerseOutput = Value;
5789 return;
5790 case CXPrintingPolicy_PolishForDeclaration:
5791 P->PolishForDeclaration = Value;
5792 return;
5793 case CXPrintingPolicy_Half:
5794 P->Half = Value;
5795 return;
5796 case CXPrintingPolicy_MSWChar:
5797 P->MSWChar = Value;
5798 return;
5799 case CXPrintingPolicy_IncludeNewlines:
5800 P->IncludeNewlines = Value;
5801 return;
5802 case CXPrintingPolicy_MSVCFormatting:
5803 P->MSVCFormatting = Value;
5804 return;
5805 case CXPrintingPolicy_ConstantsAsWritten:
5806 P->ConstantsAsWritten = Value;
5807 return;
5808 case CXPrintingPolicy_SuppressImplicitBase:
5809 P->SuppressImplicitBase = Value;
5810 return;
5811 case CXPrintingPolicy_FullyQualifiedName:
5812 P->FullyQualifiedName = Value;
5813 return;
5814 }
5815
5816 assert(false && "Invalid CXPrintingPolicyProperty");
5817}
5818
5819CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) {
5820 if (clang_Cursor_isNull(cursor: C))
5821 return cxstring::createEmpty();
5822
5823 if (clang_isDeclaration(C.kind)) {
5824 const Decl *D = getCursorDecl(Cursor: C);
5825 if (!D)
5826 return cxstring::createEmpty();
5827
5828 SmallString<128> Str;
5829 llvm::raw_svector_ostream OS(Str);
5830 PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
5831 D->print(Out&: OS, Policy: UserPolicy ? *UserPolicy
5832 : getCursorContext(Cursor: C).getPrintingPolicy());
5833
5834 return cxstring::createDup(String: OS.str());
5835 }
5836
5837 return cxstring::createEmpty();
5838}
5839
5840CXString clang_getCursorDisplayName(CXCursor C) {
5841 if (!clang_isDeclaration(C.kind))
5842 return clang_getCursorSpelling(C);
5843
5844 const Decl *D = getCursorDecl(Cursor: C);
5845 if (!D)
5846 return cxstring::createEmpty();
5847
5848 PrintingPolicy Policy = getCursorContext(Cursor: C).getPrintingPolicy();
5849 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Val: D))
5850 D = FunTmpl->getTemplatedDecl();
5851
5852 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Val: D)) {
5853 SmallString<64> Str;
5854 llvm::raw_svector_ostream OS(Str);
5855 OS << *Function;
5856 if (Function->getPrimaryTemplate())
5857 OS << "<>";
5858 OS << "(";
5859 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
5860 if (I)
5861 OS << ", ";
5862 OS << Function->getParamDecl(i: I)->getType().getAsString(Policy);
5863 }
5864
5865 if (Function->isVariadic()) {
5866 if (Function->getNumParams())
5867 OS << ", ";
5868 OS << "...";
5869 }
5870 OS << ")";
5871 return cxstring::createDup(String: OS.str());
5872 }
5873
5874 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(Val: D)) {
5875 SmallString<64> Str;
5876 llvm::raw_svector_ostream OS(Str);
5877 OS << *ClassTemplate;
5878 OS << "<";
5879 TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
5880 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
5881 if (I)
5882 OS << ", ";
5883
5884 NamedDecl *Param = Params->getParam(Idx: I);
5885 if (Param->getIdentifier()) {
5886 OS << Param->getIdentifier()->getName();
5887 continue;
5888 }
5889
5890 // There is no parameter name, which makes this tricky. Try to come up
5891 // with something useful that isn't too long.
5892 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param))
5893 if (const auto *TC = TTP->getTypeConstraint()) {
5894 TC->getConceptNameInfo().printName(OS, Policy);
5895 if (TC->hasExplicitTemplateArgs())
5896 OS << "<...>";
5897 } else
5898 OS << (TTP->wasDeclaredWithTypename() ? "typename" : "class");
5899 else if (NonTypeTemplateParmDecl *NTTP =
5900 dyn_cast<NonTypeTemplateParmDecl>(Val: Param))
5901 OS << NTTP->getType().getAsString(Policy);
5902 else
5903 OS << "template<...> class";
5904 }
5905
5906 OS << ">";
5907 return cxstring::createDup(String: OS.str());
5908 }
5909
5910 if (const ClassTemplateSpecializationDecl *ClassSpec =
5911 dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
5912 SmallString<128> Str;
5913 llvm::raw_svector_ostream OS(Str);
5914 OS << *ClassSpec;
5915 // If the template arguments were written explicitly, use them..
5916 if (const auto *ArgsWritten = ClassSpec->getTemplateArgsAsWritten()) {
5917 printTemplateArgumentList(
5918 OS, Args: ArgsWritten->arguments(), Policy,
5919 TPL: ClassSpec->getSpecializedTemplate()->getTemplateParameters());
5920 } else {
5921 printTemplateArgumentList(
5922 OS, Args: ClassSpec->getTemplateArgs().asArray(), Policy,
5923 TPL: ClassSpec->getSpecializedTemplate()->getTemplateParameters());
5924 }
5925 return cxstring::createDup(String: OS.str());
5926 }
5927
5928 return clang_getCursorSpelling(C);
5929}
5930
5931CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
5932 switch (Kind) {
5933 case CXCursor_FunctionDecl:
5934 return cxstring::createRef(String: "FunctionDecl");
5935 case CXCursor_TypedefDecl:
5936 return cxstring::createRef(String: "TypedefDecl");
5937 case CXCursor_EnumDecl:
5938 return cxstring::createRef(String: "EnumDecl");
5939 case CXCursor_EnumConstantDecl:
5940 return cxstring::createRef(String: "EnumConstantDecl");
5941 case CXCursor_StructDecl:
5942 return cxstring::createRef(String: "StructDecl");
5943 case CXCursor_UnionDecl:
5944 return cxstring::createRef(String: "UnionDecl");
5945 case CXCursor_ClassDecl:
5946 return cxstring::createRef(String: "ClassDecl");
5947 case CXCursor_FieldDecl:
5948 return cxstring::createRef(String: "FieldDecl");
5949 case CXCursor_VarDecl:
5950 return cxstring::createRef(String: "VarDecl");
5951 case CXCursor_ParmDecl:
5952 return cxstring::createRef(String: "ParmDecl");
5953 case CXCursor_ObjCInterfaceDecl:
5954 return cxstring::createRef(String: "ObjCInterfaceDecl");
5955 case CXCursor_ObjCCategoryDecl:
5956 return cxstring::createRef(String: "ObjCCategoryDecl");
5957 case CXCursor_ObjCProtocolDecl:
5958 return cxstring::createRef(String: "ObjCProtocolDecl");
5959 case CXCursor_ObjCPropertyDecl:
5960 return cxstring::createRef(String: "ObjCPropertyDecl");
5961 case CXCursor_ObjCIvarDecl:
5962 return cxstring::createRef(String: "ObjCIvarDecl");
5963 case CXCursor_ObjCInstanceMethodDecl:
5964 return cxstring::createRef(String: "ObjCInstanceMethodDecl");
5965 case CXCursor_ObjCClassMethodDecl:
5966 return cxstring::createRef(String: "ObjCClassMethodDecl");
5967 case CXCursor_ObjCImplementationDecl:
5968 return cxstring::createRef(String: "ObjCImplementationDecl");
5969 case CXCursor_ObjCCategoryImplDecl:
5970 return cxstring::createRef(String: "ObjCCategoryImplDecl");
5971 case CXCursor_CXXMethod:
5972 return cxstring::createRef(String: "CXXMethod");
5973 case CXCursor_UnexposedDecl:
5974 return cxstring::createRef(String: "UnexposedDecl");
5975 case CXCursor_ObjCSuperClassRef:
5976 return cxstring::createRef(String: "ObjCSuperClassRef");
5977 case CXCursor_ObjCProtocolRef:
5978 return cxstring::createRef(String: "ObjCProtocolRef");
5979 case CXCursor_ObjCClassRef:
5980 return cxstring::createRef(String: "ObjCClassRef");
5981 case CXCursor_TypeRef:
5982 return cxstring::createRef(String: "TypeRef");
5983 case CXCursor_TemplateRef:
5984 return cxstring::createRef(String: "TemplateRef");
5985 case CXCursor_NamespaceRef:
5986 return cxstring::createRef(String: "NamespaceRef");
5987 case CXCursor_MemberRef:
5988 return cxstring::createRef(String: "MemberRef");
5989 case CXCursor_LabelRef:
5990 return cxstring::createRef(String: "LabelRef");
5991 case CXCursor_OverloadedDeclRef:
5992 return cxstring::createRef(String: "OverloadedDeclRef");
5993 case CXCursor_VariableRef:
5994 return cxstring::createRef(String: "VariableRef");
5995 case CXCursor_IntegerLiteral:
5996 return cxstring::createRef(String: "IntegerLiteral");
5997 case CXCursor_FixedPointLiteral:
5998 return cxstring::createRef(String: "FixedPointLiteral");
5999 case CXCursor_FloatingLiteral:
6000 return cxstring::createRef(String: "FloatingLiteral");
6001 case CXCursor_ImaginaryLiteral:
6002 return cxstring::createRef(String: "ImaginaryLiteral");
6003 case CXCursor_StringLiteral:
6004 return cxstring::createRef(String: "StringLiteral");
6005 case CXCursor_CharacterLiteral:
6006 return cxstring::createRef(String: "CharacterLiteral");
6007 case CXCursor_ParenExpr:
6008 return cxstring::createRef(String: "ParenExpr");
6009 case CXCursor_UnaryOperator:
6010 return cxstring::createRef(String: "UnaryOperator");
6011 case CXCursor_ArraySubscriptExpr:
6012 return cxstring::createRef(String: "ArraySubscriptExpr");
6013 case CXCursor_ArraySectionExpr:
6014 return cxstring::createRef(String: "ArraySectionExpr");
6015 case CXCursor_OMPArrayShapingExpr:
6016 return cxstring::createRef(String: "OMPArrayShapingExpr");
6017 case CXCursor_OMPIteratorExpr:
6018 return cxstring::createRef(String: "OMPIteratorExpr");
6019 case CXCursor_BinaryOperator:
6020 return cxstring::createRef(String: "BinaryOperator");
6021 case CXCursor_CompoundAssignOperator:
6022 return cxstring::createRef(String: "CompoundAssignOperator");
6023 case CXCursor_ConditionalOperator:
6024 return cxstring::createRef(String: "ConditionalOperator");
6025 case CXCursor_CStyleCastExpr:
6026 return cxstring::createRef(String: "CStyleCastExpr");
6027 case CXCursor_CompoundLiteralExpr:
6028 return cxstring::createRef(String: "CompoundLiteralExpr");
6029 case CXCursor_InitListExpr:
6030 return cxstring::createRef(String: "InitListExpr");
6031 case CXCursor_AddrLabelExpr:
6032 return cxstring::createRef(String: "AddrLabelExpr");
6033 case CXCursor_StmtExpr:
6034 return cxstring::createRef(String: "StmtExpr");
6035 case CXCursor_GenericSelectionExpr:
6036 return cxstring::createRef(String: "GenericSelectionExpr");
6037 case CXCursor_GNUNullExpr:
6038 return cxstring::createRef(String: "GNUNullExpr");
6039 case CXCursor_CXXStaticCastExpr:
6040 return cxstring::createRef(String: "CXXStaticCastExpr");
6041 case CXCursor_CXXDynamicCastExpr:
6042 return cxstring::createRef(String: "CXXDynamicCastExpr");
6043 case CXCursor_CXXReinterpretCastExpr:
6044 return cxstring::createRef(String: "CXXReinterpretCastExpr");
6045 case CXCursor_CXXConstCastExpr:
6046 return cxstring::createRef(String: "CXXConstCastExpr");
6047 case CXCursor_CXXFunctionalCastExpr:
6048 return cxstring::createRef(String: "CXXFunctionalCastExpr");
6049 case CXCursor_CXXAddrspaceCastExpr:
6050 return cxstring::createRef(String: "CXXAddrspaceCastExpr");
6051 case CXCursor_CXXTypeidExpr:
6052 return cxstring::createRef(String: "CXXTypeidExpr");
6053 case CXCursor_CXXBoolLiteralExpr:
6054 return cxstring::createRef(String: "CXXBoolLiteralExpr");
6055 case CXCursor_CXXNullPtrLiteralExpr:
6056 return cxstring::createRef(String: "CXXNullPtrLiteralExpr");
6057 case CXCursor_CXXThisExpr:
6058 return cxstring::createRef(String: "CXXThisExpr");
6059 case CXCursor_CXXThrowExpr:
6060 return cxstring::createRef(String: "CXXThrowExpr");
6061 case CXCursor_CXXNewExpr:
6062 return cxstring::createRef(String: "CXXNewExpr");
6063 case CXCursor_CXXDeleteExpr:
6064 return cxstring::createRef(String: "CXXDeleteExpr");
6065 case CXCursor_UnaryExpr:
6066 return cxstring::createRef(String: "UnaryExpr");
6067 case CXCursor_ObjCStringLiteral:
6068 return cxstring::createRef(String: "ObjCStringLiteral");
6069 case CXCursor_ObjCBoolLiteralExpr:
6070 return cxstring::createRef(String: "ObjCBoolLiteralExpr");
6071 case CXCursor_ObjCAvailabilityCheckExpr:
6072 return cxstring::createRef(String: "ObjCAvailabilityCheckExpr");
6073 case CXCursor_ObjCSelfExpr:
6074 return cxstring::createRef(String: "ObjCSelfExpr");
6075 case CXCursor_ObjCEncodeExpr:
6076 return cxstring::createRef(String: "ObjCEncodeExpr");
6077 case CXCursor_ObjCSelectorExpr:
6078 return cxstring::createRef(String: "ObjCSelectorExpr");
6079 case CXCursor_ObjCProtocolExpr:
6080 return cxstring::createRef(String: "ObjCProtocolExpr");
6081 case CXCursor_ObjCBridgedCastExpr:
6082 return cxstring::createRef(String: "ObjCBridgedCastExpr");
6083 case CXCursor_BlockExpr:
6084 return cxstring::createRef(String: "BlockExpr");
6085 case CXCursor_PackExpansionExpr:
6086 return cxstring::createRef(String: "PackExpansionExpr");
6087 case CXCursor_SizeOfPackExpr:
6088 return cxstring::createRef(String: "SizeOfPackExpr");
6089 case CXCursor_PackIndexingExpr:
6090 return cxstring::createRef(String: "PackIndexingExpr");
6091 case CXCursor_LambdaExpr:
6092 return cxstring::createRef(String: "LambdaExpr");
6093 case CXCursor_UnexposedExpr:
6094 return cxstring::createRef(String: "UnexposedExpr");
6095 case CXCursor_DeclRefExpr:
6096 return cxstring::createRef(String: "DeclRefExpr");
6097 case CXCursor_MemberRefExpr:
6098 return cxstring::createRef(String: "MemberRefExpr");
6099 case CXCursor_CallExpr:
6100 return cxstring::createRef(String: "CallExpr");
6101 case CXCursor_ObjCMessageExpr:
6102 return cxstring::createRef(String: "ObjCMessageExpr");
6103 case CXCursor_BuiltinBitCastExpr:
6104 return cxstring::createRef(String: "BuiltinBitCastExpr");
6105 case CXCursor_ConceptSpecializationExpr:
6106 return cxstring::createRef(String: "ConceptSpecializationExpr");
6107 case CXCursor_RequiresExpr:
6108 return cxstring::createRef(String: "RequiresExpr");
6109 case CXCursor_CXXParenListInitExpr:
6110 return cxstring::createRef(String: "CXXParenListInitExpr");
6111 case CXCursor_UnexposedStmt:
6112 return cxstring::createRef(String: "UnexposedStmt");
6113 case CXCursor_DeclStmt:
6114 return cxstring::createRef(String: "DeclStmt");
6115 case CXCursor_LabelStmt:
6116 return cxstring::createRef(String: "LabelStmt");
6117 case CXCursor_CompoundStmt:
6118 return cxstring::createRef(String: "CompoundStmt");
6119 case CXCursor_CaseStmt:
6120 return cxstring::createRef(String: "CaseStmt");
6121 case CXCursor_DefaultStmt:
6122 return cxstring::createRef(String: "DefaultStmt");
6123 case CXCursor_IfStmt:
6124 return cxstring::createRef(String: "IfStmt");
6125 case CXCursor_SwitchStmt:
6126 return cxstring::createRef(String: "SwitchStmt");
6127 case CXCursor_WhileStmt:
6128 return cxstring::createRef(String: "WhileStmt");
6129 case CXCursor_DoStmt:
6130 return cxstring::createRef(String: "DoStmt");
6131 case CXCursor_ForStmt:
6132 return cxstring::createRef(String: "ForStmt");
6133 case CXCursor_GotoStmt:
6134 return cxstring::createRef(String: "GotoStmt");
6135 case CXCursor_IndirectGotoStmt:
6136 return cxstring::createRef(String: "IndirectGotoStmt");
6137 case CXCursor_ContinueStmt:
6138 return cxstring::createRef(String: "ContinueStmt");
6139 case CXCursor_BreakStmt:
6140 return cxstring::createRef(String: "BreakStmt");
6141 case CXCursor_ReturnStmt:
6142 return cxstring::createRef(String: "ReturnStmt");
6143 case CXCursor_GCCAsmStmt:
6144 return cxstring::createRef(String: "GCCAsmStmt");
6145 case CXCursor_MSAsmStmt:
6146 return cxstring::createRef(String: "MSAsmStmt");
6147 case CXCursor_ObjCAtTryStmt:
6148 return cxstring::createRef(String: "ObjCAtTryStmt");
6149 case CXCursor_ObjCAtCatchStmt:
6150 return cxstring::createRef(String: "ObjCAtCatchStmt");
6151 case CXCursor_ObjCAtFinallyStmt:
6152 return cxstring::createRef(String: "ObjCAtFinallyStmt");
6153 case CXCursor_ObjCAtThrowStmt:
6154 return cxstring::createRef(String: "ObjCAtThrowStmt");
6155 case CXCursor_ObjCAtSynchronizedStmt:
6156 return cxstring::createRef(String: "ObjCAtSynchronizedStmt");
6157 case CXCursor_ObjCAutoreleasePoolStmt:
6158 return cxstring::createRef(String: "ObjCAutoreleasePoolStmt");
6159 case CXCursor_ObjCForCollectionStmt:
6160 return cxstring::createRef(String: "ObjCForCollectionStmt");
6161 case CXCursor_CXXCatchStmt:
6162 return cxstring::createRef(String: "CXXCatchStmt");
6163 case CXCursor_CXXTryStmt:
6164 return cxstring::createRef(String: "CXXTryStmt");
6165 case CXCursor_CXXForRangeStmt:
6166 return cxstring::createRef(String: "CXXForRangeStmt");
6167 case CXCursor_SEHTryStmt:
6168 return cxstring::createRef(String: "SEHTryStmt");
6169 case CXCursor_SEHExceptStmt:
6170 return cxstring::createRef(String: "SEHExceptStmt");
6171 case CXCursor_SEHFinallyStmt:
6172 return cxstring::createRef(String: "SEHFinallyStmt");
6173 case CXCursor_SEHLeaveStmt:
6174 return cxstring::createRef(String: "SEHLeaveStmt");
6175 case CXCursor_NullStmt:
6176 return cxstring::createRef(String: "NullStmt");
6177 case CXCursor_InvalidFile:
6178 return cxstring::createRef(String: "InvalidFile");
6179 case CXCursor_InvalidCode:
6180 return cxstring::createRef(String: "InvalidCode");
6181 case CXCursor_NoDeclFound:
6182 return cxstring::createRef(String: "NoDeclFound");
6183 case CXCursor_NotImplemented:
6184 return cxstring::createRef(String: "NotImplemented");
6185 case CXCursor_TranslationUnit:
6186 return cxstring::createRef(String: "TranslationUnit");
6187 case CXCursor_UnexposedAttr:
6188 return cxstring::createRef(String: "UnexposedAttr");
6189 case CXCursor_IBActionAttr:
6190 return cxstring::createRef(String: "attribute(ibaction)");
6191 case CXCursor_IBOutletAttr:
6192 return cxstring::createRef(String: "attribute(iboutlet)");
6193 case CXCursor_IBOutletCollectionAttr:
6194 return cxstring::createRef(String: "attribute(iboutletcollection)");
6195 case CXCursor_CXXFinalAttr:
6196 return cxstring::createRef(String: "attribute(final)");
6197 case CXCursor_CXXOverrideAttr:
6198 return cxstring::createRef(String: "attribute(override)");
6199 case CXCursor_AnnotateAttr:
6200 return cxstring::createRef(String: "attribute(annotate)");
6201 case CXCursor_AsmLabelAttr:
6202 return cxstring::createRef(String: "asm label");
6203 case CXCursor_PackedAttr:
6204 return cxstring::createRef(String: "attribute(packed)");
6205 case CXCursor_PureAttr:
6206 return cxstring::createRef(String: "attribute(pure)");
6207 case CXCursor_ConstAttr:
6208 return cxstring::createRef(String: "attribute(const)");
6209 case CXCursor_NoDuplicateAttr:
6210 return cxstring::createRef(String: "attribute(noduplicate)");
6211 case CXCursor_CUDAConstantAttr:
6212 return cxstring::createRef(String: "attribute(constant)");
6213 case CXCursor_CUDADeviceAttr:
6214 return cxstring::createRef(String: "attribute(device)");
6215 case CXCursor_CUDAGlobalAttr:
6216 return cxstring::createRef(String: "attribute(global)");
6217 case CXCursor_CUDAHostAttr:
6218 return cxstring::createRef(String: "attribute(host)");
6219 case CXCursor_CUDASharedAttr:
6220 return cxstring::createRef(String: "attribute(shared)");
6221 case CXCursor_VisibilityAttr:
6222 return cxstring::createRef(String: "attribute(visibility)");
6223 case CXCursor_DLLExport:
6224 return cxstring::createRef(String: "attribute(dllexport)");
6225 case CXCursor_DLLImport:
6226 return cxstring::createRef(String: "attribute(dllimport)");
6227 case CXCursor_NSReturnsRetained:
6228 return cxstring::createRef(String: "attribute(ns_returns_retained)");
6229 case CXCursor_NSReturnsNotRetained:
6230 return cxstring::createRef(String: "attribute(ns_returns_not_retained)");
6231 case CXCursor_NSReturnsAutoreleased:
6232 return cxstring::createRef(String: "attribute(ns_returns_autoreleased)");
6233 case CXCursor_NSConsumesSelf:
6234 return cxstring::createRef(String: "attribute(ns_consumes_self)");
6235 case CXCursor_NSConsumed:
6236 return cxstring::createRef(String: "attribute(ns_consumed)");
6237 case CXCursor_ObjCException:
6238 return cxstring::createRef(String: "attribute(objc_exception)");
6239 case CXCursor_ObjCNSObject:
6240 return cxstring::createRef(String: "attribute(NSObject)");
6241 case CXCursor_ObjCIndependentClass:
6242 return cxstring::createRef(String: "attribute(objc_independent_class)");
6243 case CXCursor_ObjCPreciseLifetime:
6244 return cxstring::createRef(String: "attribute(objc_precise_lifetime)");
6245 case CXCursor_ObjCReturnsInnerPointer:
6246 return cxstring::createRef(String: "attribute(objc_returns_inner_pointer)");
6247 case CXCursor_ObjCRequiresSuper:
6248 return cxstring::createRef(String: "attribute(objc_requires_super)");
6249 case CXCursor_ObjCRootClass:
6250 return cxstring::createRef(String: "attribute(objc_root_class)");
6251 case CXCursor_ObjCSubclassingRestricted:
6252 return cxstring::createRef(String: "attribute(objc_subclassing_restricted)");
6253 case CXCursor_ObjCExplicitProtocolImpl:
6254 return cxstring::createRef(
6255 String: "attribute(objc_protocol_requires_explicit_implementation)");
6256 case CXCursor_ObjCDesignatedInitializer:
6257 return cxstring::createRef(String: "attribute(objc_designated_initializer)");
6258 case CXCursor_ObjCRuntimeVisible:
6259 return cxstring::createRef(String: "attribute(objc_runtime_visible)");
6260 case CXCursor_ObjCBoxable:
6261 return cxstring::createRef(String: "attribute(objc_boxable)");
6262 case CXCursor_FlagEnum:
6263 return cxstring::createRef(String: "attribute(flag_enum)");
6264 case CXCursor_PreprocessingDirective:
6265 return cxstring::createRef(String: "preprocessing directive");
6266 case CXCursor_MacroDefinition:
6267 return cxstring::createRef(String: "macro definition");
6268 case CXCursor_MacroExpansion:
6269 return cxstring::createRef(String: "macro expansion");
6270 case CXCursor_InclusionDirective:
6271 return cxstring::createRef(String: "inclusion directive");
6272 case CXCursor_Namespace:
6273 return cxstring::createRef(String: "Namespace");
6274 case CXCursor_LinkageSpec:
6275 return cxstring::createRef(String: "LinkageSpec");
6276 case CXCursor_CXXBaseSpecifier:
6277 return cxstring::createRef(String: "C++ base class specifier");
6278 case CXCursor_Constructor:
6279 return cxstring::createRef(String: "CXXConstructor");
6280 case CXCursor_Destructor:
6281 return cxstring::createRef(String: "CXXDestructor");
6282 case CXCursor_ConversionFunction:
6283 return cxstring::createRef(String: "CXXConversion");
6284 case CXCursor_TemplateTypeParameter:
6285 return cxstring::createRef(String: "TemplateTypeParameter");
6286 case CXCursor_NonTypeTemplateParameter:
6287 return cxstring::createRef(String: "NonTypeTemplateParameter");
6288 case CXCursor_TemplateTemplateParameter:
6289 return cxstring::createRef(String: "TemplateTemplateParameter");
6290 case CXCursor_FunctionTemplate:
6291 return cxstring::createRef(String: "FunctionTemplate");
6292 case CXCursor_ClassTemplate:
6293 return cxstring::createRef(String: "ClassTemplate");
6294 case CXCursor_ClassTemplatePartialSpecialization:
6295 return cxstring::createRef(String: "ClassTemplatePartialSpecialization");
6296 case CXCursor_NamespaceAlias:
6297 return cxstring::createRef(String: "NamespaceAlias");
6298 case CXCursor_UsingDirective:
6299 return cxstring::createRef(String: "UsingDirective");
6300 case CXCursor_UsingDeclaration:
6301 return cxstring::createRef(String: "UsingDeclaration");
6302 case CXCursor_TypeAliasDecl:
6303 return cxstring::createRef(String: "TypeAliasDecl");
6304 case CXCursor_ObjCSynthesizeDecl:
6305 return cxstring::createRef(String: "ObjCSynthesizeDecl");
6306 case CXCursor_ObjCDynamicDecl:
6307 return cxstring::createRef(String: "ObjCDynamicDecl");
6308 case CXCursor_CXXAccessSpecifier:
6309 return cxstring::createRef(String: "CXXAccessSpecifier");
6310 case CXCursor_ModuleImportDecl:
6311 return cxstring::createRef(String: "ModuleImport");
6312 case CXCursor_OMPCanonicalLoop:
6313 return cxstring::createRef(String: "OMPCanonicalLoop");
6314 case CXCursor_OMPMetaDirective:
6315 return cxstring::createRef(String: "OMPMetaDirective");
6316 case CXCursor_OMPParallelDirective:
6317 return cxstring::createRef(String: "OMPParallelDirective");
6318 case CXCursor_OMPSimdDirective:
6319 return cxstring::createRef(String: "OMPSimdDirective");
6320 case CXCursor_OMPTileDirective:
6321 return cxstring::createRef(String: "OMPTileDirective");
6322 case CXCursor_OMPStripeDirective:
6323 return cxstring::createRef(String: "OMPStripeDirective");
6324 case CXCursor_OMPUnrollDirective:
6325 return cxstring::createRef(String: "OMPUnrollDirective");
6326 case CXCursor_OMPReverseDirective:
6327 return cxstring::createRef(String: "OMPReverseDirective");
6328 case CXCursor_OMPInterchangeDirective:
6329 return cxstring::createRef(String: "OMPInterchangeDirective");
6330 case CXCursor_OMPFuseDirective:
6331 return cxstring::createRef(String: "OMPFuseDirective");
6332 case CXCursor_OMPSplitDirective:
6333 return cxstring::createRef(String: "OMPSplitDirective");
6334 case CXCursor_OMPForDirective:
6335 return cxstring::createRef(String: "OMPForDirective");
6336 case CXCursor_OMPForSimdDirective:
6337 return cxstring::createRef(String: "OMPForSimdDirective");
6338 case CXCursor_OMPSectionsDirective:
6339 return cxstring::createRef(String: "OMPSectionsDirective");
6340 case CXCursor_OMPSectionDirective:
6341 return cxstring::createRef(String: "OMPSectionDirective");
6342 case CXCursor_OMPScopeDirective:
6343 return cxstring::createRef(String: "OMPScopeDirective");
6344 case CXCursor_OMPSingleDirective:
6345 return cxstring::createRef(String: "OMPSingleDirective");
6346 case CXCursor_OMPMasterDirective:
6347 return cxstring::createRef(String: "OMPMasterDirective");
6348 case CXCursor_OMPCriticalDirective:
6349 return cxstring::createRef(String: "OMPCriticalDirective");
6350 case CXCursor_OMPParallelForDirective:
6351 return cxstring::createRef(String: "OMPParallelForDirective");
6352 case CXCursor_OMPParallelForSimdDirective:
6353 return cxstring::createRef(String: "OMPParallelForSimdDirective");
6354 case CXCursor_OMPParallelMasterDirective:
6355 return cxstring::createRef(String: "OMPParallelMasterDirective");
6356 case CXCursor_OMPParallelMaskedDirective:
6357 return cxstring::createRef(String: "OMPParallelMaskedDirective");
6358 case CXCursor_OMPParallelSectionsDirective:
6359 return cxstring::createRef(String: "OMPParallelSectionsDirective");
6360 case CXCursor_OMPTaskDirective:
6361 return cxstring::createRef(String: "OMPTaskDirective");
6362 case CXCursor_OMPTaskyieldDirective:
6363 return cxstring::createRef(String: "OMPTaskyieldDirective");
6364 case CXCursor_OMPBarrierDirective:
6365 return cxstring::createRef(String: "OMPBarrierDirective");
6366 case CXCursor_OMPTaskwaitDirective:
6367 return cxstring::createRef(String: "OMPTaskwaitDirective");
6368 case CXCursor_OMPAssumeDirective:
6369 return cxstring::createRef(String: "OMPAssumeDirective");
6370 case CXCursor_OMPErrorDirective:
6371 return cxstring::createRef(String: "OMPErrorDirective");
6372 case CXCursor_OMPTaskgroupDirective:
6373 return cxstring::createRef(String: "OMPTaskgroupDirective");
6374 case CXCursor_OMPFlushDirective:
6375 return cxstring::createRef(String: "OMPFlushDirective");
6376 case CXCursor_OMPDepobjDirective:
6377 return cxstring::createRef(String: "OMPDepobjDirective");
6378 case CXCursor_OMPScanDirective:
6379 return cxstring::createRef(String: "OMPScanDirective");
6380 case CXCursor_OMPOrderedDirective:
6381 return cxstring::createRef(String: "OMPOrderedDirective");
6382 case CXCursor_OMPAtomicDirective:
6383 return cxstring::createRef(String: "OMPAtomicDirective");
6384 case CXCursor_OMPTargetDirective:
6385 return cxstring::createRef(String: "OMPTargetDirective");
6386 case CXCursor_OMPTargetDataDirective:
6387 return cxstring::createRef(String: "OMPTargetDataDirective");
6388 case CXCursor_OMPTargetEnterDataDirective:
6389 return cxstring::createRef(String: "OMPTargetEnterDataDirective");
6390 case CXCursor_OMPTargetExitDataDirective:
6391 return cxstring::createRef(String: "OMPTargetExitDataDirective");
6392 case CXCursor_OMPTargetParallelDirective:
6393 return cxstring::createRef(String: "OMPTargetParallelDirective");
6394 case CXCursor_OMPTargetParallelForDirective:
6395 return cxstring::createRef(String: "OMPTargetParallelForDirective");
6396 case CXCursor_OMPTargetUpdateDirective:
6397 return cxstring::createRef(String: "OMPTargetUpdateDirective");
6398 case CXCursor_OMPTeamsDirective:
6399 return cxstring::createRef(String: "OMPTeamsDirective");
6400 case CXCursor_OMPCancellationPointDirective:
6401 return cxstring::createRef(String: "OMPCancellationPointDirective");
6402 case CXCursor_OMPCancelDirective:
6403 return cxstring::createRef(String: "OMPCancelDirective");
6404 case CXCursor_OMPTaskLoopDirective:
6405 return cxstring::createRef(String: "OMPTaskLoopDirective");
6406 case CXCursor_OMPTaskLoopSimdDirective:
6407 return cxstring::createRef(String: "OMPTaskLoopSimdDirective");
6408 case CXCursor_OMPMasterTaskLoopDirective:
6409 return cxstring::createRef(String: "OMPMasterTaskLoopDirective");
6410 case CXCursor_OMPMaskedTaskLoopDirective:
6411 return cxstring::createRef(String: "OMPMaskedTaskLoopDirective");
6412 case CXCursor_OMPMasterTaskLoopSimdDirective:
6413 return cxstring::createRef(String: "OMPMasterTaskLoopSimdDirective");
6414 case CXCursor_OMPMaskedTaskLoopSimdDirective:
6415 return cxstring::createRef(String: "OMPMaskedTaskLoopSimdDirective");
6416 case CXCursor_OMPParallelMasterTaskLoopDirective:
6417 return cxstring::createRef(String: "OMPParallelMasterTaskLoopDirective");
6418 case CXCursor_OMPParallelMaskedTaskLoopDirective:
6419 return cxstring::createRef(String: "OMPParallelMaskedTaskLoopDirective");
6420 case CXCursor_OMPParallelMasterTaskLoopSimdDirective:
6421 return cxstring::createRef(String: "OMPParallelMasterTaskLoopSimdDirective");
6422 case CXCursor_OMPParallelMaskedTaskLoopSimdDirective:
6423 return cxstring::createRef(String: "OMPParallelMaskedTaskLoopSimdDirective");
6424 case CXCursor_OMPDistributeDirective:
6425 return cxstring::createRef(String: "OMPDistributeDirective");
6426 case CXCursor_OMPDistributeParallelForDirective:
6427 return cxstring::createRef(String: "OMPDistributeParallelForDirective");
6428 case CXCursor_OMPDistributeParallelForSimdDirective:
6429 return cxstring::createRef(String: "OMPDistributeParallelForSimdDirective");
6430 case CXCursor_OMPDistributeSimdDirective:
6431 return cxstring::createRef(String: "OMPDistributeSimdDirective");
6432 case CXCursor_OMPTargetParallelForSimdDirective:
6433 return cxstring::createRef(String: "OMPTargetParallelForSimdDirective");
6434 case CXCursor_OMPTargetSimdDirective:
6435 return cxstring::createRef(String: "OMPTargetSimdDirective");
6436 case CXCursor_OMPTeamsDistributeDirective:
6437 return cxstring::createRef(String: "OMPTeamsDistributeDirective");
6438 case CXCursor_OMPTeamsDistributeSimdDirective:
6439 return cxstring::createRef(String: "OMPTeamsDistributeSimdDirective");
6440 case CXCursor_OMPTeamsDistributeParallelForSimdDirective:
6441 return cxstring::createRef(String: "OMPTeamsDistributeParallelForSimdDirective");
6442 case CXCursor_OMPTeamsDistributeParallelForDirective:
6443 return cxstring::createRef(String: "OMPTeamsDistributeParallelForDirective");
6444 case CXCursor_OMPTargetTeamsDirective:
6445 return cxstring::createRef(String: "OMPTargetTeamsDirective");
6446 case CXCursor_OMPTargetTeamsDistributeDirective:
6447 return cxstring::createRef(String: "OMPTargetTeamsDistributeDirective");
6448 case CXCursor_OMPTargetTeamsDistributeParallelForDirective:
6449 return cxstring::createRef(String: "OMPTargetTeamsDistributeParallelForDirective");
6450 case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective:
6451 return cxstring::createRef(
6452 String: "OMPTargetTeamsDistributeParallelForSimdDirective");
6453 case CXCursor_OMPTargetTeamsDistributeSimdDirective:
6454 return cxstring::createRef(String: "OMPTargetTeamsDistributeSimdDirective");
6455 case CXCursor_OMPInteropDirective:
6456 return cxstring::createRef(String: "OMPInteropDirective");
6457 case CXCursor_OMPDispatchDirective:
6458 return cxstring::createRef(String: "OMPDispatchDirective");
6459 case CXCursor_OMPMaskedDirective:
6460 return cxstring::createRef(String: "OMPMaskedDirective");
6461 case CXCursor_OMPGenericLoopDirective:
6462 return cxstring::createRef(String: "OMPGenericLoopDirective");
6463 case CXCursor_OMPTeamsGenericLoopDirective:
6464 return cxstring::createRef(String: "OMPTeamsGenericLoopDirective");
6465 case CXCursor_OMPTargetTeamsGenericLoopDirective:
6466 return cxstring::createRef(String: "OMPTargetTeamsGenericLoopDirective");
6467 case CXCursor_OMPParallelGenericLoopDirective:
6468 return cxstring::createRef(String: "OMPParallelGenericLoopDirective");
6469 case CXCursor_OMPTargetParallelGenericLoopDirective:
6470 return cxstring::createRef(String: "OMPTargetParallelGenericLoopDirective");
6471 case CXCursor_OverloadCandidate:
6472 return cxstring::createRef(String: "OverloadCandidate");
6473 case CXCursor_TypeAliasTemplateDecl:
6474 return cxstring::createRef(String: "TypeAliasTemplateDecl");
6475 case CXCursor_StaticAssert:
6476 return cxstring::createRef(String: "StaticAssert");
6477 case CXCursor_FriendDecl:
6478 return cxstring::createRef(String: "FriendDecl");
6479 case CXCursor_ConvergentAttr:
6480 return cxstring::createRef(String: "attribute(convergent)");
6481 case CXCursor_WarnUnusedAttr:
6482 return cxstring::createRef(String: "attribute(warn_unused)");
6483 case CXCursor_WarnUnusedResultAttr:
6484 return cxstring::createRef(String: "attribute(warn_unused_result)");
6485 case CXCursor_AlignedAttr:
6486 return cxstring::createRef(String: "attribute(aligned)");
6487 case CXCursor_ConceptDecl:
6488 return cxstring::createRef(String: "ConceptDecl");
6489 case CXCursor_OpenACCComputeConstruct:
6490 return cxstring::createRef(String: "OpenACCComputeConstruct");
6491 case CXCursor_OpenACCLoopConstruct:
6492 return cxstring::createRef(String: "OpenACCLoopConstruct");
6493 case CXCursor_OpenACCCombinedConstruct:
6494 return cxstring::createRef(String: "OpenACCCombinedConstruct");
6495 case CXCursor_OpenACCDataConstruct:
6496 return cxstring::createRef(String: "OpenACCDataConstruct");
6497 case CXCursor_OpenACCEnterDataConstruct:
6498 return cxstring::createRef(String: "OpenACCEnterDataConstruct");
6499 case CXCursor_OpenACCExitDataConstruct:
6500 return cxstring::createRef(String: "OpenACCExitDataConstruct");
6501 case CXCursor_OpenACCHostDataConstruct:
6502 return cxstring::createRef(String: "OpenACCHostDataConstruct");
6503 case CXCursor_OpenACCWaitConstruct:
6504 return cxstring::createRef(String: "OpenACCWaitConstruct");
6505 case CXCursor_OpenACCCacheConstruct:
6506 return cxstring::createRef(String: "OpenACCCacheConstruct");
6507 case CXCursor_OpenACCInitConstruct:
6508 return cxstring::createRef(String: "OpenACCInitConstruct");
6509 case CXCursor_OpenACCShutdownConstruct:
6510 return cxstring::createRef(String: "OpenACCShutdownConstruct");
6511 case CXCursor_OpenACCSetConstruct:
6512 return cxstring::createRef(String: "OpenACCSetConstruct");
6513 case CXCursor_OpenACCUpdateConstruct:
6514 return cxstring::createRef(String: "OpenACCUpdateConstruct");
6515 case CXCursor_OpenACCAtomicConstruct:
6516 return cxstring::createRef(String: "OpenACCAtomicConstruct");
6517 }
6518
6519 llvm_unreachable("Unhandled CXCursorKind");
6520}
6521
6522struct GetCursorData {
6523 SourceLocation TokenBeginLoc;
6524 bool PointsAtMacroArgExpansion;
6525 bool VisitedObjCPropertyImplDecl;
6526 SourceLocation VisitedDeclaratorDeclStartLoc;
6527 CXCursor &BestCursor;
6528
6529 GetCursorData(SourceManager &SM, SourceLocation tokenBegin,
6530 CXCursor &outputCursor)
6531 : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
6532 PointsAtMacroArgExpansion = SM.isMacroArgExpansion(Loc: tokenBegin);
6533 VisitedObjCPropertyImplDecl = false;
6534 }
6535};
6536
6537static enum CXChildVisitResult
6538GetCursorVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {
6539 GetCursorData *Data = static_cast<GetCursorData *>(client_data);
6540 CXCursor *BestCursor = &Data->BestCursor;
6541
6542 // If we point inside a macro argument we should provide info of what the
6543 // token is so use the actual cursor, don't replace it with a macro expansion
6544 // cursor.
6545 if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
6546 return CXChildVisit_Recurse;
6547
6548 if (clang_isDeclaration(cursor.kind)) {
6549 // Avoid having the implicit methods override the property decls.
6550 if (const ObjCMethodDecl *MD =
6551 dyn_cast_or_null<ObjCMethodDecl>(Val: getCursorDecl(Cursor: cursor))) {
6552 if (MD->isImplicit())
6553 return CXChildVisit_Break;
6554
6555 } else if (const ObjCInterfaceDecl *ID =
6556 dyn_cast_or_null<ObjCInterfaceDecl>(Val: getCursorDecl(Cursor: cursor))) {
6557 // Check that when we have multiple @class references in the same line,
6558 // that later ones do not override the previous ones.
6559 // If we have:
6560 // @class Foo, Bar;
6561 // source ranges for both start at '@', so 'Bar' will end up overriding
6562 // 'Foo' even though the cursor location was at 'Foo'.
6563 if (BestCursor->kind == CXCursor_ObjCInterfaceDecl ||
6564 BestCursor->kind == CXCursor_ObjCClassRef)
6565 if (const ObjCInterfaceDecl *PrevID =
6566 dyn_cast_or_null<ObjCInterfaceDecl>(
6567 Val: getCursorDecl(Cursor: *BestCursor))) {
6568 if (PrevID != ID && !PrevID->isThisDeclarationADefinition() &&
6569 !ID->isThisDeclarationADefinition())
6570 return CXChildVisit_Break;
6571 }
6572
6573 } else if (const DeclaratorDecl *DD =
6574 dyn_cast_or_null<DeclaratorDecl>(Val: getCursorDecl(Cursor: cursor))) {
6575 SourceLocation StartLoc = DD->getSourceRange().getBegin();
6576 // Check that when we have multiple declarators in the same line,
6577 // that later ones do not override the previous ones.
6578 // If we have:
6579 // int Foo, Bar;
6580 // source ranges for both start at 'int', so 'Bar' will end up overriding
6581 // 'Foo' even though the cursor location was at 'Foo'.
6582 if (Data->VisitedDeclaratorDeclStartLoc == StartLoc)
6583 return CXChildVisit_Break;
6584 Data->VisitedDeclaratorDeclStartLoc = StartLoc;
6585
6586 } else if (const ObjCPropertyImplDecl *PropImp =
6587 dyn_cast_or_null<ObjCPropertyImplDecl>(
6588 Val: getCursorDecl(Cursor: cursor))) {
6589 (void)PropImp;
6590 // Check that when we have multiple @synthesize in the same line,
6591 // that later ones do not override the previous ones.
6592 // If we have:
6593 // @synthesize Foo, Bar;
6594 // source ranges for both start at '@', so 'Bar' will end up overriding
6595 // 'Foo' even though the cursor location was at 'Foo'.
6596 if (Data->VisitedObjCPropertyImplDecl)
6597 return CXChildVisit_Break;
6598 Data->VisitedObjCPropertyImplDecl = true;
6599 }
6600 }
6601
6602 if (clang_isExpression(cursor.kind) &&
6603 clang_isDeclaration(BestCursor->kind)) {
6604 if (const Decl *D = getCursorDecl(Cursor: *BestCursor)) {
6605 // Avoid having the cursor of an expression replace the declaration cursor
6606 // when the expression source range overlaps the declaration range.
6607 // This can happen for C++ constructor expressions whose range generally
6608 // include the variable declaration, e.g.:
6609 // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl
6610 // cursor.
6611 if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
6612 D->getLocation() == Data->TokenBeginLoc)
6613 return CXChildVisit_Break;
6614 }
6615 }
6616
6617 // If our current best cursor is the construction of a temporary object,
6618 // don't replace that cursor with a type reference, because we want
6619 // clang_getCursor() to point at the constructor.
6620 if (clang_isExpression(BestCursor->kind) &&
6621 isa<CXXTemporaryObjectExpr>(Val: getCursorExpr(Cursor: *BestCursor)) &&
6622 cursor.kind == CXCursor_TypeRef) {
6623 // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
6624 // as having the actual point on the type reference.
6625 *BestCursor = getTypeRefedCallExprCursor(cursor: *BestCursor);
6626 return CXChildVisit_Recurse;
6627 }
6628
6629 // If we already have an Objective-C superclass reference, don't
6630 // update it further.
6631 if (BestCursor->kind == CXCursor_ObjCSuperClassRef)
6632 return CXChildVisit_Break;
6633
6634 *BestCursor = cursor;
6635 return CXChildVisit_Recurse;
6636}
6637
6638CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
6639 if (isNotUsableTU(TU)) {
6640 LOG_BAD_TU(TU);
6641 return clang_getNullCursor();
6642 }
6643
6644 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
6645 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
6646
6647 SourceLocation SLoc = cxloc::translateSourceLocation(L: Loc);
6648 CXCursor Result = cxcursor::getCursor(TU, SLoc);
6649
6650 LOG_FUNC_SECTION {
6651 CXFile SearchFile;
6652 unsigned SearchLine, SearchColumn;
6653 CXFile ResultFile;
6654 unsigned ResultLine, ResultColumn;
6655 CXString SearchFileName, ResultFileName, KindSpelling, USR;
6656 const char *IsDef = clang_isCursorDefinition(Result) ? " (Definition)" : "";
6657 CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
6658
6659 clang_getFileLocation(location: Loc, file: &SearchFile, line: &SearchLine, column: &SearchColumn,
6660 offset: nullptr);
6661 clang_getFileLocation(location: ResultLoc, file: &ResultFile, line: &ResultLine, column: &ResultColumn,
6662 offset: nullptr);
6663 SearchFileName = clang_getFileName(SFile: SearchFile);
6664 ResultFileName = clang_getFileName(SFile: ResultFile);
6665 KindSpelling = clang_getCursorKindSpelling(Kind: Result.kind);
6666 USR = clang_getCursorUSR(Result);
6667 *Log << llvm::format(Fmt: "(%s:%d:%d) = %s", Vals: clang_getCString(string: SearchFileName),
6668 Vals: SearchLine, Vals: SearchColumn,
6669 Vals: clang_getCString(string: KindSpelling))
6670 << llvm::format(Fmt: "(%s:%d:%d):%s%s", Vals: clang_getCString(string: ResultFileName),
6671 Vals: ResultLine, Vals: ResultColumn, Vals: clang_getCString(string: USR),
6672 Vals: IsDef);
6673 clang_disposeString(string: SearchFileName);
6674 clang_disposeString(string: ResultFileName);
6675 clang_disposeString(string: KindSpelling);
6676 clang_disposeString(string: USR);
6677
6678 CXCursor Definition = clang_getCursorDefinition(Result);
6679 if (!clang_equalCursors(Definition, clang_getNullCursor())) {
6680 CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
6681 CXString DefinitionKindSpelling =
6682 clang_getCursorKindSpelling(Kind: Definition.kind);
6683 CXFile DefinitionFile;
6684 unsigned DefinitionLine, DefinitionColumn;
6685 clang_getFileLocation(location: DefinitionLoc, file: &DefinitionFile, line: &DefinitionLine,
6686 column: &DefinitionColumn, offset: nullptr);
6687 CXString DefinitionFileName = clang_getFileName(SFile: DefinitionFile);
6688 *Log << llvm::format(Fmt: " -> %s(%s:%d:%d)",
6689 Vals: clang_getCString(string: DefinitionKindSpelling),
6690 Vals: clang_getCString(string: DefinitionFileName), Vals: DefinitionLine,
6691 Vals: DefinitionColumn);
6692 clang_disposeString(string: DefinitionFileName);
6693 clang_disposeString(string: DefinitionKindSpelling);
6694 }
6695 }
6696
6697 return Result;
6698}
6699
6700CXCursor clang_getNullCursor(void) {
6701 return MakeCXCursorInvalid(K: CXCursor_InvalidFile);
6702}
6703
6704unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
6705 // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we
6706 // can't set consistently. For example, when visiting a DeclStmt we will set
6707 // it but we don't set it on the result of clang_getCursorDefinition for
6708 // a reference of the same declaration.
6709 // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works
6710 // when visiting a DeclStmt currently, the AST should be enhanced to be able
6711 // to provide that kind of info.
6712 if (clang_isDeclaration(X.kind))
6713 X.data[1] = nullptr;
6714 if (clang_isDeclaration(Y.kind))
6715 Y.data[1] = nullptr;
6716
6717 return X == Y;
6718}
6719
6720unsigned clang_hashCursor(CXCursor C) {
6721 unsigned Index = 0;
6722 if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
6723 Index = 1;
6724
6725 return llvm::DenseMapInfo<std::pair<unsigned, const void *>>::getHashValue(
6726 PairVal: std::make_pair(x&: C.kind, y&: C.data[Index]));
6727}
6728
6729unsigned clang_isInvalid(enum CXCursorKind K) {
6730 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
6731}
6732
6733unsigned clang_isDeclaration(enum CXCursorKind K) {
6734 return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) ||
6735 (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl);
6736}
6737
6738unsigned clang_isInvalidDeclaration(CXCursor C) {
6739 if (clang_isDeclaration(K: C.kind)) {
6740 if (const Decl *D = getCursorDecl(Cursor: C))
6741 return D->isInvalidDecl();
6742 }
6743
6744 return 0;
6745}
6746
6747unsigned clang_isReference(enum CXCursorKind K) {
6748 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
6749}
6750
6751unsigned clang_isExpression(enum CXCursorKind K) {
6752 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
6753}
6754
6755unsigned clang_isStatement(enum CXCursorKind K) {
6756 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
6757}
6758
6759unsigned clang_isAttribute(enum CXCursorKind K) {
6760 return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
6761}
6762
6763unsigned clang_isTranslationUnit(enum CXCursorKind K) {
6764 return K == CXCursor_TranslationUnit;
6765}
6766
6767unsigned clang_isPreprocessing(enum CXCursorKind K) {
6768 return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
6769}
6770
6771unsigned clang_isUnexposed(enum CXCursorKind K) {
6772 switch (K) {
6773 case CXCursor_UnexposedDecl:
6774 case CXCursor_UnexposedExpr:
6775 case CXCursor_UnexposedStmt:
6776 case CXCursor_UnexposedAttr:
6777 return true;
6778 default:
6779 return false;
6780 }
6781}
6782
6783CXCursorKind clang_getCursorKind(CXCursor C) { return C.kind; }
6784
6785CXSourceLocation clang_getCursorLocation(CXCursor C) {
6786 if (clang_isReference(K: C.kind)) {
6787 switch (C.kind) {
6788 case CXCursor_ObjCSuperClassRef: {
6789 std::pair<const ObjCInterfaceDecl *, SourceLocation> P =
6790 getCursorObjCSuperClassRef(C);
6791 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6792 }
6793
6794 case CXCursor_ObjCProtocolRef: {
6795 std::pair<const ObjCProtocolDecl *, SourceLocation> P =
6796 getCursorObjCProtocolRef(C);
6797 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6798 }
6799
6800 case CXCursor_ObjCClassRef: {
6801 std::pair<const ObjCInterfaceDecl *, SourceLocation> P =
6802 getCursorObjCClassRef(C);
6803 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6804 }
6805
6806 case CXCursor_TypeRef: {
6807 std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
6808 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6809 }
6810
6811 case CXCursor_TemplateRef: {
6812 std::pair<const TemplateDecl *, SourceLocation> P =
6813 getCursorTemplateRef(C);
6814 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6815 }
6816
6817 case CXCursor_NamespaceRef: {
6818 std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
6819 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6820 }
6821
6822 case CXCursor_MemberRef: {
6823 std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
6824 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6825 }
6826
6827 case CXCursor_VariableRef: {
6828 std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C);
6829 return cxloc::translateSourceLocation(Context&: P.first->getASTContext(), Loc: P.second);
6830 }
6831
6832 case CXCursor_CXXBaseSpecifier: {
6833 const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
6834 if (!BaseSpec)
6835 return clang_getNullLocation();
6836
6837 if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
6838 return cxloc::translateSourceLocation(
6839 Context&: getCursorContext(Cursor: C), Loc: TSInfo->getTypeLoc().getBeginLoc());
6840
6841 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C),
6842 Loc: BaseSpec->getBeginLoc());
6843 }
6844
6845 case CXCursor_LabelRef: {
6846 std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
6847 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: P.second);
6848 }
6849
6850 case CXCursor_OverloadedDeclRef:
6851 return cxloc::translateSourceLocation(
6852 Context&: getCursorContext(Cursor: C), Loc: getCursorOverloadedDeclRef(C).second);
6853
6854 default:
6855 // FIXME: Need a way to enumerate all non-reference cases.
6856 llvm_unreachable("Missed a reference kind");
6857 }
6858 }
6859
6860 if (clang_isExpression(K: C.kind))
6861 return cxloc::translateSourceLocation(
6862 Context&: getCursorContext(Cursor: C), Loc: getLocationFromExpr(E: getCursorExpr(Cursor: C)));
6863
6864 if (clang_isStatement(K: C.kind))
6865 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C),
6866 Loc: getCursorStmt(Cursor: C)->getBeginLoc());
6867
6868 if (C.kind == CXCursor_PreprocessingDirective) {
6869 SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
6870 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: L);
6871 }
6872
6873 if (C.kind == CXCursor_MacroExpansion) {
6874 SourceLocation L =
6875 cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin();
6876 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: L);
6877 }
6878
6879 if (C.kind == CXCursor_MacroDefinition) {
6880 SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
6881 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: L);
6882 }
6883
6884 if (C.kind == CXCursor_InclusionDirective) {
6885 SourceLocation L =
6886 cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
6887 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: L);
6888 }
6889
6890 if (clang_isAttribute(K: C.kind)) {
6891 SourceLocation L = cxcursor::getCursorAttr(Cursor: C)->getLocation();
6892 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc: L);
6893 }
6894
6895 if (!clang_isDeclaration(K: C.kind))
6896 return clang_getNullLocation();
6897
6898 const Decl *D = getCursorDecl(Cursor: C);
6899 if (!D)
6900 return clang_getNullLocation();
6901
6902 SourceLocation Loc = D->getLocation();
6903 // FIXME: Multiple variables declared in a single declaration
6904 // currently lack the information needed to correctly determine their
6905 // ranges when accounting for the type-specifier. We use context
6906 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
6907 // and if so, whether it is the first decl.
6908 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) {
6909 if (!cxcursor::isFirstInDeclGroup(C))
6910 Loc = VD->getLocation();
6911 }
6912
6913 // For ObjC methods, give the start location of the method name.
6914 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Val: D))
6915 Loc = MD->getSelectorStartLoc();
6916
6917 return cxloc::translateSourceLocation(Context&: getCursorContext(Cursor: C), Loc);
6918}
6919
6920} // end extern "C"
6921
6922CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) {
6923 assert(TU);
6924
6925 // Guard against an invalid SourceLocation, or we may assert in one
6926 // of the following calls.
6927 if (SLoc.isInvalid())
6928 return clang_getNullCursor();
6929
6930 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
6931
6932 // Translate the given source location to make it point at the beginning of
6933 // the token under the cursor.
6934 SLoc = Lexer::GetBeginningOfToken(Loc: SLoc, SM: CXXUnit->getSourceManager(),
6935 LangOpts: CXXUnit->getASTContext().getLangOpts());
6936
6937 CXCursor Result = MakeCXCursorInvalid(K: CXCursor_NoDeclFound);
6938 if (SLoc.isValid()) {
6939 GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
6940 CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
6941 /*VisitPreprocessorLast=*/true,
6942 /*VisitIncludedEntities=*/false,
6943 SourceLocation(SLoc));
6944 CursorVis.visitFileRegion();
6945 }
6946
6947 return Result;
6948}
6949
6950static SourceRange getRawCursorExtent(CXCursor C) {
6951 if (clang_isReference(K: C.kind)) {
6952 switch (C.kind) {
6953 case CXCursor_ObjCSuperClassRef:
6954 return getCursorObjCSuperClassRef(C).second;
6955
6956 case CXCursor_ObjCProtocolRef:
6957 return getCursorObjCProtocolRef(C).second;
6958
6959 case CXCursor_ObjCClassRef:
6960 return getCursorObjCClassRef(C).second;
6961
6962 case CXCursor_TypeRef:
6963 return getCursorTypeRef(C).second;
6964
6965 case CXCursor_TemplateRef:
6966 return getCursorTemplateRef(C).second;
6967
6968 case CXCursor_NamespaceRef:
6969 return getCursorNamespaceRef(C).second;
6970
6971 case CXCursor_MemberRef:
6972 return getCursorMemberRef(C).second;
6973
6974 case CXCursor_CXXBaseSpecifier:
6975 return getCursorCXXBaseSpecifier(C)->getSourceRange();
6976
6977 case CXCursor_LabelRef:
6978 return getCursorLabelRef(C).second;
6979
6980 case CXCursor_OverloadedDeclRef:
6981 return getCursorOverloadedDeclRef(C).second;
6982
6983 case CXCursor_VariableRef:
6984 return getCursorVariableRef(C).second;
6985
6986 default:
6987 // FIXME: Need a way to enumerate all non-reference cases.
6988 llvm_unreachable("Missed a reference kind");
6989 }
6990 }
6991
6992 if (clang_isExpression(K: C.kind))
6993 return getCursorExpr(Cursor: C)->getSourceRange();
6994
6995 if (clang_isStatement(K: C.kind))
6996 return getCursorStmt(Cursor: C)->getSourceRange();
6997
6998 if (clang_isAttribute(K: C.kind))
6999 return getCursorAttr(Cursor: C)->getRange();
7000
7001 if (C.kind == CXCursor_PreprocessingDirective)
7002 return cxcursor::getCursorPreprocessingDirective(C);
7003
7004 if (C.kind == CXCursor_MacroExpansion) {
7005 ASTUnit *TU = getCursorASTUnit(Cursor: C);
7006 SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange();
7007 return TU->mapRangeFromPreamble(R: Range);
7008 }
7009
7010 if (C.kind == CXCursor_MacroDefinition) {
7011 ASTUnit *TU = getCursorASTUnit(Cursor: C);
7012 SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
7013 return TU->mapRangeFromPreamble(R: Range);
7014 }
7015
7016 if (C.kind == CXCursor_InclusionDirective) {
7017 ASTUnit *TU = getCursorASTUnit(Cursor: C);
7018 SourceRange Range =
7019 cxcursor::getCursorInclusionDirective(C)->getSourceRange();
7020 return TU->mapRangeFromPreamble(R: Range);
7021 }
7022
7023 if (C.kind == CXCursor_TranslationUnit) {
7024 ASTUnit *TU = getCursorASTUnit(Cursor: C);
7025 FileID MainID = TU->getSourceManager().getMainFileID();
7026 SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(FID: MainID);
7027 SourceLocation End = TU->getSourceManager().getLocForEndOfFile(FID: MainID);
7028 return SourceRange(Start, End);
7029 }
7030
7031 if (clang_isDeclaration(K: C.kind)) {
7032 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
7033 if (!D)
7034 return SourceRange();
7035
7036 SourceRange R = D->getSourceRange();
7037 // FIXME: Multiple variables declared in a single declaration
7038 // currently lack the information needed to correctly determine their
7039 // ranges when accounting for the type-specifier. We use context
7040 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
7041 // and if so, whether it is the first decl.
7042 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) {
7043 if (!cxcursor::isFirstInDeclGroup(C))
7044 R.setBegin(VD->getLocation());
7045 }
7046 return R;
7047 }
7048 return SourceRange();
7049}
7050
7051/// Retrieves the "raw" cursor extent, which is then extended to include
7052/// the decl-specifier-seq for declarations.
7053static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
7054 if (clang_isDeclaration(K: C.kind)) {
7055 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
7056 if (!D)
7057 return SourceRange();
7058
7059 SourceRange R = D->getSourceRange();
7060
7061 // Adjust the start of the location for declarations preceded by
7062 // declaration specifiers.
7063 SourceLocation StartLoc;
7064 if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(Val: D)) {
7065 if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
7066 StartLoc = TI->getTypeLoc().getBeginLoc();
7067 } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Val: D)) {
7068 if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
7069 StartLoc = TI->getTypeLoc().getBeginLoc();
7070 }
7071
7072 if (StartLoc.isValid() && R.getBegin().isValid() &&
7073 SrcMgr.isBeforeInTranslationUnit(LHS: StartLoc, RHS: R.getBegin()))
7074 R.setBegin(StartLoc);
7075
7076 // FIXME: Multiple variables declared in a single declaration
7077 // currently lack the information needed to correctly determine their
7078 // ranges when accounting for the type-specifier. We use context
7079 // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
7080 // and if so, whether it is the first decl.
7081 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) {
7082 if (!cxcursor::isFirstInDeclGroup(C))
7083 R.setBegin(VD->getLocation());
7084 }
7085
7086 return R;
7087 }
7088
7089 return getRawCursorExtent(C);
7090}
7091
7092CXSourceRange clang_getCursorExtent(CXCursor C) {
7093 SourceRange R = getRawCursorExtent(C);
7094 if (R.isInvalid())
7095 return clang_getNullRange();
7096
7097 return cxloc::translateSourceRange(Context&: getCursorContext(Cursor: C), R);
7098}
7099
7100CXCursor clang_getCursorReferenced(CXCursor C) {
7101 if (clang_isInvalid(K: C.kind))
7102 return clang_getNullCursor();
7103
7104 CXTranslationUnit tu = getCursorTU(Cursor: C);
7105 if (clang_isDeclaration(K: C.kind)) {
7106 const Decl *D = getCursorDecl(Cursor: C);
7107 if (!D)
7108 return clang_getNullCursor();
7109 if (const UsingDecl *Using = dyn_cast<UsingDecl>(Val: D))
7110 return MakeCursorOverloadedDeclRef(D: Using, Location: D->getLocation(), TU: tu);
7111 if (const ObjCPropertyImplDecl *PropImpl =
7112 dyn_cast<ObjCPropertyImplDecl>(Val: D))
7113 if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
7114 return MakeCXCursor(D: Property, TU: tu);
7115
7116 return C;
7117 }
7118
7119 if (clang_isExpression(K: C.kind)) {
7120 const Expr *E = getCursorExpr(Cursor: C);
7121 const Decl *D = getDeclFromExpr(E);
7122 if (D) {
7123 CXCursor declCursor = MakeCXCursor(D, TU: tu);
7124 declCursor = getSelectorIdentifierCursor(SelIdx: getSelectorIdentifierIndex(cursor: C),
7125 cursor: declCursor);
7126 return declCursor;
7127 }
7128
7129 if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(Val: E))
7130 return MakeCursorOverloadedDeclRef(E: Ovl, TU: tu);
7131
7132 return clang_getNullCursor();
7133 }
7134
7135 if (clang_isStatement(K: C.kind)) {
7136 const Stmt *S = getCursorStmt(Cursor: C);
7137 if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(Val: S))
7138 if (LabelDecl *label = Goto->getLabel())
7139 if (LabelStmt *labelS = label->getStmt())
7140 return MakeCXCursor(S: labelS, Parent: getCursorDecl(Cursor: C), TU: tu);
7141
7142 return clang_getNullCursor();
7143 }
7144
7145 if (C.kind == CXCursor_MacroExpansion) {
7146 if (const MacroDefinitionRecord *Def =
7147 getCursorMacroExpansion(C).getDefinition())
7148 return MakeMacroDefinitionCursor(Def, TU: tu);
7149 }
7150
7151 if (!clang_isReference(K: C.kind))
7152 return clang_getNullCursor();
7153
7154 switch (C.kind) {
7155 case CXCursor_ObjCSuperClassRef:
7156 return MakeCXCursor(D: getCursorObjCSuperClassRef(C).first, TU: tu);
7157
7158 case CXCursor_ObjCProtocolRef: {
7159 const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first;
7160 if (const ObjCProtocolDecl *Def = Prot->getDefinition())
7161 return MakeCXCursor(D: Def, TU: tu);
7162
7163 return MakeCXCursor(D: Prot, TU: tu);
7164 }
7165
7166 case CXCursor_ObjCClassRef: {
7167 const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
7168 if (const ObjCInterfaceDecl *Def = Class->getDefinition())
7169 return MakeCXCursor(D: Def, TU: tu);
7170
7171 return MakeCXCursor(D: Class, TU: tu);
7172 }
7173
7174 case CXCursor_TypeRef:
7175 return MakeCXCursor(D: getCursorTypeRef(C).first, TU: tu);
7176
7177 case CXCursor_TemplateRef:
7178 return MakeCXCursor(D: getCursorTemplateRef(C).first, TU: tu);
7179
7180 case CXCursor_NamespaceRef:
7181 return MakeCXCursor(D: getCursorNamespaceRef(C).first, TU: tu);
7182
7183 case CXCursor_MemberRef:
7184 return MakeCXCursor(D: getCursorMemberRef(C).first, TU: tu);
7185
7186 case CXCursor_CXXBaseSpecifier: {
7187 const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
7188 return clang_getTypeDeclaration(T: cxtype::MakeCXType(T: B->getType(), TU: tu));
7189 }
7190
7191 case CXCursor_LabelRef:
7192 // FIXME: We end up faking the "parent" declaration here because we
7193 // don't want to make CXCursor larger.
7194 return MakeCXCursor(
7195 S: getCursorLabelRef(C).first,
7196 Parent: cxtu::getASTUnit(TU: tu)->getASTContext().getTranslationUnitDecl(), TU: tu);
7197
7198 case CXCursor_OverloadedDeclRef:
7199 return C;
7200
7201 case CXCursor_VariableRef:
7202 return MakeCXCursor(D: getCursorVariableRef(C).first, TU: tu);
7203
7204 default:
7205 // We would prefer to enumerate all non-reference cursor kinds here.
7206 llvm_unreachable("Unhandled reference cursor kind");
7207 }
7208}
7209
7210CXCursor clang_getCursorDefinition(CXCursor C) {
7211 if (clang_isInvalid(K: C.kind))
7212 return clang_getNullCursor();
7213
7214 CXTranslationUnit TU = getCursorTU(Cursor: C);
7215
7216 bool WasReference = false;
7217 if (clang_isReference(K: C.kind) || clang_isExpression(K: C.kind)) {
7218 C = clang_getCursorReferenced(C);
7219 WasReference = true;
7220 }
7221
7222 if (C.kind == CXCursor_MacroExpansion)
7223 return clang_getCursorReferenced(C);
7224
7225 if (!clang_isDeclaration(K: C.kind))
7226 return clang_getNullCursor();
7227
7228 const Decl *D = getCursorDecl(Cursor: C);
7229 if (!D)
7230 return clang_getNullCursor();
7231
7232 switch (D->getKind()) {
7233 // Declaration kinds that don't really separate the notions of
7234 // declaration and definition.
7235 case Decl::Namespace:
7236 case Decl::Typedef:
7237 case Decl::TypeAlias:
7238 case Decl::TypeAliasTemplate:
7239 case Decl::TemplateTypeParm:
7240 case Decl::EnumConstant:
7241 case Decl::Field:
7242 case Decl::Binding:
7243 case Decl::MSProperty:
7244 case Decl::MSGuid:
7245 case Decl::HLSLBuffer:
7246 case Decl::HLSLRootSignature:
7247 case Decl::UnnamedGlobalConstant:
7248 case Decl::TemplateParamObject:
7249 case Decl::IndirectField:
7250 case Decl::ObjCIvar:
7251 case Decl::ObjCAtDefsField:
7252 case Decl::ImplicitParam:
7253 case Decl::ParmVar:
7254 case Decl::NonTypeTemplateParm:
7255 case Decl::TemplateTemplateParm:
7256 case Decl::ObjCCategoryImpl:
7257 case Decl::ObjCImplementation:
7258 case Decl::AccessSpec:
7259 case Decl::LinkageSpec:
7260 case Decl::Export:
7261 case Decl::ObjCPropertyImpl:
7262 case Decl::FileScopeAsm:
7263 case Decl::TopLevelStmt:
7264 case Decl::StaticAssert:
7265 case Decl::ExplicitInstantiation:
7266 case Decl::Block:
7267 case Decl::OutlinedFunction:
7268 case Decl::Captured:
7269 case Decl::OMPCapturedExpr:
7270 case Decl::Label: // FIXME: Is this right??
7271 case Decl::CXXDeductionGuide:
7272 case Decl::Import:
7273 case Decl::OMPThreadPrivate:
7274 case Decl::OMPGroupPrivate:
7275 case Decl::OMPAllocate:
7276 case Decl::OMPDeclareReduction:
7277 case Decl::OMPDeclareMapper:
7278 case Decl::OMPRequires:
7279 case Decl::ObjCTypeParam:
7280 case Decl::BuiltinTemplate:
7281 case Decl::PragmaComment:
7282 case Decl::PragmaDetectMismatch:
7283 case Decl::UsingPack:
7284 case Decl::Concept:
7285 case Decl::ImplicitConceptSpecialization:
7286 case Decl::LifetimeExtendedTemporary:
7287 case Decl::RequiresExprBody:
7288 case Decl::UnresolvedUsingIfExists:
7289 case Decl::OpenACCDeclare:
7290 case Decl::OpenACCRoutine:
7291 return C;
7292
7293 // Declaration kinds that don't make any sense here, but are
7294 // nonetheless harmless.
7295 case Decl::Empty:
7296 case Decl::TranslationUnit:
7297 case Decl::ExternCContext:
7298 break;
7299
7300 // Declaration kinds for which the definition is not resolvable.
7301 case Decl::UnresolvedUsingTypename:
7302 case Decl::UnresolvedUsingValue:
7303 break;
7304
7305 case Decl::UsingDirective:
7306 return MakeCXCursor(D: cast<UsingDirectiveDecl>(Val: D)->getNominatedNamespace(),
7307 TU);
7308
7309 case Decl::NamespaceAlias:
7310 return MakeCXCursor(D: cast<NamespaceAliasDecl>(Val: D)->getNamespace(), TU);
7311
7312 case Decl::Enum:
7313 case Decl::Record:
7314 case Decl::CXXRecord:
7315 case Decl::ClassTemplateSpecialization:
7316 case Decl::ClassTemplatePartialSpecialization:
7317 if (TagDecl *Def = cast<TagDecl>(Val: D)->getDefinition())
7318 return MakeCXCursor(D: Def, TU);
7319 return clang_getNullCursor();
7320
7321 case Decl::Function:
7322 case Decl::CXXMethod:
7323 case Decl::CXXConstructor:
7324 case Decl::CXXDestructor:
7325 case Decl::CXXConversion: {
7326 const FunctionDecl *Def = nullptr;
7327 if (cast<FunctionDecl>(Val: D)->getBody(Definition&: Def))
7328 return MakeCXCursor(D: Def, TU);
7329 return clang_getNullCursor();
7330 }
7331
7332 case Decl::Var:
7333 case Decl::VarTemplateSpecialization:
7334 case Decl::VarTemplatePartialSpecialization:
7335 case Decl::Decomposition: {
7336 // Ask the variable if it has a definition.
7337 if (const VarDecl *Def = cast<VarDecl>(Val: D)->getDefinition())
7338 return MakeCXCursor(D: Def, TU);
7339 return clang_getNullCursor();
7340 }
7341
7342 case Decl::FunctionTemplate: {
7343 const FunctionDecl *Def = nullptr;
7344 if (cast<FunctionTemplateDecl>(Val: D)->getTemplatedDecl()->getBody(Definition&: Def))
7345 return MakeCXCursor(D: Def->getDescribedFunctionTemplate(), TU);
7346 return clang_getNullCursor();
7347 }
7348
7349 case Decl::ClassTemplate: {
7350 if (RecordDecl *Def =
7351 cast<ClassTemplateDecl>(Val: D)->getTemplatedDecl()->getDefinition())
7352 return MakeCXCursor(D: cast<CXXRecordDecl>(Val: Def)->getDescribedClassTemplate(),
7353 TU);
7354 return clang_getNullCursor();
7355 }
7356
7357 case Decl::VarTemplate: {
7358 if (VarDecl *Def =
7359 cast<VarTemplateDecl>(Val: D)->getTemplatedDecl()->getDefinition())
7360 return MakeCXCursor(D: cast<VarDecl>(Val: Def)->getDescribedVarTemplate(), TU);
7361 return clang_getNullCursor();
7362 }
7363
7364 case Decl::Using:
7365 case Decl::UsingEnum:
7366 return MakeCursorOverloadedDeclRef(D: cast<BaseUsingDecl>(Val: D), Location: D->getLocation(),
7367 TU);
7368
7369 case Decl::UsingShadow:
7370 case Decl::ConstructorUsingShadow:
7371 return clang_getCursorDefinition(
7372 C: MakeCXCursor(D: cast<UsingShadowDecl>(Val: D)->getTargetDecl(), TU));
7373
7374 case Decl::ObjCMethod: {
7375 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(Val: D);
7376 if (Method->isThisDeclarationADefinition())
7377 return C;
7378
7379 // Dig out the method definition in the associated
7380 // @implementation, if we have it.
7381 // FIXME: The ASTs should make finding the definition easier.
7382 if (const ObjCInterfaceDecl *Class =
7383 dyn_cast<ObjCInterfaceDecl>(Val: Method->getDeclContext()))
7384 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
7385 if (ObjCMethodDecl *Def = ClassImpl->getMethod(
7386 Sel: Method->getSelector(), isInstance: Method->isInstanceMethod()))
7387 if (Def->isThisDeclarationADefinition())
7388 return MakeCXCursor(D: Def, TU);
7389
7390 return clang_getNullCursor();
7391 }
7392
7393 case Decl::ObjCCategory:
7394 if (ObjCCategoryImplDecl *Impl =
7395 cast<ObjCCategoryDecl>(Val: D)->getImplementation())
7396 return MakeCXCursor(D: Impl, TU);
7397 return clang_getNullCursor();
7398
7399 case Decl::ObjCProtocol:
7400 if (const ObjCProtocolDecl *Def =
7401 cast<ObjCProtocolDecl>(Val: D)->getDefinition())
7402 return MakeCXCursor(D: Def, TU);
7403 return clang_getNullCursor();
7404
7405 case Decl::ObjCInterface: {
7406 // There are two notions of a "definition" for an Objective-C
7407 // class: the interface and its implementation. When we resolved a
7408 // reference to an Objective-C class, produce the @interface as
7409 // the definition; when we were provided with the interface,
7410 // produce the @implementation as the definition.
7411 const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(Val: D);
7412 if (WasReference) {
7413 if (const ObjCInterfaceDecl *Def = IFace->getDefinition())
7414 return MakeCXCursor(D: Def, TU);
7415 } else if (ObjCImplementationDecl *Impl = IFace->getImplementation())
7416 return MakeCXCursor(D: Impl, TU);
7417 return clang_getNullCursor();
7418 }
7419
7420 case Decl::ObjCProperty:
7421 // FIXME: We don't really know where to find the
7422 // ObjCPropertyImplDecls that implement this property.
7423 return clang_getNullCursor();
7424
7425 case Decl::ObjCCompatibleAlias:
7426 if (const ObjCInterfaceDecl *Class =
7427 cast<ObjCCompatibleAliasDecl>(Val: D)->getClassInterface())
7428 if (const ObjCInterfaceDecl *Def = Class->getDefinition())
7429 return MakeCXCursor(D: Def, TU);
7430
7431 return clang_getNullCursor();
7432
7433 case Decl::Friend:
7434 if (NamedDecl *Friend = cast<FriendDecl>(Val: D)->getFriendDecl())
7435 return clang_getCursorDefinition(C: MakeCXCursor(D: Friend, TU));
7436 return clang_getNullCursor();
7437
7438 case Decl::FriendTemplate:
7439 if (NamedDecl *Friend = cast<FriendTemplateDecl>(Val: D)->getFriendDecl())
7440 return clang_getCursorDefinition(C: MakeCXCursor(D: Friend, TU));
7441 return clang_getNullCursor();
7442 }
7443
7444 return clang_getNullCursor();
7445}
7446
7447unsigned clang_isCursorDefinition(CXCursor C) {
7448 if (!clang_isDeclaration(K: C.kind))
7449 return 0;
7450
7451 return clang_getCursorDefinition(C) == C;
7452}
7453
7454CXCursor clang_getCanonicalCursor(CXCursor C) {
7455 if (!clang_isDeclaration(K: C.kind))
7456 return C;
7457
7458 if (const Decl *D = getCursorDecl(Cursor: C)) {
7459 if (const ObjCCategoryImplDecl *CatImplD =
7460 dyn_cast<ObjCCategoryImplDecl>(Val: D))
7461 if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
7462 return MakeCXCursor(D: CatD, TU: getCursorTU(Cursor: C));
7463
7464 if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
7465 if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
7466 return MakeCXCursor(D: IFD, TU: getCursorTU(Cursor: C));
7467
7468 return MakeCXCursor(D: D->getCanonicalDecl(), TU: getCursorTU(Cursor: C));
7469 }
7470
7471 return C;
7472}
7473
7474int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) {
7475 return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first;
7476}
7477
7478unsigned clang_getNumOverloadedDecls(CXCursor C) {
7479 if (C.kind != CXCursor_OverloadedDeclRef)
7480 return 0;
7481
7482 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
7483 if (const OverloadExpr *E = dyn_cast<const OverloadExpr *>(Val&: Storage))
7484 return E->getNumDecls();
7485
7486 if (OverloadedTemplateStorage *S =
7487 dyn_cast<OverloadedTemplateStorage *>(Val&: Storage))
7488 return S->size();
7489
7490 const Decl *D = cast<const Decl *>(Val&: Storage);
7491 if (const UsingDecl *Using = dyn_cast<UsingDecl>(Val: D))
7492 return Using->shadow_size();
7493
7494 return 0;
7495}
7496
7497CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
7498 if (cursor.kind != CXCursor_OverloadedDeclRef)
7499 return clang_getNullCursor();
7500
7501 if (index >= clang_getNumOverloadedDecls(C: cursor))
7502 return clang_getNullCursor();
7503
7504 CXTranslationUnit TU = getCursorTU(Cursor: cursor);
7505 OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C: cursor).first;
7506 if (const OverloadExpr *E = dyn_cast<const OverloadExpr *>(Val&: Storage))
7507 return MakeCXCursor(D: E->decls_begin()[index], TU);
7508
7509 if (OverloadedTemplateStorage *S =
7510 dyn_cast<OverloadedTemplateStorage *>(Val&: Storage))
7511 return MakeCXCursor(D: S->begin()[index], TU);
7512
7513 const Decl *D = cast<const Decl *>(Val&: Storage);
7514 if (const UsingDecl *Using = dyn_cast<UsingDecl>(Val: D)) {
7515 // FIXME: This is, unfortunately, linear time.
7516 UsingDecl::shadow_iterator Pos = Using->shadow_begin();
7517 std::advance(i&: Pos, n: index);
7518 return MakeCXCursor(D: cast<UsingShadowDecl>(Val: *Pos)->getTargetDecl(), TU);
7519 }
7520
7521 return clang_getNullCursor();
7522}
7523
7524void clang_getDefinitionSpellingAndExtent(
7525 CXCursor C, const char **startBuf, const char **endBuf, unsigned *startLine,
7526 unsigned *startColumn, unsigned *endLine, unsigned *endColumn) {
7527 assert(getCursorDecl(C) && "CXCursor has null decl");
7528 const auto *FD = cast<FunctionDecl>(Val: getCursorDecl(Cursor: C));
7529 const auto *Body = cast<CompoundStmt>(Val: FD->getBody());
7530
7531 SourceManager &SM = FD->getASTContext().getSourceManager();
7532 *startBuf = SM.getCharacterData(SL: Body->getLBracLoc());
7533 *endBuf = SM.getCharacterData(SL: Body->getRBracLoc());
7534 *startLine = SM.getSpellingLineNumber(Loc: Body->getLBracLoc());
7535 *startColumn = SM.getSpellingColumnNumber(Loc: Body->getLBracLoc());
7536 *endLine = SM.getSpellingLineNumber(Loc: Body->getRBracLoc());
7537 *endColumn = SM.getSpellingColumnNumber(Loc: Body->getRBracLoc());
7538}
7539
7540CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
7541 unsigned PieceIndex) {
7542 RefNamePieces Pieces;
7543
7544 switch (C.kind) {
7545 case CXCursor_MemberRefExpr:
7546 if (const MemberExpr *E = dyn_cast<MemberExpr>(Val: getCursorExpr(Cursor: C)))
7547 Pieces = buildPieces(NameFlags, IsMemberRefExpr: true, NI: E->getMemberNameInfo(),
7548 QLoc: E->getQualifierLoc().getSourceRange());
7549 break;
7550
7551 case CXCursor_DeclRefExpr:
7552 if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(Val: getCursorExpr(Cursor: C))) {
7553 SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc());
7554 Pieces =
7555 buildPieces(NameFlags, IsMemberRefExpr: false, NI: E->getNameInfo(),
7556 QLoc: E->getQualifierLoc().getSourceRange(), TemplateArgsLoc: &TemplateArgLoc);
7557 }
7558 break;
7559
7560 case CXCursor_CallExpr:
7561 if (const CXXOperatorCallExpr *OCE =
7562 dyn_cast<CXXOperatorCallExpr>(Val: getCursorExpr(Cursor: C))) {
7563 const Expr *Callee = OCE->getCallee();
7564 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Val: Callee))
7565 Callee = ICE->getSubExpr();
7566
7567 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: Callee))
7568 Pieces = buildPieces(NameFlags, IsMemberRefExpr: false, NI: DRE->getNameInfo(),
7569 QLoc: DRE->getQualifierLoc().getSourceRange());
7570 }
7571 break;
7572
7573 default:
7574 break;
7575 }
7576
7577 if (Pieces.empty()) {
7578 if (PieceIndex == 0)
7579 return clang_getCursorExtent(C);
7580 } else if (PieceIndex < Pieces.size()) {
7581 SourceRange R = Pieces[PieceIndex];
7582 if (R.isValid())
7583 return cxloc::translateSourceRange(Context&: getCursorContext(Cursor: C), R);
7584 }
7585
7586 return clang_getNullRange();
7587}
7588
7589void clang_enableStackTraces(void) {
7590 // FIXME: Provide an argv0 here so we can find llvm-symbolizer.
7591 llvm::sys::PrintStackTraceOnErrorSignal(Argv0: StringRef());
7592}
7593
7594void clang_executeOnThread(void (*fn)(void *), void *user_data,
7595 unsigned stack_size) {
7596 llvm::thread Thread(stack_size == 0 ? clang::DesiredStackSize
7597 : std::optional<unsigned>(stack_size),
7598 fn, user_data);
7599 Thread.join();
7600}
7601
7602//===----------------------------------------------------------------------===//
7603// Token-based Operations.
7604//===----------------------------------------------------------------------===//
7605
7606/* CXToken layout:
7607 * int_data[0]: a CXTokenKind
7608 * int_data[1]: starting token location
7609 * int_data[2]: token length
7610 * int_data[3]: reserved
7611 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
7612 * otherwise unused.
7613 */
7614CXTokenKind clang_getTokenKind(CXToken CXTok) {
7615 return static_cast<CXTokenKind>(CXTok.int_data[0]);
7616}
7617
7618CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
7619 switch (clang_getTokenKind(CXTok)) {
7620 case CXToken_Identifier:
7621 case CXToken_Keyword:
7622 // We know we have an IdentifierInfo*, so use that.
7623 return cxstring::createRef(
7624 String: static_cast<IdentifierInfo *>(CXTok.ptr_data)->getNameStart());
7625
7626 case CXToken_Literal: {
7627 // We have stashed the starting pointer in the ptr_data field. Use it.
7628 const char *Text = static_cast<const char *>(CXTok.ptr_data);
7629 return cxstring::createDup(String: StringRef(Text, CXTok.int_data[2]));
7630 }
7631
7632 case CXToken_Punctuation:
7633 case CXToken_Comment:
7634 break;
7635 }
7636
7637 if (isNotUsableTU(TU)) {
7638 LOG_BAD_TU(TU);
7639 return cxstring::createEmpty();
7640 }
7641
7642 // We have to find the starting buffer pointer the hard way, by
7643 // deconstructing the source location.
7644 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
7645 if (!CXXUnit)
7646 return cxstring::createEmpty();
7647
7648 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: CXTok.int_data[1]);
7649 FileIDAndOffset LocInfo =
7650 CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
7651 bool Invalid = false;
7652 StringRef Buffer =
7653 CXXUnit->getSourceManager().getBufferData(FID: LocInfo.first, Invalid: &Invalid);
7654 if (Invalid)
7655 return cxstring::createEmpty();
7656
7657 return cxstring::createDup(String: Buffer.substr(Start: LocInfo.second, N: CXTok.int_data[2]));
7658}
7659
7660CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
7661 if (isNotUsableTU(TU)) {
7662 LOG_BAD_TU(TU);
7663 return clang_getNullLocation();
7664 }
7665
7666 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
7667 if (!CXXUnit)
7668 return clang_getNullLocation();
7669
7670 return cxloc::translateSourceLocation(
7671 Context&: CXXUnit->getASTContext(),
7672 Loc: SourceLocation::getFromRawEncoding(Encoding: CXTok.int_data[1]));
7673}
7674
7675CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
7676 if (isNotUsableTU(TU)) {
7677 LOG_BAD_TU(TU);
7678 return clang_getNullRange();
7679 }
7680
7681 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
7682 if (!CXXUnit)
7683 return clang_getNullRange();
7684
7685 return cxloc::translateSourceRange(
7686 Context&: CXXUnit->getASTContext(),
7687 R: SourceLocation::getFromRawEncoding(Encoding: CXTok.int_data[1]));
7688}
7689
7690static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
7691 SmallVectorImpl<CXToken> &CXTokens) {
7692 SourceManager &SourceMgr = CXXUnit->getSourceManager();
7693 FileIDAndOffset BeginLocInfo =
7694 SourceMgr.getDecomposedSpellingLoc(Loc: Range.getBegin());
7695 FileIDAndOffset EndLocInfo =
7696 SourceMgr.getDecomposedSpellingLoc(Loc: Range.getEnd());
7697
7698 // Cannot tokenize across files.
7699 if (BeginLocInfo.first != EndLocInfo.first)
7700 return;
7701
7702 // Create a lexer
7703 bool Invalid = false;
7704 StringRef Buffer = SourceMgr.getBufferData(FID: BeginLocInfo.first, Invalid: &Invalid);
7705 if (Invalid)
7706 return;
7707
7708 Lexer Lex(SourceMgr.getLocForStartOfFile(FID: BeginLocInfo.first),
7709 CXXUnit->getASTContext().getLangOpts(), Buffer.begin(),
7710 Buffer.data() + BeginLocInfo.second, Buffer.end());
7711 Lex.SetCommentRetentionState(true);
7712
7713 // Lex tokens until we hit the end of the range.
7714 const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
7715 Token Tok;
7716 bool previousWasAt = false;
7717 do {
7718 // Lex the next token
7719 Lex.LexFromRawLexer(Result&: Tok);
7720 if (Tok.is(K: tok::eof))
7721 break;
7722
7723 // Initialize the CXToken.
7724 CXToken CXTok;
7725
7726 // - Common fields
7727 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
7728 CXTok.int_data[2] = Tok.getLength();
7729 CXTok.int_data[3] = 0;
7730
7731 // - Kind-specific fields
7732 if (Tok.isLiteral()) {
7733 CXTok.int_data[0] = CXToken_Literal;
7734 CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData());
7735 } else if (Tok.is(K: tok::raw_identifier)) {
7736 // Lookup the identifier to determine whether we have a keyword.
7737 IdentifierInfo *II = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Identifier&: Tok);
7738
7739 if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
7740 CXTok.int_data[0] = CXToken_Keyword;
7741 } else {
7742 CXTok.int_data[0] =
7743 Tok.is(K: tok::identifier) ? CXToken_Identifier : CXToken_Keyword;
7744 }
7745 CXTok.ptr_data = II;
7746 } else if (Tok.is(K: tok::comment)) {
7747 CXTok.int_data[0] = CXToken_Comment;
7748 CXTok.ptr_data = nullptr;
7749 } else {
7750 CXTok.int_data[0] = CXToken_Punctuation;
7751 CXTok.ptr_data = nullptr;
7752 }
7753 CXTokens.push_back(Elt: CXTok);
7754 previousWasAt = Tok.is(K: tok::at);
7755 } while (Lex.getBufferLocation() < EffectiveBufferEnd);
7756}
7757
7758CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) {
7759 LOG_FUNC_SECTION { *Log << TU << ' ' << Location; }
7760
7761 if (isNotUsableTU(TU)) {
7762 LOG_BAD_TU(TU);
7763 return nullptr;
7764 }
7765
7766 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
7767 if (!CXXUnit)
7768 return nullptr;
7769
7770 SourceLocation Begin = cxloc::translateSourceLocation(L: Location);
7771 if (Begin.isInvalid())
7772 return nullptr;
7773 SourceManager &SM = CXXUnit->getSourceManager();
7774 FileIDAndOffset DecomposedEnd = SM.getDecomposedLoc(Loc: Begin);
7775 DecomposedEnd.second +=
7776 Lexer::MeasureTokenLength(Loc: Begin, SM, LangOpts: CXXUnit->getLangOpts());
7777
7778 SourceLocation End =
7779 SM.getComposedLoc(FID: DecomposedEnd.first, Offset: DecomposedEnd.second);
7780
7781 SmallVector<CXToken, 32> CXTokens;
7782 getTokens(CXXUnit, Range: SourceRange(Begin, End), CXTokens);
7783
7784 if (CXTokens.empty())
7785 return nullptr;
7786
7787 CXTokens.resize(N: 1);
7788 CXToken *Token = static_cast<CXToken *>(llvm::safe_malloc(Sz: sizeof(CXToken)));
7789
7790 memmove(dest: Token, src: CXTokens.data(), n: sizeof(CXToken));
7791 return Token;
7792}
7793
7794void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, CXToken **Tokens,
7795 unsigned *NumTokens) {
7796 LOG_FUNC_SECTION { *Log << TU << ' ' << Range; }
7797
7798 if (Tokens)
7799 *Tokens = nullptr;
7800 if (NumTokens)
7801 *NumTokens = 0;
7802
7803 if (isNotUsableTU(TU)) {
7804 LOG_BAD_TU(TU);
7805 return;
7806 }
7807
7808 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
7809 if (!CXXUnit || !Tokens || !NumTokens)
7810 return;
7811
7812 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
7813
7814 SourceRange R = cxloc::translateCXSourceRange(R: Range);
7815 if (R.isInvalid())
7816 return;
7817
7818 SmallVector<CXToken, 32> CXTokens;
7819 getTokens(CXXUnit, Range: R, CXTokens);
7820
7821 if (CXTokens.empty())
7822 return;
7823
7824 *Tokens = static_cast<CXToken *>(
7825 llvm::safe_malloc(Sz: sizeof(CXToken) * CXTokens.size()));
7826 memmove(dest: *Tokens, src: CXTokens.data(), n: sizeof(CXToken) * CXTokens.size());
7827 *NumTokens = CXTokens.size();
7828}
7829
7830void clang_disposeTokens(CXTranslationUnit TU, CXToken *Tokens,
7831 unsigned NumTokens) {
7832 free(ptr: Tokens);
7833}
7834
7835//===----------------------------------------------------------------------===//
7836// Token annotation APIs.
7837//===----------------------------------------------------------------------===//
7838
7839static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
7840 CXCursor parent,
7841 CXClientData client_data);
7842static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
7843 CXClientData client_data);
7844
7845namespace {
7846class AnnotateTokensWorker {
7847 CXToken *Tokens;
7848 CXCursor *Cursors;
7849 unsigned NumTokens;
7850 unsigned TokIdx;
7851 unsigned PreprocessingTokIdx;
7852 CursorVisitor AnnotateVis;
7853 SourceManager &SrcMgr;
7854 bool HasContextSensitiveKeywords;
7855
7856 struct PostChildrenAction {
7857 CXCursor cursor;
7858 enum Action { Invalid, Ignore, Postpone } action;
7859 };
7860 using PostChildrenActions = SmallVector<PostChildrenAction, 0>;
7861
7862 struct PostChildrenInfo {
7863 CXCursor Cursor;
7864 SourceRange CursorRange;
7865 unsigned BeforeReachingCursorIdx;
7866 unsigned BeforeChildrenTokenIdx;
7867 PostChildrenActions ChildActions;
7868 };
7869 SmallVector<PostChildrenInfo, 8> PostChildrenInfos;
7870
7871 CXToken &getTok(unsigned Idx) {
7872 assert(Idx < NumTokens);
7873 return Tokens[Idx];
7874 }
7875 const CXToken &getTok(unsigned Idx) const {
7876 assert(Idx < NumTokens);
7877 return Tokens[Idx];
7878 }
7879 bool MoreTokens() const { return TokIdx < NumTokens; }
7880 unsigned NextToken() const { return TokIdx; }
7881 void AdvanceToken() { ++TokIdx; }
7882 SourceLocation GetTokenLoc(unsigned tokI) {
7883 return SourceLocation::getFromRawEncoding(Encoding: getTok(Idx: tokI).int_data[1]);
7884 }
7885 bool isFunctionMacroToken(unsigned tokI) const {
7886 return getTok(Idx: tokI).int_data[3] != 0;
7887 }
7888 SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
7889 return SourceLocation::getFromRawEncoding(Encoding: getTok(Idx: tokI).int_data[3]);
7890 }
7891
7892 void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
7893 bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
7894 SourceRange);
7895
7896public:
7897 AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens,
7898 CXTranslationUnit TU, SourceRange RegionOfInterest)
7899 : Tokens(tokens), Cursors(cursors), NumTokens(numTokens), TokIdx(0),
7900 PreprocessingTokIdx(0),
7901 AnnotateVis(TU, AnnotateTokensVisitor, this,
7902 /*VisitPreprocessorLast=*/true,
7903 /*VisitIncludedEntities=*/false, RegionOfInterest,
7904 /*VisitDeclsOnly=*/false,
7905 AnnotateTokensPostChildrenVisitor),
7906 SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()),
7907 HasContextSensitiveKeywords(false) {}
7908
7909 void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(Cursor: C); }
7910 enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
7911 bool IsIgnoredChildCursor(CXCursor cursor) const;
7912 PostChildrenActions DetermineChildActions(CXCursor Cursor) const;
7913
7914 bool postVisitChildren(CXCursor cursor);
7915 void HandlePostPonedChildCursors(const PostChildrenInfo &Info);
7916 void HandlePostPonedChildCursor(CXCursor Cursor, unsigned StartTokenIndex);
7917
7918 void AnnotateTokens();
7919
7920 /// Determine whether the annotator saw any cursors that have
7921 /// context-sensitive keywords.
7922 bool hasContextSensitiveKeywords() const {
7923 return HasContextSensitiveKeywords;
7924 }
7925
7926 ~AnnotateTokensWorker() { assert(PostChildrenInfos.empty()); }
7927};
7928} // namespace
7929
7930void AnnotateTokensWorker::AnnotateTokens() {
7931 // Walk the AST within the region of interest, annotating tokens
7932 // along the way.
7933 AnnotateVis.visitFileRegion();
7934}
7935
7936bool AnnotateTokensWorker::IsIgnoredChildCursor(CXCursor cursor) const {
7937 if (PostChildrenInfos.empty())
7938 return false;
7939
7940 for (const auto &ChildAction : PostChildrenInfos.back().ChildActions) {
7941 if (ChildAction.cursor == cursor &&
7942 ChildAction.action == PostChildrenAction::Ignore) {
7943 return true;
7944 }
7945 }
7946
7947 return false;
7948}
7949
7950const CXXOperatorCallExpr *GetSubscriptOrCallOperator(CXCursor Cursor) {
7951 if (!clang_isExpression(K: Cursor.kind))
7952 return nullptr;
7953
7954 const Expr *E = getCursorExpr(Cursor);
7955 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E)) {
7956 const OverloadedOperatorKind Kind = OCE->getOperator();
7957 if (Kind == OO_Call || Kind == OO_Subscript)
7958 return OCE;
7959 }
7960
7961 return nullptr;
7962}
7963
7964AnnotateTokensWorker::PostChildrenActions
7965AnnotateTokensWorker::DetermineChildActions(CXCursor Cursor) const {
7966 PostChildrenActions actions;
7967
7968 // The DeclRefExpr of CXXOperatorCallExpr referring to the custom operator is
7969 // visited before the arguments to the operator call. For the Call and
7970 // Subscript operator the range of this DeclRefExpr includes the whole call
7971 // expression, so that all tokens in that range would be mapped to the
7972 // operator function, including the tokens of the arguments. To avoid that,
7973 // ensure to visit this DeclRefExpr as last node.
7974 if (const auto *OCE = GetSubscriptOrCallOperator(Cursor)) {
7975 const Expr *Callee = OCE->getCallee();
7976 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Val: Callee)) {
7977 const Expr *SubExpr = ICE->getSubExpr();
7978 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: SubExpr)) {
7979 const Decl *parentDecl = getCursorDecl(Cursor);
7980 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
7981
7982 // Visit the DeclRefExpr as last.
7983 CXCursor cxChild = MakeCXCursor(S: DRE, Parent: parentDecl, TU);
7984 actions.push_back(Elt: {.cursor: cxChild, .action: PostChildrenAction::Postpone});
7985
7986 // The parent of the DeclRefExpr, an ImplicitCastExpr, has an equally
7987 // wide range as the DeclRefExpr. We can skip visiting this entirely.
7988 cxChild = MakeCXCursor(S: ICE, Parent: parentDecl, TU);
7989 actions.push_back(Elt: {.cursor: cxChild, .action: PostChildrenAction::Ignore});
7990 }
7991 }
7992 }
7993
7994 return actions;
7995}
7996
7997static inline void updateCursorAnnotation(CXCursor &Cursor,
7998 const CXCursor &updateC) {
7999 if (clang_isInvalid(K: updateC.kind) || !clang_isInvalid(K: Cursor.kind))
8000 return;
8001 Cursor = updateC;
8002}
8003
8004/// It annotates and advances tokens with a cursor until the comparison
8005//// between the cursor location and the source range is the same as
8006/// \arg compResult.
8007///
8008/// Pass RangeBefore to annotate tokens with a cursor until a range is reached.
8009/// Pass RangeOverlap to annotate tokens inside a range.
8010void AnnotateTokensWorker::annotateAndAdvanceTokens(
8011 CXCursor updateC, RangeComparisonResult compResult, SourceRange range) {
8012 while (MoreTokens()) {
8013 const unsigned I = NextToken();
8014 if (isFunctionMacroToken(tokI: I))
8015 if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range))
8016 return;
8017
8018 SourceLocation TokLoc = GetTokenLoc(tokI: I);
8019 if (LocationCompare(SM&: SrcMgr, L: TokLoc, R: range) == compResult) {
8020 updateCursorAnnotation(Cursor&: Cursors[I], updateC);
8021 AdvanceToken();
8022 continue;
8023 }
8024 break;
8025 }
8026}
8027
8028/// Special annotation handling for macro argument tokens.
8029/// \returns true if it advanced beyond all macro tokens, false otherwise.
8030bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
8031 CXCursor updateC, RangeComparisonResult compResult, SourceRange range) {
8032 assert(MoreTokens());
8033 assert(isFunctionMacroToken(NextToken()) &&
8034 "Should be called only for macro arg tokens");
8035
8036 // This works differently than annotateAndAdvanceTokens; because expanded
8037 // macro arguments can have arbitrary translation-unit source order, we do not
8038 // advance the token index one by one until a token fails the range test.
8039 // We only advance once past all of the macro arg tokens if all of them
8040 // pass the range test. If one of them fails we keep the token index pointing
8041 // at the start of the macro arg tokens so that the failing token will be
8042 // annotated by a subsequent annotation try.
8043
8044 bool atLeastOneCompFail = false;
8045
8046 unsigned I = NextToken();
8047 for (; I < NumTokens && isFunctionMacroToken(tokI: I); ++I) {
8048 SourceLocation TokLoc = getFunctionMacroTokenLoc(tokI: I);
8049 if (TokLoc.isFileID())
8050 continue; // not macro arg token, it's parens or comma.
8051 if (LocationCompare(SM&: SrcMgr, L: TokLoc, R: range) == compResult) {
8052 if (clang_isInvalid(K: clang_getCursorKind(C: Cursors[I])))
8053 Cursors[I] = updateC;
8054 } else
8055 atLeastOneCompFail = true;
8056 }
8057
8058 if (atLeastOneCompFail)
8059 return false;
8060
8061 TokIdx = I; // All of the tokens were handled, advance beyond all of them.
8062 return true;
8063}
8064
8065enum CXChildVisitResult AnnotateTokensWorker::Visit(CXCursor cursor,
8066 CXCursor parent) {
8067 SourceRange cursorRange = getRawCursorExtent(C: cursor);
8068 if (cursorRange.isInvalid())
8069 return CXChildVisit_Recurse;
8070
8071 if (IsIgnoredChildCursor(cursor))
8072 return CXChildVisit_Continue;
8073
8074 if (!HasContextSensitiveKeywords) {
8075 // Objective-C properties can have context-sensitive keywords.
8076 if (cursor.kind == CXCursor_ObjCPropertyDecl) {
8077 if (const ObjCPropertyDecl *Property =
8078 dyn_cast_or_null<ObjCPropertyDecl>(Val: getCursorDecl(Cursor: cursor)))
8079 HasContextSensitiveKeywords =
8080 Property->getPropertyAttributesAsWritten() != 0;
8081 }
8082 // Objective-C methods can have context-sensitive keywords.
8083 else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl ||
8084 cursor.kind == CXCursor_ObjCClassMethodDecl) {
8085 if (const ObjCMethodDecl *Method =
8086 dyn_cast_or_null<ObjCMethodDecl>(Val: getCursorDecl(Cursor: cursor))) {
8087 if (Method->getObjCDeclQualifier())
8088 HasContextSensitiveKeywords = true;
8089 else {
8090 for (const auto *P : Method->parameters()) {
8091 if (P->getObjCDeclQualifier()) {
8092 HasContextSensitiveKeywords = true;
8093 break;
8094 }
8095 }
8096 }
8097 }
8098 }
8099 // C++ methods can have context-sensitive keywords.
8100 else if (cursor.kind == CXCursor_CXXMethod) {
8101 if (const CXXMethodDecl *Method =
8102 dyn_cast_or_null<CXXMethodDecl>(Val: getCursorDecl(Cursor: cursor))) {
8103 if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>())
8104 HasContextSensitiveKeywords = true;
8105 }
8106 }
8107 // C++ classes can have context-sensitive keywords.
8108 else if (cursor.kind == CXCursor_StructDecl ||
8109 cursor.kind == CXCursor_ClassDecl ||
8110 cursor.kind == CXCursor_ClassTemplate ||
8111 cursor.kind == CXCursor_ClassTemplatePartialSpecialization) {
8112 if (const Decl *D = getCursorDecl(Cursor: cursor))
8113 if (D->hasAttr<FinalAttr>())
8114 HasContextSensitiveKeywords = true;
8115 }
8116 }
8117
8118 // Don't override a property annotation with its getter/setter method.
8119 if (cursor.kind == CXCursor_ObjCInstanceMethodDecl &&
8120 parent.kind == CXCursor_ObjCPropertyDecl)
8121 return CXChildVisit_Continue;
8122
8123 if (clang_isPreprocessing(K: cursor.kind)) {
8124 // Items in the preprocessing record are kept separate from items in
8125 // declarations, so we keep a separate token index.
8126 unsigned SavedTokIdx = TokIdx;
8127 TokIdx = PreprocessingTokIdx;
8128
8129 // Skip tokens up until we catch up to the beginning of the preprocessing
8130 // entry.
8131 while (MoreTokens()) {
8132 const unsigned I = NextToken();
8133 SourceLocation TokLoc = GetTokenLoc(tokI: I);
8134 switch (LocationCompare(SM&: SrcMgr, L: TokLoc, R: cursorRange)) {
8135 case RangeBefore:
8136 AdvanceToken();
8137 continue;
8138 case RangeAfter:
8139 case RangeOverlap:
8140 break;
8141 }
8142 break;
8143 }
8144
8145 // Look at all of the tokens within this range.
8146 while (MoreTokens()) {
8147 const unsigned I = NextToken();
8148 SourceLocation TokLoc = GetTokenLoc(tokI: I);
8149 switch (LocationCompare(SM&: SrcMgr, L: TokLoc, R: cursorRange)) {
8150 case RangeBefore:
8151 llvm_unreachable("Infeasible");
8152 case RangeAfter:
8153 break;
8154 case RangeOverlap:
8155 // For macro expansions, just note where the beginning of the macro
8156 // expansion occurs.
8157 if (cursor.kind == CXCursor_MacroExpansion) {
8158 if (TokLoc == cursorRange.getBegin())
8159 Cursors[I] = cursor;
8160 AdvanceToken();
8161 break;
8162 }
8163 // We may have already annotated macro names inside macro definitions.
8164 if (Cursors[I].kind != CXCursor_MacroExpansion)
8165 Cursors[I] = cursor;
8166 AdvanceToken();
8167 continue;
8168 }
8169 break;
8170 }
8171
8172 // Save the preprocessing token index; restore the non-preprocessing
8173 // token index.
8174 PreprocessingTokIdx = TokIdx;
8175 TokIdx = SavedTokIdx;
8176 return CXChildVisit_Recurse;
8177 }
8178
8179 if (cursorRange.isInvalid())
8180 return CXChildVisit_Continue;
8181
8182 unsigned BeforeReachingCursorIdx = NextToken();
8183 const enum CXCursorKind cursorK = clang_getCursorKind(C: cursor);
8184 const enum CXCursorKind K = clang_getCursorKind(C: parent);
8185 const CXCursor updateC =
8186 (clang_isInvalid(K) || K == CXCursor_TranslationUnit ||
8187 // Attributes are annotated out-of-order, skip tokens until we reach it.
8188 clang_isAttribute(K: cursor.kind))
8189 ? clang_getNullCursor()
8190 : parent;
8191
8192 annotateAndAdvanceTokens(updateC, compResult: RangeBefore, range: cursorRange);
8193
8194 // Avoid having the cursor of an expression "overwrite" the annotation of the
8195 // variable declaration that it belongs to.
8196 // This can happen for C++ constructor expressions whose range generally
8197 // include the variable declaration, e.g.:
8198 // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor.
8199 if (clang_isExpression(K: cursorK) && MoreTokens()) {
8200 const Expr *E = getCursorExpr(Cursor: cursor);
8201 if (const Decl *D = getCursorDecl(Cursor: cursor)) {
8202 const unsigned I = NextToken();
8203 if (E->getBeginLoc().isValid() && D->getLocation().isValid() &&
8204 E->getBeginLoc() == D->getLocation() &&
8205 E->getBeginLoc() == GetTokenLoc(tokI: I)) {
8206 updateCursorAnnotation(Cursor&: Cursors[I], updateC);
8207 AdvanceToken();
8208 }
8209 }
8210 }
8211
8212 // Before recursing into the children keep some state that we are going
8213 // to use in the AnnotateTokensWorker::postVisitChildren callback to do some
8214 // extra work after the child nodes are visited.
8215 // Note that we don't call VisitChildren here to avoid traversing statements
8216 // code-recursively which can blow the stack.
8217
8218 PostChildrenInfo Info;
8219 Info.Cursor = cursor;
8220 Info.CursorRange = cursorRange;
8221 Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx;
8222 Info.BeforeChildrenTokenIdx = NextToken();
8223 Info.ChildActions = DetermineChildActions(Cursor: cursor);
8224 PostChildrenInfos.push_back(Elt: Info);
8225
8226 return CXChildVisit_Recurse;
8227}
8228
8229bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) {
8230 if (PostChildrenInfos.empty())
8231 return false;
8232 const PostChildrenInfo &Info = PostChildrenInfos.back();
8233 if (!clang_equalCursors(X: Info.Cursor, Y: cursor))
8234 return false;
8235
8236 HandlePostPonedChildCursors(Info);
8237
8238 const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx;
8239 const unsigned AfterChildren = NextToken();
8240 SourceRange cursorRange = Info.CursorRange;
8241
8242 // Scan the tokens that are at the end of the cursor, but are not captured
8243 // but the child cursors.
8244 annotateAndAdvanceTokens(updateC: cursor, compResult: RangeOverlap, range: cursorRange);
8245
8246 // Scan the tokens that are at the beginning of the cursor, but are not
8247 // capture by the child cursors.
8248 for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
8249 if (!clang_isInvalid(K: clang_getCursorKind(C: Cursors[I])))
8250 break;
8251
8252 Cursors[I] = cursor;
8253 }
8254
8255 // Attributes are annotated out-of-order, rewind TokIdx to when we first
8256 // encountered the attribute cursor.
8257 if (clang_isAttribute(K: cursor.kind))
8258 TokIdx = Info.BeforeReachingCursorIdx;
8259
8260 PostChildrenInfos.pop_back();
8261 return false;
8262}
8263
8264void AnnotateTokensWorker::HandlePostPonedChildCursors(
8265 const PostChildrenInfo &Info) {
8266 for (const auto &ChildAction : Info.ChildActions) {
8267 if (ChildAction.action == PostChildrenAction::Postpone) {
8268 HandlePostPonedChildCursor(Cursor: ChildAction.cursor,
8269 StartTokenIndex: Info.BeforeChildrenTokenIdx);
8270 }
8271 }
8272}
8273
8274void AnnotateTokensWorker::HandlePostPonedChildCursor(
8275 CXCursor Cursor, unsigned StartTokenIndex) {
8276 unsigned I = StartTokenIndex;
8277
8278 // The bracket tokens of a Call or Subscript operator are mapped to
8279 // CallExpr/CXXOperatorCallExpr because we skipped visiting the corresponding
8280 // DeclRefExpr. Remap these tokens to the DeclRefExpr cursors.
8281 for (unsigned RefNameRangeNr = 0; I < NumTokens; RefNameRangeNr++) {
8282 const CXSourceRange CXRefNameRange = clang_getCursorReferenceNameRange(
8283 C: Cursor, NameFlags: CXNameRange_WantQualifier, PieceIndex: RefNameRangeNr);
8284 if (clang_Range_isNull(range: CXRefNameRange))
8285 break; // All ranges handled.
8286
8287 SourceRange RefNameRange = cxloc::translateCXSourceRange(R: CXRefNameRange);
8288 while (I < NumTokens) {
8289 const SourceLocation TokenLocation = GetTokenLoc(tokI: I);
8290 if (!TokenLocation.isValid())
8291 break;
8292
8293 // Adapt the end range, because LocationCompare() reports
8294 // RangeOverlap even for the not-inclusive end location.
8295 const SourceLocation fixedEnd =
8296 RefNameRange.getEnd().getLocWithOffset(Offset: -1);
8297 RefNameRange = SourceRange(RefNameRange.getBegin(), fixedEnd);
8298
8299 const RangeComparisonResult ComparisonResult =
8300 LocationCompare(SM&: SrcMgr, L: TokenLocation, R: RefNameRange);
8301
8302 if (ComparisonResult == RangeOverlap) {
8303 Cursors[I++] = Cursor;
8304 } else if (ComparisonResult == RangeBefore) {
8305 ++I; // Not relevant token, check next one.
8306 } else if (ComparisonResult == RangeAfter) {
8307 break; // All tokens updated for current range, check next.
8308 }
8309 }
8310 }
8311}
8312
8313static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
8314 CXCursor parent,
8315 CXClientData client_data) {
8316 return static_cast<AnnotateTokensWorker *>(client_data)
8317 ->Visit(cursor, parent);
8318}
8319
8320static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor,
8321 CXClientData client_data) {
8322 return static_cast<AnnotateTokensWorker *>(client_data)
8323 ->postVisitChildren(cursor);
8324}
8325
8326namespace {
8327
8328/// Uses the macro expansions in the preprocessing record to find
8329/// and mark tokens that are macro arguments. This info is used by the
8330/// AnnotateTokensWorker.
8331class MarkMacroArgTokensVisitor {
8332 SourceManager &SM;
8333 CXToken *Tokens;
8334 unsigned NumTokens;
8335 unsigned CurIdx;
8336
8337public:
8338 MarkMacroArgTokensVisitor(SourceManager &SM, CXToken *tokens,
8339 unsigned numTokens)
8340 : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) {}
8341
8342 CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {
8343 if (cursor.kind != CXCursor_MacroExpansion)
8344 return CXChildVisit_Continue;
8345
8346 SourceRange macroRange = getCursorMacroExpansion(C: cursor).getSourceRange();
8347 if (macroRange.getBegin() == macroRange.getEnd())
8348 return CXChildVisit_Continue; // it's not a function macro.
8349
8350 for (; CurIdx < NumTokens; ++CurIdx) {
8351 if (!SM.isBeforeInTranslationUnit(LHS: getTokenLoc(tokI: CurIdx),
8352 RHS: macroRange.getBegin()))
8353 break;
8354 }
8355
8356 if (CurIdx == NumTokens)
8357 return CXChildVisit_Break;
8358
8359 for (; CurIdx < NumTokens; ++CurIdx) {
8360 SourceLocation tokLoc = getTokenLoc(tokI: CurIdx);
8361 if (!SM.isBeforeInTranslationUnit(LHS: tokLoc, RHS: macroRange.getEnd()))
8362 break;
8363
8364 setFunctionMacroTokenLoc(tokI: CurIdx, loc: SM.getMacroArgExpandedLocation(Loc: tokLoc));
8365 }
8366
8367 if (CurIdx == NumTokens)
8368 return CXChildVisit_Break;
8369
8370 return CXChildVisit_Continue;
8371 }
8372
8373private:
8374 CXToken &getTok(unsigned Idx) {
8375 assert(Idx < NumTokens);
8376 return Tokens[Idx];
8377 }
8378 const CXToken &getTok(unsigned Idx) const {
8379 assert(Idx < NumTokens);
8380 return Tokens[Idx];
8381 }
8382
8383 SourceLocation getTokenLoc(unsigned tokI) {
8384 return SourceLocation::getFromRawEncoding(Encoding: getTok(Idx: tokI).int_data[1]);
8385 }
8386
8387 void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
8388 // The third field is reserved and currently not used. Use it here
8389 // to mark macro arg expanded tokens with their expanded locations.
8390 getTok(Idx: tokI).int_data[3] = loc.getRawEncoding();
8391 }
8392};
8393
8394} // end anonymous namespace
8395
8396static CXChildVisitResult
8397MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
8398 CXClientData client_data) {
8399 return static_cast<MarkMacroArgTokensVisitor *>(client_data)
8400 ->visit(cursor, parent);
8401}
8402
8403/// Used by \c annotatePreprocessorTokens.
8404/// \returns true if lexing was finished, false otherwise.
8405static bool lexNext(Lexer &Lex, Token &Tok, unsigned &NextIdx,
8406 unsigned NumTokens) {
8407 if (NextIdx >= NumTokens)
8408 return true;
8409
8410 ++NextIdx;
8411 Lex.LexFromRawLexer(Result&: Tok);
8412 return Tok.is(K: tok::eof);
8413}
8414
8415static void annotatePreprocessorTokens(CXTranslationUnit TU,
8416 SourceRange RegionOfInterest,
8417 CXCursor *Cursors, CXToken *Tokens,
8418 unsigned NumTokens) {
8419 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
8420
8421 Preprocessor &PP = CXXUnit->getPreprocessor();
8422 SourceManager &SourceMgr = CXXUnit->getSourceManager();
8423 FileIDAndOffset BeginLocInfo =
8424 SourceMgr.getDecomposedSpellingLoc(Loc: RegionOfInterest.getBegin());
8425 FileIDAndOffset EndLocInfo =
8426 SourceMgr.getDecomposedSpellingLoc(Loc: RegionOfInterest.getEnd());
8427
8428 if (BeginLocInfo.first != EndLocInfo.first)
8429 return;
8430
8431 StringRef Buffer;
8432 bool Invalid = false;
8433 Buffer = SourceMgr.getBufferData(FID: BeginLocInfo.first, Invalid: &Invalid);
8434 if (Buffer.empty() || Invalid)
8435 return;
8436
8437 Lexer Lex(SourceMgr.getLocForStartOfFile(FID: BeginLocInfo.first),
8438 CXXUnit->getASTContext().getLangOpts(), Buffer.begin(),
8439 Buffer.data() + BeginLocInfo.second, Buffer.end());
8440 Lex.SetCommentRetentionState(true);
8441
8442 unsigned NextIdx = 0;
8443 // Lex tokens in raw mode until we hit the end of the range, to avoid
8444 // entering #includes or expanding macros.
8445 while (true) {
8446 Token Tok;
8447 if (lexNext(Lex, Tok, NextIdx, NumTokens))
8448 break;
8449 unsigned TokIdx = NextIdx - 1;
8450 assert(Tok.getLocation() ==
8451 SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1]));
8452
8453 reprocess:
8454 if (Tok.is(K: tok::hash) && Tok.isAtStartOfLine()) {
8455 // We have found a preprocessing directive. Annotate the tokens
8456 // appropriately.
8457 //
8458 // FIXME: Some simple tests here could identify macro definitions and
8459 // #undefs, to provide specific cursor kinds for those.
8460
8461 SourceLocation BeginLoc = Tok.getLocation();
8462 if (lexNext(Lex, Tok, NextIdx, NumTokens))
8463 break;
8464
8465 MacroInfo *MI = nullptr;
8466 if (Tok.is(K: tok::raw_identifier) && Tok.getRawIdentifier() == "define") {
8467 if (lexNext(Lex, Tok, NextIdx, NumTokens))
8468 break;
8469
8470 if (Tok.is(K: tok::raw_identifier)) {
8471 IdentifierInfo &II =
8472 PP.getIdentifierTable().get(Name: Tok.getRawIdentifier());
8473 SourceLocation MappedTokLoc =
8474 CXXUnit->mapLocationToPreamble(Loc: Tok.getLocation());
8475 MI = getMacroInfo(II, MacroDefLoc: MappedTokLoc, TU);
8476 }
8477 }
8478
8479 bool finished = false;
8480 do {
8481 if (lexNext(Lex, Tok, NextIdx, NumTokens)) {
8482 finished = true;
8483 break;
8484 }
8485 // If we are in a macro definition, check if the token was ever a
8486 // macro name and annotate it if that's the case.
8487 if (MI) {
8488 SourceLocation SaveLoc = Tok.getLocation();
8489 Tok.setLocation(CXXUnit->mapLocationToPreamble(Loc: SaveLoc));
8490 MacroDefinitionRecord *MacroDef =
8491 checkForMacroInMacroDefinition(MI, Tok, TU);
8492 Tok.setLocation(SaveLoc);
8493 if (MacroDef)
8494 Cursors[NextIdx - 1] =
8495 MakeMacroExpansionCursor(MacroDef, Loc: Tok.getLocation(), TU);
8496 }
8497 } while (!Tok.isAtStartOfLine());
8498
8499 unsigned LastIdx = finished ? NextIdx - 1 : NextIdx - 2;
8500 assert(TokIdx <= LastIdx);
8501 SourceLocation EndLoc =
8502 SourceLocation::getFromRawEncoding(Encoding: Tokens[LastIdx].int_data[1]);
8503 CXCursor Cursor =
8504 MakePreprocessingDirectiveCursor(Range: SourceRange(BeginLoc, EndLoc), TU);
8505
8506 for (; TokIdx <= LastIdx; ++TokIdx)
8507 updateCursorAnnotation(Cursor&: Cursors[TokIdx], updateC: Cursor);
8508
8509 if (finished)
8510 break;
8511 goto reprocess;
8512 }
8513 }
8514}
8515
8516// This gets run a separate thread to avoid stack blowout.
8517static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit,
8518 CXToken *Tokens, unsigned NumTokens,
8519 CXCursor *Cursors) {
8520 CIndexer *CXXIdx = TU->CIdx;
8521 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForEditing))
8522 setThreadBackgroundPriority();
8523
8524 // Determine the region of interest, which contains all of the tokens.
8525 SourceRange RegionOfInterest;
8526 RegionOfInterest.setBegin(
8527 cxloc::translateSourceLocation(L: clang_getTokenLocation(TU, CXTok: Tokens[0])));
8528 RegionOfInterest.setEnd(cxloc::translateSourceLocation(
8529 L: clang_getTokenLocation(TU, CXTok: Tokens[NumTokens - 1])));
8530
8531 // Relex the tokens within the source range to look for preprocessing
8532 // directives.
8533 annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens);
8534
8535 // If begin location points inside a macro argument, set it to the expansion
8536 // location so we can have the full context when annotating semantically.
8537 {
8538 SourceManager &SM = CXXUnit->getSourceManager();
8539 SourceLocation Loc =
8540 SM.getMacroArgExpandedLocation(Loc: RegionOfInterest.getBegin());
8541 if (Loc.isMacroID())
8542 RegionOfInterest.setBegin(SM.getExpansionLoc(Loc));
8543 }
8544
8545 if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
8546 // Search and mark tokens that are macro argument expansions.
8547 MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), Tokens,
8548 NumTokens);
8549 CursorVisitor MacroArgMarker(
8550 TU, MarkMacroArgTokensVisitorDelegate, &Visitor,
8551 /*VisitPreprocessorLast=*/true,
8552 /*VisitIncludedEntities=*/false, RegionOfInterest);
8553 MacroArgMarker.visitPreprocessedEntitiesInRegion();
8554 }
8555
8556 // Annotate all of the source locations in the region of interest that map to
8557 // a specific cursor.
8558 AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest);
8559
8560 // FIXME: We use a ridiculous stack size here because the data-recursion
8561 // algorithm uses a large stack frame than the non-data recursive version,
8562 // and AnnotationTokensWorker currently transforms the data-recursion
8563 // algorithm back into a traditional recursion by explicitly calling
8564 // VisitChildren(). We will need to remove this explicit recursive call.
8565 W.AnnotateTokens();
8566
8567 // If we ran into any entities that involve context-sensitive keywords,
8568 // take another pass through the tokens to mark them as such.
8569 if (W.hasContextSensitiveKeywords()) {
8570 for (unsigned I = 0; I != NumTokens; ++I) {
8571 if (clang_getTokenKind(CXTok: Tokens[I]) != CXToken_Identifier)
8572 continue;
8573
8574 if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) {
8575 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
8576 if (const ObjCPropertyDecl *Property =
8577 dyn_cast_or_null<ObjCPropertyDecl>(Val: getCursorDecl(Cursor: Cursors[I]))) {
8578 if (Property->getPropertyAttributesAsWritten() != 0 &&
8579 llvm::StringSwitch<bool>(II->getName())
8580 .Case(S: "readonly", Value: true)
8581 .Case(S: "assign", Value: true)
8582 .Case(S: "unsafe_unretained", Value: true)
8583 .Case(S: "readwrite", Value: true)
8584 .Case(S: "retain", Value: true)
8585 .Case(S: "copy", Value: true)
8586 .Case(S: "nonatomic", Value: true)
8587 .Case(S: "atomic", Value: true)
8588 .Case(S: "getter", Value: true)
8589 .Case(S: "setter", Value: true)
8590 .Case(S: "strong", Value: true)
8591 .Case(S: "weak", Value: true)
8592 .Case(S: "class", Value: true)
8593 .Default(Value: false))
8594 Tokens[I].int_data[0] = CXToken_Keyword;
8595 }
8596 continue;
8597 }
8598
8599 if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl ||
8600 Cursors[I].kind == CXCursor_ObjCClassMethodDecl) {
8601 IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
8602 if (llvm::StringSwitch<bool>(II->getName())
8603 .Case(S: "in", Value: true)
8604 .Case(S: "out", Value: true)
8605 .Case(S: "inout", Value: true)
8606 .Case(S: "oneway", Value: true)
8607 .Case(S: "bycopy", Value: true)
8608 .Case(S: "byref", Value: true)
8609 .Default(Value: false))
8610 Tokens[I].int_data[0] = CXToken_Keyword;
8611 continue;
8612 }
8613
8614 if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
8615 Cursors[I].kind == CXCursor_CXXOverrideAttr) {
8616 Tokens[I].int_data[0] = CXToken_Keyword;
8617 continue;
8618 }
8619 }
8620 }
8621}
8622
8623void clang_annotateTokens(CXTranslationUnit TU, CXToken *Tokens,
8624 unsigned NumTokens, CXCursor *Cursors) {
8625 if (isNotUsableTU(TU)) {
8626 LOG_BAD_TU(TU);
8627 return;
8628 }
8629 if (NumTokens == 0 || !Tokens || !Cursors) {
8630 LOG_FUNC_SECTION { *Log << "<null input>"; }
8631 return;
8632 }
8633
8634 LOG_FUNC_SECTION {
8635 *Log << TU << ' ';
8636 CXSourceLocation bloc = clang_getTokenLocation(TU, CXTok: Tokens[0]);
8637 CXSourceLocation eloc = clang_getTokenLocation(TU, CXTok: Tokens[NumTokens - 1]);
8638 *Log << clang_getRange(begin: bloc, end: eloc);
8639 }
8640
8641 // Any token we don't specifically annotate will have a NULL cursor.
8642 CXCursor C = clang_getNullCursor();
8643 for (unsigned I = 0; I != NumTokens; ++I)
8644 Cursors[I] = C;
8645
8646 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
8647 if (!CXXUnit)
8648 return;
8649
8650 ASTUnit::ConcurrencyCheck Check(*CXXUnit);
8651
8652 auto AnnotateTokensImpl = [=]() {
8653 clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors);
8654 };
8655 llvm::CrashRecoveryContext CRC;
8656 if (!RunSafely(CRC, Fn: AnnotateTokensImpl, Size: GetSafetyThreadStackSize() * 2)) {
8657 fprintf(stderr, format: "libclang: crash detected while annotating tokens\n");
8658 }
8659}
8660
8661//===----------------------------------------------------------------------===//
8662// Operations for querying information of a GCC inline assembly block under a
8663// cursor.
8664//===----------------------------------------------------------------------===//
8665CXString clang_Cursor_getGCCAssemblyTemplate(CXCursor Cursor) {
8666 if (!clang_isStatement(K: Cursor.kind))
8667 return cxstring::createEmpty();
8668 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor))) {
8669 ASTContext const &C = getCursorContext(Cursor);
8670 std::string AsmTemplate = S->generateAsmString(C);
8671 return cxstring::createDup(String: AsmTemplate);
8672 }
8673 return cxstring::createEmpty();
8674}
8675
8676unsigned clang_Cursor_isGCCAssemblyHasGoto(CXCursor Cursor) {
8677 if (!clang_isStatement(K: Cursor.kind))
8678 return 0;
8679 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor)))
8680 return S->isAsmGoto();
8681 return 0;
8682}
8683
8684unsigned clang_Cursor_getGCCAssemblyNumOutputs(CXCursor Cursor) {
8685 if (!clang_isStatement(K: Cursor.kind))
8686 return 0;
8687 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor)))
8688 return S->getNumOutputs();
8689 return 0;
8690}
8691
8692unsigned clang_Cursor_getGCCAssemblyNumInputs(CXCursor Cursor) {
8693 if (!clang_isStatement(K: Cursor.kind))
8694 return 0;
8695 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor)))
8696 return S->getNumInputs();
8697 return 0;
8698}
8699
8700unsigned clang_Cursor_getGCCAssemblyInput(CXCursor Cursor, unsigned Index,
8701 CXString *Constraint,
8702 CXCursor *ExprCursor) {
8703 if (!clang_isStatement(K: Cursor.kind) || !Constraint || !ExprCursor)
8704 return 0;
8705 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor));
8706 S && Index < S->getNumInputs()) {
8707 *Constraint = cxstring::createDup(String: S->getInputConstraint(i: Index));
8708 *ExprCursor = MakeCXCursor(S: S->getInputExpr(i: Index), Parent: getCursorDecl(Cursor),
8709 TU: cxcursor::getCursorTU(Cursor));
8710 return 1;
8711 }
8712 return 0;
8713}
8714
8715unsigned clang_Cursor_getGCCAssemblyOutput(CXCursor Cursor, unsigned Index,
8716 CXString *Constraint,
8717 CXCursor *ExprCursor) {
8718 if (!clang_isStatement(K: Cursor.kind) || !Constraint || !ExprCursor)
8719 return 0;
8720 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor));
8721 S && Index < S->getNumOutputs()) {
8722 *Constraint = cxstring::createDup(String: S->getOutputConstraint(i: Index));
8723 *ExprCursor = MakeCXCursor(S: S->getOutputExpr(i: Index), Parent: getCursorDecl(Cursor),
8724 TU: cxcursor::getCursorTU(Cursor));
8725 return 1;
8726 }
8727 return 0;
8728}
8729
8730unsigned clang_Cursor_getGCCAssemblyNumClobbers(CXCursor Cursor) {
8731 if (!clang_isStatement(K: Cursor.kind))
8732 return 0;
8733 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor)))
8734 return S->getNumClobbers();
8735 return 0;
8736}
8737
8738CXString clang_Cursor_getGCCAssemblyClobber(CXCursor Cursor, unsigned Index) {
8739 if (!clang_isStatement(K: Cursor.kind))
8740 return cxstring::createEmpty();
8741 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor));
8742 S && Index < S->getNumClobbers())
8743 return cxstring::createDup(String: S->getClobber(i: Index));
8744 return cxstring::createEmpty();
8745}
8746
8747unsigned clang_Cursor_isGCCAssemblyVolatile(CXCursor Cursor) {
8748 if (!clang_isStatement(K: Cursor.kind))
8749 return 0;
8750 if (auto const *S = dyn_cast_or_null<GCCAsmStmt>(Val: getCursorStmt(Cursor)))
8751 return S->isVolatile();
8752 return 0;
8753}
8754
8755//===----------------------------------------------------------------------===//
8756// Operations for querying linkage of a cursor.
8757//===----------------------------------------------------------------------===//
8758
8759CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
8760 if (!clang_isDeclaration(K: cursor.kind))
8761 return CXLinkage_Invalid;
8762
8763 const Decl *D = cxcursor::getCursorDecl(Cursor: cursor);
8764 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Val: D))
8765 switch (ND->getLinkageInternal()) {
8766 case Linkage::Invalid:
8767 return CXLinkage_Invalid;
8768 case Linkage::None:
8769 case Linkage::VisibleNone:
8770 return CXLinkage_NoLinkage;
8771 case Linkage::Internal:
8772 return CXLinkage_Internal;
8773 case Linkage::UniqueExternal:
8774 return CXLinkage_UniqueExternal;
8775 case Linkage::Module:
8776 case Linkage::External:
8777 return CXLinkage_External;
8778 };
8779
8780 return CXLinkage_Invalid;
8781}
8782
8783//===----------------------------------------------------------------------===//
8784// Operations for querying visibility of a cursor.
8785//===----------------------------------------------------------------------===//
8786
8787CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) {
8788 if (!clang_isDeclaration(K: cursor.kind))
8789 return CXVisibility_Invalid;
8790
8791 const Decl *D = cxcursor::getCursorDecl(Cursor: cursor);
8792 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Val: D))
8793 switch (ND->getVisibility()) {
8794 case HiddenVisibility:
8795 return CXVisibility_Hidden;
8796 case ProtectedVisibility:
8797 return CXVisibility_Protected;
8798 case DefaultVisibility:
8799 return CXVisibility_Default;
8800 };
8801
8802 return CXVisibility_Invalid;
8803}
8804
8805//===----------------------------------------------------------------------===//
8806// Operations for querying language of a cursor.
8807//===----------------------------------------------------------------------===//
8808
8809static CXLanguageKind getDeclLanguage(const Decl *D) {
8810 if (!D)
8811 return CXLanguage_C;
8812
8813 switch (D->getKind()) {
8814 default:
8815 break;
8816 case Decl::ImplicitParam:
8817 case Decl::ObjCAtDefsField:
8818 case Decl::ObjCCategory:
8819 case Decl::ObjCCategoryImpl:
8820 case Decl::ObjCCompatibleAlias:
8821 case Decl::ObjCImplementation:
8822 case Decl::ObjCInterface:
8823 case Decl::ObjCIvar:
8824 case Decl::ObjCMethod:
8825 case Decl::ObjCProperty:
8826 case Decl::ObjCPropertyImpl:
8827 case Decl::ObjCProtocol:
8828 case Decl::ObjCTypeParam:
8829 return CXLanguage_ObjC;
8830 case Decl::CXXConstructor:
8831 case Decl::CXXConversion:
8832 case Decl::CXXDestructor:
8833 case Decl::CXXMethod:
8834 case Decl::CXXRecord:
8835 case Decl::ClassTemplate:
8836 case Decl::ClassTemplatePartialSpecialization:
8837 case Decl::ClassTemplateSpecialization:
8838 case Decl::Friend:
8839 case Decl::FriendTemplate:
8840 case Decl::FunctionTemplate:
8841 case Decl::LinkageSpec:
8842 case Decl::Namespace:
8843 case Decl::NamespaceAlias:
8844 case Decl::NonTypeTemplateParm:
8845 case Decl::StaticAssert:
8846 case Decl::ExplicitInstantiation:
8847 case Decl::TemplateTemplateParm:
8848 case Decl::TemplateTypeParm:
8849 case Decl::UnresolvedUsingTypename:
8850 case Decl::UnresolvedUsingValue:
8851 case Decl::Using:
8852 case Decl::UsingDirective:
8853 case Decl::UsingShadow:
8854 return CXLanguage_CPlusPlus;
8855 }
8856
8857 return CXLanguage_C;
8858}
8859
8860static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) {
8861 if (isa<FunctionDecl>(Val: D) && cast<FunctionDecl>(Val: D)->isDeleted())
8862 return CXAvailability_NotAvailable;
8863
8864 switch (D->getAvailability()) {
8865 case AR_Available:
8866 case AR_NotYetIntroduced:
8867 if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(Val: D))
8868 return getCursorAvailabilityForDecl(
8869 D: cast<Decl>(Val: EnumConst->getDeclContext()));
8870 return CXAvailability_Available;
8871
8872 case AR_Deprecated:
8873 return CXAvailability_Deprecated;
8874
8875 case AR_Unavailable:
8876 return CXAvailability_NotAvailable;
8877 }
8878
8879 llvm_unreachable("Unknown availability kind!");
8880}
8881
8882enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
8883 if (clang_isDeclaration(K: cursor.kind))
8884 if (const Decl *D = cxcursor::getCursorDecl(Cursor: cursor))
8885 return getCursorAvailabilityForDecl(D);
8886
8887 return CXAvailability_Available;
8888}
8889
8890static CXVersion convertVersion(VersionTuple In) {
8891 CXVersion Out = {.Major: -1, .Minor: -1, .Subminor: -1};
8892 if (In.empty())
8893 return Out;
8894
8895 Out.Major = In.getMajor();
8896
8897 std::optional<unsigned> Minor = In.getMinor();
8898 if (Minor)
8899 Out.Minor = *Minor;
8900 else
8901 return Out;
8902
8903 std::optional<unsigned> Subminor = In.getSubminor();
8904 if (Subminor)
8905 Out.Subminor = *Subminor;
8906
8907 return Out;
8908}
8909
8910static void getCursorPlatformAvailabilityForDecl(
8911 const Decl *D, int *always_deprecated, CXString *deprecated_message,
8912 int *always_unavailable, CXString *unavailable_message,
8913 SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) {
8914 bool HadAvailAttr = false;
8915 for (auto A : D->attrs()) {
8916 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(Val: A)) {
8917 HadAvailAttr = true;
8918 if (always_deprecated)
8919 *always_deprecated = 1;
8920 if (deprecated_message) {
8921 clang_disposeString(string: *deprecated_message);
8922 *deprecated_message = cxstring::createDup(String: Deprecated->getMessage());
8923 }
8924 continue;
8925 }
8926
8927 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(Val: A)) {
8928 HadAvailAttr = true;
8929 if (always_unavailable)
8930 *always_unavailable = 1;
8931 if (unavailable_message) {
8932 clang_disposeString(string: *unavailable_message);
8933 *unavailable_message = cxstring::createDup(String: Unavailable->getMessage());
8934 }
8935 continue;
8936 }
8937
8938 if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(Val: A)) {
8939 AvailabilityAttrs.push_back(Elt: Avail->getEffectiveAttr());
8940 HadAvailAttr = true;
8941 }
8942 }
8943
8944 if (!HadAvailAttr)
8945 if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(Val: D))
8946 return getCursorPlatformAvailabilityForDecl(
8947 D: cast<Decl>(Val: EnumConst->getDeclContext()), always_deprecated,
8948 deprecated_message, always_unavailable, unavailable_message,
8949 AvailabilityAttrs);
8950
8951 // If no availability attributes are found, inherit the attribute from the
8952 // containing decl or the class or category interface decl.
8953 if (AvailabilityAttrs.empty()) {
8954 const ObjCContainerDecl *CD = nullptr;
8955 const DeclContext *DC = D->getDeclContext();
8956
8957 if (auto *IMD = dyn_cast<ObjCImplementationDecl>(Val: D))
8958 CD = IMD->getClassInterface();
8959 else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Val: D))
8960 CD = CatD->getClassInterface();
8961 else if (auto *IMD = dyn_cast<ObjCCategoryImplDecl>(Val: D))
8962 CD = IMD->getCategoryDecl();
8963 else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: DC))
8964 CD = ID;
8965 else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Val: DC))
8966 CD = CatD;
8967 else if (auto *IMD = dyn_cast<ObjCImplementationDecl>(Val: DC))
8968 CD = IMD->getClassInterface();
8969 else if (auto *IMD = dyn_cast<ObjCCategoryImplDecl>(Val: DC))
8970 CD = IMD->getCategoryDecl();
8971 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(Val: DC))
8972 CD = PD;
8973
8974 if (CD)
8975 getCursorPlatformAvailabilityForDecl(
8976 D: CD, always_deprecated, deprecated_message, always_unavailable,
8977 unavailable_message, AvailabilityAttrs);
8978 return;
8979 }
8980
8981 llvm::sort(
8982 C&: AvailabilityAttrs, Comp: [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) {
8983 return LHS->getPlatform()->getName() < RHS->getPlatform()->getName();
8984 });
8985 ASTContext &Ctx = D->getASTContext();
8986 auto It = llvm::unique(
8987 R&: AvailabilityAttrs, P: [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) {
8988 if (LHS->getPlatform() != RHS->getPlatform())
8989 return false;
8990
8991 if (LHS->getIntroduced() == RHS->getIntroduced() &&
8992 LHS->getDeprecated() == RHS->getDeprecated() &&
8993 LHS->getObsoleted() == RHS->getObsoleted() &&
8994 LHS->getMessage() == RHS->getMessage() &&
8995 LHS->getReplacement() == RHS->getReplacement())
8996 return true;
8997
8998 if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) ||
8999 (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) ||
9000 (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()))
9001 return false;
9002
9003 if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty())
9004 LHS->setIntroduced(C&: Ctx, V: RHS->getIntroduced());
9005
9006 if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) {
9007 LHS->setDeprecated(C&: Ctx, V: RHS->getDeprecated());
9008 if (LHS->getMessage().empty())
9009 LHS->setMessage(C&: Ctx, S: RHS->getMessage());
9010 if (LHS->getReplacement().empty())
9011 LHS->setReplacement(C&: Ctx, S: RHS->getReplacement());
9012 }
9013
9014 if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) {
9015 LHS->setObsoleted(C&: Ctx, V: RHS->getObsoleted());
9016 if (LHS->getMessage().empty())
9017 LHS->setMessage(C&: Ctx, S: RHS->getMessage());
9018 if (LHS->getReplacement().empty())
9019 LHS->setReplacement(C&: Ctx, S: RHS->getReplacement());
9020 }
9021
9022 return true;
9023 });
9024 AvailabilityAttrs.erase(CS: It, CE: AvailabilityAttrs.end());
9025}
9026
9027int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated,
9028 CXString *deprecated_message,
9029 int *always_unavailable,
9030 CXString *unavailable_message,
9031 CXPlatformAvailability *availability,
9032 int availability_size) {
9033 if (always_deprecated)
9034 *always_deprecated = 0;
9035 if (deprecated_message)
9036 *deprecated_message = cxstring::createEmpty();
9037 if (always_unavailable)
9038 *always_unavailable = 0;
9039 if (unavailable_message)
9040 *unavailable_message = cxstring::createEmpty();
9041
9042 if (!clang_isDeclaration(K: cursor.kind))
9043 return 0;
9044
9045 const Decl *D = cxcursor::getCursorDecl(Cursor: cursor);
9046 if (!D)
9047 return 0;
9048
9049 SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs;
9050 getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message,
9051 always_unavailable, unavailable_message,
9052 AvailabilityAttrs);
9053 for (const auto &Avail : llvm::enumerate(
9054 First: llvm::ArrayRef(AvailabilityAttrs).take_front(N: availability_size))) {
9055 availability[Avail.index()].Platform =
9056 cxstring::createDup(String: Avail.value()->getPlatform()->getName());
9057 availability[Avail.index()].Introduced =
9058 convertVersion(In: Avail.value()->getIntroduced());
9059 availability[Avail.index()].Deprecated =
9060 convertVersion(In: Avail.value()->getDeprecated());
9061 availability[Avail.index()].Obsoleted =
9062 convertVersion(In: Avail.value()->getObsoleted());
9063 availability[Avail.index()].Unavailable = Avail.value()->getUnavailable();
9064 availability[Avail.index()].Message =
9065 cxstring::createDup(String: Avail.value()->getMessage());
9066 }
9067
9068 return AvailabilityAttrs.size();
9069}
9070
9071void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) {
9072 clang_disposeString(string: availability->Platform);
9073 clang_disposeString(string: availability->Message);
9074}
9075
9076CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
9077 if (clang_isDeclaration(K: cursor.kind))
9078 return getDeclLanguage(D: cxcursor::getCursorDecl(Cursor: cursor));
9079
9080 return CXLanguage_Invalid;
9081}
9082
9083CXTLSKind clang_getCursorTLSKind(CXCursor cursor) {
9084 const Decl *D = cxcursor::getCursorDecl(Cursor: cursor);
9085 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) {
9086 switch (VD->getTLSKind()) {
9087 case VarDecl::TLS_None:
9088 return CXTLS_None;
9089 case VarDecl::TLS_Dynamic:
9090 return CXTLS_Dynamic;
9091 case VarDecl::TLS_Static:
9092 return CXTLS_Static;
9093 }
9094 }
9095
9096 return CXTLS_None;
9097}
9098
9099/// If the given cursor is the "templated" declaration
9100/// describing a class or function template, return the class or
9101/// function template.
9102static const Decl *maybeGetTemplateCursor(const Decl *D) {
9103 if (!D)
9104 return nullptr;
9105
9106 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D))
9107 if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
9108 return FunTmpl;
9109
9110 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Val: D))
9111 if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
9112 return ClassTmpl;
9113
9114 return D;
9115}
9116
9117enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) {
9118 StorageClass sc = SC_None;
9119 const Decl *D = getCursorDecl(Cursor: C);
9120 if (D) {
9121 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D)) {
9122 sc = FD->getStorageClass();
9123 } else if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) {
9124 sc = VD->getStorageClass();
9125 } else {
9126 return CX_SC_Invalid;
9127 }
9128 } else {
9129 return CX_SC_Invalid;
9130 }
9131 switch (sc) {
9132 case SC_None:
9133 return CX_SC_None;
9134 case SC_Extern:
9135 return CX_SC_Extern;
9136 case SC_Static:
9137 return CX_SC_Static;
9138 case SC_PrivateExtern:
9139 return CX_SC_PrivateExtern;
9140 case SC_Auto:
9141 return CX_SC_Auto;
9142 case SC_Register:
9143 return CX_SC_Register;
9144 }
9145 llvm_unreachable("Unhandled storage class!");
9146}
9147
9148CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
9149 if (clang_isDeclaration(K: cursor.kind)) {
9150 if (const Decl *D = getCursorDecl(Cursor: cursor)) {
9151 const DeclContext *DC = D->getDeclContext();
9152 if (!DC)
9153 return clang_getNullCursor();
9154
9155 return MakeCXCursor(D: maybeGetTemplateCursor(D: cast<Decl>(Val: DC)),
9156 TU: getCursorTU(Cursor: cursor));
9157 }
9158 }
9159
9160 if (clang_isStatement(K: cursor.kind) || clang_isExpression(K: cursor.kind)) {
9161 if (const Decl *D = getCursorDecl(Cursor: cursor))
9162 return MakeCXCursor(D, TU: getCursorTU(Cursor: cursor));
9163 }
9164
9165 return clang_getNullCursor();
9166}
9167
9168CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
9169 if (clang_isDeclaration(K: cursor.kind)) {
9170 if (const Decl *D = getCursorDecl(Cursor: cursor)) {
9171 const DeclContext *DC = D->getLexicalDeclContext();
9172 if (!DC)
9173 return clang_getNullCursor();
9174
9175 return MakeCXCursor(D: maybeGetTemplateCursor(D: cast<Decl>(Val: DC)),
9176 TU: getCursorTU(Cursor: cursor));
9177 }
9178 }
9179
9180 // FIXME: Note that we can't easily compute the lexical context of a
9181 // statement or expression, so we return nothing.
9182 return clang_getNullCursor();
9183}
9184
9185CXFile clang_getIncludedFile(CXCursor cursor) {
9186 if (cursor.kind != CXCursor_InclusionDirective)
9187 return nullptr;
9188
9189 const InclusionDirective *ID = getCursorInclusionDirective(C: cursor);
9190 return cxfile::makeCXFile(FE: ID->getFile());
9191}
9192
9193unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
9194 if (C.kind != CXCursor_ObjCPropertyDecl)
9195 return CXObjCPropertyAttr_noattr;
9196
9197 unsigned Result = CXObjCPropertyAttr_noattr;
9198 const auto *PD = cast<ObjCPropertyDecl>(Val: getCursorDecl(Cursor: C));
9199 ObjCPropertyAttribute::Kind Attr = PD->getPropertyAttributesAsWritten();
9200
9201#define SET_CXOBJCPROP_ATTR(A) \
9202 if (Attr & ObjCPropertyAttribute::kind_##A) \
9203 Result |= CXObjCPropertyAttr_##A
9204 SET_CXOBJCPROP_ATTR(readonly);
9205 SET_CXOBJCPROP_ATTR(getter);
9206 SET_CXOBJCPROP_ATTR(assign);
9207 SET_CXOBJCPROP_ATTR(readwrite);
9208 SET_CXOBJCPROP_ATTR(retain);
9209 SET_CXOBJCPROP_ATTR(copy);
9210 SET_CXOBJCPROP_ATTR(nonatomic);
9211 SET_CXOBJCPROP_ATTR(setter);
9212 SET_CXOBJCPROP_ATTR(atomic);
9213 SET_CXOBJCPROP_ATTR(weak);
9214 SET_CXOBJCPROP_ATTR(strong);
9215 SET_CXOBJCPROP_ATTR(unsafe_unretained);
9216 SET_CXOBJCPROP_ATTR(class);
9217#undef SET_CXOBJCPROP_ATTR
9218
9219 return Result;
9220}
9221
9222CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C) {
9223 if (C.kind != CXCursor_ObjCPropertyDecl)
9224 return cxstring::createNull();
9225
9226 const auto *PD = cast<ObjCPropertyDecl>(Val: getCursorDecl(Cursor: C));
9227 Selector sel = PD->getGetterName();
9228 if (sel.isNull())
9229 return cxstring::createNull();
9230
9231 return cxstring::createDup(String: sel.getAsString());
9232}
9233
9234CXString clang_Cursor_getObjCPropertySetterName(CXCursor C) {
9235 if (C.kind != CXCursor_ObjCPropertyDecl)
9236 return cxstring::createNull();
9237
9238 const auto *PD = cast<ObjCPropertyDecl>(Val: getCursorDecl(Cursor: C));
9239 Selector sel = PD->getSetterName();
9240 if (sel.isNull())
9241 return cxstring::createNull();
9242
9243 return cxstring::createDup(String: sel.getAsString());
9244}
9245
9246unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) {
9247 if (!clang_isDeclaration(K: C.kind))
9248 return CXObjCDeclQualifier_None;
9249
9250 Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None;
9251 const Decl *D = getCursorDecl(Cursor: C);
9252 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Val: D))
9253 QT = MD->getObjCDeclQualifier();
9254 else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(Val: D))
9255 QT = PD->getObjCDeclQualifier();
9256 if (QT == Decl::OBJC_TQ_None)
9257 return CXObjCDeclQualifier_None;
9258
9259 unsigned Result = CXObjCDeclQualifier_None;
9260 if (QT & Decl::OBJC_TQ_In)
9261 Result |= CXObjCDeclQualifier_In;
9262 if (QT & Decl::OBJC_TQ_Inout)
9263 Result |= CXObjCDeclQualifier_Inout;
9264 if (QT & Decl::OBJC_TQ_Out)
9265 Result |= CXObjCDeclQualifier_Out;
9266 if (QT & Decl::OBJC_TQ_Bycopy)
9267 Result |= CXObjCDeclQualifier_Bycopy;
9268 if (QT & Decl::OBJC_TQ_Byref)
9269 Result |= CXObjCDeclQualifier_Byref;
9270 if (QT & Decl::OBJC_TQ_Oneway)
9271 Result |= CXObjCDeclQualifier_Oneway;
9272
9273 return Result;
9274}
9275
9276unsigned clang_Cursor_isObjCOptional(CXCursor C) {
9277 if (!clang_isDeclaration(K: C.kind))
9278 return 0;
9279
9280 const Decl *D = getCursorDecl(Cursor: C);
9281 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(Val: D))
9282 return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional;
9283 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Val: D))
9284 return MD->getImplementationControl() ==
9285 ObjCImplementationControl::Optional;
9286
9287 return 0;
9288}
9289
9290unsigned clang_Cursor_isVariadic(CXCursor C) {
9291 if (!clang_isDeclaration(K: C.kind))
9292 return 0;
9293
9294 const Decl *D = getCursorDecl(Cursor: C);
9295 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D))
9296 return FD->isVariadic();
9297 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Val: D))
9298 return MD->isVariadic();
9299
9300 return 0;
9301}
9302
9303unsigned clang_Cursor_isExternalSymbol(CXCursor C, CXString *language,
9304 CXString *definedIn,
9305 unsigned *isGenerated) {
9306 if (!clang_isDeclaration(K: C.kind))
9307 return 0;
9308
9309 const Decl *D = getCursorDecl(Cursor: C);
9310
9311 if (auto *attr = D->getExternalSourceSymbolAttr()) {
9312 if (language)
9313 *language = cxstring::createDup(String: attr->getLanguage());
9314 if (definedIn)
9315 *definedIn = cxstring::createDup(String: attr->getDefinedIn());
9316 if (isGenerated)
9317 *isGenerated = attr->getGeneratedDeclaration();
9318 return 1;
9319 }
9320 return 0;
9321}
9322
9323enum CX_BinaryOperatorKind clang_Cursor_getBinaryOpcode(CXCursor C) {
9324 return static_cast<CX_BinaryOperatorKind>(
9325 clang_getCursorBinaryOperatorKind(cursor: C));
9326}
9327
9328CXString clang_Cursor_getBinaryOpcodeStr(enum CX_BinaryOperatorKind Op) {
9329 return clang_getBinaryOperatorKindSpelling(
9330 kind: static_cast<CXBinaryOperatorKind>(Op));
9331}
9332
9333static const RawComment *getCursorRawComment(CXCursor C) {
9334 if (!clang_isDeclaration(K: C.kind) && C.kind != CXCursor_MacroDefinition)
9335 return nullptr;
9336 ASTContext &Context = getCursorContext(Cursor: C);
9337 if (clang_isDeclaration(K: C.kind))
9338 return Context.getRawCommentForAnyRedecl(Key: getCursorDecl(Cursor: C));
9339 if (C.kind == CXCursor_MacroDefinition) {
9340 const MacroDefinitionRecord *Def = getCursorMacroDefinition(C);
9341 if (!Def)
9342 return nullptr;
9343 Preprocessor &PP = getCursorASTUnit(Cursor: C)->getPreprocessor();
9344 // Walk the macro directive history to find the specific MacroInfo for
9345 // this cursor's definition. Looking up by name alone would always return
9346 // the latest definition, which is wrong for redefined macros.
9347 for (const MacroDirective *MD =
9348 PP.getLocalMacroDirectiveHistory(II: Def->getName());
9349 MD; MD = MD->getPrevious()) {
9350 const auto *DMD = dyn_cast<DefMacroDirective>(Val: MD);
9351 if (!DMD)
9352 continue;
9353 const MacroInfo *MI = DMD->getInfo();
9354 if (MI && MI->getDefinitionLoc() == Def->getLocation())
9355 return Context.getRawCommentForAnyRedecl(Key: MI);
9356 }
9357 }
9358 return nullptr;
9359}
9360
9361CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
9362 const RawComment *RC = getCursorRawComment(C);
9363 if (!RC)
9364 return clang_getNullRange();
9365
9366 ASTContext &Context = getCursorContext(Cursor: C);
9367 return cxloc::translateSourceRange(Context, R: RC->getSourceRange());
9368}
9369
9370CXString clang_Cursor_getRawCommentText(CXCursor C) {
9371 const RawComment *RC = getCursorRawComment(C);
9372 if (!RC)
9373 return cxstring::createNull();
9374
9375 ASTContext &Context = getCursorContext(Cursor: C);
9376 StringRef RawText = RC->getRawText(SourceMgr: Context.getSourceManager());
9377
9378 // Don't duplicate the string because RawText points directly into source
9379 // code.
9380 return cxstring::createRef(String: RawText);
9381}
9382
9383CXString clang_Cursor_getBriefCommentText(CXCursor C) {
9384 const RawComment *RC = getCursorRawComment(C);
9385 if (!RC)
9386 return cxstring::createNull();
9387
9388 const ASTContext &Context = getCursorContext(Cursor: C);
9389 StringRef BriefText = RC->getBriefText(Context);
9390
9391 // Don't duplicate the string because RawComment ensures that this memory
9392 // will not go away.
9393 return cxstring::createRef(String: BriefText);
9394}
9395
9396CXModule clang_Cursor_getModule(CXCursor C) {
9397 if (C.kind == CXCursor_ModuleImportDecl) {
9398 if (const ImportDecl *ImportD =
9399 dyn_cast_or_null<ImportDecl>(Val: getCursorDecl(Cursor: C)))
9400 return ImportD->getImportedModule();
9401 }
9402
9403 return nullptr;
9404}
9405
9406CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) {
9407 if (isNotUsableTU(TU)) {
9408 LOG_BAD_TU(TU);
9409 return nullptr;
9410 }
9411 if (!File)
9412 return nullptr;
9413 FileEntryRef FE = *cxfile::getFileEntryRef(File);
9414
9415 ASTUnit &Unit = *cxtu::getASTUnit(TU);
9416 HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo();
9417 ModuleMap::KnownHeader Header = HS.findModuleForHeader(File: FE);
9418
9419 return Header.getModule();
9420}
9421
9422CXFile clang_Module_getASTFile(CXModule CXMod) { return nullptr; }
9423
9424CXModule clang_Module_getParent(CXModule CXMod) {
9425 if (!CXMod)
9426 return nullptr;
9427 Module *Mod = static_cast<Module *>(CXMod);
9428 return Mod->Parent;
9429}
9430
9431CXString clang_Module_getName(CXModule CXMod) {
9432 if (!CXMod)
9433 return cxstring::createEmpty();
9434 Module *Mod = static_cast<Module *>(CXMod);
9435 return cxstring::createDup(String: Mod->Name);
9436}
9437
9438CXString clang_Module_getFullName(CXModule CXMod) {
9439 if (!CXMod)
9440 return cxstring::createEmpty();
9441 Module *Mod = static_cast<Module *>(CXMod);
9442 return cxstring::createDup(String: Mod->getFullModuleName());
9443}
9444
9445int clang_Module_isSystem(CXModule CXMod) {
9446 if (!CXMod)
9447 return 0;
9448 Module *Mod = static_cast<Module *>(CXMod);
9449 return Mod->IsSystem;
9450}
9451
9452unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
9453 CXModule CXMod) {
9454 if (isNotUsableTU(TU)) {
9455 LOG_BAD_TU(TU);
9456 return 0;
9457 }
9458 if (!CXMod)
9459 return 0;
9460 Module *Mod = static_cast<Module *>(CXMod);
9461 FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
9462 ArrayRef<FileEntryRef> TopHeaders = Mod->getTopHeaders(FileMgr);
9463 return TopHeaders.size();
9464}
9465
9466CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, CXModule CXMod,
9467 unsigned Index) {
9468 if (isNotUsableTU(TU)) {
9469 LOG_BAD_TU(TU);
9470 return nullptr;
9471 }
9472 if (!CXMod)
9473 return nullptr;
9474 Module *Mod = static_cast<Module *>(CXMod);
9475 FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
9476
9477 ArrayRef<FileEntryRef> TopHeaders = Mod->getTopHeaders(FileMgr);
9478 if (Index < TopHeaders.size())
9479 return cxfile::makeCXFile(FE: TopHeaders[Index]);
9480
9481 return nullptr;
9482}
9483
9484//===----------------------------------------------------------------------===//
9485// C++ AST instrospection.
9486//===----------------------------------------------------------------------===//
9487
9488unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) {
9489 if (!clang_isDeclaration(K: C.kind))
9490 return 0;
9491
9492 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9493 const CXXConstructorDecl *Constructor =
9494 D ? dyn_cast_or_null<CXXConstructorDecl>(Val: D->getAsFunction()) : nullptr;
9495 return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0;
9496}
9497
9498unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) {
9499 if (!clang_isDeclaration(K: C.kind))
9500 return 0;
9501
9502 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9503 const CXXConstructorDecl *Constructor =
9504 D ? dyn_cast_or_null<CXXConstructorDecl>(Val: D->getAsFunction()) : nullptr;
9505 return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0;
9506}
9507
9508unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) {
9509 if (!clang_isDeclaration(K: C.kind))
9510 return 0;
9511
9512 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9513 const CXXConstructorDecl *Constructor =
9514 D ? dyn_cast_or_null<CXXConstructorDecl>(Val: D->getAsFunction()) : nullptr;
9515 return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0;
9516}
9517
9518unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) {
9519 if (!clang_isDeclaration(K: C.kind))
9520 return 0;
9521
9522 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9523 const CXXConstructorDecl *Constructor =
9524 D ? dyn_cast_or_null<CXXConstructorDecl>(Val: D->getAsFunction()) : nullptr;
9525 // Passing 'false' excludes constructors marked 'explicit'.
9526 return (Constructor && Constructor->isConvertingConstructor(AllowExplicit: false)) ? 1 : 0;
9527}
9528
9529unsigned clang_CXXField_isMutable(CXCursor C) {
9530 if (!clang_isDeclaration(K: C.kind))
9531 return 0;
9532
9533 if (const auto D = cxcursor::getCursorDecl(Cursor: C))
9534 if (const auto FD = dyn_cast_or_null<FieldDecl>(Val: D))
9535 return FD->isMutable() ? 1 : 0;
9536 return 0;
9537}
9538
9539unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
9540 if (!clang_isDeclaration(K: C.kind))
9541 return 0;
9542
9543 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9544 const CXXMethodDecl *Method =
9545 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9546 return (Method && Method->isPureVirtual()) ? 1 : 0;
9547}
9548
9549unsigned clang_CXXMethod_isConst(CXCursor C) {
9550 if (!clang_isDeclaration(K: C.kind))
9551 return 0;
9552
9553 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9554 const CXXMethodDecl *Method =
9555 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9556 return (Method && Method->getMethodQualifiers().hasConst()) ? 1 : 0;
9557}
9558
9559unsigned clang_CXXMethod_isDefaulted(CXCursor C) {
9560 if (!clang_isDeclaration(K: C.kind))
9561 return 0;
9562
9563 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9564 const CXXMethodDecl *Method =
9565 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9566 return (Method && Method->isDefaulted()) ? 1 : 0;
9567}
9568
9569unsigned clang_CXXMethod_isDeleted(CXCursor C) {
9570 if (!clang_isDeclaration(K: C.kind))
9571 return 0;
9572
9573 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9574 const CXXMethodDecl *Method =
9575 D ? dyn_cast_if_present<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9576 return (Method && Method->isDeleted()) ? 1 : 0;
9577}
9578
9579unsigned clang_CXXMethod_isStatic(CXCursor C) {
9580 if (!clang_isDeclaration(K: C.kind))
9581 return 0;
9582
9583 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9584 const CXXMethodDecl *Method =
9585 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9586 return (Method && Method->isStatic()) ? 1 : 0;
9587}
9588
9589unsigned clang_CXXMethod_isVirtual(CXCursor C) {
9590 if (!clang_isDeclaration(K: C.kind))
9591 return 0;
9592
9593 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9594 const CXXMethodDecl *Method =
9595 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9596 return (Method && Method->isVirtual()) ? 1 : 0;
9597}
9598
9599unsigned clang_CXXMethod_isCopyAssignmentOperator(CXCursor C) {
9600 if (!clang_isDeclaration(K: C.kind))
9601 return 0;
9602
9603 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9604 const CXXMethodDecl *Method =
9605 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9606
9607 return (Method && Method->isCopyAssignmentOperator()) ? 1 : 0;
9608}
9609
9610unsigned clang_CXXMethod_isMoveAssignmentOperator(CXCursor C) {
9611 if (!clang_isDeclaration(K: C.kind))
9612 return 0;
9613
9614 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9615 const CXXMethodDecl *Method =
9616 D ? dyn_cast_or_null<CXXMethodDecl>(Val: D->getAsFunction()) : nullptr;
9617
9618 return (Method && Method->isMoveAssignmentOperator()) ? 1 : 0;
9619}
9620
9621unsigned clang_CXXMethod_isExplicit(CXCursor C) {
9622 if (!clang_isDeclaration(K: C.kind))
9623 return 0;
9624
9625 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9626 const FunctionDecl *FD = D->getAsFunction();
9627
9628 if (!FD)
9629 return 0;
9630
9631 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: FD))
9632 return Ctor->isExplicit();
9633
9634 if (const auto *Conv = dyn_cast<CXXConversionDecl>(Val: FD))
9635 return Conv->isExplicit();
9636
9637 return 0;
9638}
9639
9640unsigned clang_CXXRecord_isAbstract(CXCursor C) {
9641 if (!clang_isDeclaration(K: C.kind))
9642 return 0;
9643
9644 const auto *D = cxcursor::getCursorDecl(Cursor: C);
9645 const auto *RD = dyn_cast_or_null<CXXRecordDecl>(Val: D);
9646 if (RD)
9647 RD = RD->getDefinition();
9648 return (RD && RD->isAbstract()) ? 1 : 0;
9649}
9650
9651unsigned clang_EnumDecl_isScoped(CXCursor C) {
9652 if (!clang_isDeclaration(K: C.kind))
9653 return 0;
9654
9655 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
9656 auto *Enum = dyn_cast_or_null<EnumDecl>(Val: D);
9657 return (Enum && Enum->isScoped()) ? 1 : 0;
9658}
9659
9660//===----------------------------------------------------------------------===//
9661// Attribute introspection.
9662//===----------------------------------------------------------------------===//
9663
9664CXType clang_getIBOutletCollectionType(CXCursor C) {
9665 if (C.kind != CXCursor_IBOutletCollectionAttr)
9666 return cxtype::MakeCXType(T: QualType(), TU: cxcursor::getCursorTU(Cursor: C));
9667
9668 const IBOutletCollectionAttr *A =
9669 cast<IBOutletCollectionAttr>(Val: cxcursor::getCursorAttr(Cursor: C));
9670
9671 return cxtype::MakeCXType(T: A->getInterface(), TU: cxcursor::getCursorTU(Cursor: C));
9672}
9673
9674//===----------------------------------------------------------------------===//
9675// Inspecting memory usage.
9676//===----------------------------------------------------------------------===//
9677
9678typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
9679
9680static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
9681 enum CXTUResourceUsageKind k,
9682 unsigned long amount) {
9683 CXTUResourceUsageEntry entry = {.kind: k, .amount: amount};
9684 entries.push_back(x: entry);
9685}
9686
9687const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
9688 const char *str = "";
9689 switch (kind) {
9690 case CXTUResourceUsage_AST:
9691 str = "ASTContext: expressions, declarations, and types";
9692 break;
9693 case CXTUResourceUsage_Identifiers:
9694 str = "ASTContext: identifiers";
9695 break;
9696 case CXTUResourceUsage_Selectors:
9697 str = "ASTContext: selectors";
9698 break;
9699 case CXTUResourceUsage_GlobalCompletionResults:
9700 str = "Code completion: cached global results";
9701 break;
9702 case CXTUResourceUsage_SourceManagerContentCache:
9703 str = "SourceManager: content cache allocator";
9704 break;
9705 case CXTUResourceUsage_AST_SideTables:
9706 str = "ASTContext: side tables";
9707 break;
9708 case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
9709 str = "SourceManager: malloc'ed memory buffers";
9710 break;
9711 case CXTUResourceUsage_SourceManager_Membuffer_MMap:
9712 str = "SourceManager: mmap'ed memory buffers";
9713 break;
9714 case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
9715 str = "ExternalASTSource: malloc'ed memory buffers";
9716 break;
9717 case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
9718 str = "ExternalASTSource: mmap'ed memory buffers";
9719 break;
9720 case CXTUResourceUsage_Preprocessor:
9721 str = "Preprocessor: malloc'ed memory";
9722 break;
9723 case CXTUResourceUsage_PreprocessingRecord:
9724 str = "Preprocessor: PreprocessingRecord";
9725 break;
9726 case CXTUResourceUsage_SourceManager_DataStructures:
9727 str = "SourceManager: data structures and tables";
9728 break;
9729 case CXTUResourceUsage_Preprocessor_HeaderSearch:
9730 str = "Preprocessor: header search tables";
9731 break;
9732 }
9733 return str;
9734}
9735
9736CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
9737 if (isNotUsableTU(TU)) {
9738 LOG_BAD_TU(TU);
9739 CXTUResourceUsage usage = {.data: (void *)nullptr, .numEntries: 0, .entries: nullptr};
9740 return usage;
9741 }
9742
9743 ASTUnit *astUnit = cxtu::getASTUnit(TU);
9744 std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries());
9745 ASTContext &astContext = astUnit->getASTContext();
9746
9747 // How much memory is used by AST nodes and types?
9748 createCXTUResourceUsageEntry(
9749 entries&: *entries, k: CXTUResourceUsage_AST,
9750 amount: (unsigned long)astContext.getASTAllocatedMemory());
9751
9752 // How much memory is used by identifiers?
9753 createCXTUResourceUsageEntry(
9754 entries&: *entries, k: CXTUResourceUsage_Identifiers,
9755 amount: (unsigned long)astContext.Idents.getAllocator().getTotalMemory());
9756
9757 // How much memory is used for selectors?
9758 createCXTUResourceUsageEntry(
9759 entries&: *entries, k: CXTUResourceUsage_Selectors,
9760 amount: (unsigned long)astContext.Selectors.getTotalMemory());
9761
9762 // How much memory is used by ASTContext's side tables?
9763 createCXTUResourceUsageEntry(
9764 entries&: *entries, k: CXTUResourceUsage_AST_SideTables,
9765 amount: (unsigned long)astContext.getSideTableAllocatedMemory());
9766
9767 // How much memory is used for caching global code completion results?
9768 unsigned long completionBytes = 0;
9769 if (GlobalCodeCompletionAllocator *completionAllocator =
9770 astUnit->getCachedCompletionAllocator().get()) {
9771 completionBytes = completionAllocator->getTotalMemory();
9772 }
9773 createCXTUResourceUsageEntry(
9774 entries&: *entries, k: CXTUResourceUsage_GlobalCompletionResults, amount: completionBytes);
9775
9776 // How much memory is being used by SourceManager's content cache?
9777 createCXTUResourceUsageEntry(
9778 entries&: *entries, k: CXTUResourceUsage_SourceManagerContentCache,
9779 amount: (unsigned long)astContext.getSourceManager().getContentCacheSize());
9780
9781 // How much memory is being used by the MemoryBuffer's in SourceManager?
9782 const SourceManager::MemoryBufferSizes &srcBufs =
9783 astUnit->getSourceManager().getMemoryBufferSizes();
9784
9785 createCXTUResourceUsageEntry(entries&: *entries,
9786 k: CXTUResourceUsage_SourceManager_Membuffer_Malloc,
9787 amount: (unsigned long)srcBufs.malloc_bytes);
9788 createCXTUResourceUsageEntry(entries&: *entries,
9789 k: CXTUResourceUsage_SourceManager_Membuffer_MMap,
9790 amount: (unsigned long)srcBufs.mmap_bytes);
9791 createCXTUResourceUsageEntry(
9792 entries&: *entries, k: CXTUResourceUsage_SourceManager_DataStructures,
9793 amount: (unsigned long)astContext.getSourceManager().getDataStructureSizes());
9794
9795 // How much memory is being used by the ExternalASTSource?
9796 if (ExternalASTSource *esrc = astContext.getExternalSource()) {
9797 const ExternalASTSource::MemoryBufferSizes &sizes =
9798 esrc->getMemoryBufferSizes();
9799
9800 createCXTUResourceUsageEntry(
9801 entries&: *entries, k: CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc,
9802 amount: (unsigned long)sizes.malloc_bytes);
9803 createCXTUResourceUsageEntry(
9804 entries&: *entries, k: CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
9805 amount: (unsigned long)sizes.mmap_bytes);
9806 }
9807
9808 // How much memory is being used by the Preprocessor?
9809 Preprocessor &pp = astUnit->getPreprocessor();
9810 createCXTUResourceUsageEntry(entries&: *entries, k: CXTUResourceUsage_Preprocessor,
9811 amount: pp.getTotalMemory());
9812
9813 if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
9814 createCXTUResourceUsageEntry(entries&: *entries,
9815 k: CXTUResourceUsage_PreprocessingRecord,
9816 amount: pRec->getTotalMemory());
9817 }
9818
9819 createCXTUResourceUsageEntry(entries&: *entries,
9820 k: CXTUResourceUsage_Preprocessor_HeaderSearch,
9821 amount: pp.getHeaderSearchInfo().getTotalMemory());
9822
9823 CXTUResourceUsage usage = {.data: (void *)entries.get(), .numEntries: (unsigned)entries->size(),
9824 .entries: !entries->empty() ? &(*entries)[0] : nullptr};
9825 (void)entries.release();
9826 return usage;
9827}
9828
9829void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
9830 if (usage.data)
9831 delete (MemUsageEntries *)usage.data;
9832}
9833
9834CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) {
9835 CXSourceRangeList *skipped = new CXSourceRangeList;
9836 skipped->count = 0;
9837 skipped->ranges = nullptr;
9838
9839 if (isNotUsableTU(TU)) {
9840 LOG_BAD_TU(TU);
9841 return skipped;
9842 }
9843
9844 if (!file)
9845 return skipped;
9846
9847 ASTUnit *astUnit = cxtu::getASTUnit(TU);
9848 PreprocessingRecord *ppRec =
9849 astUnit->getPreprocessor().getPreprocessingRecord();
9850 if (!ppRec)
9851 return skipped;
9852
9853 ASTContext &Ctx = astUnit->getASTContext();
9854 SourceManager &sm = Ctx.getSourceManager();
9855 FileEntryRef fileEntry = *cxfile::getFileEntryRef(File: file);
9856 FileID wantedFileID = sm.translateFile(SourceFile: fileEntry);
9857 bool isMainFile = wantedFileID == sm.getMainFileID();
9858
9859 const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges();
9860 std::vector<SourceRange> wantedRanges;
9861 for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(),
9862 ei = SkippedRanges.end();
9863 i != ei; ++i) {
9864 if (sm.getFileID(SpellingLoc: i->getBegin()) == wantedFileID ||
9865 sm.getFileID(SpellingLoc: i->getEnd()) == wantedFileID)
9866 wantedRanges.push_back(x: *i);
9867 else if (isMainFile && (astUnit->isInPreambleFileID(Loc: i->getBegin()) ||
9868 astUnit->isInPreambleFileID(Loc: i->getEnd())))
9869 wantedRanges.push_back(x: *i);
9870 }
9871
9872 skipped->count = wantedRanges.size();
9873 skipped->ranges = new CXSourceRange[skipped->count];
9874 for (unsigned i = 0, ei = skipped->count; i != ei; ++i)
9875 skipped->ranges[i] = cxloc::translateSourceRange(Context&: Ctx, R: wantedRanges[i]);
9876
9877 return skipped;
9878}
9879
9880CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) {
9881 CXSourceRangeList *skipped = new CXSourceRangeList;
9882 skipped->count = 0;
9883 skipped->ranges = nullptr;
9884
9885 if (isNotUsableTU(TU)) {
9886 LOG_BAD_TU(TU);
9887 return skipped;
9888 }
9889
9890 ASTUnit *astUnit = cxtu::getASTUnit(TU);
9891 PreprocessingRecord *ppRec =
9892 astUnit->getPreprocessor().getPreprocessingRecord();
9893 if (!ppRec)
9894 return skipped;
9895
9896 ASTContext &Ctx = astUnit->getASTContext();
9897
9898 const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges();
9899
9900 skipped->count = SkippedRanges.size();
9901 skipped->ranges = new CXSourceRange[skipped->count];
9902 for (unsigned i = 0, ei = skipped->count; i != ei; ++i)
9903 skipped->ranges[i] = cxloc::translateSourceRange(Context&: Ctx, R: SkippedRanges[i]);
9904
9905 return skipped;
9906}
9907
9908void clang_disposeSourceRangeList(CXSourceRangeList *ranges) {
9909 if (ranges) {
9910 delete[] ranges->ranges;
9911 delete ranges;
9912 }
9913}
9914
9915void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {
9916 CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU);
9917 for (unsigned I = 0; I != Usage.numEntries; ++I)
9918 fprintf(stderr, format: " %s: %lu\n",
9919 clang_getTUResourceUsageName(kind: Usage.entries[I].kind),
9920 Usage.entries[I].amount);
9921
9922 clang_disposeCXTUResourceUsage(usage: Usage);
9923}
9924
9925CXCursor clang_Cursor_getVarDeclInitializer(CXCursor cursor) {
9926 const Decl *const D = getCursorDecl(Cursor: cursor);
9927 if (!D)
9928 return clang_getNullCursor();
9929 const auto *const VD = dyn_cast<VarDecl>(Val: D);
9930 if (!VD)
9931 return clang_getNullCursor();
9932 const Expr *const Init = VD->getInit();
9933 if (!Init)
9934 return clang_getNullCursor();
9935
9936 return cxcursor::MakeCXCursor(S: Init, Parent: VD, TU: cxcursor::getCursorTU(Cursor: cursor));
9937}
9938
9939int clang_Cursor_hasVarDeclGlobalStorage(CXCursor cursor) {
9940 const Decl *const D = getCursorDecl(Cursor: cursor);
9941 if (!D)
9942 return -1;
9943 const auto *const VD = dyn_cast<VarDecl>(Val: D);
9944 if (!VD)
9945 return -1;
9946
9947 return VD->hasGlobalStorage();
9948}
9949
9950int clang_Cursor_hasVarDeclExternalStorage(CXCursor cursor) {
9951 const Decl *const D = getCursorDecl(Cursor: cursor);
9952 if (!D)
9953 return -1;
9954 const auto *const VD = dyn_cast<VarDecl>(Val: D);
9955 if (!VD)
9956 return -1;
9957
9958 return VD->hasExternalStorage();
9959}
9960
9961//===----------------------------------------------------------------------===//
9962// Misc. utility functions.
9963//===----------------------------------------------------------------------===//
9964
9965/// Default to using our desired 8 MB stack size on "safety" threads.
9966static unsigned SafetyStackThreadSize = DesiredStackSize;
9967
9968namespace clang {
9969
9970bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn,
9971 unsigned Size) {
9972 if (!Size)
9973 Size = GetSafetyThreadStackSize();
9974 if (Size && !getenv(name: "LIBCLANG_NOTHREADS"))
9975 return CRC.RunSafelyOnThread(Fn, RequestedStackSize: Size);
9976 return CRC.RunSafely(Fn);
9977}
9978
9979unsigned GetSafetyThreadStackSize() { return SafetyStackThreadSize; }
9980
9981void SetSafetyThreadStackSize(unsigned Value) { SafetyStackThreadSize = Value; }
9982
9983} // namespace clang
9984
9985void clang::setThreadBackgroundPriority() {
9986 if (getenv(name: "LIBCLANG_BGPRIO_DISABLE"))
9987 return;
9988
9989#if LLVM_ENABLE_THREADS
9990 // The function name setThreadBackgroundPriority is for historical reasons;
9991 // Low is more appropriate.
9992 llvm::set_thread_priority(llvm::ThreadPriority::Low);
9993#endif
9994}
9995
9996void cxindex::printDiagsToStderr(ASTUnit *Unit) {
9997 if (!Unit)
9998 return;
9999
10000 for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
10001 DEnd = Unit->stored_diag_end();
10002 D != DEnd; ++D) {
10003 CXStoredDiagnostic Diag(*D, Unit->getLangOpts());
10004 CXString Msg =
10005 clang_formatDiagnostic(Diagnostic: &Diag, Options: clang_defaultDiagnosticDisplayOptions());
10006 fprintf(stderr, format: "%s\n", clang_getCString(string: Msg));
10007 clang_disposeString(string: Msg);
10008 }
10009#ifdef _WIN32
10010 // On Windows, force a flush, since there may be multiple copies of
10011 // stderr and stdout in the file system, all with different buffers
10012 // but writing to the same device.
10013 fflush(stderr);
10014#endif
10015}
10016
10017MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II,
10018 SourceLocation MacroDefLoc,
10019 CXTranslationUnit TU) {
10020 if (MacroDefLoc.isInvalid() || !TU)
10021 return nullptr;
10022 if (!II.hadMacroDefinition())
10023 return nullptr;
10024
10025 ASTUnit *Unit = cxtu::getASTUnit(TU);
10026 Preprocessor &PP = Unit->getPreprocessor();
10027 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II: &II);
10028 if (MD) {
10029 for (MacroDirective::DefInfo Def = MD->getDefinition(); Def;
10030 Def = Def.getPreviousDefinition()) {
10031 if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc())
10032 return Def.getMacroInfo();
10033 }
10034 }
10035
10036 return nullptr;
10037}
10038
10039const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef,
10040 CXTranslationUnit TU) {
10041 if (!MacroDef || !TU)
10042 return nullptr;
10043 const IdentifierInfo *II = MacroDef->getName();
10044 if (!II)
10045 return nullptr;
10046
10047 return getMacroInfo(II: *II, MacroDefLoc: MacroDef->getLocation(), TU);
10048}
10049
10050MacroDefinitionRecord *
10051cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok,
10052 CXTranslationUnit TU) {
10053 if (!MI || !TU)
10054 return nullptr;
10055 if (Tok.isNot(K: tok::raw_identifier))
10056 return nullptr;
10057
10058 if (MI->getNumTokens() == 0)
10059 return nullptr;
10060 SourceRange DefRange(MI->getReplacementToken(Tok: 0).getLocation(),
10061 MI->getDefinitionEndLoc());
10062 ASTUnit *Unit = cxtu::getASTUnit(TU);
10063
10064 // Check that the token is inside the definition and not its argument list.
10065 SourceManager &SM = Unit->getSourceManager();
10066 if (SM.isBeforeInTranslationUnit(LHS: Tok.getLocation(), RHS: DefRange.getBegin()))
10067 return nullptr;
10068 if (SM.isBeforeInTranslationUnit(LHS: DefRange.getEnd(), RHS: Tok.getLocation()))
10069 return nullptr;
10070
10071 Preprocessor &PP = Unit->getPreprocessor();
10072 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
10073 if (!PPRec)
10074 return nullptr;
10075
10076 IdentifierInfo &II = PP.getIdentifierTable().get(Name: Tok.getRawIdentifier());
10077 if (!II.hadMacroDefinition())
10078 return nullptr;
10079
10080 // Check that the identifier is not one of the macro arguments.
10081 if (llvm::is_contained(Range: MI->params(), Element: &II))
10082 return nullptr;
10083
10084 MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(II: &II);
10085 if (!InnerMD)
10086 return nullptr;
10087
10088 return PPRec->findMacroDefinition(MI: InnerMD->getMacroInfo());
10089}
10090
10091MacroDefinitionRecord *
10092cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc,
10093 CXTranslationUnit TU) {
10094 if (Loc.isInvalid() || !MI || !TU)
10095 return nullptr;
10096
10097 if (MI->getNumTokens() == 0)
10098 return nullptr;
10099 ASTUnit *Unit = cxtu::getASTUnit(TU);
10100 Preprocessor &PP = Unit->getPreprocessor();
10101 if (!PP.getPreprocessingRecord())
10102 return nullptr;
10103 Loc = Unit->getSourceManager().getSpellingLoc(Loc);
10104 Token Tok;
10105 if (PP.getRawToken(Loc, Result&: Tok))
10106 return nullptr;
10107
10108 return checkForMacroInMacroDefinition(MI, Tok, TU);
10109}
10110
10111CXString clang_getClangVersion() {
10112 return cxstring::createDup(String: getClangFullVersion());
10113}
10114
10115Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) {
10116 if (TU) {
10117 if (ASTUnit *Unit = cxtu::getASTUnit(TU)) {
10118 LogOS << '<' << Unit->getMainFileName() << '>';
10119 if (Unit->isMainFileAST())
10120 LogOS << " (" << Unit->getASTFileName() << ')';
10121 return *this;
10122 }
10123 } else {
10124 LogOS << "<NULL TU>";
10125 }
10126 return *this;
10127}
10128
10129Logger &cxindex::Logger::operator<<(FileEntryRef FE) {
10130 *this << FE.getName();
10131 return *this;
10132}
10133
10134Logger &cxindex::Logger::operator<<(CXCursor cursor) {
10135 CXString cursorName = clang_getCursorDisplayName(C: cursor);
10136 *this << cursorName << "@" << clang_getCursorLocation(C: cursor);
10137 clang_disposeString(string: cursorName);
10138 return *this;
10139}
10140
10141Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) {
10142 CXFile File;
10143 unsigned Line, Column;
10144 clang_getFileLocation(location: Loc, file: &File, line: &Line, column: &Column, offset: nullptr);
10145 CXString FileName = clang_getFileName(SFile: File);
10146 *this << llvm::format(Fmt: "(%s:%d:%d)", Vals: clang_getCString(string: FileName), Vals: Line, Vals: Column);
10147 clang_disposeString(string: FileName);
10148 return *this;
10149}
10150
10151Logger &cxindex::Logger::operator<<(CXSourceRange range) {
10152 CXSourceLocation BLoc = clang_getRangeStart(range);
10153 CXSourceLocation ELoc = clang_getRangeEnd(range);
10154
10155 CXFile BFile;
10156 unsigned BLine, BColumn;
10157 clang_getFileLocation(location: BLoc, file: &BFile, line: &BLine, column: &BColumn, offset: nullptr);
10158
10159 CXFile EFile;
10160 unsigned ELine, EColumn;
10161 clang_getFileLocation(location: ELoc, file: &EFile, line: &ELine, column: &EColumn, offset: nullptr);
10162
10163 CXString BFileName = clang_getFileName(SFile: BFile);
10164 if (BFile == EFile) {
10165 *this << llvm::format(Fmt: "[%s %d:%d-%d:%d]", Vals: clang_getCString(string: BFileName),
10166 Vals: BLine, Vals: BColumn, Vals: ELine, Vals: EColumn);
10167 } else {
10168 CXString EFileName = clang_getFileName(SFile: EFile);
10169 *this << llvm::format(Fmt: "[%s:%d:%d - ", Vals: clang_getCString(string: BFileName), Vals: BLine,
10170 Vals: BColumn)
10171 << llvm::format(Fmt: "%s:%d:%d]", Vals: clang_getCString(string: EFileName), Vals: ELine,
10172 Vals: EColumn);
10173 clang_disposeString(string: EFileName);
10174 }
10175 clang_disposeString(string: BFileName);
10176 return *this;
10177}
10178
10179Logger &cxindex::Logger::operator<<(CXString Str) {
10180 *this << clang_getCString(string: Str);
10181 return *this;
10182}
10183
10184static llvm::ManagedStatic<std::mutex> LoggingMutex;
10185
10186cxindex::Logger::~Logger() {
10187 std::lock_guard<std::mutex> L(*LoggingMutex);
10188
10189 static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();
10190
10191 raw_ostream &OS = llvm::errs();
10192 OS << "[libclang:" << Name << ':';
10193
10194#ifdef USE_DARWIN_THREADS
10195 // TODO: Portability.
10196 mach_port_t tid = pthread_mach_thread_np(pthread_self());
10197 OS << tid << ':';
10198#endif
10199
10200 llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime();
10201 OS << llvm::format(Fmt: "%7.4f] ", Vals: TR.getWallTime() - sBeginTR.getWallTime());
10202 OS << Msg << '\n';
10203
10204 if (Trace) {
10205 llvm::sys::PrintStackTrace(OS);
10206 OS << "--------------------------------------------------\n";
10207 }
10208}
10209
10210CXString clang_getBinaryOperatorKindSpelling(enum CXBinaryOperatorKind kind) {
10211 if (kind <= CXBinaryOperator_Invalid || kind > CXBinaryOperator_Last)
10212 return cxstring::createEmpty();
10213
10214 return cxstring::createDup(
10215 String: BinaryOperator::getOpcodeStr(Op: static_cast<BinaryOperatorKind>(kind - 1)));
10216}
10217
10218enum CXBinaryOperatorKind clang_getCursorBinaryOperatorKind(CXCursor cursor) {
10219 if (clang_isExpression(K: cursor.kind)) {
10220 const Expr *expr = getCursorExpr(Cursor: cursor);
10221
10222 if (const auto *op = dyn_cast<BinaryOperator>(Val: expr))
10223 return static_cast<CXBinaryOperatorKind>(op->getOpcode() + 1);
10224
10225 if (const auto *op = dyn_cast<CXXRewrittenBinaryOperator>(Val: expr))
10226 return static_cast<CXBinaryOperatorKind>(op->getOpcode() + 1);
10227 }
10228
10229 return CXBinaryOperator_Invalid;
10230}
10231
10232CXString clang_getUnaryOperatorKindSpelling(enum CXUnaryOperatorKind kind) {
10233 if (kind <= CXUnaryOperator_Invalid || kind > CXUnaryOperator_Last)
10234 return cxstring::createEmpty();
10235
10236 return cxstring::createRef(
10237 String: UnaryOperator::getOpcodeStr(Op: static_cast<UnaryOperatorKind>(kind - 1)));
10238}
10239
10240enum CXUnaryOperatorKind clang_getCursorUnaryOperatorKind(CXCursor cursor) {
10241 if (clang_isExpression(K: cursor.kind)) {
10242 const Expr *expr = getCursorExpr(Cursor: cursor);
10243
10244 if (const auto *op = dyn_cast<UnaryOperator>(Val: expr))
10245 return static_cast<CXUnaryOperatorKind>(op->getOpcode() + 1);
10246 }
10247
10248 return CXUnaryOperator_Invalid;
10249}
10250