Coverage Report

Created: 2024-01-17 10:31

/src/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/DiagnosticLex.h"
17
#include "clang/Basic/LangOptions.h"
18
#include "clang/Basic/OperatorKinds.h"
19
#include "clang/Basic/Specifiers.h"
20
#include "clang/Basic/TargetBuiltins.h"
21
#include "clang/Basic/TokenKinds.h"
22
#include "llvm/ADT/DenseMapInfo.h"
23
#include "llvm/ADT/FoldingSet.h"
24
#include "llvm/ADT/SmallString.h"
25
#include "llvm/ADT/StringMap.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/Support/Allocator.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
0
IdentifierIterator::~IdentifierIterator() = default;
47
48
0
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
public:
56
0
  StringRef Next() override { return StringRef(); }
57
};
58
59
} // namespace
60
61
0
IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
62
0
  return new EmptyLookupIterator();
63
0
}
64
65
IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup)
66
    : HashTable(8192), // Start with space for 8K identifiers.
67
20.1k
      ExternalLookup(ExternalLookup) {}
68
69
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
70
                                 IdentifierInfoLookup *ExternalLookup)
71
10.5k
    : IdentifierTable(ExternalLookup) {
72
  // Populate the identifier table with info about keywords for the current
73
  // language.
74
10.5k
  AddKeywords(LangOpts);
75
10.5k
}
76
77
//===----------------------------------------------------------------------===//
78
// Language Keyword Implementation
79
//===----------------------------------------------------------------------===//
80
81
// Constants for TokenKinds.def
82
namespace {
83
84
  enum TokenKey : unsigned {
85
    KEYC99        = 0x1,
86
    KEYCXX        = 0x2,
87
    KEYCXX11      = 0x4,
88
    KEYGNU        = 0x8,
89
    KEYMS         = 0x10,
90
    BOOLSUPPORT   = 0x20,
91
    KEYALTIVEC    = 0x40,
92
    KEYNOCXX      = 0x80,
93
    KEYBORLAND    = 0x100,
94
    KEYOPENCLC    = 0x200,
95
    KEYC23        = 0x400,
96
    KEYNOMS18     = 0x800,
97
    KEYNOOPENCL   = 0x1000,
98
    WCHARSUPPORT  = 0x2000,
99
    HALFSUPPORT   = 0x4000,
100
    CHAR8SUPPORT  = 0x8000,
101
    KEYOBJC       = 0x10000,
102
    KEYZVECTOR    = 0x20000,
103
    KEYCOROUTINES = 0x40000,
104
    KEYMODULES    = 0x80000,
105
    KEYCXX20      = 0x100000,
106
    KEYOPENCLCXX  = 0x200000,
107
    KEYMSCOMPAT   = 0x400000,
108
    KEYSYCL       = 0x800000,
109
    KEYCUDA       = 0x1000000,
110
    KEYHLSL       = 0x2000000,
111
    KEYFIXEDPOINT = 0x4000000,
112
    KEYMAX        = KEYFIXEDPOINT, // 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. This enum is ordered
119
  /// intentionally so that the value that 'wins' is the most 'permissive'.
120
  enum KeywordStatus {
121
    KS_Unknown,     // Not yet calculated. Used when figuring out the status.
122
    KS_Disabled,    // Disabled
123
    KS_Future,      // Is a keyword in future standard
124
    KS_Extension,   // Is an extension
125
    KS_Enabled,     // Enabled
126
  };
127
128
} // namespace
129
130
// This works on a single TokenKey flag and checks the LangOpts to get the
131
// KeywordStatus based exclusively on this flag, so that it can be merged in
132
// getKeywordStatus. Most should be enabled/disabled, but some might imply
133
// 'future' versions, or extensions. Returns 'unknown' unless this is KNOWN to
134
// be disabled, and the calling function makes it 'disabled' if no other flag
135
// changes it. This is necessary for the KEYNOCXX and KEYNOOPENCL flags.
136
static KeywordStatus getKeywordStatusHelper(const LangOptions &LangOpts,
137
3.77M
                                            TokenKey Flag) {
138
  // Flag is a single bit version of TokenKey (that is, not
139
  // KEYALL/KEYALLCXX/etc), so we can check with == throughout this function.
140
3.77M
  assert((Flag & ~(Flag - 1)) == Flag && "Multiple bits set?");
141
142
0
  switch (Flag) {
143
31.8k
  case KEYC99:
144
31.8k
    if (LangOpts.C99)
145
3.10k
      return KS_Enabled;
146
28.7k
    return !LangOpts.CPlusPlus ? KS_Future : KS_Unknown;
147
116k
  case KEYC23:
148
116k
    if (LangOpts.C23)
149
0
      return KS_Enabled;
150
116k
    return !LangOpts.CPlusPlus ? KS_Future : KS_Unknown;
151
1.32M
  case KEYCXX:
152
1.32M
    return LangOpts.CPlusPlus ? KS_Enabled : KS_Unknown;
153
116k
  case KEYCXX11:
154
116k
    if (LangOpts.CPlusPlus11)
155
116k
      return KS_Enabled;
156
253
    return LangOpts.CPlusPlus ? KS_Future : KS_Unknown;
157
84.9k
  case KEYCXX20:
158
84.9k
    if (LangOpts.CPlusPlus20)
159
8.08k
      return KS_Enabled;
160
76.8k
    return LangOpts.CPlusPlus ? KS_Future : KS_Unknown;
161
42.4k
  case KEYGNU:
162
42.4k
    return LangOpts.GNUKeywords ? KS_Extension : KS_Unknown;
163
488k
  case KEYMS:
164
488k
    return LangOpts.MicrosoftExt ? KS_Extension : KS_Unknown;
165
42.4k
  case BOOLSUPPORT:
166
42.4k
    if (LangOpts.Bool)      return KS_Enabled;
167
92
    return !LangOpts.CPlusPlus ? KS_Future : KS_Unknown;
168
53.1k
  case KEYALTIVEC:
169
53.1k
    return LangOpts.AltiVec ? KS_Enabled : KS_Unknown;
170
106k
  case KEYBORLAND:
171
106k
    return LangOpts.Borland ? KS_Extension : KS_Unknown;
172
361k
  case KEYOPENCLC:
173
361k
    return LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus ? KS_Enabled
174
361k
                                                        : KS_Unknown;
175
21.2k
  case WCHARSUPPORT:
176
21.2k
    return LangOpts.WChar ? KS_Enabled : KS_Unknown;
177
21.2k
  case HALFSUPPORT:
178
21.2k
    return LangOpts.Half ? KS_Enabled : KS_Unknown;
179
21.2k
  case CHAR8SUPPORT:
180
21.2k
    if (LangOpts.Char8) return KS_Enabled;
181
19.2k
    if (LangOpts.CPlusPlus20) return KS_Unknown;
182
19.2k
    if (LangOpts.CPlusPlus) return KS_Future;
183
46
    return KS_Unknown;
184
84.9k
  case KEYOBJC:
185
    // We treat bridge casts as objective-C keywords so we can warn on them
186
    // in non-arc mode.
187
84.9k
    return LangOpts.ObjC ? KS_Enabled : KS_Unknown;
188
42.4k
  case KEYZVECTOR:
189
42.4k
    return LangOpts.ZVector ? KS_Enabled : KS_Unknown;
190
42.4k
  case KEYCOROUTINES:
191
42.4k
    return LangOpts.Coroutines ? KS_Enabled : KS_Unknown;
192
31.8k
  case KEYMODULES:
193
31.8k
    return KS_Unknown;
194
361k
  case KEYOPENCLCXX:
195
361k
    return LangOpts.OpenCLCPlusPlus ? KS_Enabled : KS_Unknown;
196
148k
  case KEYMSCOMPAT:
197
148k
    return LangOpts.MSVCCompat ? KS_Enabled : KS_Unknown;
198
21.2k
  case KEYSYCL:
199
21.2k
    return LangOpts.isSYCL() ? KS_Enabled : KS_Unknown;
200
21.2k
  case KEYCUDA:
201
21.2k
    return LangOpts.CUDA ? KS_Enabled : KS_Unknown;
202
74.3k
  case KEYHLSL:
203
74.3k
    return LangOpts.HLSL ? KS_Enabled : KS_Unknown;
204
31.8k
  case KEYNOCXX:
205
    // This is enabled in all non-C++ modes, but might be enabled for other
206
    // reasons as well.
207
31.8k
    return LangOpts.CPlusPlus ? KS_Unknown : KS_Enabled;
208
10.6k
  case KEYNOOPENCL:
209
    // The disable behavior for this is handled in getKeywordStatus.
210
10.6k
    return KS_Unknown;
211
21.2k
  case KEYNOMS18:
212
    // The disable behavior for this is handled in getKeywordStatus.
213
21.2k
    return KS_Unknown;
214
42.4k
  case KEYFIXEDPOINT:
215
42.4k
    return LangOpts.FixedPoint ? KS_Enabled : KS_Disabled;
216
0
  default:
217
0
    llvm_unreachable("Unknown KeywordStatus flag");
218
3.77M
  }
219
3.77M
}
220
221
/// Translates flags as specified in TokenKinds.def into keyword status
222
/// in the given language standard.
223
static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
224
4.15M
                                      unsigned Flags) {
225
  // KEYALL means always enabled, so special case this one.
226
4.15M
  if (Flags == KEYALL) return KS_Enabled;
227
  // These are tests that need to 'always win', as they are special in that they
228
  // disable based on certain conditions.
229
2.85M
  if (LangOpts.OpenCL && (Flags & KEYNOOPENCL)) return KS_Disabled;
230
2.85M
  if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
231
2.85M
      !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
232
0
    return KS_Disabled;
233
234
2.85M
  KeywordStatus CurStatus = KS_Unknown;
235
236
6.62M
  while (Flags != 0) {
237
3.77M
    unsigned CurFlag = Flags & ~(Flags - 1);
238
3.77M
    Flags = Flags & ~CurFlag;
239
3.77M
    CurStatus = std::max(
240
3.77M
        CurStatus,
241
3.77M
        getKeywordStatusHelper(LangOpts, static_cast<TokenKey>(CurFlag)));
242
3.77M
  }
243
244
2.85M
  if (CurStatus == KS_Unknown)
245
773k
    return KS_Disabled;
246
2.08M
  return CurStatus;
247
2.85M
}
248
249
/// AddKeyword - This method is used to associate a token ID with specific
250
/// identifiers because they are language keywords.  This causes the lexer to
251
/// automatically map matching identifiers to specialized token codes.
252
static void AddKeyword(StringRef Keyword,
253
                       tok::TokenKind TokenCode, unsigned Flags,
254
4.15M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
255
4.15M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
256
257
  // Don't add this keyword if disabled in this language.
258
4.15M
  if (AddResult == KS_Disabled) return;
259
260
3.34M
  IdentifierInfo &Info =
261
3.34M
      Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
262
3.34M
  Info.setIsExtensionToken(AddResult == KS_Extension);
263
3.34M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
264
3.34M
}
265
266
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
267
/// representations.
268
static void AddCXXOperatorKeyword(StringRef Keyword,
269
                                  tok::TokenKind TokenCode,
270
116k
                                  IdentifierTable &Table) {
271
116k
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
272
116k
  Info.setIsCPlusPlusOperatorKeyword();
273
116k
}
274
275
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
276
/// or "property".
277
static void AddObjCKeyword(StringRef Name,
278
                           tok::ObjCKeywordKind ObjCID,
279
27.9k
                           IdentifierTable &Table) {
280
27.9k
  Table.get(Name).setObjCKeywordID(ObjCID);
281
27.9k
}
282
283
static void AddInterestingIdentifier(StringRef Name,
284
                                     tok::InterestingIdentifierKind BTID,
285
74.3k
                                     IdentifierTable &Table) {
286
  // Don't add 'not_interesting' identifier.
287
74.3k
  if (BTID != tok::not_interesting) {
288
63.7k
    IdentifierInfo &Info = Table.get(Name, tok::identifier);
289
63.7k
    Info.setInterestingIdentifierID(BTID);
290
63.7k
  }
291
74.3k
}
292
293
/// AddKeywords - Add all keywords to the symbol table.
294
///
295
10.6k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
296
  // Add keywords and tokens for the current language.
