Skip to content

Commit 97e519f

Browse files
committed
add BSI::dump and remove conversion functions for BoundsSafetyKind
1 parent 25b8b6c commit 97e519f

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

clang/include/clang/APINotes/Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ inline bool operator!=(const ContextInfo &LHS, const ContextInfo &RHS) {
304304
class BoundsSafetyInfo {
305305
public:
306306
enum class BoundsSafetyKind {
307-
CountedBy,
307+
CountedBy = 0,
308308
CountedByOrNull,
309309
SizedBy,
310310
SizedByOrNull,
@@ -354,6 +354,8 @@ class BoundsSafetyInfo {
354354
}
355355

356356
friend bool operator==(const BoundsSafetyInfo &, const BoundsSafetyInfo &);
357+
358+
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
357359
};
358360

359361
inline bool operator==(const BoundsSafetyInfo &LHS,

clang/lib/APINotes/APINotesReader.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,23 +323,6 @@ class FieldTableInfo
323323
};
324324

325325
/* TO_UPSTREAM(BoundsSafety) ON */
326-
BoundsSafetyInfo::BoundsSafetyKind readKindFlag(uint8_t kind) {
327-
switch (kind) {
328-
case 0x00:
329-
return BoundsSafetyInfo::BoundsSafetyKind::CountedBy;
330-
case 0x01:
331-
return BoundsSafetyInfo::BoundsSafetyKind::CountedByOrNull;
332-
case 0x02:
333-
return BoundsSafetyInfo::BoundsSafetyKind::SizedBy;
334-
case 0x03:
335-
return BoundsSafetyInfo::BoundsSafetyKind::SizedByOrNull;
336-
case 0x04:
337-
return BoundsSafetyInfo::BoundsSafetyKind::EndedBy;
338-
default:
339-
llvm_unreachable("invalid bounds safety kind");
340-
}
341-
}
342-
343326
/// Read serialized BoundsSafetyInfo.
344327
void ReadBoundsSafetyInfo(const uint8_t *&Data, BoundsSafetyInfo &Info) {
345328
uint8_t Payload = endian::readNext<uint8_t, llvm::endianness::little>(Data);
@@ -352,7 +335,9 @@ void ReadBoundsSafetyInfo(const uint8_t *&Data, BoundsSafetyInfo &Info) {
352335

353336
if (Payload & 0x01) {
354337
uint8_t Kind = (Payload >> 1) & 0x7;
355-
Info.setKindAudited(readKindFlag(Kind));
338+
assert(Kind >= (uint8_t)BoundsSafetyInfo::BoundsSafetyKind::CountedBy);
339+
assert(Kind <= (uint8_t)BoundsSafetyInfo::BoundsSafetyKind::EndedBy);
340+
Info.setKindAudited((BoundsSafetyInfo::BoundsSafetyKind)Kind);
356341
}
357342

358343
uint16_t ExternalBoundsLen =

clang/lib/APINotes/APINotesTypes.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ LLVM_DUMP_METHOD void ObjCPropertyInfo::dump(llvm::raw_ostream &OS) const {
6161
OS << '\n';
6262
}
6363

64+
LLVM_DUMP_METHOD void BoundsSafetyInfo::dump(llvm::raw_ostream &OS) const {
65+
if (KindAudited) {
66+
assert((BoundsSafetyKind)Kind >= BoundsSafetyKind::CountedBy);
67+
assert((BoundsSafetyKind)Kind <= BoundsSafetyKind::EndedBy);
68+
switch ((BoundsSafetyKind)Kind) {
69+
case BoundsSafetyKind::CountedBy:
70+
OS << "[counted_by] ";
71+
break;
72+
case BoundsSafetyKind::CountedByOrNull:
73+
OS << "[counted_by_or_null] ";
74+
break;
75+
case BoundsSafetyKind::SizedBy:
76+
OS << "[sized_by] ";
77+
break;
78+
case BoundsSafetyKind::SizedByOrNull:
79+
OS << "[sized_by_or_null] ";
80+
break;
81+
case BoundsSafetyKind::EndedBy:
82+
OS << "[ended_by] ";
83+
break;
84+
}
85+
}
86+
if (LevelAudited)
87+
OS << "Level: " << Level << " ";
88+
OS << "ExternalBounds: "
89+
<< (ExternalBounds.empty() ? "<missing>" : ExternalBounds) << '\n';
90+
}
91+
6492
LLVM_DUMP_METHOD void ParamInfo::dump(llvm::raw_ostream &OS) const {
6593
static_cast<const VariableInfo &>(*this).dump(OS);
6694
if (NoEscapeSpecified)
@@ -69,6 +97,8 @@ LLVM_DUMP_METHOD void ParamInfo::dump(llvm::raw_ostream &OS) const {
6997
OS << (Lifetimebound ? "[Lifetimebound] " : "");
7098
OS << "RawRetainCountConvention: " << RawRetainCountConvention << ' ';
7199
OS << '\n';
100+
if (BoundsSafety)
101+
BoundsSafety->dump(OS);
72102
}
73103

74104
LLVM_DUMP_METHOD void FunctionInfo::dump(llvm::raw_ostream &OS) const {

clang/lib/APINotes/APINotesWriter.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,27 +1076,12 @@ void APINotesWriter::Implementation::writeGlobalVariableBlock(
10761076

10771077
/* TO_UPSTREAM(BoundsSafety) ON */
10781078
namespace {
1079-
uint8_t getKindFlag(BoundsSafetyInfo::BoundsSafetyKind kind) {
1080-
switch (kind) {
1081-
case BoundsSafetyInfo::BoundsSafetyKind::CountedBy:
1082-
return 0x00;
1083-
case BoundsSafetyInfo::BoundsSafetyKind::CountedByOrNull:
1084-
return 0x01;
1085-
case BoundsSafetyInfo::BoundsSafetyKind::SizedBy:
1086-
return 0x02;
1087-
case BoundsSafetyInfo::BoundsSafetyKind::SizedByOrNull:
1088-
return 0x03;
1089-
case BoundsSafetyInfo::BoundsSafetyKind::EndedBy:
1090-
return 0x04;
1091-
}
1092-
}
1093-
10941079
void emitBoundsSafetyInfo(raw_ostream &OS, const BoundsSafetyInfo &BSI) {
10951080
llvm::support::endian::Writer writer(OS, llvm::endianness::little);
10961081
uint8_t flags = 0;
10971082
if (auto kind = BSI.getKind()) {
10981083
flags |= 0x01; // 1 bit
1099-
flags |= getKindFlag(*kind) << 1; // 3 bits
1084+
flags |= (uint8_t)*kind << 1; // 3 bits
11001085
}
11011086
flags <<= 4;
11021087
if (auto level = BSI.getLevel()) {

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ static void applyBoundsSafety(Sema &S, ValueDecl *D,
427427

428428
std::string AttrName;
429429
AttributeCommonInfo::Kind Kind;
430+
assert(*Info.getKind() >= BoundsSafetyKind::CountedBy);
431+
assert(*Info.getKind() <= BoundsSafetyKind::EndedBy);
430432
switch (*Info.getKind()) {
431433
case BoundsSafetyKind::CountedBy:
432434
AttrName = "__counted_by";

0 commit comments

Comments
 (0)