Coverage Report

Created: 2022-01-25 06:29

/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
351k
CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
35
351k
  assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
36
0
  CXCursor C = {K, 0, {nullptr, nullptr, TU}};
37
351k
  return C;
38
351k
}
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 ? 
138.0k
:
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::OMPGenericLoopDirectiveClass:
827
0
    K = CXCursor_OMPGenericLoopDirective;
828
0
    break;
829
0
  case Stmt::BuiltinBitCastExprClass:
830
0
    K = CXCursor_BuiltinBitCastExpr;
831
44.5k
  }
832
833
43.5k
  CXCursor C = {K, 0, {Parent, S, TU}};
834
43.5k
  return C;
835
44.5k
}
836
837
CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
838
                                               SourceLocation Loc,
839
137
                                               CXTranslationUnit TU) {
840
137
  assert(Super && TU && "Invalid arguments!");
841
0
  void *RawLoc = Loc.getPtrEncoding();
842
137
  CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
843
137
  return C;
844
137
}
845
846
std::pair<const ObjCInterfaceDecl *, SourceLocation>
847
299
cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
848
299
  assert(C.kind == CXCursor_ObjCSuperClassRef);
849
0
  return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
850
299
                        SourceLocation::getFromPtrEncoding(C.data[1]));
851
299
}
852
853
CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
854
                                             SourceLocation Loc,
855
192
                                             CXTranslationUnit TU) {
856
192
  assert(Proto && TU && "Invalid arguments!");
857
0
  void *RawLoc = Loc.getPtrEncoding();
858
192
  CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
859
192
  return C;
860
192
}
861
862
std::pair<const ObjCProtocolDecl *, SourceLocation>
863
378
cxcursor::getCursorObjCProtocolRef(CXCursor C) {
864
378
  assert(C.kind == CXCursor_ObjCProtocolRef);
865
0
  return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
866
378
                        SourceLocation::getFromPtrEncoding(C.data[1]));
867
378
}
868
869
CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
870
                                          SourceLocation Loc,
871
559
                                          CXTranslationUnit TU) {
872
  // 'Class' can be null for invalid code.
873
559
  if (!Class)
874
1
    return MakeCXCursorInvalid(CXCursor_InvalidCode);
875
558
  assert(TU && "Invalid arguments!");
876
0
  void *RawLoc = Loc.getPtrEncoding();
877
558
  CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
878
558
  return C;
879
559
}
880
881
std::pair<const ObjCInterfaceDecl *, SourceLocation>
882
1.85k
cxcursor::getCursorObjCClassRef(CXCursor C) {
883
1.85k
  assert(C.kind == CXCursor_ObjCClassRef);
884
0
  return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
885
1.85k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
886
1.85k
}
887
888
CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
889
1.38k
                                     CXTranslationUnit TU) {
890
1.38k
  assert(Type && TU && "Invalid arguments!");
891
0
  void *RawLoc = Loc.getPtrEncoding();
892
1.38k
  CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
893
1.38k
  return C;
894
1.38k
}
895
896
std::pair<const TypeDecl *, SourceLocation>
897
7.58k
cxcursor::getCursorTypeRef(CXCursor C) {
898
7.58k
  assert(C.kind == CXCursor_TypeRef);
899
0
  return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
900
7.58k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
901
7.58k
}
902
903
CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
904
                                         SourceLocation Loc,
905
159
                                         CXTranslationUnit TU) {
906
159
  assert(Template && TU && "Invalid arguments!");
907
0
  void *RawLoc = Loc.getPtrEncoding();
908
159
  CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
909
159
  return C;
910
159
}
911
912
std::pair<const TemplateDecl *, SourceLocation>
913
1.01k
cxcursor::getCursorTemplateRef(CXCursor C) {
914
1.01k
  assert(C.kind == CXCursor_TemplateRef);
915
0
  return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
916
1.01k
                        SourceLocation::getFromPtrEncoding(C.data[1]));
917
1.01k
}
918
919
CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
920
                                          SourceLocation Loc,
921
174
                                          CXTranslationUnit TU) {
922
923
174
  assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
924
174
         "Invalid arguments!");
925
0
  void *RawLoc = Loc.getPtrEncoding();
926
174
  CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
927
174
  return C;
