Coverage Report

Created: 2021-08-24 07:12

/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
372
IdentifierIterator::~IdentifierIterator() = default;
47
48
12.3k
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
153k
      ExternalLookup(ExternalLookup) {}
69
70
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
71
                                 IdentifierInfoLookup *ExternalLookup)
72
56.0k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
56.0k
  AddKeywords(LangOpts);
76
56.0k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
72
56.0k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
56.0k
  AddKeywords(LangOpts);
76
56.0k
}
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.6M
;
131
34.6M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)29.2M
)
return KS_Enabled13.0M
;
132
21.6M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)14.2M
)
return KS_Enabled1.14M
;
133
20.4M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCXX20)5.19M
)
return KS_Enabled315k
;
134
20.1M
  if (LangOpts.C99 && 
(Flags & KEYC99)9.36M
)
return KS_Enabled106k
;
135
20.0M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)14.7M
)
return KS_Extension139k
;
136
19.9M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)6.02M
)
return KS_Extension2.47M
;
137
17.4M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)999k
)
return KS_Enabled4.16k
;
138
17.4M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)811
)
return KS_Extension54
;
139
17.4M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)12.4M
)
return KS_Enabled391k
;
140
17.0M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)172k
)
return KS_Enabled842
;
141
17.0M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)9.49M
)
return KS_Enabled88.6k
;
142
16.9M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)2.68M
)
return KS_Enabled45.0k
;
143
16.9M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)95.6k
)
return KS_Enabled1.87k
;
144
16.9M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)6.36k
)
return KS_Enabled96
;
145
16.9M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus172k
&&
(Flags & KEYOPENCLC)160k
)
146
24.4k
    return KS_Enabled;
147
16.8M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)11.3k
)
return KS_Enabled3.39k
;
148
16.8M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)5.15M
)
return KS_Enabled116k
;
149
16.7M
  if (LangOpts.C11 && 
(Flags & KEYC11)4.13M
)
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.7M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)7.47M
)
return KS_Enabled572k
;
153
16.2M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCONCEPTS)2.36M
)
return KS_Enabled0
;
154
16.2M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)350k
)
return KS_Enabled183
;
155
16.2M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)15.4k
)
return KS_Enabled280
;
156
16.2M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)11.2M
)
return KS_Future753k
;
157
15.4M
  if (LangOpts.CPlusPlus && 
!LangOpts.CPlusPlus2010.4M
&&
(Flags & CHAR8SUPPORT)8.08M
)
158
84.8k
    return KS_Future;
159
15.3M
  if (LangOpts.isSYCL() && 
(Flags & KEYSYCL)3.48k
)
160
33
    return KS_Enabled;
161
15.3M
  return KS_Disabled;
162
15.3M
}
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.5M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
170
50.5M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
171
172
  // Don't add this keyword under MSVCCompat.
173
50.5M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)3.96M
&&
174
50.5M
      
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)23.9k
)
175
5.51k
    return;
176
177
  // Don't add this keyword under OpenCL.
178
50.5M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)278k
)
179
844
    return;
180
181
  // Don't add this keyword if disabled in this language.
182
50.5M
  if (AddResult == KS_Disabled) 
return15.1M
;
183
184
35.3M
  IdentifierInfo &Info =
185
35.3M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier835k
:
TokenCode34.5M
);
186
35.3M
  Info.setIsExtensionToken(AddResult == KS_Extension);
187
35.3M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
188
35.3M
}
189
190
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
191
/// representations.
192
static void AddCXXOperatorKeyword(StringRef Keyword,
193
                                  tok::TokenKind TokenCode,
194
1.01M
                                  IdentifierTable &Table) {
195
1.01M
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
196
1.01M
  Info.setIsCPlusPlusOperatorKeyword();
197
1.01M
}
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.20M
                           IdentifierTable &Table) {
204
2.20M
  Table.get(Name).setObjCKeywordID(ObjCID);
205
2.20M
}
206
207
/// AddKeywords - Add all keywords to the symbol table.
208
///
209
153k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
210
  // Add keywords and tokens for the current language.
