Skip to content

Commit c8ddf27

Browse files
committed
Revert "[Sema] Address-space sensitive index check for unbounded arrays"
This reverts commit da55e9b. Build bots uncovered coverage gap in testing. Change not ready.
1 parent cad961b commit c8ddf27

File tree

5 files changed

+17
-157
lines changed

5 files changed

+17
-157
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8847,14 +8847,6 @@ def warn_array_index_precedes_bounds : Warning<
88478847
def warn_array_index_exceeds_bounds : Warning<
88488848
"array index %0 is past the end of the array (which contains %1 "
88498849
"element%s2)">, InGroup<ArrayBounds>;
8850-
def warn_ptr_arith_exceeds_max_addressable_bounds : Warning<
8851-
"the pointer incremented by %0 refers past the last possible element for an array in %1-bit "
8852-
"address space containing %2-bit (%3-byte) elements (max possible %4 element%s5)">,
8853-
InGroup<ArrayBounds>;
8854-
def warn_array_index_exceeds_max_addressable_bounds : Warning<
8855-
"array index %0 refers past the last possible element for an array in %1-bit "
8856-
"address space containing %2-bit (%3-byte) elements (max possible %4 element%s5)">,
8857-
InGroup<ArrayBounds>;
88588850
def note_array_declared_here : Note<
88598851
"array %0 declared here">;
88608852

clang/lib/Sema/SemaChecking.cpp

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14038,11 +14038,11 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
1403814038
const ConstantArrayType *ArrayTy =
1403914039
Context.getAsConstantArrayType(BaseExpr->getType());
1404014040

14041-
const Type *BaseType =
14042-
ArrayTy == nullptr ? nullptr : ArrayTy->getElementType().getTypePtr();
14043-
bool IsUnboundedArray = (BaseType == nullptr);
14044-
if (EffectiveType->isDependentType() ||
14045-
(!IsUnboundedArray && BaseType->isDependentType()))
14041+
if (!ArrayTy)
14042+
return;
14043+
14044+
const Type *BaseType = ArrayTy->getElementType().getTypePtr();
14045+
if (EffectiveType->isDependentType() || BaseType->isDependentType())
1404614046
return;
1404714047

1404814048
Expr::EvalResult Result;
@@ -14059,69 +14059,6 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
1405914059
if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
1406014060
ND = ME->getMemberDecl();
1406114061

14062-
if (IsUnboundedArray) {
14063-
if (index.isUnsigned() || !index.isNegative()) {
14064-
const auto &ASTC = getASTContext();
14065-
unsigned AddrBits =
14066-
ASTC.getTargetInfo().getPointerWidth(ASTC.getTargetAddressSpace(
14067-
EffectiveType->getCanonicalTypeInternal()));
14068-
if (index.getBitWidth() < AddrBits)
14069-
index = index.zext(AddrBits);
14070-
CharUnits ElemCharUnits = ASTC.getTypeSizeInChars(EffectiveType);
14071-
llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits.getQuantity());
14072-
// If index has more active bits than address space, we already know
14073-
// we have a bounds violation to warn about. Otherwise, compute
14074-
// address of (index + 1)th element, and warn about bounds violation
14075-
// only if that address exceeds address space.
14076-
if (index.getActiveBits() <= AddrBits) {
14077-
bool Overflow;
14078-
llvm::APInt Product(index);
14079-
Product += 1;
14080-
Product = Product.umul_ov(ElemBytes, Overflow);
14081-
if (!Overflow && Product.getActiveBits() <= AddrBits)
14082-
return;
14083-
}
14084-
14085-
// Need to compute max possible elements in address space, since that
14086-
// is included in diag message.
14087-
llvm::APInt MaxElems = llvm::APInt::getMaxValue(AddrBits);
14088-
MaxElems = MaxElems.zext(std::max(AddrBits + 1, ElemBytes.getBitWidth()));
14089-
MaxElems += 1;
14090-
ElemBytes = ElemBytes.zextOrTrunc(MaxElems.getBitWidth());
14091-
MaxElems = MaxElems.udiv(ElemBytes);
14092-
14093-
unsigned DiagID =
14094-
ASE ? diag::warn_array_index_exceeds_max_addressable_bounds
14095-
: diag::warn_ptr_arith_exceeds_max_addressable_bounds;
14096-
14097-
// Diag message shows element size in bits and in "bytes" (platform-
14098-
// dependent CharUnits)
14099-
DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
14100-
PDiag(DiagID)
14101-
<< index.toString(10, true) << AddrBits
14102-
<< (unsigned)ASTC.toBits(ElemCharUnits)
14103-
<< ElemBytes.toString(10, false)
14104-
<< MaxElems.toString(10, false)
14105-
<< (unsigned)MaxElems.getLimitedValue(~0U)
14106-
<< IndexExpr->getSourceRange());
14107-
14108-
if (!ND) {
14109-
// Try harder to find a NamedDecl to point at in the note.
14110-
while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr))
14111-
BaseExpr = ASE->getBase()->IgnoreParenCasts();
14112-
if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
14113-
ND = DRE->getDecl();
14114-
if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr))
14115-
ND = ME->getMemberDecl();
14116-
}
14117-
14118-
if (ND)
14119-
DiagRuntimeBehavior(ND->getBeginLoc(), BaseExpr,
14120-
PDiag(diag::note_array_declared_here) << ND);
14121-
}
14122-
return;
14123-
}
14124-
1412514062
if (index.isUnsigned() || !index.isNegative()) {
1412614063
// It is possible that the type of the base expression after
1412714064
// IgnoreParenCasts is incomplete, even though the type of the base
@@ -14184,8 +14121,9 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
1418414121
}
1418514122
}
1418614123

