Coverage Report

Created: 2020-02-25 14:32

/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/TokenKinds.h"
20
#include "llvm/ADT/DenseMapInfo.h"
21
#include "llvm/ADT/FoldingSet.h"
22
#include "llvm/ADT/SmallString.h"
23
#include "llvm/ADT/StringMap.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/Support/Allocator.h"
26
#include "llvm/Support/ErrorHandling.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <cassert>
29
#include <cstdio>
30
#include <cstring>
31
#include <string>
32
33
using namespace clang;
34
35
//===----------------------------------------------------------------------===//
36
// IdentifierTable Implementation
37
//===----------------------------------------------------------------------===//
38
39
360
IdentifierIterator::~IdentifierIterator() = default;
40
41
7.80k
IdentifierInfoLookup::~IdentifierInfoLookup() = default;
42
43
namespace {
44
45
/// A simple identifier lookup iterator that represents an
46
/// empty sequence of identifiers.
47
class EmptyLookupIterator : public IdentifierIterator
48
{
49
public:
50
0
  StringRef Next() override { return StringRef(); }
51
};
52
53
} // namespace
54
55
0
IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
56
0
  return new EmptyLookupIterator();
57
0
}
58
59
IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup)
60
    : HashTable(8192), // Start with space for 8K identifiers.
61
102k
      ExternalLookup(ExternalLookup) {}
62
63
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
64
                                 IdentifierInfoLookup *ExternalLookup)
65
42.6k
    : IdentifierTable(ExternalLookup) {
66
42.6k
  // Populate the identifier table with info about keywords for the current
67
42.6k
  // language.
68
42.6k
  AddKeywords(LangOpts);
69
42.6k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
65
42.6k
    : IdentifierTable(ExternalLookup) {
66
42.6k
  // Populate the identifier table with info about keywords for the current
67
42.6k
  // language.
68
42.6k
  AddKeywords(LangOpts);
69
42.6k
}
70
71
//===----------------------------------------------------------------------===//
72
// Language Keyword Implementation
73
//===----------------------------------------------------------------------===//
74
75
// Constants for TokenKinds.def
76
namespace {
77
78
  enum {
79
    KEYC99        = 0x1,
80
    KEYCXX        = 0x2,
81
    KEYCXX11      = 0x4,
82
    KEYGNU        = 0x8,
83
    KEYMS         = 0x10,
84
    BOOLSUPPORT   = 0x20,
85
    KEYALTIVEC    = 0x40,
86
    KEYNOCXX      = 0x80,
87
    KEYBORLAND    = 0x100,
88
    KEYOPENCLC    = 0x200,
89
    KEYC11        = 0x400,
90
    KEYNOMS18     = 0x800,
91
    KEYNOOPENCL   = 0x1000,
92
    WCHARSUPPORT  = 0x2000,
93
    HALFSUPPORT   = 0x4000,
94
    CHAR8SUPPORT  = 0x8000,
95
    KEYCONCEPTS   = 0x10000,
96
    KEYOBJC       = 0x20000,
97
    KEYZVECTOR    = 0x40000,
98
    KEYCOROUTINES = 0x80000,
99
    KEYMODULES    = 0x100000,
100
    KEYCXX2A      = 0x200000,
101
    KEYOPENCLCXX  = 0x400000,
102
    KEYMSCOMPAT   = 0x800000,
103
    KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
104
    KEYALL = (0xffffff & ~KEYNOMS18 &
105
              ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
106
  };
107
108
  /// How a keyword is treated in the selected standard.
109
  enum KeywordStatus {
110
    KS_Disabled,    // Disabled
111
    KS_Extension,   // Is an extension
112
    KS_Enabled,     // Enabled
113
    KS_Future       // Is a keyword in future standard
114
  };
115
116
} // namespace
117
118
/// Translates flags as specified in TokenKinds.def into keyword status
119
/// in the given language standard.
120
static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
121
33.2M
                                      unsigned Flags) {
122
33.2M
  if (Flags == KEYALL) 
return KS_Enabled10.5M
;
123
22.6M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)18.8M
)
return KS_Enabled8.42M
;
124
14.2M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)9.00M
)
return KS_Enabled731k
;
125
13.5M
  if (LangOpts.CPlusPlus2a && 
(Flags & KEYCXX2A)3.74M
)
return KS_Enabled265k
;
126
13.2M
  if (LangOpts.C99 && 
(Flags & KEYC99)3.77M
)
return KS_Enabled50.8k
;
127
13.2M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)10.4M
)
return KS_Extension95.2k
;
128
13.1M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)3.83M
)
return KS_Extension1.65M
;
129
11.4M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)211k
)
return KS_Enabled1.47k
;
130
11.4M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)799
)
return KS_Extension54
;
131
11.4M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)7.87M
)
return KS_Enabled256k
;
132
11.1M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)128k
)
return KS_Enabled627
;
133
11.1M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)5.69M
)
return KS_Enabled52.1k
;
134
11.1M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)41.0k
)
return KS_Enabled4
;
135
11.1M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)28.7k
)
return KS_Enabled588
;
136
11.1M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)5.21k
)
return KS_Enabled81
;
137
11.1M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus128k
&&
(Flags & KEYOPENCLC)121k
)
138
18.6k
    return KS_Enabled;
