Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/IdentifierTable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- IdentifierTable.cpp - Hash table for identifier lookup -------------===//
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 implements the IdentifierInfo, IdentifierVisitor, and
10
// IdentifierTable interfaces.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Basic/IdentifierTable.h"
15
#include "clang/Basic/CharInfo.h"
16
#include "clang/Basic/LangOptions.h"
17
#include "clang/Basic/OperatorKinds.h"
18
#include "clang/Basic/Specifiers.h"
19
#include "clang/Basic/TargetBuiltins.h"
20
#include "clang/Basic/TokenKinds.h"
21
#include "llvm/ADT/DenseMapInfo.h"
22
#include "llvm/ADT/FoldingSet.h"
23
#include "llvm/ADT/SmallString.h"
24
#include "llvm/ADT/StringMap.h"
25
#include "llvm/ADT/StringRef.h"
26
#include "llvm/Support/Allocator.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Support/raw_ostream.h"
29
#include <cassert>
30
#include <cstdio>
31
#include <cstring>
32
#include <string>
33
34
using namespace clang;
35
36
// A check to make sure the ObjCOrBuiltinID has sufficient room to store the
37
// largest possible target/aux-target combination. If we exceed this, we likely
38
// need to just change the ObjCOrBuiltinIDBits value in IdentifierTable.h.
39
static_assert(2 * LargestBuiltinID < (2 << (ObjCOrBuiltinIDBits - 1)),
40
              "Insufficient ObjCOrBuiltinID Bits");
41
42
//===----------------------------------------------------------------------===//
43
// IdentifierTable Implementation
44
//===----------------------------------------------------------------------===//
45
46
374
IdentifierIterator::~IdentifierIterator() = default;
47
48
12.6k
IdentifierInfoLookup::~IdentifierInfoLookup() = default;
49
50
namespace {
51
52
/// A simple identifier lookup iterator that represents an
53
/// empty sequence of identifiers.
54
class EmptyLookupIterator : public IdentifierIterator
55
{
56
public:
57
0
  StringRef Next() override { return StringRef(); }
58
};
59
60
} // namespace
61
62
0
IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
63
0
  return new EmptyLookupIterator();
64
0
}
65
66
IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup)
67
    : HashTable(8192), // Start with space for 8K identifiers.
68
152k
      ExternalLookup(ExternalLookup) {}
69
70
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
71
                                 IdentifierInfoLookup *ExternalLookup)
72
64.6k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
64.6k
  AddKeywords(LangOpts);
76
64.6k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
72
64.6k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
64.6k
  AddKeywords(LangOpts);
