aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2017-09-15 17:21:50 +0000
committerBernd Edlinger <bernd.edlinger@hotmail.de>2017-09-15 17:21:50 +0000
commit5e1a1033e9efad49ae4baab5f2a52b22ddb9c4e9 (patch)
tree5e9491b32fa26d79e07318fdf780451f77607b38
parent0d5ea2ec2e7e74abd8548bd47bd1005ae2c9640e (diff)
2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de>
* common.opt (Wcast-align=strict): New warning option. * doc/invoke.texi: Document -Wcast-align=strict. c: 2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de> * c-typeck.c (build_c_cast): Implement -Wcast-align=strict. cp: 2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de> * typeck.c (build_reinterpret_cast_1, build_const_cast_1): Implement -Wcast-align=strict. testsuite: 2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de> * c-c++-common/Wcast-align.c: New test. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@252832 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/c/ChangeLog4
-rw-r--r--gcc/c/c-typeck.c5
-rw-r--r--gcc/common.opt4
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/typeck.c17
-rw-r--r--gcc/doc/invoke.texi9
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/c-c++-common/Wcast-align.c23
9 files changed, 69 insertions, 7 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index b3d3407f171..050aef81445 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ * common.opt (Wcast-align=strict): New warning option.
+ * doc/invoke.texi: Document -Wcast-align=strict.
+
2017-09-15 Pierre-Marie de Rodat <derodat@adacore.com>
* cgraph.h (cgraph_thunk_info): Add comments.
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 957daeb4fe9..e8e0fb087d3 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,7 @@
+2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ * c-typeck.c (build_c_cast): Implement -Wcast-align=strict.
+
2017-09-13 Marek Polacek <polacek@redhat.com>
PR c/82167
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index f45fd3cfbbf..1956d45e251 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -5578,7 +5578,7 @@ build_c_cast (location_t loc, tree type, tree expr)
}
/* Warn about possible alignment problems. */
- if (STRICT_ALIGNMENT
+ if ((STRICT_ALIGNMENT || warn_cast_align == 2)
&& TREE_CODE (type) == POINTER_TYPE
&& TREE_CODE (otype) == POINTER_TYPE
&& TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
@@ -5587,7 +5587,8 @@ build_c_cast (location_t loc, tree type, tree expr)
restriction is unknown. */
&& !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
&& TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
- && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
+ && min_align_of_type (TREE_TYPE (type))
+ > min_align_of_type (TREE_TYPE (otype)))
warning_at (loc, OPT_Wcast_align,
"cast increases required alignment of target type");
diff --git a/gcc/common.opt b/gcc/common.opt
index 1581ca885e5..fa6dd845d54 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -566,6 +566,10 @@ Wcast-align
Common Var(warn_cast_align) Warning
Warn about pointer casts which increase alignment.
+Wcast-align=strict
+Common Var(warn_cast_align,2) Warning
+Warn about pointer casts which increase alignment.
+
Wcpp
Common Var(warn_cpp) Init(1) Warning
Warn when a #warning directive is encountered.
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 0a3b80f4518..c0132251edc 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ * typeck.c (build_reinterpret_cast_1,
+ build_const_cast_1): Implement -Wcast-align=strict.
+
2017-09-15 Jakub Jelinek <jakub@redhat.com>
* decl.c (redeclaration_error_message): Use cxx17 instead of cxx1z,
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index aa5d1288a5a..028d56ff18c 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -7265,15 +7265,16 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
complain))
return error_mark_node;
/* Warn about possible alignment problems. */
- if (STRICT_ALIGNMENT && warn_cast_align
- && (complain & tf_warning)
+ if ((STRICT_ALIGNMENT || warn_cast_align == 2)
+ && (complain & tf_warning)
&& !VOID_TYPE_P (type)
&& TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
&& COMPLETE_TYPE_P (TREE_TYPE (type))
&& COMPLETE_TYPE_P (TREE_TYPE (intype))
- && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
+ && min_align_of_type (TREE_TYPE (type))
+ > min_align_of_type (TREE_TYPE (intype)))
warning (OPT_Wcast_align, "cast from %qH to %qI "
- "increases required alignment of target type", intype, type);
+ "increases required alignment of target type", intype, type);
/* We need to strip nops here, because the front end likes to
create (int *)&a for array-to-pointer decay, instead of &a[0]. */
@@ -7447,6 +7448,14 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
the user is making a potentially unsafe cast. */
check_for_casting_away_constness (src_type, dst_type,
CAST_EXPR, complain);
+ /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
+ if ((STRICT_ALIGNMENT || warn_cast_align == 2)
+ && (complain & tf_warning)
+ && min_align_of_type (TREE_TYPE (dst_type))
+ > min_align_of_type (TREE_TYPE (src_type)))
+ warning (OPT_Wcast_align, "cast from %qH to %qI "
+ "increases required alignment of target type",
+ src_type, dst_type);
}
if (reference_type)
{
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index a727d952fd2..60650c83917 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -266,7 +266,8 @@ Objective-C and Objective-C++ Dialects}.
-Wno-attributes -Wbool-compare -Wbool-operation @gol
-Wno-builtin-declaration-mismatch @gol
-Wno-builtin-macro-redefined -Wc90-c99-compat -Wc99-c11-compat @gol
--Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol
+-Wc++-compat -Wc++11-compat -Wc++14-compat @gol
+-Wcast-align -Wcast-align=strict -Wcast-qual @gol
-Wchar-subscripts -Wchkp -Wcatch-value -Wcatch-value=@var{n} @gol
-Wclobbered -Wcomment -Wconditionally-supported @gol
-Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol
@@ -5931,6 +5932,12 @@ target is increased. For example, warn if a @code{char *} is cast to
an @code{int *} on machines where integers can only be accessed at
two- or four-byte boundaries.
+@item -Wcast-align=strict
+@opindex Wcast-align=strict
+Warn whenever a pointer is cast such that the required alignment of the
+target is increased. For example, warn if a @code{char *} is cast to
+an @code{int *} regardless of the target machine.
+
@item -Wwrite-strings
@opindex Wwrite-strings
@opindex Wno-write-strings
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 303f700c451..0091855e2c9 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2017-09-15 Bernd Edlinger <bernd.edlinger@hotmail.de>
+
+ * c-c++-common/Wcast-align.c: New test.
+
2017-09-15 Eric Botcazou <ebotcazou@adacore.com>
* g++.dg/sso-1.C: New test.
diff --git a/gcc/testsuite/c-c++-common/Wcast-align.c b/gcc/testsuite/c-c++-common/Wcast-align.c
new file mode 100644
index 00000000000..fac1795476c
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wcast-align.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-Wcast-align=strict" } */
+
+typedef char __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) c;
+typedef struct __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__)))
+{
+ char x;
+} d;
+
+char *x;
+c *y;
+d *z;
+struct s { long long x; } *p;
+struct t { double x; } *q;
+
+void
+foo (void)
+{
+ y = (c *) x; /* { dg-warning "alignment" } */
+ z = (d *) x; /* { dg-warning "alignment" } */
+ (long long *) p; /* { dg-bogus "alignment" } */
+ (double *) q; /* { dg-bogus "alignment" } */
+}