Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
359
IdentifierIterator::~IdentifierIterator() = default;
40
41
5.27k
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
77.6k
      ExternalLookup(ExternalLookup) {}
62
63
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
64
                                 IdentifierInfoLookup *ExternalLookup)
65
33.4k
    : IdentifierTable(ExternalLookup) {
66
33.4k
  // Populate the identifier table with info about keywords for the current
67
33.4k
  // language.
68
33.4k
  AddKeywords(LangOpts);
69
33.4k
}
Unexecuted instantiation: clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
clang::IdentifierTable::IdentifierTable(clang::LangOptions const&, clang::IdentifierInfoLookup*)
Line
Count
Source
65
33.4k
    : IdentifierTable(ExternalLookup) {
66
33.4k
  // Populate the identifier table with info about keywords for the current
67
33.4k
  // language.
68
33.4k
  AddKeywords(LangOpts);
69
33.4k
}
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
25.1M
                                      unsigned Flags) {
122
25.1M
  if (Flags == KEYALL) 
return KS_Enabled8.03M
;
123
17.0M
  if (LangOpts.CPlusPlus && 
(Flags & KEYCXX)12.8M
)
return KS_Enabled5.72M
;
124
11.3M
  if (LangOpts.CPlusPlus11 && 
(Flags & KEYCXX11)6.67M
)
return KS_Enabled547k
;
125
10.8M
  if (LangOpts.CPlusPlus2a && 
(Flags & KEYCXX2A)3.44M
)
return KS_Enabled215k
;
126
10.6M
  if (LangOpts.C99 && 
(Flags & KEYC99)4.21M
)
return KS_Enabled57.4k
;
127
10.5M
  if (LangOpts.GNUKeywords && 
(Flags & KEYGNU)8.33M
)
return KS_Extension77.7k
;
128
10.4M
  if (LangOpts.MicrosoftExt && 
(Flags & KEYMS)3.49M
)
return KS_Extension1.51M
;
129
8.96M
  if (LangOpts.MSVCCompat && 
(Flags & KEYMSCOMPAT)147k
)
return KS_Enabled1.13k
;
130
8.96M
  if (LangOpts.Borland && 
(Flags & KEYBORLAND)792
)
return KS_Extension54
;
131
8.96M
  if (LangOpts.Bool && 
(Flags & BOOLSUPPORT)4.94M
)
return KS_Enabled176k
;
132
8.78M
  if (LangOpts.Half && 
(Flags & HALFSUPPORT)128k
)
return KS_Enabled626
;
133
8.78M
  if (LangOpts.WChar && 
(Flags & WCHARSUPPORT)2.96M
)
return KS_Enabled27.7k
;
134
8.75M
  if (LangOpts.Char8 && 
(Flags & CHAR8SUPPORT)24.2k
)
return KS_Enabled4
;
135
8.75M
  if (LangOpts.AltiVec && 
(Flags & KEYALTIVEC)28.2k
)
return KS_Enabled580
;
136
8.75M
  if (LangOpts.ZVector && 
(Flags & KEYZVECTOR)3.87k
)
return KS_Enabled63
;
137
8.75M
  if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus127k
&&
(Flags & KEYOPENCLC)122k
)
138
18.9k
    return KS_Enabled;
139
8.73M
  if (LangOpts.OpenCLCPlusPlus && 
(Flags & KEYOPENCLCXX)5.88k
)
return KS_Enabled1.70k
;
140
8.73M
  if (!LangOpts.CPlusPlus && 
(Flags & KEYNOCXX)4.11M
)
return KS_Enabled96.6k
;
141
8.63M
  if (LangOpts.C11 && 
(Flags & KEYC11)3.78M
)
return KS_Enabled0
;
142
8.63M
  // We treat bridge casts as objective-C keywords so we can warn on them
143
8.63M
  // in non-arc mode.
144
8.63M
  if (LangOpts.ObjC && 
(Flags & KEYOBJC)2.74M
)
return KS_Enabled253k
;
145
8.38M
  if (LangOpts.ConceptsTS && 
(Flags & KEYCONCEPTS)850
)
return KS_Enabled12
;
146
8.38M
  if (LangOpts.Coroutines && 
(Flags & KEYCOROUTINES)28.4k
)
return KS_Enabled123
;
147
8.38M
  if (LangOpts.ModulesTS && 
(Flags & KEYMODULES)15.0k
)
return KS_Enabled280
;
148
8.38M
  if (LangOpts.CPlusPlus && 
(Flags & KEYALLCXX)4.39M
)
return KS_Future229k
;
149
8.15M
  return KS_Disabled;
