Coverage Report

Created: 2022-01-18 06:27

/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.82k
  if (isa<ObjCTypeParamDecl>(D))
60
0
    return true;
61
62
8.82k
  if (isa<UsingDirectiveDecl>(D))
63
2
    return false;
64
8.82k
  if (!D->getParentFunctionOrMethod())
65
8.62k
    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.58k
SymbolInfo index::getSymbolInfo(const Decl *D) {
86
5.58k
  assert(D);
87
0
  SymbolInfo Info;
88
5.58k
  Info.Kind = SymbolKind::Unknown;
89
5.58k
  Info.SubKind = SymbolSubKind::None;
90
5.58k
  Info.Properties = SymbolPropertySet();
91
5.58k
  Info.Lang = SymbolLanguage::C;
92
93
5.58k
  if (isFunctionLocalSymbol(D)) {
94
104
    Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
95
104
  }
96
5.58k
  if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
97
7
    Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
98
7
  }
99
100
5.58k
  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.58k
  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.82k
  } else {
169
4.82k
    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
56
    case Decl::Namespace:
248
56
      Info.Kind = SymbolKind::Namespace;
249
56
      Info.Lang = SymbolLanguage::CXX;
250
56
      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
2
    case Decl::FunctionTemplate:
288
2
      Info.Kind = SymbolKind::Function;
289
2
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
290
2
      Info.Lang = SymbolLanguage::CXX;
291
2
      if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
292
2
                           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
2
      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
1
    case Decl::Using:
329
1
      Info.Kind = SymbolKind::Using;
330
1
      Info.Lang = SymbolLanguage::CXX;
331
1
      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
    // Other decls get the 'unknown' kind.
375
0
    default:
376
0
      break;
377
4.82k
    }
378
4.82k
  }
379
380
5.58k
  if (Info.Kind == SymbolKind::Unknown)
381
0
    return Info;
382
383
5.58k
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
384
3.88k
    if (FD->getTemplatedKind() ==
385
3.88k
          FunctionDecl::TK_FunctionTemplateSpecialization) {
386
12
      Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
387
12
      Info.Properties |=
388
12
          (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
389
12
    }
390
3.88k
  }
391
392
5.58k
  if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
393
292
    Info.Lang = SymbolLanguage::CXX;
394
395
5.58k
  if (auto *attr = D->getExternalSourceSymbolAttr()) {
396
24
    if (attr->getLanguage() == "Swift")
397
24
      Info.Lang = SymbolLanguage::Swift;
398
24
  }
399
400
5.58k
  return Info;
401
5.58k
}
402
403
17.3k
SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
404
17.3k
  SymbolInfo Info;
405
17.3k
  Info.Kind = SymbolKind::Macro;
406
17.3k
  Info.SubKind = SymbolSubKind::None;
407
17.3k
  Info.Properties = SymbolPropertySet();
408
17.3k
  Info.Lang = SymbolLanguage::C;
409
17.3k
  return Info;
410
17.3k
}
411
412
bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
413
19.4k
                                   llvm::function_ref<bool(SymbolRole)> Fn) {
414
19.4k
#define APPLY_FOR_ROLE(Role) \
415
408k
  if (Roles & (unsigned)SymbolRole::Role) \
416
408k
    
if (21.3k
!Fn(SymbolRole::Role)21.3k
) \
417
21.3k
      
return false7
;
418
419
19.4k
  APPLY_FOR_ROLE(Declaration);
420
19.4k
  APPLY_FOR_ROLE(Definition);
421
19.4k
  APPLY_FOR_ROLE(Reference);
422
19.4k
  APPLY_FOR_ROLE(Read);
423
19.4k
  APPLY_FOR_ROLE(Write);
424
19.4k
  APPLY_FOR_ROLE(Call);
425
19.4k
  APPLY_FOR_ROLE(Dynamic);
426
19.4k
  APPLY_FOR_ROLE(AddressOf);
427
19.4k
  APPLY_FOR_ROLE(Implicit);
428
19.4k
  APPLY_FOR_ROLE(Undefinition);
429
19.4k
  APPLY_FOR_ROLE(RelationChildOf);
430
19.4k
  APPLY_FOR_ROLE(RelationBaseOf);
431
19.4k
  APPLY_FOR_ROLE(RelationOverrideOf);
432
19.4k
  APPLY_FOR_ROLE(RelationReceivedBy);
433
19.4k
  APPLY_FOR_ROLE(RelationCalledBy);
434
19.4k
  APPLY_FOR_ROLE(RelationExtendedBy);
435
19.4k
  APPLY_FOR_ROLE(RelationAccessorOf);
436
19.4k
  APPLY_FOR_ROLE(RelationContainedBy);
437
19.4k
  APPLY_FOR_ROLE(RelationIBTypeOf);
438
19.4k
  APPLY_FOR_ROLE(RelationSpecializationOf);
439
19.4k
  APPLY_FOR_ROLE(NameReference);
440
441
19.4k
#undef APPLY_FOR_ROLE
442
443
19.4k
  return true;
444
19.4k
}
445
446
void index::applyForEachSymbolRole(SymbolRoleSet Roles,
447
19.4k
                                   llvm::function_ref<void(SymbolRole)> Fn) {
448
21.2k
  applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
449
21.2k
    Fn(r);
450
21.2k
    return true;
451
21.2k
  });