76
64.6k
}
77
78
//===----------------------------------------------------------------------===//
79
// Language Keyword Implementation
80
//===----------------------------------------------------------------------===//
81
82
// Constants for TokenKinds.def
83
namespace {
84
85
  enum {
86
    KEYC99        = 0x1,
87
    KEYCXX        = 0x2,
88
    KEYCXX11      = 0x4,
89
    KEYGNU        = 0x8,
90
    KEYMS         = 0x10,
91
    BOOLSUPPORT   = 0x20,
92
    KEYALTIVEC    = 0x40,
93
    KEYNOCXX      = 0x80,
94
    KEYBORLAND    = 0x100,
95
    KEYOPENCLC    = 0x200,
96
    KEYC11        = 0x400,
97
    KEYNOMS18     = 0x800,
98
    KEYNOOPENCL   = 0x1000,
99
    WCHARSUPPORT  = 0x2000,
100
    HALFSUPPORT   = 0x4000,
101
    CHAR8SUPPORT  = 0x8000,
102
    KEYCONCEPTS   = 0x10000,
103
    KEYOBJC       = 0x20000,
104
    KEYZVECTOR    = 0x40000,
105
    KEYCOROUTINES = 0x80000,
106
    KEYMODULES    = 0x100000,
107
    KEYCXX20      = 0x200000,
108
    KEYOPENCLCXX  = 0x400000,
109
    KEYMSCOMPAT   = 0x800000,
110
    KEYSYCL       = 0x1000000,
111
    KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX20,
112
    KEYALL = (0x1ffffff & ~KEYNOMS18 &
113
              ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
114
  };
115
116
  /// How a keyword is treated in the selected standard.
117
  enum KeywordStatus {
118
    KS_Disabled,    // Disabled
119
    KS_Extension,   // Is an extension
120
    KS_Enabled,     // Enabled
121
    KS_Future       // Is a keyword in future standard
122
  };
123
124
} // namespace
125
126
/// Translates flags as specified in TokenKinds.def into keyword status
127
/// in the given language standard.
128
static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
129
51.2M
                                      unsigned Flags) {
130
51.2M
  if (Flags == KEYALL) 
return KS_Enabled16.7M
;
131
34.4M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)29.2M
)
return KS_Enabled12.9M
;
132
21.4M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)14.2M
)
return KS_Enabled1.14M
;
133
20.3M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCXX20)6.20M
)
return KS_Enabled376k
;
134
19.9M
  if (LangOpts.C99 && 
(Flags & KEYC99)10.1M
)
return KS_Enabled113k
;
135
19.8M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)14.3M
)
return KS_Extension134k
;
136
19.6M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)6.99M
)
return KS_Extension2.89M
;
137
16.7M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)1.03M
)
return KS_Enabled4.24k
;
138
16.7M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)811
)
return KS_Extension54
;
139
16.7M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)11.8M
)
return KS_Enabled390k
;
140
16.3M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)127k
)
return KS_Enabled659
;
141
16.3M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)8.52M
)
return KS_Enabled80.0k
;
142
16.3M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)3.19M
)
return KS_Enabled53.8k
;
143
16.2M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)50.1k
)
return KS_Enabled1.07k
;
144
16.2M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)1.86k
)
return KS_Enabled33
;
145
16.2M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus127k
&&
(Flags & KEYOPENCLC)111k
)
146
16.8k
    return KS_Enabled;
147
16.2M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)16.3k
)
return KS_Enabled4.91k
;
148
16.2M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)5.00M
)
return KS_Enabled114k
;
149
16.1M
  if (LangOpts.C11 && 
(Flags & KEYC11)4.04M
)
return KS_Enabled0
;
150
  // We treat bridge casts as objective-C keywords so we can warn on them
151
  // in non-arc mode.
152
16.1M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)6.92M
)
return KS_Enabled566k
;
153
15.5M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCONCEPTS)2.80M
)
return KS_Enabled0
;
154
15.5M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)372k
)
return KS_Enabled180
;
155
15.5M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)15.4k
)
return KS_Enabled280
;
156
15.5M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)10.7M
)
return KS_Future687k
;
157
14.8M
  if (LangOpts.CPlusPlus && 
!LangOpts.CPlusPlus2010.0M
&&
(Flags & CHAR8SUPPORT)7.22M
)
158
76.0k
    return KS_Future;
159
14.7M
  if (LangOpts.isSYCL() && 
(Flags & KEYSYCL)3.89k
)
160
37
    return KS_Enabled;
161
14.7M
  return KS_Disabled;
162
14.7M
}
163
164
/// AddKeyword - This method is used to associate a token ID with specific
165
/// identifiers because they are language keywords.  This causes the lexer to
166
/// automatically map matching identifiers to specialized token codes.
167
static void AddKeyword(StringRef Keyword,
168
                       tok::TokenKind TokenCode, unsigned Flags,
169
50.7M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
170
50.7M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
171
172
  // Don't add this keyword under MSVCCompat.
173
50.7M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)4.15M
&&
174
50.7M
      
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)24.9k
)
175
5.76k
    return;
176
177
  // Don't add this keyword under OpenCL.
178
50.7M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)219k
)
179
661
    return;
180
181
  // Don't add this keyword if disabled in this language.
