[PATCH] D45865: [Sema] Emit -Warray-bounds for multiple levels of subscript expressions.
Bevin Hansson via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 20 01:21:13 PDT 2018
ebevhan created this revision.
ebevhan added reviewers: danielmarjamaki, aaron.ballman.
Herald added a subscriber: cfe-commits.
This patch has CheckArrayBounds recurse into
ArraySubscriptExprs and MemberExprs, giving
warnings for invalid indices for every level of
subscript instead of just the topmost one.
Repository:
rC Clang
https://reviews.llvm.org/D45865
Files:
lib/Sema/SemaChecking.cpp
test/SemaCXX/array-bounds.cpp
test/SemaCXX/constant-expression-cxx11.cpp
Index: test/SemaCXX/constant-expression-cxx11.cpp
===================================================================
--- test/SemaCXX/constant-expression-cxx11.cpp
+++ test/SemaCXX/constant-expression-cxx11.cpp
@@ -528,15 +528,18 @@
constexpr int xs0 = p[-3]; // ok
constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
-constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; // expected-note {{array 'zs' declared here}}
static_assert(zs[0][0][0][0] == 1, "");
static_assert(zs[1][1][1][1] == 16, "");
static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
static_assert((&zs[0][0][0][2])[-1] == 2, "");
static_assert(**(**(zs + 1) + 1) == 11, "");
static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
-constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
+constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // \
+expected-error {{constant expression}} \
+expected-note {{cannot access array element of pointer past the end}} \
+expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
constexpr int fail(const int &p) {
return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
Index: test/SemaCXX/array-bounds.cpp
===================================================================
--- test/SemaCXX/array-bounds.cpp
+++ test/SemaCXX/array-bounds.cpp
@@ -269,3 +269,16 @@
struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
return x[1] + x[11]; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
}
+
+int multi[2][2][2]; // expected-note {{array 'multi' declared here}}
+int test_multiarray() {
+ return multi[2][0][0]; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
+}
+
+struct multi_s {
+ int arr[4];
+};
+struct multi_s multi2[4]; // expected-note {{array 'multi2' declared here}}
+int test_struct_multiarray() {
+ return multi2[4].arr[0]; // expected-warning {{array index 4 is past the end of the array (which contains 4 elements)}}
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -11277,7 +11277,13 @@
const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
AllowOnePastEnd > 0);
- return;
+ expr = ASE->getBase();
+ break;
+ }
+ case Stmt::MemberExprClass: {
+ const MemberExpr *ME = cast<MemberExpr>(expr);
+ expr = ME->getBase();
+ break;
}
case Stmt::OMPArraySectionExprClass: {
const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45865.143250.patch
Type: text/x-patch
Size: 3378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180420/20c81ca5/attachment.bin>
More information about the cfe-commits
mailing list