Coverage Report

Created: 2020-09-19 12:23

/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
361
IdentifierIterator::~IdentifierIterator() = default;
47
48
11.2k
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
129k
      ExternalLookup(ExternalLookup) {}
69
70
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
71
                                 IdentifierInfoLookup *ExternalLookup)
72
48.8k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
48.8k
  AddKeywords(LangOpts);
76
48.8k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
72
48.8k
    : IdentifierTable(ExternalLookup) {
73
  // Populate the identifier table with info about keywords for the current
74
  // language.
75
48.8k
  AddKeywords(LangOpts);
76
48.8k
}
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
    KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX20,
111
    KEYALL = (0xffffff & ~KEYNOMS18 &
112
              ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
113
  };
114
115
  /// How a keyword is treated in the selected standard.
116
  enum KeywordStatus {
117
    KS_Disabled,    // Disabled
118
    KS_Extension,   // Is an extension
119
    KS_Enabled,     // Enabled
120
    KS_Future       // Is a keyword in future standard
121
  };
122
123
} // namespace
124
125
/// Translates flags as specified in TokenKinds.def into keyword status
126
/// in the given language standard.
127
static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
128
42.7M
                                      unsigned Flags) {
129
42.7M
  if (Flags == KEYALL) 
return KS_Enabled13.8M
;
130
28.9M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)24.0M
)
return KS_Enabled10.6M
;
131
18.2M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)11.4M
)
return KS_Enabled919k
;
132
17.3M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCXX20)4.53M
)
return KS_Enabled278k
;
133
17.0M
  if (LangOpts.C99 && 
(Flags & KEYC99)8.59M
)
return KS_Enabled99.5k
;
134
16.9M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)12.0M
)
return KS_Extension108k
;
135
16.8M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)5.27M
)
return KS_Extension2.19M
;
136
14.6M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)867k
)
return KS_Enabled3.68k
;
137
14.6M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)805
)
return KS_Extension54
;
138
14.6M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)10.0M
)
return KS_Enabled324k
;
139
14.3M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)136k
)
return KS_Enabled665
;
140
14.3M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)7.54M
)
return KS_Enabled71.2k
;
141
14.2M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)2.32M
)
return KS_Enabled39.7k
;
142
14.2M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)34.4k
)
return KS_Enabled704
;
143
14.2M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)5.23k
)
return KS_Enabled81
;
144
14.2M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus136k
&&
(Flags & KEYOPENCLC)128k
)
145
19.6k
    return KS_Enabled;
146
14.2M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)7.95k
)
return KS_Enabled2.33k
;
147
14.2M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)4.73M
)
return KS_Enabled110k
;
148
14.1M
  if (LangOpts.C11 && 
(Flags & KEYC11)4.02M
)
return KS_Enabled0
;
149
  // We treat bridge casts as objective-C keywords so we can warn on them
150
  // in non-arc mode.
151
14.1M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)5.70M
)
return KS_Enabled446k
;
152
13.6M
  if (LangOpts.CPlusPlus20 && 
(Flags & KEYCONCEPTS)2.02M
)
return KS_Enabled0
;
153
13.6M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)290k
)
return KS_Enabled168
;
154
13.6M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)15.3k
)
return KS_Enabled280
;
155
13.6M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)9.07M
)
return KS_Future630k
;
156
13.0M
  if (LangOpts.CPlusPlus && 
!LangOpts.CPlusPlus208.44M
&&
(Flags & CHAR8SUPPORT)6.41M
)
157
67.9k
    return KS_Future;
158
12.9M
  return KS_Disabled;
159
12.9M
}
160
161
/// AddKeyword - This method is used to associate a token ID with specific
162
/// identifiers because they are language keywords.  This causes the lexer to
163
/// automatically map matching identifiers to specialized token codes.
164
static void AddKeyword(StringRef Keyword,
165
                       tok::TokenKind TokenCode, unsigned Flags,
166
42.7M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
167
42.7M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
168
42.7M
169
  // Don't add this keyword under MSVCCompat.
170
42.7M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)3.51M
&&
171
21.3k
      !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
172
5.19k
    return;
173
42.7M
174
  // Don't add this keyword under OpenCL.
175
42.7M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)219k
)
176
667
    return;
177
42.7M
178
  // Don't add this keyword if disabled in this language.
