Skip to content

Commit b88b6a2

Browse files
authored
[clang] Assert the enum FPOpts and LangOpts fit into the storage (#126166)
Fix existing failure
1 parent 1889155 commit b88b6a2

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

clang/include/clang/Basic/FPOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ OPTION(FPEvalMethod, LangOptions::FPEvalMethodKind, 2, AllowApproxFunc)
2828
OPTION(Float16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, FPEvalMethod)
2929
OPTION(BFloat16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, Float16ExcessPrecision)
3030
OPTION(MathErrno, bool, 1, BFloat16ExcessPrecision)
31-
OPTION(ComplexRange, LangOptions::ComplexRangeKind, 2, MathErrno)
31+
OPTION(ComplexRange, LangOptions::ComplexRangeKind, 3, MathErrno)
3232
#undef OPTION

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ BENIGN_LANGOPT(NoSignedZero , 1, 0, "Permit Floating Point optimization wit
238238
BENIGN_LANGOPT(AllowRecip , 1, 0, "Permit Floating Point reciprocal")
239239
BENIGN_LANGOPT(ApproxFunc , 1, 0, "Permit Floating Point approximation")
240240

241-
ENUM_LANGOPT(ComplexRange, ComplexRangeKind, 2, CX_None, "Enable use of range reduction for complex arithmetics.")
241+
ENUM_LANGOPT(ComplexRange, ComplexRangeKind, 3, CX_None, "Enable use of range reduction for complex arithmetics.")
242242

243243
BENIGN_LANGOPT(ObjCGCBitmapPrint , 1, 0, "printing of GC's bitmap layout for __weak/__strong ivars")
244244

clang/include/clang/Basic/LangOptions.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,12 @@ class LangOptions : public LangOptionsBase {
648648

649649
// Define accessors/mutators for language options of enumeration type.
650650
#define LANGOPT(Name, Bits, Default, Description)
651-
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
652-
Type get##Name() const { return static_cast<Type>(Name); } \
653-
void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
651+
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
652+
Type get##Name() const { return static_cast<Type>(Name); } \
653+
void set##Name(Type Value) { \
654+
assert(static_cast<unsigned>(Value) < (1u << Bits)); \
655+
Name = static_cast<unsigned>(Value); \
656+
}
654657
#include "clang/Basic/LangOptions.def"
655658

656659
/// Are we compiling a module?
@@ -959,11 +962,14 @@ class FPOptions {
959962
void applyChanges(FPOptionsOverride FPO);
960963

961964
// We can define most of the accessors automatically:
965+
// TODO: consider enforcing the assertion that value fits within bits
966+
// statically.
962967
#define OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
963968
TYPE get##NAME() const { \
964969
return static_cast<TYPE>((Value & NAME##Mask) >> NAME##Shift); \
965970
} \
966971
void set##NAME(TYPE value) { \
972+
assert(storage_type(value) < (1u << WIDTH)); \
967973
Value = (Value & ~NAME##Mask) | (storage_type(value) << NAME##Shift); \
968974
}
969975
#include "clang/Basic/FPOptions.def"

0 commit comments

Comments
 (0)