14187-
unsigned DiagID = ASE ? diag::warn_array_index_exceeds_bounds
14188-
: diag::warn_ptr_arith_exceeds_bounds;
14124+
unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
14125+
if (ASE)
14126+
DiagID = diag::warn_array_index_exceeds_bounds;
1418914127

1419014128
DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
1419114129
PDiag(DiagID) << index.toString(10, true)
@@ -14206,11 +14144,12 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
1420614144

1420714145
if (!ND) {
1420814146
// Try harder to find a NamedDecl to point at in the note.
14209-
while (const auto *ASE = dyn_cast<ArraySubscriptExpr>(BaseExpr))
14147+
while (const ArraySubscriptExpr *ASE =
14148+
dyn_cast<ArraySubscriptExpr>(BaseExpr))
1421014149
BaseExpr = ASE->getBase()->IgnoreParenCasts();
14211-
if (const auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
14150+
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
1421214151
ND = DRE->getDecl();
14213-
if (const auto *ME = dyn_cast<MemberExpr>(BaseExpr))
14152+
if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
1421414153
ND = ME->getMemberDecl();
1421514154
}
1421614155

clang/test/Sema/const-eval.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{must have a con
140140

141141
// We evaluate these by providing 2s' complement semantics in constant
142142
// expressions, like we do for integers.
143-
void *PR28739a = (__int128)(unsigned long)-1 + &PR28739a; // expected-warning {{the pointer incremented by 18446744073709551615 refers past the last possible element for an array in 64-bit address space containing 64-bit (8-byte) elements (max possible 2305843009213693952 elements)}}
144-
void *PR28739b = &PR28739b + (__int128)(unsigned long)-1; // expected-warning {{refers past the last possible element}}
145-
__int128 PR28739c = (&PR28739c + (__int128)(unsigned long)-1) - &PR28739c; // expected-warning {{refers past the last possible element}}
146-
void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // expected-warning {{refers past the last possible element}}
143+
void *PR28739a = (__int128)(unsigned long)-1 + &PR28739a;
144+
void *PR28739b = &PR28739b + (__int128)(unsigned long)-1;
145+
__int128 PR28739c = (&PR28739c + (__int128)(unsigned long)-1) - &PR28739c;
146+
void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1];
147147

148148
struct PR35214_X {
149149
int k;

clang/test/Sema/unbounded-array-bounds.c

Lines changed: 0 additions & 70 deletions
This file was deleted.

clang/test/SemaCXX/constant-expression-cxx1y.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,9 +1018,8 @@ constexpr int S = sum(Cs); // expected-error{{must be initialized by a constant
10181018
}
10191019

10201020
constexpr void PR28739(int n) { // expected-error {{never produces a constant}}
1021-
int *p = &n; // expected-note {{declared here}}
1021+
int *p = &n;
10221022
p += (__int128)(unsigned long)-1; // expected-note {{cannot refer to element 18446744073709551615 of non-array object in a constant expression}}
1023-
// expected-warning@-1 {{the pointer incremented by 18446744073709551615 refers past the last possible element for an array in 64-bit address space containing 32-bit (4-byte) elements (max possible 4611686018427387904 elements)}}
10241023
}
10251024

10261025
constexpr void Void(int n) {

0 commit comments

Comments
 (0)