Coverage Report

Created: 2023-05-31 04:38

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CIndex.cpp
Line
Count
Source (jump to first uncovered line)
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 "CXSourceLocation.h"
19
#include "CXString.h"
20
#include "CXTranslationUnit.h"
21
#include "CXType.h"
22
#include "CursorVisitor.h"
23
#include "clang-c/FatalErrorHandler.h"
24
#include "clang/AST/Attr.h"
25
#include "clang/AST/DeclObjCCommon.h"
26
#include "clang/AST/Mangle.h"
27
#include "clang/AST/OpenMPClause.h"
28
#include "clang/AST/StmtVisitor.h"
29
#include "clang/Basic/Diagnostic.h"
30
#include "clang/Basic/DiagnosticCategories.h"
31
#include "clang/Basic/DiagnosticIDs.h"
32
#include "clang/Basic/Stack.h"
33
#include "clang/Basic/TargetInfo.h"
34
#include "clang/Basic/Version.h"
35
#include "clang/Frontend/ASTUnit.h"
36
#include "clang/Frontend/CompilerInstance.h"
37
#include "clang/Index/CommentToXML.h"
38
#include "clang/Lex/HeaderSearch.h"
39
#include "clang/Lex/Lexer.h"
40
#include "clang/Lex/PreprocessingRecord.h"
41
#include "clang/Lex/Preprocessor.h"
42
#include "llvm/ADT/STLExtras.h"
43
#include "llvm/ADT/StringSwitch.h"
44
#include "llvm/Config/llvm-config.h"
45
#include "llvm/Support/Compiler.h"
46
#include "llvm/Support/CrashRecoveryContext.h"
47
#include "llvm/Support/Format.h"
48
#include "llvm/Support/ManagedStatic.h"
49
#include "llvm/Support/MemoryBuffer.h"
50
#include "llvm/Support/Program.h"
51
#include "llvm/Support/SaveAndRestore.h"
52
#include "llvm/Support/Signals.h"
53
#include "llvm/Support/TargetSelect.h"
54
#include "llvm/Support/Threading.h"
55
#include "llvm/Support/Timer.h"
56
#include "llvm/Support/raw_ostream.h"
57
#include "llvm/Support/thread.h"
58
#include <mutex>
59
#include <optional>
60
61
#if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__)
62
#define USE_DARWIN_THREADS
63
#endif
64
65
#ifdef USE_DARWIN_THREADS
66
#include <pthread.h>
67
#endif
68
69
using namespace clang;
70
using namespace clang::cxcursor;
71
using namespace clang::cxtu;
72
using namespace clang::cxindex;
73
74
CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx,
75
1.09k
                                              std::unique_ptr<ASTUnit> AU) {
76
1.09k
  if (!AU)
77
2
    return nullptr;
78
1.09k
  assert(CIdx);
79
1.09k
  CXTranslationUnit D = new CXTranslationUnitImpl();
80
1.09k
  D->CIdx = CIdx;
81
1.09k
  D->TheASTUnit = AU.release();
82
1.09k
  D->StringPool = new cxstring::CXStringPool();
83
1.09k
  D->Diagnostics = nullptr;
84
1.09k
  D->OverridenCursorsPool = createOverridenCXCursorsPool();
85
1.09k
  D->CommentToXML = nullptr;
86
1.09k
  D->ParsingOptions = 0;
87
1.09k
  D->Arguments = {};
88
1.09k
  return D;
89
1.09k
}
90
91
1.06k
bool cxtu::isASTReadError(ASTUnit *AU) {
92
1.06k
  for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(),
93
1.06k
                                     DEnd = AU->stored_diag_end();
94
6.24k
       D != DEnd; 
++D5.17k
) {
95
5.18k
    if (D->getLevel() >= DiagnosticsEngine::Error &&
96
5.18k
        DiagnosticIDs::getCategoryNumberForDiag(D->getID()) ==
97
1.74k
            diag::DiagCat_AST_Deserialization_Issue)
98
2
      return true;
99
5.18k
  }
100
1.06k
  return false;
101
1.06k
}
102
103
45
cxtu::CXTUOwner::~CXTUOwner() {
104
45
  if (TU)
105
45
    clang_disposeTranslationUnit(TU);
106
45
}
107
108
/// Compare two source ranges to determine their relative position in
109
/// the translation unit.
110
static RangeComparisonResult RangeCompare(SourceManager &SM, SourceRange R1,
111
36.5k
                                          SourceRange R2) {
112
36.5k
  assert(R1.isValid() && "First range is invalid?");
113
36.5k
  assert(R2.isValid() && "Second range is invalid?");
114
36.5k
  if (R1.getEnd() != R2.getBegin() &&
115
36.5k
      
SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())36.0k
)
116
16.6k
    return RangeBefore;
117
19.8k
  if (R2.getEnd() != R1.getBegin() &&
118
19.8k
      
SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())19.3k
)
119
3.31k
    return RangeAfter;
120
16.4k
  return RangeOverlap;
121
19.8k
}
122
123
/// Determine if a source location falls within, before, or after a
124
///   a given source range.
125
static RangeComparisonResult LocationCompare(SourceManager &SM,
126
23.6M
                                             SourceLocation L, SourceRange R) {
127
23.6M
  assert(R.isValid() && "First range is invalid?");
128
23.6M
  assert(L.isValid() && "Second range is invalid?");
129
23.6M
  if (L == R.getBegin() || 
L == R.getEnd()23.6M
)
130
24.0k
    return RangeOverlap;
131
23.6M
  if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
132
23.6M
    return RangeBefore;
133
24.4k
  if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
134
18.2k
    return RangeAfter;
135
6.15k
  return RangeOverlap;
136
24.4k
}
137
138
/// Translate a Clang source range into a CIndex source range.
139
///
140
/// Clang internally represents ranges where the end location points to the
141
/// start of the token at the end. However, for external clients it is more
142
/// useful to have a CXSourceRange be a proper half-open interval. This routine
143
/// does the appropriate translation.
144
CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
145
                                          const LangOptions &LangOpts,
146
386k
                                          const CharSourceRange &R) {
147
  // We want the last character in this location, so we will adjust the
148
  // location accordingly.
149
386k
  SourceLocation EndLoc = R.getEnd();
150
386k
  bool IsTokenRange = R.isTokenRange();
151
386k
  if (EndLoc.isValid() && 
EndLoc.isMacroID()386k
&&
152
386k
      
!SM.isMacroArgExpansion(EndLoc)1.47k
) {
153
994
    CharSourceRange Expansion = SM.getExpansionRange(EndLoc);
154
994
    EndLoc = Expansion.getEnd();
155
994
    IsTokenRange = Expansion.isTokenRange();
156
994
  }
157
386k
  if (IsTokenRange && 
EndLoc.isValid()386k
) {
158
386k
    unsigned Length =
159
386k
        Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), SM, LangOpts);
160
386k
    EndLoc = EndLoc.getLocWithOffset(Length);
161
386k
  }
162
163
386k
  CXSourceRange Result = {
164
386k
      {&SM, &LangOpts}, R.getBegin().getRawEncoding(), EndLoc.getRawEncoding()};
165
386k
  return Result;
166
386k
}
167
168
4
CharSourceRange cxloc::translateCXRangeToCharRange(CXSourceRange R) {
169
4
  return CharSourceRange::getCharRange(
170
4
      SourceLocation::getFromRawEncoding(R.begin_int_data),
171
4
      SourceLocation::getFromRawEncoding(R.end_int_data));
172
4
}
173
174
//===----------------------------------------------------------------------===//
175
// Cursor visitor.
176
//===----------------------------------------------------------------------===//
177
178
static SourceRange getRawCursorExtent(CXCursor C);
179
static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
180
181
25.7k
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
182
25.7k
  return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
183
25.7k
}
184
185
/// Visit the given cursor and, if requested by the visitor,
186
/// its children.
187
///
188
/// \param Cursor the cursor to visit.
189
///
190
/// \param CheckedRegionOfInterest if true, then the caller already checked
191
/// that this cursor is within the region of interest.
192
///
193
/// \returns true if the visitation should be aborted, false if it
194
/// should continue.
195
100k
bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
196
100k
  if (clang_isInvalid(Cursor.kind))
197
1
    return false;
198
199
100k
  if (clang_isDeclaration(Cursor.kind)) {
200
10.2k
    const Decl *D = getCursorDecl(Cursor);
201
10.2k
    if (!D) {
202
0
      assert(0 && "Invalid declaration cursor");
203
0
      return true; // abort.
204
0
    }
205
206
    // Ignore implicit declarations, unless it's an objc method because
207
    // currently we should report implicit methods for properties when indexing.
208
10.2k
    if (D->isImplicit() && 
!isa<ObjCMethodDecl>(D)2.15k
)
209
2.02k
      return false;
210
10.2k
  }
211
212
  // If we have a range of interest, and this cursor doesn't intersect with it,
213
  // we're done.
214
98.6k
  if (RegionOfInterest.isValid() && 
!CheckedRegionOfInterest11.4k
) {
215
9.37k
    SourceRange Range = getRawCursorExtent(Cursor);
216
9.37k
    if (Range.isInvalid() || 
CompareRegionOfInterest(Range)9.37k
)
217
7.26k
      return false;
218
9.37k
  }
219
220
91.4k
  switch (Visitor(Cursor, Parent, ClientData)) {
221
19
  case CXChildVisit_Break:
222
19
    return true;
223
224
7.89k
  case CXChildVisit_Continue:
225
7.89k
    return false;
226
227
83.5k
  case CXChildVisit_Recurse: {
228
83.5k
    bool ret = VisitChildren(Cursor);
229
83.5k
    if (PostChildrenVisitor)
230
1.20k
      if (PostChildrenVisitor(Cursor, ClientData))
231
0
        return true;
232
83.5k
    return ret;
233
83.5k
  }
234
91.4k
  }
235
236
0
  llvm_unreachable("Invalid CXChildVisitResult!");
237
0
}
238
239
static bool visitPreprocessedEntitiesInRange(SourceRange R,
240
                                             PreprocessingRecord &PPRec,
241
9.63k
                                             CursorVisitor &Visitor) {
242
9.63k
  SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
243
9.63k
  FileID FID;
244
245
9.63k
  if (!Visitor.shouldVisitIncludedEntities()) {
246
    // If the begin/end of the range lie in the same FileID, do the optimization
247
    // where we skip preprocessed entities that do not come from the same
248
    // FileID.
249
9.63k
    FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
250
9.63k
    if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
251
0
      FID = FileID();
252
9.63k
  }
253
254
9.63k
  const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R);
255
9.63k
  return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(),
256
9.63k
                                           PPRec, FID);
257
9.63k
}
258
259
9.59k
bool CursorVisitor::visitFileRegion() {
260
9.59k
  if (RegionOfInterest.isInvalid())
261
0
    return false;
262
263
9.59k
  ASTUnit *Unit = cxtu::getASTUnit(TU);
264
9.59k
  SourceManager &SM = Unit->getSourceManager();
265
266
9.59k
  std::pair<FileID, unsigned> Begin = SM.getDecomposedLoc(
267
9.59k
                                  SM.getFileLoc(RegionOfInterest.getBegin())),
268
9.59k
                              End = SM.getDecomposedLoc(
269
9.59k
                                  SM.getFileLoc(RegionOfInterest.getEnd()));
270
271
9.59k
  if (End.first != Begin.first) {
272
    // If the end does not reside in the same file, try to recover by
273
    // picking the end of the file of begin location.
274
0
    End.first = Begin.first;
275
0
    End.second = SM.getFileIDSize(Begin.first);
276
0
  }
277
278
9.59k
  assert(Begin.first == End.first);
279
9.59k
  if (Begin.second > End.second)
280
0
    return false;
281
282
9.59k
  FileID File = Begin.first;
283
9.59k
  unsigned Offset = Begin.second;
284
9.59k
  unsigned Length = End.second - Begin.second;
285
286
9.59k
  if (!VisitDeclsOnly && 
!VisitPreprocessorLast9.56k
)
287
0
    if (visitPreprocessedEntitiesInRegion())
288
0
      return true; // visitation break.
289
290
9.59k
  if (visitDeclsFromFileRegion(File, Offset, Length))
291
14
    return true; // visitation break.
292
293
9.57k
  if (!VisitDeclsOnly && 
VisitPreprocessorLast9.55k
)
294
9.55k
    return visitPreprocessedEntitiesInRegion();
295
296
24
  return false;
297
9.57k
}
298
299
10.8k
static bool isInLexicalContext(Decl *D, DeclContext *DC) {
300
10.8k
  if (!DC)
301
9.69k
    return false;
302
303
2.28k
  
for (DeclContext *DeclDC = D->getLexicalDeclContext(); 1.16k
DeclDC;
304
1.16k
       
DeclDC = DeclDC->getLexicalParent()1.12k
) {
305
1.16k
    if (DeclDC == DC)
306
40
      return true;
307
1.16k
  }
308
1.12k
  return false;
309
1.16k
}
310
311
bool CursorVisitor::visitDeclsFromFileRegion(FileID File, unsigned Offset,
312
9.59k
                                             unsigned Length) {
313
9.59k
  ASTUnit *Unit = cxtu::getASTUnit(TU);
314
9.59k
  SourceManager &SM = Unit->getSourceManager();
315
9.59k
  SourceRange Range = RegionOfInterest;
316
317
9.59k
  SmallVector<Decl *, 16> Decls;
318
9.59k
  Unit->findFileRegionDecls(File, Offset, Length, Decls);
319
320
  // If we didn't find any file level decls for the file, try looking at the
321
  // file that it was included from.
322
9.60k
  while (Decls.empty() || 
Decls.front()->isTopLevelDeclInObjCContainer()9.57k
) {
323
29
    bool Invalid = false;
324
29
    const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
325
29
    if (Invalid)
326
0
      return false;
327
328
29
    SourceLocation Outer;
329
29
    if (SLEntry.isFile())
330
29
      Outer = SLEntry.getFile().getIncludeLoc();
331
0
    else
332
0
      Outer = SLEntry.getExpansion().getExpansionLocStart();
333
29
    if (Outer.isInvalid())
334
14
      return false;
335
336
15
    std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
337
15
    Length = 0;
338
15
    Unit->findFileRegionDecls(File, Offset, Length, Decls);
339
15
  }
340
341
9.57k
  assert(!Decls.empty());
342
343
9.57k
  bool VisitedAtLeastOnce = false;
344
9.57k
  DeclContext *CurDC = nullptr;
345
9.57k
  SmallVectorImpl<Decl *>::iterator DIt = Decls.begin();
346
18.5k
  for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; 
++DIt9.01k
) {
347
10.8k
    Decl *D = *DIt;
348
10.8k
    if (D->getSourceRange().isInvalid())
349
0
      continue;
350
351
10.8k
    if (isInLexicalContext(D, CurDC))
352
40
      continue;
353
354
10.8k
    CurDC = dyn_cast<DeclContext>(D);
355
356
10.8k
    if (TagDecl *TD = dyn_cast<TagDecl>(D))
357
302
      if (!TD->isFreeStanding())
358
42
        continue;
359
360
10.7k
    RangeComparisonResult CompRes =
361
10.7k
        RangeCompare(SM, D->getSourceRange(), Range);
362
10.7k
    if (CompRes == RangeBefore)
363
7.55k
      continue;
364
3.22k
    if (CompRes == RangeAfter)
365
1.83k
      break;
366
367
1.39k
    assert(CompRes == RangeOverlap);
368
1.39k
    VisitedAtLeastOnce = true;
369
370
1.39k
    if (isa<ObjCContainerDecl>(D)) {
371
511
      FileDI_current = &DIt;
372
511
      FileDE_current = DE;
373
881
    } else {
374
881
      FileDI_current = nullptr;
375
881
    }
376
377
1.39k
    if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
378
14
      return true; // visitation break.
379
1.39k
  }
380
381
9.56k
  if (VisitedAtLeastOnce)
382
1.12k
    return false;
383
384
  // No Decls overlapped with the range. Move up the lexical context until there
385
  // is a context that contains the range or we reach the translation unit
386
  // level.
387
8.43k
  DeclContext *DC = DIt == Decls.begin()
388
8.43k
                        ? 
(*DIt)->getLexicalDeclContext()1.08k
389
8.43k
                        : 
(*(DIt - 1))->getLexicalDeclContext()7.35k
;
390
391
8.43k
  while (DC && !DC->isTranslationUnit()) {
392
0
    Decl *D = cast<Decl>(DC);
393
0
    SourceRange CurDeclRange = D->getSourceRange();
394
0
    if (CurDeclRange.isInvalid())
395
0
      break;
396
397
0
    if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
398
0
      if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
399
0
        return true; // visitation break.
400
0
    }
401
402
0
    DC = D->getLexicalDeclContext();
403
0
  }
404
405
8.43k
  return false;
406
8.43k
}
407
408
9.83k
bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
409
9.83k
  if (!AU->getPreprocessor().getPreprocessingRecord())
410
0
    return false;
411
412
9.83k
  PreprocessingRecord &PPRec = *AU->getPreprocessor().getPreprocessingRecord();
413
9.83k
  SourceManager &SM = AU->getSourceManager();
414
415
9.83k
  if (RegionOfInterest.isValid()) {
416
9.62k
    SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
417
9.62k
    SourceLocation B = MappedRange.getBegin();
418
9.62k
    SourceLocation E = MappedRange.getEnd();
419
420
9.62k
    if (AU->isInPreambleFileID(B)) {
421
22
      if (SM.isLoadedSourceLocation(E))
422
15
        return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec,
423
15
                                                *this);
424
425
      // Beginning of range lies in the preamble but it also extends beyond
426
      // it into the main file. Split the range into 2 parts, one covering
427
      // the preamble and another covering the main file. This allows subsequent
428
      // calls to visitPreprocessedEntitiesInRange to accept a source range that
429
      // lies in the same FileID, allowing it to skip preprocessed entities that
430
      // do not come from the same FileID.
431
7
      bool breaked = visitPreprocessedEntitiesInRange(
432
7
          SourceRange(B, AU->getEndOfPreambleFileID()), PPRec, *this);
433
7
      if (breaked)
434
0
        return true;
435
7
      return visitPreprocessedEntitiesInRange(
436
7
          SourceRange(AU->getStartOfMainFileID(), E), PPRec, *this);
437
7
    }
438
439
9.60k
    return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
440
9.62k
  }
441
442
207
  bool OnlyLocalDecls = !AU->isMainFileAST() && 
AU->getOnlyLocalDecls()188
;
443
444
207
  if (OnlyLocalDecls)
445
45
    return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
446
45
                                     PPRec);
447
448
162
  return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
449
207
}
450
451
template <typename InputIterator>
452
bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
453
                                              InputIterator Last,
454
                                              PreprocessingRecord &PPRec,
455
9.84k
                                              FileID FID) {
456
98.6k
  for (; First != Last; 
++First88.8k
) {
457
88.8k
    if (!FID.isInvalid() && 
!PPRec.isEntityInFileID(First, FID)8.75k
)
458
2.70k
      continue;
459
460
86.1k
    PreprocessedEntity *PPE = *First;
461
86.1k
    if (!PPE)
462
0
      continue;
463
464
86.1k
    if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
465
5.97k
      if (Visit(MakeMacroExpansionCursor(ME, TU)))
466
1
        return true;
467
468
5.96k
      continue;
469
5.97k
    }
470
471
80.1k
    if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) {
472
79.8k
      if (Visit(MakeMacroDefinitionCursor(MD, TU)))
473
0
        return true;
474
475
79.8k
      continue;
476
79.8k
    }
477
478
325
    if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
479
325
      if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
480
0
        return true;
481
482
325
      continue;
483
325
    }
484
325
  }
485
486
9.84k
  return false;
487
9.84k
}
488
489
/// Visit the children of the given cursor.
490
///
491
/// \returns true if the visitation should be aborted, false if it
492
/// should continue.
493
83.7k
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
494
83.7k
  if (clang_isReference(Cursor.kind) &&
495
83.7k
      
Cursor.kind != CXCursor_CXXBaseSpecifier2.08k
) {
496
    // By definition, references have no children.
497
1.96k
    return false;
498
1.96k
  }
499
500
  // Set the Parent field to Cursor, then back to its old value once we're
501
  // done.
502
81.7k
  SetParentRAII SetParent(Parent, StmtParent, Cursor);
503
504
81.7k
  if (clang_isDeclaration(Cursor.kind)) {
505
7.48k
    Decl *D = const_cast<Decl *>(getCursorDecl(Cursor));
506
7.48k
    if (!D)
507
0
      return false;
508
509
7.48k
    return VisitAttributes(D) || Visit(D);
510
7.48k
  }
511
512
74.3k
  if (clang_isStatement(Cursor.kind)) {
513
779
    if (const Stmt *S = getCursorStmt(Cursor))
514
779
      return Visit(S);
515
516
0
    return false;
517
779
  }
518
519
73.5k
  if (clang_isExpression(Cursor.kind)) {
520
396
    if (const Expr *E = getCursorExpr(Cursor))
521
396
      return Visit(E);
522
523
0
    return false;
524
396
  }
525
526
73.1k
  if (clang_isTranslationUnit(Cursor.kind)) {
527
209
    CXTranslationUnit TU = getCursorTU(Cursor);
528
209
    ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
529
530
209
    int VisitOrder[2] = {VisitPreprocessorLast, !VisitPreprocessorLast};
531
623
    for (unsigned I = 0; I != 2; 
++I414
) {
532
418
      if (VisitOrder[I]) {
533
209
        if (!CXXUnit->isMainFileAST() && 
CXXUnit->getOnlyLocalDecls()190
&&
534
209
            
RegionOfInterest.isInvalid()47
) {
535
47
          for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
536
47
                                           TLEnd = CXXUnit->top_level_end();
537
232
               TL != TLEnd; 
++TL185
) {
538
185
            const std::optional<bool> V = handleDeclForVisitation(*TL);
539
185
            if (!V)
540
185
              continue;
541
0
            return *V;
542
185
          }
543
162
        } else if (VisitDeclContext(
544
162
                       CXXUnit->getASTContext().getTranslationUnitDecl()))
545
4
          return true;
546
205
        continue;
547
209
      }
548
549
      // Walk the preprocessing record.
550
209
      if (CXXUnit->getPreprocessor().getPreprocessingRecord())
551
207
        visitPreprocessedEntitiesInRegion();
552
209
    }
553
554
205
    return false;
555
209
  }
556
557
72.9k
  if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
558
122
    if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
559
122
      if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
560
122
        return Visit(BaseTSInfo->getTypeLoc());
561
122
      }
562
122
    }
563
122
  }
564
565
72.7k
  if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
566
3
    const IBOutletCollectionAttr *A =
567
3
        cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
568
3
    if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>())
569
2
      return Visit(cxcursor::MakeCursorObjCClassRef(
570
2
          ObjT->getInterface(),
571
2
          A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU));
572
3
  }
573
574
  // If pointing inside a macro definition, check if the token is an identifier
575
  // that was ever defined as a macro. In such a case, create a "pseudo" macro
576
  // expansion cursor for that token.
577
72.7k
  SourceLocation BeginLoc = RegionOfInterest.getBegin();
578
72.7k
  if (Cursor.kind == CXCursor_MacroDefinition &&
579
72.7k
      
BeginLoc == RegionOfInterest.getEnd()72.0k
) {
580
72.0k
    SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc);
581
72.0k
    const MacroInfo *MI =
582
72.0k
        getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU);
583
72.0k
    if (MacroDefinitionRecord *MacroDef =
584
72.0k
            checkForMacroInMacroDefinition(MI, Loc, TU))
585
12
      return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU));
586
72.0k
  }
587
588
  // Nothing to visit at the moment.
589
72.7k
  return false;
590
72.7k
}
591
592
8
bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
593
8
  if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
594
8
    if (Visit(TSInfo->getTypeLoc()))
595
0
      return true;
596
597
8
  if (Stmt *Body = B->getBody())
598
8
    return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
599
600
0
  return false;
601
8
}
602
603
7.87k
std::optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
604
7.87k
  if (RegionOfInterest.isValid()) {
605
2.42k
    SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
606
2.42k
    if (Range.isInvalid())
607
8
      return std::nullopt;
608
609
2.41k
    switch (CompareRegionOfInterest(Range)) {
610
1.30k
    case RangeBefore:
611
      // This declaration comes before the region of interest; skip it.
612
1.30k
      return std::nullopt;
613
614
303
    case RangeAfter:
615
      // This declaration comes after the region of interest; we're done.
616
303
      return false;
617
618
805
    case RangeOverlap:
619
      // This declaration overlaps the region of interest; visit it.
620
805
      break;
621
2.41k
    }
622
2.41k
  }
623
6.26k
  return true;
624
7.87k
}
625
626
1.77k
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
627
1.77k
  DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
628
629
  // FIXME: Eventually remove.  This part of a hack to support proper
630
  // iteration over all Decls contained lexically within an ObjC container.
631
1.77k
  SaveAndRestore DI_saved(DI_current, &I);
632
1.77k
  SaveAndRestore DE_saved(DE_current, E);
633
634
9.19k
  for (; I != E; 
++I7.42k
) {
635
7.73k
    Decl *D = *I;
636
7.73k
    if (D->getLexicalDeclContext() != DC)
637
0
      continue;
638
    // Filter out synthesized property accessor redeclarations.
639
7.73k
    if (isa<ObjCImplDecl>(DC))
640
186
      if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
641
119
        if (OMD->isSynthesizedAccessorStub())
642
45
          continue;
643
7.69k
    const std::optional<bool> V = handleDeclForVisitation(D);
644
7.69k
    if (!V)
645
7.38k
      continue;
646
311
    return *V;
647
7.69k
  }
648
1.46k
  return false;
649
1.77k
}
650
651
7.87k
std::optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) {
652
7.87k
  CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
653
654
  // Ignore synthesized ivars here, otherwise if we have something like:
655
  //   @synthesize prop = _prop;
656
  // and '_prop' is not declared, we will encounter a '_prop' ivar before
657
  // encountering the 'prop' synthesize declaration and we will think that
658
  // we passed the region-of-interest.
659
7.87k
  if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) {
660
181
    if (ivarD->getSynthesize())
661
20
      return std::nullopt;
662
181
  }
