Skip to content

Commit 9059c3c

Browse files
committed
[APINotes] NFC: use ContextTableKey instead of std::tuple
rdar://113403829
1 parent e4c600f commit 9059c3c

File tree

3 files changed

+165
-81
lines changed

3 files changed

+165
-81
lines changed

clang/lib/APINotes/APINotesFormat.h

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,35 @@ struct StoredObjCSelector {
250250
llvm::SmallVector<IdentifierID, 2> Identifiers;
251251
};
252252

253-
inline std::tuple<uint32_t, uint8_t, uint32_t>
254-
getTableKey(std::optional<Context> context, IdentifierID nameID) {
255-
std::tuple<uint32_t, uint8_t, uint32_t> key;
256-
if (context)
257-
key = {context->id.Value, (uint8_t)context->kind, nameID};
258-
else
259-
key = {(uint32_t)-1, (uint8_t)-1, nameID};
260-
return key;
253+
/// A stored Objective-C or C++ context, represented by the ID of its parent
254+
/// context, the kind of this context (Objective-C class / C++ namespace / etc),
255+
/// and the ID of this context.
256+
struct ContextTableKey {
257+
uint32_t parentContextID;
258+
uint8_t contextKind;
259+
uint32_t contextID;
260+
261+
ContextTableKey() : parentContextID(-1), contextKind(-1), contextID(-1) {}
262+
263+
ContextTableKey(uint32_t parentContextID, uint8_t contextKind,
264+
uint32_t contextID)
265+
: parentContextID(parentContextID), contextKind(contextKind),
266+
contextID(contextID) {}
267+
268+
ContextTableKey(std::optional<Context> context, IdentifierID nameID)
269+
: parentContextID(context ? context->id.Value : (uint32_t)-1),
270+
contextKind(context ? (uint8_t)context->kind : (uint8_t)-1),
271+
contextID(nameID) {}
272+
273+
llvm::hash_code hashValue() const {
274+
return llvm::hash_value(
275+
std::tuple{parentContextID, contextKind, contextID});
276+
}
277+
};
278+
279+
inline bool operator==(const ContextTableKey &lhs, const ContextTableKey &rhs) {
280+
return lhs.parentContextID == rhs.parentContextID &&
281+
lhs.contextKind == rhs.contextKind && lhs.contextID == rhs.contextID;
261282
}
262283

263284
} // namespace api_notes
@@ -295,6 +316,29 @@ namespace llvm {
295316
lhs.Identifiers == rhs.Identifiers;
296317
}
297318
};
298-
}
319+
320+
template <> struct DenseMapInfo<clang::api_notes::ContextTableKey> {
321+
static inline clang::api_notes::ContextTableKey getEmptyKey() {
322+
return clang::api_notes::ContextTableKey();
323+
}
324+
325+
static inline clang::api_notes::ContextTableKey getTombstoneKey() {
326+
return clang::api_notes::ContextTableKey{
327+
DenseMapInfo<uint32_t>::getTombstoneKey(),
328+
DenseMapInfo<uint8_t>::getTombstoneKey(),
329+
DenseMapInfo<uint32_t>::getTombstoneKey()};
330+
}
331+
332+
static unsigned
333+
getHashValue(const clang::api_notes::ContextTableKey &value) {
334+
return value.hashValue();
335+
}
336+
337+
static bool isEqual(const clang::api_notes::ContextTableKey &lhs,
338+
const clang::api_notes::ContextTableKey &rhs) {
339+
return lhs == rhs;
340+
}
341+
};
342+
} // namespace llvm
299343

300344
#endif

clang/lib/APINotes/APINotesReader.cpp

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ namespace {
6666
return key;
6767
}
6868