139
11.1M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)6.91k
)
return KS_Enabled1.98k
;
140
11.1M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)3.66M
)
return KS_Enabled85.4k
;
141
11.0M
  if (LangOpts.C11 && 
(Flags & KEYC11)3.35M
)
return KS_Enabled0
;
142
11.0M
  // We treat bridge casts as objective-C keywords so we can warn on them
143
11.0M
  // in non-arc mode.
144
11.0M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)5.16M
)
return KS_Enabled406k
;
145
10.6M
  if (LangOpts.CPlusPlus2a && 
(Flags & KEYCONCEPTS)1.61M
)
return KS_Enabled0
;
146
10.6M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)45.5k
)
return KS_Enabled129
;
147
10.6M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)15.1k
)
return KS_Enabled280
;
148
10.6M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)7.08M
)
return KS_Future532k
;
149
10.0M
  return KS_Disabled;
150
10.0M
}
151
152
/// AddKeyword - This method is used to associate a token ID with specific
153
/// identifiers because they are language keywords.  This causes the lexer to
154
/// automatically map matching identifiers to specialized token codes.
155
static void AddKeyword(StringRef Keyword,
156
                       tok::TokenKind TokenCode, unsigned Flags,
157
33.2M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
158
33.2M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
159
33.2M
160
33.2M
  // Don't add this keyword under MSVCCompat.
161
33.2M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)830k
&&
162
33.2M
      
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)5.09k
)
163
4.89k
    return;
164
33.2M
165
33.2M
  // Don't add this keyword under OpenCL.
166
33.2M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)204k
)
167
629
    return;
168
33.2M
169
33.2M
  // Don't add this keyword if disabled in this language.
170
33.2M
  if (AddResult == KS_Disabled) 
return10.0M
;
171
23.1M
172
23.1M
  IdentifierInfo &Info =
173
23.1M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier530k
:
TokenCode22.5M
);
174
23.1M
  Info.setIsExtensionToken(AddResult == KS_Extension);
175
23.1M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
176
23.1M
}
177
178
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
179
/// representations.
180
static void AddCXXOperatorKeyword(StringRef Keyword,
181
                                  tok::TokenKind TokenCode,
182
691k
                                  IdentifierTable &Table) {
183
691k
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
184
691k
  Info.setIsCPlusPlusOperatorKeyword();
185
691k
}
186
187
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
188
/// or "property".
189
static void AddObjCKeyword(StringRef Name,
190
                           tok::ObjCKeywordKind ObjCID,
191
1.56M
                           IdentifierTable &Table) {
192
1.56M
  Table.get(Name).setObjCKeywordID(ObjCID);
193
1.56M
}
194
195
/// AddKeywords - Add all keywords to the symbol table.
196
///
197
102k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
198
102k
  // Add keywords and tokens for the current language.
199
102k
#define KEYWORD(NAME, FLAGS) \
200
27.4M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
201
27.4M
             FLAGS, LangOpts, *this);