297
10.6k
#define KEYWORD(NAME, FLAGS) \
298
3.42M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
299
3.42M
             FLAGS, LangOpts, *this);
300
10.6k
#define ALIAS(NAME, TOK, FLAGS) \
301
722k
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
302
722k
             FLAGS, LangOpts, *this);
303
10.6k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
304
116k
  if (LangOpts.CXXOperatorNames)          \
305
116k
    AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
306
10.6k
#define OBJC_AT_KEYWORD(NAME)  \
307
286k
  if (LangOpts.ObjC)           \
308
286k
    AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
309
10.6k
#define INTERESTING_IDENTIFIER(NAME)                                           \
310
74.3k
  AddInterestingIdentifier(StringRef(#NAME), tok::NAME, *this);
311
312
10.6k
#define TESTING_KEYWORD(NAME, FLAGS)
313
10.6k
#include "clang/Basic/TokenKinds.def"
314
315
10.6k
  if (LangOpts.ParseUnknownAnytype)
316
0
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
317
0
               LangOpts, *this);
318
319
10.6k
  if (LangOpts.DeclSpecKeyword)
320
10.5k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
321
322
10.6k
  if (LangOpts.IEEE128)
323
0
    AddKeyword("__ieee128", tok::kw___float128, KEYALL, LangOpts, *this);
324
325
  // Add the 'import' contextual keyword.
326
10.6k
  get("import").setModulesImport(true);
327
10.6k
}
328
329
/// Checks if the specified token kind represents a keyword in the
330
/// specified language.
331
/// \returns Status of the keyword in the language.
332
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
333
0
                                      tok::TokenKind K) {
334
0
  switch (K) {
335
0
#define KEYWORD(NAME, FLAGS) \
336
0
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
337
0
#include "clang/Basic/TokenKinds.def"
338
0
  default: return KS_Disabled;
339
0
  }
340
0
}
341
342
/// Returns true if the identifier represents a keyword in the
343
/// specified language.
344
0
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
345
0
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
346
0
  case KS_Enabled:
