Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Index/IndexSymbol.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
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
#include "clang/Index/IndexSymbol.h"
10
#include "clang/AST/Attr.h"
11
#include "clang/AST/DeclCXX.h"
12
#include "clang/AST/DeclObjC.h"
13
#include "clang/AST/DeclTemplate.h"
14
#include "clang/AST/PrettyPrinter.h"
15
#include "clang/Lex/MacroInfo.h"
16
17
using namespace clang;
18
using namespace clang::index;
19
20
/// \returns true if \c D is a subclass of 'XCTestCase'.
21
212
static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
22
212
  if (!D)
23
0
    return false;
24
245
  
while (const ObjCInterfaceDecl *212
SuperD = D->getSuperClass()) {
25
45
    if (SuperD->getName() == "XCTestCase")
26
12
      return true;
27
33
    D = SuperD;
28
33
  }
29
200
  return false;
30
212
}
31
32
/// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
33
/// no parameters, and its name starts with 'test'.
34
258
static bool isUnitTest(const ObjCMethodDecl *D) {
35
258
  if (!D->parameters().empty())
36
114
    return false;
37
144
  if (!D->getReturnType()->isVoidType())
38
102
    return false;
39
42
  if (!D->getSelector().getNameForSlot(0).startswith("test"))
40
39
    return false;
41
3
  return isUnitTestCase(D->getClassInterface());
42
42
}
43
44
98
static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
45
98
  if (D->hasAttr<IBOutletAttr>()) {
46
1
    PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
47
97
  } else if (D->hasAttr<IBOutletCollectionAttr>()) {
48
8
    PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
49
8
    PropSet |= (SymbolPropertySet)SymbolProperty::IBOutletCollection;
50
8
  }
51
98
}
52
53
10.9k
bool index::isFunctionLocalSymbol(const Decl *D) {
54
10.9k
  assert(D);
55
56
10.9k
  if (isa<ParmVarDecl>(D))
57
2.08k
    return true;
58
59
8.83k
  if (isa<ObjCTypeParamDecl>(D))
60
0
    return true;
61
62
8.83k
  if (isa<UsingDirectiveDecl>(D))
63
2
    return false;
64
8.83k
  if (!D->getParentFunctionOrMethod())
65
8.63k
    return false;
66
67
206
  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68
205
    switch (ND->getFormalLinkage()) {
69
205
      case NoLinkage:
70
205
      case InternalLinkage:
71
205
        return true;
72
0
      case VisibleNoLinkage:
73
0
      case UniqueExternalLinkage:
74
0
      case ModuleInternalLinkage:
75
0
        llvm_unreachable("Not a sema linkage");
76
0
      case ModuleLinkage:
77
0
      case ExternalLinkage:
78
0
        return false;
79
205
    }
80
205
  }
81
82
1
  return true;
83
206
}
84
85
5.59k
SymbolInfo index::getSymbolInfo(const Decl *D) {
86
5.59k
  assert(D);
87
0
  SymbolInfo Info;
88
5.59k
  Info.Kind = SymbolKind::Unknown;
89
5.59k
  Info.SubKind = SymbolSubKind::None;
90
5.59k
  Info.Properties = SymbolPropertySet();
91
5.59k
  Info.Lang = SymbolLanguage::C;
92
93
5.59k
  if (isFunctionLocalSymbol(D)) {
94
104
    Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
95
104
  }
96
5.59k
  if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
97
7
    Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
98
7
  }
99
100
5.59k
  if (auto *VT = dyn_cast<VarTemplateDecl>(D)) {
101
0
    Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
102
0
    Info.Lang = SymbolLanguage::CXX;
103
    // All other fields are filled from the templated decl.
104
0
    D = VT->getTemplatedDecl();
105
0
  }
106
107
5.59k
  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