182
50.7M
  if (AddResult == KS_Disabled) 
return14.6M
;
183
184
36.0M
  IdentifierInfo &Info =
185
36.0M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier760k
:
TokenCode35.2M
);
186
36.0M
  Info.setIsExtensionToken(AddResult == KS_Extension);
187
36.0M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
188
36.0M
}
189
190
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
191
/// representations.
192
static void AddCXXOperatorKeyword(StringRef Keyword,
193
                                  tok::TokenKind TokenCode,
194
1.12M
                                  IdentifierTable &Table) {
195
1.12M
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
196
1.12M
  Info.setIsCPlusPlusOperatorKeyword();
197
1.12M
}
198
199
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
200
/// or "property".
201
static void AddObjCKeyword(StringRef Name,
202
                           tok::ObjCKeywordKind ObjCID,
203
2.18M
                           IdentifierTable &Table) {
204
2.18M
  Table.get(Name).setObjCKeywordID(ObjCID);
205
2.18M
}
206
207
/// AddKeywords - Add all keywords to the symbol table.
208
///
209
152k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
210
  // Add keywords and tokens for the current language.
211
152k
#define KEYWORD(NAME, FLAGS) \
212
42.1M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
213
42.1M
             FLAGS, LangOpts, *this);
214
152k
#define ALIAS(NAME, TOK, FLAGS) \
215
8.54M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
216
8.54M
             FLAGS, LangOpts, *this);
217
152k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
218
1.67M
  if (LangOpts.CXXOperatorNames)          \
219
1.67M
    
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this)1.12M
;
220
152k
#define OBJC_AT_KEYWORD(NAME)  \
221
4.11M
  if (LangOpts.ObjC)           \
222
4.11M
    
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this)2.18M
;
223
152k
#define TESTING_KEYWORD(NAME, FLAGS)
224
152k
#include "clang/Basic/TokenKinds.def"
225
226
152k
  if (LangOpts.ParseUnknownAnytype)
227
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
228
9
               LangOpts, *this);
229
230
152k
  if (LangOpts.DeclSpecKeyword)
231
63.3k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
232
233
152k
  if (LangOpts.IEEE128)
234
3.99k
    AddKeyword("__ieee128", tok::kw___float128, KEYALL, LangOpts, *this);
235
236
  // Add the 'import' contextual keyword.
237
152k
  get("import").setModulesImport(true);
238
152k
}
239
240
/// Checks if the specified token kind represents a keyword in the
241
/// specified language.
242
/// \returns Status of the keyword in the language.
243
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
244
3.65M
                                      tok::TokenKind K) {
245
3.65M
  switch (K) {
246
0
#define KEYWORD(NAME, FLAGS) \
247
513k
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
248
0
#include "clang/Basic/TokenKinds.def"
249
3.14M
  default: return KS_Disabled;
250
3.65M
  }
251
3.65M
}
252
253
/// Returns true if the identifier represents a keyword in the
254
/// specified language.
255
3.65M
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
256
3.65M
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
257
369k
  case KS_Enabled:
258
376k
  case KS_Extension:
259
376k
    return true;
260
3.27M
  default:
261
3.27M
    return false;
262
3.65M
  }
263
3.65M
}
264
265
/// Returns true if the identifier represents a C++ keyword in the
266
/// specified language.
267
376k
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
268
376k
  if (!LangOpts.CPlusPlus || 
!isKeyword(LangOpts)376k
)
269
124k
    return false;
270
  // This is a C++ keyword if this identifier is not a keyword when checked
271
  // using LangOptions without C++ support.
272
251k
  LangOptions LangOptsNoCPP = LangOpts;
273
251k
  LangOptsNoCPP.CPlusPlus = false;
274
251k
  LangOptsNoCPP.CPlusPlus11 = false;
275
251k
  LangOptsNoCPP.CPlusPlus20 = false;
276
251k
  return !isKeyword(LangOptsNoCPP);