347
0
  case KS_Extension:
348
0
    return true;
349
0
  default:
350
0
    return false;
351
0
  }
352
0
}
353
354
/// Returns true if the identifier represents a C++ keyword in the
355
/// specified language.
356
0
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
357
0
  if (!LangOpts.CPlusPlus || !isKeyword(LangOpts))
358
0
    return false;
359
  // This is a C++ keyword if this identifier is not a keyword when checked
360
  // using LangOptions without C++ support.
361
0
  LangOptions LangOptsNoCPP = LangOpts;
362
0
  LangOptsNoCPP.CPlusPlus = false;
363
0
  LangOptsNoCPP.CPlusPlus11 = false;
364
0
  LangOptsNoCPP.CPlusPlus20 = false;
365
0
  return !isKeyword(LangOptsNoCPP);
366
0
}
367
368
ReservedIdentifierStatus
369
4.90k
IdentifierInfo::isReserved(const LangOptions &LangOpts) const {
370
4.90k
  StringRef Name = getName();
371
372
  // '_' is a reserved identifier, but its use is so common (e.g. to store
373
  // ignored values) that we don't warn on it.
374
4.90k
  if (Name.size() <= 1)
375
3.20k
    return ReservedIdentifierStatus::NotReserved;
376
377
  // [lex.name] p3
378
1.70k
  if (Name[0] == '_') {
379
380
    // Each name that begins with an underscore followed by an uppercase letter
381
    // or another underscore is reserved.
382
15
    if (Name[1] == '_')
383
0
      return ReservedIdentifierStatus::StartsWithDoubleUnderscore;
384
385
15
    if ('A' <= Name[1] && Name[1] <= 'Z')
386
7
      return ReservedIdentifierStatus::
387
7
          StartsWithUnderscoreFollowedByCapitalLetter;
388
389
    // This is a bit misleading: it actually means it's only reserved if we're
390
    // at global scope because it starts with an underscore.
391
8
    return ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope;
392
15
  }
393
394
  // Each name that contains a double underscore (__) is reserved.
395
1.69k
  if (LangOpts.CPlusPlus && Name.contains("__"))
396
0
    return ReservedIdentifierStatus::ContainsDoubleUnderscore;
397
398
1.69k
  return ReservedIdentifierStatus::NotReserved;
399
1.69k
}
400
401
ReservedLiteralSuffixIdStatus
402
0
IdentifierInfo::isReservedLiteralSuffixId() const {
403
0
  StringRef Name = getName();
404
405
0
  if (Name[0] != '_')
406
0
    return ReservedLiteralSuffixIdStatus::NotStartsWithUnderscore;
407
408
0
  if (Name.contains("__"))
409
0
    return ReservedLiteralSuffixIdStatus::ContainsDoubleUnderscore;
410
411
0
  return ReservedLiteralSuffixIdStatus::NotReserved;
412
0
}
413
414
0
StringRef IdentifierInfo::deuglifiedName() const {
415
0
  StringRef Name = getName();
416
0
  if (Name.size() >= 2 && Name.front() == '_' &&
417
0
      (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')))
418
0
    return Name.ltrim('_');
419
0
  return Name;
420
0
}
421
422
7.74M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
423
  // We use a perfect hash function here involving the length of the keyword,
424
  // the first and third character.  For preprocessor ID's there are no
425
  // collisions (if there were, the switch below would complain about duplicate
426
  // case values).  Note that this depends on 'if' being null terminated.
427
428
7.74M
#define HASH(LEN, FIRST, THIRD) \
429
7.74M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
430
7.74M
#define CASE(LEN, FIRST, THIRD, NAME) \
431
7.74M
  case HASH(LEN, FIRST, THIRD): \
432
397k
    return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
433
434
7.74M
  unsigned Len = getLength();
435
7.74M
  if (Len < 2) return tok::pp_not_keyword;
436
3.80M
  const char *Name = getNameStart();
437
3.80M
  switch (HASH(Len, Name[0], Name[2])) {
438
3.40M
  default: return tok::pp_not_keyword;
439
67.0k
  CASE( 2, 'i', '\0', if);
440
14.5k
  CASE( 4, 'e', 'i', elif);
441
14.5k
  CASE( 4, 'e', 's', else);
442
13.2k
  CASE( 4, 'l', 'n', line);
443
16.6k
  CASE( 4, 's', 'c', sccs);
444
7.94k
  CASE( 5, 'e', 'd', endif);
445
4.31k
  CASE( 5, 'e', 'r', error);
446
6.94k
  CASE( 5, 'i', 'e', ident);
447
4.22k
  CASE( 5, 'i', 'd', ifdef);
448
16.2k
  CASE( 5, 'u', 'd', undef);
449
450
42.3k
  CASE( 6, 'a', 's', assert);
451
136k
  CASE( 6, 'd', 'f', define);
452
4.24k
  CASE( 6, 'i', 'n', ifndef);
453
13.7k
  CASE( 6, 'i', 'p', import);
454
6.11k
  CASE( 6, 'p', 'a', pragma);
455
456
5.64k
  CASE( 7, 'd', 'f', defined);
457
5.29k
  CASE( 7, 'e', 'i', elifdef);
458
5.95k
  CASE( 7, 'i', 'c', include);
459
1.32k
  CASE( 7, 'w', 'r', warning);
460
461
2.66k
  CASE( 8, 'e', 'i', elifndef);
462
4.54k
  CASE( 8, 'u', 'a', unassert);
463
1.17k
  CASE(12, 'i', 'c', include_next);
464
465
600
  CASE(14, '_', 'p', __public_macro);
466
467
524
  CASE(15, '_', 'p', __private_macro);
468
469
3.80M
  CASE(16, '_', 'i', __include_macros);
470
3.80M
#undef CASE
471
3.80M
#undef HASH
472
3.80M
  }
473
3.80M
}
474
475
//===----------------------------------------------------------------------===//
476
// Stats Implementation
477
//===----------------------------------------------------------------------===//
478
479
/// PrintStats - Print statistics about how well the identifier table is doing
480
/// at hashing identifiers.
481
0
void IdentifierTable::PrintStats() const {
482
0
  unsigned NumBuckets = HashTable.getNumBuckets();
483
0
  unsigned NumIdentifiers = HashTable.getNumItems();
484
0
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
485
0
  unsigned AverageIdentifierSize = 0;
486
0
  unsigned MaxIdentifierLength = 0;
487
488
  // TODO: Figure out maximum times an identifier had to probe for -stats.
489
0
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
490
0
       I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
491
0
    unsigned IdLen = I->getKeyLength();
492
0
    AverageIdentifierSize += IdLen;
493
0
    if (MaxIdentifierLength < IdLen)
494
0
      MaxIdentifierLength = IdLen;
495
0
  }
496
497
0
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
498
0
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
499
0
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
500
0
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
501
0
          NumIdentifiers/(double)NumBuckets);
