Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXCursor.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
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 defines routines for manipulating CXCursors. It should be the
10
// only file that has internal knowledge of the encoding of the data in
11
// CXCursor.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "CXCursor.h"
16
#include "CXString.h"
17
#include "CXTranslationUnit.h"
18
#include "CXType.h"
19
#include "clang-c/Index.h"
20
#include "clang/AST/Attr.h"
21
#include "clang/AST/Decl.h"
22
#include "clang/AST/DeclCXX.h"
23
#include "clang/AST/DeclObjC.h"
24
#include "clang/AST/DeclTemplate.h"
25
#include "clang/AST/Expr.h"
26
#include "clang/AST/ExprCXX.h"
27
#include "clang/AST/ExprObjC.h"
28
#include "clang/Frontend/ASTUnit.h"
29
#include "llvm/Support/ErrorHandling.h"
30
31
using namespace clang;
32
using namespace cxcursor;
33
34
361k
CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
35
361k
  assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
36
0
  CXCursor C = {K, 0, {nullptr, nullptr, TU}};
37
361k
  return C;
38
361k
}
39
40
225
static CXCursorKind GetCursorKind(const Attr *A) {
41
225
  assert(A && "Invalid arguments!");
42
0
  switch (A->getKind()) {
43
120
  default:
44
120
    break;
45
120
  case attr::IBAction:
46
5
    return CXCursor_IBActionAttr;
47
3
  case attr::IBOutlet:
48
3
    return CXCursor_IBOutletAttr;
49
6
  case attr::IBOutletCollection:
50
6
    return CXCursor_IBOutletCollectionAttr;
51
2
  case attr::Final:
52
2
    return CXCursor_CXXFinalAttr;
53
3
  case attr::Override:
54
3
    return CXCursor_CXXOverrideAttr;
55
12
  case attr::Annotate:
56
12
    return CXCursor_AnnotateAttr;
57
1
  case attr::AsmLabel:
58
1
    return CXCursor_AsmLabelAttr;
59
7
  case attr::Packed:
60
7
    return CXCursor_PackedAttr;
61
1
  case attr::Pure:
62
1
    return CXCursor_PureAttr;
63
1
  case attr::Const:
64
1
    return CXCursor_ConstAttr;
65
1
  case attr::NoDuplicate:
66
1
    return CXCursor_NoDuplicateAttr;
67
3
  case attr::CUDAConstant:
68
3
    return CXCursor_CUDAConstantAttr;
69
3
  case attr::CUDADevice:
70
3
    return CXCursor_CUDADeviceAttr;
71
3
  case attr::CUDAGlobal:
72
3
    return CXCursor_CUDAGlobalAttr;
73
3
  case attr::CUDAHost:
74
3
    return CXCursor_CUDAHostAttr;
75
3
  case attr::CUDAShared:
76
3
    return CXCursor_CUDASharedAttr;
77
8
  case attr::Visibility:
78
8
    return CXCursor_VisibilityAttr;
79
9
  case attr::DLLExport:
80
9
    return CXCursor_DLLExport;
81
8
  case attr::DLLImport:
82
8
    return CXCursor_DLLImport;
83
2
  case attr::NSReturnsRetained:
84
2
    return CXCursor_NSReturnsRetained;
85
1
  case attr::NSReturnsNotRetained:
86
1
    return CXCursor_NSReturnsNotRetained;
87
1
  case attr::NSReturnsAutoreleased:
88
1
    return CXCursor_NSReturnsAutoreleased;
89
2
  case attr::NSConsumesSelf:
90
2
    return CXCursor_NSConsumesSelf;
91
0
  case attr::NSConsumed:
92
0
    return CXCursor_NSConsumed;
93
1
  case attr::ObjCException:
94
1
    return CXCursor_ObjCException;
95
0
  case attr::ObjCNSObject:
96
0
    return CXCursor_ObjCNSObject;
97
1
  case attr::ObjCIndependentClass:
98
1
    return CXCursor_ObjCIndependentClass;
99
1
  case attr::ObjCPreciseLifetime:
100
1
    return CXCursor_ObjCPreciseLifetime;
101
1
  case attr::ObjCReturnsInnerPointer:
102
1
    return CXCursor_ObjCReturnsInnerPointer;
103
1
  case attr::ObjCRequiresSuper:
104
1
    return CXCursor_ObjCRequiresSuper;
105
1
  case attr::ObjCRootClass:
106
1
    return CXCursor_ObjCRootClass;
107
1
  case attr::ObjCSubclassingRestricted:
108
1
    return CXCursor_ObjCSubclassingRestricted;
109
1
  case attr::ObjCExplicitProtocolImpl:
110
1
    return CXCursor_ObjCExplicitProtocolImpl;
111
1
  case attr::ObjCDesignatedInitializer:
112
1
    return CXCursor_ObjCDesignatedInitializer;
113
1
  case attr::ObjCRuntimeVisible:
114
1
    return CXCursor_ObjCRuntimeVisible;
115
1
  case attr::ObjCBoxable:
116
1
    return CXCursor_ObjCBoxable;
117
1
  case attr::FlagEnum:
118
1
    return CXCursor_FlagEnum;
119
1
  case attr::Convergent:
120
1
    return CXCursor_ConvergentAttr;
121
1
  case attr::WarnUnused:
122
1
    return CXCursor_WarnUnusedAttr;
123
1
  case attr::WarnUnusedResult:
124
1
    return CXCursor_WarnUnusedResultAttr;
125
2
  case attr::Aligned:
126
2
    return CXCursor_AlignedAttr;
127
225
  }
128
129
120
  return CXCursor_UnexposedAttr;
130
225
}
131
132
CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
133
225
                                CXTranslationUnit TU) {
134
225
  assert(A && Parent && TU && "Invalid arguments!");
135
0
  CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
136
225
  return C;
137
225
}
138
139
CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
140
                                SourceRange RegionOfInterest,
141
39.0k
                                bool FirstInDeclGroup) {
142
39.0k
  assert(D && TU && "Invalid arguments!");
143
144
0
  CXCursorKind K = getCursorKindForDecl(D);
145
146
39.0k
  if (K == CXCursor_ObjCClassMethodDecl ||
147
39.0k
      
K == CXCursor_ObjCInstanceMethodDecl38.9k
) {
148
1.09k
    int SelectorIdIndex = -1;
149
    // Check if cursor points to a selector id.
150
1.09k
    if (RegionOfInterest.isValid() &&
151
1.09k
        
RegionOfInterest.getBegin() == RegionOfInterest.getEnd()467
) {
152
395
      SmallVector<SourceLocation, 16> SelLocs;
153
395
      cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
154
395
      SmallVectorImpl<SourceLocation>::iterator I =
155
395
          llvm::find(SelLocs, RegionOfInterest.getBegin());
156
395
      if (I != SelLocs.end())
157
76
        SelectorIdIndex = I - SelLocs.begin();
158
395
    }
159
1.09k
    CXCursor C = {K,
160
1.09k
                  SelectorIdIndex,
161
1.09k
                  {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 
00
), TU}};
162
1.09k
    return C;
163
1.09k
  }
164
165
37.9k
  CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 
137.9k
:
012
), TU}};
166
37.9k
  return C;
167
39.0k
}
168
169
CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
170
                                CXTranslationUnit TU,