452
19.4k
}
453
454
19.4k
void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
455
19.4k
  bool VisitedOnce = false;
456
21.2k
  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
457
21.2k
    if (VisitedOnce)
458
1.85k
      OS << ',';
459
19.4k
    else
460
19.4k
      VisitedOnce = true;
461
21.2k
    switch (Role) {
462
300
    case SymbolRole::Declaration: OS << "Decl"; break;
463
17.3k
    case SymbolRole::Definition: OS << "Def"; break;
464
646
    case SymbolRole::Reference: OS << "Ref"; break;
465
39
    case SymbolRole::Read: OS << "Read"; break;
466
16
    case SymbolRole::Write: OS << "Writ"; break;
467
75
    case SymbolRole::Call: OS << "Call"; break;
468
168
    case SymbolRole::Dynamic: OS << "Dyn"; break;
469
0
    case SymbolRole::AddressOf: OS << "Addr"; break;
470
116
    case SymbolRole::Implicit: OS << "Impl"; break;
471
4
    case SymbolRole::Undefinition: OS << "Undef"; break;
472
936
    case SymbolRole::RelationChildOf: OS << "RelChild"; break;
473
102
    case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
474
0
    case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
475
68
    case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
476
138
    case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
477
10
    case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
478
130
    case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
479
1.05k
    case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
480
10
    case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
481
140
    case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
482
22
    case SymbolRole::NameReference: OS << "NameReference"; break;
483
21.2k
    }
484
21.2k
  });