179
42.7M
  if (AddResult == KS_Disabled) 
return12.9M
;
180
29.7M
181
29.7M
  IdentifierInfo &Info =
182
29.0M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier695k
: TokenCode);
183
29.7M
  Info.setIsExtensionToken(AddResult == KS_Extension);
184
29.7M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
185
29.7M
}
186
187
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
188
/// representations.
189
static void AddCXXOperatorKeyword(StringRef Keyword,
190
                                  tok::TokenKind TokenCode,
191
922k
                                  IdentifierTable &Table) {
192
922k
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
193
922k
  Info.setIsCPlusPlusOperatorKeyword();
194
922k
}
195
196
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
197
/// or "property".
198
static void AddObjCKeyword(StringRef Name,
199
                           tok::ObjCKeywordKind ObjCID,
200
1.72M
                           IdentifierTable &Table) {
201
1.72M
  Table.get(Name).setObjCKeywordID(ObjCID);
202
1.72M
}
203
204
/// AddKeywords - Add all keywords to the symbol table.
205
///
206
129k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
207
  // Add keywords and tokens for the current language.
208
129k
#define KEYWORD(NAME, FLAGS) \
209
35.4M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
210
35.4M
             FLAGS, LangOpts, *this);
211
129k
#define ALIAS(NAME, TOK, FLAGS) \
212
7.26M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
213
7.26M
             FLAGS, LangOpts, *this);
214
129k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
215
1.42M
  if (LangOpts.CXXOperatorNames)          \
216
922k
    AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
217
129k
#define OBJC_AT_KEYWORD(NAME)  \
218
3.50M
  if (LangOpts.ObjC)           \
219
1.72M
    AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
220
129k
#define TESTING_KEYWORD(NAME, FLAGS)
221
129k
#include "clang/Basic/TokenKinds.def"
222
129k
223
129k
  if (LangOpts.ParseUnknownAnytype)
224
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
225
9
               LangOpts, *this);
226
129k
227
129k
  if (LangOpts.DeclSpecKeyword)
228
48.1k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
229
129k
230
  // Add the 'import' contextual keyword.
231
129k
  get("import").setModulesImport(true);
232
129k
}
233
234
/// Checks if the specified token kind represents a keyword in the
235
/// specified language.
236
/// \returns Status of the keyword in the language.
237
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
238
3.20M
                                      tok::TokenKind K) {
239
3.20M
  switch (K) {
240
0
#define KEYWORD(NAME, FLAGS) \
241
178
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
242
0
#include "clang/Basic/TokenKinds.def"
243
3.20M
  default: return KS_Disabled;
244
3.20M
  }
245
3.20M
}
246
247
/// Returns true if the identifier represents a keyword in the
248
/// specified language.
249
3.20M
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
250
3.20M
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
251
160
  case KS_Enabled:
252
160
  case KS_Extension:
253
160
    return true;
254
3.20M
  default:
255
3.20M
    return false;
256
3.20M
  }
257
3.20M
}
258
259
/// Returns true if the identifier represents a C++ keyword in the
260
/// specified language.
261
19
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
262
19
  if (!LangOpts.CPlusPlus || 
!isKeyword(LangOpts)18
)
263
1
    return false;
264
  // This is a C++ keyword if this identifier is not a keyword when checked
265
  // using LangOptions without C++ support.
266
18
  LangOptions LangOptsNoCPP = LangOpts;
267
18
  LangOptsNoCPP.CPlusPlus = false;
268
18
  LangOptsNoCPP.CPlusPlus11 = false;
269
18
  LangOptsNoCPP.CPlusPlus20 = false;
270
18
  return !isKeyword(LangOptsNoCPP);
271
18
}
272
273
117M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
274
  // We use a perfect hash function here involving the length of the keyword,
275
  // the first and third character.  For preprocessor ID's there are no
276
  // collisions (if there were, the switch below would complain about duplicate
277
  // case values).  Note that this depends on 'if' being null terminated.
278
117M
279
117M
#define HASH(LEN, FIRST, THIRD) \
280
186M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
281
117M
#define CASE(LEN, FIRST, THIRD, NAME) \
282
68.8M
  case HASH(LEN, FIRST, THIRD): \
283
68.2M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword612k
: tok::pp_ ## NAME
284
117M
285
117M
  unsigned Len = getLength();