150
8.15M
}
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
25.1M
                       const LangOptions &LangOpts, IdentifierTable &Table) {
158
25.1M
  KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
159
25.1M
160
25.1M
  // Don't add this keyword under MSVCCompat.
161
25.1M
  if (LangOpts.MSVCCompat && 
(Flags & KEYNOMS18)561k
&&
162
25.1M
      
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)3.46k
)
163
3.29k
    return;
164
25.1M
165
25.1M
  // Don't add this keyword under OpenCL.
166
25.1M
  if (LangOpts.OpenCL && 
(Flags & KEYNOOPENCL)202k
)
167
628
    return;
168
25.1M
169
25.1M
  // Don't add this keyword if disabled in this language.
170
25.1M
  if (AddResult == KS_Disabled) 
return8.15M
;
171
16.9M
172
16.9M
  IdentifierInfo &Info =
173
16.9M
      Table.get(Keyword, AddResult == KS_Future ? 
tok::identifier227k
:
TokenCode16.7M
);
174
16.9M
  Info.setIsExtensionToken(AddResult == KS_Extension);
175
16.9M
  Info.setIsFutureCompatKeyword(AddResult == KS_Future);
176
16.9M
}
177
178
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
179
/// representations.
180
static void AddCXXOperatorKeyword(StringRef Keyword,
181
                                  tok::TokenKind TokenCode,
182
600k
                                  IdentifierTable &Table) {
183
600k
  IdentifierInfo &Info = Table.get(Keyword, TokenCode);
184
600k
  Info.setIsCPlusPlusOperatorKeyword();
185
600k
}
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
976k
                           IdentifierTable &Table) {
192
976k
  Table.get(Name).setObjCKeywordID(ObjCID);
193
976k
}
194
195
/// AddKeywords - Add all keywords to the symbol table.
196
///
197
77.6k
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
198
77.6k
  // Add keywords and tokens for the current language.
199
77.6k
#define KEYWORD(NAME, FLAGS) \
200
20.8M
  AddKeyword(StringRef(#NAME), tok::kw_ ## NAME,  \
201
20.8M
             FLAGS, LangOpts, *this);
202
77.6k
#define ALIAS(NAME, TOK, FLAGS) \
203
4.27M
  AddKeyword(StringRef(NAME), tok::kw_ ## TOK,  \
204
4.27M
             FLAGS, LangOpts, *this);
205
77.6k
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
206
854k
  if (LangOpts.CXXOperatorNames)          \
207
854k
    
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this)600k
;
208
77.6k
#define OBJC_AT_KEYWORD(NAME)  \
209
2.09M
  if (LangOpts.ObjC)           \
210
2.09M
    
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this)976k
;
211
77.6k
#define TESTING_KEYWORD(NAME, FLAGS)
212
77.6k
#include "clang/Basic/TokenKinds.def"
213
77.6k
214
77.6k
  if (LangOpts.ParseUnknownAnytype)
215
9
    AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
216
9
               LangOpts, *this);
217
77.6k
218
77.6k
  if (LangOpts.DeclSpecKeyword)
219
33.2k
    AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
220
77.6k
221
77.6k
  // Add the 'import' contextual keyword.
222
77.6k
  get("import").setModulesImport(true);
223
77.6k
}
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
720k
                                      tok::TokenKind K) {
230
720k
  switch (K) {
231
720k
#define KEYWORD(NAME, FLAGS) \
232
720k
  
case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS)174
;
233
720k
#include "clang/Basic/TokenKinds.def"
234
720k
  
default: return KS_Disabled720k
;
235
720k
  }
236
720k
}
237
238
/// Returns true if the identifier represents a keyword in the
239
/// specified language.
240
720k
bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
241
720k
  switch (getTokenKwStatus(LangOpts, getTokenID())) {
242
720k
  case KS_Enabled:
243
156
  case KS_Extension:
244
156
    return true;
245
720k
  default:
246
720k
    return false;
247
720k
  }
