From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1930) id 1B897385E036; Fri, 4 Jun 2021 16:36:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B897385E036 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Martin Sebor To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-1218] PR c/100719 - missing -Wvla-parameter on a mismatch in second parameter X-Act-Checkin: gcc X-Git-Author: Martin Sebor X-Git-Refname: refs/heads/master X-Git-Oldrev: 1b51f038cf027fdc1bf00240cacee59dd5cbe458 X-Git-Newrev: c6503fa93b5565c922f76611a55b0a53cd940a5f Message-Id: <20210604163653.1B897385E036@sourceware.org> Date: Fri, 4 Jun 2021 16:36:53 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Jun 2021 16:36:53 -0000 https://gcc.gnu.org/g:c6503fa93b5565c922f76611a55b0a53cd940a5f commit r12-1218-gc6503fa93b5565c922f76611a55b0a53cd940a5f Author: Martin Sebor Date: Fri Jun 4 10:35:27 2021 -0600 PR c/100719 - missing -Wvla-parameter on a mismatch in second parameter gcc/ChangeLog: * attribs.c (init_attr_rdwr_indices): Use VLA bounds in the expected order. (attr_access::vla_bounds): Also handle VLA bounds. gcc/c-family/ChangeLog: * c-warn.c (warn_parm_array_mismatch): Check TREE_PURPOSE to test for element presence. gcc/testsuite/ChangeLog: * gcc.dg/Wvla-parameter-10.c: New test. * gcc.dg/Wvla-parameter-11.c: New test. Diff: --- gcc/attribs.c | 21 ++++++---- gcc/c-family/c-warn.c | 2 +- gcc/testsuite/gcc.dg/Wvla-parameter-10.c | 68 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wvla-parameter-11.c | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 8 deletions(-) diff --git a/gcc/attribs.c b/gcc/attribs.c index ebc0783c439..70e0a2f188f 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -2126,14 +2126,14 @@ init_attr_rdwr_indices (rdwr_map *rwm, tree attrs) /* The (optional) list of VLA bounds. */ tree vblist = TREE_CHAIN (mode); - if (vblist) - vblist = TREE_VALUE (vblist); - mode = TREE_VALUE (mode); if (TREE_CODE (mode) != STRING_CST) continue; gcc_assert (TREE_CODE (mode) == STRING_CST); + if (vblist) + vblist = nreverse (copy_list (TREE_VALUE (vblist))); + for (const char *m = TREE_STRING_POINTER (mode); *m; ) { attr_access acc = { }; @@ -2308,11 +2308,18 @@ attr_access::to_external_string () const unsigned attr_access::vla_bounds (unsigned *nunspec) const { + unsigned nbounds = 0; *nunspec = 0; - for (const char* p = strrchr (str, ']'); p && *p != '['; --p) - if (*p == '*') - ++*nunspec; - return list_length (size); + /* STR points to the beginning of the specified string for the current + argument that may be followed by the string for the next argument. */ + for (const char* p = strchr (str, ']'); p && *p != '['; --p) + { + if (*p == '*') + ++*nunspec; + else if (*p == '$') + ++nbounds; + } + return nbounds; } /* Reset front end-specific attribute access data from ATTRS. diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index c48dc2e6d24..a587b993fde 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -3511,7 +3511,7 @@ warn_parm_array_mismatch (location_t origloc, tree fndecl, tree newparms) && newa->sizarg != UINT_MAX && newa->sizarg == cura->sizarg && newa->minsize == cura->minsize - && !TREE_CHAIN (newa->size) && !TREE_CHAIN (cura->size)) + && !TREE_PURPOSE (newa->size) && !TREE_PURPOSE (cura->size)) continue; if (newa->size || cura->size) diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-10.c b/gcc/testsuite/gcc.dg/Wvla-parameter-10.c new file mode 100644 index 00000000000..68db3edfbc5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wvla-parameter-10.c @@ -0,0 +1,68 @@ +/* PR c/100719 - missing -Wvla-parameter on a mismatch in second parameter + { dg-do compile } + { dg-options "-Wall" } */ + +typedef struct A1 { int i; } A1; +typedef struct A2 { int i; } A2; +typedef struct A3 { int i; } A3; + +void f2 (int n, A1[n], A2[n]); +void f2 (int n, A1[n], A2[n]); + +void f2_x1 (int n, A1[n], A2[n]); // { dg-note "previously declared as 'A1\\\[n]' with bound argument 1" } +void f2_x1 (int n, A1[n + 1], A2[n]); // { dg-warning "argument 2 of type 'A1\\\[n \\+ 1]' declared with mismatched bound 'n \\+ 1'" } + +void f2_x2 (int n, A1[n], A2[n]); // { dg-note "previously declared as 'A2\\\[n]' with bound argument 1" } +void f2_x2 (int n, A1[n], A2[n + 2]); // { dg-warning "argument 3 of type 'A2\\\[n \\+ 2]' declared with mismatched bound 'n \\+ 2'" } + + +void f3 (int n, A1[n], A2[n], A3[n]); +void f3 (int n, A1[n], A2[n], A3[n]); + +void f3_x1 (int n, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void f3_x1 (int n, A1[n + 1], A2[n], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n \\+ 1]' declared with mismatched bound 'n \\+ 1'" "" { target *-*-* } .-1 } + +void f3_x2 (int n, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void f3_x2 (int n, A1[n], A2[n + 2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n \\+ 2]' declared with mismatched bound 'n \\+ 2'" "" { target *-*-* } .-1 } + +void f3_x3 (int n, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void f3_x3 (int n, A1[n], A2[n], A3[n + 3]); +// { dg-warning "argument 4 of type 'A3\\\[n \\+ 3]' declared with mismatched bound 'n \\+ 3'" "" { target *-*-* } .-1 } + + +void g3_x1 (int n, A1[n], A2[*], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void g3_x1 (int n, A1[n + 1], A2[*], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n \\+ 1]' declared with mismatched bound 'n \\+ 1'" "" { target *-*-* } .-1 } + +void g3_x2 (int n, A1[*], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void g3_x2 (int n, A1[*], A2[n + 2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n \\+ 2]' declared with mismatched bound 'n \\+ 2'" "" { target *-*-* } .-1 } + +void g3_x3 (int n, A1[*], A2[*], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void g3_x3 (int n, A1[*], A2[*], A3[n + 3]); +// { dg-warning "argument 4 of type 'A3\\\[n \\+ 3]' declared with mismatched bound 'n \\+ 3'" "" { target *-*-* } .-1 } + + +void h3_x1 (int n, A1[n], A2[ ], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void h3_x1 (int n, A1[n + 1], A2[ ], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n \\+ 1]' declared with mismatched bound 'n \\+ 1'" "" { target *-*-* } .-1 } + +void h3_x2 (int n, A1[ ], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void h3_x2 (int n, A1[ ], A2[n + 2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n \\+ 2]' declared with mismatched bound 'n \\+ 2'" "" { target *-*-* } .-1 } + +void h3_x3 (int n, A1[ ], A2[ ], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]' with bound argument 1" "note" { target *-*-* } .-1 } +void h3_x3 (int n, A1[ ], A2[ ], A3[n + 3]); +// { dg-warning "argument 4 of type 'A3\\\[n \\+ 3]' declared with mismatched bound 'n \\+ 3'" "" { target *-*-* } .-1 } + diff --git a/gcc/testsuite/gcc.dg/Wvla-parameter-11.c b/gcc/testsuite/gcc.dg/Wvla-parameter-11.c new file mode 100644 index 00000000000..39886a22d68 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wvla-parameter-11.c @@ -0,0 +1,70 @@ +/* PR c/100719 - missing -Wvla-parameter on a mismatch in second parameter + { dg-do compile } + { dg-options "-Wall" } */ + +typedef struct A1 { int i; } A1; +typedef struct A2 { int i; } A2; +typedef struct A3 { int i; } A3; + +extern int n, n1, n2, n3; + +void f2 (int, A1[n], A2[n]); +void f2 (int, A1[n], A2[n]); + +void f2_x1 (int, A1[n], A2[n]); // { dg-note "previously declared as 'A1\\\[n]'" } +void f2_x1 (int, A1[n1], A2[n]); // { dg-warning "argument 2 of type 'A1\\\[n1]' declared with mismatched bound 'n1'" } + +void f2_x2 (int, A1[n], A2[n]); // { dg-note "previously declared as 'A2\\\[n]'" } +void f2_x2 (int, A1[n], A2[n2]); // { dg-warning "argument 3 of type 'A2\\\[n2]' declared with mismatched bound 'n2'" } + + +void f3 (int, A1[n], A2[n], A3[n]); +void f3 (int, A1[n], A2[n], A3[n]); + +void f3_x1 (int, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]'" "note" { target *-*-* } .-1 } +void f3_x1 (int, A1[n1], A2[n], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n1]' declared with mismatched bound 'n1'" "" { target *-*-* } .-1 } + +void f3_x2 (int, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]'" "note" { target *-*-* } .-1 } +void f3_x2 (int, A1[n], A2[n2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n2]' declared with mismatched bound 'n2'" "" { target *-*-* } .-1 } + +void f3_x3 (int, A1[n], A2[n], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]'" "note" { target *-*-* } .-1 } +void f3_x3 (int, A1[n], A2[n], A3[n3]); +// { dg-warning "argument 4 of type 'A3\\\[n3]' declared with mismatched bound 'n3'" "" { target *-*-* } .-1 } + + +void g3_x1 (int, A1[n], A2[*], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]'" "note" { target *-*-* } .-1 } +void g3_x1 (int, A1[n1], A2[*], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n1]' declared with mismatched bound 'n1'" "" { target *-*-* } .-1 } + +void g3_x2 (int, A1[*], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]'" "note" { target *-*-* } .-1 } +void g3_x2 (int, A1[*], A2[n2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n2]' declared with mismatched bound 'n2'" "" { target *-*-* } .-1 } + +void g3_x3 (int, A1[*], A2[*], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]'" "note" { target *-*-* } .-1 } +void g3_x3 (int, A1[*], A2[*], A3[n3]); +// { dg-warning "argument 4 of type 'A3\\\[n3]' declared with mismatched bound 'n3'" "" { target *-*-* } .-1 } + + +void h3_x1 (int, A1[n], A2[ ], A3[n]); +// { dg-note "previously declared as 'A1\\\[n]'" "note" { target *-*-* } .-1 } +void h3_x1 (int, A1[n1], A2[ ], A3[n]); +// { dg-warning "argument 2 of type 'A1\\\[n1]' declared with mismatched bound 'n1'" "" { target *-*-* } .-1 } + +void h3_x2 (int, A1[ ], A2[n], A3[n]); +// { dg-note "previously declared as 'A2\\\[n]'" "note" { target *-*-* } .-1 } +void h3_x2 (int, A1[ ], A2[n2], A3[n]); +// { dg-warning "argument 3 of type 'A2\\\[n2]' declared with mismatched bound 'n2'" "" { target *-*-* } .-1 } + +void h3_x3 (int, A1[ ], A2[ ], A3[n]); +// { dg-note "previously declared as 'A3\\\[n]'" "note" { target *-*-* } .-1 } +void h3_x3 (int, A1[ ], A2[ ], A3[n3]); +// { dg-warning "argument 4 of type 'A3\\\[n3]' declared with mismatched bound 'n3'" "" { target *-*-* } .-1 } +