928
174
}
929
930
std::pair<const NamedDecl *, SourceLocation>
931
975
cxcursor::getCursorNamespaceRef(CXCursor C) {
932
975
  assert(C.kind == CXCursor_NamespaceRef);
933
0
  return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
934
975
                        SourceLocation::getFromPtrEncoding(C.data[1]));
935
975
}
936
937
CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
938
13
                                         CXTranslationUnit TU) {
939
940
13
  assert(Var && TU && "Invalid arguments!");
941
0
  void *RawLoc = Loc.getPtrEncoding();
942
13
  CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
943
13
  return C;
944
13
}
945
946
std::pair<const VarDecl *, SourceLocation>
947
82
cxcursor::getCursorVariableRef(CXCursor C) {
948
82
  assert(C.kind == CXCursor_VariableRef);
949
0
  return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
950
82
                        SourceLocation::getFromPtrEncoding(C.data[1]));
951
82
}
952
953
CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
954
                                       SourceLocation Loc,
955
50
                                       CXTranslationUnit TU) {
956
957
50
  assert(Field && TU && "Invalid arguments!");
958
0
  void *RawLoc = Loc.getPtrEncoding();
959
50
  CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
960
50
  return C;
961
50
}
962
963
std::pair<const FieldDecl *, SourceLocation>
964
348
cxcursor::getCursorMemberRef(CXCursor C) {
965
348
  assert(C.kind == CXCursor_MemberRef);
966
0
  return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
967
348
                        SourceLocation::getFromPtrEncoding(C.data[1]));
968
348
}
969
970
CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
971
128
                                              CXTranslationUnit TU) {
972
128
  CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
973
128
  return C;
974
128
}
975
976
1.20k
const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
977
1.20k
  assert(C.kind == CXCursor_CXXBaseSpecifier);
978
0
  return static_cast<const CXXBaseSpecifier *>(C.data[0]);
979
1.20k
}
980
981
CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
982
57
                                                    CXTranslationUnit TU) {
983
57
  CXCursor C = {
984
57
      CXCursor_PreprocessingDirective,
985
57
      0,
986
57
      {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
987
57
  return C;
988
57
}
989
990
300
SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
991
300
  assert(C.kind == CXCursor_PreprocessingDirective);
992
0
  SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
993
300
                    SourceLocation::getFromPtrEncoding(C.data[1]));
994
300
  ASTUnit *TU = getCursorASTUnit(C);
995
300
  return TU->mapRangeFromPreamble(Range);
996
300
}
997
998
CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
999
71.5k
                                             CXTranslationUnit TU) {
1000
71.5k
  CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
1001
71.5k
  return C;
1002
71.5k
}
1003
1004
466k
const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
1005
466k
  assert(C.kind == CXCursor_MacroDefinition);
1006
0
  return static_cast<const MacroDefinitionRecord *>(C.data[0]);
1007
466k
}
1008
1009
CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
1010
5.91k
                                            CXTranslationUnit TU) {
1011
5.91k
  CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
1012
5.91k
  return C;
1013
5.91k
}
1014
1015
CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1016
                                            SourceLocation Loc,
1017
16
                                            CXTranslationUnit TU) {
1018
16
  assert(Loc.isValid());
1019
0
  CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
1020
16
  return C;
1021
16
}
1022
1023
246
const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1024
246
  if (isPseudo())
1025
12
    return getAsMacroDefinition()->getName();
1026
234
  return getAsMacroExpansion()->getName();
1027
246
}
1028
const MacroDefinitionRecord *
1029
211
cxcursor::MacroExpansionCursor::getDefinition() const {
1030
211
  if (isPseudo())
1031
8
    return getAsMacroDefinition();
1032
203
  return getAsMacroExpansion()->getDefinition();
1033
211
}
1034
6.83k
SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1035
6.83k
  if (isPseudo())
1036
48
    return getPseudoLoc();
1037
6.78k
  return getAsMacroExpansion()->getSourceRange();
1038
6.83k
}
1039
1040
CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1041
311
                                                CXTranslationUnit TU) {
1042
311
  CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
1043
311
  return C;
1044
311
}
1045
1046
1.85k
const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1047
1.85k
  assert(C.kind == CXCursor_InclusionDirective);
1048
0
  return static_cast<const InclusionDirective *>(C.data[0]);