171
44.6k
                                SourceRange RegionOfInterest) {
172
44.6k
  assert(S && TU && "Invalid arguments!");
173
0
  CXCursorKind K = CXCursor_NotImplemented;
174
175
44.6k
  switch (S->getStmtClass()) {
176
0
  case Stmt::NoStmtClass:
177
0
    break;
178
179
54
  case Stmt::CaseStmtClass:
180
54
    K = CXCursor_CaseStmt;
181
54
    break;
182
183
9
  case Stmt::DefaultStmtClass:
184
9
    K = CXCursor_DefaultStmt;
185
9
    break;
186
187
22
  case Stmt::IfStmtClass:
188
22
    K = CXCursor_IfStmt;
189
22
    break;
190
191
25
  case Stmt::SwitchStmtClass:
192
25
    K = CXCursor_SwitchStmt;
193
25
    break;
194
195
4
  case Stmt::WhileStmtClass:
196
4
    K = CXCursor_WhileStmt;
197
4
    break;
198
199
12
  case Stmt::DoStmtClass:
200
12
    K = CXCursor_DoStmt;
201
12
    break;
202
203
4
  case Stmt::ForStmtClass:
204
4
    K = CXCursor_ForStmt;
205
4
    break;
206
207
4
  case Stmt::GotoStmtClass:
208
4
    K = CXCursor_GotoStmt;
209
4
    break;
210
211
2
  case Stmt::IndirectGotoStmtClass:
212
2
    K = CXCursor_IndirectGotoStmt;
213
2
    break;
214
215
0
  case Stmt::ContinueStmtClass:
216
0
    K = CXCursor_ContinueStmt;
217
0
    break;
218
219
4
  case Stmt::BreakStmtClass:
220
4
    K = CXCursor_BreakStmt;
221
4
    break;
222
223
332
  case Stmt::ReturnStmtClass:
224
332
    K = CXCursor_ReturnStmt;
225
332
    break;
226
227
0
  case Stmt::GCCAsmStmtClass:
228
0
    K = CXCursor_GCCAsmStmt;
229
0
    break;
230
231
0
  case Stmt::MSAsmStmtClass:
232
0
    K = CXCursor_MSAsmStmt;
233
0
    break;
234
235
0
  case Stmt::ObjCAtTryStmtClass:
236
0
    K = CXCursor_ObjCAtTryStmt;
237
0
    break;
238
239
0
  case Stmt::ObjCAtCatchStmtClass:
240
0
    K = CXCursor_ObjCAtCatchStmt;
241
0
    break;
242
243
0
  case Stmt::ObjCAtFinallyStmtClass:
244
0
    K = CXCursor_ObjCAtFinallyStmt;
245
0
    break;
246
247
0
  case Stmt::ObjCAtThrowStmtClass:
248
0
    K = CXCursor_ObjCAtThrowStmt;
249
0
    break;
250
251
0
  case Stmt::ObjCAtSynchronizedStmtClass:
252
0
    K = CXCursor_ObjCAtSynchronizedStmt;
253
0
    break;
254
255
0
  case Stmt::ObjCAutoreleasePoolStmtClass:
256
0
    K = CXCursor_ObjCAutoreleasePoolStmt;
257
0
    break;
258
259
0
  case Stmt::ObjCForCollectionStmtClass:
260
0
    K = CXCursor_ObjCForCollectionStmt;
261
0
    break;
262
263
8
  case Stmt::CXXCatchStmtClass:
264
8
    K = CXCursor_CXXCatchStmt;
265
8
    break;
266
267
9
  case Stmt::CXXTryStmtClass:
268
9
    K = CXCursor_CXXTryStmt;
269
9
    break;
270
271
8
  case Stmt::CXXForRangeStmtClass:
272
8
    K = CXCursor_CXXForRangeStmt;
273
8
    break;
274
275
0
  case Stmt::SEHTryStmtClass:
276
0
    K = CXCursor_SEHTryStmt;
277
0
    break;
278
279
0
  case Stmt::SEHExceptStmtClass:
280
0
    K = CXCursor_SEHExceptStmt;
281
0
    break;
282
283
0
  case Stmt::SEHFinallyStmtClass:
284
0
    K = CXCursor_SEHFinallyStmt;
285
0
    break;
286
287
0
  case Stmt::SEHLeaveStmtClass:
288
0
    K = CXCursor_SEHLeaveStmt;
289
0
    break;
290
291
2
  case Stmt::CoroutineBodyStmtClass:
292
6
  case Stmt::CoreturnStmtClass:
293
6
    K = CXCursor_UnexposedStmt;
294
6
    break;
295
296
0
  case Stmt::ArrayTypeTraitExprClass:
297
0
  case Stmt::AsTypeExprClass:
298
0
  case Stmt::AtomicExprClass:
299
8
  case Stmt::BinaryConditionalOperatorClass:
300
20
  case Stmt::TypeTraitExprClass:
301
24
  case Stmt::CoawaitExprClass:
302
24
  case Stmt::DependentCoawaitExprClass:
303
24
  case Stmt::CoyieldExprClass:
304
24
  case Stmt::CXXBindTemporaryExprClass:
305
26
  case Stmt::CXXDefaultArgExprClass:
306
26
  case Stmt::CXXDefaultInitExprClass:
307
26
  case Stmt::CXXFoldExprClass:
308
26
  case Stmt::CXXRewrittenBinaryOperatorClass:
309
28
  case Stmt::CXXStdInitializerListExprClass:
310
32
  case Stmt::CXXScalarValueInitExprClass:
311
32
  case Stmt::CXXUuidofExprClass:
312
32
  case Stmt::ChooseExprClass:
313
62
  case Stmt::DesignatedInitExprClass:
314
62
  case Stmt::DesignatedInitUpdateExprClass:
315
64
  case Stmt::ArrayInitLoopExprClass:
316
66
  case Stmt::ArrayInitIndexExprClass:
317
204
  case Stmt::ExprWithCleanupsClass:
318
204
  case Stmt::ExpressionTraitExprClass:
319
204
  case Stmt::ExtVectorElementExprClass:
320
13.5k
  case Stmt::ImplicitCastExprClass:
321
13.5k
  case Stmt::ImplicitValueInitExprClass:
322
13.5k
  case Stmt::NoInitExprClass:
323
14.0k
  case Stmt::MaterializeTemporaryExprClass:
324
14.0k
  case Stmt::ObjCIndirectCopyRestoreExprClass:
325
14.0k
  case Stmt::OffsetOfExprClass:
326
14.0k
  case Stmt::ParenListExprClass:
327
14.0k
  case Stmt::PredefinedExprClass:
328
14.0k
  case Stmt::ShuffleVectorExprClass:
329
14.0k
  case Stmt::SourceLocExprClass:
330
14.0k
  case Stmt::ConvertVectorExprClass:
331
14.0k
  case Stmt::VAArgExprClass:
332
14.0k
  case Stmt::ObjCArrayLiteralClass:
333
14.0k
  case Stmt::ObjCDictionaryLiteralClass:
334
14.1k
  case Stmt::ObjCBoxedExprClass:
335
14.1k
  case Stmt::ObjCSubscriptRefExprClass:
336
14.1k
  case Stmt::RecoveryExprClass:
337
14.1k
  case Stmt::SYCLUniqueStableNameExprClass:
338
14.1k
    K = CXCursor_UnexposedExpr;
339
14.1k
    break;
340
341
109
  case Stmt::OpaqueValueExprClass:
342
109
    if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
343
105
      return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
344
4
    K = CXCursor_UnexposedExpr;
345
4
    break;
346
347
45
  case Stmt::PseudoObjectExprClass:
348
45
    return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
349
45
                        TU, RegionOfInterest);
350
351
1.75k
  case Stmt::CompoundStmtClass:
352
1.75k
    K = CXCursor_CompoundStmt;
353
1.75k
    break;
354
355
6
  case Stmt::NullStmtClass:
356
6
    K = CXCursor_NullStmt;
357
6
    break;
358
359
5
  case Stmt::LabelStmtClass:
360
5
    K = CXCursor_LabelStmt;
361
5
    break;
362
363
0
  case Stmt::AttributedStmtClass:
364
0
    K = CXCursor_UnexposedStmt;
365
0
    break;
366
367
1.36k
  case Stmt::DeclStmtClass:
368
1.36k
    K = CXCursor_DeclStmt;
369
1.36k
    break;
370
371
0
  case Stmt::CapturedStmtClass:
372
0
    K = CXCursor_UnexposedStmt;
373
0
    break;
374
375
1.46k
  case Stmt::IntegerLiteralClass:
376
1.46k
    K = CXCursor_IntegerLiteral;
377
1.46k
    break;
378
379
0
  case Stmt::FixedPointLiteralClass:
380
0
    K = CXCursor_FixedPointLiteral;
381
0
    break;
382
383
14
  case Stmt::FloatingLiteralClass:
384
14
    K = CXCursor_FloatingLiteral;
385
14
    break;
386
387
0
  case Stmt::ImaginaryLiteralClass:
388
0
    K = CXCursor_ImaginaryLiteral;
389
0
    break;
390
391
346
  case Stmt::StringLiteralClass:
392
346
    K = CXCursor_StringLiteral;
393
346
    break;
394
395
0
  case Stmt::CharacterLiteralClass:
396
0
    K = CXCursor_CharacterLiteral;
397
0
    break;
398
399
148
  case Stmt::ConstantExprClass:
400
148
    return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
401
148
                        RegionOfInterest);
402
403
428
  case Stmt::ParenExprClass:
404
428
    K = CXCursor_ParenExpr;
405
428
    break;
406
407
75
  case Stmt::UnaryOperatorClass:
408
75
    K = CXCursor_UnaryOperator;
409
75
    break;
410
411
17
  case Stmt::UnaryExprOrTypeTraitExprClass:
412
17
  case Stmt::CXXNoexceptExprClass:
413
17
    K = CXCursor_UnaryExpr;
414
17
    break;
415
416
0
  case Stmt::MSPropertySubscriptExprClass:
417
12
  case Stmt::ArraySubscriptExprClass:
418
12
    K = CXCursor_ArraySubscriptExpr;
419
12
    break;
420
421
0
  case Stmt::MatrixSubscriptExprClass:
422
    // TODO: add support for MatrixSubscriptExpr.