663
664
  // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
665
  // declarations is a mismatch with the compiler semantics.
666
7.85k
  if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
667
161
    auto *ID = cast<ObjCInterfaceDecl>(D);
668
161
    if (!ID->isThisDeclarationADefinition())
669
43
      Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
670
671
7.69k
  } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
672
17
    auto *PD = cast<ObjCProtocolDecl>(D);
673
17
    if (!PD->isThisDeclarationADefinition())
674
0
      Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
675
17
  }
676
677
7.85k
  const std::optional<bool> V = shouldVisitCursor(Cursor);
678
7.85k
  if (!V)
679
1.30k
    return std::nullopt;
680
6.55k
  if (!*V)
681
303
    return false;
682
6.25k
  if (Visit(Cursor, true))
683
8
    return true;
684
6.24k
  return std::nullopt;
685
6.25k
}
686
687
0
bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
688
0
  llvm_unreachable("Translation units are visited directly by Visit()");
689
0
}
690
691
13
bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
692
13
  if (VisitTemplateParameters(D->getTemplateParameters()))
693
0
    return true;
694
695
13
  return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest));
696
13
}
697
698
46
bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
699
46
  if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
700
46
    return Visit(TSInfo->getTypeLoc());
701
702
0
  return false;
703
46
}
704
705
306
bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
706
306
  if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
707
306
    return Visit(TSInfo->getTypeLoc());
708
709
0
  return false;
710
306
}
711
712
838
bool CursorVisitor::VisitTagDecl(TagDecl *D) { return VisitDeclContext(D); }
713
714
bool CursorVisitor::VisitClassTemplateSpecializationDecl(
715
22
    ClassTemplateSpecializationDecl *D) {
716
22
  bool ShouldVisitBody = false;
717
22
  switch (D->getSpecializationKind()) {
718
0
  case TSK_Undeclared:
719
0
  case TSK_ImplicitInstantiation:
720
    // Nothing to visit
721
0
    return false;
722
723
2
  case TSK_ExplicitInstantiationDeclaration:
724
10
  case TSK_ExplicitInstantiationDefinition:
725
10
    break;
726
727
12
  case TSK_ExplicitSpecialization:
728
12
    ShouldVisitBody = true;
729
12
    break;
730
22
  }
731
732
  // Visit the template arguments used in the specialization.
733
22
  if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
734
22
    TypeLoc TL = SpecType->getTypeLoc();
735
22
    if (TemplateSpecializationTypeLoc TSTLoc =
736
22
            TL.getAs<TemplateSpecializationTypeLoc>()) {
737
54
      for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; 
++I32
)
738
32
        if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I)))
739
0
          return true;
740
22
    }
741
22
  }
742
743
22
  return ShouldVisitBody && 
VisitCXXRecordDecl(D)12
;
744
22
}
745
746
bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
747
15
    ClassTemplatePartialSpecializationDecl *D) {
748
  // FIXME: Visit the "outer" template parameter lists on the TagDecl
749
  // before visiting these template parameters.
750
15
  if (VisitTemplateParameters(D->getTemplateParameters()))
751
0
    return true;
752
753
  // Visit the partial specialization arguments.
754
15
  const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten();
755
15
  const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs();
756
38
  for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; 
++I23
)
757
23
    if (VisitTemplateArgumentLoc(TemplateArgs[I]))
758
0
      return true;
759
760
15
  return VisitCXXRecordDecl(D);
761
15
}
762
763
335
bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
764
335
  if (const auto *TC = D->getTypeConstraint()) {
765
1
    if (VisitTypeConstraint(*TC))
766
0
      return true;
767
1
  }
768
769
  // Visit the default argument.
770
335
  if (D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()10
)
771
10
    if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
772
10
      if (Visit(DefArg->getTypeLoc()))
773
0
        return true;
774
775
335
  return false;
776
335
}
777
778
285
bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
779
285
  if (Expr *Init = D->getInitExpr())
780
59
    return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
781
226
  return false;
782
285
}
783
784
2.14k
bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
785
2.14k
  unsigned NumParamList = DD->getNumTemplateParameterLists();
786
2.14k
  for (unsigned i = 0; i < NumParamList; 
i++1
) {
787
1
    TemplateParameterList *Params = DD->getTemplateParameterList(i);
788
1
    if (VisitTemplateParameters(Params))
789
0
      return true;
790
1
  }
791
792
2.14k
  if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
793
2.08k
    if (Visit(TSInfo->getTypeLoc()))
794
0
      return true;
795
796
  // Visit the nested-name-specifier, if present.
797
2.14k
  if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
798
1
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
799
0
      return true;
800
801
2.14k
  return false;
802
2.14k
}
803
804
1.93k
static bool HasTrailingReturnType(FunctionDecl *ND) {
805
1.93k
  const QualType Ty = ND->getType();
806
1.93k
  if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
807
1.93k
    if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT))
808
1.83k
      return FT->hasTrailingReturn();
809
1.93k
  }
810
811
107
  return false;
812
1.93k
}
813
814
/// Compare two base or member initializers based on their source order.
815
static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X,
816
10
                                      CXXCtorInitializer *const *Y) {
817
10
  return (*X)->getSourceOrder() - (*Y)->getSourceOrder();
818
10
}
819
820
1.93k
bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
821
1.93k
  unsigned NumParamList = ND->getNumTemplateParameterLists();
822
1.95k
  for (unsigned i = 0; i < NumParamList; 
i++20
) {
823
20
    TemplateParameterList *Params = ND->getTemplateParameterList(i);
824
20
    if (VisitTemplateParameters(Params))
825
0
      return true;
826
20
  }
827
828
1.93k
  if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
829
    // Visit the function declaration's syntactic components in the order
830
    // written. This requires a bit of work.
831
1.93k
    TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
832
1.93k
    FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>();
833
1.93k
    const bool HasTrailingRT = HasTrailingReturnType(ND);
834
835
    // If we have a function declared directly (without the use of a typedef),
836
    // visit just the return type. Otherwise, just visit the function's type
837
    // now.
838
1.93k
    if ((FTL && !isa<CXXConversionDecl>(ND) && 
!HasTrailingRT1.92k
&&
839
1.93k
         
Visit(FTL.getReturnLoc())1.92k
) ||
840
1.93k
        (!FTL && 
Visit(TL)0
))
841
0
      return true;
842
843
    // Visit the nested-name-specifier, if present.
844
1.93k
    if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
845
45
      if (VisitNestedNameSpecifierLoc(QualifierLoc))
846
0
        return true;
847
848
    // Visit the declaration name.
849
1.93k
    if (!isa<CXXDestructorDecl>(ND))
850
1.91k
      if (VisitDeclarationNameInfo(ND->getNameInfo()))
851
0
        return true;
852
853
    // FIXME: Visit explicitly-specified template arguments!
854
855
    // Visit the function parameters, if we have a function type.
856
1.93k
    if (FTL && VisitFunctionTypeLoc(FTL, true))
857
0
      return true;
858
859
    // Visit the function's trailing return type.
860
1.93k
    if (FTL && HasTrailingRT && 
Visit(FTL.getReturnLoc())3
)
861
0
      return true;
862
863
    // FIXME: Attributes?
864
1.93k
  }
865
866
1.93k
  if (auto *E = ND->getTrailingRequiresClause()) {
867
1
    if (Visit(E))
868
0
      return true;
869
1
  }
870
871
1.93k
  if (ND->doesThisDeclarationHaveABody() && 
!ND->isLateTemplateParsed()829
) {
872
829
    if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
873
      // Find the initializers that were written in the source.
874
42
      SmallVector<CXXCtorInitializer *, 4> WrittenInits;
875
42
      for (auto *I : Constructor->inits()) {
876
41
        if (!I->isWritten())
877
5
          continue;
878
879
36
        WrittenInits.push_back(I);
880
36
      }
881
882
      // Sort the initializers in source order
883
42
      llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
884
42
                           &CompareCXXCtorInitializers);
885
886
      // Visit the initializers in source order
887
78
      for (unsigned I = 0, N = WrittenInits.size(); I != N; 
++I36
) {
888
36
        CXXCtorInitializer *Init = WrittenInits[I];
889
36
        if (Init->isAnyMemberInitializer()) {
890
22
          if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
891
22
                                        Init->getMemberLocation(), TU)))
892
0
            return true;
893
22
        } else 
if (TypeSourceInfo *14
TInfo14
= Init->getTypeSourceInfo()) {
894
14
          if (Visit(TInfo->getTypeLoc()))
895
0
            return true;
896
14
        }
897
898
        // Visit the initializer value.
899
36
        if (Expr *Initializer = Init->getInit())
900
36
          if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
901
0
            return true;
902
36
      }
903
42
    }
904
905
829
    if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
906
2
      return true;
907
829
  }
908
909
1.93k
  return false;
910
1.93k
}
911
912
470
bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
913
470
  if (VisitDeclaratorDecl(D))
914
0
    return true;
915
916
470
  if (Expr *BitWidth = D->getBitWidth())
917
20
    return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
918
919
450
  if (Expr *Init = D->getInClassInitializer())
920
5
    return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
921
922
445
  return false;
923
450
}
924
925
1.63k
bool CursorVisitor::VisitVarDecl(VarDecl *D) {
926
1.63k
  if (VisitDeclaratorDecl(D))
927
0
    return true;
928
929
1.63k
  if (Expr *Init = D->getInit())
930
264
    return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
931
932
1.37k
  return false;
933
1.63k
}
934
935
37
bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
936
37
  if (VisitDeclaratorDecl(D))
937
0
    return true;
938
939
37
  if (D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()2
)
940
2
    if (Expr *DefArg = D->getDefaultArgument())
941
2
      return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
942
943
35
  return false;
944
37
}
945
946
117
bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
947
  // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
948
  // before visiting these template parameters.
949
117
  if (VisitTemplateParameters(D->getTemplateParameters()))
950
0
    return true;
951
952
117
  auto *FD = D->getTemplatedDecl();
953
117
  return VisitAttributes(FD) || VisitFunctionDecl(FD);
954
117
}
955
956
118
bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
957
  // FIXME: Visit the "outer" template parameter lists on the TagDecl
958
  // before visiting these template parameters.
959
118
  if (VisitTemplateParameters(D->getTemplateParameters()))
960
0
    return true;
961
962
118
  auto *CD = D->getTemplatedDecl();
963
118
  return VisitAttributes(CD) || VisitCXXRecordDecl(CD);
964
118
}
965
966
23
bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
967
23
  if (VisitTemplateParameters(D->getTemplateParameters()))
968
0
    return true;
969
970
23
  if (D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()2
&&
971
23
      
VisitTemplateArgumentLoc(D->getDefaultArgument())2
)
972
0
    return true;
973
974
23
  return false;
975
23
}
976
977
11
bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
978
  // Visit the bound, if it's explicit.
979
11
  if (D->hasExplicitBound()) {
980
8
    if (auto TInfo = D->getTypeSourceInfo()) {
981
8
      if (Visit(TInfo->getTypeLoc()))
982
0
        return true;
983
8
    }
984
8
  }
985
986
11
  return false;
987
11
}
988
989
453
bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
990
453
  if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo())
991
279
    if (Visit(TSInfo->getTypeLoc()))
992
0
      return true;
993
994
453
  for (const auto *P : ND->parameters()) {
995
180
    if (Visit(MakeCXCursor(P, TU, RegionOfInterest)))
996
0
      return true;
997
180
  }
998
999
453
  return ND->isThisDeclarationADefinition() &&
1000
453
         
Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))70
;
1001
453
}
1002
1003
template <typename DeclIt>
1004
static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
1005
                                      SourceManager &SM, SourceLocation EndLoc,
1006
570
                                      SmallVectorImpl<Decl *> &Decls) {
1007
570
  DeclIt next = *DI_current;
1008
583
  while (++next != DE_current) {
1009
449
    Decl *D_next = *next;
1010
449
    if (!D_next)
1011
0
      break;
1012
449
    SourceLocation L = D_next->getBeginLoc();
1013
449
    if (!L.isValid())
1014
0
      break;
1015
449
    if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
1016
13
      *DI_current = next;
1017
13
      Decls.push_back(D_next);
1018
13
      continue;
1019
13
    }
1020
436
    break;
1021
449
  }
1022
570
}
CIndex.cpp:void addRangedDeclsInContainer<clang::DeclContext::decl_iterator>(clang::DeclContext::decl_iterator*, clang::DeclContext::decl_iterator, clang::SourceManager&, clang::SourceLocation, llvm::SmallVectorImpl<clang::Decl*>&)
Line
Count
Source
1006
97
                                      SmallVectorImpl<Decl *> &Decls) {
1007
97
  DeclIt next = *DI_current;
1008
97
  while (++next != DE_current) {
1009
81
    Decl *D_next = *next;
1010
81
    if (!D_next)
1011
0
      break;
1012
81
    SourceLocation L = D_next->getBeginLoc();
1013
81
    if (!L.isValid())
1014
0
      break;
1015
81
    if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
1016
0
      *DI_current = next;
1017
0
      Decls.push_back(D_next);
1018
0
      continue;
1019
0
    }
1020
81
    break;
1021
81
  }
1022
97
}
CIndex.cpp:void addRangedDeclsInContainer<clang::Decl**>(clang::Decl***, clang::Decl**, clang::SourceManager&, clang::SourceLocation, llvm::SmallVectorImpl<clang::Decl*>&)
Line
Count
Source
1006
473
                                      SmallVectorImpl<Decl *> &Decls) {
1007
473
  DeclIt next = *DI_current;
1008
486
  while (++next != DE_current) {
1009
368
    Decl *D_next = *next;
1010
368
    if (!D_next)
1011
0
      break;
1012
368
    SourceLocation L = D_next->getBeginLoc();
1013
368
    if (!L.isValid())
1014
0
      break;
1015
368
    if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
1016
13
      *DI_current = next;
1017
13
      Decls.push_back(D_next);
1018
13
      continue;
1019
13
    }
1020
355
    break;
1021
368
  }
1022
473
}
1023
1024
664
bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
1025
  // FIXME: Eventually convert back to just 'VisitDeclContext()'.  Essentially
1026
  // an @implementation can lexically contain Decls that are not properly
1027
  // nested in the AST.  When we identify such cases, we need to retrofit
1028
  // this nesting here.
1029
664
  if (!DI_current && 
!FileDI_current567
)
1030
94
    return VisitDeclContext(D);
1031
1032
  // Scan the Decls that immediately come after the container
1033
  // in the current DeclContext.  If any fall within the
1034
  // container's lexical region, stash them into a vector
1035
  // for later processing.
1036
570
  SmallVector<Decl *, 24> DeclsInContainer;
1037
570
  SourceLocation EndLoc = D->getSourceRange().getEnd();
1038
570
  SourceManager &SM = AU->getSourceManager();
1039
570
  if (EndLoc.isValid()) {
1040
570
    if (DI_current) {
1041
97
      addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
1042
97
                                DeclsInContainer);
1043
473
    } else {
1044
473
      addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
1045
473
                                DeclsInContainer);
1046
473
    }
1047
570
  }
1048
1049
  // The common case.
1050
570
  if (DeclsInContainer.empty())
1051
563
    return VisitDeclContext(D);
1052
1053
  // Get all the Decls in the DeclContext, and sort them with the
1054
  // additional ones we've collected.  Then visit them.
1055
9
  
for (auto *SubDecl : D->decls())7
{
1056
9
    if (!SubDecl || SubDecl->getLexicalDeclContext() != D ||
1057
9
        SubDecl->getBeginLoc().isInvalid())
1058
0
      continue;
1059
9
    DeclsInContainer.push_back(SubDecl);
1060
9
  }
1061
1062
  // Now sort the Decls so that they appear in lexical order.
1063
15
  llvm::sort(DeclsInContainer, [&SM](Decl *A, Decl *B) {
1064
15
    SourceLocation L_A = A->getBeginLoc();
1065
15
    SourceLocation L_B = B->getBeginLoc();
1066
15
    return L_A != L_B
1067
15
               ? SM.isBeforeInTranslationUnit(L_A, L_B)
1068
15
               : 
SM.isBeforeInTranslationUnit(A->getEndLoc(), B->getEndLoc())0
;
1069
15
  });
1070
1071
  // Now visit the decls.
1072
7
  for (SmallVectorImpl<Decl *>::iterator I = DeclsInContainer.begin(),
1073
7
                                         E = DeclsInContainer.end();
1074
29
       I != E; 
++I22
) {
1075
22
    CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
1076
22
    const std::optional<bool> &V = shouldVisitCursor(Cursor);
1077
22
    if (!V)
1078
11
      continue;
1079
11
    if (!*V)
1080
0
      return false;
1081
11
    if (Visit(Cursor, true))
1082
0
      return true;
1083
11
  }
1084
7
  return false;
1085
7
}
1086
1087
128
bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
1088
128
  if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
1089
128
                                   TU)))
1090
0
    return true;
1091
1092
128
  if (VisitObjCTypeParamList(ND->getTypeParamList()))
1093
0
    return true;
1094
1095
128
  ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
1096
128
  for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
1097
128
                                           E = ND->protocol_end();
1098
130
       I != E; 
++I, ++PL2
)
1099
2
    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1100
0
      return true;
1101
1102
128
  return VisitObjCContainerDecl(ND);
1103
128
}
1104
1105
109
bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1106
109
  if (!PID->isThisDeclarationADefinition())
1107
0
    return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
1108
1109
109
  ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
1110
109
  for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
1111
109
                                           E = PID->protocol_end();
1112
157
       I != E; 
++I, ++PL48
)
1113
48
    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1114
0
      return true;
1115
1116
109
  return VisitObjCContainerDecl(PID);
1117
109
}
1118
1119
70
bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
1120
70
  if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
1121
0
    return true;
1122
1123
  // FIXME: This implements a workaround with @property declarations also being
1124
  // installed in the DeclContext for the @interface.  Eventually this code
1125
  // should be removed.
1126
70
  ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
1127
70
  if (!CDecl || 
!CDecl->IsClassExtension()18
)
1128
57
    return false;
1129
1130
13
  ObjCInterfaceDecl *ID = CDecl->getClassInterface();
1131
13
  if (!ID)
1132
0
    return false;
1133
1134
13
  IdentifierInfo *PropertyId = PD->getIdentifier();
1135
13
  ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
1136
13
      cast<DeclContext>(ID), PropertyId, PD->getQueryKind());
1137
1138
13
  if (!prevDecl)
1139
0
    return false;
1140
1141
  // Visit synthesized methods since they will be skipped when visiting
1142
  // the @interface.
1143
13
  if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1144
13
    if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1145
10
      if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1146
0
        return true;
1147
1148
13
  if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1149
13
    if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl)
1150
12
      if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1151
0
        return true;
1152
1153
13
  return false;
1154
13
}
1155
1156
491
bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) {
1157
491
  if (!typeParamList)
1158
483
    return false;
1159
1160
15
  
for (auto *typeParam : *typeParamList)8
{
1161
    // Visit the type parameter.
1162
15
    if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest)))
1163
0
      return true;
1164
15
  }
1165
1166
8
  return false;
1167
8
}
1168
1169
393
bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1170
393
  if (!D->isThisDeclarationADefinition()) {
1171
    // Forward declaration is treated like a reference.
1172
30
    return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
1173
30
  }
1174
1175
  // Objective-C type parameters.
1176
363
  if (VisitObjCTypeParamList(D->getTypeParamListAsWritten()))
1177
0
    return true;
1178
1179
  // Issue callbacks for super class.
1180
363
  if (D->getSuperClass() && Visit(MakeCursorObjCSuperClassRef(
1181
136
                                D->getSuperClass(), D->getSuperClassLoc(), TU)))
1182
0
    return true;
1183
1184
363
  if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo())
1185
136
    if (Visit(SuperClassTInfo->getTypeLoc()))
1186
6
      return true;
1187
1188
357
  ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1189
357
  for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1190
357
                                            E = D->protocol_end();
1191
443
       I != E; 
++I, ++PL86
)
1192
86
    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1193
0
      return true;
1194
1195
357
  return VisitObjCContainerDecl(D);
1196
357
}
1197
1198
70
bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1199
70
  return VisitObjCContainerDecl(D);
1200
70
}
1201
1202
7
bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1203
  // 'ID' could be null when dealing with invalid code.
1204
7
  if (ObjCInterfaceDecl *ID = D->getClassInterface())
1205
7
    if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1206
0
      return true;
1207
1208
7
  return VisitObjCImplDecl(D);
1209
7
}
1210
1211
63
bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1212
#if 0
1213
  // Issue callbacks for super class.
1214
  // FIXME: No source location information!
1215
  if (D->getSuperClass() &&
1216
      Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1217
                                        D->getSuperClassLoc(),
1218
                                        TU)))
1219
    return true;
1220
#endif
1221
1222
63
  return VisitObjCImplDecl(D);
1223
63
}
1224
1225
20
bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1226
20
  if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1227
16
    if (PD->isIvarNameSpecified())
1228
12
      return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1229
1230
8
  return false;
1231
20
}
1232
1233
109
bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1234
109
  return VisitDeclContext(D);
1235
109
}
1236
1237
7
bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1238
  // Visit nested-name-specifier.
1239
7
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1240
2
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1241
0
      return true;
1242
1243
7
  return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1244
7
                                      D->getTargetNameLoc(), TU));
1245
7
}
1246
1247
8
bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1248
  // Visit nested-name-specifier.
1249
8
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1250
8
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1251
0
      return true;
1252
8
  }
1253
1254
8
  if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1255
0
    return true;
1256
1257
8
  return VisitDeclarationNameInfo(D->getNameInfo());
1258
8
}
1259
1260
5
bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1261
  // Visit nested-name-specifier.
1262
5
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1263
1
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1264
0
      return true;
1265
1266
5
  return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1267
5
                                      D->getIdentLocation(), TU));
1268
5
}
1269
1270
4
bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1271
  // Visit nested-name-specifier.
1272
4
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1273
4
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1274
0
      return true;
1275
4
  }
1276
1277
4
  return VisitDeclarationNameInfo(D->getNameInfo());
1278
4
}
1279
1280
bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1281
3
    UnresolvedUsingTypenameDecl *D) {
1282
  // Visit nested-name-specifier.
1283
3
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1284
3
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1285
0
      return true;
1286
1287
3
  return false;
1288
3
}
1289
1290
1
bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) {
1291
1
  if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest)))
1292
0
    return true;
1293
1
  if (StringLiteral *Message = D->getMessage())
1294
1
    if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest)))
1295
0
      return true;
1296
1
  return false;
1297
1
}
1298
1299
41
bool CursorVisitor::VisitFriendDecl(FriendDecl *D) {
1300
41
  if (NamedDecl *FriendD = D->getFriendDecl()) {
1301
32
    if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest)))
1302
0
      return true;
1303
32
  } else 
if (TypeSourceInfo *9
TI9
= D->getFriendType()) {
1304
9
    if (Visit(TI->getTypeLoc()))
1305
0
      return true;
1306
9
  }
1307
41
  return false;
1308
41
}
1309
1310
1
bool CursorVisitor::VisitDecompositionDecl(DecompositionDecl *D) {
1311
2
  for (auto *B : D->bindings()) {
1312
2
    if (Visit(MakeCXCursor(B, TU, RegionOfInterest)))
1313
0
      return true;
1314
2
  }
1315
1
  return VisitVarDecl(D);
1316
1
}
1317
1318
8
bool CursorVisitor::VisitConceptDecl(ConceptDecl *D) {
1319
8
  if (VisitTemplateParameters(D->getTemplateParameters()))
1320
0
    return true;
1321
1322
8
  if (auto *E = D->getConstraintExpr()) {
1323
8
    if (Visit(MakeCXCursor(E, D, TU, RegionOfInterest)))
1324
0
      return true;
1325
8
  }
1326
8
  return false;
1327
8
}
1328
1329
4
bool CursorVisitor::VisitTypeConstraint(const TypeConstraint &TC) {
1330
4
  if (TC.getNestedNameSpecifierLoc()) {
1331
2
    if (VisitNestedNameSpecifierLoc(TC.getNestedNameSpecifierLoc()))
1332
0
      return true;
1333
2
  }
1334
4
  if (TC.getNamedConcept()) {
1335
4
    if (Visit(MakeCursorTemplateRef(TC.getNamedConcept(),
1336
4
                                    TC.getConceptNameLoc(), TU)))
1337
0
      return true;
1338
4
  }
1339
4
  if (auto Args = TC.getTemplateArgsAsWritten()) {
1340
1
    for (const auto &Arg : Args->arguments()) {
1341
1
      if (VisitTemplateArgumentLoc(Arg))
1342
0
        return true;
1343
1
    }
1344
1
  }
1345
4
  return false;
1346
4
}
1347
1348
6
bool CursorVisitor::VisitConceptRequirement(const concepts::Requirement &R) {
1349
6
  using namespace concepts;
1350
6
  switch (R.getKind()) {
1351
1
  case Requirement::RK_Type: {
1352
1
    const TypeRequirement &TR = cast<TypeRequirement>(R);
1353
1
    if (!TR.isSubstitutionFailure()) {
1354
1
      if (Visit(TR.getType()->getTypeLoc()))
1355
0
        return true;
1356
1
    }
1357
1
    break;
1358
1
  }
1359
2
  case Requirement::RK_Simple:
1360
4
  case Requirement::RK_Compound: {
1361
4
    const ExprRequirement &ER = cast<ExprRequirement>(R);
1362
4
    if (!ER.isExprSubstitutionFailure()) {
1363
4
      if (Visit(ER.getExpr()))
1364
0
        return true;
1365
4
    }
1366
4
    if (ER.getKind() == Requirement::RK_Compound) {
1367
2
      const auto &RTR = ER.getReturnTypeRequirement();
1368
2
      if (RTR.isTypeConstraint()) {
1369
2
        if (const auto *Cons = RTR.getTypeConstraint())
1370
2
          VisitTypeConstraint(*Cons);
1371
2
      }
1372
2
    }
1373
4
    break;
1374
4
  }
1375
1
  case Requirement::RK_Nested: {
1376
1
    const NestedRequirement &NR = cast<NestedRequirement>(R);
1377
1
    if (!NR.hasInvalidConstraint()) {
1378
1
      if (Visit(NR.getConstraintExpr()))
1379
0
        return true;
1380
1
    }
1381
1
    break;
1382
1
  }
1383
6
  }
1384
6
  return false;
1385
6
}
1386
1387
3.69k
bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1388
3.69k
  switch (Name.getName().getNameKind()) {
1389
3.42k
  case clang::DeclarationName::Identifier:
1390
3.43k
  case clang::DeclarationName::CXXLiteralOperatorName:
1391
3.43k
  case clang::DeclarationName::CXXDeductionGuideName:
1392
3.58k
  case clang::DeclarationName::CXXOperatorName:
1393
3.58k
  case clang::DeclarationName::CXXUsingDirective:
1394
3.58k
    return false;
1395
1396
101
  case clang::DeclarationName::CXXConstructorName:
1397
101
  case clang::DeclarationName::CXXDestructorName:
1398
112
  case clang::DeclarationName::CXXConversionFunctionName:
1399
112
    if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1400
11
      return Visit(TSInfo->getTypeLoc());
1401
101
    return false;
1402
1403
0
  case clang::DeclarationName::ObjCZeroArgSelector:
1404
0
  case clang::DeclarationName::ObjCOneArgSelector:
1405
0
  case clang::DeclarationName::ObjCMultiArgSelector:
1406
    // FIXME: Per-identifier location info?
1407
0
    return false;
1408
3.69k
  }