502
0
  fprintf(stderr, "Ave identifier length: %f\n",
503
0
          (AverageIdentifierSize/(double)NumIdentifiers));
504
0
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
505
506
  // Compute statistics about the memory allocated for identifiers.
507
0
  HashTable.getAllocator().PrintStats();
508
0
}
509
510
//===----------------------------------------------------------------------===//
511
// SelectorTable Implementation
512
//===----------------------------------------------------------------------===//
513
514
0
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
515
0
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
516
0
}
517
518
0
bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
519
0
  assert(!Names.empty() && "must have >= 1 selector slots");
520
0
  if (getNumArgs() != Names.size())
521
0
    return false;
522
0
  for (unsigned I = 0, E = Names.size(); I != E; ++I) {
523
0
    if (getNameForSlot(I) != Names[I])
524
0
      return false;
525
0
  }
526
0
  return true;
527
0
}
528
529
0
bool Selector::isUnarySelector(StringRef Name) const {
530
0
  return isUnarySelector() && getNameForSlot(0) == Name;
531
0
}
532
533
0
unsigned Selector::getNumArgs() const {
534
0
  unsigned IIF = getIdentifierInfoFlag();
535
0
  if (IIF <= ZeroArg)
536
0
    return 0;
537
0
  if (IIF == OneArg)
538
0
    return 1;
539
  // We point to a MultiKeywordSelector.
540
0
  MultiKeywordSelector *SI = getMultiKeywordSelector();
541
0
  return SI->getNumArgs();
542
0
}
543
544
0
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
545
0
  if (getIdentifierInfoFlag() < MultiArg) {
546
0
    assert(argIndex == 0 && "illegal keyword index");
547
0
    return getAsIdentifierInfo();
548
0
  }