1049
1.85k
}
1050
1051
CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1052
2
                                      CXTranslationUnit TU) {
1053
1054
2
  assert(Label && TU && "Invalid arguments!");
1055
0
  void *RawLoc = Loc.getPtrEncoding();
1056
2
  CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
1057
2
  return C;
1058
2
}
1059
1060
std::pair<const LabelStmt *, SourceLocation>
1061
16
cxcursor::getCursorLabelRef(CXCursor C) {
1062
16
  assert(C.kind == CXCursor_LabelRef);
1063
0
  return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
1064
16
                        SourceLocation::getFromPtrEncoding(C.data[1]));
1065
16
}
1066
1067
CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1068
29
                                               CXTranslationUnit TU) {
1069
29
  assert(E && TU && "Invalid arguments!");
1070
0
  OverloadedDeclRefStorage Storage(E);
1071
29
  void *RawLoc = E->getNameLoc().getPtrEncoding();
1072
29
  CXCursor C = {
1073
29
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1074
29
  return C;
1075
29
}
1076
1077
CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1078
                                               SourceLocation Loc,
1079
33
                                               CXTranslationUnit TU) {
1080
33
  assert(D && TU && "Invalid arguments!");
1081
0
  void *RawLoc = Loc.getPtrEncoding();
1082
33
  OverloadedDeclRefStorage Storage(D);
1083
33
  CXCursor C = {
1084
33
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1085
33
  return C;
1086
33
}
1087
1088
CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1089
                                               SourceLocation Loc,
1090
0
                                               CXTranslationUnit TU) {
1091
0
  assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1092
0
  void *RawLoc = Loc.getPtrEncoding();
1093
0
  OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1094
0
  CXCursor C = {
1095
0
      CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1096
0
  return C;
1097
0
}
1098
1099
std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
1100
319
cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1101
319
  assert(C.kind == CXCursor_OverloadedDeclRef);
1102
0
  return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1103
319
                            const_cast<void *>(C.data[0])),
1104
319
                        SourceLocation::getFromPtrEncoding(C.data[1]));
1105
319
}
1106
1107
308k
const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1108
308k
  return static_cast<const Decl *>(Cursor.data[0]);
1109
308k
}
1110
1111
195k
const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1112
195k
  return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
1113
195k
}
1114
1115
204k
const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1116
204k
  if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1117
204k
      Cursor.kind == CXCursor_ObjCProtocolRef ||
1118
204k
      Cursor.kind == CXCursor_ObjCClassRef)
1119
0
    return nullptr;
1120
1121
204k
  return static_cast<const Stmt *>(Cursor.data[1]);
1122
204k
}
1123
1124
1.05k
const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1125
1.05k
  return static_cast<const Attr *>(Cursor.data[1]);
1126
1.05k
}
1127
1128
510k
ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1129
510k
  return getCursorASTUnit(Cursor)->getASTContext();
1130
510k
}
1131
1132
750k
ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1133
750k
  CXTranslationUnit TU = getCursorTU(Cursor);
1134
750k
  if (!TU)
1135
0
    return nullptr;
1136
750k
  return cxtu::getASTUnit(TU);
1137
750k
}
1138
1139
1.07M
CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1140
1.07M
  return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1141
1.07M
}
1142
1143
void cxcursor::getOverriddenCursors(CXCursor cursor,
1144
7.06k
                                    SmallVectorImpl<CXCursor> &overridden) {
1145
7.06k
  assert(clang_isDeclaration(cursor.kind));
1146
0
  const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
1147
7.06k
  if (!D)
1148
165
    return;
1149
1150
6.90k
  CXTranslationUnit TU = getCursorTU(cursor);
1151
6.90k
  SmallVector<const NamedDecl *, 8> OverDecls;
1152
6.90k
  D->getASTContext().getOverriddenMethods(D, OverDecls);
1153
1154
6.90k
  for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1155
6.90k
                                                    E = OverDecls.end();
1156
6.97k
       I != E; 
++I76
) {
1157
76
    overridden.push_back(MakeCXCursor(*I, TU));
1158
76
  }
