Coverage Report

Created: 2022-05-17 06:19

/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
377
IdentifierIterator::~IdentifierIterator() = default;
47
48
13.0k
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
168k
      ExternalLookup(ExternalLookup) {}
69
70
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
71
                                 IdentifierInfoLookup *ExternalLookup)
72
79.4k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
79.4k
  AddKeywords(LangOpts);
76
79.4k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
72
79.4k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
79.4k
  AddKeywords(LangOpts);
76
79.4k
}
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
    KEYCUDA       = 0x2000000,
112
    KEYMAX        = KEYCUDA, // The maximum key
113
    KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX20,
114
    KEYALL = (KEYMAX | (KEYMAX-1)) & ~KEYNOMS18 &
115
             ~KEYNOOPENCL // KEYNOMS18 and KEYNOOPENCL are used to exclude.
116
  };
117
118
  /// How a keyword is treated in the selected standard.
119
  enum KeywordStatus {
120
    KS_Disabled,    // Disabled
121
    KS_Extension,   // Is an extension
122
    KS_Enabled,     // Enabled
123
    KS_Future       // Is a keyword in future standard
124
  };
125
126
} // namespace
127
128
/// Translates flags as specified in TokenKinds.def into keyword status
129
/// in the given language standard.
130
static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
131
59.1M
                                      unsigned Flags) {
132
59.1M
  if (Flags == KEYALL) 
return KS_Enabled18.5M
;
133
40.6M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)35.0M
)
return KS_Enabled14.8M
;
134
25.7M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)17.9M
)
return KS_Enabled1.30M
;
135
24.4M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCXX20)8.79M
)
return KS_Enabled480k
;
136
23.9M
  if (LangOpts.C99 && 
(Flags & KEYC99)12.9M
)
return KS_Enabled128k
;
137
23.8M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)17.6M
)
return KS_Extension150k
;
138
23.6M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)9.56M
)
return KS_Extension3.57M
;
139
20.1M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)1.21M
)
return KS_Enabled155k
;
140
19.9M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)891
)
return KS_Extension54
;
141
19.9M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)14.6M
)
return KS_Enabled439k
;
142
19.5M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)144k
)
return KS_Enabled689
;
143
19.5M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)9.59M
)
return KS_Enabled81.3k
;
144
19.4M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)4.91M
)
return KS_Enabled68.6k
;
145
19.3M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)62.0k
)
return KS_Enabled1.26k
;
146
19.3M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)2.02k
)
return KS_Enabled33
;
147
19.3M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus143k
&&
(Flags & KEYOPENCLC)125k
)
148
17.8k
    return KS_Enabled;
149
19.3M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)18.4k
)
return KS_Enabled4.95k
;
150
19.3M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)5.36M
)
return KS_Enabled115k
;
151
19.2M
  if (LangOpts.C11 && 
(Flags & KEYC11)4.35M
)
return KS_Enabled0
;
152
  // We treat bridge casts as objective-C keywords so we can warn on them
153
  // in non-arc mode.
154
19.2M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)9.06M
)
return KS_Enabled671k
;
155
18.5M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCONCEPTS)4.41M
)
return KS_Enabled0
;
156
18.5M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)434k
)
return KS_Enabled174
;
157
18.5M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)17.6k
)
return KS_Enabled286
;
158
18.5M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)13.3M
)
return KS_Future696k
;
159
17.8M
  if (LangOpts.CPlusPlus && 
!LangOpts.CPlusPlus2012.6M
&&
(Flags & CHAR8SUPPORT)8.23M
)
160
77.2k
    return KS_Future;
161
17.7M
  if (LangOpts.isSYCL() && 
(Flags & KEYSYCL)4.60k
)
162
39
    return KS_Enabled;
163
17.7M
  if (LangOpts.CUDA && 
(Flags & KEYCUDA)40.4k
)
164
355
    return KS_Enabled;
165
17.7M
  return KS_Disabled;