549
550
  // We point to a MultiKeywordSelector.
551
0
  MultiKeywordSelector *SI = getMultiKeywordSelector();
552
0
  return SI->getIdentifierInfoForSlot(argIndex);
553
0
}
554
555
0
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
556
0
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
557
0
  return II ? II->getName() : StringRef();
558
0
}
559
560
0
std::string MultiKeywordSelector::getName() const {
561
0
  SmallString<256> Str;
562
0
  llvm::raw_svector_ostream OS(Str);
563
0
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
564
0
    if (*I)
565
0
      OS << (*I)->getName();
566
0
    OS << ':';
567
0
  }
568
569
0
  return std::string(OS.str());
570
0
}
571
572
0
std::string Selector::getAsString() const {
573
0
  if (isNull())
574
0
    return "<null selector>";
575
576
0
  if (getIdentifierInfoFlag() < MultiArg) {
577
0
    IdentifierInfo *II = getAsIdentifierInfo();
578
579
0
    if (getNumArgs() == 0) {
580
0
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
581
0
                   "not be null.");
582
0
      return std::string(II->getName());
583
0
    }
584
585
0
    if (!II)
586
0
      return ":";
587
588
0
    return II->getName().str() + ":";
589
0
  }
590
591
  // We have a multiple keyword selector.