1409
1410
0
  llvm_unreachable("Invalid DeclarationName::Kind!");
1411
0
}
1412
1413
bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1414
0
                                             SourceRange Range) {
1415
  // FIXME: This whole routine is a hack to work around the lack of proper
1416
  // source information in nested-name-specifiers (PR5791). Since we do have
1417
  // a beginning source location, we can visit the first component of the
1418
  // nested-name-specifier, if it's a single-token component.
1419
0
  if (!NNS)
1420
0
    return false;
1421
1422
  // Get the first component in the nested-name-specifier.
1423
0
  while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1424
0
    NNS = Prefix;
1425
1426
0
  switch (NNS->getKind()) {
1427
0
  case NestedNameSpecifier::Namespace:
1428
0
    return Visit(
1429
0
        MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), TU));
1430
1431
0
  case NestedNameSpecifier::NamespaceAlias:
1432
0
    return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1433
0
                                        Range.getBegin(), TU));
1434
1435
0
  case NestedNameSpecifier::TypeSpec: {
1436
    // If the type has a form where we know that the beginning of the source
1437
    // range matches up with a reference cursor. Visit the appropriate reference
1438
    // cursor.
1439
0
    const Type *T = NNS->getAsType();
1440
0
    if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1441
0
      return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1442
0
    if (const TagType *Tag = dyn_cast<TagType>(T))
1443
0
      return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1444
0
    if (const TemplateSpecializationType *TST =
1445
0
            dyn_cast<TemplateSpecializationType>(T))
1446
0
      return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1447
0
    break;
1448
0
  }
1449
1450
0
  case NestedNameSpecifier::TypeSpecWithTemplate:
1451
0
  case NestedNameSpecifier::Global:
1452
0
  case NestedNameSpecifier::Identifier:
1453
0
  case NestedNameSpecifier::Super:
1454
0
    break;
1455
0
  }
1456
1457
0
  return false;
1458
0
}
1459
1460
bool CursorVisitor::VisitNestedNameSpecifierLoc(
1461
1.23k
    NestedNameSpecifierLoc Qualifier) {
1462
1.23k
  SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1463
1.52k
  for (; Qualifier; 
Qualifier = Qualifier.getPrefix()285
)
1464
285
    Qualifiers.push_back(Qualifier);
1465
1466
1.52k
  while (!Qualifiers.empty()) {
1467
285
    NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1468
285
    NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1469
285
    switch (NNS->getKind()) {
1470
141
    case NestedNameSpecifier::Namespace:
1471
141
      if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
1472
141
                                       Q.getLocalBeginLoc(), TU)))
1473
0
        return true;
1474
1475
141
      break;
1476
1477
141
    case NestedNameSpecifier::NamespaceAlias:
1478
17
      if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1479
17
                                       Q.getLocalBeginLoc(), TU)))
1480
0
        return true;
1481
1482
17
      break;
1483
1484
109
    case NestedNameSpecifier::TypeSpec:
1485
111
    case NestedNameSpecifier::TypeSpecWithTemplate:
1486
111
      if (Visit(Q.getTypeLoc()))
1487
0
        return true;
1488
1489
111
      break;
1490
1491
111
    case NestedNameSpecifier::Global:
1492
16
    case NestedNameSpecifier::Identifier:
1493
16
    case NestedNameSpecifier::Super:
1494
16
      break;
1495
285
    }
1496
285
  }
1497
1498
1.23k
  return false;
1499
1.23k
}
1500
1501
bool CursorVisitor::VisitTemplateParameters(
1502
315
    const TemplateParameterList *Params) {
1503
315
  if (!Params)
1504
0
    return false;
1505
1506
315
  for (TemplateParameterList::const_iterator P = Params->begin(),
1507
315
                                             PEnd = Params->end();
1508
715
       P != PEnd; 
++P400
) {
1509
400
    if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
1510
0
      return true;
1511
400
  }
1512
1513
315
  if (const auto *E = Params->getRequiresClause()) {
1514
3
    if (Visit(MakeCXCursor(E, nullptr, TU, RegionOfInterest)))
1515
0
      return true;
1516
3
  }
1517
1518
315
  return false;
1519
315
}
1520
1521
167
bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1522
167
  switch (Name.getKind()) {
1523
166
  case TemplateName::Template:
1524
166
  case TemplateName::UsingTemplate:
1525
166
  case TemplateName::QualifiedTemplate: // FIXME: Visit nested-name-specifier.
1526
166
    return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1527
1528
0
  case TemplateName::OverloadedTemplate:
1529
    // Visit the overloaded template set.
1530
0
    if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1531
0
      return true;
1532
1533
0
    return false;
1534
1535
0
  case TemplateName::AssumedTemplate:
1536
    // FIXME: Visit DeclarationName?
1537
0
    return false;
1538
1539
1
  case TemplateName::DependentTemplate:
1540
    // FIXME: Visit nested-name-specifier.
1541
1
    return false;
1542
1543
0
  case TemplateName::SubstTemplateTemplateParm:
1544
0
    return Visit(MakeCursorTemplateRef(
1545
0
        Name.getAsSubstTemplateTemplateParm()->getParameter(), Loc, TU));
1546
1547
0
  case TemplateName::SubstTemplateTemplateParmPack:
1548
0
    return Visit(MakeCursorTemplateRef(
1549
0
        Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), Loc,
1550
0
        TU));
1551
167
  }
1552
1553
0
  llvm_unreachable("Invalid TemplateName::Kind!");
1554
0
}
1555
1556
296
bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1557
296
  switch (TAL.getArgument().getKind()) {
1558
0
  case TemplateArgument::Null:
1559
0
  case TemplateArgument::Integral:
1560
0
  case TemplateArgument::Pack:
1561
0
    return false;
1562
1563
276
  case TemplateArgument::Type:
1564
276
    if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1565
276
      return Visit(TSInfo->getTypeLoc());
1566
0
    return false;
1567
1568
0
  case TemplateArgument::Declaration:
1569
0
    if (Expr *E = TAL.getSourceDeclExpression())
1570
0
      return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1571
0
    return false;
1572
1573
0
  case TemplateArgument::NullPtr:
1574
0
    if (Expr *E = TAL.getSourceNullPtrExpression())
1575
0
      return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1576
0
    return false;
1577
1578
14
  case TemplateArgument::Expression:
1579
14
    if (Expr *E = TAL.getSourceExpression())
1580
14
      return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1581
0
    return false;
1582
1583
6
  case TemplateArgument::Template:
1584
6
  case TemplateArgument::TemplateExpansion:
1585
6
    if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1586
0
      return true;
1587
1588
6
    return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
1589
6
                             TAL.getTemplateNameLoc());
1590
296
  }
1591
1592
0
  llvm_unreachable("Invalid TemplateArgument::Kind!");
1593
0
}
1594
1595
7
bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1596
7
  return VisitDeclContext(D);
1597
7
}
1598
1599
206
bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1600
206
  return Visit(TL.getUnqualifiedLoc());
1601
206
}
1602
1603
3.61k
bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1604
3.61k
  ASTContext &Context = AU->getASTContext();
1605
1606
  // Some builtin types (such as Objective-C's "id", "sel", and
1607
  // "Class") have associated declarations. Create cursors for those.
1608
3.61k
  QualType VisitType;
1609
3.61k
  switch (TL.getTypePtr()->getKind()) {
1610
1611
1.39k
  case BuiltinType::Void:
1612
1.39k
  case BuiltinType::NullPtr:
1613
1.39k
  case BuiltinType::Dependent:
1614
1.39k
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix)                   \
1615
51.0k
  case BuiltinType::Id:
1616
51.0k
#include 
"clang/Basic/OpenCLImageTypes.def"1.39k
1617
51.0k
#define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) 
case BuiltinType::Id:17.2k
1618
51.0k
#include 
"clang/Basic/OpenCLExtensionTypes.def"1.43k
1619
17.2k
  case BuiltinType::OCLSampler:
1620
1.43k
  case BuiltinType::OCLEvent:
1621
1.43k
  case BuiltinType::OCLClkEvent:
1622
1.43k
  case BuiltinType::OCLQueue:
1623
1.43k
  case BuiltinType::OCLReserveID:
1624
74.6k
#define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1625
74.6k
#include 
"clang/Basic/AArch64SVEACLETypes.def"1.43k
1626
74.6k
#define PPC_VECTOR_TYPE(Name, Id, Size) 
case BuiltinType::Id:2.87k
1627
74.6k
#include 
"clang/Basic/PPCTypes.def"1.43k
1628
96.1k
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
1629
96.1k
#include 
"clang/Basic/RISCVVTypes.def"1.43k
1630
96.1k
#define WASM_TYPE(Name, Id, SingletonId) 
case BuiltinType::Id:1.43k
1631
96.1k
#include 
"clang/Basic/WebAssemblyReferenceTypes.def"1.43k
1632
1.43k
#define BUILTIN_TYPE(Id, SingletonId)
1633
58.5k
#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1634
56.8k
#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1635
28.3k
#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1636
35.6k
#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1637
3.56k
#include 
"clang/AST/BuiltinTypes.def"1.43k
1638
3.56k
    break;
1639
1640
48
  case BuiltinType::ObjCId:
1641
48
    VisitType = Context.getObjCIdType();
1642
48
    break;
1643
1644
0
  case BuiltinType::ObjCClass:
1645
0
    VisitType = Context.getObjCClassType();
1646
0
    break;
1647
1648
0
  case BuiltinType::ObjCSel:
1649
0
    VisitType = Context.getObjCSelType();
1650
0
    break;
1651
3.61k
  }
1652
1653
3.61k
  if (!VisitType.isNull()) {
1654
48
    if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1655
48
      return Visit(
1656
48
          MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), TU));
1657
48
  }
1658
1659
3.56k
  return false;
1660
3.61k
}
1661
1662
384
bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1663
384
  return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
1664
384
}
1665
1666
0
bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1667
0
  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1668
0
}
1669
1670
648
bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1671
648
  if (TL.isDefinition())
1672
68
    return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
1673
1674
580
  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1675
648
}
1676
1677
302
bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1678
302
  if (const auto *TC = TL.getDecl()->getTypeConstraint()) {
1679
1
    if (VisitTypeConstraint(*TC))
1680
0
      return true;
1681
1
  }
1682
1683
302
  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1684
302
}
1685
1686
322
bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1687
322
  return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU));
1688
322
}
1689
1690
5
bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
1691
5
  if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getBeginLoc(), TU)))
1692
0
    return true;
1693
5
  for (unsigned I = 0, N = TL.getNumProtocols(); I != N; 
++I0
) {
1694
0
    if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1695
0
                                        TU)))
1696
0
      return true;
1697
0
  }
1698
1699
5
  return false;
1700
5
}
1701
1702
56
bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1703
56
  if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1704
0
    return true;
1705
1706
70
  
for (unsigned I = 0, N = TL.getNumTypeArgs(); 56
I != N;
++I14
) {
1707
14
    if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc()))
1708
0
      return true;
1709
14
  }
1710
1711
106
  
for (unsigned I = 0, N = TL.getNumProtocols(); 56
I != N;
++I50
) {
1712
50
    if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1713
50
                                        TU)))
1714
0
      return true;
1715
50
  }
1716
1717
56
  return false;
1718
56
}
1719
1720
203
bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1721
203
  return Visit(TL.getPointeeLoc());
1722
203
}
1723
1724
41
bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1725
41
  return Visit(TL.getInnerLoc());
1726
41
}
1727
1728
0
bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
1729
0
  return Visit(TL.getInnerLoc());
1730
0
}
1731
1732
288
bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1733
288
  return Visit(TL.getPointeeLoc());
1734
288
}
1735
1736
4
bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1737
4
  return Visit(TL.getPointeeLoc());
1738
4
}
1739
1740
3
bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1741
3
  return Visit(TL.getPointeeLoc());
1742
3
}
1743
1744
131
bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1745
131
  return Visit(TL.getPointeeLoc());
1746
131
}
1747
1748
20
bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1749
20
  return Visit(TL.getPointeeLoc());
1750
20
}
1751
1752
3
bool CursorVisitor::VisitUsingTypeLoc(UsingTypeLoc TL) {
1753
3
  auto *underlyingDecl = TL.getUnderlyingType()->getAsTagDecl();
1754
3
  if (underlyingDecl) {
1755
3
    return Visit(MakeCursorTypeRef(underlyingDecl, TL.getNameLoc(), TU));
1756
3
  }
1757
0
  return false;
1758
3
}
1759
1760
10
bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1761
10
  return Visit(TL.getModifiedLoc());
1762
10
}
1763
1764
0
bool CursorVisitor::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
1765
0
  return Visit(TL.getWrappedLoc());
1766
0
}
1767
1768
bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1769
2.00k
                                         bool SkipResultType) {
1770
2.00k
  if (!SkipResultType && 
Visit(TL.getReturnLoc())63
)
1771
0
    return true;
1772
1773
3.55k
  
for (unsigned I = 0, N = TL.getNumParams(); 2.00k
I != N;
++I1.55k
)
1774
1.55k
    if (Decl *D = TL.getParam(I))
1775
1.55k
      if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
1776
0
        return true;
1777
1778
2.00k
  return false;
1779
2.00k
}
1780
1781
54
bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1782
54
  if (Visit(TL.getElementLoc()))
1783
0
    return true;
1784
1785
54
  if (Expr *Size = TL.getSizeExpr())
1786
26
    return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
1787
1788
28
  return false;
1789
54
}
1790
1791
0
bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
1792
0
  return Visit(TL.getOriginalLoc());
1793
0
}
1794
1795
0
bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
1796
0
  return Visit(TL.getOriginalLoc());
1797
0
}
1798
1799
bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc(
1800
0
    DeducedTemplateSpecializationTypeLoc TL) {
1801
0
  if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1802
0
                        TL.getTemplateNameLoc()))
1803
0
    return true;
1804
1805
0
  return false;
1806
0
}
1807
1808
bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1809
161
    TemplateSpecializationTypeLoc TL) {
1810
  // Visit the template name.
1811
161
  if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1812
161
                        TL.getTemplateNameLoc()))
1813
0
    return true;
1814
1815
  // Visit the template arguments.
1816
368
  
for (unsigned I = 0, N = TL.getNumArgs(); 161
I != N;
++I207
)
1817
207
    if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1818
0
      return true;
1819
1820
161
  return false;
1821
161
}
1822
1823
0
bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1824
0
  return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1825
0
}
1826
1827
0
bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1828
0
  if (TypeSourceInfo *TSInfo = TL.getUnmodifiedTInfo())
1829
0
    return Visit(TSInfo->getTypeLoc());
1830
1831
0
  return false;
1832
0
}
1833
1834
0
bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1835
0
  if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1836
0
    return Visit(TSInfo->getTypeLoc());
1837
1838
0
  return false;
1839
0
}
1840
1841
22
bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1842
22
  return VisitNestedNameSpecifierLoc(TL.getQualifierLoc());
1843
22
}
1844
1845
bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1846
4
    DependentTemplateSpecializationTypeLoc TL) {
1847
  // Visit the nested-name-specifier, if there is one.
1848
4
  if (TL.getQualifierLoc() && VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1849
0
    return true;
1850
1851
  // Visit the template arguments.
1852
8
  
for (unsigned I = 0, N = TL.getNumArgs(); 4
I != N;
++I4
)
1853
4
    if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1854
0
      return true;
1855
1856
4
  return false;
1857
4
}
1858
1859
1.11k
bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1860
1.11k
  if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1861
0
    return true;
1862
1863
1.11k
  return Visit(TL.getNamedTypeLoc());
1864
1.11k
}
1865
1866
1
bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1867
1
  return Visit(TL.getPatternLoc());
1868
1
}
1869
1870
1
bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1871
1
  if (Expr *E = TL.getUnderlyingExpr())
1872
1
    return Visit(MakeCXCursor(E, StmtParent, TU));
1873
1874
0
  return false;
1875
1
}
1876
1877
13
bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1878
13
  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1879
13
}
1880
1881
3
bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1882
3
  return Visit(TL.getValueLoc());
1883
3
}
1884
1885
2
bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) {
1886
2
  return Visit(TL.getValueLoc());
1887
2
}
1888
1889
#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT)                                    \
1890
1.02k
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
1.02k
    return Visit##PARENT##Loc(TL);                                             \
1892
1.02k
  }
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitComplexTypeLoc(clang::ComplexTypeLoc)
clang::cxcursor::CursorVisitor::VisitConstantArrayTypeLoc(clang::ConstantArrayTypeLoc)
Line
Count
Source
1890
18
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
18
    return Visit##PARENT##Loc(TL);                                             \
1892
18
  }
clang::cxcursor::CursorVisitor::VisitIncompleteArrayTypeLoc(clang::IncompleteArrayTypeLoc)
Line
Count
Source
1890
28
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
28
    return Visit##PARENT##Loc(TL);                                             \
1892
28
  }
clang::cxcursor::CursorVisitor::VisitVariableArrayTypeLoc(clang::VariableArrayTypeLoc)
Line
Count
Source
1890
5
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
5
    return Visit##PARENT##Loc(TL);                                             \
1892
5
  }
clang::cxcursor::CursorVisitor::VisitDependentSizedArrayTypeLoc(clang::DependentSizedArrayTypeLoc)
Line
Count
Source
1890
3
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
3
    return Visit##PARENT##Loc(TL);                                             \
1892
3
  }
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitDependentAddressSpaceTypeLoc(clang::DependentAddressSpaceTypeLoc)
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitDependentVectorTypeLoc(clang::DependentVectorTypeLoc)
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitDependentSizedExtVectorTypeLoc(clang::DependentSizedExtVectorTypeLoc)
clang::cxcursor::CursorVisitor::VisitVectorTypeLoc(clang::VectorTypeLoc)
Line
Count
Source
1890
117
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
117
    return Visit##PARENT##Loc(TL);                                             \
1892
117
  }
clang::cxcursor::CursorVisitor::VisitExtVectorTypeLoc(clang::ExtVectorTypeLoc)
Line
Count
Source
1890
114
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
114
    return Visit##PARENT##Loc(TL);                                             \
1892
114
  }
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitConstantMatrixTypeLoc(clang::ConstantMatrixTypeLoc)
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitDependentSizedMatrixTypeLoc(clang::DependentSizedMatrixTypeLoc)
clang::cxcursor::CursorVisitor::VisitFunctionProtoTypeLoc(clang::FunctionProtoTypeLoc)
Line
Count
Source
1890
60
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
60
    return Visit##PARENT##Loc(TL);                                             \
1892
60
  }
clang::cxcursor::CursorVisitor::VisitFunctionNoProtoTypeLoc(clang::FunctionNoProtoTypeLoc)
Line
Count
Source
1890
3
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
3
    return Visit##PARENT##Loc(TL);                                             \
1892
3
  }
clang::cxcursor::CursorVisitor::VisitRecordTypeLoc(clang::RecordTypeLoc)
Line
Count
Source
1890
621
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
621
    return Visit##PARENT##Loc(TL);                                             \
1892
621
  }
clang::cxcursor::CursorVisitor::VisitEnumTypeLoc(clang::EnumTypeLoc)
Line
Count
Source
1890
27
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
27
    return Visit##PARENT##Loc(TL);                                             \
1892
27
  }
clang::cxcursor::CursorVisitor::VisitSubstTemplateTypeParmTypeLoc(clang::SubstTemplateTypeParmTypeLoc)
Line
Count
Source
1890
3
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
3
    return Visit##PARENT##Loc(TL);                                             \
1892
3
  }
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitSubstTemplateTypeParmPackTypeLoc(clang::SubstTemplateTypeParmPackTypeLoc)
clang::cxcursor::CursorVisitor::VisitAutoTypeLoc(clang::AutoTypeLoc)
Line
Count
Source
1890
24
  bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) {               \
1891
24
    return Visit##PARENT##Loc(TL);                                             \
1892
24
  }
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitBitIntTypeLoc(clang::BitIntTypeLoc)
Unexecuted instantiation: clang::cxcursor::CursorVisitor::VisitDependentBitIntTypeLoc(clang::DependentBitIntTypeLoc)
1893
1894
DEFAULT_TYPELOC_IMPL(Complex, Type)
1895
DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
1896
DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
1897
DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
1898
DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
1899
DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type)
1900
DEFAULT_TYPELOC_IMPL(DependentVector, Type)
1901
DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
1902
DEFAULT_TYPELOC_IMPL(Vector, Type)
1903
DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
1904
DEFAULT_TYPELOC_IMPL(ConstantMatrix, MatrixType)
1905
DEFAULT_TYPELOC_IMPL(DependentSizedMatrix, MatrixType)
1906
DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
1907
DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
1908
DEFAULT_TYPELOC_IMPL(Record, TagType)
1909
DEFAULT_TYPELOC_IMPL(Enum, TagType)
1910
DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
1911
DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
1912
DEFAULT_TYPELOC_IMPL(Auto, Type)
1913
DEFAULT_TYPELOC_IMPL(BitInt, Type)
1914
DEFAULT_TYPELOC_IMPL(DependentBitInt, Type)
1915
1916
630
bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1917
  // Visit the nested-name-specifier, if present.
1918
630
  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1919
1
    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1920
0
      return true;
1921
1922
630
  if (D->isCompleteDefinition()) {
1923
592
    for (const auto &I : D->bases()) {
1924
126
      if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU)))
1925
0
        return true;
1926
126
    }
1927
592
  }
1928
1929
630
  return VisitTagDecl(D);
1930
630
}
1931
1932
7.71k
bool CursorVisitor::VisitAttributes(Decl *D) {
1933
7.71k
  for (const auto *I : D->attrs())
1934
210
    if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes ||
1935
210
         
!I->isImplicit()208
) &&
1936
210
        
Visit(MakeCXCursor(I, D, TU))198
)
1937
0
      return true;
1938
1939
7.71k
  return false;