166
17.7M
}
167
168
/// AddKeyword - This method is used to associate a token ID with specific
169
/// identifiers because they are language keywords.  This causes the lexer to
170
/// automatically map matching identifiers to specialized token codes.
171
static void AddKeyword(StringRef Keyword,
172
                       tok::TokenKind TokenCode, unsigned Flags,
173
58.6M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
174
58.6M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
175
176
  // Don't add this keyword under MSVCCompat.
177
58.6M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)4.37M
&&
178
58.6M
      
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)25.1k
)
179
5.84k
    return;
180
181
  // Don't add this keyword under OpenCL.
182
58.6M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)239k
)
183
691
    return;
184
185
  // Don't add this keyword if disabled in this language.
186
58.6M
  if (AddResult == KS_Disabled) 
return17.6M
;
187
188
40.9M
  IdentifierInfo &Info =
189
40.9M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier771k
:
TokenCode40.2M
);
190
40.9M
  Info.setIsExtensionToken(AddResult == KS_Extension);
191
40.9M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
192
40.9M
}
193
194
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
195
/// representations.
196
static void AddCXXOperatorKeyword(StringRef Keyword,
197
                                  tok::TokenKind TokenCode,
198
1.29M
                                  IdentifierTable &Table) {
199
1.29M
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
200
1.29M
  Info.setIsCPlusPlusOperatorKeyword();
201
1.29M
}
202
203
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
204
/// or "property".
205
static void AddObjCKeyword(StringRef Name,
206
                           tok::ObjCKeywordKind ObjCID,
207
2.59M
                           IdentifierTable &Table) {
208
2.59M
  Table.get(Name).setObjCKeywordID(ObjCID);
209
2.59M
}
210
211
/// AddKeywords - Add all keywords to the symbol table.
212
///
213
168k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
214
  // Add keywords and tokens for the current language.
215
168k
#define KEYWORD(NAME, FLAGS) \
216
47.0M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
217
47.0M
             FLAGS, LangOpts, *this);
218
168k
#define ALIAS(NAME, TOK, FLAGS) \
219
11.4M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
220
11.4M
             FLAGS, LangOpts, *this);
221
168k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
222
1.85M
  if (LangOpts.CXXOperatorNames)          \
223
1.85M
    
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this)1.29M
;
224
168k
#define OBJC_AT_KEYWORD(NAME)  \
225
4.55M
  if (LangOpts.ObjC)           \
226
4.55M
    
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this)2.59M
;
227
168k
#define TESTING_KEYWORD(NAME, FLAGS)
228
168k
#include "clang/Basic/TokenKinds.def"
229
230
168k
  if (LangOpts.ParseUnknownAnytype)
231
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
232
9
               LangOpts, *this);
233
234
168k
  if (LangOpts.DeclSpecKeyword)
235
78.1k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
236
237
168k
  if (LangOpts.IEEE128)
238
4.06k
    AddKeyword("__ieee128", tok::kw___float128, KEYALL, LangOpts, *this);
239
240
  // Add the 'import' contextual keyword.
241
168k
  get("import").setModulesImport(true);
242
168k
}
243
244
/// Checks if the specified token kind represents a keyword in the
245
/// specified language.
246
/// \returns Status of the keyword in the language.
247
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
248
4.67M
                                      tok::TokenKind K) {
249
4.67M
  switch (K) {
250
0
#define KEYWORD(NAME, FLAGS) \
251
518k
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
252
0
#include "clang/Basic/TokenKinds.def"
253
4.15M
  default: return KS_Disabled;
254
4.67M
  }
255
4.67M
}
256
257
/// Returns true if the identifier represents a keyword in the
258
/// specified language.
259
4.67M
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
260
4.67M
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
261
372k
  case KS_Enabled:
262
378k
  case KS_Extension:
263
378k
    return true;
264
4.29M
  default:
265
4.29M
    return false;
266
4.67M
  }
267
4.67M
}
268
269
/// Returns true if the identifier represents a C++ keyword in the
270
/// specified language.
271
380k
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
272
380k
  if (!LangOpts.CPlusPlus || 
!isKeyword(LangOpts)380k
)
273
126k
    return false;
274
  // This is a C++ keyword if this identifier is not a keyword when checked
275
  // using LangOptions without C++ support.