592
0
  return getMultiKeywordSelector()->getName();
593
0
}
594
595
0
void Selector::print(llvm::raw_ostream &OS) const {
596
0
  OS << getAsString();
597
0
}
598
599
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
600
601
/// Interpreting the given string using the normal CamelCase
602
/// conventions, determine whether the given string starts with the
603
/// given "word", which is assumed to end in a lowercase letter.
604
0
static bool startsWithWord(StringRef name, StringRef word) {
605
0
  if (name.size() < word.size()) return false;
606
0
  return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
607
0
          name.starts_with(word));
608
0
}
609
610
0
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
611
0
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
612
0
  if (!first) return OMF_None;
613
614
0
  StringRef name = first->getName();
615
0
  if (sel.isUnarySelector()) {
616
0
    if (name == "autorelease") return OMF_autorelease;
617
0
    if (name == "dealloc") return OMF_dealloc;
618
0
    if (name == "finalize") return OMF_finalize;
619
0
    if (name == "release") return OMF_release;
620
0
    if (name == "retain") return OMF_retain;
621
0
    if (name == "retainCount") return OMF_retainCount;
622
0
    if (name == "self") return OMF_self;
623
0
    if (name == "initialize") return OMF_initialize;
624
0
  }
625
626
0
  if (name == "performSelector" || name == "performSelectorInBackground" ||
627
0
      name == "performSelectorOnMainThread")
