Coverage Report

Created: 2020-09-22 08:39

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