diff mbox

C++ PATCH for c++/69688 (bogus error with -Wsign-compare)

Message ID 20160205223243.GA3321@redhat.com
State New
Headers show

Commit Message

Marek Polacek Feb. 5, 2016, 10:32 p.m. UTC
This issue is similar to c++/68586 -- maybe_constant_value returned stale
value for a decl from the cache.  Fixed by clearing the caches when we
store init value for a decl.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2016-02-05  Marek Polacek  <polacek@redhat.com>

	PR c++/69688
	* typeck2.c (store_init_value): Clear cv and fold caches.

	* g++.dg/init/const12.C: New test.


	Marek

Comments

Jason Merrill Feb. 5, 2016, 10:36 p.m. UTC | #1
On 02/05/2016 05:32 PM, Marek Polacek wrote:
>    if (TREE_CODE (type) == ERROR_MARK)
>      return NULL_TREE;
>
> +  /* Here, DECL may change value; purge caches.  */
> +  clear_fold_cache ();
> +  clear_cv_cache ();
> +
>    if (MAYBE_CLASS_TYPE_P (type))

This should happen after computing the value to be stored, not before. 
Also, could you combine those two functions into one?  There's no reason 
for callers such as this to need to call two different functions.

Jason
diff mbox

Patch

diff --git gcc/cp/typeck2.c gcc/cp/typeck2.c
index 419faa2..737dfe4 100644
--- gcc/cp/typeck2.c
+++ gcc/cp/typeck2.c
@@ -783,6 +783,10 @@  store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
   if (TREE_CODE (type) == ERROR_MARK)
     return NULL_TREE;
 
+  /* Here, DECL may change value; purge caches.  */
+  clear_fold_cache ();
+  clear_cv_cache ();
+
   if (MAYBE_CLASS_TYPE_P (type))
     {
       if (TREE_CODE (init) == TREE_LIST)
diff --git gcc/testsuite/g++.dg/init/const12.C gcc/testsuite/g++.dg/init/const12.C
index e69de29..2f6f9b2 100644
--- gcc/testsuite/g++.dg/init/const12.C
+++ gcc/testsuite/g++.dg/init/const12.C
@@ -0,0 +1,20 @@ 
+// PR c++/69688
+// { dg-do compile }
+// { dg-options "-Wsign-compare" }
+
+struct S
+{
+  static const int s;
+  static const char c[];
+  static wchar_t w[];
+
+  S ()
+    {
+      for (int i = 0; i < s; i++)
+	w[i] = 0;
+    }
+};
+
+const char S::c[] = "x";
+const int S::s = sizeof (S::c) - 1;
+wchar_t S::w[S::s];