277
376k
}
278
279
ReservedIdentifierStatus
280
51.7M
IdentifierInfo::isReserved(const LangOptions &LangOpts) const {
281
51.7M
  StringRef Name = getName();
282
283
  // '_' is a reserved identifier, but its use is so common (e.g. to store
284
  // ignored values) that we don't warn on it.
285
51.7M
  if (Name.size() <= 1)
286
582k
    return ReservedIdentifierStatus::NotReserved;
287
288
  // [lex.name] p3
289
51.1M
  if (Name[0] == '_') {
290
291
    // Each name that begins with an underscore followed by an uppercase letter
292
    // or another underscore is reserved.
293
12.8M
    if (Name[1] == '_')
294
8.93M
      return ReservedIdentifierStatus::StartsWithDoubleUnderscore;
295
296
3.87M
    if ('A' <= Name[1] && 
Name[1] <= 'Z'3.86M
)
297
2.57M
      return ReservedIdentifierStatus::
298
2.57M
          StartsWithUnderscoreFollowedByCapitalLetter;
299
300
    // This is a bit misleading: it actually means it's only reserved if we're
301
    // at global scope because it starts with an underscore.
302
1.29M
    return ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope;
303
3.87M
  }
304
305
  // Each name that contains a double underscore (__) is reserved.
306
38.3M
  if (LangOpts.CPlusPlus && 
Name.contains("__")15.1M
)
307
29.5k
    return ReservedIdentifierStatus::ContainsDoubleUnderscore;
308
309
38.2M
  return ReservedIdentifierStatus::NotReserved;
310
38.3M
}
311
312
103M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
313
  // We use a perfect hash function here involving the length of the keyword,
314
  // the first and third character.  For preprocessor ID's there are no
315
  // collisions (if there were, the switch below would complain about duplicate
316
  // case values).  Note that this depends on 'if' being null terminated.
317
318
103M
#define HASH(LEN, FIRST, THIRD) \
319
166M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
320
103M
#define CASE(LEN, FIRST, THIRD, NAME) \
321
103M
  
case 62.2M
HASH62.2M
(LEN, FIRST, THIRD): \
322
62.2M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword626k
:
tok::pp_61.6M
## NAME
323
324
103M
  unsigned Len = getLength();
325
103M
  if (Len < 2) 
return tok::pp_not_keyword95.6k
;
326
103M
  const char *Name = getNameStart();
327
103M
  switch (HASH(Len, Name[0], Name[2])) {
328
41.4M
  default: return tok::pp_not_keyword;
329
2.23M
  CASE
(0
2, 'i', '\0', if);
330
262k
  CASE
(0
4, 'e', 'i', elif);
331
1.25M
  CASE
(0
4, 'e', 's', else);
332
58.5k
  CASE
(0
4, 'l', 'n', line);
333
5.22k
  CASE
(0
4, 's', 'c', sccs);
334
5.89M
  CASE
(0
5, 'e', 'd', endif);
335
12.5k
  CASE
(0
5, 'e', 'r', error);
336
3.08k
  CASE
(0
5, 'i', 'e', ident);
337
1.24M
  CASE
(0
5, 'i', 'd', ifdef);
338
122k
  CASE
(0
5, 'u', 'd', undef);
339
340
32.2k
  CASE
(0
6, 'a', 's', assert);
341
41.7M
  CASE
(0
6, 'd', 'f', define);
342
4.84M
  CASE
(0
6, 'i', 'n', ifndef);
343
281k
  CASE
(0
6, 'i', 'p', import);
344
698k
  CASE
(0
6, 'p', 'a', pragma);
345
346
10.7k
  CASE
(0
7, 'd', 'f', defined);
347
8.87k
  CASE
(0
7, 'e', 'i', elifdef);
348
3.18M
  CASE
(0
7, 'i', 'c', include);
349
1.33k
  CASE
(0
7, 'w', 'r', warning);
350
351
34.4k
  CASE
(0
8, 'e', 'i', elifndef);
352
5.59k
  CASE
(0
8, 'u', 'a', unassert);
353
40.1k
  CASE
(0
12, 'i', 'c', include_next);
354
355
5.01k
  CASE
(0
14, '_', 'p', __public_macro);
356
357
85.4k
  CASE
(0
15, '_', 'p', __private_macro);
358
359
103M
  
CASE231k
(16, '_', 'i', __include_macros);
360
103M
#undef CASE
361
103M
#undef HASH
362
103M
  }
