Skip to content

Commit

Permalink
invoke.texi: Document -Wdiscarded-array-qualifiers.
Browse files Browse the repository at this point in the history
2014-12-20  Martin Uecker <uecker@eecs.berkeley.edu>

	* doc/invoke.texi: Document -Wdiscarded-array-qualifiers.
	* doc/extend.texi: Document new behavior for pointers to arrays
	with qualifiers.

c/
	* c-typeck.c: New behavious for pointers to arrays with qualifiers
        (common-pointer-type): For pointers to arrays take qualifiers from
	element type.
        (build_conditional_expr): Add warnings for lost qualifiers.
	(comp-target-types): Allow pointers to arrays with different qualifiers.
        (convert-for-assignment): Adapt warnings for discarded qualifiers. Add
	WARNING_FOR_QUALIFIERS macro and rename WARN_FOR_QUALIFIERS
        to PEDWARN_FOR_QUALIFIERS.

c-family/
	* c.opt (Wdiscarded-array-qualifiers): New option.

testsuite/
	* gcc.dg/Wwrite-strings-1.c: Change dg-warning.
	* gcc.dg/array-quals-1.c: Use -Wno-discarded-array-qualifiers.
	* gcc.dg/array-quals-2.c: Change dg-options, dg-warning.
	* gcc.dg/pointer-array-atomic.c: New test.
	* gcc.dg/pointer-array-quals-1.c: New test.
	* gcc.dg/pointer-array-quals-2.c: New test (-pedantic-errors).
	* gcc.dg/qual-component-1.c: Change dg-options, dg-warnings.

From-SVN: r218985
  • Loading branch information
uecker authored and jsm28 committed Dec 20, 2014
1 parent 189486b commit 768952b
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 148 deletions.
6 changes: 6 additions & 0 deletions gcc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>

* doc/invoke.texi: Document -Wdiscarded-array-qualifiers.
* doc/extend.texi: Document new behavior for pointers to arrays
with qualifiers.

2014-12-19 Jan Hubicka <hubicka@ucw.cz>

* hash-table.h (struct pointer_hash): Fix formating.
Expand Down
4 changes: 4 additions & 0 deletions gcc/c-family/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>

* c.opt (Wdiscarded-array-qualifiers): New option.

2014-12-19 Jakub Jelinek <jakub@redhat.com>

PR preprocessor/63831
Expand Down
4 changes: 4 additions & 0 deletions gcc/c-family/c.opt
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ Wdesignated-init
C ObjC Var(warn_designated_init) Init(1) Warning
Warn about positional initialization of structs requiring designated initializers

Wdiscarded-array-qualifiers
C ObjC Var(warn_discarded_array_qualifiers) Init(1) Warning
Warn if qualifiers on arrays which are pointer targets are discarded

Wdiscarded-qualifiers
C ObjC Var(warn_discarded_qualifiers) Init(1) Warning
Warn if type qualifiers on pointers are discarded
Expand Down
11 changes: 11 additions & 0 deletions gcc/c/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>

* c-typeck.c: New behavious for pointers to arrays with qualifiers
(common-pointer-type): For pointers to arrays take qualifiers from
element type.
(build_conditional_expr): Add warnings for lost qualifiers.
(comp-target-types): Allow pointers to arrays with different qualifiers.
(convert-for-assignment): Adapt warnings for discarded qualifiers. Add
WARNING_FOR_QUALIFIERS macro and rename WARN_FOR_QUALIFIERS
to PEDWARN_FOR_QUALIFIERS.

2014-12-17 Jakub Jelinek <jakub@redhat.com>

PR sanitizer/64289
Expand Down
328 changes: 206 additions & 122 deletions gcc/c/c-typeck.c

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions gcc/doc/extend.texi
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extensions, accepted by GCC in C90 mode and in C++.
* Escaped Newlines:: Slightly looser rules for escaped newlines.
* Subscripting:: Any array can be subscripted, even if not an lvalue.
* Pointer Arith:: Arithmetic on @code{void}-pointers and function pointers.
* Pointers to Arrays:: Pointers to arrays with qualifiers work as expected.
* Initializers:: Non-constant initializers.
* Compound Literals:: Compound literals give structures, unions
or arrays as values.
Expand Down Expand Up @@ -1784,6 +1785,27 @@ and on function types, and returns 1.
The option @option{-Wpointer-arith} requests a warning if these extensions
are used.