211
153k
#define KEYWORD(NAME, FLAGS) \
212
41.9M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
213
41.9M
             FLAGS, LangOpts, *this);
214
153k
#define ALIAS(NAME, TOK, FLAGS) \
215
8.57M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
216
8.57M
             FLAGS, LangOpts, *this);
217
153k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
218
1.68M
  if (LangOpts.CXXOperatorNames)          \
219
1.68M
    
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this)1.01M
;
220
153k
#define OBJC_AT_KEYWORD(NAME)  \
221
4.13M
  if (LangOpts.ObjC)           \
222
4.13M
    
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this)2.20M
;
223
153k
#define TESTING_KEYWORD(NAME, FLAGS)
224
153k
#include "clang/Basic/TokenKinds.def"
225
226
153k
  if (LangOpts.ParseUnknownAnytype)
227
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
228
9
               LangOpts, *this);
229
230
153k
  if (LangOpts.DeclSpecKeyword)
231
54.4k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
232
233
153k
  if (LangOpts.IEEE128)
234
4.25k
    AddKeyword("__ieee128", tok::kw___float128, KEYALL, LangOpts, *this);
235
236
  // Add the 'import' contextual keyword.
237
153k
  get("import").setModulesImport(true);
238
153k
}
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
4.06M
                                      tok::TokenKind K) {
245
4.06M
  switch (K) {
246
0
#define KEYWORD(NAME, FLAGS) \
247
702k
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
248
0
#include "clang/Basic/TokenKinds.def"
249
3.36M
  default: return KS_Disabled;
250
4.06M
  }
251
4.06M
}
252
253
/// Returns true if the identifier represents a keyword in the
254
/// specified language.
255
4.06M
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
256
4.06M
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
257
506k
  case KS_Enabled:
258
515k
  case KS_Extension:
259
515k
    return true;
260
3.54M
  default:
261
3.54M
    return false;
262
4.06M
  }
263
4.06M
}
264
265
/// Returns true if the identifier represents a C++ keyword in the
266
/// specified language.
267
522k
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
268
522k
  if (!LangOpts.CPlusPlus || 
!isKeyword(LangOpts)522k
)
269
177k
    return false;
270
  // This is a C++ keyword if this identifier is not a keyword when checked
271
  // using LangOptions without C++ support.
272
345k
  LangOptions LangOptsNoCPP = LangOpts;
273
345k
  LangOptsNoCPP.CPlusPlus = false;
274
345k
  LangOptsNoCPP.CPlusPlus11 = false;
275
345k
  LangOptsNoCPP.CPlusPlus20 = false;
276
345k
  return !isKeyword(LangOptsNoCPP);
277
522k
}
278
279
ReservedIdentifierStatus
280
56.2M
IdentifierInfo::isReserved(const LangOptions &LangOpts) const {
281
56.2M
  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
56.2M
  if (Name.size() <= 1)
286
585k
    return ReservedIdentifierStatus::NotReserved;
287
288
  // [lex.name] p3
289
55.6M
  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.7M
    if (Name[1] == '_')
294
8.98M
      return ReservedIdentifierStatus::StartsWithDoubleUnderscore;
295
296
3.78M
    if ('A' <= Name[1] && 
Name[1] <= 'Z'3.77M
)
297
2.53M
      return ReservedIdentifierStatus::
298
2.53M
          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.24M
    return ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope;
303
3.78M
  }
304
305
  // Each name that contains a double underscore (__) is reserved.
306
42.8M
  if (LangOpts.CPlusPlus && 
Name.contains("__")15.2M
)
307
49.4k
    return ReservedIdentifierStatus::ContainsDoubleUnderscore;
308
309
42.8M
  return ReservedIdentifierStatus::NotReserved;
310
42.8M
}
311
312
117M
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
117M
#define HASH(LEN, FIRST, THIRD) \
319
187M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
320
117M
#define CASE(LEN, FIRST, THIRD, NAME) \
321
117M
  
case 69.5M
HASH69.5M
(LEN, FIRST, THIRD): \
322
69.5M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword728k
:
tok::pp_68.8M
## NAME
323
324
117M
  unsigned Len = getLength();