69-
hash_value_type ComputeHash(internal_key_type key) {
70-
return static_cast<size_t>(llvm::hash_value(key));
71-
}
72-
7369
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
7470
return lhs == rhs;
7571
}
@@ -187,8 +183,7 @@ namespace {
187183
/// Used to deserialize the on-disk Objective-C class table.
188184
class ObjCContextIDTableInfo {
189185
public:
190-
// parent context ID, context kind, identifier ID
191-
using internal_key_type = std::tuple<uint32_t, uint8_t, uint32_t>;
186+
using internal_key_type = ContextTableKey;
192187
using external_key_type = internal_key_type;
193188
using data_type = unsigned;
194189
using hash_value_type = size_t;
@@ -203,7 +198,7 @@ namespace {
203198
}
204199

205200
hash_value_type ComputeHash(internal_key_type key) {
206-
return static_cast<size_t>(llvm::hash_value(key));
201+
return static_cast<size_t>(key.hashValue());
207202
}
208203

209204
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
@@ -241,7 +236,11 @@ namespace {
241236
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
242237
return endian::readNext<uint32_t, little, unaligned>(data);
243238
}
244-
239+
240+
hash_value_type ComputeHash(internal_key_type key) {
241+
return static_cast<size_t>(llvm::hash_value(key));
242+
}
243+
245244
static ObjCContextInfo readUnversioned(internal_key_type key,
246245
const uint8_t *&data) {
247246
ObjCContextInfo info;
@@ -294,7 +293,11 @@ namespace {
294293
char isInstance = endian::readNext<uint8_t, little, unaligned>(data);
295294
return std::make_tuple(classID, nameID, isInstance);
296295
}
297-
296+
297+
hash_value_type ComputeHash(internal_key_type key) {
298+
return static_cast<size_t>(llvm::hash_value(key));
299+
}
300+
298301
static ObjCPropertyInfo readUnversioned(internal_key_type key,
299302
const uint8_t *&data) {
300303
ObjCPropertyInfo info;
@@ -366,7 +369,11 @@ namespace {
366369
auto isInstance = endian::readNext<uint8_t, little, unaligned>(data);
367370
return internal_key_type{ classID, selectorID, isInstance };
368371
}
369-
372+
373+
hash_value_type ComputeHash(internal_key_type key) {
374+
return static_cast<size_t>(llvm::hash_value(key));
375+
}
376+
370377
static ObjCMethodInfo readUnversioned(internal_key_type key,
371378
const uint8_t *&data) {
372379
ObjCMethodInfo info;
@@ -432,8 +439,7 @@ namespace {
432439

433440
/// Used to deserialize the on-disk global variable table.
434441
class GlobalVariableTableInfo
435-
: public VersionedTableInfo<GlobalVariableTableInfo,
436-
std::tuple<uint32_t, uint8_t, uint32_t>,
442+
: public VersionedTableInfo<GlobalVariableTableInfo, ContextTableKey,
437443
GlobalVariableInfo> {
438444
public:
439445
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
@@ -443,6 +449,10 @@ namespace {
443449
return {contextID, contextKind, nameID};
444450
}
445451

452+
hash_value_type ComputeHash(internal_key_type key) {
453+
return static_cast<size_t>(key.hashValue());
454+
}
455+
446456
static GlobalVariableInfo readUnversioned(internal_key_type key,
447457
const uint8_t *&data) {
448458
GlobalVariableInfo info;
@@ -453,8 +463,7 @@ namespace {
453463

454464
/// Used to deserialize the on-disk global function table.
455465
class GlobalFunctionTableInfo
456-
: public VersionedTableInfo<GlobalFunctionTableInfo,
457-
std::tuple<uint32_t, uint8_t, uint32_t>,
466+
: public VersionedTableInfo<GlobalFunctionTableInfo, ContextTableKey,
458467
GlobalFunctionInfo> {
459468
public:
460469
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
@@ -463,7 +472,11 @@ namespace {
463472
auto nameID = endian::readNext<uint32_t, little, unaligned>(data);
464473
return {contextID, contextKind, nameID};
465474
}
466-
475+
476+
hash_value_type ComputeHash(internal_key_type key) {
477+
return static_cast<size_t>(key.hashValue());
478+
}
479+
467480
static GlobalFunctionInfo readUnversioned(internal_key_type key,
468481
const uint8_t *&data) {
469482
GlobalFunctionInfo info;
@@ -481,7 +494,11 @@ namespace {
481494
auto nameID = endian::readNext<uint32_t, little, unaligned>(data);
482495
return nameID;
483496
}
484-
497+
498+
hash_value_type ComputeHash(internal_key_type key) {
499+
return static_cast<size_t>(llvm::hash_value(key));
500+
}
501+
485502
static EnumConstantInfo readUnversioned(internal_key_type key,
486503
const uint8_t *&data) {
487504
EnumConstantInfo info;
@@ -492,16 +509,19 @@ namespace {
492509

493510
/// Used to deserialize the on-disk tag table.
494511
class TagTableInfo
495-
: public VersionedTableInfo<
496-
TagTableInfo, std::tuple<uint32_t, uint8_t, uint32_t>, TagInfo> {
512+
: public VersionedTableInfo<TagTableInfo, ContextTableKey, TagInfo> {
497513
public:
498514
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
499515
auto contextID = endian::readNext<uint32_t, little, unaligned>(data);
500516
auto contextKind = endian::readNext<uint8_t, little, unaligned>(data);
501517
auto nameID = endian::readNext<IdentifierID, little, unaligned>(data);
502518
return {contextID, contextKind, nameID};
503519
}
504-
520+
521+
hash_value_type ComputeHash(internal_key_type key) {
522+
return static_cast<size_t>(key.hashValue());
523+
}
524+
505525
static TagInfo readUnversioned(internal_key_type key,
506526
const uint8_t *&data) {
507527
TagInfo info;
@@ -523,9 +543,7 @@ namespace {
523543

524544
/// Used to deserialize the on-disk typedef table.
525545
class TypedefTableInfo
526-
: public VersionedTableInfo<TypedefTableInfo,
527-
std::tuple<uint32_t, uint8_t, uint32_t>,
528-
TypedefInfo> {
546+
: public VersionedTableInfo<TypedefTableInfo, ContextTableKey, TypedefInfo> {
529547
public:
530548
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
531549
auto contextID = endian::readNext<uint32_t, little, unaligned>(data);
@@ -534,6 +552,10 @@ namespace {
534552
return {contextID, contextKind, nameID};
535553
}
536554

555+
hash_value_type ComputeHash(internal_key_type key) {
556+
return static_cast<size_t>(key.hashValue());
557+
}
558+
537559
static TypedefInfo readUnversioned(internal_key_type key,
538560
const uint8_t *&data) {
539561
TypedefInfo info;
@@ -1839,7 +1861,7 @@ auto APINotesReader::lookupObjCClassID(StringRef name) -> Optional<ContextID> {
18391861
// ObjC classes can't be declared in C++ namespaces, so use -1 as the global
18401862
// context.
18411863
auto knownID = Impl.ObjCContextIDTable->find(
1842-
{-1, (uint8_t)ContextKind::ObjCClass, *classID});
1864+
ContextTableKey(-1, (uint8_t)ContextKind::ObjCClass, *classID));
18431865
if (knownID == Impl.ObjCContextIDTable->end())
18441866
return None;
18451867

@@ -1874,7 +1896,7 @@ auto APINotesReader::lookupObjCProtocolID(StringRef name)
18741896
// ObjC classes can't be declared in C++ namespaces, so use -1 as the global
18751897
// context.
18761898
auto knownID = Impl.ObjCContextIDTable->find(
1877-
{-1, (uint8_t)ContextKind::ObjCProtocol, *classID});
1899+
ContextTableKey(-1, (uint8_t)ContextKind::ObjCProtocol, *classID));
18781900
if (knownID == Impl.ObjCContextIDTable->end())
18791901
return None;
18801902

@@ -1949,7 +1971,7 @@ auto APINotesReader::lookupGlobalVariable(std::optional<Context> context,
19491971
if (!nameID)
19501972
return None;
19511973

1952-
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
1974+
ContextTableKey key(context, *nameID);
19531975

19541976
auto known = Impl.GlobalVariableTable->find(key);
19551977
if (known == Impl.GlobalVariableTable->end())
@@ -1968,7 +1990,7 @@ auto APINotesReader::lookupGlobalFunction(std::optional<Context> context,
19681990
if (!nameID)
19691991
return None;
19701992

1971-
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
1993+
ContextTableKey key(context, *nameID);
19721994

19731995
auto known = Impl.GlobalFunctionTable->find(key);
19741996
if (known == Impl.GlobalFunctionTable->end())
@@ -2002,7 +2024,7 @@ auto APINotesReader::lookupTag(std::optional<Context> context, StringRef name)
20022024
if (!nameID)
20032025
return None;
20042026

2005-
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
2027+
ContextTableKey key(context, *nameID);
20062028

20072029
auto known = Impl.TagTable->find(key);
20082030
if (known == Impl.TagTable->end())
@@ -2021,7 +2043,7 @@ auto APINotesReader::lookupTypedef(std::optional<Context> context,
20212043
if (!nameID)
20222044
return None;
20232045

2024-
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
2046+
ContextTableKey key(context, *nameID);
20252047

20262048
auto known = Impl.TypedefTable->find(key);
20272049
if (known == Impl.TypedefTable->end())

0 commit comments

Comments
 (0)