248
720k
}
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
64.3M
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
265
64.3M
  // We use a perfect hash function here involving the length of the keyword,
266
64.3M
  // the first and third character.  For preprocessor ID's there are no
267
64.3M
  // collisions (if there were, the switch below would complain about duplicate
268
64.3M
  // case values).  Note that this depends on 'if' being null terminated.
269
64.3M
270
64.3M
#define HASH(LEN, FIRST, THIRD) \
271
103M
  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
272
64.3M
#define CASE(LEN, FIRST, THIRD, NAME) \
273
64.3M
  
case 38.7M
HASH38.7M
(LEN, FIRST, THIRD): \
274
38.7M
    return memcmp(Name, #NAME, LEN) ? 
tok::pp_not_keyword354k
:
tok::pp_38.3M
## NAME
275
64.3M
276
64.3M
  unsigned Len = getLength();
277
64.3M
  if (Len < 2) 
return tok::pp_not_keyword72.2k
;
278
64.3M
  const char *Name = getNameStart();
279
64.3M
  switch (HASH(Len, Name[0], Name[2])) {
280
64.3M
  
default: return tok::pp_not_keyword25.5M
;
281
64.3M
  
CASE3.85M
( 2, 'i', '\0', if);
282
64.3M
  
CASE109k
( 4, 'e', 'i', elif);
283
64.3M
  
CASE2.77M
( 4, 'e', 's', else);
284
64.3M
  
CASE16.3k
( 4, 'l', 'n', line);
285
64.3M
  
CASE6.40k
( 4, 's', 'c', sccs);
286
64.3M
  
CASE2.08M
( 5, 'e', 'd', endif);
287
64.3M
  
CASE16.3k
( 5, 'e', 'r', error);
288
64.3M
  
CASE1.79k
( 5, 'i', 'e', ident);
289
64.3M
  
CASE873k
( 5, 'i', 'd', ifdef);
290
64.3M
  
CASE295k
( 5, 'u', 'd', undef);
291
64.3M
292
64.3M
  
CASE28.2k
( 6, 'a', 's', assert);
293
64.3M
  
CASE25.1M
( 6, 'd', 'f', define);
294
64.3M
  
CASE1.01M
( 6, 'i', 'n', ifndef);
295
64.3M
  
CASE26.6k
( 6, 'i', 'p', import);
296
64.3M
  
CASE235k
( 6, 'p', 'a', pragma);
297
64.3M
298
64.3M
  
CASE9.74k
( 7, 'd', 'f', defined);
299
64.3M
  
CASE2.00M
( 7, 'i', 'c', include);
300
64.3M
  
CASE1.54k
( 7, 'w', 'r', warning);
301
64.3M
302
64.3M
  
CASE1.97k
( 8, 'u', 'a', unassert);
303
64.3M
  
CASE46.0k
(12, 'i', 'c', include_next);
304
64.3M
305
64.3M
  
CASE1.53k
(14, '_', 'p', __public_macro);
306
64.3M
307
64.3M
  
CASE42.2k
(15, '_', 'p', __private_macro);
308
64.3M
309
64.3M
  
CASE136k
(16, '_', 'i', __include_macros);
310
64.3M
#undef CASE
311
64.3M
#undef HASH
312
64.3M
  }
313
64.3M
}
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.82k
       I = HashTable.begin(), E = HashTable.end(); I != E; 
++I3.81k
) {
331
3.81k
    unsigned IdLen = I->getKeyLength();
332
3.81k
    AverageIdentifierSize += IdLen;
333
3.81k
    if (MaxIdentifierLength < IdLen)
334
18
      MaxIdentifierLength = IdLen;
335
3.81k
  }
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
419k
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
355
419k
  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
356
419k
}
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
25.3k
      : DeclarationNameExtra(nKeys) {
373
25.3k
    assert((nKeys > 1) && "not a multi-keyword selector");
374
25.3k
375
25.3k
    // Fill in the trailing keyword array.
376
25.3k
    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
377
91.2k
    for (unsigned i = 0; i != nKeys; 
++i65.9k
)
378
65.9k
      KeyInfo[i] = IIV[i];
379
25.3k
  }
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
1.65M
  keyword_iterator keyword_begin() const {
389
1.65M
    return reinterpret_cast<keyword_iterator>(this + 1);
390
1.65M
  }