276
254k
  LangOptions LangOptsNoCPP = LangOpts;
277
254k
  LangOptsNoCPP.CPlusPlus = false;
278
254k
  LangOptsNoCPP.CPlusPlus11 = false;
279
254k
  LangOptsNoCPP.CPlusPlus20 = false;
280
254k
  return !isKeyword(LangOptsNoCPP);
281
380k
}
282
283
ReservedIdentifierStatus
284
48.8M
IdentifierInfo::isReserved(const LangOptions &LangOpts) const {
285
48.8M
  StringRef Name = getName();
286
287
  // '_' is a reserved identifier, but its use is so common (e.g. to store
288
  // ignored values) that we don't warn on it.
289
48.8M
  if (Name.size() <= 1)
290
612k
    return ReservedIdentifierStatus::NotReserved;
291
292
  // [lex.name] p3
293
48.2M
  if (Name[0] == '_') {
294
295
    // Each name that begins with an underscore followed by an uppercase letter
296
    // or another underscore is reserved.
297
12.4M
    if (Name[1] == '_')
298
8.77M
      return ReservedIdentifierStatus::StartsWithDoubleUnderscore;
299
300
3.72M
    if ('A' <= Name[1] && 
Name[1] <= 'Z'3.72M
)
301
2.32M
      return ReservedIdentifierStatus::
302
2.32M
          StartsWithUnderscoreFollowedByCapitalLetter;
303
304
    // This is a bit misleading: it actually means it's only reserved if we're
305
    // at global scope because it starts with an underscore.
306
1.39M
    return ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope;
307
3.72M
  }
308
309
  // Each name that contains a double underscore (__) is reserved.
310
35.7M
  if (LangOpts.CPlusPlus && 
Name.contains("__")15.3M
)
311
29.8k
    return ReservedIdentifierStatus::ContainsDoubleUnderscore;
312
313
35.6M
  return ReservedIdentifierStatus::NotReserved;
314
35.7M
}
315
316
2.08k
StringRef IdentifierInfo::deuglifiedName() const {
317
2.08k
  StringRef Name = getName();
318
2.08k
  if (Name.size() >= 2 && 
Name.front() == '_'311
&&
319
2.08k
      
(14
Name[1] == '_'14
||
(8
Name[1] >= 'A'8
&&
Name[1] <= 'Z'8
)))
320
14
    return Name.ltrim('_');
321
2.06k
  return Name;
322
2.08k
}
323
324
106M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
325
  // We use a perfect hash function here involving the length of the keyword,
326
  // the first and third character.  For preprocessor ID's there are no
327
  // collisions (if there were, the switch below would complain about duplicate
328
  // case values).  Note that this depends on 'if' being null terminated.
329
330
106M
#define HASH(LEN, FIRST, THIRD) \
331
170M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
332
106M
#define CASE(LEN, FIRST, THIRD, NAME) \
333
106M
  
case 63.6M
HASH63.6M
(LEN, FIRST, THIRD): \
334
63.6M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword636k
:
tok::pp_62.9M
## NAME
335
336
106M
  unsigned Len = getLength();
337
106M
  if (Len < 2) 
return tok::pp_not_keyword124k
;
338
106M
  const char *Name = getNameStart();