@node Pointers to Arrays
@section Pointers to arrays with qualifiers work as expected
@cindex pointers to arrays
@cindex const qualifier

In GNU C, pointers to arrays with qualifiers work similar to pointers
to other qualified types. For example, a value of type @code{int (*)[5]}
can be used to initialize a variable of type @code{const int (*)[5]}.
These types are incompatible in ISO C because the @code{const} qualifier
is formally attached to the element type of the array and not the
array itself.

@smallexample
extern void
transpose (int N, int M, double out[M][N], const double in[N][M]);
double x[3][2];
double y[2][3];
@r{@dots{}}
transpose(3, 2, y, x);
@end smallexample

@node Initializers
@section Non-Constant Initializers
@cindex initializers, non-constant
Expand Down
14 changes: 12 additions & 2 deletions gcc/doc/invoke.texi
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ Objective-C and Objective-C++ Dialects}.
-Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol
-Wconversion -Wcoverage-mismatch -Wdate-time -Wdelete-incomplete -Wno-cpp @gol
-Wno-deprecated -Wno-deprecated-declarations -Wno-designated-init @gol
-Wdisabled-optimization -Wno-discarded-qualifiers @gol
-Wdisabled-optimization @gol
-Wno-discarded-qualifiers -Wno-discarded-array-qualifiers @gol
-Wno-div-by-zero -Wdouble-promotion -Wempty-body -Wenum-compare @gol
-Wno-endif-labels -Werror -Werror=* @gol
-Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol
Expand Down Expand Up @@ -4300,9 +4301,18 @@ This warning is enabled by @option{-Wall}.
@opindex Wdiscarded-qualifiers
Do not warn if type qualifiers on pointers are being discarded.
Typically, the compiler will warn if a @code{const char *} variable is
passed to a function that takes @code{char *} parameter. This option
passed to a function that takes a @code{char *} parameter. This option
can be used to suppress such a warning.

@item -Wno-discarded-array-qualifiers @r{(C and Objective-C only)}
@opindex Wno-discarded-array-qualifiers
@opindex Wdiscarded-array-qualifiers
Do not warn if type qualifiers on arrays which are pointer targets
are being discarded. Typically, the compiler will warn if a
@code{const int (*)[]} variable is passed to a function that
takes a @code{int (*)[]} parameter. This option can be used to
suppress such a warning.

@item -Wno-incompatible-pointer-types @r{(C and Objective-C only)}
@opindex Wno-incompatible-pointer-types
@opindex Wincompatible-pointer-types
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>

* gcc.dg/Wwrite-strings-1.c: Change dg-warning.
* gcc.dg/array-quals-1.c: Use -Wno-discarded-array-qualifiers.
* gcc.dg/array-quals-2.c: Change dg-options, dg-warning.
* gcc.dg/pointer-array-atomic.c: New test.
* gcc.dg/pointer-array-quals-1.c: New test.
* gcc.dg/pointer-array-quals-2.c: New test (-pedantic-errors).
* gcc.dg/qual-component-1.c: Change dg-options, dg-warnings.

2014-12-19 David Malcolm <dmalcolm@redhat.com>