202
102k
#define ALIAS(NAME, TOK, FLAGS) \
203
5.71M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
204
5.71M
             FLAGS, LangOpts, *this);
205
102k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
206
1.12M
  if (LangOpts.CXXOperatorNames)          \
207
1.12M
    
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this)691k
;
208
102k
#define OBJC_AT_KEYWORD(NAME)  \
209
2.75M
  if (LangOpts.ObjC)           \
210
2.75M
    
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this)1.56M
;
211
102k
#define TESTING_KEYWORD(NAME, FLAGS)
212
102k
#include "clang/Basic/TokenKinds.def"
213
102k
214
102k
  if (LangOpts.ParseUnknownAnytype)
215
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
216
9
               LangOpts, *this);
217
102k
218
102k
  if (LangOpts.DeclSpecKeyword)
219
36.3k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
220
102k
221
102k
  // Add the 'import' contextual keyword.
222
102k
  get("import").setModulesImport(true);
223
102k
}
224
225
/// Checks if the specified token kind represents a keyword in the
226
/// specified language.
227
/// \returns Status of the keyword in the language.
228
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
229
2.83M
                                      tok::TokenKind K) {
230
2.83M
  switch (K) {
231
0
#define KEYWORD(NAME, FLAGS) \
232
174
  case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
233
0
#include "clang/Basic/TokenKinds.def"
234
2.83M
  default: return KS_Disabled;
235
2.83M
  }
236
2.83M
}
237
238
/// Returns true if the identifier represents a keyword in the
239
/// specified language.
240
2.83M
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
241
2.83M
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
242
156
  case KS_Enabled:
243
156
  case KS_Extension:
244
156
    return true;
245
2.83M
  default:
246
2.83M
    return false;
247
2.83M
  }
248
2.83M
}
249
250
/// Returns true if the identifier represents a C++ keyword in the
251
/// specified language.
252
19
bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
253
19
  if (!LangOpts.CPlusPlus || 
!isKeyword(LangOpts)18
)
254
1
    return false;
255
18
  // This is a C++ keyword if this identifier is not a keyword when checked
256
18
  // using LangOptions without C++ support.
257
18
  LangOptions LangOptsNoCPP = LangOpts;
258
18
  LangOptsNoCPP.CPlusPlus = false;
259
18
  LangOptsNoCPP.CPlusPlus11 = false;
260
18
  LangOptsNoCPP.CPlusPlus2a = false;
261
18
  return !isKeyword(LangOptsNoCPP);
262
18
}
263
264
85.7M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
265
85.7M
  // We use a perfect hash function here involving the length of the keyword,
266
85.7M
  // the first and third character.  For preprocessor ID's there are no
267
85.7M
  // collisions (if there were, the switch below would complain about duplicate
268
85.7M
  // case values).  Note that this depends on 'if' being null terminated.
269
85.7M
270
85.7M
#define HASH(LEN, FIRST, THIRD) \
271
137M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
272
85.7M
#define CASE(LEN, FIRST, THIRD, NAME) \
273
85.7M
  
case 51.5M
HASH51.5M
(LEN, FIRST, THIRD): \
274
51.5M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword493k
:
tok::pp_51.0M
## NAME
275
85.7M
276
85.7M
  unsigned Len = getLength();
277
85.7M
  if (Len < 2) 
return tok::pp_not_keyword61.7k
;
278
85.7M
  const char *Name = getNameStart();