108
516
    switch (TD->getTagKind()) {
109
229
    case TTK_Struct:
110
229
      Info.Kind = SymbolKind::Struct; break;
111
5
    case TTK_Union:
112
5
      Info.Kind = SymbolKind::Union; break;
113
257
    case TTK_Class:
114
257
      Info.Kind = SymbolKind::Class;
115
257
      Info.Lang = SymbolLanguage::CXX;
116
257
      break;
117
0
    case TTK_Interface:
118
0
      Info.Kind = SymbolKind::Protocol;
119
0
      Info.Lang = SymbolLanguage::CXX;
120
0
      break;
121
25
    case TTK_Enum:
122
25
      Info.Kind = SymbolKind::Enum; break;
123
516
    }
124
125
516
    if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
126
470
      if (!CXXRec->isCLike()) {
127
418
        Info.Lang = SymbolLanguage::CXX;
128
418
        if (CXXRec->getDescribedClassTemplate()) {
129
172
          Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
130
172
        }
131
418
      }
132
470
    }
133
134
516
    if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
135
20
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
136
20
      Info.Properties |=
137
20
          (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
138
496
    } else if (isa<ClassTemplateSpecializationDecl>(D)) {
139
45
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
140
45
      Info.Properties |=
141
45
          (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
142
45
    }
143
144
5.07k
  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
145
245
    Info.Kind = SymbolKind::Variable;
146
245
    if (isa<ParmVarDecl>(D)) {
147
37
      Info.Kind = SymbolKind::Parameter;
148
208
    } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
149
27
      Info.Kind = SymbolKind::StaticProperty;
150
27
      Info.Lang = SymbolLanguage::CXX;
151
27
    }