1159
6.90k
}
1160
1161
std::pair<int, SourceLocation>
1162
15.3k
cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1163
15.3k
  if (cursor.kind == CXCursor_ObjCMessageExpr) {
1164
159
    if (cursor.xdata != -1)
1165
76
      return std::make_pair(cursor.xdata,
1166
76
                            cast<ObjCMessageExpr>(getCursorExpr(cursor))
1167
76
                                ->getSelectorLoc(cursor.xdata));
1168
15.1k
  } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1169
15.1k
             cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1170
44
    if (cursor.xdata != -1)
1171
44
      return std::make_pair(cursor.xdata,
1172
44
                            cast<ObjCMethodDecl>(getCursorDecl(cursor))
1173
44
                                ->getSelectorLoc(cursor.xdata));
1174
44
  }
1175
1176
15.2k
  return std::make_pair(-1, SourceLocation());
1177
15.3k
}
1178
1179
15.7k
CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1180
15.7k
  CXCursor newCursor = cursor;
1181
1182
15.7k
  if (cursor.kind == CXCursor_ObjCMessageExpr) {
1183
691
    if (SelIdx == -1 ||
1184
691
        unsigned(SelIdx) >=
1185
130
            cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1186
561
      newCursor.xdata = -1;
1187
130
    else
1188
130
      newCursor.xdata = SelIdx;
1189
15.0k
  } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1190
15.0k
             
cursor.kind == CXCursor_ObjCInstanceMethodDecl15.0k
) {
1191
121
    if (SelIdx == -1 ||
1192
121
        unsigned(SelIdx) >=
1193
38
            cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1194
83
      newCursor.xdata = -1;
1195
38
    else
1196
38
      newCursor.xdata = SelIdx;
1197
121
  }
1198
1199
15.7k
  return newCursor;
1200
15.7k
}
1201
1202
31
CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1203
31
  if (cursor.kind != CXCursor_CallExpr)
1204
28
    return cursor;
1205
1206
3
  if (cursor.xdata == 0)
1207
0
    return cursor;
1208
1209
3
  const Expr *E = getCursorExpr(cursor);
1210
3
  TypeSourceInfo *Type = nullptr;
1211
3
  if (const CXXUnresolvedConstructExpr *UnCtor =
1212
3
          dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1213
0
    Type = UnCtor->getTypeSourceInfo();
1214
3
  } else if (const CXXTemporaryObjectExpr *Tmp =
1215
3
                 dyn_cast<CXXTemporaryObjectExpr>(E)) {
1216
3
    Type = Tmp->getTypeSourceInfo();
1217
3
  }
1218
1219
3
  if (!Type)
1220
0
    return cursor;
1221
1222
3
  CXTranslationUnit TU = getCursorTU(cursor);
1223
3
  QualType Ty = Type->getType();
1224
3
  TypeLoc TL = Type->getTypeLoc();
1225
3
  SourceLocation Loc = TL.getBeginLoc();
1226
1227
3
  if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1228
3
    Ty = ElabT->getNamedType();
1229
3
    ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1230
3
    Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1231
3
  }
1232
1233
3
  if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1234
1
    return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1235
2
  if (const TagType *Tag = Ty->getAs<TagType>())
1236
2
    return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1237
0
  if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1238
0
    return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1239
1240
0
  return cursor;
1241
0
}
1242
1243
218k
bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1244
218k
  return X.kind == Y.kind && 
X.data[0] == Y.data[0]176k
&&
X.data[1] == Y.data[1]176k
&&
1245
218k
         
X.data[2] == Y.data[2]176k
;
1246
218k
}
1247
1248
// FIXME: Remove once we can model DeclGroups and their appropriate ranges
1249
// properly in the ASTs.
1250
10.5k
bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1251
10.5k
  assert(clang_isDeclaration(C.kind));
1252
0
  return ((uintptr_t)(C.data[1])) != 0;
1253
10.5k
}
1254
1255
//===----------------------------------------------------------------------===//
1256
// libclang CXCursor APIs
1257
//===----------------------------------------------------------------------===//
1258
1259
1.38k
int clang_Cursor_isNull(CXCursor cursor) {
1260
1.38k
  return clang_equalCursors(cursor, clang_getNullCursor());
1261
1.38k
}
1262
1263
92.0k
CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1264
92.0k
  return getCursorTU(cursor);