286
117M
  if (Len < 2) 
return tok::pp_not_keyword66.3k
;
287
117M
  const char *Name = getNameStart();
288
117M
  switch (HASH(Len, Name[0], Name[2])) {
289
48.9M
  default: return tok::pp_not_keyword;
290
3.63M
  CASE( 2, 'i', '\0', if);
291
247k
  CASE( 4, 'e', 'i', elif);
292
1.82M
  CASE( 4, 'e', 's', else);
293
50.2k
  CASE( 4, 'l', 'n', line);
294
3.38k
  CASE( 4, 's', 'c', sccs);
295
3.63M
  CASE( 5, 'e', 'd', endif);
296
18.6k
  CASE( 5, 'e', 'r', error);
297
4.37k
  CASE( 5, 'i', 'e', ident);
298
1.81M
  CASE( 5, 'i', 'd', ifdef);
299
210k
  CASE( 5, 'u', 'd', undef);
300
0
301
38.8k
  CASE( 6, 'a', 's', assert);
302
49.1M
  CASE( 6, 'd', 'f', define);
303
1.97M
  CASE( 6, 'i', 'n', ifndef);
304
661k
  CASE( 6, 'i', 'p', import);
305
1.06M
  CASE( 6, 'p', 'a', pragma);
306
0
307
16.4k
  CASE( 7, 'd', 'f', defined);
308
4.11M
  CASE( 7, 'i', 'c', include);
309
2.05k
  CASE( 7, 'w', 'r', warning);
310
0
311
6.62k
  CASE( 8, 'u', 'a', unassert);
312
59.9k
  CASE(12, 'i', 'c', include_next);
313
0
314
6.39k
  CASE(14, '_', 'p', __public_macro);
315
0
316
78.8k
  CASE(15, '_', 'p', __private_macro);
317
0
318
218k
  CASE(16, '_', 'i', __include_macros);
319
117M
#undef CASE
320
117M
#undef HASH
321
117M
  }
322
117M
}
323
324
//===----------------------------------------------------------------------===//
325
// Stats Implementation
326
//===----------------------------------------------------------------------===//
327
328
/// PrintStats - Print statistics about how well the identifier table is doing
329
/// at hashing identifiers.
330
3
void IdentifierTable::PrintStats() const {
331
3
  unsigned NumBuckets = HashTable.getNumBuckets();
332
3
  unsigned NumIdentifiers = HashTable.getNumItems();
333
3
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
334
3
  unsigned AverageIdentifierSize = 0;
335
3
  unsigned MaxIdentifierLength = 0;
336
3
337
  // TODO: Figure out maximum times an identifier had to probe for -stats.
338
3
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
339
3.86k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I3.86k
) {
340
3.86k
    unsigned IdLen = I->getKeyLength();
341
3.86k
    AverageIdentifierSize += IdLen;
342
3.86k
    if (MaxIdentifierLength < IdLen)
343
18
      MaxIdentifierLength = IdLen;
344
3.86k
  }
345
3
346
3
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
347
3
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
348
3
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
349
3
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
350
3
          NumIdentifiers/(double)NumBuckets);
351
3
  fprintf(stderr, "Ave identifier length: %f\n",
352
3
          (AverageIdentifierSize/(double)NumIdentifiers));
353
3
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
354
3
355
  // Compute statistics about the memory allocated for identifiers.
356
3
  HashTable.getAllocator().PrintStats();
