diff mbox series

avoid -Wabsolute-value in dead subexpressions (PR 89425)

Message ID ae9637ef-85dc-661c-b614-8f10346386da@gmail.com
State New
Headers show
Series avoid -Wabsolute-value in dead subexpressions (PR 89425) | expand

Commit Message

Martin Sebor Feb. 21, 2019, 11 p.m. UTC
Joseph noticed the -Wabsolute-value warning newly introduced in GCC
9 and included in -Wextra triggers on some unreachable subexpressions
in Glibc code that the C front end otherwise makes an effort to avoid
diagnosing.  The attached trivial patch guards the warning to avoid
these spurious instances.

Tested on x86_64-linux.

Similarly to other warnings like it, -Wabsolute-value still triggers
in unreachable statements but the only way to deal with those instances
would be to move all such warnings into the middle-end.

Martin

Comments

Joseph Myers Feb. 22, 2019, 1:47 a.m. UTC | #1
On Thu, 21 Feb 2019, Martin Sebor wrote:

> Joseph noticed the -Wabsolute-value warning newly introduced in GCC
> 9 and included in -Wextra triggers on some unreachable subexpressions
> in Glibc code that the C front end otherwise makes an effort to avoid
> diagnosing.  The attached trivial patch guards the warning to avoid
> these spurious instances.
> 
> Tested on x86_64-linux.

OK.
diff mbox series

Patch

PR c/89425 - -Wabsolute-value warns in dead subexpressions

gcc/c/ChangeLog:

	PR c/89425
	* c-parser.c (sizeof_ptr_memacc_comptypes): Avoid warning in
	unreachable subexpressions.

gcc/testsuite/ChangeLog:

	PR c/89425
	* gcc.dg/Wabsolute-value.c: New test. 

Index: gcc/c/c-parser.c
===================================================================
--- gcc/c/c-parser.c	(revision 269041)
+++ gcc/c/c-parser.c	(working copy)
@@ -9374,6 +9374,10 @@  sizeof_ptr_memacc_comptypes (tree type1, tree type
 static void
 warn_for_abs (location_t loc, tree fndecl, tree arg)
 {
+  /* Avoid warning in unreachable subexpressions.  */
+  if (c_inhibit_evaluation_warnings)
+    return;
+
   tree atype = TREE_TYPE (arg);
 
   /* Casts from pointers (and thus arrays and fndecls) will generate
Index: gcc/testsuite/gcc.dg/Wabsolute-value.c
===================================================================
--- gcc/testsuite/gcc.dg/Wabsolute-value.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/Wabsolute-value.c	(working copy)
@@ -0,0 +1,57 @@ 
+/* PR c/89425 - -Wabsolute-value warns in dead subexpressions
+   { dg-do compile }
+   { dg-options "-Wabsolute-value -ftrack-macro-expansion=0" } */
+
+struct Vals
+{
+  signed char sc;
+  signed short ss;
+  signed int si;
+  signed long sl;
+  signed long long sll;
+
+  unsigned char uc;
+  unsigned short us;
+  unsigned int ui;
+  unsigned long ul;
+  unsigned long long ull;
+
+  float f;
+  double d;
+  long double ld;
+};
+
+#define abs(x)     __builtin_abs (x)
+#define labs(x)    __builtin_labs (x)
+#define llabs(x)   __builtin_llabs (x)
+
+#define fabsf(x)   __builtin_fabsf (x)
+#define fabs(x)    __builtin_fabs (x)
+
+
+void tst_warn (struct Vals *p)
+{
+  /* Verify that "-Wabsolute-value is issued for subexpressions
+     that are evaluated.  */
+
+  p->uc =  0 ? abs (p->sc) : abs (p->uc);         /* { dg-warning "\\\[-Wabsolute-value]" } */
+  p->us =  0 ? abs (p->ss) : abs (p->us);         /* { dg-warning "\\\[-Wabsolute-value]" } */
+  p->ui =  0 ? abs (p->si) : abs (p->ui);         /* { dg-warning "\\\[-Wabsolute-value]" } */
+  p->ul =  0 ? labs (p->sl) : labs (p->ul);       /* { dg-warning "\\\[-Wabsolute-value]" } */
+  p->ull = 0 ? llabs (p->sll) : llabs (p->ull);   /* { dg-warning "\\\[-Wabsolute-value]" } */
+
+  p->d   = 0 ? fabs (p->d) : fabsf (p->d);        /* { dg-warning "\\\[-Wabsolute-value]" } */
+}
+
+void tst_no_warn (struct Vals *p)
+{
+  /* Verify that "-Wabsolute-value is not issued for subexpressions
+     that are not evaluated.  */
+
+  p->uc =  0 ? abs (p->uc) : abs (p->sc);
+  p->us =  0 ? abs (p->us) : abs (p->ss);
+  p->ui =  0 ? abs (p->ui) : abs (p->si);
+  p->ul =  0 ? labs (p->ul) : labs (p->sl);
+  p->ull = 0 ? llabs (p->ull) : llabs (p->sll);
+  p->d   = 0 ? fabsf (p->d) : fabs (p->d);
+}