423
0
    K = CXCursor_UnexposedExpr;
424
0
    break;
425
426
0
  case Stmt::OMPArraySectionExprClass:
427
0
    K = CXCursor_OMPArraySectionExpr;
428
0
    break;
429
430
0
  case Stmt::OMPArrayShapingExprClass:
431
0
    K = CXCursor_OMPArrayShapingExpr;
432
0
    break;
433
434
0
  case Stmt::OMPIteratorExprClass:
435
0
    K = CXCursor_OMPIteratorExpr;
436
0
    break;
437
438
1.92k
  case Stmt::BinaryOperatorClass:
439
1.92k
    K = CXCursor_BinaryOperator;
440
1.92k
    break;
441
442
2
  case Stmt::CompoundAssignOperatorClass:
443
2
    K = CXCursor_CompoundAssignOperator;
444
2
    break;
445
446
6
  case Stmt::ConditionalOperatorClass:
447
6
    K = CXCursor_ConditionalOperator;
448
6
    break;
449
450
136
  case Stmt::CStyleCastExprClass:
451
136
    K = CXCursor_CStyleCastExpr;
452
136
    break;
453
454
8
  case Stmt::CompoundLiteralExprClass:
455
8
    K = CXCursor_CompoundLiteralExpr;
456
8
    break;
457
458
34
  case Stmt::InitListExprClass:
459
34
    K = CXCursor_InitListExpr;
460
34
    break;
461
462
2
  case Stmt::AddrLabelExprClass:
463
2
    K = CXCursor_AddrLabelExpr;
464
2
    break;
465
466
0
  case Stmt::StmtExprClass:
467
0
    K = CXCursor_StmtExpr;
468
0
    break;
469
470
0
  case Stmt::GenericSelectionExprClass:
471
0
    K = CXCursor_GenericSelectionExpr;
472
0
    break;
473
474
0
  case Stmt::GNUNullExprClass:
475
0
    K = CXCursor_GNUNullExpr;
476
0
    break;
477
478
2
  case Stmt::CXXStaticCastExprClass:
479
2
    K = CXCursor_CXXStaticCastExpr;
480
2
    break;
481
482
0
  case Stmt::CXXDynamicCastExprClass:
483
0
    K = CXCursor_CXXDynamicCastExpr;
484
0
    break;
485
486
2
  case Stmt::CXXReinterpretCastExprClass:
487
2
    K = CXCursor_CXXReinterpretCastExpr;
488
2
    break;
489
490
0
  case Stmt::CXXConstCastExprClass:
491
0
    K = CXCursor_CXXConstCastExpr;
492
0
    break;
493
494
24
  case Stmt::CXXFunctionalCastExprClass:
495
24
    K = CXCursor_CXXFunctionalCastExpr;
496
24
    break;
497
498
2
  case Stmt::CXXAddrspaceCastExprClass:
499
2
    K = CXCursor_CXXAddrspaceCastExpr;
500
2
    break;
501
502
4
  case Stmt::CXXTypeidExprClass:
503
4
    K = CXCursor_CXXTypeidExpr;
504
4
    break;
505
506
10
  case Stmt::CXXBoolLiteralExprClass:
507
10
    K = CXCursor_CXXBoolLiteralExpr;
508
10
    break;
509
510
0
  case Stmt::CXXNullPtrLiteralExprClass:
511
0
    K = CXCursor_CXXNullPtrLiteralExpr;
512
0
    break;
513
514
22
  case Stmt::CXXThisExprClass:
515
22
    K = CXCursor_CXXThisExpr;
516
22
    break;
517
518
0
  case Stmt::CXXThrowExprClass:
519
0
    K = CXCursor_CXXThrowExpr;
520
0
    break;
521
522
10
  case Stmt::CXXNewExprClass:
523
10
    K = CXCursor_CXXNewExpr;
524
10
    break;
525
526
0
  case Stmt::CXXDeleteExprClass:
527
0
    K = CXCursor_CXXDeleteExpr;
528
0
    break;
529
530
4
  case Stmt::ObjCStringLiteralClass:
531
4
    K = CXCursor_ObjCStringLiteral;
532
4
    break;
533
534
2
  case Stmt::ObjCEncodeExprClass:
535
2
    K = CXCursor_ObjCEncodeExpr;
536
2
    break;
537
538
0
  case Stmt::ObjCSelectorExprClass:
539
0
    K = CXCursor_ObjCSelectorExpr;
540
0
    break;
541
542
3
  case Stmt::ObjCProtocolExprClass:
543
3
    K = CXCursor_ObjCProtocolExpr;
544
3
    break;
545
546
0
  case Stmt::ObjCBoolLiteralExprClass:
547
0
    K = CXCursor_ObjCBoolLiteralExpr;
548
0
    break;
549
550
0
  case Stmt::ObjCAvailabilityCheckExprClass:
551
0
    K = CXCursor_ObjCAvailabilityCheckExpr;
552
0
    break;
553
554
0
  case Stmt::ObjCBridgedCastExprClass:
555
0
    K = CXCursor_ObjCBridgedCastExpr;
556
0
    break;
557
558
12
  case Stmt::BlockExprClass:
559
12
    K = CXCursor_BlockExpr;
560
12
    break;
561
562
0
  case Stmt::PackExpansionExprClass:
563
0
    K = CXCursor_PackExpansionExpr;
564
0
    break;
565
566
4
  case Stmt::SizeOfPackExprClass:
567
4
    K = CXCursor_SizeOfPackExpr;
568
4
    break;
569
570
9.15k
  case Stmt::DeclRefExprClass:
571
9.15k
    if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
572
9.15k
            cast<DeclRefExpr>(S)->getDecl())) {
573
26
      if (const ObjCMethodDecl *MD =
574
26
              dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
575
26
        if (MD->getSelfDecl() == IPD) {
576
24
          K = CXCursor_ObjCSelfExpr;
577
24
          break;
578
24
        }
579
26
      }
580
26
    }
581
582
9.12k
    K = CXCursor_DeclRefExpr;
583
9.12k
    break;
584
585
8
  case Stmt::DependentScopeDeclRefExprClass:
586
8
  case Stmt::SubstNonTypeTemplateParmExprClass:
587
8
  case Stmt::SubstNonTypeTemplateParmPackExprClass:
588
8
  case Stmt::FunctionParmPackExprClass:
589
38
  case Stmt::UnresolvedLookupExprClass:
590
38
  case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
591
38
    K = CXCursor_DeclRefExpr;
592
38
    break;
593
594
6
  case Stmt::CXXDependentScopeMemberExprClass:
595
10
  case Stmt::CXXPseudoDestructorExprClass:
596
1.07k
  case Stmt::MemberExprClass:
597
1.07k
  case Stmt::MSPropertyRefExprClass:
598
1.07k
  case Stmt::ObjCIsaExprClass:
599
1.07k
  case Stmt::ObjCIvarRefExprClass:
600
1.08k
  case Stmt::ObjCPropertyRefExprClass:
601
1.09k
  case Stmt::UnresolvedMemberExprClass:
602
1.09k
    K = CXCursor_MemberRefExpr;
603
1.09k
    break;
604
605
390
  case Stmt::CallExprClass:
606
10.2k
  case Stmt::CXXOperatorCallExprClass:
607
10.6k
  case Stmt::CXXMemberCallExprClass:
608
10.6k
  case Stmt::CUDAKernelCallExprClass:
609
10.8k
  case Stmt::CXXConstructExprClass:
610
10.8k
  case Stmt::CXXInheritedCtorInitExprClass:
611
10.9k
  case Stmt::CXXTemporaryObjectExprClass:
612
10.9k
  case Stmt::CXXUnresolvedConstructExprClass:
613
10.9k
  case Stmt::UserDefinedLiteralClass:
614
10.9k
    K = CXCursor_CallExpr;
615
10.9k
    break;
616
617
8
  case Stmt::LambdaExprClass:
618
8
    K = CXCursor_LambdaExpr;
619
8
    break;
620
621
683
  case Stmt::ObjCMessageExprClass: {
622
683
    K = CXCursor_ObjCMessageExpr;
623
683
    int SelectorIdIndex = -1;
624
    // Check if cursor points to a selector id.
625
683
    if (RegionOfInterest.isValid() &&
626
683
        
RegionOfInterest.getBegin() == RegionOfInterest.getEnd()661
) {
627
597
      SmallVector<SourceLocation, 16> SelLocs;
628
597
      cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
629
597
      SmallVectorImpl<SourceLocation>::iterator I =
630
597
          llvm::find(SelLocs, RegionOfInterest.getBegin());
631
597
      if (I != SelLocs.end())
632
122
        SelectorIdIndex = I - SelLocs.begin();
633
597
    }
634
683
    CXCursor C = {K, 0, {Parent, S, TU}};
635
683
    return getSelectorIdentifierCursor(SelectorIdIndex, C);
636
10.9k
  }
