Skip to content

Commit ecdd17a

Browse files
authored
Merge pull request #9008 from swiftlang/egorzhdan/20240625-apinotes-refactor
🍒[APINotes] Minor refactoring from upstream
2 parents 146866e + a42edc3 commit ecdd17a

File tree

9 files changed

+115
-123
lines changed

9 files changed

+115
-123
lines changed

clang/include/clang/APINotes/APINotesReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class APINotesReader {
108108
/// \param Name The name of the class we're looking for.
109109
///
110110
/// \returns The information about the class, if known.
111-
VersionedInfo<ObjCContextInfo> lookupObjCClassInfo(llvm::StringRef Name);
111+
VersionedInfo<ContextInfo> lookupObjCClassInfo(llvm::StringRef Name);
112112

113113
/// Look for the context ID of the given Objective-C protocol.
114114
///
@@ -122,7 +122,7 @@ class APINotesReader {
122122
/// \param Name The name of the protocol we're looking for.
123123
///
124124
/// \returns The information about the protocol, if known.
125-
VersionedInfo<ObjCContextInfo> lookupObjCProtocolInfo(llvm::StringRef Name);
125+
VersionedInfo<ContextInfo> lookupObjCProtocolInfo(llvm::StringRef Name);
126126

127127
/// Look for information regarding the given Objective-C property in
128128
/// the given context.

clang/include/clang/APINotes/APINotesWriter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class APINotesWriter {
5353
///
5454
/// \returns the ID of the class, protocol, or namespace, which can be used to
5555
/// add properties and methods to the class/protocol/namespace.
56-
ContextID addObjCContext(std::optional<ContextID> ParentCtxID,
57-
llvm::StringRef Name, ContextKind Kind,
58-
const ObjCContextInfo &Info,
59-
llvm::VersionTuple SwiftVersion);
56+
ContextID addContext(std::optional<ContextID> ParentCtxID,
57+
llvm::StringRef Name, ContextKind Kind,
58+
const ContextInfo &Info,
59+
llvm::VersionTuple SwiftVersion);
6060

6161
/// Add information about a specific Objective-C property.
6262
///

clang/include/clang/APINotes/Types.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ inline bool operator!=(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
192192
return !(LHS == RHS);
193193
}
194194

195-
/// Describes API notes data for an Objective-C class or protocol.
196-
class ObjCContextInfo : public CommonTypeInfo {
195+
/// Describes API notes data for an Objective-C class or protocol or a C++
196+
/// namespace.
197+
class ContextInfo : public CommonTypeInfo {
197198
/// Whether this class has a default nullability.
198199
LLVM_PREFERRED_TYPE(bool)
199200
unsigned HasDefaultNullability : 1;
@@ -217,7 +218,7 @@ class ObjCContextInfo : public CommonTypeInfo {
217218
unsigned SwiftObjCMembers : 1;
218219

219220
public:
220-
ObjCContextInfo()
221+
ContextInfo()
221222
: HasDefaultNullability(0), DefaultNullability(0), HasDesignatedInits(0),
222223
SwiftImportAsNonGenericSpecified(false), SwiftImportAsNonGeneric(false),
223224
SwiftObjCMembersSpecified(false), SwiftObjCMembers(false) {}
@@ -262,16 +263,9 @@ class ObjCContextInfo : public CommonTypeInfo {
262263
SwiftObjCMembers = Value.value_or(false);
263264
}
264265

265-
/// Strip off any information within the class information structure that is
266-
/// module-local, such as 'audited' flags.
267-
void stripModuleLocalInfo() {
268-
HasDefaultNullability = false;
269-
DefaultNullability = 0;
270-
}
271-
272-
friend bool operator==(const ObjCContextInfo &, const ObjCContextInfo &);
266+
friend bool operator==(const ContextInfo &, const ContextInfo &);
273267

274-
ObjCContextInfo &operator|=(const ObjCContextInfo &RHS) {
268+
ContextInfo &operator|=(const ContextInfo &RHS) {
275269
// Merge inherited info.
276270
static_cast<CommonTypeInfo &>(*this) |= RHS;
277271

@@ -294,15 +288,15 @@ class ObjCContextInfo : public CommonTypeInfo {
294288
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS);
295289
};
296290

297-
inline bool operator==(const ObjCContextInfo &LHS, const ObjCContextInfo &RHS) {
291+
inline bool operator==(const ContextInfo &LHS, const ContextInfo &RHS) {
298292
return static_cast<const CommonTypeInfo &>(LHS) == RHS &&
299293
LHS.getDefaultNullability() == RHS.getDefaultNullability() &&
300294
LHS.HasDesignatedInits == RHS.HasDesignatedInits &&
301295
LHS.getSwiftImportAsNonGeneric() == RHS.getSwiftImportAsNonGeneric() &&
302296
LHS.getSwiftObjCMembers() == RHS.getSwiftObjCMembers();
303297
}
304298

305-
inline bool operator!=(const ObjCContextInfo &LHS, const ObjCContextInfo &RHS) {
299+
inline bool operator!=(const ContextInfo &LHS, const ContextInfo &RHS) {
306300
return !(LHS == RHS);
307301
}
308302

@@ -387,7 +381,7 @@ class ObjCPropertyInfo : public VariableInfo {
387381
friend bool operator==(const ObjCPropertyInfo &, const ObjCPropertyInfo &);
388382

389383
/// Merge class-wide information into the given property.
390-
ObjCPropertyInfo &operator|=(const ObjCContextInfo &RHS) {
384+
ObjCPropertyInfo &operator|=(const ContextInfo &RHS) {
391385
static_cast<CommonEntityInfo &>(*this) |= RHS;
392386

393387
// Merge nullability.
@@ -626,7 +620,7 @@ class ObjCMethodInfo : public FunctionInfo {
626620

627621
friend bool operator==(const ObjCMethodInfo &, const ObjCMethodInfo &);
628622

629-
ObjCMethodInfo &operator|=(const ObjCContextInfo &RHS) {
623+
ObjCMethodInfo &operator|=(const ContextInfo &RHS) {
630624
// Merge Nullability.
631625
if (!NullabilityAudited) {
632626
if (auto Nullable = RHS.getDefaultNullability()) {

clang/lib/APINotes/APINotesFormat.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,26 @@ using IdentifierDataLayout = llvm::BCRecordLayout<
132132
>;
133133
} // namespace identifier_block
134134

135-
namespace objc_context_block {
135+
namespace context_block {
136136
enum {
137-
OBJC_CONTEXT_ID_DATA = 1,
138-
OBJC_CONTEXT_INFO_DATA = 2,
137+
CONTEXT_ID_DATA = 1,
138+
CONTEXT_INFO_DATA = 2,
139139
};
140140

141-
using ObjCContextIDLayout =
142-
llvm::BCRecordLayout<OBJC_CONTEXT_ID_DATA, // record ID
141+
using ContextIDLayout =
142+
llvm::BCRecordLayout<CONTEXT_ID_DATA, // record ID
143143
llvm::BCVBR<16>, // table offset within the blob (see
144144
// below)
145145
llvm::BCBlob // map from ObjC class names/protocol (as
146146
// IDs) to context IDs
147147
>;
148148

149-
using ObjCContextInfoLayout = llvm::BCRecordLayout<
150-
OBJC_CONTEXT_INFO_DATA, // record ID
151-
llvm::BCVBR<16>, // table offset within the blob (see below)
152-
llvm::BCBlob // map from ObjC context IDs to context information.
149+
using ContextInfoLayout = llvm::BCRecordLayout<
150+
CONTEXT_INFO_DATA, // record ID
151+
llvm::BCVBR<16>, // table offset within the blob (see below)
152+
llvm::BCBlob // map from ObjC context IDs to context information.
153153
>;
154-
} // namespace objc_context_block
154+
} // namespace context_block
155155

156156
namespace objc_property_block {
157157
enum {

clang/lib/APINotes/APINotesReader.cpp

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ class IdentifierTableInfo {
176176
}
177177
};
178178

179-
/// Used to deserialize the on-disk Objective-C class table.
180-
class ObjCContextIDTableInfo {
179+
/// Used to deserialize the on-disk table of Objective-C classes and C++
180+
/// namespaces.
181+
class ContextIDTableInfo {
181182
public:
182183
using internal_key_type = ContextTableKey;
183184
using external_key_type = internal_key_type;
@@ -221,9 +222,8 @@ class ObjCContextIDTableInfo {
221222
};
222223

223224
/// Used to deserialize the on-disk Objective-C property table.
224-
class ObjCContextInfoTableInfo
225-
: public VersionedTableInfo<ObjCContextInfoTableInfo, unsigned,
226-
ObjCContextInfo> {
225+
class ContextInfoTableInfo
226+
: public VersionedTableInfo<ContextInfoTableInfo, unsigned, ContextInfo> {
227227
public:
228228
static internal_key_type ReadKey(const uint8_t *Data, unsigned Length) {
229229
return endian::readNext<uint32_t, llvm::endianness::little>(Data);
@@ -233,9 +233,9 @@ class ObjCContextInfoTableInfo
233233
return static_cast<size_t>(llvm::hash_value(Key));
234234
}
235235

236-
static ObjCContextInfo readUnversioned(internal_key_type Key,
237-
const uint8_t *&Data) {
238-
ObjCContextInfo Info;
236+
static ContextInfo readUnversioned(internal_key_type Key,
237+
const uint8_t *&Data) {
238+
ContextInfo Info;
239239
ReadCommonTypeInfo(Data, Info);
240240
uint8_t Payload = *Data++;
241241

@@ -617,17 +617,17 @@ class APINotesReader::Implementation {
617617
/// The identifier table.
618618
std::unique_ptr<SerializedIdentifierTable> IdentifierTable;
619619

620-
using SerializedObjCContextIDTable =
621-
llvm::OnDiskIterableChainedHashTable<ObjCContextIDTableInfo>;
620+
using SerializedContextIDTable =
621+
llvm::OnDiskIterableChainedHashTable<ContextIDTableInfo>;
622622

623-
/// The Objective-C context ID table.
624-
std::unique_ptr<SerializedObjCContextIDTable> ObjCContextIDTable;
623+
/// The Objective-C / C++ context ID table.
624+
std::unique_ptr<SerializedContextIDTable> ContextIDTable;
625625

626-
using SerializedObjCContextInfoTable =
627-
llvm::OnDiskIterableChainedHashTable<ObjCContextInfoTableInfo>;
626+
using SerializedContextInfoTable =
627+
llvm::OnDiskIterableChainedHashTable<ContextInfoTableInfo>;
628628

629629
/// The Objective-C context info table.
630-
std::unique_ptr<SerializedObjCContextInfoTable> ObjCContextInfoTable;
630+
std::unique_ptr<SerializedContextInfoTable> ContextInfoTable;
631631

632632
using SerializedObjCPropertyTable =
633633
llvm::OnDiskIterableChainedHashTable<ObjCPropertyTableInfo>;
@@ -688,8 +688,8 @@ class APINotesReader::Implementation {
688688
llvm::SmallVectorImpl<uint64_t> &Scratch);
689689
bool readIdentifierBlock(llvm::BitstreamCursor &Cursor,
690690
llvm::SmallVectorImpl<uint64_t> &Scratch);
691-
bool readObjCContextBlock(llvm::BitstreamCursor &Cursor,
692-
llvm::SmallVectorImpl<uint64_t> &Scratch);
691+
bool readContextBlock(llvm::BitstreamCursor &Cursor,
692+
llvm::SmallVectorImpl<uint64_t> &Scratch);
693693
bool readObjCPropertyBlock(llvm::BitstreamCursor &Cursor,
694694
llvm::SmallVectorImpl<uint64_t> &Scratch);
695695
bool readObjCMethodBlock(llvm::BitstreamCursor &Cursor,
@@ -910,7 +910,7 @@ bool APINotesReader::Implementation::readIdentifierBlock(
910910
return false;
911911
}
912912

913-
bool APINotesReader::Implementation::readObjCContextBlock(
913+
bool APINotesReader::Implementation::readContextBlock(
914914
llvm::BitstreamCursor &Cursor, llvm::SmallVectorImpl<uint64_t> &Scratch) {
915915
if (Cursor.EnterSubBlock(OBJC_CONTEXT_BLOCK_ID))
916916
return true;
@@ -954,31 +954,30 @@ bool APINotesReader::Implementation::readObjCContextBlock(
954954
}
955955
unsigned Kind = MaybeKind.get();
956956
switch (Kind) {
957-
case objc_context_block::OBJC_CONTEXT_ID_DATA: {
958-
// Already saw Objective-C context ID table.
959-
if (ObjCContextIDTable)
957+
case context_block::CONTEXT_ID_DATA: {
958+
// Already saw Objective-C / C++ context ID table.
959+
if (ContextIDTable)
960960
return true;
961961

962962
uint32_t tableOffset;
963-
objc_context_block::ObjCContextIDLayout::readRecord(Scratch, tableOffset);
963+
context_block::ContextIDLayout::readRecord(Scratch, tableOffset);
964964
auto base = reinterpret_cast<const uint8_t *>(BlobData.data());
965965

966-
ObjCContextIDTable.reset(SerializedObjCContextIDTable::Create(
966+
ContextIDTable.reset(SerializedContextIDTable::Create(
967967
base + tableOffset, base + sizeof(uint32_t), base));
968968
break;
969969
}
970970

971-
case objc_context_block::OBJC_CONTEXT_INFO_DATA: {
972-
// Already saw Objective-C context info table.
973-
if (ObjCContextInfoTable)
971+
case context_block::CONTEXT_INFO_DATA: {
972+
// Already saw Objective-C / C++ context info table.
973+
if (ContextInfoTable)
974974
return true;
975975

976976
uint32_t tableOffset;
977-
objc_context_block::ObjCContextInfoLayout::readRecord(Scratch,
978-
tableOffset);
977+
context_block::ContextInfoLayout::readRecord(Scratch, tableOffset);
979978
auto base = reinterpret_cast<const uint8_t *>(BlobData.data());
980979

981-
ObjCContextInfoTable.reset(SerializedObjCContextInfoTable::Create(
980+
ContextInfoTable.reset(SerializedContextInfoTable::Create(
982981
base + tableOffset, base + sizeof(uint32_t), base));
983982
break;
984983
}
@@ -1682,7 +1681,7 @@ APINotesReader::APINotesReader(llvm::MemoryBuffer *InputBuffer,
16821681

16831682
case OBJC_CONTEXT_BLOCK_ID:
16841683
if (!HasValidControlBlock ||
1685-
Implementation->readObjCContextBlock(Cursor, Scratch)) {
1684+
Implementation->readContextBlock(Cursor, Scratch)) {
16861685
Failed = true;
16871686
return;
16881687
}
@@ -1827,7 +1826,7 @@ APINotesReader::VersionedInfo<T>::VersionedInfo(
18271826

18281827
auto APINotesReader::lookupObjCClassID(llvm::StringRef Name)
18291828
-> std::optional<ContextID> {
1830-
if (!Implementation->ObjCContextIDTable)
1829+
if (!Implementation->ContextIDTable)
18311830
return std::nullopt;
18321831

18331832
std::optional<IdentifierID> ClassID = Implementation->getIdentifier(Name);
@@ -1836,33 +1835,33 @@ auto APINotesReader::lookupObjCClassID(llvm::StringRef Name)
18361835

18371836
// ObjC classes can't be declared in C++ namespaces, so use -1 as the global
18381837
// context.
1839-
auto KnownID = Implementation->ObjCContextIDTable->find(
1838+
auto KnownID = Implementation->ContextIDTable->find(
18401839
ContextTableKey(-1, (uint8_t)ContextKind::ObjCClass, *ClassID));
1841-
if (KnownID == Implementation->ObjCContextIDTable->end())
1840+
if (KnownID == Implementation->ContextIDTable->end())
18421841
return std::nullopt;
18431842

18441843
return ContextID(*KnownID);
18451844
}
18461845

18471846
auto APINotesReader::lookupObjCClassInfo(llvm::StringRef Name)
1848-
-> VersionedInfo<ObjCContextInfo> {
1849-
if (!Implementation->ObjCContextInfoTable)
1847+
-> VersionedInfo<ContextInfo> {
1848+
if (!Implementation->ContextInfoTable)
18501849
return std::nullopt;
18511850

18521851
std::optional<ContextID> CtxID = lookupObjCClassID(Name);
18531852
if (!CtxID)
18541853
return std::nullopt;
18551854

1856-
auto KnownInfo = Implementation->ObjCContextInfoTable->find(CtxID->Value);
1857-
if (KnownInfo == Implementation->ObjCContextInfoTable->end())
1855+
auto KnownInfo = Implementation->ContextInfoTable->find(CtxID->Value);
1856+
if (KnownInfo == Implementation->ContextInfoTable->end())
18581857
return std::nullopt;
18591858

18601859
return {Implementation->SwiftVersion, *KnownInfo};
18611860
}
18621861

18631862
auto APINotesReader::lookupObjCProtocolID(llvm::StringRef Name)
18641863
-> std::optional<ContextID> {
1865-
if (!Implementation->ObjCContextIDTable)
1864+
if (!Implementation->ContextIDTable)
18661865
return std::nullopt;
18671866

18681867
std::optional<IdentifierID> classID = Implementation->getIdentifier(Name);
@@ -1871,25 +1870,25 @@ auto APINotesReader::lookupObjCProtocolID(llvm::StringRef Name)
18711870

18721871
// ObjC classes can't be declared in C++ namespaces, so use -1 as the global
18731872
// context.
1874-
auto KnownID = Implementation->ObjCContextIDTable->find(
1873+
auto KnownID = Implementation->ContextIDTable->find(
18751874
ContextTableKey(-1, (uint8_t)ContextKind::ObjCProtocol, *classID));
1876-
if (KnownID == Implementation->ObjCContextIDTable->end())
1875+
if (KnownID == Implementation->ContextIDTable->end())
18771876
return std::nullopt;
18781877

18791878
return ContextID(*KnownID);
18801879
}
18811880

18821881
auto APINotesReader::lookupObjCProtocolInfo(llvm::StringRef Name)
1883-
-> VersionedInfo<ObjCContextInfo> {
1884-
if (!Implementation->ObjCContextInfoTable)
1882+
-> VersionedInfo<ContextInfo> {
1883+
if (!Implementation->ContextInfoTable)
18851884
return std::nullopt;
18861885

18871886
std::optional<ContextID> CtxID = lookupObjCProtocolID(Name);
18881887
if (!CtxID)
18891888
return std::nullopt;
18901889

1891-
auto KnownInfo = Implementation->ObjCContextInfoTable->find(CtxID->Value);
1892-
if (KnownInfo == Implementation->ObjCContextInfoTable->end())
1890+
auto KnownInfo = Implementation->ContextInfoTable->find(CtxID->Value);
1891+
if (KnownInfo == Implementation->ContextInfoTable->end())
18931892
return std::nullopt;
18941893

18951894
return {Implementation->SwiftVersion, *KnownInfo};
@@ -2026,7 +2025,7 @@ auto APINotesReader::lookupTypedef(llvm::StringRef Name,
20262025
auto APINotesReader::lookupNamespaceID(
20272026
llvm::StringRef Name, std::optional<ContextID> ParentNamespaceID)
20282027
-> std::optional<ContextID> {
2029-
if (!Implementation->ObjCContextIDTable)
2028+
if (!Implementation->ContextIDTable)
20302029
return std::nullopt;
20312030

20322031
std::optional<IdentifierID> NamespaceID = Implementation->getIdentifier(Name);
@@ -2035,9 +2034,9 @@ auto APINotesReader::lookupNamespaceID(
20352034

20362035
uint32_t RawParentNamespaceID =
20372036
ParentNamespaceID ? ParentNamespaceID->Value : -1;
2038-
auto KnownID = Implementation->ObjCContextIDTable->find(
2037+
auto KnownID = Implementation->ContextIDTable->find(
20392038
{RawParentNamespaceID, (uint8_t)ContextKind::Namespace, *NamespaceID});
2040-
if (KnownID == Implementation->ObjCContextIDTable->end())
2039+
if (KnownID == Implementation->ContextIDTable->end())
20412040
return std::nullopt;
20422041

20432042
return ContextID(*KnownID);

clang/lib/APINotes/APINotesTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LLVM_DUMP_METHOD void CommonTypeInfo::dump(llvm::raw_ostream &OS) const {
3232
OS << '\n';
3333
}
3434

35-
LLVM_DUMP_METHOD void ObjCContextInfo::dump(llvm::raw_ostream &OS) {
35+
LLVM_DUMP_METHOD void ContextInfo::dump(llvm::raw_ostream &OS) {
3636
static_cast<CommonTypeInfo &>(*this).dump(OS);
3737
if (HasDefaultNullability)
3838
OS << "DefaultNullability: " << DefaultNullability << ' ';

0 commit comments

Comments
 (0)