Skip to content

Commit 7404b85

Browse files
committed
[clang][NFC] Use enum for -fstrict-flex-arrays
Use enums for the strict flex arrays flag so that it's more readable. Differential Revision: https://reviews.llvm.org/D135107
1 parent ae57333 commit 7404b85

File tree

9 files changed

+41
-13
lines changed

9 files changed

+41
-13
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ class Expr : public ValueStmt {
528528
/// When IgnoreTemplateOrMacroSubstitution is set, it doesn't consider sizes
529529
/// resulting from the substitution of a macro or a template as special sizes.
530530
bool isFlexibleArrayMemberLike(
531-
ASTContext &Context, unsigned StrictFlexArraysLevel,
531+
ASTContext &Context,
532+
LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel,
532533
bool IgnoreTemplateOrMacroSubstitution = false) const;
533534

534535
/// isIntegerConstantExpr - Return the value if this expression is a valid

clang/include/clang/Basic/LangOptions.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ LANGOPT(PaddingOnUnsignedFixedPoint, 1, 0,
427427
LANGOPT(RegisterStaticDestructors, 1, 1, "Register C++ static destructors")
428428

429429
LANGOPT(MatrixTypes, 1, 0, "Enable or disable the builtin matrix type")
430-
LANGOPT(StrictFlexArrays, 2, 0, "Rely on strict definition of flexible arrays")
430+
431+
ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2,
432+
StrictFlexArraysLevelKind::Default,
433+
"Rely on strict definition of flexible arrays")
431434

432435
COMPATIBLE_VALUE_LANGOPT(MaxTokens, 32, 0, "Max number of tokens per TU or 0")
433436

clang/include/clang/Basic/LangOptions.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ class LangOptions : public LangOptionsBase {
365365
All,
366366
};
367367

368+
enum class StrictFlexArraysLevelKind {
369+
/// Any trailing array member is a FAM.
370+
Default = 0,
371+
/// Any trailing array member of undefined, 0, or 1 size is a FAM.
372+
OneZeroOrIncomplete = 1,
373+
/// Any trailing array member of undefined or 0 size is a FAM.
374+
ZeroOrIncomplete = 2,
375+
/// Any trailing array member of undefined size is a FAM.
376+
Incomplete = 3,
377+
};
378+
368379
public:
369380
/// The used language standard.
370381
LangStandard::Kind LangStd;

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,10 +1149,12 @@ def fapple_kext : Flag<["-"], "fapple-kext">, Group<f_Group>, Flags<[CC1Option]>
11491149
MarshallingInfoFlag<LangOpts<"AppleKext">>;
11501150
def fstrict_flex_arrays_EQ : Joined<["-"], "fstrict-flex-arrays=">, Group<f_Group>,
11511151
MetaVarName<"<n>">, Values<"0,1,2">,
1152-
LangOpts<"StrictFlexArrays">,
1152+
LangOpts<"StrictFlexArraysLevel">,
11531153
Flags<[CC1Option]>,
1154+
NormalizedValuesScope<"LangOptions::StrictFlexArraysLevelKind">,
1155+
NormalizedValues<["Default", "OneZeroOrIncomplete", "ZeroOrIncomplete", "Incomplete"]>,
11541156
HelpText<"Enable optimizations based on the strict definition of flexible arrays">,
1155-
MarshallingInfoInt<LangOpts<"StrictFlexArrays">>;
1157+
MarshallingInfoEnum<LangOpts<"StrictFlexArraysLevel">, "Default">;
11561158
defm apple_pragma_pack : BoolFOption<"apple-pragma-pack",
11571159
LangOpts<"ApplePragmaPack">, DefaultFalse,
11581160
PosFlag<SetTrue, [CC1Option], "Enable Apple gcc-compatible #pragma pack handling">,

clang/lib/AST/Expr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ bool Expr::isKnownToHaveBooleanValue(bool Semantic) const {
204204
}
205205

206206
bool Expr::isFlexibleArrayMemberLike(
207-
ASTContext &Context, unsigned StrictFlexArraysLevel,
207+
ASTContext &Context,
208+
LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel,
208209
bool IgnoreTemplateOrMacroSubstitution) const {
209210

210211
// For compatibility with existing code, we treat arrays of length 0 or
@@ -219,7 +220,8 @@ bool Expr::isFlexibleArrayMemberLike(
219220
// FIXME: While the default -fstrict-flex-arrays=0 permits Size>1 trailing
220221
// arrays to be treated as flexible-array-members, we still emit diagnostics
221222
// as if they are not. Pending further discussion...
222-
if (StrictFlexArraysLevel >= 2 || Size.uge(2))
223+
using FAMKind = LangOptions::StrictFlexArraysLevelKind;
224+
if (StrictFlexArraysLevel == FAMKind::ZeroOrIncomplete || Size.uge(2))
223225
return false;
224226

225227
} else if (!Context.getAsIncompleteArrayType(getType()))

clang/lib/AST/ExprConstant.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11613,15 +11613,17 @@ static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
1161311613
// conservative with the last element in structs (if it's an array), so our
1161411614
// current behavior is more compatible than an explicit list approach would
1161511615
// be.
11616-
int StrictFlexArraysLevel = Ctx.getLangOpts().StrictFlexArrays;
11616+
using FAMKind = LangOptions::StrictFlexArraysLevelKind;
11617+
FAMKind StrictFlexArraysLevel = Ctx.getLangOpts().getStrictFlexArraysLevel();
1161711618
return LVal.InvalidBase &&
1161811619
Designator.Entries.size() == Designator.MostDerivedPathLength &&
1161911620
Designator.MostDerivedIsArrayElement &&
1162011621
(Designator.isMostDerivedAnUnsizedArray() ||
1162111622
Designator.getMostDerivedArraySize() == 0 ||
1162211623
(Designator.getMostDerivedArraySize() == 1 &&
11623-
StrictFlexArraysLevel < 2) ||
11624-
StrictFlexArraysLevel == 0) &&
11624+
StrictFlexArraysLevel != FAMKind::Incomplete &&
11625+
StrictFlexArraysLevel != FAMKind::ZeroOrIncomplete) ||
11626+
StrictFlexArraysLevel == FAMKind::Default) &&
1162511627
isDesignatorAtObjectEnd(Ctx, LVal);
1162611628
}
1162711629

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ llvm::Value *CodeGenFunction::LoadPassedObjectSize(const Expr *E,
919919
static llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF,
920920
const Expr *Base,
921921
QualType &IndexedType,
922-
unsigned StrictFlexArraysLevel) {
922+
LangOptions::StrictFlexArraysLevelKind
923+
StrictFlexArraysLevel) {
923924
// For the vector indexing extension, the bound is the number of elements.
924925
if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
925926
IndexedType = Base->getType();
@@ -958,7 +959,8 @@ void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
958959
"should not be called unless adding bounds checks");
959960
SanitizerScope SanScope(this);
960961