* jit.dg/test-expressions.c (make_tests_of_casts): Add tests of
Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/gcc.dg/Wwrite-strings-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/* { dg-do compile } */
/* { dg-options "-Wwrite-strings" } */
typedef char T[1];
T *p = &""; /* { dg-warning "initialization from incompatible pointer type" } */
T *p = &""; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
1 change: 1 addition & 0 deletions gcc/testsuite/gcc.dg/array-quals-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
all should end up in a read-only section. PR c/12165. */
/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
/* { dg-do compile } */
/* { dg-options "-Wno-discarded-array-qualifiers" } */
/* The MMIX port always switches to the .data section at the end of a file. */
/* { dg-final { scan-assembler-not "\\.data(?!\\.rel\\.ro)" { xfail powerpc*-*-aix* mmix-*-* x86_64-*-mingw* } } } */
static const int a[2] = { 1, 2 };
Expand Down
4 changes: 2 additions & 2 deletions gcc/testsuite/gcc.dg/array-quals-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
lost in forming composite types. */
/* Origin: Joseph Myers <joseph@codesourcery.com> */
/* { dg-do compile } */
/* { dg-options "" } */
/* { dg-options "-pedantic -Wno-discarded-array-qualifiers" } */
typedef const char T[1];
typedef const char T2[1];
typedef volatile char U[1];
T *p;
T2 *p2;
U *q;
void *f(void) { return 1 ? p : q; } /* { dg-warning "pointer type mismatch in conditional expression" } */
void *f(void) { return 1 ? p : q; } /* { dg-warning "pointers to arrays with different qualifiers" } */
T *g(void) { return 1 ? p : p2; }
60 changes: 60 additions & 0 deletions gcc/testsuite/gcc.dg/pointer-array-atomic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* { dg-do compile } */
/* { dg-options "-std=c11" } */
/* Origin: Martin Uecker <uecker@eecs.berkeley.edu> */
void tvoid(void* x);
void transpose0(double* out, _Atomic double* in) { }
void transpose1(double out[2][2], _Atomic double in[2][2]) { }
void transpose2(double out[2][2][2], _Atomic double in[2][2][2]) { }
// return
int (*x2(_Atomic int x[3][3]))[3] { return x; } /* { dg-warning "return from incompatible pointer type" } */
_Atomic int (*x3(int x[3][3]))[3] { return x; } /* { dg-warning "return from incompatible pointer type" } */
void test(void)
{
double x0[2];
double y0[2];
_Atomic double z0[4];
double x1[2][2];
double y1[2][2];
double o1[2][3];
_Atomic double z1[2][2];
double x2[2][2][2];
double y2[2][2][2];
double o2[2][2][3];
_Atomic double z2[2][2][2];
tvoid(z0);
tvoid(z1);
tvoid(z2);
// passing as arguments
transpose0(y0, x0); /* { dg-warning "passing argument 2 of 'transpose0' from incompatible pointer type" } */
transpose1(y1, o1); /* { dg-warning "passing argument 2 of 'transpose1' from incompatible pointer type" } */
transpose1(y1, x1); /* { dg-warning "passing argument 2 of 'transpose1' from incompatible pointer type" } */
transpose2(y2, o2); /* { dg-warning "passing argument 2 of 'transpose2' from incompatible pointer type" } */
transpose2(y2, x2); /* { dg-warning "passing argument 2 of 'transpose2' from incompatible pointer type" } */
// initialization
_Atomic double (*x0p) = x0; /* { dg-warning "initialization from incompatible pointer type" } */
_Atomic double (*x1p)[2] = x1; /* { dg-warning "initialization from incompatible pointer type" } */
_Atomic double (*x2p)[2][2] = x2; /* { dg-warning "initialization from incompatible pointer type" } */
// assignment
x0p = x0; /* { dg-warning "assignment from incompatible pointer type" } */
x1p = x1; /* { dg-warning "assignment from incompatible pointer type" } */
x2p = x2; /* { dg-warning "assignment from incompatible pointer type" } */
// subtraction
&(x0[1]) - &(z0[0]); /* { dg-error "invalid operands to binary" } */
&(x1[1]) - &(z1[0]); /* { dg-error "invalid operands to binary" } */
&(x2[1]) - &(z2[0]); /* { dg-error "invalid operands to binary" } */
// comparison
x0 == z0; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x1 == z1; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x2 == z2; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x0 > z0; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x1 > z1; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x2 > z2; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x0 < z0; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x1 < z1; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
x2 < z2; /* { dg-warning "comparison of distinct pointer types lacks a cast" } */
// conditional expressions
(void)(1 ? x0 : z0); /* { dg-warning "pointer type mismatch in conditional expression" } */
(void)(1 ? x1 : z1); /* { dg-warning "pointer type mismatch in conditional expression" } */
(void)(1 ? x2 : z2); /* { dg-warning "pointer type mismatch in conditional expression" } */
}