391
392
2.07k
  keyword_iterator keyword_end() const {
393
2.07k
    return keyword_begin() + getNumArgs();
394
2.07k
  }
395
396
54.9k
  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
397
54.9k
    assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
398
54.9k
    return keyword_begin()[i];
399
54.9k
  }
400
401
  static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
402
3.16M
                      unsigned NumArgs) {
403
3.16M
    ID.AddInteger(NumArgs);
404
11.0M
    for (unsigned i = 0; i != NumArgs; 
++i7.92M
)
405
7.92M
      ID.AddPointer(ArgTys[i]);
406
3.16M
  }
407
408
1.59M
  void Profile(llvm::FoldingSetNodeID &ID) {
409
1.59M
    Profile(ID, keyword_begin(), getNumArgs());
410
1.59M
  }
411
};
412
413
} // namespace clang.
414
415
244k
unsigned Selector::getNumArgs() const {
416
244k
  unsigned IIF = getIdentifierInfoFlag();
417
244k
  if (IIF <= ZeroArg)
418
102k
    return 0;
419
141k
  if (IIF == OneArg)
420
70.5k
    return 1;
421
70.9k
  // We point to a MultiKeywordSelector.
422
70.9k
  MultiKeywordSelector *SI = getMultiKeywordSelector();
423
70.9k
  return SI->getNumArgs();
424
70.9k
}
425
426
173k
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
427
173k
  if (getIdentifierInfoFlag() < MultiArg) {
428
118k
    assert(argIndex == 0 && "illegal keyword index");
429
118k
    return getAsIdentifierInfo();
430
118k
  }
431
54.9k
432
54.9k
  // We point to a MultiKeywordSelector.
433
54.9k
  MultiKeywordSelector *SI = getMultiKeywordSelector();
434
54.9k
  return SI->getIdentifierInfoForSlot(argIndex);
435
54.9k
}
436
437
2.48k
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
438
2.48k
  IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
439
2.48k
  return II ? 
II->getName()2.47k
:
StringRef()9
;
440
2.48k
}
441
442
2.07k
std::string MultiKeywordSelector::getName() const {
443
2.07k
  SmallString<256> Str;
444
2.07k
  llvm::raw_svector_ostream OS(Str);
445
7.60k
  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; 
++I5.52k
) {
446
5.52k
    if (*I)
447
5.28k
      OS << (*I)->getName();
448
5.52k
    OS << ':';
449
5.52k
  }
450
2.07k
451
2.07k
  return OS.str();
452
2.07k
}
453
454
19.6k
std::string Selector::getAsString() const {
455
19.6k
  if (InfoPtr == 0)
456
0
    return "<null selector>";
457
19.6k
458
19.6k
  if (getIdentifierInfoFlag() < MultiArg) {
459
17.6k
    IdentifierInfo *II = getAsIdentifierInfo();
460
17.6k
461
17.6k
    if (getNumArgs() == 0) {
462
13.2k
      assert(II && "If the number of arguments is 0 then II is guaranteed to "
463
13.2k
                   "not be null.");
464
13.2k
      return II->getName();
465
13.2k
    }
466
4.39k
467
4.39k
    if (!II)
468
33
      return ":";
469
4.35k
470
4.35k
    return II->getName().str() + ":";
471
4.35k
  }
472
2.07k
473
2.07k
  // We have a multiple keyword selector.
474
2.07k
  return getMultiKeywordSelector()->getName();
475
2.07k
}
476
477
6.13k
void Selector::print(llvm::raw_ostream &OS) const {
478
6.13k
  OS << getAsString();
479
6.13k
}
480
481
0
LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
482
483
/// Interpreting the given string using the normal CamelCase
484
/// conventions, determine whether the given string starts with the
485
/// given "word", which is assumed to end in a lowercase letter.
486
19.7k
static bool startsWithWord(StringRef name, StringRef word) {
487
19.7k
  if (name.size() < word.size()) 
return false2.99k
;
488
16.7k
  return ((name.size() == word.size() || 
!isLowercase(name[word.size()])11.8k
) &&
489
16.7k
          
name.startswith(word)10.2k
);
490
16.7k
}
491
492
44.0k
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
493
44.0k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
494
44.0k
  if (!first) 
return OMF_None22
;
495
43.9k
496
43.9k
  StringRef name = first->getName();