279
85.7M
  switch (HASH(Len, Name[0], Name[2])) {
280
34.2M
  default: return tok::pp_not_keyword;
281
3.85M
  CASE( 2, 'i', '\0', if);
282
342k
  CASE( 4, 'e', 'i', elif);
283
1.99M
  CASE( 4, 'e', 's', else);
284
47.6k
  CASE( 4, 'l', 'n', line);
285
3.18k
  CASE( 4, 's', 'c', sccs);
286
2.72M
  CASE( 5, 'e', 'd', endif);
287
19.9k
  CASE( 5, 'e', 'r', error);
288
4.12k
  CASE( 5, 'i', 'e', ident);
289
937k
  CASE( 5, 'i', 'd', ifdef);
290
220k
  CASE( 5, 'u', 'd', undef);
291
0
292
35.0k
  CASE( 6, 'a', 's', assert);
293
34.3M
  CASE( 6, 'd', 'f', define);
294
1.73M
  CASE( 6, 'i', 'n', ifndef);
295
639k
  CASE( 6, 'i', 'p', import);
296
515k
  CASE( 6, 'p', 'a', pragma);
297
0
298
15.7k
  CASE( 7, 'd', 'f', defined);
299
3.81M
  CASE( 7, 'i', 'c', include);
300
1.97k
  CASE( 7, 'w', 'r', warning);
301
0
302
5.75k
  CASE( 8, 'u', 'a', unassert);
303
60.6k
  CASE(12, 'i', 'c', include_next);
304
0
305
6.29k
  CASE(14, '_', 'p', __public_macro);
306
0
307
58.0k
  CASE(15, '_', 'p', __private_macro);
308
0
309
151k
  CASE(16, '_', 'i', __include_macros);
310
85.7M
#undef CASE
311
85.7M
#undef HASH
312
85.7M
  }
313
85.7M
}
314
315
//===----------------------------------------------------------------------===//
316
// Stats Implementation
317
//===----------------------------------------------------------------------===//
318
319
/// PrintStats - Print statistics about how well the identifier table is doing
320
/// at hashing identifiers.
321
3
void IdentifierTable::PrintStats() const {
322
3
  unsigned NumBuckets = HashTable.getNumBuckets();
323
3
  unsigned NumIdentifiers = HashTable.getNumItems();
324
3
  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
325
3
  unsigned AverageIdentifierSize = 0;
326
3
  unsigned MaxIdentifierLength = 0;
327
3
328
3
  // TODO: Figure out maximum times an identifier had to probe for -stats.
329
3
  for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
330
3.83k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I3.83k
) {
331
3.83k
    unsigned IdLen = I->getKeyLength();
332
3.83k
    AverageIdentifierSize += IdLen;
333
3.83k
    if (MaxIdentifierLength < IdLen)
334
18
      MaxIdentifierLength = IdLen;
335
3.83k
  }
336
3
337
3
  fprintf(stderr, "\n*** Identifier Table Stats:\n");
338
3
  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
339
3
  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
340
3
  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
341
3
          NumIdentifiers/(double)NumBuckets);
342
3
  fprintf(stderr, "Ave identifier length: %f\n",
343
3
          (AverageIdentifierSize/(double)NumIdentifiers));
344
3
  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
345
3
346
3
  // Compute statistics about the memory allocated for identifiers.
347
3
  HashTable.getAllocator().PrintStats();
348
3
}
349
350
//===----------------------------------------------------------------------===//
351
// SelectorTable Implementation
352
//===----------------------------------------------------------------------===//
353
354
8.33M
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
355
8.33M
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
356
8.33M
}
357
358
namespace clang {
359
360
/// One of these variable length records is kept for each
361
/// selector containing more than one keyword. We use a folding set
362
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
363
/// this class is provided strictly through Selector.
364
class alignas(IdentifierInfoAlignment) MultiKeywordSelector
365
    : public detail::DeclarationNameExtra,
366
      public llvm::FoldingSetNode {
367
0
  MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
368
369
public:
370
  // Constructor for keyword selectors.
371
  MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
372
301k
      : DeclarationNameExtra(nKeys) {
373
301k
    assert((nKeys > 1) && "not a multi-keyword selector");
374
301k
375
301k
    // Fill in the trailing keyword array.
376
301k
    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
377
1.12M
    for (unsigned i = 0; i != nKeys; 
++i820k
)
378
820k
      KeyInfo[i] = IIV[i];
379
301k
  }
380
381
  // getName - Derive the full selector name and return it.
382
  std::string getName() const;
383
384
  using DeclarationNameExtra::getNumArgs;
385
386
  using keyword_iterator = IdentifierInfo *const *;
387
388
2.07M
  keyword_iterator keyword_begin() const {
389
2.07M
    return reinterpret_cast<keyword_iterator>(this + 1);
390
2.07M
  }
391
392
32.7k
  keyword_iterator keyword_end() const {
393
32.7k
    return keyword_begin() + getNumArgs();
394
32.7k
  }
395
396
1.22M
  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
397
1.22M
    assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
398
1.22M
    return keyword_begin()[i];
399
1.22M
  }
400
401
  static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
402
1.16M
                      unsigned NumArgs) {
403
1.16M
    ID.AddInteger(NumArgs);
404
4.24M
    for (unsigned i = 0; i != NumArgs; 
++i3.07M
)
405
3.07M
      ID.AddPointer(ArgTys[i]);
406
1.16M
  }