106 changes: 106 additions & 0 deletions gcc/testsuite/gcc.dg/pointer-array-quals-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* { dg-do compile } */
/* Origin: Martin Uecker <uecker@eecs.berkeley.edu> */
/* { dg-options "-Wdiscarded-array-qualifiers" } */
void tvoid(void* x);
void transpose0(double* out, const double* in) { }
void transpose1(double out[2][2], const double in[2][2]) { }
void transpose2(double out[2][2][2], const double in[2][2][2]) { }
// return
int (*y2(const int x[3][3]))[3] { return x; } /* { dg-warning "return discards 'const' qualifier from pointer target type" } */
const int (*y3(int x[3][3]))[3] { return x; }
void test(void)
{
double x0[2];
double y0[2];
const double z0[4];
double x1[2][2];
double y1[2][2];
double o1[2][3];
const double z1[2][2];
double x2[2][2][2];
double y2[2][2][2];
double o2[2][2][3];
const double z2[2][2][2];
// void pointers
tvoid(x0);
tvoid(x1);
tvoid(x2);
tvoid(z0); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
tvoid(z1); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
tvoid(z2); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
void* p;
const void* pc;
p = x0;
p = x1;
p = x2;
p = z0; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
p = z1; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
p = z2; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
pc = x0;
pc = x1;
pc = x2;
pc = z0;
pc = z1;
pc = z2;
transpose0(pc, p); /* { dg-warning "passing argument 1 of 'transpose0' discards 'const' qualifier from pointer target type" } */
transpose1(pc, p); /* { dg-warning "passing argument 1 of 'transpose1' discards 'const' qualifier from pointer target type" } */
transpose2(pc, p); /* { dg-warning "passing argument 1 of 'transpose2' discards 'const' qualifier from pointer target type" } */
transpose0(p, pc);
transpose1(p, pc);
transpose2(p, pc);
// passing as arguments
transpose0(y0, x0);
transpose1(y1, x1);
transpose2(y2, x2);
// initialization
const double (*u0p) = x0;
const double (*u1p)[2] = x1;
const double (*u2p)[2][2] = x2;
double (*v0p) = z0; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
double (*v1p)[2] = z1; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
double (*v2p)[2][2] = z2; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
// subtraction
&(x0[1]) - &(z0[0]);
&(x1[1]) - &(z1[0]);
&(x2[1]) - &(z2[0]);
// comparison
x0 == z0;
x1 == z1;
x2 == z2;
x0 < z0;
x1 < z1;
x2 < z2;
x0 > z0;
x1 > z1;
x2 > z2;
// assignment
u0p = x0;
u1p = x1;
u2p = x2;
v0p = z0; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v1p = z1; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v2p = z2; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
// conditional expressions
(void)(1 ? x0 : z0);
(void)(1 ? x1 : z1);
(void)(1 ? x2 : z2);
(void)(1 ? x0 : x1); /* { dg-warning "pointer type mismatch in conditional expression" } */
(void)(1 ? x1 : x2); /* { dg-warning "pointer type mismatch in conditional expression" } */
(void)(1 ? x2 : x0); /* { dg-warning "pointer type mismatch in conditional expression" } */
v0p = (1 ? z0 : v0p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v1p = (1 ? z1 : v1p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v2p = (1 ? z2 : v2p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v0p = (1 ? x0 : u0p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v1p = (1 ? x1 : u1p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v2p = (1 ? x2 : u2p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
(1 ? x0 : z0)[0] = 1; /* { dg-error "assignment of read-only location" } */
(1 ? x1 : z1)[0][0] = 1; /* { dg-error "assignment of read-only location" } */
(1 ? x2 : z2)[0][0][0] = 1; /* { dg-error "assignment of read-only location" } */
v0p = (1 ? p : z0); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v1p = (1 ? p : z1); /* { dg-warning "pointer to array loses qualifier in conditional expression" } */
v2p = (1 ? p : z2); /* { dg-warning "pointer to array loses qualifier in conditional expression" } */
v0p = (1 ? pc : x0); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v1p = (1 ? pc : x1); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
v2p = (1 ? pc : x2); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
}

Loading

0 comments on commit 768952b

Please sign in to comment.