1265
92.0k
}
1266
1267
5.92k
int clang_Cursor_getNumArguments(CXCursor C) {
1268
5.92k
  if (clang_isDeclaration(C.kind)) {
1269
406
    const Decl *D = cxcursor::getCursorDecl(C);
1270
406
    if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1271
8
      return MD->param_size();
1272
398
    if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1273
63
      return FD->param_size();
1274
398
  }
1275
1276
5.85k
  if (clang_isExpression(C.kind)) {
1277
86
    const Expr *E = cxcursor::getCursorExpr(C);
1278
86
    if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1279
2
      return CE->getNumArgs();
1280
2
    }
1281
84
    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1282
5
      return CE->getNumArgs();
1283
5
    }
1284
84
  }
1285
1286
5.84k
  return -1;
1287
5.85k
}
1288
1289
67
CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1290
67
  if (clang_isDeclaration(C.kind)) {
1291
63
    const Decl *D = cxcursor::getCursorDecl(C);
1292
63
    if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1293
7
      if (i < MD->param_size())
1294
7
        return cxcursor::MakeCXCursor(MD->parameters()[i],
1295
7
                                      cxcursor::getCursorTU(C));
1296
56
    } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1297
56
      if (i < FD->param_size())
1298
56
        return cxcursor::MakeCXCursor(FD->parameters()[i],
1299
56
                                      cxcursor::getCursorTU(C));
1300
56
    }
1301
63
  }
1302
1303
4
  if (clang_isExpression(C.kind)) {
1304
4
    const Expr *E = cxcursor::getCursorExpr(C);
1305
4
    if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1306
2
      if (i < CE->getNumArgs()) {
1307
2
        return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1308
2
                                      cxcursor::getCursorTU(C));
1309
2
      }
1310
2
    }
1311
2
    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1312
2
      if (i < CE->getNumArgs()) {
1313
2
        return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1314
2
                                      cxcursor::getCursorTU(C));
1315
2
      }
1316
2
    }
1317
2
  }
1318
1319
0
  return clang_getNullCursor();
1320
4
}
1321
1322
12
int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1323
12
  if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1324
0
    return -1;
1325
0
  }
1326
1327
12
  const FunctionDecl *FD =
1328
12
      llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1329
12
  if (!FD) {
1330
0
    return -1;
1331
0
  }
1332
1333
12
  const FunctionTemplateSpecializationInfo *SpecInfo =
1334
12
      FD->getTemplateSpecializationInfo();
1335
12
  if (!SpecInfo) {
1336
0
    return -1;
1337
0
  }
1338
1339
12
  return SpecInfo->TemplateArguments->size();
1340
12
}
1341
1342
enum CXGetTemplateArgumentStatus {
1343
  /** The operation completed successfully */
1344
  CXGetTemplateArgumentStatus_Success = 0,
1345
1346
  /** The specified cursor did not represent a FunctionDecl. */
1347
  CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
1348
1349
  /** The specified cursor was not castable to a FunctionDecl. */
1350
  CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
1351
1352
  /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1353
  CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1354
1355
  /** An invalid (OOB) argument index was specified */
1356
  CXGetTemplateArgumentStatus_InvalidIndex = -4
1357
};
1358
1359
static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1360
36
                                            TemplateArgument *TA) {
1361
36
  if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1362
0
    return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
1363
0
  }
1364
1365
36
  const FunctionDecl *FD =
1366
36
      llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1367
36
  if (!FD) {
1368
0
    return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
1369
0
  }
1370
1371
36
  const FunctionTemplateSpecializationInfo *SpecInfo =
1372
36
      FD->getTemplateSpecializationInfo();
1373
36
  if (!SpecInfo) {
1374
0
    return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1375
0
  }
1376
1377
36
  if (I >= SpecInfo->TemplateArguments->size()) {
1378
0
    return CXGetTemplateArgumentStatus_InvalidIndex;
1379
0
  }
1380
1381
36
  *TA = SpecInfo->TemplateArguments->get(I);
1382
36
  return 0;
1383
36
}
1384
1385
enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1386
18
                                                                 unsigned I) {
1387
18
  TemplateArgument TA;
1388
18
  if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
1389
0
    return CXTemplateArgumentKind_Invalid;
1390
0
  }