628
0
    return OMF_performSelector;
629
630
  // The other method families may begin with a prefix of underscores.
631
0
  name = name.ltrim('_');
632
633
0
  if (name.empty()) return OMF_None;
634
0
  switch (name.front()) {
635
0
  case 'a':
636
0
    if (startsWithWord(name, "alloc")) return OMF_alloc;
637
0
    break;
638
0
  case 'c':
639
0
    if (startsWithWord(name, "copy")) return OMF_copy;
640
0
    break;
641
0
  case 'i':
642
0
    if (startsWithWord(name, "init")) return OMF_init;
643
0
    break;
644
0
  case 'm':
645
0
    if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
646
0
    break;
647
0
  case 'n':
648
0
    if (startsWithWord(name, "new")) return OMF_new;
649
0
    break;
650
0
  default:
651
0
    break;
652
0
  }
653
654
0
  return OMF_None;
655
0
}
656
657
0
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
658
0
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
659
0
  if (!first) return OIT_None;
660
661
0
  StringRef name = first->getName();
662
663
0
  if (name.empty()) return OIT_None;
664
0
  switch (name.front()) {
665
0
    case 'a':
666
0
      if (startsWithWord(name, "array")) return OIT_Array;
667
0
      break;
668
0
    case 'd':
669
0
      if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
670
0
      if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
671
0
      break;
672
0
    case 's':
673
0
      if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
674
0
      if (startsWithWord(name, "standard")) return OIT_Singleton;
675
0
      break;
676
0
    case 'i':
677
0
      if (startsWithWord(name, "init")) return OIT_Init;
678
0
      break;
679
0
    default:
680
0
      break;
681
0
  }
682
0
  return OIT_None;
683
0
}
684
685
0
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
686
0
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
687
0
  if (!first) return SFF_None;
688
689
0
  StringRef name = first->getName();
690
691
0
  switch (name.front()) {
692
0
    case 'a':
693
0
      if (name == "appendFormat") return SFF_NSString;
694
0
      break;
695
696
0
    case 'i':
697
0
      if (name == "initWithFormat") return SFF_NSString;
698
0
      break;
699
700
0
    case 'l':
701
0
      if (name == "localizedStringWithFormat") return SFF_NSString;
702
0
      break;
703
704
0
    case 's':
705
0
      if (name == "stringByAppendingFormat" ||
706
0
          name == "stringWithFormat") return SFF_NSString;
707
0
      break;
708
0
  }
709
0
  return SFF_None;
710
0
}
711
712
namespace {
713
714
struct SelectorTableImpl {
715
  llvm::FoldingSet<MultiKeywordSelector> Table;
716
  llvm::BumpPtrAllocator Allocator;
717
};
718
719
} // namespace
720
721
46
static SelectorTableImpl &getSelectorTableImpl(void *P) {
722
46
  return *static_cast<SelectorTableImpl*>(P);
723
46
}
724
725
SmallString<64>
726
0
SelectorTable::constructSetterName(StringRef Name) {
727
0
  SmallString<64> SetterName("set");
728
0
  SetterName += Name;
729
0
  SetterName[3] = toUppercase(SetterName[3]);
730
0
  return SetterName;
731
0
}
732
733
Selector
734
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
735
                                       SelectorTable &SelTable,
736
0
                                       const IdentifierInfo *Name) {
737
0
  IdentifierInfo *SetterName =
738
0
    &Idents.get(constructSetterName(Name->getName()));
739
0
  return SelTable.getUnarySelector(SetterName);
740
0
}
741
742
0
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
743
0
  StringRef Name = Sel.getNameForSlot(0);
744
0
  assert(Name.starts_with("set") && "invalid setter name");
745
0
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
746
0
}
747
748
0
size_t SelectorTable::getTotalMemory() const {
749
0
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
750
0
  return SelTabImpl.Allocator.getTotalMemory();
751
0
}
752
753
0
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
754
0
  if (nKeys < 2)
755
0
    return Selector(IIV[0], nKeys);
756
757
0
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
758
759
  // Unique selector, to guarantee there is one per name.