637
638
13
  case Stmt::ConceptSpecializationExprClass:
639
13
    K = CXCursor_ConceptSpecializationExpr;
640
13
    break;
641
642
8
  case Stmt::RequiresExprClass:
643
8
    K = CXCursor_RequiresExpr;
644
8
    break;
645
646
4
  case Stmt::MSDependentExistsStmtClass:
647
4
    K = CXCursor_UnexposedStmt;
648
4
    break;
649
0
  case Stmt::OMPCanonicalLoopClass:
650
0
    K = CXCursor_OMPCanonicalLoop;
651
0
    break;
652
0
  case Stmt::OMPMetaDirectiveClass:
653
0
    K = CXCursor_OMPMetaDirective;
654
0
    break;
655
0
  case Stmt::OMPParallelDirectiveClass:
656
0
    K = CXCursor_OMPParallelDirective;
657
0
    break;
658
0
  case Stmt::OMPSimdDirectiveClass:
659
0
    K = CXCursor_OMPSimdDirective;
660
0
    break;
661
2
  case Stmt::OMPTileDirectiveClass:
662
2
    K = CXCursor_OMPTileDirective;
663
2
    break;
664
0
  case Stmt::OMPUnrollDirectiveClass:
665
0
    K = CXCursor_OMPUnrollDirective;
666
0
    break;
667
0
  case Stmt::OMPForDirectiveClass:
668
0
    K = CXCursor_OMPForDirective;
669
0
    break;
670
0
  case Stmt::OMPForSimdDirectiveClass:
671
0
    K = CXCursor_OMPForSimdDirective;
672
0
    break;
673
0
  case Stmt::OMPSectionsDirectiveClass:
674
0
    K = CXCursor_OMPSectionsDirective;
675
0
    break;
676
0
  case Stmt::OMPSectionDirectiveClass:
677
0
    K = CXCursor_OMPSectionDirective;
678
0
    break;
679
0
  case Stmt::OMPSingleDirectiveClass:
680
0
    K = CXCursor_OMPSingleDirective;
681
0
    break;
682
0
  case Stmt::OMPMasterDirectiveClass:
683
0
    K = CXCursor_OMPMasterDirective;
684
0
    break;
685
0
  case Stmt::OMPCriticalDirectiveClass:
686
0
    K = CXCursor_OMPCriticalDirective;
687
0
    break;
688
0
  case Stmt::OMPParallelForDirectiveClass:
689
0
    K = CXCursor_OMPParallelForDirective;
690
0
    break;
691
0
  case Stmt::OMPParallelForSimdDirectiveClass:
692
0
    K = CXCursor_OMPParallelForSimdDirective;
693
0
    break;
694
0
  case Stmt::OMPParallelMasterDirectiveClass:
695
0
    K = CXCursor_OMPParallelMasterDirective;
696
0
    break;
697
0
  case Stmt::OMPParallelMaskedDirectiveClass:
698
0
    K = CXCursor_OMPParallelMaskedDirective;
699
0
    break;
700
0
  case Stmt::OMPParallelSectionsDirectiveClass:
701
0
    K = CXCursor_OMPParallelSectionsDirective;
702
0
    break;
703
0
  case Stmt::OMPTaskDirectiveClass:
704
0
    K = CXCursor_OMPTaskDirective;
705
0
    break;
706
0
  case Stmt::OMPTaskyieldDirectiveClass:
707
0
    K = CXCursor_OMPTaskyieldDirective;
708
0
    break;
709
0
  case Stmt::OMPBarrierDirectiveClass:
710
0
    K = CXCursor_OMPBarrierDirective;
711
0
    break;
712
0
  case Stmt::OMPTaskwaitDirectiveClass:
713
0
    K = CXCursor_OMPTaskwaitDirective;
714
0
    break;
715
0
  case Stmt::OMPTaskgroupDirectiveClass:
716
0
    K = CXCursor_OMPTaskgroupDirective;
717
0
    break;
718
0
  case Stmt::OMPFlushDirectiveClass:
719
0
    K = CXCursor_OMPFlushDirective;
720
0
    break;
721
0
  case Stmt::OMPDepobjDirectiveClass:
722
0
    K = CXCursor_OMPDepobjDirective;
723
0
    break;
724
0
  case Stmt::OMPScanDirectiveClass:
725
0
    K = CXCursor_OMPScanDirective;
726
0
    break;
727
0
  case Stmt::OMPOrderedDirectiveClass:
728
0
    K = CXCursor_OMPOrderedDirective;
729
0
    break;
730
0
  case Stmt::OMPAtomicDirectiveClass:
731
0
    K = CXCursor_OMPAtomicDirective;
732
0
    break;
733
0
  case Stmt::OMPTargetDirectiveClass:
734
0
    K = CXCursor_OMPTargetDirective;
735
0
    break;
736
0
  case Stmt::OMPTargetDataDirectiveClass:
737
0
    K = CXCursor_OMPTargetDataDirective;
738
0
    break;
739
0
  case Stmt::OMPTargetEnterDataDirectiveClass:
740
0
    K = CXCursor_OMPTargetEnterDataDirective;
741
0
    break;
742
0
  case Stmt::OMPTargetExitDataDirectiveClass:
743
0
    K = CXCursor_OMPTargetExitDataDirective;
744
0
    break;
745
0
  case Stmt::OMPTargetParallelDirectiveClass:
746
0
    K = CXCursor_OMPTargetParallelDirective;
747
0
    break;
748
0
  case Stmt::OMPTargetParallelForDirectiveClass:
749
0
    K = CXCursor_OMPTargetParallelForDirective;
750
0
    break;
751
0
  case Stmt::OMPTargetUpdateDirectiveClass:
752
0
    K = CXCursor_OMPTargetUpdateDirective;
753
0
    break;
754
0
  case Stmt::OMPTeamsDirectiveClass:
755
0
    K = CXCursor_OMPTeamsDirective;
756
0
    break;
757
0
  case Stmt::OMPCancellationPointDirectiveClass:
758
0
    K = CXCursor_OMPCancellationPointDirective;
759
0
    break;
760
0
  case Stmt::OMPCancelDirectiveClass:
761
0
    K = CXCursor_OMPCancelDirective;
762
0
    break;
763
0
  case Stmt::OMPTaskLoopDirectiveClass:
764
0
    K = CXCursor_OMPTaskLoopDirective;
765
0
    break;
766
0
  case Stmt::OMPTaskLoopSimdDirectiveClass:
767
0
    K = CXCursor_OMPTaskLoopSimdDirective;
768
0
    break;
769
0
  case Stmt::OMPMasterTaskLoopDirectiveClass:
770
0
    K = CXCursor_OMPMasterTaskLoopDirective;
771
0
    break;
772
0
  case Stmt::OMPMaskedTaskLoopDirectiveClass:
773
0
    K = CXCursor_OMPMaskedTaskLoopDirective;
774
0
    break;
775
0
  case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
776
0
    K = CXCursor_OMPMasterTaskLoopSimdDirective;
777
0
    break;
778
0
  case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
779
0
    K = CXCursor_OMPMaskedTaskLoopSimdDirective;
780
0
    break;
781
0
  case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
782
0
    K = CXCursor_OMPParallelMasterTaskLoopDirective;
783
0
    break;
784
0
  case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
785
0
    K = CXCursor_OMPParallelMaskedTaskLoopDirective;
786
0
    break;
787
0
  case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
788
0
    K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
789
0
    break;
790
0
  case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
791
0
    K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
792
0
    break;
793
0
  case Stmt::OMPDistributeDirectiveClass:
794
0
    K = CXCursor_OMPDistributeDirective;
795
0
    break;
796
0
  case Stmt::OMPDistributeParallelForDirectiveClass:
797
0
    K = CXCursor_OMPDistributeParallelForDirective;
798
0
    break;
799
0
  case Stmt::OMPDistributeParallelForSimdDirectiveClass:
800
0
    K = CXCursor_OMPDistributeParallelForSimdDirective;
801
0
    break;
802
0
  case Stmt::OMPDistributeSimdDirectiveClass:
803
0
    K = CXCursor_OMPDistributeSimdDirective;
804
0
    break;
805
0
  case Stmt::OMPTargetParallelForSimdDirectiveClass:
806
0
    K = CXCursor_OMPTargetParallelForSimdDirective;
807
0
    break;
808
0
  case Stmt::OMPTargetSimdDirectiveClass:
809
0
    K = CXCursor_OMPTargetSimdDirective;
810
0
    break;
811
0
  case Stmt::OMPTeamsDistributeDirectiveClass:
812
0
    K = CXCursor_OMPTeamsDistributeDirective;
813
0
    break;
814
0
  case Stmt::OMPTeamsDistributeSimdDirectiveClass:
815
0
    K = CXCursor_OMPTeamsDistributeSimdDirective;
816
0
    break;
817
0
  case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
818
0
    K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
819
0
    break;
820
0
  case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
821
0
    K = CXCursor_OMPTeamsDistributeParallelForDirective;
822
0
    break;
823
0
  case Stmt::OMPTargetTeamsDirectiveClass:
824
0
    K = CXCursor_OMPTargetTeamsDirective;
825
0
    break;
826
0
  case Stmt::OMPTargetTeamsDistributeDirectiveClass:
827
0
    K = CXCursor_OMPTargetTeamsDistributeDirective;
828
0
    break;
829
0
  case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
830
0
    K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
831
0
    break;
832
0
  case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
833
0
    K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
834
0
    break;
835
0
  case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
836
0
    K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
837
0
    break;
838
0
  case Stmt::OMPInteropDirectiveClass:
839
0
    K = CXCursor_OMPInteropDirective;
840
0
    break;
841
0
  case Stmt::OMPDispatchDirectiveClass:
842
0
    K = CXCursor_OMPDispatchDirective;
843
0
    break;
844
0
  case Stmt::OMPMaskedDirectiveClass:
845
0
    K = CXCursor_OMPMaskedDirective;
846
0
    break;
847
0
  case Stmt::OMPGenericLoopDirectiveClass:
848
0
    K = CXCursor_OMPGenericLoopDirective;
849
0
    break;
850
0
  case Stmt::OMPTeamsGenericLoopDirectiveClass:
851
0
    K = CXCursor_OMPTeamsGenericLoopDirective;
852
0
    break;
853
0
  case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
854
0
    K = CXCursor_OMPTargetTeamsGenericLoopDirective;
855
0
    break;
856
0
  case Stmt::OMPParallelGenericLoopDirectiveClass:
857
0
    K = CXCursor_OMPParallelGenericLoopDirective;
858
0
    break;
859
0
  case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
860
0
    K = CXCursor_OMPTargetParallelGenericLoopDirective;
861
0
    break;
862
0
  case Stmt::BuiltinBitCastExprClass:
863
0
    K = CXCursor_BuiltinBitCastExpr;
864
44.6k
  }
865
866
43.6k
  CXCursor C = {K, 0, {Parent, S, TU}};
867
43.6k
  return C;
868
44.6k
}
869
870
CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
871
                                               SourceLocation Loc,
872
137
                                               CXTranslationUnit TU) {
873
137
  assert(Super && TU && "Invalid arguments!");
874
0
  void *RawLoc = Loc.getPtrEncoding();
875
137
  CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
876
137
  return C;
877
137
}
878
879
std::pair<const ObjCInterfaceDecl *, SourceLocation>
880
299
cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
881
299
  assert(C.kind == CXCursor_ObjCSuperClassRef);
882
0
  return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
883
299
                        SourceLocation::getFromPtrEncoding(C.data[1]));
884
299
}
885
886
CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
887
                                             SourceLocation Loc,
888
192
                                             CXTranslationUnit TU) {
889
192
  assert(Proto && TU && "Invalid arguments!");
890
0
  void *RawLoc = Loc.getPtrEncoding();
891
192
  CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
892
192
  return C;
893
192
}
894
895
std::pair<const ObjCProtocolDecl *, SourceLocation>
896
378
cxcursor::getCursorObjCProtocolRef(CXCursor C) {
897
378
  assert(C.kind == CXCursor_ObjCProtocolRef);
898
0
  return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
899
378
                        SourceLocation::getFromPtrEncoding(C.data[1]));
900
378
}
901
902
CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
903
                                          SourceLocation Loc,
904
559
                                          CXTranslationUnit TU) {
905
  // 'Class' can be null for invalid code.
906
559
  if (!Class)
907
1
    return MakeCXCursorInvalid(CXCursor_InvalidCode);
908
558
  assert(TU && "Invalid arguments!");
909
0
  void *RawLoc = Loc.getPtrEncoding();
910
558
  CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
911
558
  return C;
912
559
}
913
914
std::pair<const ObjCInterfaceDecl *, SourceLocation>
915
1.85k
cxcursor::getCursorObjCClassRef(CXCursor C) {
916
1.85k
  assert(C.kind == CXCursor_ObjCClassRef);
917
0
  return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
918
1.85k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
919
1.85k
}
920
921
CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
922
1.35k
                                     CXTranslationUnit TU) {
923
1.35k
  assert(Type && TU && "Invalid arguments!");
924
0
  void *RawLoc = Loc.getPtrEncoding();
925
1.35k
  CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
926
1.35k
  return C;
927
1.35k
}
928
929
std::pair<const TypeDecl *, SourceLocation>
930
7.31k
cxcursor::getCursorTypeRef(CXCursor C) {
931
7.31k
  assert(C.kind == CXCursor_TypeRef);
932
0
  return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
933
7.31k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
934
7.31k
}
935
936
CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
937
                                         SourceLocation Loc,
938
175
                                         CXTranslationUnit TU) {
939
175
  assert(Template && TU && "Invalid arguments!");
940
0
  void *RawLoc = Loc.getPtrEncoding();
941
175
  CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
942
175
  return C;
943
175
}
944
945
std::pair<const TemplateDecl *, SourceLocation>
946
1.13k
cxcursor::getCursorTemplateRef(CXCursor C) {
947
1.13k
  assert(C.kind == CXCursor_TemplateRef);
948
0
  return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
949
1.13k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
950
1.13k
}
951
952
CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
953
                                          SourceLocation Loc,
954
179
                                          CXTranslationUnit TU) {
955
956
179
  assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
957
179
         "Invalid arguments!");
958
0
  void *RawLoc = Loc.getPtrEncoding();
959
179
  CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
960
179
  return C;
961
179
}
962
963
std::pair<const NamedDecl *, SourceLocation>
964
1.01k
cxcursor::getCursorNamespaceRef(CXCursor C) {
965
1.01k
  assert(C.kind == CXCursor_NamespaceRef);
966
0
  return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
967
1.01k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
968
1.01k
}
969
970
CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
971
13
                                         CXTranslationUnit TU) {
972
973
13
  assert(Var && TU && "Invalid arguments!");
974
0
  void *RawLoc = Loc.getPtrEncoding();
975
13
  CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
976
13
  return C;
977
13
}
978
979
std::pair<const VarDecl *, SourceLocation>
980
82
cxcursor::getCursorVariableRef(CXCursor C) {
981
82
  assert(C.kind == CXCursor_VariableRef);
982
0
  return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
983
82
                        SourceLocation::getFromPtrEncoding(C.data[1]));
984
82
}
985
986
CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
987
                                       SourceLocation Loc,
988
50
                                       CXTranslationUnit TU) {
989
990
50
  assert(Field && TU && "Invalid arguments!");
991
0
  void *RawLoc = Loc.getPtrEncoding();
992
50
  CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
993
50
  return C;
994
50
}
995
996
std::pair<const FieldDecl *, SourceLocation>
997
348
cxcursor::getCursorMemberRef(CXCursor C) {
998
348
  assert(C.kind == CXCursor_MemberRef);
999
0
  return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
1000
348
                        SourceLocation::getFromPtrEncoding(C.data[1]));
1001
348
}
1002
1003
CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
1004
128
                                              CXTranslationUnit TU) {
1005
128
  CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
1006
128
  return C;
1007
128
}
1008
1009
1.20k
const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
1010
1.20k
  assert(C.kind == CXCursor_CXXBaseSpecifier);
1011
0
  return static_cast<const CXXBaseSpecifier *>(C.data[0]);
1012
1.20k
}
1013
1014
CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
1015
57
                                                    CXTranslationUnit TU) {
1016
57
  CXCursor C = {
1017
57
      CXCursor_PreprocessingDirective,
1018
57
      0,
1019
57
      {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
1020
57
  return C;
1021
57
}
1022
1023
300
SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
1024
300
  assert(C.kind == CXCursor_PreprocessingDirective);
1025
0
  SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
1026
300
                    SourceLocation::getFromPtrEncoding(C.data[1]));
1027
300
  ASTUnit *TU = getCursorASTUnit(C);
1028
300
  return TU->mapRangeFromPreamble(Range);
1029
300
}
1030
1031
CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
1032
74.4k
                                             CXTranslationUnit TU) {
1033
74.4k
  CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
1034
74.4k
  return C;
1035
74.4k
}
1036
1037
485k
const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
1038
485k
  assert(C.kind == CXCursor_MacroDefinition);
1039
0
  return static_cast<const MacroDefinitionRecord *>(C.data[0]);
1040
485k
}
1041
1042
CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
1043
5.91k
                                            CXTranslationUnit TU) {
1044
5.91k
  CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
1045
5.91k
  return C;
1046
5.91k
}
1047
1048
CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1049
                                            SourceLocation Loc,