1940
7.71k
}
1941
1942
//===----------------------------------------------------------------------===//
1943
// Data-recursive visitor methods.
1944
//===----------------------------------------------------------------------===//
1945
1946
namespace {
1947
#define DEF_JOB(NAME, DATA, KIND)                                              \
1948
  class NAME : public VisitorJob {                                             \
1949
  public:                                                                      \
1950
    NAME(const DATA *d, CXCursor parent)                                       \
1951
26.5k
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::PostChildrenVisit::PostChildrenVisit(void const*, CXCursor)
Line
Count
Source
1951
6.38k
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::StmtVisit::StmtVisit(clang::Stmt const*, CXCursor)
Line
Count
Source
1951
18.4k
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::ConceptSpecializationExprVisit::ConceptSpecializationExprVisit(clang::ConceptSpecializationExpr const*, CXCursor)
Line
Count
Source
1951
7
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::DeclRefExprParts::DeclRefExprParts(clang::DeclRefExpr const*, CXCursor)
Line
Count
Source
1951
1.21k
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::LambdaExprParts::LambdaExprParts(clang::LambdaExpr const*, CXCursor)
Line
Count
Source
1951
4
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::MemberExprParts::MemberExprParts(clang::MemberExpr const*, CXCursor)
Line
Count
Source
1951
528
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::OverloadExprParts::OverloadExprParts(clang::OverloadExpr const*, CXCursor)
Line
Count
Source
1951
20
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::RequiresExprVisit::RequiresExprVisit(clang::RequiresExpr const*, CXCursor)
Line
Count
Source
1951
4
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
CIndex.cpp:(anonymous namespace)::SizeOfPackExprParts::SizeOfPackExprParts(clang::SizeOfPackExpr const*, CXCursor)
Line
Count
Source
1951
2
        : VisitorJob(parent, VisitorJob::KIND, d) {}                           \
1952
20.2k
    static bool classof(const VisitorJob *VJ) {                                \
1953
20.2k
      return VJ->getKind() == KIND;                                            \
1954
20.2k
    }                                                                          \
CIndex.cpp:(anonymous namespace)::StmtVisit::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
18.4k
    static bool classof(const VisitorJob *VJ) {                                \
1953
18.4k
      return VJ->getKind() == KIND;                                            \
1954
18.4k
    }                                                                          \
CIndex.cpp:(anonymous namespace)::MemberExprParts::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
528
    static bool classof(const VisitorJob *VJ) {                                \
1953
528
      return VJ->getKind() == KIND;                                            \
1954
528
    }                                                                          \
CIndex.cpp:(anonymous namespace)::DeclRefExprParts::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
1.21k
    static bool classof(const VisitorJob *VJ) {                                \
1953
1.21k
      return VJ->getKind() == KIND;                                            \
1954
1.21k
    }                                                                          \
CIndex.cpp:(anonymous namespace)::OverloadExprParts::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
20
    static bool classof(const VisitorJob *VJ) {                                \
1953
20
      return VJ->getKind() == KIND;                                            \
1954
20
    }                                                                          \
CIndex.cpp:(anonymous namespace)::SizeOfPackExprParts::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
2
    static bool classof(const VisitorJob *VJ) {                                \
1953
2
      return VJ->getKind() == KIND;                                            \
1954
2
    }                                                                          \
CIndex.cpp:(anonymous namespace)::LambdaExprParts::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
4
    static bool classof(const VisitorJob *VJ) {                                \
1953
4
      return VJ->getKind() == KIND;                                            \
1954
4
    }                                                                          \
CIndex.cpp:(anonymous namespace)::ConceptSpecializationExprVisit::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
7
    static bool classof(const VisitorJob *VJ) {                                \
1953
7
      return VJ->getKind() == KIND;                                            \
1954
7
    }                                                                          \
CIndex.cpp:(anonymous namespace)::RequiresExprVisit::classof(clang::cxcursor::VisitorJob const*)
Line
Count
Source
1952
4
    static bool classof(const VisitorJob *VJ) {                                \
1953
4
      return VJ->getKind() == KIND;                                            \
1954
4
    }                                                                          \
Unexecuted instantiation: CIndex.cpp:(anonymous namespace)::PostChildrenVisit::classof(clang::cxcursor::VisitorJob const*)
1955
20.2k
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::StmtVisit::get() const
Line
Count
Source
1955
18.4k
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::MemberExprParts::get() const
Line
Count
Source
1955
528
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::DeclRefExprParts::get() const
Line
Count
Source
1955
1.21k
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::OverloadExprParts::get() const
Line
Count
Source
1955
20
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::SizeOfPackExprParts::get() const
Line
Count
Source
1955
2
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::LambdaExprParts::get() const
Line
Count
Source
1955
4
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::ConceptSpecializationExprVisit::get() const
Line
Count
Source
1955
7
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
CIndex.cpp:(anonymous namespace)::RequiresExprVisit::get() const
Line
Count
Source
1955
4
    const DATA *get() const { return static_cast<const DATA *>(data[0]); }     \
Unexecuted instantiation: CIndex.cpp:(anonymous namespace)::PostChildrenVisit::get() const
1956
  };
1957
1958
DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1959
DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1960
DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1961
DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1962
DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1963
DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1964
DEF_JOB(ConceptSpecializationExprVisit, ConceptSpecializationExpr,
1965
        ConceptSpecializationExprVisitKind)
1966
DEF_JOB(RequiresExprVisit, RequiresExpr, RequiresExprVisitKind)
1967
DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind)
1968
#undef DEF_JOB
1969
1970
class ExplicitTemplateArgsVisit : public VisitorJob {
1971
public:
1972
  ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin,
1973
                            const TemplateArgumentLoc *End, CXCursor parent)
1974
      : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin,
1975
12
                   End) {}
1976
12
  static bool classof(const VisitorJob *VJ) {
1977
12
    return VJ->getKind() == ExplicitTemplateArgsVisitKind;
1978
12
  }
1979
12
  const TemplateArgumentLoc *begin() const {
1980
12
    return static_cast<const TemplateArgumentLoc *>(data[0]);
1981
12
  }
1982
12
  const TemplateArgumentLoc *end() {
1983
12
    return static_cast<const TemplateArgumentLoc *>(data[1]);
1984
12
  }
1985
};
1986
class DeclVisit : public VisitorJob {
1987
public:
1988
  DeclVisit(const Decl *D, CXCursor parent, bool isFirst)
1989
      : VisitorJob(parent, VisitorJob::DeclVisitKind, D,
1990
365
                   isFirst ? (void *)1 : (void *)nullptr) {}
1991
730
  static bool classof(const VisitorJob *VJ) {
1992
730
    return VJ->getKind() == DeclVisitKind;
1993
730
  }
1994
365
  const Decl *get() const { return static_cast<const Decl *>(data[0]); }
1995
365
  bool isFirst() const { return data[1] != nullptr; }
1996
};
1997
class TypeLocVisit : public VisitorJob {
1998
public:
1999
  TypeLocVisit(TypeLoc tl, CXCursor parent)
2000
      : VisitorJob(parent, VisitorJob::TypeLocVisitKind,
2001
190
                   tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
2002
2003
190
  static bool classof(const VisitorJob *VJ) {
2004
190
    return VJ->getKind() == TypeLocVisitKind;
2005
190
  }
2006
2007
190
  TypeLoc get() const {
2008
190
    QualType T = QualType::getFromOpaquePtr(data[0]);
2009
190
    return TypeLoc(T, const_cast<void *>(data[1]));
2010
190
  }
2011
};
2012
2013
class LabelRefVisit : public VisitorJob {
2014
public:
2015
  LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
2016
      : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
2017
3
                   labelLoc.getPtrEncoding()) {}
2018
2019
5
  static bool classof(const VisitorJob *VJ) {
2020
5
    return VJ->getKind() == VisitorJob::LabelRefVisitKind;
2021
5
  }
2022
3
  const LabelDecl *get() const {
2023
3
    return static_cast<const LabelDecl *>(data[0]);
2024
3
  }
2025
2
  SourceLocation getLoc() const {
2026
2
    return SourceLocation::getFromPtrEncoding(data[1]);
2027
2
  }
2028
};
2029
2030
class NestedNameSpecifierLocVisit : public VisitorJob {
2031
public:
2032
  NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
2033
      : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
2034
                   Qualifier.getNestedNameSpecifier(),
2035
9
                   Qualifier.getOpaqueData()) {}
2036
2037
9
  static bool classof(const VisitorJob *VJ) {
2038
9
    return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
2039
9
  }
2040
2041
9
  NestedNameSpecifierLoc get() const {
2042
9
    return NestedNameSpecifierLoc(
2043
9
        const_cast<NestedNameSpecifier *>(
2044
9
            static_cast<const NestedNameSpecifier *>(data[0])),
2045
9
        const_cast<void *>(data[1]));
2046
9
  }
2047
};
2048
2049
class DeclarationNameInfoVisit : public VisitorJob {
2050
public:
2051
  DeclarationNameInfoVisit(const Stmt *S, CXCursor parent)
2052
9
      : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
2053
9
  static bool classof(const VisitorJob *VJ) {
2054
9
    return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
2055
9
  }
2056
9
  DeclarationNameInfo get() const {
2057
9
    const Stmt *S = static_cast<const Stmt *>(data[0]);
2058
9
    switch (S->getStmtClass()) {
2059
0
    default:
2060
0
      llvm_unreachable("Unhandled Stmt");
2061
2
    case clang::Stmt::MSDependentExistsStmtClass:
2062
2
      return cast<MSDependentExistsStmt>(S)->getNameInfo();
2063
3
    case Stmt::CXXDependentScopeMemberExprClass:
2064
3
      return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
2065
4
    case Stmt::DependentScopeDeclRefExprClass:
2066
4
      return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
2067
0
    case Stmt::OMPCriticalDirectiveClass:
2068
0
      return cast<OMPCriticalDirective>(S)->getDirectiveName();
2069
9
    }
2070
9
  }
2071
};
2072
class MemberRefVisit : public VisitorJob {
2073
public:
2074
  MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent)
2075
      : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
2076
18
                   L.getPtrEncoding()) {}
2077
18
  static bool classof(const VisitorJob *VJ) {
2078
18
    return VJ->getKind() == VisitorJob::MemberRefVisitKind;
2079
18
  }
2080
18
  const FieldDecl *get() const {
2081
18
    return static_cast<const FieldDecl *>(data[0]);
2082
18
  }
2083
18
  SourceLocation getLoc() const {
2084
18
    return SourceLocation::getFromRawEncoding(
2085
18
        (SourceLocation::UIntTy)(uintptr_t)data[1]);
2086
18
  }
2087
};
2088
class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
2089
  friend class OMPClauseEnqueue;
2090
  VisitorWorkList &WL;
2091
  CXCursor Parent;
2092
2093
public:
2094
  EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
2095
12.9k
      : WL(wl), Parent(parent) {}
2096
2097
  void VisitAddrLabelExpr(const AddrLabelExpr *E);
2098
  void VisitBlockExpr(const BlockExpr *B);
2099
  void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
2100
  void VisitCompoundStmt(const CompoundStmt *S);
2101
0
  void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */
2102
0
  }
2103
  void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S);
2104
  void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E);
2105
  void VisitCXXNewExpr(const CXXNewExpr *E);
2106
  void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
2107
  void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E);
2108
  void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E);
2109
  void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
2110
  void VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2111
  void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E);
2112
  void VisitCXXUuidofExpr(const CXXUuidofExpr *E);
2113
  void VisitCXXCatchStmt(const CXXCatchStmt *S);
2114
  void VisitCXXForRangeStmt(const CXXForRangeStmt *S);
2115
  void VisitDeclRefExpr(const DeclRefExpr *D);
2116
  void VisitDeclStmt(const DeclStmt *S);
2117
  void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E);
2118
  void VisitDesignatedInitExpr(const DesignatedInitExpr *E);
2119
  void VisitExplicitCastExpr(const ExplicitCastExpr *E);
2120
  void VisitForStmt(const ForStmt *FS);
2121
  void VisitGotoStmt(const GotoStmt *GS);
2122
  void VisitIfStmt(const IfStmt *If);
2123
  void VisitInitListExpr(const InitListExpr *IE);
2124
  void VisitMemberExpr(const MemberExpr *M);
2125
  void VisitOffsetOfExpr(const OffsetOfExpr *E);
2126
  void VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
2127
  void VisitObjCMessageExpr(const ObjCMessageExpr *M);
2128
  void VisitOverloadExpr(const OverloadExpr *E);
2129
  void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
2130
  void VisitStmt(const Stmt *S);
2131
  void VisitSwitchStmt(const SwitchStmt *S);
2132
  void VisitWhileStmt(const WhileStmt *W);
2133
  void VisitTypeTraitExpr(const TypeTraitExpr *E);
2134
  void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
2135
  void VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
2136
  void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U);
2137
  void VisitVAArgExpr(const VAArgExpr *E);
2138
  void VisitSizeOfPackExpr(const SizeOfPackExpr *E);
2139
  void VisitPseudoObjectExpr(const PseudoObjectExpr *E);
2140
  void VisitOpaqueValueExpr(const OpaqueValueExpr *E);
2141
  void VisitLambdaExpr(const LambdaExpr *E);
2142
  void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
2143
  void VisitRequiresExpr(const RequiresExpr *E);
2144
  void VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
2145
  void VisitOMPExecutableDirective(const OMPExecutableDirective *D);
2146
  void VisitOMPLoopBasedDirective(const OMPLoopBasedDirective *D);
2147
  void VisitOMPLoopDirective(const OMPLoopDirective *D);
2148
  void VisitOMPParallelDirective(const OMPParallelDirective *D);
2149
  void VisitOMPSimdDirective(const OMPSimdDirective *D);
2150
  void
2151
  VisitOMPLoopTransformationDirective(const OMPLoopTransformationDirective *D);
2152
  void VisitOMPTileDirective(const OMPTileDirective *D);
2153
  void VisitOMPUnrollDirective(const OMPUnrollDirective *D);
2154
  void VisitOMPForDirective(const OMPForDirective *D);
2155
  void VisitOMPForSimdDirective(const OMPForSimdDirective *D);
2156
  void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
2157
  void VisitOMPSectionDirective(const OMPSectionDirective *D);
2158
  void VisitOMPSingleDirective(const OMPSingleDirective *D);
2159
  void VisitOMPMasterDirective(const OMPMasterDirective *D);
2160
  void VisitOMPCriticalDirective(const OMPCriticalDirective *D);
2161
  void VisitOMPParallelForDirective(const OMPParallelForDirective *D);
2162
  void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D);
2163
  void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D);
2164
  void VisitOMPParallelMaskedDirective(const OMPParallelMaskedDirective *D);
2165
  void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D);
2166
  void VisitOMPTaskDirective(const OMPTaskDirective *D);
2167
  void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D);
2168
  void VisitOMPBarrierDirective(const OMPBarrierDirective *D);
2169
  void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D);
2170
  void VisitOMPErrorDirective(const OMPErrorDirective *D);
2171
  void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D);
2172
  void
2173
  VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D);
2174
  void VisitOMPCancelDirective(const OMPCancelDirective *D);
2175
  void VisitOMPFlushDirective(const OMPFlushDirective *D);
2176
  void VisitOMPDepobjDirective(const OMPDepobjDirective *D);
2177
  void VisitOMPScanDirective(const OMPScanDirective *D);
2178
  void VisitOMPOrderedDirective(const OMPOrderedDirective *D);
2179
  void VisitOMPAtomicDirective(const OMPAtomicDirective *D);
2180
  void VisitOMPTargetDirective(const OMPTargetDirective *D);
2181
  void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D);
2182
  void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D);
2183
  void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D);
2184
  void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D);
2185
  void
2186
  VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D);
2187
  void VisitOMPTeamsDirective(const OMPTeamsDirective *D);
2188
  void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D);
2189
  void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D);
2190
  void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D);
2191
  void VisitOMPMaskedTaskLoopDirective(const OMPMaskedTaskLoopDirective *D);
2192
  void
2193
  VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D);
2194
  void VisitOMPMaskedTaskLoopSimdDirective(
2195
      const OMPMaskedTaskLoopSimdDirective *D);
2196
  void VisitOMPParallelMasterTaskLoopDirective(
2197
      const OMPParallelMasterTaskLoopDirective *D);
2198
  void VisitOMPParallelMaskedTaskLoopDirective(
2199
      const OMPParallelMaskedTaskLoopDirective *D);
2200
  void VisitOMPParallelMasterTaskLoopSimdDirective(
2201
      const OMPParallelMasterTaskLoopSimdDirective *D);
2202
  void VisitOMPParallelMaskedTaskLoopSimdDirective(
2203
      const OMPParallelMaskedTaskLoopSimdDirective *D);
2204
  void VisitOMPDistributeDirective(const OMPDistributeDirective *D);
2205
  void VisitOMPDistributeParallelForDirective(
2206
      const OMPDistributeParallelForDirective *D);
2207
  void VisitOMPDistributeParallelForSimdDirective(
2208
      const OMPDistributeParallelForSimdDirective *D);
2209
  void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D);
2210
  void VisitOMPTargetParallelForSimdDirective(
2211
      const OMPTargetParallelForSimdDirective *D);
2212
  void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D);
2213
  void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D);
2214
  void VisitOMPTeamsDistributeSimdDirective(
2215
      const OMPTeamsDistributeSimdDirective *D);
2216
  void VisitOMPTeamsDistributeParallelForSimdDirective(
2217
      const OMPTeamsDistributeParallelForSimdDirective *D);
2218
  void VisitOMPTeamsDistributeParallelForDirective(
2219
      const OMPTeamsDistributeParallelForDirective *D);
2220
  void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D);
2221
  void VisitOMPTargetTeamsDistributeDirective(
2222
      const OMPTargetTeamsDistributeDirective *D);
2223
  void VisitOMPTargetTeamsDistributeParallelForDirective(
2224
      const OMPTargetTeamsDistributeParallelForDirective *D);
2225
  void VisitOMPTargetTeamsDistributeParallelForSimdDirective(
2226
      const OMPTargetTeamsDistributeParallelForSimdDirective *D);
2227
  void VisitOMPTargetTeamsDistributeSimdDirective(
2228
      const OMPTargetTeamsDistributeSimdDirective *D);
2229
2230
private:
2231
  void AddDeclarationNameInfo(const Stmt *S);
2232
  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
2233
  void AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2234
                               unsigned NumTemplateArgs);
2235
  void AddMemberRef(const FieldDecl *D, SourceLocation L);
2236
  void AddStmt(const Stmt *S);
2237
  void AddDecl(const Decl *D, bool isFirst = true);
2238
  void AddTypeLoc(TypeSourceInfo *TI);
2239
  void EnqueueChildren(const Stmt *S);
2240
  void EnqueueChildren(const OMPClause *S);
2241
};
2242
} // namespace
2243
2244
9
void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) {
2245
  // 'S' should always be non-null, since it comes from the
2246
  // statement we are visiting.
2247
9
  WL.push_back(DeclarationNameInfoVisit(S, Parent));
2248
9
}
2249
2250
void EnqueueVisitor::AddNestedNameSpecifierLoc(
2251
9
    NestedNameSpecifierLoc Qualifier) {
2252
9
  if (Qualifier)
2253
9
    WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
2254
9
}
2255
2256
18.4k
void EnqueueVisitor::AddStmt(const Stmt *S) {
2257
18.4k
  if (S)
2258
18.4k
    WL.push_back(StmtVisit(S, Parent));
2259
18.4k
}
2260
385
void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) {
2261
385
  if (D)
2262
365
    WL.push_back(DeclVisit(D, Parent, isFirst));
2263
385
}
2264
void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A,
2265
12
                                             unsigned NumTemplateArgs) {
2266
12
  WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent));
2267
12
}
2268
18
void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) {
2269
18
  if (D)
2270
18
    WL.push_back(MemberRefVisit(D, L, Parent));
2271
18
}
2272
317
void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
2273
317
  if (TI)
2274
190
    WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
2275
317
}
2276
5.09k
void EnqueueVisitor::EnqueueChildren(const Stmt *S) {
2277
5.09k
  unsigned size = WL.size();
2278
5.68k
  for (const Stmt *SubStmt : S->children()) {
2279
5.68k
    AddStmt(SubStmt);
2280
5.68k
  }
2281
5.09k
  if (size == WL.size())
2282
1.00k
    return;
2283
  // Now reverse the entries we just added.  This will match the DFS
2284
  // ordering performed by the worklist.
2285
4.08k
  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2286
4.08k
  std::reverse(I, E);
2287
4.08k
}
2288
namespace {
2289
class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> {
2290
  EnqueueVisitor *Visitor;
2291
  /// Process clauses with list of variables.
2292
  template <typename T> void VisitOMPClauseList(T *Node);
2293
2294
public:
2295
1
  OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) {}
2296
#define GEN_CLANG_CLAUSE_CLASS
2297
#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(const Class *C);
2298
#include "llvm/Frontend/OpenMP/OMP.inc"
2299
  void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C);
2300
  void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C);
2301
};
2302
2303
void OMPClauseEnqueue::VisitOMPClauseWithPreInit(
2304
0
    const OMPClauseWithPreInit *C) {
2305
0
  Visitor->AddStmt(C->getPreInitStmt());
2306
0
}
2307
2308
void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate(
2309
0
    const OMPClauseWithPostUpdate *C) {
2310
0
  VisitOMPClauseWithPreInit(C);
2311
0
  Visitor->AddStmt(C->getPostUpdateExpr());
2312
0
}
2313
2314
0
void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
2315
0
  VisitOMPClauseWithPreInit(C);
2316
0
  Visitor->AddStmt(C->getCondition());
2317
0
}
2318
2319
0
void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) {
2320
0
  Visitor->AddStmt(C->getCondition());
2321
0
}
2322
2323
0
void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
2324
0
  VisitOMPClauseWithPreInit(C);
2325
0
  Visitor->AddStmt(C->getNumThreads());
2326
0
}
2327
2328
0
void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) {
2329
0
  Visitor->AddStmt(C->getSafelen());
2330
0
}
2331
2332
0
void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) {
2333
0
  Visitor->AddStmt(C->getSimdlen());
2334
0
}
2335
2336
1
void OMPClauseEnqueue::VisitOMPSizesClause(const OMPSizesClause *C) {
2337
1
  for (auto E : C->getSizesRefs())
2338
1
    Visitor->AddStmt(E);
2339
1
}
2340
2341
0
void OMPClauseEnqueue::VisitOMPFullClause(const OMPFullClause *C) {}
2342
2343
0
void OMPClauseEnqueue::VisitOMPPartialClause(const OMPPartialClause *C) {
2344
0
  Visitor->AddStmt(C->getFactor());
2345
0
}
2346
2347
0
void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) {
2348
0
  Visitor->AddStmt(C->getAllocator());
2349
0
}
2350
2351
0
void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) {
2352
0
  Visitor->AddStmt(C->getNumForLoops());
2353
0
}
2354
2355
0
void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) {}
2356
2357
0
void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) {}
2358
2359
0
void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) {
2360
0
  VisitOMPClauseWithPreInit(C);
2361
0
  Visitor->AddStmt(C->getChunkSize());
2362
0
}
2363
2364
0
void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) {
2365
0
  Visitor->AddStmt(C->getNumForLoops());
2366
0
}
2367
2368
0
void OMPClauseEnqueue::VisitOMPDetachClause(const OMPDetachClause *C) {
2369
0
  Visitor->AddStmt(C->getEventHandler());
2370
0
}
2371
2372
0
void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {}
2373
2374
0
void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {}
2375
2376
0
void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {}
2377
2378
0
void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {}
2379
2380
0
void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {}
2381
2382
0
void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {}
2383
2384
0
void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
2385
2386
0
void OMPClauseEnqueue::VisitOMPCompareClause(const OMPCompareClause *) {}
2387
2388
0
void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
2389
2390
0
void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {}
2391
2392
0
void OMPClauseEnqueue::VisitOMPAcquireClause(const OMPAcquireClause *) {}
2393
2394
0
void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {}
2395
2396
0
void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {}
2397
2398
0
void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {}
2399
2400
0
void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {}
2401
2402
0
void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {}
2403
2404
0
void OMPClauseEnqueue::VisitOMPInitClause(const OMPInitClause *C) {
2405
0
  VisitOMPClauseList(C);
2406
0
}
2407
2408
0
void OMPClauseEnqueue::VisitOMPUseClause(const OMPUseClause *C) {
2409
0
  Visitor->AddStmt(C->getInteropVar());
2410
0
}
2411
2412
0
void OMPClauseEnqueue::VisitOMPDestroyClause(const OMPDestroyClause *C) {
2413
0
  if (C->getInteropVar())
2414
0
    Visitor->AddStmt(C->getInteropVar());
2415
0
}
2416
2417
0
void OMPClauseEnqueue::VisitOMPNovariantsClause(const OMPNovariantsClause *C) {
2418
0
  Visitor->AddStmt(C->getCondition());
2419
0
}
2420
2421
0
void OMPClauseEnqueue::VisitOMPNocontextClause(const OMPNocontextClause *C) {
2422
0
  Visitor->AddStmt(C->getCondition());
2423
0
}
2424
2425
0
void OMPClauseEnqueue::VisitOMPFilterClause(const OMPFilterClause *C) {
2426
0
  VisitOMPClauseWithPreInit(C);
2427
0
  Visitor->AddStmt(C->getThreadID());
2428
0
}
2429
2430
0
void OMPClauseEnqueue::VisitOMPAlignClause(const OMPAlignClause *C) {
2431
0
  Visitor->AddStmt(C->getAlignment());
2432
0
}
2433
2434
void OMPClauseEnqueue::VisitOMPUnifiedAddressClause(
2435
0
    const OMPUnifiedAddressClause *) {}
2436
2437
void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause(
2438
0
    const OMPUnifiedSharedMemoryClause *) {}
2439
2440
void OMPClauseEnqueue::VisitOMPReverseOffloadClause(
2441
0
    const OMPReverseOffloadClause *) {}
2442
2443
void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause(
2444
0
    const OMPDynamicAllocatorsClause *) {}
2445
2446
void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause(
2447
0
    const OMPAtomicDefaultMemOrderClause *) {}
2448
2449
0
void OMPClauseEnqueue::VisitOMPAtClause(const OMPAtClause *) {}
2450
2451
0
void OMPClauseEnqueue::VisitOMPSeverityClause(const OMPSeverityClause *) {}
2452
2453
0
void OMPClauseEnqueue::VisitOMPMessageClause(const OMPMessageClause *) {}
2454
2455
0
void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) {
2456
0
  Visitor->AddStmt(C->getDevice());
2457
0
}
2458
2459
0
void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) {
2460
0
  VisitOMPClauseWithPreInit(C);
2461
0
  Visitor->AddStmt(C->getNumTeams());
2462
0
}
2463
2464
void OMPClauseEnqueue::VisitOMPThreadLimitClause(
2465
0
    const OMPThreadLimitClause *C) {
2466
0
  VisitOMPClauseWithPreInit(C);
2467
0
  Visitor->AddStmt(C->getThreadLimit());
2468
0
}
2469
2470
0
void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) {
2471
0
  Visitor->AddStmt(C->getPriority());
2472
0
}
2473
2474
0
void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) {
2475
0
  Visitor->AddStmt(C->getGrainsize());
2476
0
}
2477
2478
0
void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) {
2479
0
  Visitor->AddStmt(C->getNumTasks());
2480
0
}
2481
2482
0
void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) {
2483
0
  Visitor->AddStmt(C->getHint());
2484
0
}
2485
2486
0
template <typename T> void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
2487
0
  for (const auto *I : Node->varlists()) {
2488
0
    Visitor->AddStmt(I);
2489
0
  }