1391
1392
18
  switch (TA.getKind()) {
1393
0
  case TemplateArgument::Null:
1394
0
    return CXTemplateArgumentKind_Null;
1395
15
  case TemplateArgument::Type:
1396
15
    return CXTemplateArgumentKind_Type;
1397
0
  case TemplateArgument::Declaration:
1398
0
    return CXTemplateArgumentKind_Declaration;
1399
0
  case TemplateArgument::NullPtr:
1400
0
    return CXTemplateArgumentKind_NullPtr;
1401
3
  case TemplateArgument::Integral:
1402
3
    return CXTemplateArgumentKind_Integral;
1403
0
  case TemplateArgument::Template:
1404
0
    return CXTemplateArgumentKind_Template;
1405
0
  case TemplateArgument::TemplateExpansion:
1406
0
    return CXTemplateArgumentKind_TemplateExpansion;
1407
0
  case TemplateArgument::Expression:
1408
0
    return CXTemplateArgumentKind_Expression;
1409
0
  case TemplateArgument::Pack:
1410
0
    return CXTemplateArgumentKind_Pack;
1411
18
  }
1412
1413
0
  return CXTemplateArgumentKind_Invalid;
1414
18
}
1415
1416
15
CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1417
15
  TemplateArgument TA;
1418
15
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1419
15
      CXGetTemplateArgumentStatus_Success) {
1420
0
    return cxtype::MakeCXType(QualType(), getCursorTU(C));
1421
0
  }
1422
1423
15
  if (TA.getKind() != TemplateArgument::Type) {
1424
0
    return cxtype::MakeCXType(QualType(), getCursorTU(C));
1425
0
  }
1426
1427
15
  return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
1428
15
}
1429
1430
3
long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1431
3
  TemplateArgument TA;
1432
3
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1433
3
      CXGetTemplateArgumentStatus_Success) {
1434
0
    assert(0 && "Unable to retrieve TemplateArgument");
1435
0
    return 0;
1436
0
  }
1437
1438
3
  if (TA.getKind() != TemplateArgument::Integral) {
1439
0
    assert(0 && "Passed template argument is not Integral");
1440
0
    return 0;
1441
0
  }
1442
1443
3
  return TA.getAsIntegral().getSExtValue();
1444
3
}
1445
1446
unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1447
0
                                                                 unsigned I) {
1448
0
  TemplateArgument TA;
1449
0
  if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1450
0
      CXGetTemplateArgumentStatus_Success) {
1451
0
    assert(0 && "Unable to retrieve TemplateArgument");
1452
0
    return 0;
1453
0
  }
1454
1455
0
  if (TA.getKind() != TemplateArgument::Integral) {
1456
0
    assert(0 && "Passed template argument is not Integral");
1457
0
    return 0;
1458
0
  }
1459
1460
0
  return TA.getAsIntegral().getZExtValue();
1461
0
}
1462
1463
//===----------------------------------------------------------------------===//
1464
// CXCursorSet.
1465
//===----------------------------------------------------------------------===//
1466
1467
typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1468
1469
0
static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1470
0
  return (CXCursorSet)setImpl;
1471
0
}
1472
0
static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1473
0
  return (CXCursorSet_Impl *)set;
1474
0
}
1475
namespace llvm {
1476
template <> struct DenseMapInfo<CXCursor> {
1477
public:
1478
0
  static inline CXCursor getEmptyKey() {
1479
0
    return MakeCXCursorInvalid(CXCursor_InvalidFile);
1480
0
  }
1481
0
  static inline CXCursor getTombstoneKey() {
1482
0
    return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1483
0
  }
1484
0
  static inline unsigned getHashValue(const CXCursor &cursor) {
1485
0
    return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1486
0
        getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1487
0
  }
1488
0
  static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1489
0
    return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1490
0
  }
1491
};
1492
} // namespace llvm
1493
1494
0
CXCursorSet clang_createCXCursorSet() {
1495
0
  return packCXCursorSet(new CXCursorSet_Impl());
1496
0
}
1497
1498
0
void clang_disposeCXCursorSet(CXCursorSet set) {
1499
0
  delete unpackCXCursorSet(set);
1500
0
}
1501
1502
0
unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1503
0
  CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1504
0
  if (!setImpl)
1505
0
    return 0;
1506
0
  return setImpl->find(cursor) != setImpl->end();