339
106M
  switch (HASH(Len, Name[0], Name[2])) {
340
43.1M
  default: return tok::pp_not_keyword;
341
2.21M
  CASE
(0
2, 'i', '\0', if);
342
266k
  CASE
(0
4, 'e', 'i', elif);
343
1.26M
  CASE
(0
4, 'e', 's', else);
344
59.8k
  CASE
(0
4, 'l', 'n', line);
345
6.46k
  CASE
(0
4, 's', 'c', sccs);
346
5.86M
  CASE
(0
5, 'e', 'd', endif);
347
12.8k
  CASE
(0
5, 'e', 'r', error);
348
3.25k
  CASE
(0
5, 'i', 'e', ident);
349
1.22M
  CASE
(0
5, 'i', 'd', ifdef);
350
126k
  CASE
(0
5, 'u', 'd', undef);
351
352
32.6k
  CASE
(0
6, 'a', 's', assert);
353
43.2M
  CASE
(0
6, 'd', 'f', define);
354
4.84M
  CASE
(0
6, 'i', 'n', ifndef);
355
289k
  CASE
(0
6, 'i', 'p', import);
356
688k
  CASE
(0
6, 'p', 'a', pragma);
357
358
11.3k
  CASE
(0
7, 'd', 'f', defined);
359
8.98k
  CASE
(0
7, 'e', 'i', elifdef);
360
3.01M
  CASE
(0
7, 'i', 'c', include);
361
1.36k
  CASE
(0
7, 'w', 'r', warning);
362
363
34.8k
  CASE
(0
8, 'e', 'i', elifndef);
364
5.66k
  CASE
(0
8, 'u', 'a', unassert);
365
40.7k
  CASE
(0
12, 'i', 'c', include_next);
366
367
5.05k
  CASE
(0
14, '_', 'p', __public_macro);
368
369
86.7k
  CASE
(0
15, '_', 'p', __private_macro);
370
371
106M
  
CASE230k
(16, '_', 'i', __include_macros);
372
106M
#undef CASE
373
106M
#undef HASH
374
106M
  }
375
106M
}
376
377
//===----------------------------------------------------------------------===//
378
// Stats Implementation
379
//===----------------------------------------------------------------------===//
380
381
/// PrintStats - Print statistics about how well the identifier table is doing
382
/// at hashing identifiers.
383
4
void IdentifierTable::PrintStats() const {
384
4
  unsigned NumBuckets = HashTable.getNumBuckets();
385
4
  unsigned NumIdentifiers = HashTable.getNumItems();
386
4
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
387
4
  unsigned AverageIdentifierSize = 0;
388
4
  unsigned MaxIdentifierLength = 0;
389
390
  // TODO: Figure out maximum times an identifier had to probe for -stats.
391
4
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
392
7.69k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I7.68k
) {
393
7.68k
    unsigned IdLen = I->getKeyLength();
394
7.68k
    AverageIdentifierSize += IdLen;
395
7.68k
    if (MaxIdentifierLength < IdLen)
396
22
      MaxIdentifierLength = IdLen;
397
7.68k
  }
398
399
4
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
400
4
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
401
4
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
402
4
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
403
4
          NumIdentifiers/(double)NumBuckets);
404
4
  fprintf(stderr, "Ave identifier length: %f\n",
405
4
          (AverageIdentifierSize/(double)NumIdentifiers));
406
4
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
407
408
  // Compute statistics about the memory allocated for identifiers.
409
4
  HashTable.getAllocator().PrintStats();
410
4
}
411
412
//===----------------------------------------------------------------------===//
413
// SelectorTable Implementation
414
//===----------------------------------------------------------------------===//
415
416
6.84M
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
417
6.84M
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
418
6.84M
}
419
420
namespace clang {
421
422
/// One of these variable length records is kept for each
423
/// selector containing more than one keyword. We use a folding set
424
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
425
/// this class is provided strictly through Selector.
426
class alignas(IdentifierInfoAlignment) MultiKeywordSelector
427
    : public detail::DeclarationNameExtra,
428
      public llvm::FoldingSetNode {
429
0
  MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
430
431
public:
432
  // Constructor for keyword selectors.
433
  MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
434
245k
      : DeclarationNameExtra(nKeys) {
435
245k
    assert((nKeys > 1) && "not a multi-keyword selector");
436
437
    // Fill in the trailing keyword array.
438
0
    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
439
911k
    for (unsigned i = 0; i != nKeys; 
++i666k
)
440
666k
      KeyInfo[i] = IIV[i];
441
245k
  }
442
443
  // getName - Derive the full selector name and return it.
444
  std::string getName() const;
445
446
  using DeclarationNameExtra::getNumArgs;
447
448
  using keyword_iterator = IdentifierInfo *const *;
449
450
1.73M
  keyword_iterator keyword_begin() const {
451
1.73M
    return reinterpret_cast<keyword_iterator>(this + 1);
452
1.73M
  }
453
454
27.4k
  keyword_iterator keyword_end() const {
455
27.4k
    return keyword_begin() + getNumArgs();
456
27.4k
  }
457
458
1.04M
  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
459
1.04M
    assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
460
0
    return keyword_begin()[i];
461
1.04M
  }
462
463
  static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
464
962k
                      unsigned NumArgs) {
465
962k
    ID.AddInteger(NumArgs);
466
3.50M
    for (unsigned i = 0; i != NumArgs; 
++i2.53M
)
467
2.53M
      ID.AddPointer(ArgTys[i]);
468
962k
  }