325
117M
  if (Len < 2) 
return tok::pp_not_keyword80.3k
;
326
117M
  const char *Name = getNameStart();
327
117M
  switch (HASH(Len, Name[0], Name[2])) {
328
48.2M
  default: return tok::pp_not_keyword;
329
2.16M
  CASE
(0
2, 'i', '\0', if);
330
258k
  CASE
(0
4, 'e', 'i', elif);
331
1.20M
  CASE
(0
4, 'e', 's', else);
332
98.5k
  CASE
(0
4, 'l', 'n', line);
333
5.61k
  CASE
(0
4, 's', 'c', sccs);
334
6.10M
  CASE
(0
5, 'e', 'd', endif);
335
11.8k
  CASE
(0
5, 'e', 'r', error);
336
3.28k
  CASE
(0
5, 'i', 'e', ident);
337
1.34M
  CASE
(0
5, 'i', 'd', ifdef);
338
121k
  CASE
(0
5, 'u', 'd', undef);
339
340
31.4k
  CASE
(0
6, 'a', 's', assert);
341
48.6M
  CASE
(0
6, 'd', 'f', define);
342
5.05M
  CASE
(0
6, 'i', 'n', ifndef);
343
283k
  CASE
(0
6, 'i', 'p', import);
344
676k
  CASE
(0
6, 'p', 'a', pragma);
345
346
10.5k
  CASE
(0
7, 'd', 'f', defined);
347
9.29k
  CASE
(0
7, 'e', 'i', elifdef);
348
3.09M
  CASE
(0
7, 'i', 'c', include);
349
1.34k
  CASE
(0
7, 'w', 'r', warning);
350
351
43.7k
  CASE
(0
8, 'e', 'i', elifndef);
352
5.92k
  CASE
(0
8, 'u', 'a', unassert);
353
46.8k
  CASE
(0
12, 'i', 'c', include_next);
354
355
5.33k
  CASE
(0
14, '_', 'p', __public_macro);
356
357
94.8k
  CASE
(0
15, '_', 'p', __private_macro);
358
359
117M
  
CASE264k
(16, '_', 'i', __include_macros);
360
117M
#undef CASE
361
117M
#undef HASH
362
117M
  }
363
117M
}
364
365
//===----------------------------------------------------------------------===//
366
// Stats Implementation
367
//===----------------------------------------------------------------------===//
368
369
/// PrintStats - Print statistics about how well the identifier table is doing
370
/// at hashing identifiers.
371
3
void IdentifierTable::PrintStats() const {
372
3
  unsigned NumBuckets = HashTable.getNumBuckets();
373
3
  unsigned NumIdentifiers = HashTable.getNumItems();
374
3
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
375
3
  unsigned AverageIdentifierSize = 0;
376
3
  unsigned MaxIdentifierLength = 0;
377
378
  // TODO: Figure out maximum times an identifier had to probe for -stats.
379
3
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
380
4.14k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I4.14k
) {
381
4.14k
    unsigned IdLen = I->getKeyLength();
382
4.14k
    AverageIdentifierSize += IdLen;
383
4.14k
    if (MaxIdentifierLength < IdLen)
384
15
      MaxIdentifierLength = IdLen;
385
4.14k
  }
386
387
3
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
388
3
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
389
3
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
390
3
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
391
3
          NumIdentifiers/(double)NumBuckets);
392
3
  fprintf(stderr, "Ave identifier length: %f\n",
393
3
          (AverageIdentifierSize/(double)NumIdentifiers));
394
3
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
395
396
  // Compute statistics about the memory allocated for identifiers.
397
3
  HashTable.getAllocator().PrintStats();
398
3
}
399
400
//===----------------------------------------------------------------------===//
401
// SelectorTable Implementation
402
//===----------------------------------------------------------------------===//
403
404
6.67M
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
405
6.67M
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
406
6.67M
}
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
241k
      : DeclarationNameExtra(nKeys) {
423
241k
    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
896k
    for (unsigned i = 0; i != nKeys; 
++i655k
)
428
655k
      KeyInfo[i] = IIV[i];
429
241k
  }
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.68M
  keyword_iterator keyword_begin() const {
439
1.68M
    return reinterpret_cast<keyword_iterator>(this + 1);
440
1.68M
  }