485
19.4k
}
486
487
bool index::printSymbolName(const Decl *D, const LangOptions &LO,
488
2.54k
                            raw_ostream &OS) {
489
2.54k
  if (auto *ND = dyn_cast<NamedDecl>(D)) {
490
2.54k
    PrintingPolicy Policy(LO);
491
    // Forward references can have different template argument names. Suppress
492
    // the template argument names in constructors to make their name more
493
    // stable.
494
2.54k
    Policy.SuppressTemplateArgsInCXXConstructors = true;
495
2.54k
    DeclarationName DeclName = ND->getDeclName();
496
2.54k
    if (DeclName.isEmpty())
497
8
      return true;
498
2.53k
    DeclName.print(OS, Policy);
499
2.53k
    return false;
500
2.54k
  } else {
501
0
    return true;
502
0
  }
503
2.54k
}
504
505
18.2k
StringRef index::getSymbolKindString(SymbolKind K) {
506
18.2k
  switch (K) {
507
0
  case SymbolKind::Unknown: return "<unknown>";
508
9
  case SymbolKind::Module: return "module";
509
37
  case SymbolKind::Namespace: return "namespace";
510
10
  case SymbolKind::NamespaceAlias: return "namespace-alias";
511
16.8k
  case SymbolKind::Macro: return "macro";
512
21
  case SymbolKind::Enum: return "enum";
513
182
  case SymbolKind::Struct: return "struct";
514
348
  case SymbolKind::Class: return "class";
515
47
  case SymbolKind::Protocol: return "protocol";
516
8
  case SymbolKind::Extension: return "extension";
517
4
  case SymbolKind::Union: return "union";
518
67
  case SymbolKind::TypeAlias: return "type-alias";
519
95
  case SymbolKind::Function: return "function";
520
69
  case SymbolKind::Variable: return "variable";
521
80
  case SymbolKind::Field: return "field";
522
19
  case SymbolKind::EnumConstant: return "enumerator";
523
199
  case SymbolKind::InstanceMethod: return "instance-method";
524
28
  case SymbolKind::ClassMethod: return "class-method";
525
10
  case SymbolKind::StaticMethod: return "static-method";
526
56
  case SymbolKind::InstanceProperty: return "instance-property";
527
0
  case SymbolKind::ClassProperty: return "class-property";
528
27
  case SymbolKind::StaticProperty: return "static-property";
529
27
  case SymbolKind::Constructor: return "constructor";
530
5
  case SymbolKind::Destructor: return "destructor";
531
0
  case SymbolKind::ConversionFunction: return "conversion-func";
532
25
  case SymbolKind::Parameter: return "param";
533
8
  case SymbolKind::Using: return "using";
534
0
  case SymbolKind::TemplateTypeParm: return "template-type-param";
535
0
  case SymbolKind::TemplateTemplateParm: return "template-template-param";
536
0
  case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
537
18.2k
  }
538
0
  llvm_unreachable("invalid symbol kind");
539
0
}
540
541
113
StringRef index::getSymbolSubKindString(SymbolSubKind K) {
542
113
  switch (K) {
543
0
  case SymbolSubKind::None: return "<none>";
544
4
  case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
545
2
  case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
546
53
  case SymbolSubKind::AccessorGetter: return "acc-get";
547
46
  case SymbolSubKind::AccessorSetter: return "acc-set";
548
2
  case SymbolSubKind::UsingTypename: return "using-typename";
549
6
  case SymbolSubKind::UsingValue: return "using-value";
550
0
  case SymbolSubKind::UsingEnum:
551
0
    return "using-enum";
552
113
  }
553
0
  llvm_unreachable("invalid symbol subkind");
554
0
}
555
556
18.2k
StringRef index::getSymbolLanguageString(SymbolLanguage K) {
557
18.2k
  switch (K) {
558
17.2k
  case SymbolLanguage::C: return "C";
559
427
  case SymbolLanguage::ObjC: return "ObjC";
560
611
  case SymbolLanguage::CXX: return "C++";
561
24
  case SymbolLanguage::Swift: return "Swift";
562
18.2k
  }
563
0
  llvm_unreachable("invalid symbol language kind");
564
0
}
565
566
void index::applyForEachSymbolProperty(SymbolPropertySet Props,
567
316
                                  llvm::function_ref<void(SymbolProperty)> Fn) {
568
316
#define APPLY_FOR_PROPERTY(K)                                                  \
569
2.84k
  if (Props & (SymbolPropertySet)SymbolProperty::K)                            \
570
2.84k
  
Fn(SymbolProperty::K)392
571
572
316
  APPLY_FOR_PROPERTY(Generic);
573
316
  APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
574
316
  APPLY_FOR_PROPERTY(TemplateSpecialization);
575
316
  APPLY_FOR_PROPERTY(UnitTest);
576
316
  APPLY_FOR_PROPERTY(IBAnnotated);
577
316
  APPLY_FOR_PROPERTY(IBOutletCollection);
578
316
  APPLY_FOR_PROPERTY(GKInspectable);
579
316
  APPLY_FOR_PROPERTY(Local);
580
316
  APPLY_FOR_PROPERTY(ProtocolInterface);
581
582
316
#undef APPLY_FOR_PROPERTY
583
316
}
584
585
316
void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
586
316
  bool VisitedOnce = false;
587
392
  applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
588
392
    if (VisitedOnce)
589
76
      OS << ',';
590
316
    else
591
316
      VisitedOnce = true;
592
392
    switch (Prop) {
593
248
    case SymbolProperty::Generic: OS << "Gen"; break;
594
16
    case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
595
55
    case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
596
12
    case SymbolProperty::UnitTest: OS << "test"; break;
597
9
    case SymbolProperty::IBAnnotated: OS << "IB"; break;
598
5
    case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
599
1
    case SymbolProperty::GKInspectable: OS << "GKI"; break;
600
41
    case SymbolProperty::Local: OS << "local"; break;
601
5
    case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
602
392
    }
603
392
  });
604
316
}