961-
const unsigned StrictFlexArraysLevel = getLangOpts().StrictFlexArrays;
962+
const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
963+
getLangOpts().getStrictFlexArraysLevel();
962964

963965
QualType IndexedType;
964966
llvm::Value *Bound =

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15915,7 +15915,8 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
1591515915
const ConstantArrayType *ArrayTy =
1591615916
Context.getAsConstantArrayType(BaseExpr->getType());
1591715917

15918-
unsigned StrictFlexArraysLevel = getLangOpts().StrictFlexArrays;
15918+
LangOptions::StrictFlexArraysLevelKind
15919+
StrictFlexArraysLevel = getLangOpts().getStrictFlexArraysLevel();
1591915920

1592015921
const Type *BaseType =
1592115922
ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr();

clang/lib/StaticAnalyzer/Core/MemRegion.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,11 @@ DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
794794
if (Size.isZero())
795795
return true;
796796

797-
if (getContext().getLangOpts().StrictFlexArrays >= 2)
797+
using FAMKind = LangOptions::StrictFlexArraysLevelKind;
798+
const FAMKind StrictFlexArraysLevel =
799+
Ctx.getLangOpts().getStrictFlexArraysLevel();
800+
if (StrictFlexArraysLevel == FAMKind::ZeroOrIncomplete ||
801+
StrictFlexArraysLevel == FAMKind::Incomplete)
798802
return false;
799803

800804
const AnalyzerOptions &Opts = SVB.getAnalyzerOptions();

0 commit comments

Comments
 (0)