407
408
785k
  void Profile(llvm::FoldingSetNodeID &ID) {
409
785k
    Profile(ID, keyword_begin(), getNumArgs());
410
785k
  }
411
};
412
413
} // namespace clang.
414
415
5
bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
416
5
  assert(!Names.empty() && "must have >= 1 selector slots");
417
5
  if (getNumArgs() != Names.size())
418
0
    return false;
419
14
  
for (unsigned I = 0, E = Names.size(); 5
I != E;
++I9
) {
420
11
    if (getNameForSlot(I) != Names[I])
421
2
      return false;
422
11
  }
423
5
  
return true3
;
424
5
}
425
426
0
bool Selector::isUnarySelector(StringRef Name) const {
427
0
  return isUnarySelector() && getNameForSlot(0) == Name;
428
0
}
429
430
3.57M
unsigned Selector::getNumArgs() const {
431
3.57M
  unsigned IIF = getIdentifierInfoFlag();
432
3.57M
  if (IIF <= ZeroArg)
433
868k
    return 0;
434
2.70M
  if (IIF == OneArg)
435
1.30M
    return 1;
436
1.39M
  // We point to a MultiKeywordSelector.
437
1.39M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
438
1.39M
  return SI->getNumArgs();
439
1.39M
}
440
441
2.75M
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
442
2.75M
  if (getIdentifierInfoFlag() < MultiArg) {
443
1.53M
    assert(argIndex == 0 && "illegal keyword index");
444
1.53M
    return getAsIdentifierInfo();
445
1.53M
  }
446
1.22M
447
1.22M
  // We point to a MultiKeywordSelector.
448
1.22M
  MultiKeywordSelector *SI = getMultiKeywordSelector();
449
1.22M
  return SI->getIdentifierInfoForSlot(argIndex);
450
1.22M
}
451
452
221k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
453
221k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
454
221k
  return II ? 
II->getName()221k
:
StringRef()9
;
455
221k
}
456
457
32.7k
std::string MultiKeywordSelector::getName() const {
458
32.7k
  SmallString<256> Str;
459
32.7k
  llvm::raw_svector_ostream OS(Str);
460
125k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I92.7k
) {
461
92.7k
    if (*I)
462
92.5k
      OS << (*I)->getName();
463
92.7k
    OS << ':';
464
92.7k
  }
465
32.7k
466
32.7k
  return std::string(OS.str());
467
32.7k
}
468
469
183k
std::string Selector::getAsString() const {
470
183k
  if (InfoPtr == 0)
471
6
    return "<null selector>";
472
183k
473
183k
  if (getIdentifierInfoFlag() < MultiArg) {
474
150k
    IdentifierInfo *II = getAsIdentifierInfo();
475
150k
476
150k
    if (getNumArgs() == 0) {
477
116k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
478
116k
                   "not be null.");
479
116k
      return std::string(II->getName());
480
116k
    }
481
34.3k
482
34.3k
    if (!II)
483
33
      return ":";
484
34.3k
485
34.3k
    return II->getName().str() + ":";
486
34.3k
  }
487
32.7k
488
32.7k
  // We have a multiple keyword selector.
489
32.7k
  return getMultiKeywordSelector()->getName();
490
32.7k
}
491
492
9.69k
void Selector::print(llvm::raw_ostream &OS) const {
493
9.69k
  OS << getAsString();
494
9.69k
}
495
496
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
497
498
/// Interpreting the given string using the normal CamelCase
499
/// conventions, determine whether the given string starts with the
500
/// given "word", which is assumed to end in a lowercase letter.
501
133k
static bool startsWithWord(StringRef name, StringRef word) {
502
133k
  if (name.size() < word.size()) 
return false3.89k
;
503
129k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])112k
) &&
504
129k
          