1507
0
}
1508
1509
0
unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1510
  // Do not insert invalid cursors into the set.
1511
0
  if (cursor.kind >= CXCursor_FirstInvalid &&
1512
0
      cursor.kind <= CXCursor_LastInvalid)
1513
0
    return 1;
1514
1515
0
  CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1516
0
  if (!setImpl)
1517
0
    return 1;
1518
0
  unsigned &entry = (*setImpl)[cursor];
1519
0
  unsigned flag = entry == 0 ? 1 : 0;
1520
0
  entry = 1;
1521
0
  return flag;
1522
0
}
1523
1524
231
CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1525
231
  enum CXCursorKind kind = clang_getCursorKind(cursor);
1526
231
  if (clang_isDeclaration(kind)) {
1527
111
    const Decl *decl = getCursorDecl(cursor);
1528
111
    if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1529
105
      ASTUnit *unit = getCursorASTUnit(cursor);
1530
105
      CodeCompletionResult Result(namedDecl, CCP_Declaration);
1531
105
      CodeCompletionString *String = Result.CreateCodeCompletionString(
1532
105
          unit->getASTContext(), unit->getPreprocessor(),
1533
105
          CodeCompletionContext::CCC_Other,
1534
105
          unit->getCodeCompletionTUInfo().getAllocator(),
1535
105
          unit->getCodeCompletionTUInfo(), true);
1536
105
      return String;
1537
105
    }
1538
120
  } else if (kind == CXCursor_MacroDefinition) {
1539
12
    const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
1540
12
    const IdentifierInfo *Macro = definition->getName();
1541
12
    ASTUnit *unit = getCursorASTUnit(cursor);
1542
12
    CodeCompletionResult Result(
1543
12
        Macro,
1544
12
        unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
1545
12
    CodeCompletionString *String = Result.CreateCodeCompletionString(
1546
12
        unit->getASTContext(), unit->getPreprocessor(),
1547
12
        CodeCompletionContext::CCC_Other,
1548
12
        unit->getCodeCompletionTUInfo().getAllocator(),
1549
12
        unit->getCodeCompletionTUInfo(), false);
1550
12
    return String;
1551
12
  }
1552
114
  return nullptr;
1553
231
}
1554
1555
namespace {
1556
struct OverridenCursorsPool {
1557
  typedef SmallVector<CXCursor, 2> CursorVec;
1558
  std::vector<CursorVec *> AllCursors;
1559
  std::vector<CursorVec *> AvailableCursors;
1560
1561
1.04k
  ~OverridenCursorsPool() {
1562
1.04k
    for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1563
1.04k
                                            E = AllCursors.end();
1564
1.36k
         I != E; 
++I321
) {
1565
321
      delete *I;
1566
321
    }
1567
1.04k
  }
1568
};
1569
} // namespace
1570
1571
1.05k
void *cxcursor::createOverridenCXCursorsPool() {
1572
1.05k
  return new OverridenCursorsPool();
1573
1.05k
}
1574
1575
1.04k
void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1576
1.04k
  delete static_cast<OverridenCursorsPool *>(pool);