363
103M
}
364
365
//===----------------------------------------------------------------------===//
366
// Stats Implementation
367
//===----------------------------------------------------------------------===//
368
369
/// PrintStats - Print statistics about how well the identifier table is doing
370
/// at hashing identifiers.
371
4
void IdentifierTable::PrintStats() const {
372
4
  unsigned NumBuckets = HashTable.getNumBuckets();
373
4
  unsigned NumIdentifiers = HashTable.getNumItems();
374
4
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
375
4
  unsigned AverageIdentifierSize = 0;
376
4
  unsigned MaxIdentifierLength = 0;
377
378
  // TODO: Figure out maximum times an identifier had to probe for -stats.
379
4
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
380
7.87k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I7.87k
) {
381
7.87k
    unsigned IdLen = I->getKeyLength();
382
7.87k
    AverageIdentifierSize += IdLen;
383
7.87k
    if (MaxIdentifierLength < IdLen)
384
21
      MaxIdentifierLength = IdLen;
385
7.87k
  }
386
387
4
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
388
4
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
389
4
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
390
4
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
391
4
          NumIdentifiers/(double)NumBuckets);
392
4
  fprintf(stderr, "Ave identifier length: %f\n",
393
4
          (AverageIdentifierSize/(double)NumIdentifiers));
394
4
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
395
396
  // Compute statistics about the memory allocated for identifiers.
397
4
  HashTable.getAllocator().PrintStats();
398
4
}
399
400
//===----------------------------------------------------------------------===//
401
// SelectorTable Implementation
402
//===----------------------------------------------------------------------===//
403
404
6.63M
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
405
6.63M
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
406
6.63M
}
407
408
namespace clang {
409
410
/// One of these variable length records is kept for each
411
/// selector containing more than one keyword. We use a folding set
412
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
413
/// this class is provided strictly through Selector.
414
class alignas(IdentifierInfoAlignment) MultiKeywordSelector
415
    : public detail::DeclarationNameExtra,
416
      public llvm::FoldingSetNode {
417
0
  MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
418
419
public:
420
  // Constructor for keyword selectors.
421
  MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
422
240k
      : DeclarationNameExtra(nKeys) {
423
240k
    assert((nKeys > 1) && "not a multi-keyword selector");
424
425
    // Fill in the trailing keyword array.
426
0
    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
427
892k
    for (unsigned i = 0; i != nKeys; 
++i652k
)
428
652k
      KeyInfo[i] = IIV[i];
429
240k
  }
430
431
  // getName - Derive the full selector name and return it.
432
  std::string getName() const;
433
434
  using DeclarationNameExtra::getNumArgs;
435
436
  using keyword_iterator = IdentifierInfo *const *;
437
438
1.67M
  keyword_iterator keyword_begin() const {
439
1.67M
    return reinterpret_cast<keyword_iterator>(this + 1);
440
1.67M
  }
441
442
26.0k
  keyword_iterator keyword_end() const {
443
26.0k
    return keyword_begin() + getNumArgs();
444
26.0k
  }
445
446
1.00M
  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
447
1.00M
    assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
448
0
    return keyword_begin()[i];
449
1.00M
  }
450
451
  static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
452
941k
                      unsigned NumArgs) {
453
941k
    ID.AddInteger(NumArgs);
454
3.42M
    for (unsigned i = 0; i != NumArgs; 
++i2.48M
)
455
2.48M
      ID.AddPointer(ArgTys[i]);
456
941k
  }