2490
0
}
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPAlignedClause const>(clang::OMPAlignedClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPAllocateClause const>(clang::OMPAllocateClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPCopyprivateClause const>(clang::OMPCopyprivateClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPCopyinClause const>(clang::OMPCopyinClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPDependClause const>(clang::OMPDependClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPExclusiveClause const>(clang::OMPExclusiveClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPFirstprivateClause const>(clang::OMPFirstprivateClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPFlushClause const>(clang::OMPFlushClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPFromClause const>(clang::OMPFromClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPHasDeviceAddrClause const>(clang::OMPHasDeviceAddrClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPInReductionClause const>(clang::OMPInReductionClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPInclusiveClause const>(clang::OMPInclusiveClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPInitClause const>(clang::OMPInitClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPIsDevicePtrClause const>(clang::OMPIsDevicePtrClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPLastprivateClause const>(clang::OMPLastprivateClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPLinearClause const>(clang::OMPLinearClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPMapClause const>(clang::OMPMapClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPNontemporalClause const>(clang::OMPNontemporalClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPPrivateClause const>(clang::OMPPrivateClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPReductionClause const>(clang::OMPReductionClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPSharedClause const>(clang::OMPSharedClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPTaskReductionClause const>(clang::OMPTaskReductionClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPToClause const>(clang::OMPToClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPUseDeviceAddrClause const>(clang::OMPUseDeviceAddrClause const*)
Unexecuted instantiation: CIndex.cpp:void (anonymous namespace)::OMPClauseEnqueue::VisitOMPClauseList<clang::OMPUseDevicePtrClause const>(clang::OMPUseDevicePtrClause const*)
2491
2492
0
void OMPClauseEnqueue::VisitOMPInclusiveClause(const OMPInclusiveClause *C) {
2493
0
  VisitOMPClauseList(C);
2494
0
}
2495
0
void OMPClauseEnqueue::VisitOMPExclusiveClause(const OMPExclusiveClause *C) {
2496
0
  VisitOMPClauseList(C);
2497
0
}
2498
0
void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) {
2499
0
  VisitOMPClauseList(C);
2500
0
  Visitor->AddStmt(C->getAllocator());
2501
0
}
2502
0
void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) {
2503
0
  VisitOMPClauseList(C);
2504
0
  for (const auto *E : C->private_copies()) {
2505
0
    Visitor->AddStmt(E);
2506
0
  }
2507
0
}
2508
void OMPClauseEnqueue::VisitOMPFirstprivateClause(
2509
0
    const OMPFirstprivateClause *C) {
2510
0
  VisitOMPClauseList(C);
2511
0
  VisitOMPClauseWithPreInit(C);
2512
0
  for (const auto *E : C->private_copies()) {
2513
0
    Visitor->AddStmt(E);
2514
0
  }
2515
0
  for (const auto *E : C->inits()) {
2516
0
    Visitor->AddStmt(E);
2517
0
  }
2518
0
}
2519
void OMPClauseEnqueue::VisitOMPLastprivateClause(
2520
0
    const OMPLastprivateClause *C) {
2521
0
  VisitOMPClauseList(C);
2522
0
  VisitOMPClauseWithPostUpdate(C);
2523
0
  for (auto *E : C->private_copies()) {
2524
0
    Visitor->AddStmt(E);
2525
0
  }
2526
0
  for (auto *E : C->source_exprs()) {
2527
0
    Visitor->AddStmt(E);
2528
0
  }
2529
0
  for (auto *E : C->destination_exprs()) {
2530
0
    Visitor->AddStmt(E);
2531
0
  }
2532
0
  for (auto *E : C->assignment_ops()) {
2533
0
    Visitor->AddStmt(E);
2534
0
  }
2535
0
}
2536
0
void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) {
2537
0
  VisitOMPClauseList(C);
2538
0
}
2539
0
void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) {
2540
0
  VisitOMPClauseList(C);
2541
0
  VisitOMPClauseWithPostUpdate(C);
2542
0
  for (auto *E : C->privates()) {
2543
0
    Visitor->AddStmt(E);
2544
0
  }
2545
0
  for (auto *E : C->lhs_exprs()) {
2546
0
    Visitor->AddStmt(E);
2547
0
  }
2548
0
  for (auto *E : C->rhs_exprs()) {
2549
0
    Visitor->AddStmt(E);
2550
0
  }
2551
0
  for (auto *E : C->reduction_ops()) {
2552
0
    Visitor->AddStmt(E);
2553
0
  }
2554
0
  if (C->getModifier() == clang::OMPC_REDUCTION_inscan) {
2555
0
    for (auto *E : C->copy_ops()) {
2556
0
      Visitor->AddStmt(E);
2557
0
    }
2558
0
    for (auto *E : C->copy_array_temps()) {
2559
0
      Visitor->AddStmt(E);
2560
0
    }
2561
0
    for (auto *E : C->copy_array_elems()) {
2562
0
      Visitor->AddStmt(E);
2563
0
    }
2564
0
  }
2565
0
}
2566
void OMPClauseEnqueue::VisitOMPTaskReductionClause(
2567
0
    const OMPTaskReductionClause *C) {
2568
0
  VisitOMPClauseList(C);
2569
0
  VisitOMPClauseWithPostUpdate(C);
2570
0
  for (auto *E : C->privates()) {
2571
0
    Visitor->AddStmt(E);
2572
0
  }
2573
0
  for (auto *E : C->lhs_exprs()) {
2574
0
    Visitor->AddStmt(E);
2575
0
  }
2576
0
  for (auto *E : C->rhs_exprs()) {
2577
0
    Visitor->AddStmt(E);
2578
0
  }
2579
0
  for (auto *E : C->reduction_ops()) {
2580
0
    Visitor->AddStmt(E);
2581
0
  }
2582
0
}
2583
void OMPClauseEnqueue::VisitOMPInReductionClause(
2584
0
    const OMPInReductionClause *C) {
2585
0
  VisitOMPClauseList(C);
2586
0
  VisitOMPClauseWithPostUpdate(C);
2587
0
  for (auto *E : C->privates()) {
2588
0
    Visitor->AddStmt(E);
2589
0
  }
2590
0
  for (auto *E : C->lhs_exprs()) {
2591
0
    Visitor->AddStmt(E);
2592
0
  }
2593
0
  for (auto *E : C->rhs_exprs()) {
2594
0
    Visitor->AddStmt(E);
2595
0
  }
2596
0
  for (auto *E : C->reduction_ops()) {
2597
0
    Visitor->AddStmt(E);
2598
0
  }
2599
0
  for (auto *E : C->taskgroup_descriptors())
2600
0
    Visitor->AddStmt(E);
2601
0
}
2602
0
void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) {
2603
0
  VisitOMPClauseList(C);
2604
0
  VisitOMPClauseWithPostUpdate(C);
2605
0
  for (const auto *E : C->privates()) {
2606
0
    Visitor->AddStmt(E);
2607
0
  }
2608
0
  for (const auto *E : C->inits()) {
2609
0
    Visitor->AddStmt(E);
2610
0
  }
2611
0
  for (const auto *E : C->updates()) {
2612
0
    Visitor->AddStmt(E);
2613
0
  }
2614
0
  for (const auto *E : C->finals()) {
2615
0
    Visitor->AddStmt(E);
2616
0
  }
2617
0
  Visitor->AddStmt(C->getStep());
2618
0
  Visitor->AddStmt(C->getCalcStep());
2619
0
}
2620
0
void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) {
2621
0
  VisitOMPClauseList(C);
2622
0
  Visitor->AddStmt(C->getAlignment());
2623
0
}
2624
0
void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) {
2625
0
  VisitOMPClauseList(C);
2626
0
  for (auto *E : C->source_exprs()) {
2627
0
    Visitor->AddStmt(E);
2628
0
  }
2629
0
  for (auto *E : C->destination_exprs()) {
2630
0
    Visitor->AddStmt(E);
2631
0
  }
2632
0
  for (auto *E : C->assignment_ops()) {
2633
0
    Visitor->AddStmt(E);
2634
0
  }
2635
0
}
2636
void OMPClauseEnqueue::VisitOMPCopyprivateClause(
2637
0
    const OMPCopyprivateClause *C) {
2638
0
  VisitOMPClauseList(C);
2639
0
  for (auto *E : C->source_exprs()) {
2640
0
    Visitor->AddStmt(E);
2641
0
  }
2642
0
  for (auto *E : C->destination_exprs()) {
2643
0
    Visitor->AddStmt(E);
2644
0
  }
2645
0
  for (auto *E : C->assignment_ops()) {
2646
0
    Visitor->AddStmt(E);
2647
0
  }
2648
0
}
2649
0
void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) {
2650
0
  VisitOMPClauseList(C);
2651
0
}
2652
0
void OMPClauseEnqueue::VisitOMPDepobjClause(const OMPDepobjClause *C) {
2653
0
  Visitor->AddStmt(C->getDepobj());
2654
0
}
2655
0
void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) {
2656
0
  VisitOMPClauseList(C);
2657
0
}
2658
0
void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) {
2659
0
  VisitOMPClauseList(C);
2660
0
}
2661
void OMPClauseEnqueue::VisitOMPDistScheduleClause(
2662
0
    const OMPDistScheduleClause *C) {
2663
0
  VisitOMPClauseWithPreInit(C);
2664
0
  Visitor->AddStmt(C->getChunkSize());
2665
0
}
2666
void OMPClauseEnqueue::VisitOMPDefaultmapClause(
2667
0
    const OMPDefaultmapClause * /*C*/) {}
2668
0
void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) {
2669
0
  VisitOMPClauseList(C);
2670
0
}
2671
0
void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) {
2672
0
  VisitOMPClauseList(C);
2673
0
}
2674
void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(
2675
0
    const OMPUseDevicePtrClause *C) {
2676
0
  VisitOMPClauseList(C);
2677
0
}
2678
void OMPClauseEnqueue::VisitOMPUseDeviceAddrClause(
2679
0
    const OMPUseDeviceAddrClause *C) {
2680
0
  VisitOMPClauseList(C);
2681
0
}
2682
void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(
2683
0
    const OMPIsDevicePtrClause *C) {
2684
0
  VisitOMPClauseList(C);
2685
0
}
2686
void OMPClauseEnqueue::VisitOMPHasDeviceAddrClause(
2687
0
    const OMPHasDeviceAddrClause *C) {
2688
0
  VisitOMPClauseList(C);
2689
0
}
2690
void OMPClauseEnqueue::VisitOMPNontemporalClause(
2691
0
    const OMPNontemporalClause *C) {
2692
0
  VisitOMPClauseList(C);
2693
0
  for (const auto *E : C->private_refs())
2694
0
    Visitor->AddStmt(E);
2695
0
}
2696
0
void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {}
2697
void OMPClauseEnqueue::VisitOMPUsesAllocatorsClause(
2698
0
    const OMPUsesAllocatorsClause *C) {
2699
0
  for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) {
2700
0
    const OMPUsesAllocatorsClause::Data &D = C->getAllocatorData(I);
2701
0
    Visitor->AddStmt(D.Allocator);
2702
0
    Visitor->AddStmt(D.AllocatorTraits);
2703
0
  }
2704
0
}
2705
0
void OMPClauseEnqueue::VisitOMPAffinityClause(const OMPAffinityClause *C) {
2706
0
  Visitor->AddStmt(C->getModifier());
2707
0
  for (const Expr *E : C->varlists())
2708
0
    Visitor->AddStmt(E);
2709
0
}
2710
0
void OMPClauseEnqueue::VisitOMPBindClause(const OMPBindClause *C) {}
2711
void OMPClauseEnqueue::VisitOMPXDynCGroupMemClause(
2712
0
    const OMPXDynCGroupMemClause *C) {
2713
0
  VisitOMPClauseWithPreInit(C);
2714
0
  Visitor->AddStmt(C->getSize());
2715
0
}
2716
2717
} // namespace
2718
2719
1
void EnqueueVisitor::EnqueueChildren(const OMPClause *S) {
2720
1
  unsigned size = WL.size();
2721
1
  OMPClauseEnqueue Visitor(this);
2722
1
  Visitor.Visit(S);
2723
1
  if (size == WL.size())
2724
0
    return;
2725
  // Now reverse the entries we just added.  This will match the DFS
2726
  // ordering performed by the worklist.
2727
1
  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2728
1
  std::reverse(I, E);
2729
1
}
2730
1
void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2731
1
  WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
2732
1
}
2733
6
void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) {
2734
6
  AddDecl(B->getBlockDecl());
2735
6
}
2736
4
void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2737
4
  EnqueueChildren(E);
2738
4
  AddTypeLoc(E->getTypeSourceInfo());
2739
4
}
2740
820
void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) {
2741
820
  for (auto &I : llvm::reverse(S->body()))
2742
2.34k
    AddStmt(I);
2743
820
}
2744
void EnqueueVisitor::VisitMSDependentExistsStmt(
2745
2
    const MSDependentExistsStmt *S) {
2746
2
  AddStmt(S->getSubStmt());
2747
2
  AddDeclarationNameInfo(S);
2748
2
  if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
2749
2
    AddNestedNameSpecifierLoc(QualifierLoc);
2750
2
}
2751
2752
void EnqueueVisitor::VisitCXXDependentScopeMemberExpr(
2753
3
    const CXXDependentScopeMemberExpr *E) {
2754
3
  if (E->hasExplicitTemplateArgs())
2755
1
    AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2756
3
  AddDeclarationNameInfo(E);
2757
3
  if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2758
2
    AddNestedNameSpecifierLoc(QualifierLoc);
2759
3
  if (!E->isImplicitAccess())
2760
3
    AddStmt(E->getBase());
2761
3
}
2762
5
void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) {
2763
  // Enqueue the initializer , if any.
2764
5
  AddStmt(E->getInitializer());
2765
  // Enqueue the array size, if any.
2766
5
  AddStmt(E->getArraySize().value_or(nullptr));
2767
  // Enqueue the allocated type.
2768
5
  AddTypeLoc(E->getAllocatedTypeSourceInfo());
2769
  // Enqueue the placement arguments.
2770
6
  for (unsigned I = E->getNumPlacementArgs(); I > 0; 
--I1
)
2771
1
    AddStmt(E->getPlacementArg(I - 1));
2772
5
}
2773
4.89k
void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) {
2774
4.92k
  for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; 
--I31
)
2775
31
    AddStmt(CE->getArg(I - 1));
2776
4.89k
  AddStmt(CE->getCallee());
2777
4.89k
  AddStmt(CE->getArg(0));
2778
4.89k
}
2779
void EnqueueVisitor::VisitCXXPseudoDestructorExpr(
2780
2
    const CXXPseudoDestructorExpr *E) {
2781
  // Visit the name of the type being destroyed.
2782
2
  AddTypeLoc(E->getDestroyedTypeInfo());
2783
  // Visit the scope type that looks disturbingly like the nested-name-specifier
2784
  // but isn't.
2785
2
  AddTypeLoc(E->getScopeTypeInfo());
2786
  // Visit the nested-name-specifier.
2787
2
  if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
2788
1
    AddNestedNameSpecifierLoc(QualifierLoc);
2789
  // Visit base expression.
2790
2
  AddStmt(E->getBase());
2791
2
}
2792
void EnqueueVisitor::VisitCXXScalarValueInitExpr(
2793
2
    const CXXScalarValueInitExpr *E) {
2794
2
  AddTypeLoc(E->getTypeSourceInfo());
2795
2
}
2796
void EnqueueVisitor::VisitCXXTemporaryObjectExpr(
2797
39
    const CXXTemporaryObjectExpr *E) {
2798
39
  EnqueueChildren(E);
2799
39
  AddTypeLoc(E->getTypeSourceInfo());
2800
39
}
2801
2
void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2802
2
  EnqueueChildren(E);
2803
2
  if (E->isTypeOperand())
2804
1
    AddTypeLoc(E->getTypeOperandSourceInfo());
2805
2
}
2806
2807
void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(
2808
8
    const CXXUnresolvedConstructExpr *E) {
2809
8
  EnqueueChildren(E);
2810
8
  AddTypeLoc(E->getTypeSourceInfo());
2811
8
}
2812
0
void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
2813
0
  EnqueueChildren(E);
2814
0
  if (E->isTypeOperand())
2815
0
    AddTypeLoc(E->getTypeOperandSourceInfo());
2816
0
}
2817
2818
4
void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) {
2819
4
  EnqueueChildren(S);
2820
4
  AddDecl(S->getExceptionDecl());
2821
4
}
2822
2823
4
void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
2824
4
  AddStmt(S->getBody());
2825
4
  AddStmt(S->getRangeInit());
2826
4
  AddDecl(S->getLoopVariable());
2827
4
}
2828
2829
1.21k
void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) {
2830
1.21k
  if (DR->hasExplicitTemplateArgs())
2831
6
    AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs());
2832
1.21k
  WL.push_back(DeclRefExprParts(DR, Parent));
2833
1.21k
}
2834
void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
2835
4
    const DependentScopeDeclRefExpr *E) {
2836
4
  if (E->hasExplicitTemplateArgs())
2837
1
    AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2838
4
  AddDeclarationNameInfo(E);
2839
4
  AddNestedNameSpecifierLoc(E->getQualifierLoc());
2840
4
}
2841
328
void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
2842
328
  unsigned size = WL.size();
2843
328
  bool isFirst = true;
2844
343
  for (const auto *D : S->decls()) {
2845
343
    AddDecl(D, isFirst);
2846
343
    isFirst = false;
2847
343
  }
2848
328
  if (size == WL.size())
2849
0
    return;
2850
  // Now reverse the entries we just added.  This will match the DFS
2851
  // ordering performed by the worklist.
2852
328
  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
2853
328
  std::reverse(I, E);
2854
328
}
2855
14
void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) {
2856
14
  AddStmt(E->getInit());
2857
14
  for (const DesignatedInitExpr::Designator &D :
2858
18
       llvm::reverse(E->designators())) {
2859
18
    if (D.isFieldDesignator()) {
2860
16
      if (const FieldDecl *Field = D.getFieldDecl())
2861
16
        AddMemberRef(Field, D.getFieldLoc());
2862
16
      continue;
2863
16
    }
2864
2
    if (D.isArrayDesignator()) {
2865
2
      AddStmt(E->getArrayIndex(D));
2866
2
      continue;
2867
2
    }
2868
0
    assert(D.isArrayRangeDesignator() && "Unknown designator kind");
2869
0
    AddStmt(E->getArrayRangeEnd(D));
2870
0
    AddStmt(E->getArrayRangeStart(D));
2871
0
  }
2872
14
}
2873
75
void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) {
2874
75
  EnqueueChildren(E);
2875
75
  AddTypeLoc(E->getTypeInfoAsWritten());
2876
75
}
2877
2
void EnqueueVisitor::VisitForStmt(const ForStmt *FS) {
2878
2
  AddStmt(FS->getBody());
2879
2
  AddStmt(FS->getInc());
2880
2
  AddStmt(FS->getCond());
2881
2
  AddDecl(FS->getConditionVariable());
2882
2
  AddStmt(FS->getInit());
2883
2
}
2884
2
void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) {
2885
2
  WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
2886
2
}
2887
10
void EnqueueVisitor::VisitIfStmt(const IfStmt *If) {
2888
10
  AddStmt(If->getElse());
2889
10
  AddStmt(If->getThen());
2890
10
  AddStmt(If->getCond());
2891
10
  AddStmt(If->getInit());
2892
10
  AddDecl(If->getConditionVariable());
2893
10
}
2894
17
void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) {
2895
  // We care about the syntactic form of the initializer list, only.
2896
17
  if (InitListExpr *Syntactic = IE->getSyntacticForm())
2897
15
    IE = Syntactic;
2898
17
  EnqueueChildren(IE);
2899
17
}
2900
528
void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) {
2901
528
  WL.push_back(MemberExprParts(M, Parent));
2902
2903
  // If the base of the member access expression is an implicit 'this', don't
2904
  // visit it.
2905
  // FIXME: If we ever want to show these implicit accesses, this will be
2906
  // unfortunate. However, clang_getCursor() relies on this behavior.
2907
528
  if (M->isImplicitAccess())
2908
37
    return;
2909
2910
  // Ignore base anonymous struct/union fields, otherwise they will shadow the
2911
  // real field that we are interested in.
2912
491
  if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) {
2913
4
    if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) {
2914
4
      if (FD->isAnonymousStructOrUnion()) {
2915
4
        AddStmt(SubME->getBase());
2916
4
        return;
2917
4
      }
2918
4
    }
2919
4
  }
2920
2921
487
  AddStmt(M->getBase());
2922
487
}
2923
1
void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2924
1
  AddTypeLoc(E->getEncodedTypeSourceInfo());
2925
1
}
2926
155
void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) {
2927
155
  EnqueueChildren(M);
2928
155
  AddTypeLoc(M->getClassReceiverTypeInfo());
2929
155
}
2930
1
void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) {
2931
  // Visit the components of the offsetof expression.
2932
4
  for (unsigned N = E->getNumComponents(), I = N; I > 0; 
--I3
) {
2933
3
    const OffsetOfNode &Node = E->getComponent(I - 1);
2934
3
    switch (Node.getKind()) {
2935
1
    case OffsetOfNode::Array:
2936
1
      AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
2937
1
      break;
2938
2
    case OffsetOfNode::Field:
2939
2
      AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
2940
2
      break;
2941
0
    case OffsetOfNode::Identifier:
2942
0
    case OffsetOfNode::Base:
2943
0
      continue;
2944
3
    }
2945
3
  }
2946
  // Visit the type into which we're computing the offset.
2947
1
  AddTypeLoc(E->getTypeSourceInfo());
2948
1
}
2949
20
void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) {
2950
20
  if (E->hasExplicitTemplateArgs())
2951
4
    AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs());
2952
20
  WL.push_back(OverloadExprParts(E, Parent));
2953
20
}
2954
void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
2955
7
    const UnaryExprOrTypeTraitExpr *E) {
2956
7
  EnqueueChildren(E);
2957
7
  if (E->isArgumentType())
2958
7
    AddTypeLoc(E->getArgumentTypeInfo());
2959
7
}
2960
4.77k
void EnqueueVisitor::VisitStmt(const Stmt *S) { EnqueueChildren(S); }
2961
12
void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) {
2962
12
  AddStmt(S->getBody());
2963
12
  AddStmt(S->getCond());
2964
12
  AddDecl(S->getConditionVariable());
2965
12
}
2966
2967
2
void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) {
2968
2
  AddStmt(W->getBody());
2969
2
  AddStmt(W->getCond());
2970
2
  AddDecl(W->getConditionVariable());
2971
2
}
2972
2973
6
void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) {
2974
18
  for (unsigned I = E->getNumArgs(); I > 0; 
--I12
)
2975
12
    AddTypeLoc(E->getArg(I - 1));
2976
6
}
2977
2978
0
void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
2979
0
  AddTypeLoc(E->getQueriedTypeSourceInfo());
2980
0
}
2981
2982
0
void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
2983
0
  EnqueueChildren(E);
2984
0
}
2985
2986
5
void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) {
2987
5
  VisitOverloadExpr(U);
2988
5
  if (!U->isImplicitAccess())
2989
4
    AddStmt(U->getBase());
2990
5
}
2991
3
void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) {
2992
3
  AddStmt(E->getSubExpr());
2993
3
  AddTypeLoc(E->getWrittenTypeInfo());
2994
3
}
2995
2
void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
2996
2
  WL.push_back(SizeOfPackExprParts(E, Parent));
2997
2
}
2998
50
void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2999
  // If the opaque value has a source expression, just transparently
3000
  // visit that.  This is useful for (e.g.) pseudo-object expressions.
3001
50
  if (Expr *SourceExpr = E->getSourceExpr())
3002
48
    return Visit(SourceExpr);
3003
50
}
3004
4
void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) {
3005
4
  AddStmt(E->getBody());
3006
4
  WL.push_back(LambdaExprParts(E, Parent));
3007
4
}
3008
void EnqueueVisitor::VisitConceptSpecializationExpr(
3009
7
    const ConceptSpecializationExpr *E) {
3010
7
  WL.push_back(ConceptSpecializationExprVisit(E, Parent));
3011
7
}
3012
4
void EnqueueVisitor::VisitRequiresExpr(const RequiresExpr *E) {
3013
4
  WL.push_back(RequiresExprVisit(E, Parent));
3014
4
  for (ParmVarDecl *VD : E->getLocalParameters())
3015
2
    AddDecl(VD);
3016
4
}
3017
0
void EnqueueVisitor::VisitCXXParenListInitExpr(const CXXParenListInitExpr *E) {
3018
0
  EnqueueChildren(E);
3019
0
}
3020
17
void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
3021
  // Treat the expression like its syntactic form.
3022
17
  Visit(E->getSyntacticForm());
3023
17
}
3024
3025
void EnqueueVisitor::VisitOMPExecutableDirective(
3026
1
    const OMPExecutableDirective *D) {
3027
1
  EnqueueChildren(D);
3028
1
  for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(),
3029
1
                                       E = D->clauses().end();
3030
2
       I != E; 
++I1
)
3031
1
    EnqueueChildren(*I);
3032
1
}
3033
3034
void EnqueueVisitor::VisitOMPLoopBasedDirective(
3035
1
    const OMPLoopBasedDirective *D) {
3036
1
  VisitOMPExecutableDirective(D);
3037
1
}
3038
3039
0
void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) {
3040
0
  VisitOMPLoopBasedDirective(D);
3041
0
}
3042
3043
0
void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) {
3044
0
  VisitOMPExecutableDirective(D);
3045
0
}
3046
3047
0
void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) {
3048
0
  VisitOMPLoopDirective(D);
3049
0
}
3050
3051
void EnqueueVisitor::VisitOMPLoopTransformationDirective(
3052
1
    const OMPLoopTransformationDirective *D) {
3053
1
  VisitOMPLoopBasedDirective(D);
3054
1
}
3055
3056
1
void EnqueueVisitor::VisitOMPTileDirective(const OMPTileDirective *D) {
3057
1
  VisitOMPLoopTransformationDirective(D);
3058
1
}
3059
3060
0
void EnqueueVisitor::VisitOMPUnrollDirective(const OMPUnrollDirective *D) {
3061
0
  VisitOMPLoopTransformationDirective(D);
3062
0
}
3063
3064
0
void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) {
3065
0
  VisitOMPLoopDirective(D);
3066
0
}
3067
3068
0
void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) {
3069
0
  VisitOMPLoopDirective(D);
3070
0
}
3071
3072
0
void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
3073
0
  VisitOMPExecutableDirective(D);
3074
0
}
3075
3076
0
void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
3077
0
  VisitOMPExecutableDirective(D);