357
3
}
358
359
//===----------------------------------------------------------------------===//
360
// SelectorTable Implementation
361
//===----------------------------------------------------------------------===//
362
363
8.69M
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
364
8.69M
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
365
8.69M
}
366
367
namespace clang {
368
369
/// One of these variable length records is kept for each
370
/// selector containing more than one keyword. We use a folding set
371
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
372
/// this class is provided strictly through Selector.
373
class alignas(IdentifierInfoAlignment) MultiKeywordSelector
374
    : public detail::DeclarationNameExtra,
375
      public llvm::FoldingSetNode {
376
0
  MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
377
378
public:
379
  // Constructor for keyword selectors.
380
  MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
381
316k
      : DeclarationNameExtra(nKeys) {
382
316k
    assert((nKeys > 1) && "not a multi-keyword selector");
383
316k
384
    // Fill in the trailing keyword array.
385
316k
    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
386
1.17M
    for (unsigned i = 0; i != nKeys; 
++i857k
)
387
857k
      KeyInfo[i] = IIV[i];
388
316k
  }
389
390
  // getName - Derive the full selector name and return it.
391
  std::string getName() const;
392
393
  using DeclarationNameExtra::getNumArgs;
394
395
  using keyword_iterator = IdentifierInfo *const *;
396
397
2.17M
  keyword_iterator keyword_begin() const {
398
2.17M
    return reinterpret_cast<keyword_iterator>(this + 1);
399
2.17M
  }
400
401
34.4k
  keyword_iterator keyword_end() const {
402
34.4k
    return keyword_begin() + getNumArgs();
403
34.4k
  }
404
405
1.28M
  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
406
1.28M
    assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
407
1.28M
    return keyword_begin()[i];
408
1.28M
  }
409
410
  static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
411
1.21M
                      unsigned NumArgs) {
412
1.21M
    ID.AddInteger(NumArgs);
413
4.42M
    for (unsigned i = 0; i != NumArgs; 
++i3.21M
)
414
3.21M
      ID.AddPointer(ArgTys[i]);
415
1.21M
  }
416
417
813k
  void Profile(llvm::FoldingSetNodeID &ID) {
418
813k
    Profile(ID, keyword_begin(), getNumArgs());
419
813k
  }
420
};
421
422
} // namespace clang.
423
424
5
bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
425
5
  assert(!Names.empty() && "must have >= 1 selector slots");
426
5
  if (getNumArgs() != Names.size())
427
0
    return false;
428
14
  
for (unsigned I = 0, E = Names.size(); 5
I != E;
++I9
) {
429
11
    if (getNameForSlot(I) != Names[I])
430
2
      return false;
431
11
  }
432
3
  return true;
433
5
}
434
435
0
bool Selector::isUnarySelector(StringRef Name) const {
436
0
  return isUnarySelector() && getNameForSlot(0) == Name;
437
0
}
438
439
3.81M
unsigned Selector::getNumArgs() const {
440
3.81M
  unsigned IIF = getIdentifierInfoFlag();
441
3.81M
  if (IIF <= ZeroArg)
442
933k
    return 0;
443
2.87M
  if (IIF == OneArg)
444
1.40M
    return 1;
445
  // We point to a MultiKeywordSelector.
446
1.46M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
447
1.46M
  return SI->getNumArgs();
448
1.46M
}
449
450
2.93M
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
451
2.93M
  if (getIdentifierInfoFlag() < MultiArg) {
452
1.65M
    assert(argIndex == 0 && "illegal keyword index");
453
1.65M
    return getAsIdentifierInfo();
454
1.65M
  }
455
1.28M
456
  // We point to a MultiKeywordSelector.
457
1.28M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
458
1.28M
  return SI->getIdentifierInfoForSlot(argIndex);
459
1.28M
}
460
461
306k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
462
306k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
463
306k
  return II ? II->getName() : 
StringRef()13
;
464
306k
}
465
466
34.4k
std::string MultiKeywordSelector::getName() const {
467
34.4k
  SmallString<256> Str;
468
34.4k
  llvm::raw_svector_ostream OS(Str);
469
131k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I97.0k
) {
470
97.0k
    if (*I)
471
96.6k
      OS << (*I)->getName();
472
97.0k
    OS << ':';
473
97.0k
  }
474
34.4k
475
34.4k
  return std::string(OS.str());
476
34.4k
}
477
478
201k
std::string Selector::getAsString() const {
479
201k
  if (InfoPtr == 0)
480
6
    return "<null selector>";
481
201k
482
201k
  if (getIdentifierInfoFlag() < MultiArg) {
483
166k
    IdentifierInfo *II = getAsIdentifierInfo();
484
166k
485
166k
    if (getNumArgs() == 0) {
486
129k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
487
129k
                   "not be null.");
488
129k
      return std::string(II->getName());
489
129k
    }
490
37.2k
491
37.2k
    if (!II)
492
137
      return ":";
493
37.0k
494
37.0k
    return II->getName().str() + ":";
495
37.0k
  }
496
34.4k
497
  // We have a multiple keyword selector.
498
34.4k
  return getMultiKeywordSelector()->getName();