152
153
245
    if (isa<VarTemplatePartialSpecializationDecl>(D)) {
154
0
      Info.Lang = SymbolLanguage::CXX;
155
0
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
156
0
      Info.Properties |=
157
0
          (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
158
245
    } else if (isa<VarTemplateSpecializationDecl>(D)) {
159
4
      Info.Lang = SymbolLanguage::CXX;
160
4
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
161
4
      Info.Properties |=
162
4
          (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
163
241
    } else if (VD->getDescribedVarTemplate()) {
164
8
      Info.Lang = SymbolLanguage::CXX;
165
8
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
166
8
    }
167
168
4.83k
  } else {
169
4.83k
    switch (D->getKind()) {
170
9
    case Decl::Import:
171
9
      Info.Kind = SymbolKind::Module;
172
9
      break;
173
75
    case Decl::Typedef:
174
75
      Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
175
1.96k
    case Decl::Function:
176
1.96k
      Info.Kind = SymbolKind::Function;
177
1.96k
      break;
178
71
    case Decl::Field:
179
71
    case Decl::IndirectField:
180
71
      Info.Kind = SymbolKind::Field;
181
71
      if (const CXXRecordDecl *
182
71
            CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
183
59
        if (!CXXRec->isCLike())
184
37
          Info.Lang = SymbolLanguage::CXX;
185
59
      }
186
71
      break;
187
25
    case Decl::EnumConstant:
188
25
      Info.Kind = SymbolKind::EnumConstant; break;
189
175
    case Decl::ObjCInterface:
190
191
    case Decl::ObjCImplementation: {
191
191
      Info.Kind = SymbolKind::Class;
192
191
      Info.Lang = SymbolLanguage::ObjC;
193
191
      const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
194
191
      if (!ClsD)
195
16
        ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
196
191
      if (isUnitTestCase(ClsD))
197
7
        Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
198
191
      break;
199
175
    }
200
56
    case Decl::ObjCProtocol:
201
56
      Info.Kind = SymbolKind::Protocol;
202
56
      Info.Lang = SymbolLanguage::ObjC;
203
56
      break;
204
15
    case Decl::ObjCCategory:
205
18
    case Decl::ObjCCategoryImpl: {
206
18
      Info.Kind = SymbolKind::Extension;
207
18
      Info.Lang = SymbolLanguage::ObjC;
208
18
      const ObjCInterfaceDecl *ClsD = nullptr;
209
18
      if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
210
15
        ClsD = CatD->getClassInterface();
211
3
      else
212
3
        ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
213
18
      if (isUnitTestCase(ClsD))
214
2
        Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
215
18
      break;
216
15
    }
217
258
    case Decl::ObjCMethod: {
218
258
      const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
219
258
      Info.Kind = MD->isInstanceMethod() ? 
SymbolKind::InstanceMethod215
:
SymbolKind::ClassMethod43
;
220
258
      if (MD->isPropertyAccessor()) {
221
136
        if (MD->param_size())
222
64
          Info.SubKind = SymbolSubKind::AccessorSetter;
223
72
        else
224
72
          Info.SubKind = SymbolSubKind::AccessorGetter;
225
136
      }
226
258
      Info.Lang = SymbolLanguage::ObjC;
227
258
      if (isUnitTest(MD))
228
3
        Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
229
258
      if (D->hasAttr<IBActionAttr>())
230
3
        Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
231
258
      break;
232
15
    }
233
65
    case Decl::ObjCProperty:
234
65
      Info.Kind = SymbolKind::InstanceProperty;
235
65
      Info.Lang = SymbolLanguage::ObjC;
236
65
      checkForIBOutlets(D, Info.Properties);
237
65
      if (auto *Annot = D->getAttr<AnnotateAttr>()) {
238
2
        if (Annot->getAnnotation() == "gk_inspectable")
239
1
          Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
240
2
      }
241
65
      break;
242
33
    case Decl::ObjCIvar:
243
33
      Info.Kind = SymbolKind::Field;
244
33
      Info.Lang = SymbolLanguage::ObjC;
245
33
      checkForIBOutlets(D, Info.Properties);
246
33
      break;
247
58
    case Decl::Namespace:
248
58
      Info.Kind = SymbolKind::Namespace;
249
58
      Info.Lang = SymbolLanguage::CXX;
250
58
      break;
251
10
    case Decl::NamespaceAlias:
252
10
      Info.Kind = SymbolKind::NamespaceAlias;
253
10
      Info.Lang = SymbolLanguage::CXX;
254
10
      break;
255
39
    case Decl::CXXConstructor: {
256
39
      Info.Kind = SymbolKind::Constructor;
257
39
      Info.Lang = SymbolLanguage::CXX;
258
39
      auto *CD = cast<CXXConstructorDecl>(D);
259
39
      if (CD->isCopyConstructor())
260
10
        Info.SubKind = SymbolSubKind::CXXCopyConstructor;
261
29
      else if (CD->isMoveConstructor())
262
4
        Info.SubKind = SymbolSubKind::CXXMoveConstructor;
263
39
      break;
264
15
    }
265
6
    case Decl::CXXDestructor:
266
6
      Info.Kind = SymbolKind::Destructor;
267
6
      Info.Lang = SymbolLanguage::CXX;
268
6
      break;
269
0
    case Decl::CXXConversion:
270
0
      Info.Kind = SymbolKind::ConversionFunction;
271
0
      Info.Lang = SymbolLanguage::CXX;
272
0
      break;
273
1.87k
    case Decl::CXXMethod: {
274
1.87k
      const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
275
1.87k
      if (MD->isStatic())
276
10
        Info.Kind = SymbolKind::StaticMethod;
277
1.86k
      else
278
1.86k
        Info.Kind = SymbolKind::InstanceMethod;
279
1.87k
      Info.Lang = SymbolLanguage::CXX;
280
1.87k
      break;
281
15
    }
282
21
    case Decl::ClassTemplate:
283
21
      Info.Kind = SymbolKind::Class;
284
21
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
285
21
      Info.Lang = SymbolLanguage::CXX;
286
21
      break;
287
4
    case Decl::FunctionTemplate:
288
4
      Info.Kind = SymbolKind::Function;
289
4
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
290
4
      Info.Lang = SymbolLanguage::CXX;
291
4
      if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
292
4
                           cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
293
0
        if (isa<CXXConstructorDecl>(MD))
294
0
          Info.Kind = SymbolKind::Constructor;
295
0
        else if (isa<CXXDestructorDecl>(MD))
296
0
          Info.Kind = SymbolKind::Destructor;
297
0
        else if (isa<CXXConversionDecl>(MD))
298
0
          Info.Kind = SymbolKind::ConversionFunction;
299
0
        else {
300
0
          if (MD->isStatic())
301
0
            Info.Kind = SymbolKind::StaticMethod;
302
0
          else
303
0
            Info.Kind = SymbolKind::InstanceMethod;
304
0
        }
305
0
      }
306
4
      break;
307
0
    case Decl::TypeAliasTemplate:
308
0
      Info.Kind = SymbolKind::TypeAlias;
309
0
      Info.Lang = SymbolLanguage::CXX;
310
0
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
311
0
      break;
312
22
    case Decl::TypeAlias:
313
22
      Info.Kind = SymbolKind::TypeAlias;
314
22
      Info.Lang = SymbolLanguage::CXX;
315
22
      break;
316
2
    case Decl::UnresolvedUsingTypename:
317
2
      Info.Kind = SymbolKind::Using;
318
2
      Info.SubKind = SymbolSubKind::UsingTypename;
319
2
      Info.Lang = SymbolLanguage::CXX;
320
2
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
321
2
      break;
322
6
    case Decl::UnresolvedUsingValue:
323
6
      Info.Kind = SymbolKind::Using;
324
6
      Info.SubKind = SymbolSubKind::UsingValue;
325
6
      Info.Lang = SymbolLanguage::CXX;
326
6
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
327
6
      break;
328
2
    case Decl::Using:
329
2
      Info.Kind = SymbolKind::Using;
330
2
      Info.Lang = SymbolLanguage::CXX;
331
2
      break;
332
0
    case Decl::UsingEnum:
333
0
      Info.Kind = SymbolKind::Using;
334
0
      Info.Lang = SymbolLanguage::CXX;
335
0
      Info.SubKind = SymbolSubKind::UsingEnum;
336
0
      break;
337
12
    case Decl::Binding:
338
12
      Info.Kind = SymbolKind::Variable;
339
12
      Info.Lang = SymbolLanguage::CXX;
340
12
      break;
341
3
    case Decl::MSProperty:
342
3
      Info.Kind = SymbolKind::InstanceProperty;
343
3
      if (const CXXRecordDecl *CXXRec =
344
3
              dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
345
3
        if (!CXXRec->isCLike())
346
3
          Info.Lang = SymbolLanguage::CXX;
347
3
      }
348
3
      break;
349
0
    case Decl::ClassTemplatePartialSpecialization:
350
0
    case Decl::ClassScopeFunctionSpecialization:
351
0
    case Decl::ClassTemplateSpecialization:
352
0
    case Decl::CXXRecord:
353
0
    case Decl::Enum:
354
0
    case Decl::Record:
355
0
      llvm_unreachable("records handled before");
356
0
      break;
357
0
    case Decl::VarTemplateSpecialization:
358
0
    case Decl::VarTemplatePartialSpecialization:
359
0
    case Decl::ImplicitParam:
360
0
    case Decl::ParmVar:
361
0
    case Decl::Var:
362
0
    case Decl::VarTemplate:
363
0
      llvm_unreachable("variables handled before");
364
0
      break;
365
3
    case Decl::TemplateTypeParm:
366
3
      Info.Kind = SymbolKind::TemplateTypeParm;
367
3
      break;
368
2
    case Decl::TemplateTemplateParm:
369
2
      Info.Kind = SymbolKind::TemplateTemplateParm;
370
2
      break;
371
2
    case Decl::NonTypeTemplateParm:
372
2
      Info.Kind = SymbolKind::NonTypeTemplateParm;
373
2
      break;
374
2
    case Decl::Concept:
375
2
      Info.Kind = SymbolKind::Concept;
376
2
      break;
377
    // Other decls get the 'unknown' kind.
378
0
    default:
379
0
      break;
380
4.83k
    }
381
4.83k
  }