3078
0
}
3079
3080
0
void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) {
3081
0
  VisitOMPExecutableDirective(D);
3082
0
}
3083
3084
0
void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) {
3085
0
  VisitOMPExecutableDirective(D);
3086
0
}
3087
3088
0
void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) {
3089
0
  VisitOMPExecutableDirective(D);
3090
0
  AddDeclarationNameInfo(D);
3091
0
}
3092
3093
void EnqueueVisitor::VisitOMPParallelForDirective(
3094
0
    const OMPParallelForDirective *D) {
3095
0
  VisitOMPLoopDirective(D);
3096
0
}
3097
3098
void EnqueueVisitor::VisitOMPParallelForSimdDirective(
3099
0
    const OMPParallelForSimdDirective *D) {
3100
0
  VisitOMPLoopDirective(D);
3101
0
}
3102
3103
void EnqueueVisitor::VisitOMPParallelMasterDirective(
3104
0
    const OMPParallelMasterDirective *D) {
3105
0
  VisitOMPExecutableDirective(D);
3106
0
}
3107
3108
void EnqueueVisitor::VisitOMPParallelMaskedDirective(
3109
0
    const OMPParallelMaskedDirective *D) {
3110
0
  VisitOMPExecutableDirective(D);
3111
0
}
3112
3113
void EnqueueVisitor::VisitOMPParallelSectionsDirective(
3114
0
    const OMPParallelSectionsDirective *D) {
3115
0
  VisitOMPExecutableDirective(D);
3116
0
}
3117
3118
0
void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) {
3119
0
  VisitOMPExecutableDirective(D);
3120
0
}
3121
3122
void EnqueueVisitor::VisitOMPTaskyieldDirective(
3123
0
    const OMPTaskyieldDirective *D) {
3124
0
  VisitOMPExecutableDirective(D);
3125
0
}
3126
3127
0
void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) {
3128
0
  VisitOMPExecutableDirective(D);
3129
0
}
3130
3131
0
void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) {
3132
0
  VisitOMPExecutableDirective(D);
3133
0
}
3134
3135
0
void EnqueueVisitor::VisitOMPErrorDirective(const OMPErrorDirective *D) {
3136
0
  VisitOMPExecutableDirective(D);
3137
0
}
3138
3139
void EnqueueVisitor::VisitOMPTaskgroupDirective(
3140
0
    const OMPTaskgroupDirective *D) {
3141
0
  VisitOMPExecutableDirective(D);
3142
0
  if (const Expr *E = D->getReductionRef())
3143
0
    VisitStmt(E);
3144
0
}
3145
3146
0
void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) {
3147
0
  VisitOMPExecutableDirective(D);
3148
0
}
3149
3150
0
void EnqueueVisitor::VisitOMPDepobjDirective(const OMPDepobjDirective *D) {
3151
0
  VisitOMPExecutableDirective(D);
3152
0
}
3153
3154
0
void EnqueueVisitor::VisitOMPScanDirective(const OMPScanDirective *D) {
3155
0
  VisitOMPExecutableDirective(D);
3156
0
}
3157
3158
0
void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) {
3159
0
  VisitOMPExecutableDirective(D);
3160
0
}
3161
3162
0
void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) {
3163
0
  VisitOMPExecutableDirective(D);
3164
0
}
3165
3166
0
void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) {
3167
0
  VisitOMPExecutableDirective(D);
3168
0
}
3169
3170
void EnqueueVisitor::VisitOMPTargetDataDirective(
3171
0
    const OMPTargetDataDirective *D) {
3172
0
  VisitOMPExecutableDirective(D);
3173
0
}
3174
3175
void EnqueueVisitor::VisitOMPTargetEnterDataDirective(
3176
0
    const OMPTargetEnterDataDirective *D) {
3177
0
  VisitOMPExecutableDirective(D);
3178
0
}
3179
3180
void EnqueueVisitor::VisitOMPTargetExitDataDirective(
3181
0
    const OMPTargetExitDataDirective *D) {
3182
0
  VisitOMPExecutableDirective(D);
3183
0
}
3184
3185
void EnqueueVisitor::VisitOMPTargetParallelDirective(
3186
0
    const OMPTargetParallelDirective *D) {
3187
0
  VisitOMPExecutableDirective(D);
3188
0
}
3189
3190
void EnqueueVisitor::VisitOMPTargetParallelForDirective(
3191
0
    const OMPTargetParallelForDirective *D) {
3192
0
  VisitOMPLoopDirective(D);
3193
0
}
3194
3195
0
void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) {
3196
0
  VisitOMPExecutableDirective(D);
3197
0
}
3198
3199
void EnqueueVisitor::VisitOMPCancellationPointDirective(
3200
0
    const OMPCancellationPointDirective *D) {
3201
0
  VisitOMPExecutableDirective(D);
3202
0
}
3203
3204
0
void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) {
3205
0
  VisitOMPExecutableDirective(D);
3206
0
}
3207
3208
0
void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) {
3209
0
  VisitOMPLoopDirective(D);
3210
0
}
3211
3212
void EnqueueVisitor::VisitOMPTaskLoopSimdDirective(
3213
0
    const OMPTaskLoopSimdDirective *D) {
3214
0
  VisitOMPLoopDirective(D);
3215
0
}
3216
3217
void EnqueueVisitor::VisitOMPMasterTaskLoopDirective(
3218
0
    const OMPMasterTaskLoopDirective *D) {
3219
0
  VisitOMPLoopDirective(D);
3220
0
}
3221
3222
void EnqueueVisitor::VisitOMPMaskedTaskLoopDirective(
3223
0
    const OMPMaskedTaskLoopDirective *D) {
3224
0
  VisitOMPLoopDirective(D);
3225
0
}
3226
3227
void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective(
3228
0
    const OMPMasterTaskLoopSimdDirective *D) {
3229
0
  VisitOMPLoopDirective(D);
3230
0
}
3231
3232
void EnqueueVisitor::VisitOMPMaskedTaskLoopSimdDirective(
3233
0
    const OMPMaskedTaskLoopSimdDirective *D) {
3234
0
  VisitOMPLoopDirective(D);
3235
0
}
3236
3237
void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective(
3238
0
    const OMPParallelMasterTaskLoopDirective *D) {
3239
0
  VisitOMPLoopDirective(D);
3240
0
}
3241
3242
void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopDirective(
3243
0
    const OMPParallelMaskedTaskLoopDirective *D) {
3244
0
  VisitOMPLoopDirective(D);
3245
0
}
3246
3247
void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective(
3248
0
    const OMPParallelMasterTaskLoopSimdDirective *D) {
3249
0
  VisitOMPLoopDirective(D);
3250
0
}
3251
3252
void EnqueueVisitor::VisitOMPParallelMaskedTaskLoopSimdDirective(
3253
0
    const OMPParallelMaskedTaskLoopSimdDirective *D) {
3254
0
  VisitOMPLoopDirective(D);
3255
0
}
3256
3257
void EnqueueVisitor::VisitOMPDistributeDirective(
3258
0
    const OMPDistributeDirective *D) {
3259
0
  VisitOMPLoopDirective(D);
3260
0
}
3261
3262
void EnqueueVisitor::VisitOMPDistributeParallelForDirective(
3263
0
    const OMPDistributeParallelForDirective *D) {
3264
0
  VisitOMPLoopDirective(D);
3265
0
}
3266
3267
void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective(
3268
0
    const OMPDistributeParallelForSimdDirective *D) {
3269
0
  VisitOMPLoopDirective(D);
3270
0
}
3271
3272
void EnqueueVisitor::VisitOMPDistributeSimdDirective(
3273
0
    const OMPDistributeSimdDirective *D) {
3274
0
  VisitOMPLoopDirective(D);
3275
0
}
3276
3277
void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective(
3278
0
    const OMPTargetParallelForSimdDirective *D) {
3279
0
  VisitOMPLoopDirective(D);
3280
0
}
3281
3282
void EnqueueVisitor::VisitOMPTargetSimdDirective(
3283
0
    const OMPTargetSimdDirective *D) {
3284
0
  VisitOMPLoopDirective(D);
3285
0
}
3286
3287
void EnqueueVisitor::VisitOMPTeamsDistributeDirective(
3288
0
    const OMPTeamsDistributeDirective *D) {
3289
0
  VisitOMPLoopDirective(D);
3290
0
}
3291
3292
void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective(
3293
0
    const OMPTeamsDistributeSimdDirective *D) {
3294
0
  VisitOMPLoopDirective(D);
3295
0
}
3296
3297
void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective(
3298
0
    const OMPTeamsDistributeParallelForSimdDirective *D) {
3299
0
  VisitOMPLoopDirective(D);
3300
0
}
3301
3302
void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective(
3303
0
    const OMPTeamsDistributeParallelForDirective *D) {
3304
0
  VisitOMPLoopDirective(D);
3305
0
}
3306
3307
void EnqueueVisitor::VisitOMPTargetTeamsDirective(
3308
0
    const OMPTargetTeamsDirective *D) {
3309
0
  VisitOMPExecutableDirective(D);
3310
0
}
3311
3312
void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective(
3313
0
    const OMPTargetTeamsDistributeDirective *D) {
3314
0
  VisitOMPLoopDirective(D);
3315
0
}
3316
3317
void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective(
3318
0
    const OMPTargetTeamsDistributeParallelForDirective *D) {
3319
0
  VisitOMPLoopDirective(D);
3320
0
}
3321
3322
void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
3323
0
    const OMPTargetTeamsDistributeParallelForSimdDirective *D) {
3324
0
  VisitOMPLoopDirective(D);
3325
0
}
3326
3327
void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective(
3328
0
    const OMPTargetTeamsDistributeSimdDirective *D) {
3329
0
  VisitOMPLoopDirective(D);
3330
0
}
3331
3332
12.9k
void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
3333
12.9k
  EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU, RegionOfInterest))
3334
12.9k
      .Visit(S);
3335
12.9k
}
3336
3337
18.4k
bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
3338
18.4k
  if (RegionOfInterest.isValid()) {
3339
13.9k
    SourceRange Range = getRawCursorExtent(C);
3340
13.9k
    if (Range.isInvalid() || 
CompareRegionOfInterest(Range)13.9k
)
3341
1.75k
      return false;
3342
13.9k
  }
3343
16.6k
  return true;
3344
18.4k
}
3345
3346
1.18k
bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
3347
28.3k
  while (!WL.empty()) {
3348
    // Dequeue the worklist item.
3349
27.2k
    VisitorJob LI = WL.pop_back_val();
3350
3351
    // Set the Parent field, then back to its old value once we're done.
3352
27.2k
    SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
3353
3354
27.2k
    switch (LI.getKind()) {
3355
365
    case VisitorJob::DeclVisitKind: {
3356
365
      const Decl *D = cast<DeclVisit>(&LI)->get();
3357
365
      if (!D)
3358
0
        continue;
3359
3360
      // For now, perform default visitation for Decls.
3361
365
      if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
3362
365
                             cast<DeclVisit>(&LI)->isFirst())))
3363
1
        return true;
3364
3365
364
      continue;
3366
365
    }
3367
364
    case VisitorJob::ExplicitTemplateArgsVisitKind: {
3368
12
      for (const TemplateArgumentLoc &Arg :
3369
16
           *cast<ExplicitTemplateArgsVisit>(&LI)) {
3370
16
        if (VisitTemplateArgumentLoc(Arg))
3371
0
          return true;
3372
16
      }
3373
12
      continue;
3374
12
    }
3375
190
    case VisitorJob::TypeLocVisitKind: {
3376
      // Perform default visitation for TypeLocs.
3377
190
      if (Visit(cast<TypeLocVisit>(&LI)->get()))
3378
0
        return true;
3379
190
      continue;
3380
190
    }
3381
190
    case VisitorJob::LabelRefVisitKind: {
3382
3
      const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
3383
3
      if (LabelStmt *stmt = LS->getStmt()) {
3384
2
        if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
3385
2
                                     TU))) {
3386
0
          return true;
3387
0
        }
3388
2
      }
3389
3
      continue;
3390
3
    }
3391
3392
9
    case VisitorJob::NestedNameSpecifierLocVisitKind: {
3393
9
      NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
3394
9
      if (VisitNestedNameSpecifierLoc(V->get()))
3395
0
        return true;
3396
9
      continue;
3397
9
    }
3398
3399
9
    case VisitorJob::DeclarationNameInfoVisitKind: {
3400
9
      if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)->get()))
3401
0
        return true;
3402
9
      continue;
3403
9
    }
3404
18
    case VisitorJob::MemberRefVisitKind: {
3405
18
      MemberRefVisit *V = cast<MemberRefVisit>(&LI);
3406
18
      if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
3407
0
        return true;
3408
18
      continue;
3409
18
    }
3410
18.4k
    case VisitorJob::StmtVisitKind: {
3411
18.4k
      const Stmt *S = cast<StmtVisit>(&LI)->get();
3412
18.4k
      if (!S)
3413
0
        continue;
3414
3415
      // Update the current cursor.
3416
18.4k
      CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
3417
18.4k
      if (!IsInRegionOfInterest(Cursor))
3418
1.75k
        continue;
3419
16.6k
      switch (Visitor(Cursor, Parent, ClientData)) {
3420
1
      case CXChildVisit_Break:
3421
1
        return true;
3422
4.87k
      case CXChildVisit_Continue:
3423
4.87k
        break;
3424
11.8k
      case CXChildVisit_Recurse:
3425
11.8k
        if (PostChildrenVisitor)
3426
6.38k
          WL.push_back(PostChildrenVisit(nullptr, Cursor));
3427
11.8k
        EnqueueWorkList(WL, S);
3428
11.8k
        break;
3429
16.6k
      }
3430
16.6k
      continue;
3431
16.6k
    }
3432
16.6k
    case VisitorJob::MemberExprPartsKind: {
3433
      // Handle the other pieces in the MemberExpr besides the base.
3434
528
      const MemberExpr *M = cast<MemberExprParts>(&LI)->get();
3435
3436
      // Visit the nested-name-specifier
3437
528
      if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
3438
5
        if (VisitNestedNameSpecifierLoc(QualifierLoc))
3439
0
          return true;
3440
3441
      // Visit the declaration name.
3442
528
      if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
3443
0
        return true;
3444
3445
      // Visit the explicitly-specified template arguments, if any.
3446
528
      if (M->hasExplicitTemplateArgs()) {
3447
2
        for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
3448
2
                                       *ArgEnd = Arg + M->getNumTemplateArgs();
3449
4
             Arg != ArgEnd; 
++Arg2
) {
3450
2
          if (VisitTemplateArgumentLoc(*Arg))
3451
0
            return true;
3452
2
        }
3453
2
      }
3454
528
      continue;
3455
528
    }
3456
1.21k
    case VisitorJob::DeclRefExprPartsKind: {
3457
1.21k
      const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
3458
      // Visit nested-name-specifier, if present.
3459
1.21k
      if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
3460
2
        if (VisitNestedNameSpecifierLoc(QualifierLoc))
3461
0
          return true;
3462
      // Visit declaration name.
3463
1.21k
      if (VisitDeclarationNameInfo(DR->getNameInfo()))
3464
0
        return true;
3465
1.21k
      continue;
3466
1.21k
    }
3467
1.21k
    case VisitorJob::OverloadExprPartsKind: {
3468
20
      const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
3469
      // Visit the nested-name-specifier.
3470
20
      if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
3471
5
        if (VisitNestedNameSpecifierLoc(QualifierLoc))
3472
0
          return true;
3473
      // Visit the declaration name.
3474
20
      if (VisitDeclarationNameInfo(O->getNameInfo()))
3475
0
        return true;
3476
      // Visit the overloaded declaration reference.
3477
20
      if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
3478
0
        return true;
3479
20
      continue;
3480
20
    }
3481
20
    case VisitorJob::SizeOfPackExprPartsKind: {
3482
2
      const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
3483
2
      NamedDecl *Pack = E->getPack();
3484
2
      if (isa<TemplateTypeParmDecl>(Pack)) {
3485
1
        if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
3486
1
                                    E->getPackLoc(), TU)))
3487
0
          return true;
3488
3489
1
        continue;
3490
1
      }
3491
3492
1
      if (isa<TemplateTemplateParmDecl>(Pack)) {
3493
0
        if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
3494
0
                                        E->getPackLoc(), TU)))
3495
0
          return true;
3496
3497
0
        continue;
3498
0
      }
3499
3500
      // Non-type template parameter packs and function parameter packs are
3501
      // treated like DeclRefExpr cursors.
3502
1
      continue;
3503
1
    }
3504
3505
4
    case VisitorJob::LambdaExprPartsKind: {
3506
      // Visit non-init captures.
3507
4
      const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
3508
4
      for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
3509
4
                                        CEnd = E->explicit_capture_end();
3510
10
           C != CEnd; 
++C6
) {
3511
6
        if (!C->capturesVariable())
3512
1
          continue;
3513
        // TODO: handle structured bindings here ?
3514
5
        if (!isa<VarDecl>(C->getCapturedVar()))
3515
0
          continue;
3516
5
        if (Visit(MakeCursorVariableRef(cast<VarDecl>(C->getCapturedVar()),
3517
5
                                        C->getLocation(), TU)))
3518
0
          return true;
3519
5
      }
3520
      // Visit init captures
3521
6
      
for (auto InitExpr : E->capture_inits())4
{
3522
6
        if (InitExpr && 
Visit(InitExpr)5
)
3523
0
          return true;
3524
6
      }
3525
3526
4
      TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
3527
      // Visit parameters and return type, if present.
3528
4
      if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) {
3529
4
        if (E->hasExplicitParameters()) {
3530
          // Visit parameters.
3531
6
          for (unsigned I = 0, N = Proto.getNumParams(); I != N; 
++I3
)
3532
3
            if (Visit(MakeCXCursor(Proto.getParam(I), TU)))
3533
0
              return true;
3534
3
        }
3535
4
        if (E->hasExplicitResultType()) {
3536
          // Visit result type.
3537
2
          if (Visit(Proto.getReturnLoc()))
3538
0
            return true;
3539
2
        }
3540
4
      }
3541
4
      break;
3542
4
    }
3543
3544
7
    case VisitorJob::ConceptSpecializationExprVisitKind: {
3545
7
      const ConceptSpecializationExpr *E =
3546
7
          cast<ConceptSpecializationExprVisit>(&LI)->get();
3547
7
      if (NestedNameSpecifierLoc QualifierLoc =
3548
7
              E->getNestedNameSpecifierLoc()) {
3549
3
        if (VisitNestedNameSpecifierLoc(QualifierLoc))
3550
0
          return true;
3551
3
      }
3552
3553
7
      if (E->getNamedConcept() &&
3554
7
          Visit(MakeCursorTemplateRef(E->getNamedConcept(),
3555
7
                                      E->getConceptNameLoc(), TU)))
3556
0
        return true;
3557
3558
7
      if (auto Args = E->getTemplateArgsAsWritten()) {
3559
9
        for (const auto &Arg : Args->arguments()) {
3560
9
          if (VisitTemplateArgumentLoc(Arg))
3561
0
            return true;
3562
9
        }
3563
7
      }
3564
7
      break;
3565
7
    }
3566
3567
7
    case VisitorJob::RequiresExprVisitKind: {
3568
4
      const RequiresExpr *E = cast<RequiresExprVisit>(&LI)->get();
3569
4
      for (const concepts::Requirement *R : E->getRequirements())
3570
6
        VisitConceptRequirement(*R);
3571
4
      break;
3572
7
    }
3573
3574
6.38k
    case VisitorJob::PostChildrenVisitKind:
3575
6.38k
      if (PostChildrenVisitor(Parent, ClientData))
3576
0
        return true;
3577
6.38k
      break;
3578
27.2k
    }
3579
27.2k
  }
3580
1.18k
  return false;
3581
1.18k
}
3582
3583
1.18k
bool CursorVisitor::Visit(const Stmt *S) {
3584
1.18k
  VisitorWorkList *WL = nullptr;
3585
1.18k
  if (!WorkListFreeList.empty()) {
3586
554
    WL = WorkListFreeList.back();
3587
554
    WL->clear();
3588
554
    WorkListFreeList.pop_back();
3589
632
  } else {
3590
632
    WL = new VisitorWorkList();
3591
632
    WorkListCache.push_back(WL);
3592
632
  }
3593
1.18k
  EnqueueWorkList(*WL, S);
3594
1.18k
  bool result = RunVisitorWorkList(*WL);
3595
1.18k
  WorkListFreeList.push_back(WL);
3596
1.18k
  return result;
3597
1.18k
}
3598
3599
namespace {
3600
typedef SmallVector<SourceRange, 4> RefNamePieces;
3601
RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
3602
                          const DeclarationNameInfo &NI, SourceRange QLoc,
3603
65.9k
                          const SourceRange *TemplateArgsLoc = nullptr) {
3604
65.9k
  const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
3605
65.9k
  const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
3606
65.9k
  const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
3607
3608
65.9k
  const DeclarationName::NameKind Kind = NI.getName().getNameKind();
3609
3610
65.9k
  RefNamePieces Pieces;
3611
3612
65.9k
  if (WantQualifier && QLoc.isValid())
3613
86
    Pieces.push_back(QLoc);
3614
3615
65.9k
  if (Kind != DeclarationName::CXXOperatorName || 
IsMemberRefExpr60.6k
)
3616
5.38k
    Pieces.push_back(NI.getLoc());
3617
3618
65.9k
  if (WantTemplateArgs && 
TemplateArgsLoc51.3k
&&
TemplateArgsLoc->isValid()49.1k
)
3619
20
    Pieces.push_back(*TemplateArgsLoc);
3620
3621
65.9k
  if (Kind == DeclarationName::CXXOperatorName) {
3622
60.6k
    Pieces.push_back(NI.getInfo().getCXXOperatorNameBeginLoc());
3623
60.6k
    Pieces.push_back(NI.getInfo().getCXXOperatorNameEndLoc());
3624
60.6k
  }
3625
3626
65.9k
  if (WantSinglePiece) {
3627
13.2k
    SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
3628
13.2k
    Pieces.clear();
3629
13.2k
    Pieces.push_back(R);
3630
13.2k
  }
3631
3632
65.9k
  return Pieces;
3633
65.9k
}
3634
} // namespace
3635
3636
//===----------------------------------------------------------------------===//
3637
// Misc. API hooks.
3638
//===----------------------------------------------------------------------===//
3639
3640
namespace {
3641
struct RegisterFatalErrorHandler {
3642
1.09k
  RegisterFatalErrorHandler() {
3643
1.09k
    clang_install_aborting_llvm_fatal_error_handler();
3644
1.09k
  }
3645
};
3646
} // namespace
3647
3648
static llvm::ManagedStatic<RegisterFatalErrorHandler>
3649
    RegisterFatalErrorHandlerOnce;
3650
3651
static CIndexer *clang_createIndex_Impl(
3652
    int excludeDeclarationsFromPCH, int displayDiagnostics,
3653
    unsigned char threadBackgroundPriorityForIndexing = CXChoice_Default,
3654
1.10k
    unsigned char threadBackgroundPriorityForEditing = CXChoice_Default) {
3655
  // We use crash recovery to make some of our APIs more reliable, implicitly
3656
  // enable it.
3657
1.10k
  if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
3658
1.10k
    llvm::CrashRecoveryContext::Enable();
3659
3660
  // Look through the managed static to trigger construction of the managed
3661
  // static which registers our fatal error handler. This ensures it is only
3662
  // registered once.
3663
1.10k
  (void)*RegisterFatalErrorHandlerOnce;
3664
3665
  // Initialize targets for clang module support.
3666
1.10k
  llvm::InitializeAllTargets();
3667
1.10k
  llvm::InitializeAllTargetMCs();
3668
1.10k
  llvm::InitializeAllAsmPrinters();
3669
1.10k
  llvm::InitializeAllAsmParsers();
3670
3671
1.10k
  CIndexer *CIdxr = new CIndexer();
3672
3673
1.10k
  if (excludeDeclarationsFromPCH)
3674
307
    CIdxr->setOnlyLocalDecls();
3675
1.10k
  if (displayDiagnostics)
3676
526
    CIdxr->setDisplayDiagnostics();
3677
3678
1.10k
  unsigned GlobalOptions = CIdxr->getCXGlobalOptFlags();
3679
1.10k
  const auto updateGlobalOption =
3680
1.10k
      [&GlobalOptions](unsigned char Policy, CXGlobalOptFlags Flag,
3681
2.21k
                       const char *EnvironmentVariableName) {
3682
2.21k
        switch (Policy) {
3683
4
        case CXChoice_Enabled:
3684
4
          GlobalOptions |= Flag;
3685
4
          break;
3686
1
        case CXChoice_Disabled:
3687
1
          GlobalOptions &= ~Flag;
3688
1
          break;
3689
2.21k
        case CXChoice_Default:
3690
2.21k
        default: // Fall back to default behavior if Policy is unsupported.
3691
2.21k
          if (getenv(EnvironmentVariableName))
3692
0
            GlobalOptions |= Flag;
3693
2.21k
        }
3694
2.21k
      };
3695
1.10k
  updateGlobalOption(threadBackgroundPriorityForIndexing,
3696
1.10k
                     CXGlobalOpt_ThreadBackgroundPriorityForIndexing,
3697
1.10k
                     "LIBCLANG_BGPRIO_INDEX");
3698
1.10k
  updateGlobalOption(threadBackgroundPriorityForEditing,
3699
1.10k
                     CXGlobalOpt_ThreadBackgroundPriorityForEditing,
3700
1.10k
                     "LIBCLANG_BGPRIO_EDIT");
3701
1.10k
  CIdxr->setCXGlobalOptFlags(GlobalOptions);
3702
3703
1.10k
  return CIdxr;
3704
1.10k
}
3705
3706
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
3707
398
                          int displayDiagnostics) {
3708
398
  return clang_createIndex_Impl(excludeDeclarationsFromPCH, displayDiagnostics);
3709
398
}
3710
3711
1.10k
void clang_disposeIndex(CXIndex CIdx) {
3712
1.10k
  if (CIdx)
3713
1.10k
    delete static_cast<CIndexer *>(CIdx);
3714
1.10k
}
3715
3716
710
CXIndex clang_createIndexWithOptions(const CXIndexOptions *options) {
3717
  // Adding new options to struct CXIndexOptions:
3718
  // 1. If no other new option has been added in the same libclang version,
3719
  // sizeof(CXIndexOptions) must increase for versioning purposes.
3720
  // 2. Options should be added at the end of the struct in order to seamlessly
3721
  // support older struct versions. If options->Size < sizeof(CXIndexOptions),
3722
  // don't attempt to read the missing options and rely on the default values of
3723
  // recently added options being reasonable. For example:
3724
  // if (options->Size >= offsetof(CXIndexOptions, RecentlyAddedMember))
3725
  //   do_something(options->RecentlyAddedMember);
3726
3727
  // An exception: if a new option is small enough, it can be squeezed into the
3728
  // /*Reserved*/ bits in CXIndexOptions. Since the default value of each option
3729
  // is guaranteed to be 0 and the callers are advised to zero out the struct,
3730
  // programs built against older libclang versions would implicitly set the new
3731
  // options to default values, which should keep the behavior of previous
3732
  // libclang versions and thus be backward-compatible.
3733
3734
  // If options->Size > sizeof(CXIndexOptions), the user may have set an option
3735
  // we can't handle, in which case we return nullptr to report failure.
3736
  // Replace `!=` with `>` here to support older struct versions. `!=` has the
3737
  // advantage of catching more usage bugs and no disadvantages while there is a
3738
  // single supported struct version (the initial version).
3739
710
  if (options->Size != sizeof(CXIndexOptions))
3740
0
    return nullptr;
3741
710
  CIndexer *const CIdxr = clang_createIndex_Impl(
3742
710
      options->ExcludeDeclarationsFromPCH, options->DisplayDiagnostics,
3743
710
      options->ThreadBackgroundPriorityForIndexing,
3744
710
      options->ThreadBackgroundPriorityForEditing);
3745
710
  CIdxr->setStorePreamblesInMemory(options->StorePreamblesInMemory);
3746
710
  CIdxr->setPreambleStoragePath(options->PreambleStoragePath);
3747
710
  CIdxr->setInvocationEmissionPath(options->InvocationEmissionPath);
3748
710
  return CIdxr;
3749
710
}
3750
3751
0
void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
3752
0
  if (CIdx)