499
34.4k
}
500
501
10.4k
void Selector::print(llvm::raw_ostream &OS) const {
502
10.4k
  OS << getAsString();
503
10.4k
}
504
505
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
506
507
/// Interpreting the given string using the normal CamelCase
508
/// conventions, determine whether the given string starts with the
509
/// given "word", which is assumed to end in a lowercase letter.
510
136k
static bool startsWithWord(StringRef name, StringRef word) {
511
136k
  if (name.size() < word.size()) 
return false3.98k
;
512
132k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])116k
) &&
513
86.7k
          name.startswith(word));
514
132k
}
515
516
284k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
517
284k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
518
284k
  if (!first) 
return OMF_None22
;
519
284k
520
284k
  StringRef name = first->getName();
521
284k
  if (sel.isUnarySelector()) {
522
73.3k
    if (name == "autorelease") 
return OMF_autorelease1.10k
;
523
72.2k
    if (name == "dealloc") 
return OMF_dealloc854
;
524
71.4k
    if (name == "finalize") 
return OMF_finalize150
;
525
71.2k
    if (name == "release") 
return OMF_release1.35k
;
526
69.9k
    if (name == "retain") 
return OMF_retain700
;
527
69.2k
    if (name == "retainCount") 
return OMF_retainCount106
;
528
69.0k
    if (name == "self") 
return OMF_self95
;
529
69.0k
    if (name == "initialize") 
return OMF_initialize30
;
530
279k
  }
531
279k
532
279k
  if (name == "performSelector" || 
name == "performSelectorInBackground"278k
||
533
278k
      name == "performSelectorOnMainThread")
534
964
    return OMF_performSelector;
535
278k
536
  // The other method families may begin with a prefix of underscores.
537
279k
  
while (278k
!name.empty() && name.front() == '_')
538
233
    name = name.substr(1);
539
278k
540
278k
  if (name.empty()) 
return OMF_None0
;
541
278k
  switch (name.front()) {
542
21.3k
  case 'a':
543
21.3k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc5.21k
;
544
16.1k
    break;
545
17.5k
  case 'c':
546
17.5k
    if (startsWithWord(name, "copy")) 
return OMF_copy1.56k
;
547
16.0k
    break;
548
77.4k
  case 'i':
549
77.4k
    if (startsWithWord(name, "init")) 
return OMF_init70.9k
;
550
6.43k
    break;
551
8.62k
  case 'm':
552
8.62k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy1.05k
;
553
7.56k
    break;
554
11.3k
  case 'n':
555
11.3k
    if (startsWithWord(name, "new")) 
return OMF_new1.59k
;
556
9.72k
    break;
557
142k
  default:
558
142k
    break;
559
198k
  }
560
198k
561
198k
  return OMF_None;
562
198k
}
563
564
554
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
565
554
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
566
554
  if (!first) 
return OIT_None2
;
567
552
568
552
  StringRef name = first->getName();
569
552
570
552
  if (name.empty()) 
return OIT_None0
;
571
552
  switch (name.front()) {
572
53
    case 'a':
573
53
      if (startsWithWord(name, "array")) 
return OIT_Array15
;
574
38
      break;
575
37
    case 'd':
576
37
      if (startsWithWord(name, "default")) 
return OIT_ReturnsSelf2
;
577
35
      if (startsWithWord(name, "dictionary")) 
return OIT_Dictionary11
;
578
24
      break;
579
35
    case 's':
580
35
      if (startsWithWord(name, "shared")) 
return OIT_ReturnsSelf2
;
581
33
      if (startsWithWord(name, "standard")) 
return OIT_Singleton1
;
582
32
      break;
583
93
    case 'i':
584
93
      if (startsWithWord(name, "init")) 
return OIT_Init45
;
585
48
      break;
586
334
    default:
587
334
      break;
588
476
  }
589
476
  return OIT_None;
590
476
}
591
592
25.8k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
593
25.8k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
594
25.8k
  if (!first) 
return SFF_None6
;
595
25.8k
596
25.8k
  StringRef name = first->getName();