457
458
623k
  void Profile(llvm::FoldingSetNodeID &ID) {
459
623k
    Profile(ID, keyword_begin(), getNumArgs());
460
623k
  }
461
};
462
463
} // namespace clang.
464
465
7
bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
466
7
  assert(!Names.empty() && "must have >= 1 selector slots");
467
7
  if (getNumArgs() != Names.size())
468
2
    return false;
469
14
  
for (unsigned I = 0, E = Names.size(); 5
I != E;
++I9
) {
470
11
    if (getNameForSlot(I) != Names[I])
471
2
      return false;
472
11
  }
473
3
  return true;
474
5
}
475
476
0
bool Selector::isUnarySelector(StringRef Name) const {
477
0
  return isUnarySelector() && getNameForSlot(0) == Name;
478
0
}
479
480
3.09M
unsigned Selector::getNumArgs() const {
481
3.09M
  unsigned IIF = getIdentifierInfoFlag();
482
3.09M
  if (IIF <= ZeroArg)
483
820k
    return 0;
484
2.27M
  if (IIF == OneArg)
485
1.15M
    return 1;
486
  // We point to a MultiKeywordSelector.
487
1.12M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
488
1.12M
  return SI->getNumArgs();
489
2.27M
}
490
491
2.41M
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
492
2.41M
  if (getIdentifierInfoFlag() < MultiArg) {
493
1.41M
    assert(argIndex == 0 && "illegal keyword index");
494
0
    return getAsIdentifierInfo();
495
1.41M
  }
496
497
  // We point to a MultiKeywordSelector.
498
1.00M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
499
1.00M
  return SI->getIdentifierInfoForSlot(argIndex);
500
2.41M
}
501
502
254k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
503
254k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
504
254k
  return II ? 
II->getName()254k
:
StringRef()12
;
505
254k
}
506
507
26.0k
std::string MultiKeywordSelector::getName() const {
508
26.0k
  SmallString<256> Str;
509
26.0k
  llvm::raw_svector_ostream OS(Str);
510
99.3k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I73.3k
) {
511
73.3k
    if (*I)
512
72.7k
      OS << (*I)->getName();
513
73.3k
    OS << ':';
514
73.3k
  }
515
516
26.0k
  return std::string(OS.str());
517
26.0k
}
518
519
175k
std::string Selector::getAsString() const {
520
175k
  if (InfoPtr == 0)
521
6
    return "<null selector>";
522
523
175k
  if (getIdentifierInfoFlag() < MultiArg) {
524
149k
    IdentifierInfo *II = getAsIdentifierInfo();
525
526
149k
    if (getNumArgs() == 0) {
527
118k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
528
118k
                   "not be null.");
529
0
      return std::string(II->getName());
530
118k
    }
531
532
30.7k
    if (!II)
533
190
      return ":";
534
535
30.5k
    return II->getName().str() + ":";
536
30.7k
  }
537
538
  // We have a multiple keyword selector.
539
26.0k
  return getMultiKeywordSelector()->getName();
540
175k
}
541
542
13.1k
void Selector::print(llvm::raw_ostream &OS) const {
543
13.1k
  OS << getAsString();
544
13.1k
}
545
546
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
547
548
/// Interpreting the given string using the normal CamelCase
549
/// conventions, determine whether the given string starts with the
550
/// given "word", which is assumed to end in a lowercase letter.
551
113k
static bool startsWithWord(StringRef name, StringRef word) {
552
113k
  if (name.size() < word.size()) 
return false3.99k
;
553
109k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])94.4k
) &&
554
109k
          
name.startswith(word)69.3k
);
555
113k
}
556
557
241k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
558
241k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
559
241k
  if (!first) 
return OMF_None22
;
560
561
241k
  StringRef name = first->getName();