469
470
637k
  void Profile(llvm::FoldingSetNodeID &ID) {
471
637k
    Profile(ID, keyword_begin(), getNumArgs());
472
637k
  }
473
};
474
475
} // namespace clang.
476
477
7
bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
478
7
  assert(!Names.empty() && "must have >= 1 selector slots");
479
7
  if (getNumArgs() != Names.size())
480
2
    return false;
481
14
  
for (unsigned I = 0, E = Names.size(); 5
I != E;
++I9
) {
482
11
    if (getNameForSlot(I) != Names[I])
483
2
      return false;
484
11
  }
485
3
  return true;
486
5
}
487
488
0
bool Selector::isUnarySelector(StringRef Name) const {
489
0
  return isUnarySelector() && getNameForSlot(0) == Name;
490
0
}
491
492
3.19M
unsigned Selector::getNumArgs() const {
493
3.19M
  unsigned IIF = getIdentifierInfoFlag();
494
3.19M
  if (IIF <= ZeroArg)
495
836k
    return 0;
496
2.35M
  if (IIF == OneArg)
497
1.19M
    return 1;
498
  // We point to a MultiKeywordSelector.
499
1.16M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
500
1.16M
  return SI->getNumArgs();
501
2.35M
}
502
503
2.50M
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
504
2.50M
  if (getIdentifierInfoFlag() < MultiArg) {
505
1.46M
    assert(argIndex == 0 && "illegal keyword index");
506
0
    return getAsIdentifierInfo();
507
1.46M
  }
508
509
  // We point to a MultiKeywordSelector.
510
1.04M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
511
1.04M
  return SI->getIdentifierInfoForSlot(argIndex);
512
2.50M
}
513
514
282k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
515
282k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
516
282k
  return II ? 
II->getName()282k
:
StringRef()12
;
517
282k
}
518
519
27.4k
std::string MultiKeywordSelector::getName() const {
520
27.4k
  SmallString<256> Str;
521
27.4k
  llvm::raw_svector_ostream OS(Str);
522
104k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I77.1k
) {
523
77.1k
    if (*I)
524
76.5k
      OS << (*I)->getName();
525
77.1k
    OS << ':';
526
77.1k
  }
527
528
27.4k
  return std::string(OS.str());
529
27.4k
}
530
531
178k
std::string Selector::getAsString() const {
532
178k
  if (InfoPtr == 0)
533
6
    return "<null selector>";
534
535
178k
  if (getIdentifierInfoFlag() < MultiArg) {
536
151k
    IdentifierInfo *II = getAsIdentifierInfo();
537
538
151k
    if (getNumArgs() == 0) {
539
118k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
540
118k
                   "not be null.");
541
0
      return std::string(II->getName());
542
118k
    }
543
544
32.2k
    if (!II)
545
190
      return ":";
546
547
32.0k
    return II->getName().str() + ":";
548
32.2k
  }
549
550
  // We have a multiple keyword selector.
551
27.4k
  return getMultiKeywordSelector()->getName();
552
178k
}
553
554
13.1k
void Selector::print(llvm::raw_ostream &OS) const {
555
13.1k
  OS << getAsString();
556
13.1k
}
557
558
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
559
560
/// Interpreting the given string using the normal CamelCase
561
/// conventions, determine whether the given string starts with the
562
/// given "word", which is assumed to end in a lowercase letter.
563
114k
static bool startsWithWord(StringRef name, StringRef word) {
564
114k
  if (name.size() < word.size()) 
return false4.00k
;
565
110k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])95.9k
) &&
566
110k
          