441
442
26.1k
  keyword_iterator keyword_end() const {
443
26.1k
    return keyword_begin() + getNumArgs();
444
26.1k
  }
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
948k
                      unsigned NumArgs) {
453
948k
    ID.AddInteger(NumArgs);
454
3.45M
    for (unsigned i = 0; i != NumArgs; 
++i2.50M
)
455
2.50M
      ID.AddPointer(ArgTys[i]);
456
948k
  }
457
458
629k
  void Profile(llvm::FoldingSetNodeID &ID) {
459
629k
    Profile(ID, keyword_begin(), getNumArgs());
460
629k
  }
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.11M
unsigned Selector::getNumArgs() const {
481
3.11M
  unsigned IIF = getIdentifierInfoFlag();
482
3.11M
  if (IIF <= ZeroArg)
483
820k
    return 0;
484
2.28M
  if (IIF == OneArg)
485
1.15M
    return 1;
486
  // We point to a MultiKeywordSelector.
487
1.13M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
488
1.13M
  return SI->getNumArgs();
489
2.28M
}
490
491
2.42M
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
492
2.42M
  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.42M
}
501
502
256k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
503
256k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
504
256k
  return II ? 
II->getName()256k
:
StringRef()12
;
505
256k
}
506
507
26.1k
std::string MultiKeywordSelector::getName() const {
508
26.1k
  SmallString<256> Str;
509
26.1k
  llvm::raw_svector_ostream OS(Str);
510
99.7k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I73.6k
) {
511
73.6k
    if (*I)
512
73.1k
      OS << (*I)->getName();
513
73.6k
    OS << ':';
514
73.6k
  }
515
516
26.1k
  return std::string(OS.str());
517
26.1k
}
518
519
174k
std::string Selector::getAsString() const {
520
174k
  if (InfoPtr == 0)
521
6
    return "<null selector>";
522
523
174k
  if (getIdentifierInfoFlag() < MultiArg) {
524
147k
    IdentifierInfo *II = getAsIdentifierInfo();
525
526
147k
    if (getNumArgs() == 0) {
527
117k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
528
117k
                   "not be null.");
529
0
      return std::string(II->getName());
530
117k
    }
531
532
30.4k
    if (!II)
533
137
      return ":";
534
535
30.2k
    return II->getName().str() + ":";
536
30.4k
  }
537
538
  // We have a multiple keyword selector.
539
26.1k
  return getMultiKeywordSelector()->getName();
540
174k
}
541
542
13.0k
void Selector::print(llvm::raw_ostream &OS) const {
543
13.0k
  OS << getAsString();
544
13.0k
}
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
112k
static bool startsWithWord(StringRef name, StringRef word) {
552
112k
  if (name.size() < word.size()) 
return false3.93k
;
553
108k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])93.5k
) &&
554
108k
          
name.startswith(word)69.2k
);
555
112k
}
556
557
238k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
558
238k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
559
238k
  if (!first) 
return OMF_None22
;
560
561
238k
  StringRef name = first->getName();
562
238k
  if (sel.isUnarySelector()) {
563
67.8k
    if (name == "autorelease") 
return OMF_autorelease950
;
564
66.9k
    if (name == "dealloc") 
return OMF_dealloc844
;
565
66.0k
    if (name == "finalize") 
return OMF_finalize152
;
566
65.9k
    if (name == "release") 
return OMF_release1.36k
;
567
64.5k
    if (name == "retain") 
return OMF_retain701
;
568
63.8k
    if (name == "retainCount") 
return OMF_retainCount107
;
569
63.7k
    if (name == "self") 
return OMF_self96
;
570
63.6k
    if (name == "initialize") 
return OMF_initialize31
;
571
63.6k
  }