name.startswith(word)85.1k
);
505
129k
}
506
507
276k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
508
276k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
509
276k
  if (!first) 
return OMF_None22
;
510
276k
511
276k
  StringRef name = first->getName();
512
276k
  if (sel.isUnarySelector()) {
513
71.7k
    if (name == "autorelease") 
return OMF_autorelease1.09k
;
514
70.6k
    if (name == "dealloc") 
return OMF_dealloc853
;
515
69.7k
    if (name == "finalize") 
return OMF_finalize150
;
516
69.6k
    if (name == "release") 
return OMF_release1.35k
;
517
68.2k
    if (name == "retain") 
return OMF_retain701
;
518
67.5k
    if (name == "retainCount") 
return OMF_retainCount106
;
519
67.4k
    if (name == "self") 
return OMF_self93
;
520
67.3k
    if (name == "initialize") 
return OMF_initialize30
;
521
272k
  }
522
272k
523
272k
  if (name == "performSelector" || 
name == "performSelectorInBackground"271k
||
524
272k
      
name == "performSelectorOnMainThread"271k
)
525
955
    return OMF_performSelector;
526
271k
527
271k
  // The other method families may begin with a prefix of underscores.
528
271k
  
while (271k
!name.empty() && name.front() == '_')
529
231
    name = name.substr(1);
530
271k
531
271k
  if (name.empty()) 
return OMF_None0
;
532
271k
  switch (name.front()) {
533
21.1k
  case 'a':
534
21.1k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc5.54k
;
535
15.6k
    break;
536
16.3k
  case 'c':
537
16.3k
    if (startsWithWord(name, "copy")) 
return OMF_copy1.54k
;
538
14.8k
    break;
539
75.6k
  case 'i':
540
75.6k
    if (startsWithWord(name, "init")) 
return OMF_init69.4k
;
541
6.16k
    break;
542
8.45k
  case 'm':
543
8.45k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy1.04k
;
544
7.40k
    break;
545
11.1k
  case 'n':
546
11.1k
    if (startsWithWord(name, "new")) 
return OMF_new1.55k
;
547
9.62k
    break;
548
138k
  default:
549
138k
    break;
550
192k
  }
551
192k
552
192k
  return OMF_None;
553
192k
}
554
555
554
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
556
554
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
557
554
  if (!first) 
return OIT_None2
;
558
552
559
552
  StringRef name = first->getName();
560
552
561
552
  if (name.empty()) 
return OIT_None0
;
562
552
  switch (name.front()) {
563
53
    case 'a':
564
53
      if (startsWithWord(name, "array")) 
return OIT_Array15
;
565
38
      break;
566
38
    case 'd':
567
37
      if (startsWithWord(name, "default")) 
return OIT_ReturnsSelf2
;
568
35
      if (startsWithWord(name, "dictionary")) 
return OIT_Dictionary11
;
569
24
      break;
570
35
    case 's':
571
35
      if (startsWithWord(name, "shared")) 
return OIT_ReturnsSelf2
;
572
33
      if (startsWithWord(name, "standard")) 
return OIT_Singleton1
;
573
32
      break;
574
93
    case 'i':
575
93
      if (startsWithWord(name, "init")) 
return OIT_Init45
;
576
48
      break;
577
334
    default:
578
334
      break;
579
476
  }
580
476
  return OIT_None;
581
476
}
582
583
25.4k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
584
25.4k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
585
25.4k
  if (!first) 
return SFF_None6
;
586
25.4k
587
25.4k
  StringRef name = first->getName();
588
25.4k
589
25.4k
  switch (name.front()) {
590
5.78k
    case 'a':
591
5.78k
      if (name == "appendFormat") 
return SFF_NSString1
;
592
5.78k
      break;
593
5.78k
594
5.78k
    case 'i':
595
3.32k
      if (name == "initWithFormat") 
return SFF_NSString7
;
596
3.32k
      break;
597
3.32k
598
3.32k
    case 'l':
599
283
      if (name == "localizedStringWithFormat") 
return SFF_NSString2
;
600
281
      break;
601
281
602
3.92k
    case 's':
603
3.92k
      if (name == "stringByAppendingFormat" ||
604
3.92k
          
name == "stringWithFormat"3.87k
)
return SFF_NSString731
;
605
3.19k
      break;
606
24.6k
  }