name.startswith(word)70.5k
);
567
114k
}
568
569
245k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
570
245k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
571
245k
  if (!first) 
return OMF_None22
;
572
573
245k
  StringRef name = first->getName();
574
245k
  if (sel.isUnarySelector()) {
575
70.2k
    if (name == "autorelease") 
return OMF_autorelease959
;
576
69.2k
    if (name == "dealloc") 
return OMF_dealloc848
;
577
68.4k
    if (name == "finalize") 
return OMF_finalize154
;
578
68.2k
    if (name == "release") 
return OMF_release1.36k
;
579
66.9k
    if (name == "retain") 
return OMF_retain702
;
580
66.2k
    if (name == "retainCount") 
return OMF_retainCount108
;
581
66.1k
    if (name == "self") 
return OMF_self97
;
582
66.0k
    if (name == "initialize") 
return OMF_initialize34
;
583
66.0k
  }
584
585
240k
  if (name == "performSelector" || 
name == "performSelectorInBackground"239k
||
586
240k
      
name == "performSelectorOnMainThread"239k
)
587
980
    return OMF_performSelector;
588
589
  // The other method families may begin with a prefix of underscores.
590
240k
  
while (239k
!name.empty() && name.front() == '_')
591
233
    name = name.substr(1);
592
593
239k
  if (name.empty()) 
return OMF_None0
;
594
239k
  switch (name.front()) {
595
19.0k
  case 'a':
596
19.0k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc4.95k
;
597
14.1k
    break;
598
15.3k
  case 'c':
599
15.3k
    if (startsWithWord(name, "copy")) 
return OMF_copy1.50k
;
600
13.8k
    break;
601
62.3k
  case 'i':
602
62.3k
    if (startsWithWord(name, "init")) 
return OMF_init56.1k
;
603
6.28k
    break;
604
7.94k
  case 'm':
605
7.94k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy984
;
606
6.95k
    break;
607
9.72k
  case 'n':
608
9.72k
    if (startsWithWord(name, "new")) 
return OMF_new1.51k
;
609
8.20k
    break;
610
125k
  default:
611
125k
    break;
612
239k
  }
613
614
174k
  return OMF_None;
615
239k
}
616
617
554
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
618
554
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
619
554
  if (!first) 
return OIT_None2
;
620
621
552
  StringRef name = first->getName();
622
623
552
  if (name.empty()) 
return OIT_None0
;
624
552
  switch (name.front()) {
625
53
    case 'a':
626
53
      if (startsWithWord(name, "array")) 
return OIT_Array15
;
627
38
      break;
628
38
    case 'd':
629
37
      if (startsWithWord(name, "default")) 
return OIT_ReturnsSelf2
;
630
35
      if (startsWithWord(name, "dictionary")) 
return OIT_Dictionary11
;
631
24
      break;
632
35
    case 's':
633
35
      if (startsWithWord(name, "shared")) 
return OIT_ReturnsSelf2
;
634
33
      if (startsWithWord(name, "standard")) 
return OIT_Singleton1
;
635
32
      break;
636
93
    case 'i':
637
93
      if (startsWithWord(name, "init")) 
return OIT_Init45
;
638
48
      break;
639
334
    default:
640
334
      break;
641
552
  }
642
476
  return OIT_None;
643
552
}
644
645
25.3k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
646
25.3k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
647
25.3k
  if (!first) 
return SFF_None6
;
648
649
25.3k
  StringRef name = first->getName();
650
651
25.3k
  switch (name.front()) {
652
5.48k
    case 'a':
653
5.48k
      if (name == "appendFormat") 
return SFF_NSString1
;
654
5.48k
      break;
655
656
5.48k
    case 'i':
657
3.30k
      if (name == "initWithFormat") 
return SFF_NSString7
;
658
3.30k
      break;
659
660
3.30k
    case 'l':
661
275
      if (name == "localizedStringWithFormat") 
return SFF_NSString2
;
662
273
      break;
663
664
3.77k
    case 's':
665
3.77k
      if (name == "stringByAppendingFormat" ||
666
3.77k
          
name == "stringWithFormat"3.72k
)
return SFF_NSString688
;
667
3.08k
      break;
668
25.3k
  }