497
43.9k
  if (sel.isUnarySelector()) {
498
23.9k
    if (name == "autorelease") 
return OMF_autorelease515
;
499
23.4k
    if (name == "dealloc") 
return OMF_dealloc555
;
500
22.8k
    if (name == "finalize") 
return OMF_finalize148
;
501
22.7k
    if (name == "release") 
return OMF_release925
;
502
21.7k
    if (name == "retain") 
return OMF_retain697
;
503
21.0k
    if (name == "retainCount") 
return OMF_retainCount105
;
504
20.9k
    if (name == "self") 
return OMF_self84
;
505
20.8k
    if (name == "initialize") 
return OMF_initialize20
;
506
40.9k
  }
507
40.9k
508
40.9k
  if (name == "performSelector" || 
name == "performSelectorInBackground"40.8k
||
509
40.9k
      
name == "performSelectorOnMainThread"40.8k
)
510
145
    return OMF_performSelector;
511
40.7k
512
40.7k
  // The other method families may begin with a prefix of underscores.
513
41.0k
  
while (40.7k
!name.empty() && name.front() == '_')
514
215
    name = name.substr(1);
515
40.7k
516
40.7k
  if (name.empty()) 
return OMF_None0
;
517
40.7k
  switch (name.front()) {
518
40.7k
  case 'a':
519
3.79k
    if (startsWithWord(name, "alloc")) 
return OMF_alloc2.23k
;
520
1.55k
    break;
521
2.76k
  case 'c':
522
2.76k
    if (startsWithWord(name, "copy")) 
return OMF_copy429
;
523
2.33k
    break;
524
7.10k
  case 'i':
525
7.10k
    if (startsWithWord(name, "init")) 
return OMF_init5.53k
;
526
1.57k
    break;
527
3.25k
  case 'm':
528
3.25k
    if (startsWithWord(name, "mutableCopy")) 
return OMF_mutableCopy242
;
529
3.01k
    break;
530
3.01k
  case 'n':
531
2.53k
    if (startsWithWord(name, "new")) 
return OMF_new733
;
532
1.80k
    break;
533
21.3k
  default:
534
21.3k
    break;
535
31.6k
  }
536
31.6k
537
31.6k
  return OMF_None;
538
31.6k
}
539
540
552
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
541
552
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
542
552
  if (!first) 
return OIT_None2
;
543
550
544
550
  StringRef name = first->getName();
545
550
546
550
  if (name.empty()) 
return OIT_None0
;
547
550
  switch (name.front()) {
548
550
    case 'a':
549
53
      if (startsWithWord(name, "array")) 
return OIT_Array15
;
550
38
      break;
551
38
    case 'd':
552
37
      if (startsWithWord(name, "default")) 
return OIT_ReturnsSelf2
;
553
35
      if (startsWithWord(name, "dictionary")) 
return OIT_Dictionary11
;
554
24
      break;
555
34
    case 's':
556
34
      if (startsWithWord(name, "shared")) 
return OIT_ReturnsSelf2
;
557
32
      if (startsWithWord(name, "standard")) 
return OIT_Singleton1
;
558
31
      break;
559
93
    case 'i':
560
93
      if (startsWithWord(name, "init")) 
return OIT_Init45
;
561
48
      break;
562
333
    default:
563
333
      break;
564
474
  }
565
474
  return OIT_None;
566
474
}
567
568
13.5k
ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
569
13.5k
  IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
570
13.5k
  if (!first) 
return SFF_None6
;
571
13.5k
572
13.5k
  StringRef name = first->getName();
573
13.5k
574
13.5k
  switch (name.front()) {
575
13.5k
    case 'a':
576
1.84k
      if (name == "appendFormat") 
return SFF_NSString1
;
577
1.84k
      break;
578
1.84k
579
1.84k
    case 'i':
580
1.68k
      if (name == "initWithFormat") 
return SFF_NSString7
;
581
1.67k
      break;
582
1.67k
583
1.67k
    case 'l':
584
92
      if (name == "localizedStringWithFormat") 
return SFF_NSString2
;
585
90
      break;
586
90
587
1.60k
    case 's':
588
1.60k
      if (name == "stringByAppendingFormat" ||
589
1.60k
          
name == "stringWithFormat"1.59k
)
return SFF_NSString31
;
590
1.57k
      break;
591
13.4k
  }