597
25.8k
598
25.8k
  switch (name.front()) {
599
5.80k
    case 'a':
600
5.80k
      if (name == "appendFormat") 
return SFF_NSString1
;
601
5.80k
      break;
602
5.80k
603
3.35k
    case 'i':
604
3.35k
      if (name == "initWithFormat") 
return SFF_NSString7
;
605
3.34k
      break;
606
3.34k
607
282
    case 'l':
608
282
      if (name == "localizedStringWithFormat") 
return SFF_NSString2
;
609
280
      break;
610
280
611
3.93k
    case 's':
612
3.93k
      if (name == "stringByAppendingFormat" ||
613
3.89k
          name == "stringWithFormat") 
return SFF_NSString734
;
614
3.20k
      break;
615
25.1k
  }
616
25.1k
  return SFF_None;
617
25.1k
}
618
619
namespace {
620
621
struct SelectorTableImpl {
622
  llvm::FoldingSet<MultiKeywordSelector> Table;
623
  llvm::BumpPtrAllocator Allocator;
624
};
625
626
} // namespace
627
628
481k
static SelectorTableImpl &getSelectorTableImpl(void *P) {
629
481k
  return *static_cast<SelectorTableImpl*>(P);
630
481k
}
631
632
SmallString<64>
633
467k
SelectorTable::constructSetterName(StringRef Name) {
634
467k
  SmallString<64> SetterName("set");
635
467k
  SetterName += Name;
636
467k
  SetterName[3] = toUppercase(SetterName[3]);
637
467k
  return SetterName;
638
467k
}
639
640
Selector
641
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
642
                                       SelectorTable &SelTable,
643
462k
                                       const IdentifierInfo *Name) {
644
462k
  IdentifierInfo *SetterName =
645
462k
    &Idents.get(constructSetterName(Name->getName()));
646
462k
  return SelTable.getUnarySelector(SetterName);
647
462k
}
648
649
1
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
650
1
  StringRef Name = Sel.getNameForSlot(0);
651
1
  assert(Name.startswith("set") && "invalid setter name");
652
1
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
653
1
}
654
655
1
size_t SelectorTable::getTotalMemory() const {
656
1
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
657
1
  return SelTabImpl.Allocator.getTotalMemory();
658
1
}
659
660
856k
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
661
856k
  if (nKeys < 2)
662
455k
    return Selector(IIV[0], nKeys);
663
401k
664
401k
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
665
401k
666
  // Unique selector, to guarantee there is one per name.
667
401k
  llvm::FoldingSetNodeID ID;
668
401k
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
669
401k
670
401k
  void *InsertPos = nullptr;
671
401k
  if (MultiKeywordSelector *SI =
672
85.3k
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
673
85.3k
    return Selector(SI);
674
316k
675
  // MultiKeywordSelector objects are not allocated with new because they have a
676
  // variable size array (for parameter types) at the end of them.
677
316k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
678
316k
  MultiKeywordSelector *SI =
679
316k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
680
316k
          Size, alignof(MultiKeywordSelector));
681
316k
  new (SI) MultiKeywordSelector(nKeys, IIV);
682
316k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
683
316k
  return Selector(SI);
684
316k
}
685
686
88.3k
SelectorTable::SelectorTable() {
687
88.3k
  Impl = new SelectorTableImpl();
688
88.3k
}
689
690
79.9k
SelectorTable::~SelectorTable() {
691
79.9k
  delete &getSelectorTableImpl(Impl);
692
79.9k
}
693
694
155k
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
695
155k
  switch (Operator) {
696
0
  case OO_None:
697
0
  case NUM_OVERLOADED_OPERATORS:
698
0
    return nullptr;
699
0
700
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
701
155k
  case OO_##Name: return Spelling;
702
0
#include "clang/Basic/OperatorKinds.def"
703
155k
  }
704
155k
705
155k
  
llvm_unreachable0
("Invalid OverloadedOperatorKind!");
706
155k
}
707
708
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
709
905
                                        bool isContextSensitive) {
710
905
  switch (kind) {
711
478
  case NullabilityKind::NonNull:
712
476
    return isContextSensitive ? 
"nonnull"2
: "_Nonnull";
713
0
714
427
  case NullabilityKind::Nullable:
715
425
    return isContextSensitive ? 
"nullable"2
: "_Nullable";
716
0
717
0
  case NullabilityKind::Unspecified:
718
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
719
0
  }
720
0
  llvm_unreachable("Unknown nullability kind.");
721
0
}