669
24.6k
  return SFF_None;
670
25.3k
}
671
672
namespace {
673
674
struct SelectorTableImpl {
675
  llvm::FoldingSet<MultiKeywordSelector> Table;
676
  llvm::BumpPtrAllocator Allocator;
677
};
678
679
} // namespace
680
681
416k
static SelectorTableImpl &getSelectorTableImpl(void *P) {
682
416k
  return *static_cast<SelectorTableImpl*>(P);
683
416k
}
684
685
SmallString<64>
686
360k
SelectorTable::constructSetterName(StringRef Name) {
687
360k
  SmallString<64> SetterName("set");
688
360k
  SetterName += Name;
689
360k
  SetterName[3] = toUppercase(SetterName[3]);
690
360k
  return SetterName;
691
360k
}
692
693
Selector
694
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
695
                                       SelectorTable &SelTable,
696
355k
                                       const IdentifierInfo *Name) {
697
355k
  IdentifierInfo *SetterName =
698
355k
    &Idents.get(constructSetterName(Name->getName()));
699
355k
  return SelTable.getUnarySelector(SetterName);
700
355k
}
701
702
1
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
703
1
  StringRef Name = Sel.getNameForSlot(0);
704
1
  assert(Name.startswith("set") && "invalid setter name");
705
0
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
706
1
}
707
708
1
size_t SelectorTable::getTotalMemory() const {
709
1
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
710
1
  return SelTabImpl.Allocator.getTotalMemory();
711
1
}
712
713
698k
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
714
698k
  if (nKeys < 2)
715
373k
    return Selector(IIV[0], nKeys);
716
717
324k
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
718
719
  // Unique selector, to guarantee there is one per name.
720
324k
  llvm::FoldingSetNodeID ID;
721
324k
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
722
723
324k
  void *InsertPos = nullptr;
724
324k
  if (MultiKeywordSelector *SI =
725
324k
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
726
79.2k
    return Selector(SI);
727
728
  // MultiKeywordSelector objects are not allocated with new because they have a
729
  // variable size array (for parameter types) at the end of them.
730
245k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
731
245k
  MultiKeywordSelector *SI =
732
245k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
733
245k
          Size, alignof(MultiKeywordSelector));
734
245k
  new (SI) MultiKeywordSelector(nKeys, IIV);
735
245k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
736
245k
  return Selector(SI);
737
324k
}
738
739
97.2k
SelectorTable::SelectorTable() {
740
97.2k
  Impl = new SelectorTableImpl();
741
97.2k
}
742
743
91.3k
SelectorTable::~SelectorTable() {
744
91.3k
  delete &getSelectorTableImpl(Impl);
745
91.3k
}
746
747
3.67M
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
748
3.67M
  switch (Operator) {
749
0
  case OO_None:
750
0
  case NUM_OVERLOADED_OPERATORS:
751
0
    return nullptr;
752
753
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
754
3.67M
  case OO_##Name: return Spelling;
755
3.67M
#include 
"clang/Basic/OperatorKinds.def"0
756
3.67M
  }
757
758
0
  llvm_unreachable("Invalid OverloadedOperatorKind!");
759
0
}
760
761
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
762
1.18k
                                        bool isContextSensitive) {
763
1.18k
  switch (kind) {
764
699
  case NullabilityKind::NonNull:
765
699
    return isContextSensitive ? 
"nonnull"224
:
"_Nonnull"475
;
766
767
483
  case NullabilityKind::Nullable:
768
483
    return isContextSensitive ? 
"nullable"58
:
"_Nullable"425
;
769
770
0
  case NullabilityKind::NullableResult:
771
0
    assert(!isContextSensitive &&
772
0
           "_Nullable_result isn't supported as context-sensitive keyword");
773
0
    return "_Nullable_result";
774
775
0
  case NullabilityKind::Unspecified:
776
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
777
1.18k
  }
778
0
  llvm_unreachable("Unknown nullability kind.");
779
0
}