562
241k
  if (sel.isUnarySelector()) {
563
69.7k
    if (name == "autorelease") 
return OMF_autorelease949
;
564
68.7k
    if (name == "dealloc") 
return OMF_dealloc848
;
565
67.9k
    if (name == "finalize") 
return OMF_finalize154
;
566
67.7k
    if (name == "release") 
return OMF_release1.36k
;
567
66.4k
    if (name == "retain") 
return OMF_retain702
;
568
65.7k
    if (name == "retainCount") 
return OMF_retainCount108
;
569
65.5k
    if (name == "self") 
return OMF_self97
;
570
65.5k
    if (name == "initialize") 
return OMF_initialize34
;
571
65.5k
  }
572
573
237k
  if (name == "performSelector" || 
name == "performSelectorInBackground"236k
||
574
237k
      
name == "performSelectorOnMainThread"236k
)
575
980
    return OMF_performSelector;
576
577
  // The other method families may begin with a prefix of underscores.
578
236k
  
while (236k
!name.empty() && name.front() == '_')
579
233
    name = name.substr(1);
580
581
236k
  if (name.empty()) 
return OMF_None0
;
582
236k
  switch (name.front()) {
583
18.9k
  case 'a':
584
18.9k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc4.94k
;
585
13.9k
    break;
586
15.1k
  case 'c':
587
15.1k
    if (startsWithWord(name, "copy")) 
return OMF_copy1.49k
;
588
13.6k
    break;
589
61.2k
  case 'i':
590
61.2k
    if (startsWithWord(name, "init")) 
return OMF_init55.0k
;
591
6.23k
    break;
592
7.86k
  case 'm':
593
7.86k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy979
;
594
6.88k
    break;
595
9.59k
  case 'n':
596
9.59k
    if (startsWithWord(name, "new")) 
return OMF_new1.51k
;
597
8.08k
    break;
598
123k
  default:
599
123k
    break;
600
236k
  }
601
602
172k
  return OMF_None;
603
236k
}
604
605
554
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
606
554
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
607
554
  if (!first) 
return OIT_None2
;
608
609
552
  StringRef name = first->getName();
610
611
552
  if (name.empty()) 
return OIT_None0
;
612
552
  switch (name.front()) {
613
53
    case 'a':
614
53
      if (startsWithWord(name, "array")) 
return OIT_Array15
;
615
38
      break;
616
38
    case 'd':
617
37
      if (startsWithWord(name, "default")) 
return OIT_ReturnsSelf2
;
618
35
      if (startsWithWord(name, "dictionary")) 
return OIT_Dictionary11
;
619
24
      break;
620
35
    case 's':
621
35
      if (startsWithWord(name, "shared")) 
return OIT_ReturnsSelf2
;
622
33
      if (startsWithWord(name, "standard")) 
return OIT_Singleton1
;
623
32
      break;
624
93
    case 'i':
625
93
      if (startsWithWord(name, "init")) 
return OIT_Init45
;
626
48
      break;
627
334
    default:
628
334
      break;
629
552
  }
630
476
  return OIT_None;
631
552
}
632
633
25.3k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
634
25.3k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
635
25.3k
  if (!first) 
return SFF_None6
;
636
637
25.2k
  StringRef name = first->getName();
638
639
25.2k
  switch (name.front()) {
640
5.48k
    case 'a':
641
5.48k
      if (name == "appendFormat") 
return SFF_NSString1
;
642
5.47k
      break;
643
644
5.47k
    case 'i':
645
3.30k
      if (name == "initWithFormat") 
return SFF_NSString7
;
646
3.29k
      break;
647
648
3.29k
    case 'l':
649
275
      if (name == "localizedStringWithFormat") 
return SFF_NSString2
;
650
273
      break;
651
652
3.77k
    case 's':
653
3.77k
      if (name == "stringByAppendingFormat" ||
654
3.77k
          
name == "stringWithFormat"3.72k
)
return SFF_NSString688
;
655
3.08k
      break;
656
25.2k
  }
657
24.5k
  return SFF_None;