3753
0
    static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
3754
0
}
3755
3756
5
unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
3757
5
  if (CIdx)
3758
5
    return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
3759
0
  return 0;
3760
5
}
3761
3762
void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx,
3763
0
                                                   const char *Path) {
3764
0
  if (CIdx)
3765
0
    static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : "");
3766
0
}
3767
3768
2
void clang_toggleCrashRecovery(unsigned isEnabled) {
3769
2
  if (isEnabled)
3770
0
    llvm::CrashRecoveryContext::Enable();
3771
2
  else
3772
2
    llvm::CrashRecoveryContext::Disable();
3773
2
}
3774
3775
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
3776
2
                                              const char *ast_filename) {
3777
2
  CXTranslationUnit TU;
3778
2
  enum CXErrorCode Result =
3779
2
      clang_createTranslationUnit2(CIdx, ast_filename, &TU);
3780
2
  (void)Result;
3781
2
  assert((TU && Result == CXError_Success) ||
3782
2
         (!TU && Result != CXError_Success));
3783
2
  return TU;
3784
2
}
3785
3786
enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
3787
                                              const char *ast_filename,
3788
36
                                              CXTranslationUnit *out_TU) {
3789
36
  if (out_TU)
3790
35
    *out_TU = nullptr;
3791
3792
36
  if (!CIdx || 
!ast_filename33
||
!out_TU33
)
3793
3
    return CXError_InvalidArguments;
3794
3795
33
  LOG_FUNC_SECTION 
{ *Log << ast_filename; }0
3796
3797
33
  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
3798
33
  FileSystemOptions FileSystemOpts;
3799
33
  auto HSOpts = std::make_shared<HeaderSearchOptions>();
3800
3801
33
  IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
3802
33
      CompilerInstance::createDiagnostics(new DiagnosticOptions());
3803
33
  std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
3804
33
      ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
3805
33
      ASTUnit::LoadEverything, Diags, FileSystemOpts, HSOpts,
3806
33
      /*UseDebugInfo=*/false, CXXIdx->getOnlyLocalDecls(),
3807
33
      CaptureDiagsKind::All, /*AllowASTWithCompilerErrors=*/true,
3808
33
      /*UserFilesAreVolatile=*/true);
3809
33
  *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));
3810
33
  return *out_TU ? 
CXError_Success32
:
CXError_Failure1
;
3811
36
}
3812
3813
142
unsigned clang_defaultEditingTranslationUnitOptions() {
3814
142
  return CXTranslationUnit_PrecompiledPreamble |
3815
142
         CXTranslationUnit_CacheCompletionResults;
3816
142
}
3817
3818
CXTranslationUnit clang_createTranslationUnitFromSourceFile(
3819
    CXIndex CIdx, const char *source_filename, int num_command_line_args,
3820
    const char *const *command_line_args, unsigned num_unsaved_files,
3821
0
    struct CXUnsavedFile *unsaved_files) {
3822
0
  unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord;
3823
0
  return clang_parseTranslationUnit(CIdx, source_filename, command_line_args,
3824
0
                                    num_command_line_args, unsaved_files,
3825
0
                                    num_unsaved_files, Options);
3826
0
}
3827
3828
static CXErrorCode
3829
clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename,
3830
                                const char *const *command_line_args,
3831
                                int num_command_line_args,
3832
                                ArrayRef<CXUnsavedFile> unsaved_files,
3833
1.03k
                                unsigned options, CXTranslationUnit *out_TU) {
3834
  // Set up the initial return values.
3835
1.03k
  if (out_TU)
3836
1.02k
    *out_TU = nullptr;
3837
3838
  // Check arguments.
3839
1.03k
  if (!CIdx || 
!out_TU1.02k
)
3840
1
    return CXError_InvalidArguments;
3841
3842
1.02k
  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
3843
3844
1.02k
  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
3845
0
    setThreadBackgroundPriority();
3846
3847
1.02k
  bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
3848
1.02k
  bool CreatePreambleOnFirstParse =
3849
1.02k
      options & CXTranslationUnit_CreatePreambleOnFirstParse;
3850
  // FIXME: Add a flag for modules.
3851
1.02k
  TranslationUnitKind TUKind = (options & (CXTranslationUnit_Incomplete |
3852
1.02k
                                           CXTranslationUnit_SingleFileParse))
3853
1.02k
                                   ? 
TU_Prefix61
3854
1.02k
                                   : 
TU_Complete968
;
3855
1.02k
  bool CacheCodeCompletionResults =
3856
1.02k
      options & CXTranslationUnit_CacheCompletionResults;
3857
1.02k
  bool IncludeBriefCommentsInCodeCompletion =
3858
1.02k
      options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
3859
1.02k
  bool SingleFileParse = options & CXTranslationUnit_SingleFileParse;
3860
1.02k
  bool ForSerialization = options & CXTranslationUnit_ForSerialization;
3861
1.02k
  bool RetainExcludedCB =
3862
1.02k
      options & CXTranslationUnit_RetainExcludedConditionalBlocks;
3863
1.02k
  SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None;
3864
1.02k
  if (options & CXTranslationUnit_SkipFunctionBodies) {
3865
6
    SkipFunctionBodies =
3866
6
        (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble)
3867
6
            ? 
SkipFunctionBodiesScope::Preamble1
3868
6
            : 
SkipFunctionBodiesScope::PreambleAndMainFile5
;
3869
6
  }
3870
3871
  // Configure the diagnostics.
3872
1.02k
  std::unique_ptr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(
3873
1.02k
      llvm::ArrayRef(command_line_args, num_command_line_args));
3874
1.02k
  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
3875
1.02k
      CompilerInstance::createDiagnostics(DiagOpts.release()));
3876
3877
1.02k
  if (options & CXTranslationUnit_KeepGoing)
3878
5
    Diags->setFatalsAsError(true);
3879
3880
1.02k
  CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
3881
1.02k
  if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
3882
1
    CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
3883
3884
  // Recover resources if we crash before exiting this function.
3885
1.02k
  llvm::CrashRecoveryContextCleanupRegistrar<
3886
1.02k
      DiagnosticsEngine,
3887
1.02k
      llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
3888
1.02k
      DiagCleanup(Diags.get());
3889
3890
1.02k
  std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
3891
1.02k
      new std::vector<ASTUnit::RemappedFile>());
3892
3893
  // Recover resources if we crash before exiting this function.
3894
1.02k
  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
3895
1.02k
      RemappedCleanup(RemappedFiles.get());
3896
3897
1.02k
  for (auto &UF : unsaved_files) {
3898
4
    std::unique_ptr<llvm::MemoryBuffer> MB =
3899
4
        llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
3900
4
    RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
3901
4
  }
3902
3903
1.02k
  std::unique_ptr<std::vector<const char *>> Args(
3904
1.02k
      new std::vector<const char *>());
3905
3906
  // Recover resources if we crash before exiting this method.
3907
1.02k
  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char *>>
3908
1.02k
      ArgsCleanup(Args.get());
3909
3910
  // Since the Clang C library is primarily used by batch tools dealing with
3911
  // (often very broken) source code, where spell-checking can have a
3912
  // significant negative impact on performance (particularly when
3913
  // precompiled headers are involved), we disable it by default.
3914
  // Only do this if we haven't found a spell-checking-related argument.
3915
1.02k
  bool FoundSpellCheckingArgument = false;
3916
3.89k
  for (int I = 0; I != num_command_line_args; 
++I2.86k
) {
3917
2.86k
    if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
3918
2.86k
        strcmp(command_line_args[I], "-fspell-checking") == 0) {
3919
2
      FoundSpellCheckingArgument = true;
3920
2
      break;
3921
2
    }
3922
2.86k
  }
3923
1.02k
  Args->insert(Args->end(), command_line_args,
3924
1.02k
               command_line_args + num_command_line_args);
3925
3926
1.02k
  if (!FoundSpellCheckingArgument)
3927
1.02k
    Args->insert(Args->begin() + 1, "-fno-spell-checking");
3928
3929
  // The 'source_filename' argument is optional.  If the caller does not
3930
  // specify it then it is assumed that the source file is specified
3931
  // in the actual argument list.
3932
  // Put the source file after command_line_args otherwise if '-x' flag is
3933
  // present it will be unused.
3934
1.02k
  if (source_filename)
3935
233
    Args->push_back(source_filename);
3936
3937
  // Do we need the detailed preprocessing record?
3938
1.02k
  if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
3939
1.02k
    Args->push_back("-Xclang");
3940
1.02k
    Args->push_back("-detailed-preprocessing-record");
3941
1.02k
  }
3942
3943
  // Suppress any editor placeholder diagnostics.
3944
1.02k
  Args->push_back("-fallow-editor-placeholders");
3945
3946
1.02k
  unsigned NumErrors = Diags->getClient()->getNumErrors();
3947
1.02k
  std::unique_ptr<ASTUnit> ErrUnit;
3948
  // Unless the user specified that they want the preamble on the first parse
3949
  // set it up to be created on the first reparse. This makes the first parse
3950
  // faster, trading for a slower (first) reparse.
3951
1.02k
  unsigned PrecompilePreambleAfterNParses =
3952
1.02k
      !PrecompilePreamble ? 
0892
:
2 - CreatePreambleOnFirstParse137
;
3953
3954
1.02k
  LibclangInvocationReporter InvocationReporter(
3955
1.02k
      *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation,
3956
1.02k
      options, llvm::ArrayRef(*Args), /*InvocationArgs=*/std::nullopt,
3957
1.02k
      unsaved_files);
3958
1.02k
  std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine(
3959
1.02k
      Args->data(), Args->data() + Args->size(),
3960
1.02k
      CXXIdx->getPCHContainerOperations(), Diags,
3961
1.02k
      CXXIdx->getClangResourcesPath(), CXXIdx->getStorePreamblesInMemory(),
3962
1.02k
      CXXIdx->getPreambleStoragePath(), CXXIdx->getOnlyLocalDecls(),
3963
1.02k
      CaptureDiagnostics, *RemappedFiles.get(),
3964
1.02k
      /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses,
3965
1.02k
      TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,
3966
1.02k
      /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse,
3967
1.02k
      /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedCB,
3968
1.02k
      CXXIdx->getPCHContainerOperations()->getRawReader().getFormats().front(),
3969
1.02k
      &ErrUnit));
3970
3971
  // Early failures in LoadFromCommandLine may return with ErrUnit unset.
3972
1.02k
  if (!Unit && 
!ErrUnit2
)
3973
0
    return CXError_ASTReadError;
3974
3975
1.02k
  if (NumErrors != Diags->getClient()->getNumErrors()) {
3976
    // Make sure to check that 'Unit' is non-NULL.
3977
453
    if (CXXIdx->getDisplayDiagnostics())
3978
95
      printDiagsToStderr(Unit ? 
Unit.get()93
:
ErrUnit.get()2
);
3979
453
  }
3980
3981
1.02k
  if (isASTReadError(Unit ? 
Unit.get()1.02k
:
ErrUnit.get()9
))
3982
2
    return CXError_ASTReadError;
3983
3984
1.02k
  *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit));
3985
1.02k
  if (CXTranslationUnitImpl *TU = *out_TU) {
3986
1.01k
    TU->ParsingOptions = options;
3987
1.01k
    TU->Arguments.reserve(Args->size());
3988
1.01k
    for (const char *Arg : *Args)
3989
7.11k
      TU->Arguments.push_back(Arg);
3990
1.01k
    return CXError_Success;
3991
1.01k
  }
3992
8
  return CXError_Failure;
3993
1.02k
}
3994
3995
CXTranslationUnit
3996
clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
3997
                           const char *const *command_line_args,
3998
                           int num_command_line_args,
3999
                           struct CXUnsavedFile *unsaved_files,
4000
30
                           unsigned num_unsaved_files, unsigned options) {
4001
30
  CXTranslationUnit TU;
4002
30
  enum CXErrorCode Result = clang_parseTranslationUnit2(
4003
30
      CIdx, source_filename, command_line_args, num_command_line_args,
4004
30
      unsaved_files, num_unsaved_files, options, &TU);
4005
30
  (void)Result;
4006
30
  assert((TU && Result == CXError_Success) ||
4007
30
         (!TU && Result != CXError_Success));
4008
30
  return TU;
4009
30
}
4010
4011
enum CXErrorCode clang_parseTranslationUnit2(
4012
    CXIndex CIdx, const char *source_filename,
4013
    const char *const *command_line_args, int num_command_line_args,
4014
    struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
4015
1.02k
    unsigned options, CXTranslationUnit *out_TU) {
4016
1.02k
  noteBottomOfStack();
4017
1.02k
  SmallVector<const char *, 4> Args;
4018
1.02k
  Args.push_back("clang");
4019
1.02k
  Args.append(command_line_args, command_line_args + num_command_line_args);
4020
1.02k
  return clang_parseTranslationUnit2FullArgv(
4021
1.02k
      CIdx, source_filename, Args.data(), Args.size(), unsaved_files,
4022
1.02k
      num_unsaved_files, options, out_TU);
4023
1.02k
}
4024
4025
enum CXErrorCode clang_parseTranslationUnit2FullArgv(
4026
    CXIndex CIdx, const char *source_filename,
4027
    const char *const *command_line_args, int num_command_line_args,
4028
    struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
4029
1.03k
    unsigned options, CXTranslationUnit *out_TU) {
4030
1.03k
  LOG_FUNC_SECTION {
4031
0
    *Log << source_filename << ": ";
4032
0
    for (int i = 0; i != num_command_line_args; ++i)
4033
0
      *Log << command_line_args[i] << " ";
4034
0
  }
4035
4036
1.03k
  if (num_unsaved_files && 
!unsaved_files4
)
4037
0
    return CXError_InvalidArguments;
4038
4039
1.03k
  CXErrorCode result = CXError_Failure;
4040
1.03k
  auto ParseTranslationUnitImpl = [=, &result] {
4041
1.03k
    noteBottomOfStack();
4042
1.03k
    result = clang_parseTranslationUnit_Impl(
4043
1.03k
        CIdx, source_filename, command_line_args, num_command_line_args,
4044
1.03k
        llvm::ArrayRef(unsaved_files, num_unsaved_files), options, out_TU);
4045
1.03k
  };
4046
4047
1.03k
  llvm::CrashRecoveryContext CRC;
4048
4049
1.03k
  if (!RunSafely(CRC, ParseTranslationUnitImpl)) {
4050
7
    fprintf(stderr, "libclang: crash detected during parsing: {\n");
4051
7
    fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
4052
7
    fprintf(stderr, "  'command_line_args' : [");
4053
36
    for (int i = 0; i != num_command_line_args; 
++i29
) {
4054
29
      if (i)
4055
22
        fprintf(stderr, ", ");
4056
29
      fprintf(stderr, "'%s'", command_line_args[i]);
4057
29
    }
4058
7
    fprintf(stderr, "],\n");
4059
7
    fprintf(stderr, "  'unsaved_files' : [");
4060
9
    for (unsigned i = 0; i != num_unsaved_files; 
++i2
) {
4061
2
      if (i)
4062
0
        fprintf(stderr, ", ");
4063
2
      fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
4064
2
              unsaved_files[i].Length);
4065
2
    }
4066
7
    fprintf(stderr, "],\n");
4067
7
    fprintf(stderr, "  'options' : %d,\n", options);
4068
7
    fprintf(stderr, "}\n");
4069
4070
7
    return CXError_Crashed;
4071
1.02k
  } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
4072
0
    if (CXTranslationUnit *TU = out_TU)
4073
0
      PrintLibclangResourceUsage(*TU);
4074
0
  }
4075
4076
1.02k
  return result;
4077
1.03k
}
4078
4079
0
CXString clang_Type_getObjCEncoding(CXType CT) {
4080
0
  CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]);
4081
0
  ASTContext &Ctx = getASTUnit(tu)->getASTContext();
4082
0
  std::string encoding;
4083
0
  Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), encoding);
4084
4085
0
  return cxstring::createDup(encoding);
4086
0
}
4087
4088
5
static const IdentifierInfo *getMacroIdentifier(CXCursor C) {
4089
5
  if (C.kind == CXCursor_MacroDefinition) {
4090
2
    if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C))
4091
2
      return MDR->getName();
4092
3
  } else if (C.kind == CXCursor_MacroExpansion) {
4093
3
    MacroExpansionCursor ME = getCursorMacroExpansion(C);
4094
3
    return ME.getName();
4095
3
  }
4096
0
  return nullptr;
4097
5
}
4098
4099
2
unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) {
4100
2
  const IdentifierInfo *II = getMacroIdentifier(C);
4101
2
  if (!II) {
4102
0
    return false;
4103
0
  }
4104
2
  ASTUnit *ASTU = getCursorASTUnit(C);
4105
2
  Preprocessor &PP = ASTU->getPreprocessor();
4106
2
  if (const MacroInfo *MI = PP.getMacroInfo(II))
4107
2
    return MI->isFunctionLike();
4108
0
  return false;
4109
2
}
4110
4111
3
unsigned clang_Cursor_isMacroBuiltin(CXCursor C) {
4112
3
  const IdentifierInfo *II = getMacroIdentifier(C);
4113
3
  if (!II) {
4114
0
    return false;
4115
0
  }
4116
3
  ASTUnit *ASTU = getCursorASTUnit(C);
4117
3
  Preprocessor &PP = ASTU->getPreprocessor();
4118
3
  if (const MacroInfo *MI = PP.getMacroInfo(II))
4119
3
    return MI->isBuiltinMacro();
4120
0
  return false;
4121
3
}
4122
4123
0
unsigned clang_Cursor_isFunctionInlined(CXCursor C) {
4124
0
  const Decl *D = getCursorDecl(C);
4125
0
  const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
4126
0
  if (!FD) {
4127
0
    return false;
4128
0
  }
4129
0
  return FD->isInlined();
4130
0
}
4131
4132
0
static StringLiteral *getCFSTR_value(CallExpr *callExpr) {
4133
0
  if (callExpr->getNumArgs() != 1) {
4134
0
    return nullptr;
4135
0
  }
4136
4137
0
  StringLiteral *S = nullptr;
4138
0
  auto *arg = callExpr->getArg(0);
4139
0
  if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
4140
0
    ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg);
4141
0
    auto *subExpr = I->getSubExprAsWritten();
4142
4143
0
    if (subExpr->getStmtClass() != Stmt::StringLiteralClass) {
4144
0
      return nullptr;
4145
0
    }
4146
4147
0
    S = static_cast<StringLiteral *>(I->getSubExprAsWritten());
4148
0
  } else if (arg->getStmtClass() == Stmt::StringLiteralClass) {
4149
0
    S = static_cast<StringLiteral *>(callExpr->getArg(0));
4150
0
  } else {
4151
0
    return nullptr;
4152
0
  }
4153
0
  return S;
4154
0
}
4155
4156
struct ExprEvalResult {
4157
  CXEvalResultKind EvalType;
4158
  union {
4159
    unsigned long long unsignedVal;
4160
    long long intVal;
4161
    double floatVal;
4162
    char *stringVal;
4163
  } EvalData;
4164
  bool IsUnsignedInt;
4165
13
  ~ExprEvalResult() {
4166
13
    if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float &&
4167
13
        EvalType != CXEval_Int) {
4168
1
      delete[] EvalData.stringVal;
4169
1
    }
4170
13
  }
4171
};
4172
4173
13
void clang_EvalResult_dispose(CXEvalResult E) {
4174
13
  delete static_cast<ExprEvalResult *>(E);
4175
13
}
4176
4177
13
CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) {
4178
13
  if (!E) {
4179
0
    return CXEval_UnExposed;
4180
0
  }
4181
13
  return ((ExprEvalResult *)E)->EvalType;
4182
13
}
4183
4184
0
int clang_EvalResult_getAsInt(CXEvalResult E) {
4185
0
  return clang_EvalResult_getAsLongLong(E);
4186
0
}
4187
4188
5
long long clang_EvalResult_getAsLongLong(CXEvalResult E) {
4189
5
  if (!E) {
4190
0
    return 0;
4191
0
  }
4192
5
  ExprEvalResult *Result = (ExprEvalResult *)E;
4193
5
  if (Result->IsUnsignedInt)
4194
0
    return Result->EvalData.unsignedVal;
4195
5
  return Result->EvalData.intVal;
4196
5
}
4197
4198
12
unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) {
4199
12
  return ((ExprEvalResult *)E)->IsUnsignedInt;
4200
12
}
4201
4202
7
unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) {
4203
7
  if (!E) {
4204
0
    return 0;
4205
0
  }
4206
4207
7
  ExprEvalResult *Result = (ExprEvalResult *)E;
4208
7
  if (Result->IsUnsignedInt)
4209
7
    return Result->EvalData.unsignedVal;
4210
0
  return Result->EvalData.intVal;
4211
7
}
4212
4213
0
double clang_EvalResult_getAsDouble(CXEvalResult E) {
4214
0
  if (!E) {
4215
0
    return 0;
4216
0
  }
4217
0
  return ((ExprEvalResult *)E)->EvalData.floatVal;
4218
0
}
4219
4220
0
const char *clang_EvalResult_getAsStr(CXEvalResult E) {
4221
0
  if (!E) {
4222
0
    return nullptr;
4223
0
  }
4224
0
  return ((ExprEvalResult *)E)->EvalData.stringVal;
4225
0
}
4226
4227
14
static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
4228
14
  Expr::EvalResult ER;
4229
14
  ASTContext &ctx = getCursorContext(C);
4230
14
  if (!expr)
4231
0
    return nullptr;
4232
4233
14
  expr = expr->IgnoreParens();
4234
14
  if (expr->isValueDependent())
4235
1
    return nullptr;
4236
13
  if (!expr->EvaluateAsRValue(ER, ctx))
4237
0
    return nullptr;
4238
4239
13
  QualType rettype;
4240
13
  CallExpr *callExpr;
4241
13
  auto result = std::make_unique<ExprEvalResult>();
4242
13
  result->EvalType = CXEval_UnExposed;
4243
13
  result->IsUnsignedInt = false;
4244
4245
13
  if (ER.Val.isInt()) {
4246
12
    result->EvalType = CXEval_Int;
4247
4248
12
    auto &val = ER.Val.getInt();
4249
12
    if (val.isUnsigned()) {
4250
7
      result->IsUnsignedInt = true;
4251
7
      result->EvalData.unsignedVal = val.getZExtValue();
4252
7
    } else {
4253
5
      result->EvalData.intVal = val.getExtValue();
4254
5
    }
4255
4256
12
    return result.release();
4257
12
  }
4258
4259
1
  if (ER.Val.isFloat()) {
4260
0
    llvm::SmallVector<char, 100> Buffer;
4261
0
    ER.Val.getFloat().toString(Buffer);
4262
0
    std::string floatStr(Buffer.data(), Buffer.size());
4263
0
    result->EvalType = CXEval_Float;
4264
0
    bool ignored;
4265
0
    llvm::APFloat apFloat = ER.Val.getFloat();
4266
0
    apFloat.convert(llvm::APFloat::IEEEdouble(),
4267
0
                    llvm::APFloat::rmNearestTiesToEven, &ignored);
4268
0
    result->EvalData.floatVal = apFloat.convertToDouble();
4269
0
    return result.release();
4270
0
  }
4271
4272
1
  if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
4273
0
    const auto *I = cast<ImplicitCastExpr>(expr);
4274
0
    auto *subExpr = I->getSubExprAsWritten();
4275
0
    if (subExpr->getStmtClass() == Stmt::StringLiteralClass ||
4276
0
        subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) {
4277
0
      const StringLiteral *StrE = nullptr;
4278
0
      const ObjCStringLiteral *ObjCExpr;
4279
0
      ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr);