607
24.6k
  return SFF_None;
608
24.6k
}
609
610
namespace {
611
612
struct SelectorTableImpl {
613
  llvm::FoldingSet<MultiKeywordSelector> Table;
614
  llvm::BumpPtrAllocator Allocator;
615
};
616
617
} // namespace
618
619
439k
static SelectorTableImpl &getSelectorTableImpl(void *P) {
620
439k
  return *static_cast<SelectorTableImpl*>(P);
621
439k
}
622
623
SmallString<64>
624
435k
SelectorTable::constructSetterName(StringRef Name) {
625
435k
  SmallString<64> SetterName("set");
626
435k
  SetterName += Name;
627
435k
  SetterName[3] = toUppercase(SetterName[3]);
628
435k
  return SetterName;
629
435k
}
630
631
Selector
632
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
633
                                       SelectorTable &SelTable,
634
429k
                                       const IdentifierInfo *Name) {
635
429k
  IdentifierInfo *SetterName =
636
429k
    &Idents.get(constructSetterName(Name->getName()));
637
429k
  return SelTable.getUnarySelector(SetterName);
638
429k
}
639
640
1
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
641
1
  StringRef Name = Sel.getNameForSlot(0);
642
1
  assert(Name.startswith("set") && "invalid setter name");
643
1
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
644
1
}
645
646
1
size_t SelectorTable::getTotalMemory() const {
647
1
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
648
1
  return SelTabImpl.Allocator.getTotalMemory();
649
1
}
650
651
812k
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
652
812k
  if (nKeys < 2)
653
431k
    return Selector(IIV[0], nKeys);
654
381k
655
381k
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
656
381k
657
381k
  // Unique selector, to guarantee there is one per name.
658
381k
  llvm::FoldingSetNodeID ID;
659
381k
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
660
381k
661
381k
  void *InsertPos = nullptr;
662
381k
  if (MultiKeywordSelector *SI =
663
79.2k
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
664
79.2k
    return Selector(SI);
665
301k
666
301k
  // MultiKeywordSelector objects are not allocated with new because they have a
667
301k
  // variable size array (for parameter types) at the end of them.
668
301k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
669
301k
  MultiKeywordSelector *SI =
670
301k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
671
301k
          Size, alignof(MultiKeywordSelector));
672
301k
  new (SI) MultiKeywordSelector(nKeys, IIV);
673
301k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
674
301k
  return Selector(SI);
675
301k
}
676
677
66.4k
SelectorTable::SelectorTable() {
678
66.4k
  Impl = new SelectorTableImpl();
679
66.4k
}
680
681
58.5k
SelectorTable::~SelectorTable() {
682
58.5k
  delete &getSelectorTableImpl(Impl);
683
58.5k
}
684
685
140k
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
686
140k
  switch (Operator) {
687
0
  case OO_None:
688
0
  case NUM_OVERLOADED_OPERATORS:
689
0
    return nullptr;
690
0
691
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
692
140k
  case OO_##Name: return Spelling;
693
0
#include "clang/Basic/OperatorKinds.def"
694
140k
  }
695
140k
696
140k
  
llvm_unreachable0
("Invalid OverloadedOperatorKind!");
697
140k
}
698
699
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
700
859
                                        bool isContextSensitive) {
701
859
  switch (kind) {
702
455
  case NullabilityKind::NonNull:
703
455
    return isContextSensitive ? 
"nonnull"2
:
"_Nonnull"453
;
704
0
705
404
  case NullabilityKind::Nullable:
706
404
    return isContextSensitive ? 
"nullable"2
:
"_Nullable"402
;
707
0
708
0
  case NullabilityKind::Unspecified:
709
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
710
0
  }
711
0
  llvm_unreachable("Unknown nullability kind.");
712
0
}