760
0
  llvm::FoldingSetNodeID ID;
761
0
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
762
763
0
  void *InsertPos = nullptr;
764
0
  if (MultiKeywordSelector *SI =
765
0
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
766
0
    return Selector(SI);
767
768
  // MultiKeywordSelector objects are not allocated with new because they have a
769
  // variable size array (for parameter types) at the end of them.
770
0
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
771
0
  MultiKeywordSelector *SI =
772
0
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
773
0
          Size, alignof(MultiKeywordSelector));
774
0
  new (SI) MultiKeywordSelector(nKeys, IIV);
775
0
  SelTabImpl.Table.InsertNode(SI, InsertPos);
776
0
  return Selector(SI);
777
0
}
778
779
46
SelectorTable::SelectorTable() {
780
46
  Impl = new SelectorTableImpl();
781
46
}
782
783
46
SelectorTable::~SelectorTable() {
784
46
  delete &getSelectorTableImpl(Impl);
785
46
}
786
787
0
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
788
0
  switch (Operator) {
789
0
  case OO_None:
790
0
  case NUM_OVERLOADED_OPERATORS:
791
0
    return nullptr;
792
793
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
794
0
  case OO_##Name: return Spelling;
795
0
#include "clang/Basic/OperatorKinds.def"
796
0
  }
797
798
0
  llvm_unreachable("Invalid OverloadedOperatorKind!");
799
0
}
800
801
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
802
0
                                        bool isContextSensitive) {
803
0
  switch (kind) {
804
0
  case NullabilityKind::NonNull:
805
0
    return isContextSensitive ? "nonnull" : "_Nonnull";
806
807
0
  case NullabilityKind::Nullable:
808
0
    return isContextSensitive ? "nullable" : "_Nullable";
809
810
0
  case NullabilityKind::NullableResult:
811
0
    assert(!isContextSensitive &&
812
0
           "_Nullable_result isn't supported as context-sensitive keyword");
813
0
    return "_Nullable_result";
814
815
0
  case NullabilityKind::Unspecified:
816
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
817
0
  }
818
0
  llvm_unreachable("Unknown nullability kind.");
819
0
}
820
821
llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
822
0
                                     NullabilityKind NK) {
823
0
  switch (NK) {
824
0
  case NullabilityKind::NonNull:
825
0
    return OS << "NonNull";
826
0
  case NullabilityKind::Nullable:
827
0
    return OS << "Nullable";
828
0
  case NullabilityKind::NullableResult:
829
0
    return OS << "NullableResult";
830
0
  case NullabilityKind::Unspecified:
831
0
    return OS << "Unspecified";
832
0
  }
833
0
  llvm_unreachable("Unknown nullability kind.");
834
0
}
835
836
diag::kind
837
IdentifierTable::getFutureCompatDiagKind(const IdentifierInfo &II,
838
4
                                         const LangOptions &LangOpts) {
839
4
  assert(II.isFutureCompatKeyword() && "diagnostic should not be needed");
840
841
0
  unsigned Flags = llvm::StringSwitch<unsigned>(II.getName())
842
1.29k
#define KEYWORD(NAME, FLAGS) .Case(#NAME, FLAGS)
843
4
#include "clang/Basic/TokenKinds.def"
844
4
#undef KEYWORD
845
4
      ;
846
847
4
  if (LangOpts.CPlusPlus) {
848
0
    if ((Flags & KEYCXX11) == KEYCXX11)
849
0
      return diag::warn_cxx11_keyword;
850
851
    // char8_t is not modeled as a CXX20_KEYWORD because it's not
852
    // unconditionally enabled in C++20 mode. (It can be disabled
853
    // by -fno-char8_t.)
854
0
    if (((Flags & KEYCXX20) == KEYCXX20) ||
855
0
        ((Flags & CHAR8SUPPORT) == CHAR8SUPPORT))
856
0
      return diag::warn_cxx20_keyword;
857
4
  } else {
858
4
    if ((Flags & KEYC99) == KEYC99)
859
0
      return diag::warn_c99_keyword;
860
4
    if ((Flags & KEYC23) == KEYC23)
861
4
      return diag::warn_c23_keyword;
862
4
  }
863
864
0
  llvm_unreachable(
865
0
      "Keyword not known to come from a newer Standard or proposed Standard");
866
0
}