382
383
5.59k
  if (Info.Kind == SymbolKind::Unknown)
384
0
    return Info;
385
386
5.59k
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
387
3.88k
    if (FD->getTemplatedKind() ==
388
3.88k
          FunctionDecl::TK_FunctionTemplateSpecialization) {
389
12
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
390
12
      Info.Properties |=
391
12
          (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
392
12
    }
393
3.88k
  }
394
395
5.59k
  if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
396
294
    Info.Lang = SymbolLanguage::CXX;
397
398
5.59k
  if (auto *attr = D->getExternalSourceSymbolAttr()) {
399
24
    if (attr->getLanguage() == "Swift")
400
24
      Info.Lang = SymbolLanguage::Swift;
401
24
  }
402
403
5.59k
  return Info;
404
5.59k
}
405
406
18.7k
SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
407
18.7k
  SymbolInfo Info;
408
18.7k
  Info.Kind = SymbolKind::Macro;
409
18.7k
  Info.SubKind = SymbolSubKind::None;
410
18.7k
  Info.Properties = SymbolPropertySet();
411
18.7k
  Info.Lang = SymbolLanguage::C;
412
18.7k
  return Info;
413
18.7k
}
414
415
bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
416
20.8k
                                   llvm::function_ref<bool(SymbolRole)> Fn) {
417
20.8k
#define APPLY_FOR_ROLE(Role) \
418
437k
  if (Roles & (unsigned)SymbolRole::Role) \
419
437k
    
if (22.6k
!Fn(SymbolRole::Role)22.6k
) \
420
22.6k
      
return false7
;
421
422
20.8k
  APPLY_FOR_ROLE(Declaration);
423
20.8k
  APPLY_FOR_ROLE(Definition);
424
20.8k
  APPLY_FOR_ROLE(Reference);
425
20.8k
  APPLY_FOR_ROLE(Read);
426
20.8k
  APPLY_FOR_ROLE(Write);
427
20.8k
  APPLY_FOR_ROLE(Call);
428
20.8k
  APPLY_FOR_ROLE(Dynamic);
429
20.8k
  APPLY_FOR_ROLE(AddressOf);
430
20.8k
  APPLY_FOR_ROLE(Implicit);
431
20.8k
  APPLY_FOR_ROLE(Undefinition);
432
20.8k
  APPLY_FOR_ROLE(RelationChildOf);
433
20.8k
  APPLY_FOR_ROLE(RelationBaseOf);
434
20.8k
  APPLY_FOR_ROLE(RelationOverrideOf);
435
20.8k
  APPLY_FOR_ROLE(RelationReceivedBy);
436
20.8k
  APPLY_FOR_ROLE(RelationCalledBy);
437
20.8k
  APPLY_FOR_ROLE(RelationExtendedBy);
438
20.8k
  APPLY_FOR_ROLE(RelationAccessorOf);
439
20.8k
  APPLY_FOR_ROLE(RelationContainedBy);
440
20.8k
  APPLY_FOR_ROLE(RelationIBTypeOf);
441
20.8k
  APPLY_FOR_ROLE(RelationSpecializationOf);
442
20.8k
  APPLY_FOR_ROLE(NameReference);
443
444
20.8k
#undef APPLY_FOR_ROLE
445
446
20.8k
  return true;
447
20.8k
}
448
449
void index::applyForEachSymbolRole(SymbolRoleSet Roles,
450
20.8k
                                   llvm::function_ref<void(SymbolRole)> Fn) {
451
22.6k
  applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
452
22.6k
    Fn(r);
453
22.6k
    return true;
454
22.6k
  });