1050
16
                                            CXTranslationUnit TU) {
1051
16
  assert(Loc.isValid());
1052
0
  CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
1053
16
  return C;
1054
16
}
1055
1056
254
const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1057
254
  if (isPseudo())
1058
12
    return getAsMacroDefinition()->getName();
1059
242
  return getAsMacroExpansion()->getName();
1060
254
}
1061
const MacroDefinitionRecord *
1062
219
cxcursor::MacroExpansionCursor::getDefinition() const {
1063
219
  if (isPseudo())
1064
8
    return getAsMacroDefinition();
1065
211
  return getAsMacroExpansion()->getDefinition();
1066
219
}
1067
6.86k
SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1068
6.86k
  if (isPseudo())
1069
48
    return getPseudoLoc();
1070
6.82k
  return getAsMacroExpansion()->getSourceRange();
1071
6.86k
}
1072
1073
CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1074
323
                                                CXTranslationUnit TU) {
1075
323
  CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
1076
323
  return C;
1077
323
}
1078
1079
1.86k
const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1080
1.86k
  assert(C.kind == CXCursor_InclusionDirective);
1081
0
  return static_cast<const InclusionDirective *>(C.data[0]);
1082
1.86k
}
1083
1084
CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1085
2
                                      CXTranslationUnit TU) {
1086
1087
2
  assert(Label && TU && "Invalid arguments!");
1088
0
  void *RawLoc = Loc.getPtrEncoding();
1089
2
  CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
1090
2
  return C;
1091
2
}
1092
1093
std::pair<const LabelStmt *, SourceLocation>
1094
16
cxcursor::getCursorLabelRef(CXCursor C) {
1095
16
  assert(C.kind == CXCursor_LabelRef);
1096
0
  return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
1097
16
                        SourceLocation::getFromPtrEncoding(C.data[1]));
1098
16
}
1099
1100
CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1101
35
                                               CXTranslationUnit TU) {
1102
35
  assert(E && TU && "Invalid arguments!");
1103
0
  OverloadedDeclRefStorage Storage(E);
1104
35
  void *RawLoc = E->getNameLoc().getPtrEncoding();
1105
35
  CXCursor C = {
1106
35
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1107
35
  return C;
1108
35
}
1109
1110
CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1111
                                               SourceLocation Loc,
1112
33
                                               CXTranslationUnit TU) {
1113
33
  assert(D && TU && "Invalid arguments!");
1114
0
  void *RawLoc = Loc.getPtrEncoding();
1115
33
  OverloadedDeclRefStorage Storage(D);
1116
33
  CXCursor C = {
1117
33
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1118
33
  return C;
1119
33
}
1120
1121
CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1122
                                               SourceLocation Loc,
1123
0
                                               CXTranslationUnit TU) {
1124
0
  assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1125
0
  void *RawLoc = Loc.getPtrEncoding();
1126
0
  OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1127
0
  CXCursor C = {
1128
0
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1129
0
  return C;
1130
0
}
1131
1132
std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
1133
358
cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1134
358
  assert(C.kind == CXCursor_OverloadedDeclRef);
1135
0
  return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1136
358
                            const_cast<void *>(C.data[0])),
1137
358
                        SourceLocation::getFromPtrEncoding(C.data[1]));
1138
358
}
1139
1140
305k
const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1141
305k
  return static_cast<const Decl *>(Cursor.data[0]);
1142
305k
}
1143
1144
196k
const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1145
196k
  return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
1146
196k
}
1147
1148
204k
const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1149
204k
  if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1150
204k
      Cursor.kind == CXCursor_ObjCProtocolRef ||
1151
204k
      Cursor.kind == CXCursor_ObjCClassRef)
1152
0
    return nullptr;
1153
1154
204k
  return static_cast<const Stmt *>(Cursor.data[1]);
1155
204k
}
1156
1157
1.05k
const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1158
1.05k
  return static_cast<const Attr *>(Cursor.data[1]);
1159
1.05k
}
1160
1161
524k
ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1162
524k
  return getCursorASTUnit(Cursor)->getASTContext();
1163
524k
}
1164
1165
774k
ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1166
774k
  CXTranslationUnit TU = getCursorTU(Cursor);
1167
774k
  if (!TU)
1168
0
    return nullptr;
1169
774k
  return cxtu::getASTUnit(TU);
1170
774k
}
1171
1172
1.10M
CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1173
1.10M
  return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1174
1.10M
}
1175
1176
void cxcursor::getOverriddenCursors(CXCursor cursor,
1177
6.98k
                                    SmallVectorImpl<CXCursor> &overridden) {
1178
6.98k
  assert(clang_isDeclaration(cursor.kind));
1179
0
  const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
1180
6.98k
  if (!D)
1181
165
    return;
1182
1183
6.81k
  CXTranslationUnit TU = getCursorTU(cursor);
1184
6.81k
  SmallVector<const NamedDecl *, 8> OverDecls;
1185
6.81k
  D->getASTContext().getOverriddenMethods(D, OverDecls);
1186
1187
6.81k
  for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1188
6.81k
                                                    E = OverDecls.end();
1189
6.89k
       I != E; 
++I76
) {
1190
76
    overridden.push_back(MakeCXCursor(*I, TU));
1191
76
  }
1192
6.81k
}
1193
1194
std::pair<int, SourceLocation>
1195
15.3k
cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1196
15.3k
  if (cursor.kind == CXCursor_ObjCMessageExpr) {
1197
159
    if (cursor.xdata != -1)
1198
76
      return std::make_pair(cursor.xdata,
1199
76
                            cast<ObjCMessageExpr>(getCursorExpr(cursor))
1200
76
                                ->getSelectorLoc(cursor.xdata));
1201
15.1k
  } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1202
15.1k
             cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1203
44
    if (cursor.xdata != -1)
1204
44
      return std::make_pair(cursor.xdata,
1205
44
                            cast<ObjCMethodDecl>(getCursorDecl(cursor))
1206
44
                                ->getSelectorLoc(cursor.xdata));
1207
44
  }
1208
1209
15.2k
  return std::make_pair(-1, SourceLocation());
1210
15.3k
}
1211
1212
15.7k
CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1213
15.7k
  CXCursor newCursor = cursor;
1214
1215
15.7k
  if (cursor.kind == CXCursor_ObjCMessageExpr) {
1216
691
    if (SelIdx == -1 ||
1217
691
        unsigned(SelIdx) >=
1218
130
            cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1219
561
      newCursor.xdata = -1;
1220
130
    else
1221
130
      newCursor.xdata = SelIdx;
1222
15.0k
  } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1223
15.0k
             
cursor.kind == CXCursor_ObjCInstanceMethodDecl15.0k
) {
1224
121
    if (SelIdx == -1 ||
1225
121
        unsigned(SelIdx) >=
1226
38
            cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1227
83
      newCursor.xdata = -1;
1228
38
    else
1229
38
      newCursor.xdata = SelIdx;
1230
121
  }
1231
1232
15.7k
  return newCursor;
1233
15.7k
}
1234
1235
31
CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1236
31
  if (cursor.kind != CXCursor_CallExpr)
1237
28
    return cursor;
1238
1239
3
  if (cursor.xdata == 0)
1240
0
    return cursor;
1241
1242
3
  const Expr *E = getCursorExpr(cursor);
1243
3
  TypeSourceInfo *Type = nullptr;
1244
3
  if (const CXXUnresolvedConstructExpr *UnCtor =
1245
3
          dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1246
0
    Type = UnCtor->getTypeSourceInfo();
1247
3
  } else if (const CXXTemporaryObjectExpr *Tmp =
1248
3
                 dyn_cast<CXXTemporaryObjectExpr>(E)) {
1249
3
    Type = Tmp->getTypeSourceInfo();
1250
3
  }
1251
1252
3
  if (!Type)
1253
0
    return cursor;
1254
1255
3
  CXTranslationUnit TU = getCursorTU(cursor);
1256
3
  QualType Ty = Type->getType();
1257
3
  TypeLoc TL = Type->getTypeLoc();
1258
3
  SourceLocation Loc = TL.getBeginLoc();
1259
1260
3
  if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1261
3
    Ty = ElabT->getNamedType();
1262
3
    ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1263
3
    Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1264
3
  }
1265
1266
3
  if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1267
1
    return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1268
2
  if (const TagType *Tag = Ty->getAs<TagType>())
1269
2
    return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1270
0
  if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1271
0
    return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1272
1273
0
  return cursor;
1274
0
}
1275
1276
223k
bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1277
223k
  return X.kind == Y.kind && 
X.data[0] == Y.data[0]181k
&&
X.data[1] == Y.data[1]181k
&&
1278
223k
         