1577
1.04k
}
1578
1579
void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1580
86.9k
                                unsigned *num_overridden) {
1581
86.9k
  if (overridden)
1582
86.9k
    *overridden = nullptr;
1583
86.9k
  if (num_overridden)
1584
86.9k
    *num_overridden = 0;
1585
1586
86.9k
  CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1587
1588
86.9k
  if (!overridden || !num_overridden || !TU)
1589
0
    return;
1590
1591
86.9k
  if (!clang_isDeclaration(cursor.kind))
1592
80.0k
    return;
1593
1594
6.94k
  OverridenCursorsPool &pool =
1595
6.94k
      *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1596
1597
6.94k
  OverridenCursorsPool::CursorVec *Vec = nullptr;
1598
1599
6.94k
  if (!pool.AvailableCursors.empty()) {
1600
6.62k
    Vec = pool.AvailableCursors.back();
1601
6.62k
    pool.AvailableCursors.pop_back();
1602
6.62k
  } else {
1603
321
    Vec = new OverridenCursorsPool::CursorVec();
1604
321
    pool.AllCursors.push_back(Vec);
1605
321
  }
1606
1607
  // Clear out the vector, but don't free the memory contents.  This
1608
  // reduces malloc() traffic.
1609
6.94k
  Vec->clear();
1610
1611
  // Use the first entry to contain a back reference to the vector.
1612
  // This is a complete hack.
1613
6.94k
  CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1614
6.94k
  backRefCursor.data[0] = Vec;
1615
6.94k
  assert(cxcursor::getCursorTU(backRefCursor) == TU);
1616
0
  Vec->push_back(backRefCursor);
1617
1618
  // Get the overridden cursors.
1619
6.94k
  cxcursor::getOverriddenCursors(cursor, *Vec);
1620
1621
  // Did we get any overridden cursors?  If not, return Vec to the pool
1622
  // of available cursor vectors.
1623
6.94k
  if (Vec->size() == 1) {
1624
6.89k
    pool.AvailableCursors.push_back(Vec);
1625
6.89k
    return;
1626
6.89k
  }
1627
1628
  // Now tell the caller about the overridden cursors.
1629
50
  assert(Vec->size() > 1);
1630
0
  *overridden = &((*Vec)[1]);
1631
50
  *num_overridden = Vec->size() - 1;
1632
50
}
1633
1634
50
void clang_disposeOverriddenCursors(CXCursor *overridden) {
1635
50
  if (!overridden)
1636
0
    return;
1637
1638
  // Use pointer arithmetic to get back the first faux entry
1639
  // which has a back-reference to the TU and the vector.
1640
50
  --overridden;
1641
50
  OverridenCursorsPool::CursorVec *Vec =
1642
50
      static_cast<OverridenCursorsPool::CursorVec *>(
1643
50
          const_cast<void *>(overridden->data[0]));
1644
50
  CXTranslationUnit TU = getCursorTU(*overridden);
1645
1646
50
  assert(Vec && TU);
1647
1648
0
  OverridenCursorsPool &pool =
1649
50
      *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1650
1651
50
  pool.AvailableCursors.push_back(Vec);
1652
50
}
1653
1654
231
int clang_Cursor_isDynamicCall(CXCursor C) {
1655
231
  const Expr *E = nullptr;
1656
231
  if (clang_isExpression(C.kind))
1657
52
    E = getCursorExpr(C);
1658
231
  if (!E)
1659
179
    return 0;
1660
1661
52
  if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1662
20
    if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1663
11
      return false;
1664
9
    if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1665
9
            MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1666
1
      if (RecE->getMethodFamily() == OMF_alloc)
1667
1
        return false;
1668
1
    }
1669
8
    return true;
1670
9
  }
1671
1672
32
  if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1673
5
    return !PropRefE->isSuperReceiver();
1674
5
  }
1675
1676
27
  const MemberExpr *ME = nullptr;
1677
27
  if (isa<MemberExpr>(E))
1678
7
    ME = cast<MemberExpr>(E);
1679
20
  else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1680
0
    ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1681
1682
27
  if (ME) {
1683
7
    if (const CXXMethodDecl *MD =
1684
7
            dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1685
3
      return MD->isVirtual() &&
1686
3
             ME->performsVirtualDispatch(
1687
3
                 cxcursor::getCursorContext(C).getLangOpts());
1688
7
  }
1689
1690
24
  return 0;
1691
27
}
1692
1693
32
CXType clang_Cursor_getReceiverType(CXCursor C) {
1694
32
  CXTranslationUnit TU = cxcursor::getCursorTU(C);
1695
32
  const Expr *E = nullptr;
1696
32
  if (clang_isExpression(C.kind))
1697
32
    E = getCursorExpr(C);
1698
1699
32
  if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1700
20
    return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1701
1702
12
  if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1703
5
    return cxtype::MakeCXType(
1704
5
        PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
1705
5
  }
1706
1707
7
  const MemberExpr *ME = nullptr;
1708
7
  if (isa<MemberExpr>(E))
1709
7
    ME = cast<MemberExpr>(E);
1710
0
  else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1711
0
    ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1712
1713
7
  if (ME) {
1714
7
    if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
1715
3
      auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1716
3
      return cxtype::MakeCXType(receiverTy, TU);
1717
3
    }
1718
7
  }
1719
1720
4
  return cxtype::MakeCXType(QualType(), TU);
1721
7
}