572
573
233k
  if (name == "performSelector" || 
name == "performSelectorInBackground"232k
||
574
233k
      
name == "performSelectorOnMainThread"232k
)
575
972
    return OMF_performSelector;
576
577
  // The other method families may begin with a prefix of underscores.
578
233k
  
while (232k
!name.empty() && name.front() == '_')
579
233
    name = name.substr(1);
580
581
232k
  if (name.empty()) 
return OMF_None0
;
582
232k
  switch (name.front()) {
583
18.7k
  case 'a':
584
18.7k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc4.94k
;
585
13.7k
    break;
586
14.9k
  case 'c':
587
14.9k
    if (startsWithWord(name, "copy")) 
return OMF_copy1.49k
;
588
13.4k
    break;
589
61.0k
  case 'i':
590
61.0k
    if (startsWithWord(name, "init")) 
return OMF_init55.0k
;
591
6.01k
    break;
592
7.75k
  case 'm':
593
7.75k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy979
;
594
6.78k
    break;
595
9.48k
  case 'n':
596
9.48k
    if (startsWithWord(name, "new")) 
return OMF_new1.50k
;
597
7.98k
    break;
598
120k
  default:
599
120k
    break;
600
232k
  }
601
602
168k
  return OMF_None;
603
232k
}
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.2k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
634
25.2k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
635
25.2k
  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.48k
      break;
643
644
5.48k
    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.76k
    case 's':
653
3.76k
      if (name == "stringByAppendingFormat" ||
654
3.76k
          
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
418k
static SelectorTableImpl &getSelectorTableImpl(void *P) {
670
418k
  return *static_cast<SelectorTableImpl*>(P);
671
418k
}
672
673
SmallString<64>
674
355k
SelectorTable::constructSetterName(StringRef Name) {
675
355k
  SmallString<64> SetterName("set");
676
355k
  SetterName += Name;
677
355k
  SetterName[3] = toUppercase(SetterName[3]);
678
355k
  return SetterName;
679
355k
}
680
681
Selector
682
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
683
                                       SelectorTable &SelTable,
684
351k
                                       const IdentifierInfo *Name) {
685
351k
  IdentifierInfo *SetterName =
686
351k
    &Idents.get(constructSetterName(Name->getName()));
687
351k
  return SelTable.getUnarySelector(SetterName);
688
351k
}
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
687k
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
702
687k
  if (nKeys < 2)
703
368k
    return Selector(IIV[0], nKeys);
704
705
319k
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
706
707
  // Unique selector, to guarantee there is one per name.
708
319k
  llvm::FoldingSetNodeID ID;
709
319k
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
710
711
319k
  void *InsertPos = nullptr;
712
319k
  if (MultiKeywordSelector *SI =
713
319k
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
714
77.5k
    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
241k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
719
241k
  MultiKeywordSelector *SI =
720
241k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
721
241k
          Size, alignof(MultiKeywordSelector));
722
241k
  new (SI) MultiKeywordSelector(nKeys, IIV);
723
241k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
724
241k
  return Selector(SI);
725
319k
}
726
727
104k
SelectorTable::SelectorTable() {
728
104k
  Impl = new SelectorTableImpl();
729
104k
}
730
731
98.9k
SelectorTable::~SelectorTable() {
732
98.9k
  delete &getSelectorTableImpl(Impl);
733
98.9k
}
734
735
1.24M
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
736
1.24M
  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.24M
  case OO_##Name: return Spelling;
743
1.24M
#include 
"clang/Basic/OperatorKinds.def"0
744
1.24M
  }
745
746
0
  llvm_unreachable("Invalid OverloadedOperatorKind!");
747
0
}
748
749
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
750
903
                                        bool isContextSensitive) {
751
903
  switch (kind) {
752
476
  case NullabilityKind::NonNull:
753
476
    return isContextSensitive ? 
"nonnull"2
:
"_Nonnull"474
;
754
755
427
  case NullabilityKind::Nullable:
756
427
    return isContextSensitive ? 
"nullable"2
:
"_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
903
  }
766
0
  llvm_unreachable("Unknown nullability kind.");
767
0
}