455
20.8k
}
456
457
20.8k
void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
458
20.8k
  bool VisitedOnce = false;
459
22.6k
  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
460
22.6k
    if (VisitedOnce)
461
1.85k
      OS << ',';
462
20.8k
    else
463
20.8k
      VisitedOnce = true;
464
22.6k
    switch (Role) {
465
302
    case SymbolRole::Declaration: OS << "Decl"; break;
466
18.7k
    case SymbolRole::Definition: OS << "Def"; break;
467
647
    case SymbolRole::Reference: OS << "Ref"; break;
468
39
    case SymbolRole::Read: OS << "Read"; break;
469
16
    case SymbolRole::Write: OS << "Writ"; break;
470
75
    case SymbolRole::Call: OS << "Call"; break;
471
168
    case SymbolRole::Dynamic: OS << "Dyn"; break;
472
0
    case SymbolRole::AddressOf: OS << "Addr"; break;
473
116
    case SymbolRole::Implicit: OS << "Impl"; break;
474
4
    case SymbolRole::Undefinition: OS << "Undef"; break;
475
936
    case SymbolRole::RelationChildOf: OS << "RelChild"; break;
476
102
    case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
477
0
    case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
478
68
    case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
479
138
    case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
480
10
    case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
481
130
    case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
482
1.05k
    case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
483
10
    case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
484
140
    case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
485
22
    case SymbolRole::NameReference: OS << "NameReference"; break;
486
22.6k
    }