X.data[2] == Y.data[2]181k
;
1279
223k
}
1280
1281
// FIXME: Remove once we can model DeclGroups and their appropriate ranges
1282
// properly in the ASTs.
1283
10.6k
bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1284
10.6k
  assert(clang_isDeclaration(C.kind));
1285
0
  return ((uintptr_t)(C.data[1])) != 0;
1286
10.6k
}
1287
1288
//===----------------------------------------------------------------------===//
1289
// libclang CXCursor APIs
1290
//===----------------------------------------------------------------------===//
1291
1292
1.41k
int clang_Cursor_isNull(CXCursor cursor) {
1293
1.41k
  return clang_equalCursors(cursor, clang_getNullCursor());
1294
1.41k
}
1295
1296
94.4k
CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1297
94.4k
  return getCursorTU(cursor);
1298
94.4k
}
1299
1300
6.08k
int clang_Cursor_getNumArguments(CXCursor C) {
1301
6.08k
  if (clang_isDeclaration(C.kind)) {
1302
406
    const Decl *D = cxcursor::getCursorDecl(C);
1303
406
    if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1304
8
      return MD->param_size();
1305
398
    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1306
63
      return FD->param_size();
1307
398
  }
1308
1309
6.01k
  if (clang_isExpression(C.kind)) {
1310
86
    const Expr *E = cxcursor::getCursorExpr(C);
1311
86
    if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1312
2
      return CE->getNumArgs();
1313
2
    }
1314
84
    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1315
5
      return CE->getNumArgs();
1316
5
    }
1317
84
  }
1318
1319
6.00k
  return -1;
1320
6.01k
}
1321
1322
67
CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1323
67
  if (clang_isDeclaration(C.kind)) {
1324
63
    const Decl *D = cxcursor::getCursorDecl(C);
1325
63
    if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1326
7
      if (i < MD->param_size())
1327
7
        return cxcursor::MakeCXCursor(MD->parameters()[i],
1328
7
                                      cxcursor::getCursorTU(C));
1329
56
    } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1330
56
      if (i < FD->param_size())
1331
56
        return cxcursor::MakeCXCursor(FD->parameters()[i],
1332
56
                                      cxcursor::getCursorTU(C));
1333
56
    }
1334
63
  }
1335
1336
4
  if (clang_isExpression(C.kind)) {
1337
4
    const Expr *E = cxcursor::getCursorExpr(C);
1338
4
    if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1339
2
      if (i < CE->getNumArgs()) {
1340
2
        return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1341
2
                                      cxcursor::getCursorTU(C));
1342
2
      }
1343
2
    }
1344
2
    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1345
2
      if (i < CE->getNumArgs()) {
1346
2
        return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1347
2
                                      cxcursor::getCursorTU(C));
1348
2
      }
1349
2
    }
1350
2
  }
1351
1352
0
  return clang_getNullCursor();
1353
4
}
1354
1355
12
int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1356
12
  if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1357
0
    return -1;
1358
0
  }
1359
1360
12
  const FunctionDecl *FD =
1361
12
      llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1362
12
  if (!FD) {
1363
0
    return -1;
1364
0
  }
1365
1366
12
  const FunctionTemplateSpecializationInfo *SpecInfo =
1367
12
      FD->getTemplateSpecializationInfo();
1368
12
  if (!SpecInfo) {
1369
0
    return -1;
1370
0
  }
1371
1372
12
  return SpecInfo->TemplateArguments->size();
1373
12
}
1374
1375
enum CXGetTemplateArgumentStatus {
1376
  /** The operation completed successfully */
1377
  CXGetTemplateArgumentStatus_Success = 0,
1378
1379
  /** The specified cursor did not represent a FunctionDecl. */
1380
  CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
1381
1382
  /** The specified cursor was not castable to a FunctionDecl. */
1383
  CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
1384
1385
  /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1386
  CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1387
1388
  /** An invalid (OOB) argument index was specified */
1389
  CXGetTemplateArgumentStatus_InvalidIndex = -4
1390
};
1391
1392
static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1393
36
                                            TemplateArgument *TA) {
1394
36
  if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1395
0
    return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
1396
0
  }
1397
1398
36
  const FunctionDecl *FD =
1399
36
      llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1400
36
  if (!FD) {
1401
0
    return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
1402
0
  }
1403
1404
36
  const FunctionTemplateSpecializationInfo *SpecInfo =
1405
36
      FD->getTemplateSpecializationInfo();
1406
36
  if (!SpecInfo) {
1407
0
    return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1408
0
  }
1409
1410
36
  if (I >= SpecInfo->TemplateArguments->size()) {
1411
0
    return CXGetTemplateArgumentStatus_InvalidIndex;
1412
0
  }
1413
1414
36
  *TA = SpecInfo->TemplateArguments->get(I);
1415
36
  return 0;
1416
36
}
1417
1418
enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1419
18
                                                                 unsigned I) {
1420
18
  TemplateArgument TA;
1421
18
  if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
1422
0
    return CXTemplateArgumentKind_Invalid;
1423
0
  }
1424
1425
18
  switch (TA.getKind()) {
1426
0
  case TemplateArgument::Null:
1427
0
    return CXTemplateArgumentKind_Null;
1428
15
  case TemplateArgument::Type:
1429
15
    return CXTemplateArgumentKind_Type;
1430
0
  case TemplateArgument::Declaration:
1431
0
    return CXTemplateArgumentKind_Declaration;
1432
0
  case TemplateArgument::NullPtr:
1433
0
    return CXTemplateArgumentKind_NullPtr;
1434
3
  case TemplateArgument::Integral:
1435
3
    return CXTemplateArgumentKind_Integral;
1436
0
  case TemplateArgument::Template:
1437
0
    return CXTemplateArgumentKind_Template;
1438
0
  case TemplateArgument::TemplateExpansion:
1439
0
    return CXTemplateArgumentKind_TemplateExpansion;
1440
0
  case TemplateArgument::Expression:
1441
0
    return CXTemplateArgumentKind_Expression;
1442
0
  case TemplateArgument::Pack:
1443
0
    return CXTemplateArgumentKind_Pack;
1444
18
  }
1445
1446
0
  return CXTemplateArgumentKind_Invalid;
1447
18
}
1448
1449
15
CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1450
15
  TemplateArgument TA;
1451
15
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1452
15
      CXGetTemplateArgumentStatus_Success) {
1453
0
    return cxtype::MakeCXType(QualType(), getCursorTU(C));
1454
0
  }
1455
1456
15
  if (TA.getKind() != TemplateArgument::Type) {
1457
0
    return cxtype::MakeCXType(QualType(), getCursorTU(C));
1458
0
  }
1459
1460
15
  return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
1461
15
}
1462
1463
3
long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1464
3
  TemplateArgument TA;
1465
3
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1466
3
      CXGetTemplateArgumentStatus_Success) {
1467
0
    assert(0 && "Unable to retrieve TemplateArgument");
1468
0
    return 0;
1469
0
  }
1470
1471
3
  if (TA.getKind() != TemplateArgument::Integral) {
1472
0
    assert(0 && "Passed template argument is not Integral");
1473
0
    return 0;
1474
0
  }
1475
1476
3
  return TA.getAsIntegral().getSExtValue();
1477
3
}
1478
1479
unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1480
0
                                                                 unsigned I) {
1481
0
  TemplateArgument TA;
1482
0
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1483
0
      CXGetTemplateArgumentStatus_Success) {
1484
0
    assert(0 && "Unable to retrieve TemplateArgument");
1485
0
    return 0;
1486
0
  }
1487
1488
0
  if (TA.getKind() != TemplateArgument::Integral) {
1489
0
    assert(0 && "Passed template argument is not Integral");
1490
0
    return 0;
1491
0
  }
1492
1493
0
  return TA.getAsIntegral().getZExtValue();
1494
0
}
1495
1496
//===----------------------------------------------------------------------===//
1497
// CXCursorSet.
1498
//===----------------------------------------------------------------------===//
1499
1500
typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1501
1502
0
static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1503
0
  return (CXCursorSet)setImpl;
1504
0
}
1505
0
static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1506
0
  return (CXCursorSet_Impl *)set;
1507
0
}
1508
namespace llvm {
1509
template <> struct DenseMapInfo<CXCursor> {
1510
public:
1511
0
  static inline CXCursor getEmptyKey() {
1512
0
    return MakeCXCursorInvalid(CXCursor_InvalidFile);
1513
0
  }
1514
0
  static inline CXCursor getTombstoneKey() {
1515
0
    return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1516
0
  }
1517
0
  static inline unsigned getHashValue(const CXCursor &cursor) {
1518
0
    return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1519
0
        getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1520
0
  }
1521
0
  static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1522
0
    return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1523
0
  }
1524
};
1525
} // namespace llvm
1526
1527
0
CXCursorSet clang_createCXCursorSet() {
1528
0
  return packCXCursorSet(new CXCursorSet_Impl());
1529
0
}
1530
1531
0
void clang_disposeCXCursorSet(CXCursorSet set) {
1532
0
  delete unpackCXCursorSet(set);
1533
0
}
1534
1535
0
unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1536
0
  CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1537
