Coverage Report

Created: 2020-02-15 09:57

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