658
25.2k
}
659
660
namespace {
661
662
struct SelectorTableImpl {
663
  llvm::FoldingSet<MultiKeywordSelector> Table;
664
  llvm::BumpPtrAllocator Allocator;
665
};
666
667
} // namespace
668
669
408k
static SelectorTableImpl &getSelectorTableImpl(void *P) {
670
408k
  return *static_cast<SelectorTableImpl*>(P);
671
408k
}
672
673
SmallString<64>
674
352k
SelectorTable::constructSetterName(StringRef Name) {
675
352k
  SmallString<64> SetterName("set");
676
352k
  SetterName += Name;
677
352k
  SetterName[3] = toUppercase(SetterName[3]);
678
352k
  return SetterName;
679
352k
}
680
681
Selector
682
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
683
                                       SelectorTable &SelTable,
684
347k
                                       const IdentifierInfo *Name) {
685
347k
  IdentifierInfo *SetterName =
686
347k
    &Idents.get(constructSetterName(Name->getName()));
687
347k
  return SelTable.getUnarySelector(SetterName);
688
347k
}
689
690
1
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
691
1
  StringRef Name = Sel.getNameForSlot(0);
692
1
  assert(Name.startswith("set") && "invalid setter name");
693
0
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
694
1
}
695
696
1
size_t SelectorTable::getTotalMemory() const {
697
1
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
698
1
  return SelTabImpl.Allocator.getTotalMemory();
699
1
}
700
701
685k
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
702
685k
  if (nKeys < 2)
703
367k
    return Selector(IIV[0], nKeys);
704
705
318k
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
706
707
  // Unique selector, to guarantee there is one per name.
708
318k
  llvm::FoldingSetNodeID ID;
709
318k
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
710
711
318k
  void *InsertPos = nullptr;
712
318k
  if (MultiKeywordSelector *SI =
713
318k
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
714
77.4k
    return Selector(SI);
715
716
  // MultiKeywordSelector objects are not allocated with new because they have a
717
  // variable size array (for parameter types) at the end of them.
718
240k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
719
240k
  MultiKeywordSelector *SI =
720
240k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
721
240k
          Size, alignof(MultiKeywordSelector));
722
240k
  new (SI) MultiKeywordSelector(nKeys, IIV);
723
240k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
724
240k
  return Selector(SI);
725
318k
}
726
727
95.8k
SelectorTable::SelectorTable() {
728
95.8k
  Impl = new SelectorTableImpl();
729
95.8k
}
730
731
90.1k
SelectorTable::~SelectorTable() {
732
90.1k
  delete &getSelectorTableImpl(Impl);
733
90.1k
}
734
735
1.56M
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
736
1.56M
  switch (Operator) {
737
0
  case OO_None:
738
0
  case NUM_OVERLOADED_OPERATORS:
739
0
    return nullptr;
740
741
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
742
1.56M
  case OO_##Name: return Spelling;
743
1.56M
#include 
"clang/Basic/OperatorKinds.def"0
744
1.56M
  }
745
746
0
  llvm_unreachable("Invalid OverloadedOperatorKind!");
747
0
}
748
749
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
750
1.18k
                                        bool isContextSensitive) {
751
1.18k
  switch (kind) {
752
698
  case NullabilityKind::NonNull:
753
698
    return isContextSensitive ? 
"nonnull"224
:
"_Nonnull"474
;
754
755
483
  case NullabilityKind::Nullable:
756
483
    return isContextSensitive ? 
"nullable"58
:
"_Nullable"425
;
757
758
0
  case NullabilityKind::NullableResult:
759
0
    assert(!isContextSensitive &&
760
0
           "_Nullable_result isn't supported as context-sensitive keyword");
761
0
    return "_Nullable_result";
762
763
0
  case NullabilityKind::Unspecified:
764
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
765
1.18k
  }
766
0
  llvm_unreachable("Unknown nullability kind.");
767
0
}