4280
4281
0
      if (ObjCExpr) {
4282
0
        StrE = ObjCExpr->getString();
4283
0
        result->EvalType = CXEval_ObjCStrLiteral;
4284
0
      } else {
4285
0
        StrE = cast<StringLiteral>(I->getSubExprAsWritten());
4286
0
        result->EvalType = CXEval_StrLiteral;
4287
0
      }
4288
4289
0
      std::string strRef(StrE->getString().str());
4290
0
      result->EvalData.stringVal = new char[strRef.size() + 1];
4291
0
      strncpy((char *)result->EvalData.stringVal, strRef.c_str(),
4292
0
              strRef.size());
4293
0
      result->EvalData.stringVal[strRef.size()] = '\0';
4294
0
      return result.release();
4295
0
    }
4296
1
  } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass ||
4297
1
             
expr->getStmtClass() == Stmt::StringLiteralClass0
) {
4298
1
    const StringLiteral *StrE = nullptr;
4299
1
    const ObjCStringLiteral *ObjCExpr;
4300
1
    ObjCExpr = dyn_cast<ObjCStringLiteral>(expr);
4301
4302
1
    if (ObjCExpr) {
4303
1
      StrE = ObjCExpr->getString();
4304
1
      result->EvalType = CXEval_ObjCStrLiteral;
4305
1
    } else {
4306
0
      StrE = cast<StringLiteral>(expr);
4307
0
      result->EvalType = CXEval_StrLiteral;
4308
0
    }
4309
4310
1
    std::string strRef(StrE->getString().str());
4311
1
    result->EvalData.stringVal = new char[strRef.size() + 1];
4312
1
    strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size());
4313
1
    result->EvalData.stringVal[strRef.size()] = '\0';
4314
1
    return result.release();
4315
1
  }
4316
4317
0
  if (expr->getStmtClass() == Stmt::CStyleCastExprClass) {
4318
0
    CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr);
4319
4320
0
    rettype = CC->getType();
4321
0
    if (rettype.getAsString() == "CFStringRef" &&
4322
0
        CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) {
4323
4324
0
      callExpr = static_cast<CallExpr *>(CC->getSubExpr());
4325
0
      StringLiteral *S = getCFSTR_value(callExpr);
4326
0
      if (S) {
4327
0
        std::string strLiteral(S->getString().str());
4328
0
        result->EvalType = CXEval_CFStr;
4329
4330
0
        result->EvalData.stringVal = new char[strLiteral.size() + 1];
4331
0
        strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
4332
0
                strLiteral.size());
4333
0
        result->EvalData.stringVal[strLiteral.size()] = '\0';
4334
0
        return result.release();
4335
0
      }
4336
0
    }
4337
4338
0
  } else if (expr->getStmtClass() == Stmt::CallExprClass) {
4339
0
    callExpr = static_cast<CallExpr *>(expr);
4340
0
    rettype = callExpr->getCallReturnType(ctx);
4341
4342
0
    if (rettype->isVectorType() || callExpr->getNumArgs() > 1)
4343
0
      return nullptr;
4344
4345
0
    if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) {
4346
0
      if (callExpr->getNumArgs() == 1 &&
4347
0
          !callExpr->getArg(0)->getType()->isIntegralType(ctx))
4348
0
        return nullptr;
4349
0
    } else if (rettype.getAsString() == "CFStringRef") {
4350
4351
0
      StringLiteral *S = getCFSTR_value(callExpr);
4352
0
      if (S) {
4353
0
        std::string strLiteral(S->getString().str());
4354
0
        result->EvalType = CXEval_CFStr;
4355
0
        result->EvalData.stringVal = new char[strLiteral.size() + 1];
4356
0
        strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
4357
0
                strLiteral.size());
4358
0
        result->EvalData.stringVal[strLiteral.size()] = '\0';
4359
0
        return result.release();
4360
0
      }
4361
0
    }
4362
0
  } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) {
4363
0
    DeclRefExpr *D = static_cast<DeclRefExpr *>(expr);
4364
0
    ValueDecl *V = D->getDecl();
4365
0
    if (V->getKind() == Decl::Function) {
4366
0
      std::string strName = V->getNameAsString();
4367
0
      result->EvalType = CXEval_Other;
4368
0
      result->EvalData.stringVal = new char[strName.size() + 1];
4369
0
      strncpy(result->EvalData.stringVal, strName.c_str(), strName.size());
4370
0
      result->EvalData.stringVal[strName.size()] = '\0';
4371
0
      return result.release();
4372
0
    }
4373
0
  }
4374
4375
0
  return nullptr;
4376
0
}
4377
4378
9
static const Expr *evaluateDeclExpr(const Decl *D) {
4379
9
  if (!D)
4380
0
    return nullptr;
4381
9
  if (auto *Var = dyn_cast<VarDecl>(D))
4382
8
    return Var->getInit();
4383
1
  else if (auto *Field = dyn_cast<FieldDecl>(D))
4384
1
    return Field->getInClassInitializer();
4385
0
  return nullptr;
4386
9
}
4387
4388
1
static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) {
4389
1
  assert(CS && "invalid compound statement");
4390
1
  for (auto *bodyIterator : CS->body()) {
4391
1
    if (const auto *E = dyn_cast<Expr>(bodyIterator))
4392
1
      return E;
4393
1
  }
4394
0
  return nullptr;
4395
1
}
4396
4397
15
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
4398
15
  const Expr *E = nullptr;
4399
15
  if (clang_getCursorKind(C) == CXCursor_CompoundStmt)
4400
1
    E = evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C)));
4401
14
  else if (clang_isDeclaration(C.kind))
4402
9
    E = evaluateDeclExpr(getCursorDecl(C));
4403
5
  else if (clang_isExpression(C.kind))
4404
4
    E = getCursorExpr(C);
4405
15
  if (E)
4406
14
    return const_cast<CXEvalResult>(
4407
14
        reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C)));
4408
1
  return nullptr;
4409
15
}
4410
4411
0
unsigned clang_Cursor_hasAttrs(CXCursor C) {
4412
0
  const Decl *D = getCursorDecl(C);
4413
0
  if (!D) {
4414
0
    return 0;
4415
0
  }
4416
4417
0
  if (D->hasAttrs()) {
4418
0
    return 1;
4419
0
  }
4420
4421
0
  return 0;
4422
0
}
4423
59
unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
4424
59
  return CXSaveTranslationUnit_None;
4425
59
}
4426
4427
static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU,
4428
                                                  const char *FileName,
4429
59
                                                  unsigned options) {
4430
59
  CIndexer *CXXIdx = TU->CIdx;
4431
59
  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
4432
0
    setThreadBackgroundPriority();
4433
4434
59
  bool hadError = cxtu::getASTUnit(TU)->Save(FileName);
4435
59
  return hadError ? 
CXSaveError_Unknown0
: CXSaveError_None;
4436
59
}
4437
4438
int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
4439
59
                              unsigned options) {
4440
59
  LOG_FUNC_SECTION 
{ *Log << TU << ' ' << FileName; }0
4441
4442
59
  if (isNotUsableTU(TU)) {
4443
0
    LOG_BAD_TU(TU);
4444
0
    return CXSaveError_InvalidTU;
4445
0
  }
4446
4447
59
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4448
59
  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4449
59
  if (!CXXUnit->hasSema())
4450
0
    return CXSaveError_InvalidTU;
4451
4452
59
  CXSaveError result;
4453
59
  auto SaveTranslationUnitImpl = [=, &result]() {
4454
59
    result = clang_saveTranslationUnit_Impl(TU, FileName, options);
4455
59
  };
4456
4457
59
  if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) {
4458
54
    SaveTranslationUnitImpl();
4459
4460
54
    if (getenv("LIBCLANG_RESOURCE_USAGE"))
4461
0
      PrintLibclangResourceUsage(TU);
4462
4463
54
    return result;
4464
54
  }
4465
4466
  // We have an AST that has invalid nodes due to compiler errors.
4467
  // Use a crash recovery thread for protection.
4468
4469
5
  llvm::CrashRecoveryContext CRC;
4470
4471
5
  if (!RunSafely(CRC, SaveTranslationUnitImpl)) {
4472
0
    fprintf(stderr, "libclang: crash detected during AST saving: {\n");
4473
0
    fprintf(stderr, "  'filename' : '%s'\n", FileName);
4474
0
    fprintf(stderr, "  'options' : %d,\n", options);
4475
0
    fprintf(stderr, "}\n");
4476
4477
0
    return CXSaveError_Unknown;
4478
4479
5
  } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
4480
0
    PrintLibclangResourceUsage(TU);
4481
0
  }
4482
4483
5
  return result;
4484
5
}
4485
4486
1.10k
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
4487
1.10k
  if (CTUnit) {
4488
    // If the translation unit has been marked as unsafe to free, just discard
4489
    // it.
4490
1.09k
    ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
4491
1.09k
    if (Unit && Unit->isUnsafeToFree())
4492
1
      return;
4493
4494
1.09k
    delete cxtu::getASTUnit(CTUnit);
4495
1.09k
    delete CTUnit->StringPool;
4496
1.09k
    delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
4497
1.09k
    disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool);
4498
1.09k
    delete CTUnit->CommentToXML;
4499
1.09k
    delete CTUnit;
4500
1.09k
  }
4501
1.10k
}
4502
4503
30
unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) {
4504
30
  if (CTUnit) {
4505
30
    ASTUnit *Unit = cxtu::getASTUnit(CTUnit);
4506
4507
30
    if (Unit && Unit->isUnsafeToFree())
4508
0
      return false;
4509
4510
30
    Unit->ResetForParse();
4511
30
    return true;
4512
30
  }
4513
4514
0
  return false;
4515
30
}
4516
4517
819
unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
4518
819
  return CXReparse_None;
4519
819
}
4520
4521
static CXErrorCode
4522
clang_reparseTranslationUnit_Impl(CXTranslationUnit TU,
4523
                                  ArrayRef<CXUnsavedFile> unsaved_files,
4524
819
                                  unsigned options) {
4525
  // Check arguments.
4526
819
  if (isNotUsableTU(TU)) {
4527
0
    LOG_BAD_TU(TU);
4528
0
    return CXError_InvalidArguments;
4529
0
  }
4530
4531
  // Reset the associated diagnostics.
4532
819
  delete static_cast<CXDiagnosticSetImpl *>(TU->Diagnostics);
4533
819
  TU->Diagnostics = nullptr;
4534
4535
819
  CIndexer *CXXIdx = TU->CIdx;
4536
819
  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
4537
0
    setThreadBackgroundPriority();
4538
4539
819
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4540
819
  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4541
4542
819
  std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles(
4543
819
      new std::vector<ASTUnit::RemappedFile>());
4544
4545
  // Recover resources if we crash before exiting this function.
4546
819
  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>>
4547
819
      RemappedCleanup(RemappedFiles.get());
4548
4549
819
  for (auto &UF : unsaved_files) {
4550
12
    std::unique_ptr<llvm::MemoryBuffer> MB =
4551
12
        llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
4552
12
    RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release()));
4553
12
  }
4554
4555
819
  if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(),
4556
819
                        *RemappedFiles.get()))
4557
818
    return CXError_Success;
4558
1
  if (isASTReadError(CXXUnit))
4559
0
    return CXError_ASTReadError;
4560
1
  return CXError_Failure;
4561
1
}
4562
4563
int clang_reparseTranslationUnit(CXTranslationUnit TU,
4564
                                 unsigned num_unsaved_files,
4565
                                 struct CXUnsavedFile *unsaved_files,
4566
819
                                 unsigned options) {
4567
819
  LOG_FUNC_SECTION 
{ *Log << TU; }0
4568
4569
819
  if (num_unsaved_files && 
!unsaved_files12
)
4570
0
    return CXError_InvalidArguments;
4571
4572
819
  CXErrorCode result;
4573
819
  auto ReparseTranslationUnitImpl = [=, &result]() {
4574
819
    result = clang_reparseTranslationUnit_Impl(
4575
819
        TU, llvm::ArrayRef(unsaved_files, num_unsaved_files), options);
4576
819
  };
4577
4578
819
  llvm::CrashRecoveryContext CRC;
4579
4580
819
  if (!RunSafely(CRC, ReparseTranslationUnitImpl)) {
4581
1
    fprintf(stderr, "libclang: crash detected during reparsing\n");
4582
1
    cxtu::getASTUnit(TU)->setUnsafeToFree(true);
4583
1
    return CXError_Crashed;
4584
818
  } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
4585
0
    PrintLibclangResourceUsage(TU);
4586
4587
818
  return result;
4588
819
}
4589
4590
0
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
4591
0
  if (isNotUsableTU(CTUnit)) {
4592
0
    LOG_BAD_TU(CTUnit);
4593
0
    return cxstring::createEmpty();
4594
0
  }
4595
4596
0
  ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4597
0
  return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
4598
0
}
4599
4600
212
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
4601
212
  if (isNotUsableTU(TU)) {
4602
0
    LOG_BAD_TU(TU);
4603
0
    return clang_getNullCursor();
4604
0
  }
4605
4606
212
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4607
212
  return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
4608
212
}
4609
4610
2
CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) {
4611
2
  if (isNotUsableTU(CTUnit)) {
4612
0
    LOG_BAD_TU(CTUnit);
4613
0
    return nullptr;
4614
0
  }
4615
4616
2
  CXTargetInfoImpl *impl = new CXTargetInfoImpl();
4617
2
  impl->TranslationUnit = CTUnit;
4618
2
  return impl;
4619
2
}
4620
4621
2
CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) {
4622
2
  if (!TargetInfo)
4623
0
    return cxstring::createEmpty();
4624
4625
2
  CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
4626
2
  assert(!isNotUsableTU(CTUnit) &&
4627
2
         "Unexpected unusable translation unit in TargetInfo");
4628
4629
2
  ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4630
2
  std::string Triple =
4631
2
      CXXUnit->getASTContext().getTargetInfo().getTriple().normalize();
4632
2
  return cxstring::createDup(Triple);
4633
2
}
4634
4635
2
int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) {
4636
2
  if (!TargetInfo)
4637
0
    return -1;
4638
4639
2
  CXTranslationUnit CTUnit = TargetInfo->TranslationUnit;
4640
2
  assert(!isNotUsableTU(CTUnit) &&
4641
2
         "Unexpected unusable translation unit in TargetInfo");
4642
4643
2
  ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
4644
2
  return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth();
4645
2
}
4646
4647
2
void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) {
4648
2
  if (!TargetInfo)
4649
0
    return;
4650
4651
2
  delete TargetInfo;
4652
2
}
4653
4654
//===----------------------------------------------------------------------===//
4655
// CXFile Operations.
4656
//===----------------------------------------------------------------------===//
4657
4658
72.4k
CXString clang_getFileName(CXFile SFile) {
4659
72.4k
  if (!SFile)
4660
57.8k
    return cxstring::createNull();
4661
4662
14.6k
  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4663
14.6k
  return cxstring::createRef(FEnt->getName());
4664
72.4k
}
4665
4666
0
time_t clang_getFileTime(CXFile SFile) {
4667
0
  if (!SFile)
4668
0
    return 0;
4669
4670
0
  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4671
0
  return FEnt->getModificationTime();
4672
0
}
4673
4674
483
CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
4675
483
  if (isNotUsableTU(TU)) {
4676
0
    LOG_BAD_TU(TU);
4677
0
    return nullptr;
4678
0
  }
4679
4680
483
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4681
4682
483
  FileManager &FMgr = CXXUnit->getFileManager();
4683
483
  auto File = FMgr.getFile(file_name);
4684
483
  if (!File)
4685
0
    return nullptr;
4686
483
  return const_cast<FileEntry *>(*File);
4687
483
}
4688
4689
const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
4690
0
                                  size_t *size) {
4691
0
  if (isNotUsableTU(TU)) {
4692
0
    LOG_BAD_TU(TU);
4693
0
    return nullptr;
4694
0
  }
4695
4696
0
  const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
4697
0
  FileID fid = SM.translateFile(static_cast<FileEntry *>(file));
4698
0
  std::optional<llvm::MemoryBufferRef> buf = SM.getBufferOrNone(fid);
4699
0
  if (!buf) {
4700
0
    if (size)
4701
0
      *size = 0;
4702
0
    return nullptr;
4703
0
  }
4704
0
  if (size)
4705
0
    *size = buf->getBufferSize();
4706
0
  return buf->getBufferStart();
4707
0
}
4708
4709
331
unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
4710
331
  if (isNotUsableTU(TU)) {
4711
0
    LOG_BAD_TU(TU);
4712
0
    return 0;
4713
0
  }
4714
4715
331
  if (!file)
4716
4
    return 0;
4717
4718
327
  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
4719
327
  FileEntry *FEnt = static_cast<FileEntry *>(file);
4720
327
  return CXXUnit->getPreprocessor()
4721
327
      .getHeaderSearchInfo()
4722
327
      .isFileMultipleIncludeGuarded(FEnt);
4723
331
}
4724
4725
0
int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
4726
0
  if (!file || !outID)
4727
0
    return 1;
4728
4729
0
  FileEntry *FEnt = static_cast<FileEntry *>(file);
4730
0
  const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
4731
0
  outID->data[0] = ID.getDevice();
4732
0
  outID->data[1] = ID.getFile();
4733
0
  outID->data[2] = FEnt->getModificationTime();
4734
0
  return 0;
4735
0
}
4736
4737
0
int clang_File_isEqual(CXFile file1, CXFile file2) {
4738
0
  if (file1 == file2)
4739
0
    return true;
4740
4741
0
  if (!file1 || !file2)
4742
0
    return false;
4743
4744
0
  FileEntry *FEnt1 = static_cast<FileEntry *>(file1);
4745
0
  FileEntry *FEnt2 = static_cast<FileEntry *>(file2);
4746
0
  return FEnt1->getUniqueID() == FEnt2->getUniqueID();
4747
0
}
4748
4749
1
CXString clang_File_tryGetRealPathName(CXFile SFile) {
4750
1
  if (!SFile)
4751
0
    return cxstring::createNull();
4752
4753
1
  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
4754
1
  return cxstring::createRef(FEnt->tryGetRealPathName());
4755
1
}
4756
4757
//===----------------------------------------------------------------------===//
4758
// CXCursor Operations.
4759
//===----------------------------------------------------------------------===//
4760
4761
38.8k
static const Decl *getDeclFromExpr(const Stmt *E) {
4762
38.8k
  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
4763
2.62k
    return getDeclFromExpr(CE->getSubExpr());
4764
4765
36.2k
  if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
4766
26.6k
    return RefExpr->getDecl();
4767
9.60k
  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
4768
2.56k
    return ME->getMemberDecl();
4769
7.04k
  if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
4770
4
    return RE->getDecl();
4771
7.03k
  if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) {
4772
19
    if (PRE->isExplicitProperty())
4773
10
      return PRE->getExplicitProperty();
4774
    // It could be messaging both getter and setter as in:
4775
    // ++myobj.myprop;
4776
    // in which case prefer to associate the setter since it is less obvious
4777
    // from inspecting the source that the setter is going to get called.
4778
9
    if (PRE->isMessagingSetter())
4779
6
      return PRE->getImplicitPropertySetter();
4780
3
    return PRE->getImplicitPropertyGetter();
4781
9
  }
4782
7.01k
  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
4783
0
    return getDeclFromExpr(POE->getSyntacticForm());
4784
7.01k
  if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
4785
2
    if (Expr *Src = OVE->getSourceExpr())
4786
2
      return getDeclFromExpr(Src);
4787
4788
7.01k
  if (const CallExpr *CE = dyn_cast<CallExpr>(E))
4789
968
    return getDeclFromExpr(CE->getCallee());
4790
6.04k
  if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
4791
192
    if (!CE->isElidable())
4792
174
      return CE->getConstructor();
4793
5.87k
  if (const CXXInheritedCtorInitExpr *CE =
4794
5.87k
          dyn_cast<CXXInheritedCtorInitExpr>(E))
4795
0
    return CE->getConstructor();
4796
5.87k
  if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
4797
210
    return OME->getMethodDecl();
4798
4799
5.66k
  if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
4800
12
    return PE->getProtocol();
4801
5.65k
  if (const SubstNonTypeTemplateParmPackExpr *NTTP =
4802
5.65k
          dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
4803
0
    return NTTP->getParameterPack();
4804
5.65k
  if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
4805
18
    if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
4806
18
        isa<ParmVarDecl>(SizeOfPack->getPack()))
4807
10
      return SizeOfPack->getPack();
4808
4809
5.64k
  return nullptr;
4810
5.65k
}
4811
4812
10.5k
static SourceLocation getLocationFromExpr(const Expr *E) {
4813
10.5k
  if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
4814
2.30k
    return getLocationFromExpr(CE->getSubExpr());
4815
4816
8.27k
  if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
4817
46
    return /*FIXME:*/ Msg->getLeftLoc();
4818
8.22k
  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
4819
2.46k
    return DRE->getLocation();
4820
5.75k
  if (const MemberExpr *Member = dyn_cast<MemberExpr>(E))
4821
821
    return Member->getMemberLoc();
4822
4.93k
  if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
4823
0
    return Ivar->getLocation();
4824
4.93k
  if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
4825
0
    return SizeOfPack->getPackLoc();
4826
4.93k
  if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E))
4827
10
    return PropRef->getLocation();
4828
4829
4.92k
  return E->getBeginLoc();
4830
4.93k
}
4831
4832
extern "C" {
4833
4834
unsigned clang_visitChildren(CXCursor parent, CXCursorVisitor visitor,
4835
217
                             CXClientData client_data) {
4836
217
  CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
4837
217
                          /*VisitPreprocessorLast=*/false);
4838
217
  return CursorVis.VisitChildren(parent);
4839
217
}
4840
4841
#ifndef __has_feature
4842
#define __has_feature(x) 0
4843
#endif
4844
#if __has_feature(blocks)
4845
typedef enum CXChildVisitResult (^CXCursorVisitorBlock)(CXCursor cursor,
4846
                                                        CXCursor parent);
4847
4848
static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
4849
0
                                              CXClientData client_data) {
4850
0
  CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
4851
0
  return block(cursor, parent);
4852
0
}
4853
#else
4854
// If we are compiled with a compiler that doesn't have native blocks support,
4855
// define and call the block manually, so the
4856
typedef struct _CXChildVisitResult {
4857
  void *isa;
4858
  int flags;
4859
  int reserved;
4860
  enum CXChildVisitResult (*invoke)(struct _CXChildVisitResult *, CXCursor,
4861
                                    CXCursor);
4862
} * CXCursorVisitorBlock;
4863
4864
static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
4865
                                              CXClientData client_data) {
4866
  CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
4867
  return block->invoke(block, cursor, parent);
4868
}
4869
#endif
4870
4871
unsigned clang_visitChildrenWithBlock(CXCursor parent,
4872
0
                                      CXCursorVisitorBlock block) {
4873
0
  return clang_visitChildren(parent, visitWithBlock, block);
4874
0
}
4875
4876
22.7k
static CXString getDeclSpelling(const Decl *D) {
4877
22.7k
  if (!D)
4878
0
    return cxstring::createEmpty();
4879
4880
22.7k
  const NamedDecl *ND = dyn_cast<NamedDecl>(D);
4881
22.7k
  if (!ND) {
4882
195
    if (const ObjCPropertyImplDecl *PropImpl =
4883
195
            dyn_cast<ObjCPropertyImplDecl>(D))
4884
30
      if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
4885
30
        return cxstring::createDup(Property->getIdentifier()->getName());
4886
4887
165
    if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D))
4888
15
      if (Module *Mod = ImportD->getImportedModule())
4889
15
        return cxstring::createDup(Mod->getFullModuleName());
4890
4891
150
    return cxstring::createEmpty();
4892
165
  }
4893
4894
22.5k
  if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
4895
538
    return cxstring::createDup(OMD->getSelector().getAsString());
4896
4897
21.9k
  if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
4898
    // No, this isn't the same as the code below. getIdentifier() is non-virtual
4899
    // and returns different names. NamedDecl returns the class name and
4900
    // ObjCCategoryImplDecl returns the category name.
4901
7
    return cxstring::createRef(CIMP->getIdentifier()->getNameStart());
4902
4903
21.9k
  if (isa<UsingDirectiveDecl>(D))
4904
8
    return cxstring::createEmpty();
4905
4906
21.9k
  SmallString<1024> S;
4907
21.9k
  llvm::raw_svector_ostream os(S);
4908
21.9k
  ND->printName(os);
4909
4910
21.9k
  return cxstring::createDup(os.str());
4911
21.9k
}
4912
4913
95.7k
CXString clang_getCursorSpelling(CXCursor C) {
4914
95.7k
  if (clang_isTranslationUnit(C.kind))
4915
0
    return clang_getTranslationUnitSpelling(getCursorTU(C));
4916
4917
95.7k
  if (clang_isReference(C.kind)) {
4918
1.90k
    switch (C.kind) {
4919
25
    case CXCursor_ObjCSuperClassRef: {
4920
25
      const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
4921
25
      return cxstring::createRef(Super->getIdentifier()->getNameStart());
4922
0
    }
4923
220
    case CXCursor_ObjCClassRef: {
4924
220
      const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
4925
220
      return cxstring::createRef(Class->getIdentifier()->getNameStart());
4926
0
    }
4927
29
    case CXCursor_ObjCProtocolRef: {
4928
29
      const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
4929
29
      assert(OID && "getCursorSpelling(): Missing protocol decl");
4930
29
      return cxstring::createRef(OID->getIdentifier()->getNameStart());
4931
29
    }
4932
167
    case CXCursor_CXXBaseSpecifier: {
4933
167
      const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
4934
167
      return cxstring::createDup(B->getType().getAsString());
4935
29
    }
4936
1.08k
    case CXCursor_TypeRef: {
4937
1.08k
      const TypeDecl *Type = getCursorTypeRef(C).first;
4938
1.08k
      assert(Type && "Missing type decl");
4939
4940
1.08k
      return cxstring::createDup(
4941
1.08k
          getCursorContext(C).getTypeDeclType(Type).getAsString());
4942
1.08k
    }
4943
159