aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorserge-sans-paille <sguelton@redhat.com>2022-09-01 15:15:29 +0200
committerserge-sans-paille <sguelton@redhat.com>2022-09-22 14:04:35 +0200
commitdad36245a5c2e3779b4eede6fcbbddad19a2f01d (patch)
treed3229c82eb9fbcf7f604f2c99e0c5c3b8e7a970a /clang/lib/Sema/SemaChecking.cpp
parent2e9118f1e420a3ec4fd525374753121f2e215a23 (diff)
downloadllvm-dad36245a5c2e3779b4eede6fcbbddad19a2f01d.zip
llvm-dad36245a5c2e3779b4eede6fcbbddad19a2f01d.tar.gz
llvm-dad36245a5c2e3779b4eede6fcbbddad19a2f01d.tar.bz2
[clang] Rework IsTailPaddedMemberArray into isFlexibleArrayMemberExpr
This fixes a bunch of FIXME within IsTailPaddedMemberArray related code. As a side effect, this now also triggers a warning when trying to access a "struct hack" member with an index above address space index range. Differential Revision: https://reviews.llvm.org/D133108
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp51
1 files changed, 25 insertions, 26 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 1b74b09..90eca75 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -15898,17 +15898,24 @@ void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
<< TRange << Op->getSourceRange();
}
-/// Check whether this array fits the idiom of a size-one tail padded
-/// array member of a struct.
+/// Check whether this array fits the idiom of a flexible array member,
+/// depending on the value of -fstrict-flex-array.
///
-/// We avoid emitting out-of-bounds access warnings for such arrays as they are
-/// commonly used to emulate flexible arrays in C89 code.
-static bool IsTailPaddedMemberArray(Sema &S, const llvm::APInt &Size,
- const NamedDecl *ND,
- unsigned StrictFlexArraysLevel) {
+/// We avoid emitting out-of-bounds access warnings for such arrays.
+static bool isFlexibleArrayMemberExpr(Sema &S, const Expr *E,
+ const NamedDecl *ND,
+ unsigned StrictFlexArraysLevel) {
+
if (!ND)
return false;
+ const ConstantArrayType *ArrayTy =
+ S.Context.getAsConstantArrayType(E->getType());
+ llvm::APInt Size = ArrayTy->getSize();
+
+ if (Size == 0)
+ return true;
+
// FIXME: While the default -fstrict-flex-arrays=0 permits Size>1 trailing
// arrays to be treated as flexible-array-members, we still emit diagnostics
// as if they are not. Pending further discussion...
@@ -15974,9 +15981,19 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
const ConstantArrayType *ArrayTy =
Context.getAsConstantArrayType(BaseExpr->getType());
+ unsigned StrictFlexArraysLevel = getLangOpts().StrictFlexArrays;
+
+ const NamedDecl *ND = nullptr;
+ if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
+ ND = DRE->getDecl();
+ else if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr))
+ ND = ME->getMemberDecl();
+
const Type *BaseType =
ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr();
- bool IsUnboundedArray = (BaseType == nullptr);
+ bool IsUnboundedArray =
+ BaseType == nullptr ||
+ isFlexibleArrayMemberExpr(*this, BaseExpr, ND, StrictFlexArraysLevel);
if (EffectiveType->isDependentType() ||
(!IsUnboundedArray && BaseType->isDependentType()))
return;
@@ -15991,12 +16008,6 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
index = -index;
}
- const NamedDecl *ND = nullptr;
- if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
- ND = DRE->getDecl();
- if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
- ND = ME->getMemberDecl();
-
if (IsUnboundedArray) {
if (EffectiveType->isFunctionType())
return;
@@ -16074,17 +16085,10 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
// example). In this case we have no information about whether the array
// access exceeds the array bounds. However we can still diagnose an array
// access which precedes the array bounds.
- //
- // FIXME: this check should be redundant with the IsUnboundedArray check
- // above.
if (BaseType->isIncompleteType())
return;
- // FIXME: this check should be used to set IsUnboundedArray from the
- // beginning.
llvm::APInt size = ArrayTy->getSize();
- if (!size.isStrictlyPositive())
- return;
if (BaseType != EffectiveType) {
// Make sure we're comparing apples to apples when comparing index to size
@@ -16114,11 +16118,6 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
return;
- // Also don't warn for Flexible Array Member emulation.
- const unsigned StrictFlexArraysLevel = getLangOpts().StrictFlexArrays;
- if (IsTailPaddedMemberArray(*this, size, ND, StrictFlexArraysLevel))
- return;
-
// Suppress the warning if the subscript expression (as identified by the
// ']' location) and the index expression are both from macro expansions
// within a system header.