487
22.6k
  });
488
20.8k
}
489
490
bool index::printSymbolName(const Decl *D, const LangOptions &LO,
491
2.54k
                            raw_ostream &OS) {
492
2.54k
  if (auto *ND = dyn_cast<NamedDecl>(D)) {
493
2.54k
    PrintingPolicy Policy(LO);
494
    // Forward references can have different template argument names. Suppress
495
    // the template argument names in constructors to make their name more
496
    // stable.
497
2.54k
    Policy.SuppressTemplateArgsInCXXConstructors = true;
498
2.54k
    DeclarationName DeclName = ND->getDeclName();
499
2.54k
    if (DeclName.isEmpty())
500
8
      return true;
501
2.53k
    DeclName.print(OS, Policy);
502
2.53k
    return false;
503
2.54k
  } else {
504
0
    return true;
505
0
  }
506
2.54k
}
507
508
19.6k
StringRef index::getSymbolKindString(SymbolKind K) {
509
19.6k
  switch (K) {
510
0
  case SymbolKind::Unknown: return "<unknown>";
511
9
  case SymbolKind::Module: return "module";
512
39
  case SymbolKind::Namespace: return "namespace";
513
10
  case SymbolKind::NamespaceAlias: return "namespace-alias";
514
18.2k
  case SymbolKind::Macro: return "macro";
515
21
  case SymbolKind::Enum: return "enum";
516
182
  case SymbolKind::Struct: return "struct";
517
348
  case SymbolKind::Class: return "class";
518
47
  case SymbolKind::Protocol: return "protocol";
519
8
  case SymbolKind::Extension: return "extension";
520
4
  case SymbolKind::Union: return "union";
521
67
  case SymbolKind::TypeAlias: return "type-alias";
522
95
  case SymbolKind::Function: return "function";
523
69
  case SymbolKind::Variable: return "variable";
524
80
  case SymbolKind::Field: return "field";
525
19
  case SymbolKind::EnumConstant: return "enumerator";
526
199
  case SymbolKind::InstanceMethod: return "instance-method";
527
28
  case SymbolKind::ClassMethod: return "class-method";
528
10
  case SymbolKind::StaticMethod: return "static-method";
529
56
  case SymbolKind::InstanceProperty: return "instance-property";
530
0
  case SymbolKind::ClassProperty: return "class-property";
531
27
  case SymbolKind::StaticProperty: return "static-property";
532
27
  case SymbolKind::Constructor: return "constructor";
533
5
  case SymbolKind::Destructor: return "destructor";
534
0
  case SymbolKind::ConversionFunction: return "conversion-func";
535
25
  case SymbolKind::Parameter: return "param";
536
9
  case SymbolKind::Using: return "using";
537
0
  case SymbolKind::TemplateTypeParm: return "template-type-param";
538
0
  case SymbolKind::TemplateTemplateParm: return "template-template-param";
539
0
  case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
540
0
  case SymbolKind::Concept:
541
0
    return "concept";
542
19.6k
  }
543
0
  llvm_unreachable("invalid symbol kind");
544
0
}
545
546
113
StringRef index::getSymbolSubKindString(SymbolSubKind K) {
547
113
  switch (K) {
548
0
  case SymbolSubKind::None: return "<none>";
549
4
  case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
550
2
  case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
551
53
  case SymbolSubKind::AccessorGetter: return "acc-get";
552
46
  case SymbolSubKind::AccessorSetter: return "acc-set";
553
2
  case SymbolSubKind::UsingTypename: return "using-typename";
554
6
  case SymbolSubKind::UsingValue: return "using-value";
555
0
  case SymbolSubKind::UsingEnum:
556
0
    return "using-enum";
557
113
  }
558
0
  llvm_unreachable("invalid symbol subkind");
559
0
}
560
561
19.6k
StringRef index::getSymbolLanguageString(SymbolLanguage K) {
562
19.6k
  switch (K) {
563
18.5k
  case SymbolLanguage::C: return "C";
564
427
  case SymbolLanguage::ObjC: return "ObjC";
565
614
  case SymbolLanguage::CXX: return "C++";
566
24
  case SymbolLanguage::Swift: return "Swift";
567
19.6k
  }
568
0
  llvm_unreachable("invalid symbol language kind");
569
0
}
570
571
void index::applyForEachSymbolProperty(SymbolPropertySet Props,
572
316
                                  llvm::function_ref<void(SymbolProperty)> Fn) {
573
316
#define APPLY_FOR_PROPERTY(K)                                                  \
574
2.84k
  if (Props & (SymbolPropertySet)SymbolProperty::K)                            \
575
2.84k
  
Fn(SymbolProperty::K)392
576
577
316
  APPLY_FOR_PROPERTY(Generic);
578
316
  APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
579
316
  APPLY_FOR_PROPERTY(TemplateSpecialization);
580
316
  APPLY_FOR_PROPERTY(UnitTest);
581
316
  APPLY_FOR_PROPERTY(IBAnnotated);
582
316
  APPLY_FOR_PROPERTY(IBOutletCollection);
583
316
  APPLY_FOR_PROPERTY(GKInspectable);
584
316
  APPLY_FOR_PROPERTY(Local);
585
316
  APPLY_FOR_PROPERTY(ProtocolInterface);
586
587
316
#undef APPLY_FOR_PROPERTY
588
316
}
589
590
316
void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
591
316
  bool VisitedOnce = false;
592
392
  applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
593
392
    if (VisitedOnce)
594
76
      OS << ',';
595
316
    else
596
316
      VisitedOnce = true;
597
392
    switch (Prop) {
598
248
    case SymbolProperty::Generic: OS << "Gen"; break;
599
16
    case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
600
55
    case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
601
12
    case SymbolProperty::UnitTest: OS << "test"; break;
602
9
    case SymbolProperty::IBAnnotated: OS << "IB"; break;
603
5
    case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
604
1
    case SymbolProperty::GKInspectable: OS << "GKI"; break;
605
41
    case SymbolProperty::Local: OS << "local"; break;
606
5
    case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
607
392
    }
608
392
  });
609
316
}