592
13.4k
  return SFF_None;
593
13.4k
}
594
595
namespace {
596
597
struct SelectorTableImpl {
598
  llvm::FoldingSet<MultiKeywordSelector> Table;
599
  llvm::BumpPtrAllocator Allocator;
600
};
601
602
} // namespace
603
604
1.60M
static SelectorTableImpl &getSelectorTableImpl(void *P) {
605
1.60M
  return *static_cast<SelectorTableImpl*>(P);
606
1.60M
}
607
608
SmallString<64>
609
23.0k
SelectorTable::constructSetterName(StringRef Name) {
610
23.0k
  SmallString<64> SetterName("set");
611
23.0k
  SetterName += Name;
612
23.0k
  SetterName[3] = toUppercase(SetterName[3]);
613
23.0k
  return SetterName;
614
23.0k
}
615
616
Selector
617
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
618
                                       SelectorTable &SelTable,
619
23.0k
                                       const IdentifierInfo *Name) {
620
23.0k
  IdentifierInfo *SetterName =
621
23.0k
    &Idents.get(constructSetterName(Name->getName()));
622
23.0k
  return SelTable.getUnarySelector(SetterName);
623
23.0k
}
624
625
1
std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
626
1
  StringRef Name = Sel.getNameForSlot(0);
627
1
  assert(Name.startswith("set") && "invalid setter name");
628
1
  return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
629
1
}
630
631
1
size_t SelectorTable::getTotalMemory() const {
632
1
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
633
1
  return SelTabImpl.Allocator.getTotalMemory();
634
1
}
635
636
2.38M
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
637
2.38M
  if (nKeys < 2)
638
812k
    return Selector(IIV[0], nKeys);
639
1.57M
640
1.57M
  SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
641
1.57M
642
1.57M
  // Unique selector, to guarantee there is one per name.
643
1.57M
  llvm::FoldingSetNodeID ID;
644
1.57M
  MultiKeywordSelector::Profile(ID, IIV, nKeys);
645
1.57M
646
1.57M
  void *InsertPos = nullptr;
647
1.57M
  if (MultiKeywordSelector *SI =
648
1.54M
        SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
649
1.54M
    return Selector(SI);
650
25.3k
651
25.3k
  // MultiKeywordSelector objects are not allocated with new because they have a
652
25.3k
  // variable size array (for parameter types) at the end of them.
653
25.3k
  unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
654
25.3k
  MultiKeywordSelector *SI =
655
25.3k
      (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
656
25.3k
          Size, alignof(MultiKeywordSelector));
657
25.3k
  new (SI) MultiKeywordSelector(nKeys, IIV);
658
25.3k
  SelTabImpl.Table.InsertNode(SI, InsertPos);
659
25.3k
  return Selector(SI);
660
25.3k
}
661
662
44.4k
SelectorTable::SelectorTable() {
663
44.4k
  Impl = new SelectorTableImpl();
664
44.4k
}
665
666
34.2k
SelectorTable::~SelectorTable() {
667
34.2k
  delete &getSelectorTableImpl(Impl);
668
34.2k
}
669
670
25.1k
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
671
25.1k
  switch (Operator) {
672
25.1k
  case OO_None:
673
0
  case NUM_OVERLOADED_OPERATORS:
674
0
    return nullptr;
675
0
676
0
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
677
25.1k
  case OO_##Name: return Spelling;
678
0
#include "clang/Basic/OperatorKinds.def"
679
25.1k
  }
680
25.1k
681
25.1k
  
llvm_unreachable0
("Invalid OverloadedOperatorKind!");
682
25.1k
}
683
684
StringRef clang::getNullabilitySpelling(NullabilityKind kind,
685
846
                                        bool isContextSensitive) {
686
846
  switch (kind) {
687
846
  case NullabilityKind::NonNull:
688
445
    return isContextSensitive ? 
"nonnull"2
:
"_Nonnull"443
;
689
846
690
846
  case NullabilityKind::Nullable:
691
401
    return isContextSensitive ? 
"nullable"2
:
"_Nullable"399
;
692
846
693
846
  case NullabilityKind::Unspecified:
694
0
    return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
695
0
  }
696
0
  llvm_unreachable("Unknown nullability kind.");
697
0
}