Coverage Report

Created: 2020-09-22 08:39

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