0
  if (!setImpl)
1538
0
    return 0;
1539
0
  return setImpl->find(cursor) != setImpl->end();
1540
0
}
1541
1542
0
unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1543
  // Do not insert invalid cursors into the set.
1544
0
  if (cursor.kind >= CXCursor_FirstInvalid &&
1545
0
      cursor.kind <= CXCursor_LastInvalid)
1546
0
    return 1;
1547
1548
0
  CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1549
0
  if (!setImpl)
1550
0
    return 1;
1551
0
  unsigned &entry = (*setImpl)[cursor];
1552
0
  unsigned flag = entry == 0 ? 1 : 0;
1553
0
  entry = 1;
1554
0
  return flag;
1555
0
}
1556
1557
231
CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1558
231
  enum CXCursorKind kind = clang_getCursorKind(cursor);
1559
231
  if (clang_isDeclaration(kind)) {
1560
111
    const Decl *decl = getCursorDecl(cursor);
1561
111
    if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1562
105
      ASTUnit *unit = getCursorASTUnit(cursor);
1563
105
      CodeCompletionResult Result(namedDecl, CCP_Declaration);
1564
105
      CodeCompletionString *String = Result.CreateCodeCompletionString(
1565
105
          unit->getASTContext(), unit->getPreprocessor(),
1566
105
          CodeCompletionContext::CCC_Other,
1567
105
          unit->getCodeCompletionTUInfo().getAllocator(),
1568
105
          unit->getCodeCompletionTUInfo(), true);
1569
105
      return String;
1570
105
    }
1571
120
  } else if (kind == CXCursor_MacroDefinition) {
1572
12
    const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
1573
12
    const IdentifierInfo *Macro = definition->getName();
1574
12
    ASTUnit *unit = getCursorASTUnit(cursor);
1575
12
    CodeCompletionResult Result(
1576
12
        Macro,
1577
12
        unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
1578
12
    CodeCompletionString *String = Result.CreateCodeCompletionString(
1579
12
        unit->getASTContext(), unit->getPreprocessor(),
1580
12
        CodeCompletionContext::CCC_Other,
1581
12
        unit->getCodeCompletionTUInfo().getAllocator(),
1582
12
        unit->getCodeCompletionTUInfo(), false);
1583
12
    return String;
1584
12
  }
1585
114
  return nullptr;
1586
231
}
1587
1588
namespace {
1589
struct OverridenCursorsPool {
1590
  typedef SmallVector<CXCursor, 2> CursorVec;
1591
  std::vector<CursorVec *> AllCursors;
1592
  std::vector<CursorVec *> AvailableCursors;
1593
1594
1.04k
  ~OverridenCursorsPool() {
1595
1.04k
    for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1596
1.04k
                                            E = AllCursors.end();
1597
1.37k
         I != E; 
++I324
) {
1598
324
      delete *I;
1599
324
    }
1600
1.04k
  }
1601
};
1602
} // namespace
1603
1604
1.05k
void *cxcursor::createOverridenCXCursorsPool() {
1605
1.05k
  return new OverridenCursorsPool();
1606
1.05k
}
1607
1608
1.04k
void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1609
1.04k
  delete static_cast<OverridenCursorsPool *>(pool);
1610
1.04k
}
1611
1612
void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1613
89.3k
                                unsigned *num_overridden) {
1614
89.3k
  if (overridden)
1615
89.3k
    *overridden = nullptr;
1616
89.3k
  if (num_overridden)
1617
89.3k
    *num_overridden = 0;
1618
1619
89.3k
  CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1620
1621
89.3k
  if (!overridden || !num_overridden || !TU)
1622
0
    return;
1623
1624
89.3k
  if (!clang_isDeclaration(cursor.kind))
1625
82.4k
    return;
1626
1627
6.86k
  OverridenCursorsPool &pool =
1628
6.86k
      *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1629
1630
6.86k
  OverridenCursorsPool::CursorVec *Vec = nullptr;
1631
1632
6.86k
  if (!pool.AvailableCursors.empty()) {
1633
6.54k
    Vec = pool.AvailableCursors.back();
1634
6.54k
    pool.AvailableCursors.pop_back();
1635
6.54k
  } else {
1636
324
    Vec = new OverridenCursorsPool::CursorVec();
1637
324
    pool.AllCursors.push_back(Vec);
1638
324
  }
1639
1640
  // Clear out the vector, but don't free the memory contents.  This
1641
  // reduces malloc() traffic.
1642
6.86k
  Vec->clear();
1643
1644
  // Use the first entry to contain a back reference to the vector.
1645
  // This is a complete hack.
1646
6.86k
  CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1647
6.86k
  backRefCursor.data[0] = Vec;
1648
6.86k
  assert(cxcursor::getCursorTU(backRefCursor) == TU);
1649
0
  Vec->push_back(backRefCursor);
1650
1651
  // Get the overridden cursors.
1652
6.86k
  cxcursor::getOverriddenCursors(cursor, *Vec);
1653
1654
  // Did we get any overridden cursors?  If not, return Vec to the pool
1655
  // of available cursor vectors.
1656
6.86k
  if (Vec->size() == 1) {
1657
6.81k
    pool.AvailableCursors.push_back(Vec);
1658
6.81k
    return;
1659
6.81k
  }
1660
1661
  // Now tell the caller about the overridden cursors.
1662
50
  assert(Vec->size() > 1);
1663
0
  *overridden = &((*Vec)[1]);
1664
50
  *num_overridden = Vec->size() - 1;
1665
50
}
1666
1667
50
void clang_disposeOverriddenCursors(CXCursor *overridden) {
1668
50
  if (!overridden)
1669
0
    return;
1670
1671
  // Use pointer arithmetic to get back the first faux entry
1672
  // which has a back-reference to the TU and the vector.
1673
50
  --overridden;
1674
50
  OverridenCursorsPool::CursorVec *Vec =
1675
50
      static_cast<OverridenCursorsPool::CursorVec *>(
1676
50
          const_cast<void *>(overridden->data[0]));
1677
50
  CXTranslationUnit TU = getCursorTU(*overridden);
1678
1679
50
  assert(Vec && TU);
1680
1681
0
  OverridenCursorsPool &pool =
1682
50
      *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1683
1684
50
  pool.AvailableCursors.push_back(Vec);
1685
50
}
1686
1687
231
int clang_Cursor_isDynamicCall(CXCursor C) {
1688
231
  const Expr *E = nullptr;
1689
231
  if (clang_isExpression(C.kind))
1690
52
    E = getCursorExpr(C);
1691
231
  if (!E)
1692
179
    return 0;
1693
1694
52
  if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1695
20
    if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1696
11
      return false;
1697
9
    if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1698
9
            MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1699
1
      if (RecE->getMethodFamily() == OMF_alloc)
1700
1
        return false;
1701
1
    }
1702
8
    return true;
1703
9
  }
1704
1705
32
  if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1706
5
    return !PropRefE->isSuperReceiver();
1707
5
  }
1708
1709
27
  const MemberExpr *ME = nullptr;
1710
27
  if (isa<MemberExpr>(E))
1711
7
    ME = cast<MemberExpr>(E);
1712
20
  else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1713
0
    ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1714
1715
27
  if (ME) {
1716
7
    if (const CXXMethodDecl *MD =
1717
7
            dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1718
3
      return MD->isVirtual() &&
1719
3
             ME->performsVirtualDispatch(
1720
3
                 cxcursor::getCursorContext(C).getLangOpts());
1721
7
  }
1722
1723
24
  return 0;
1724
27
}
1725
1726
32
CXType clang_Cursor_getReceiverType(CXCursor C) {
1727
32
  CXTranslationUnit TU = cxcursor::getCursorTU(C);
1728
32
  const Expr *E = nullptr;
1729
32
  if (clang_isExpression(C.kind))
1730
32
    E = getCursorExpr(C);
1731
1732
32
  if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1733
20
    return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1734
1735
12
  if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1736
5
    return cxtype::MakeCXType(
1737
5
        PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
1738
5
  }
1739
1740
7
  const MemberExpr *ME = nullptr;
1741
7
  if (isa<MemberExpr>(E))
1742
7
    ME = cast<MemberExpr>(E);
1743
0
  else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1744
0
    ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1745
1746
7
  if (ME) {
1747
7
    if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
1748
3
      auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1749
3
      return cxtype::MakeCXType(receiverTy, TU);
1750
3
    }
1751
7
  }
1752
1753
4
  return cxtype::MakeCXType(QualType(), TU);
1754
7
}