Coverage Report

Created: 2021-09-21 08:58

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