Coverage Report

Created: 2020-02-15 09:57

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