From a6bf99f00f8e86c98bdc797c18b171d8958ddb9a Mon Sep 17 00:00:00 2001
From: Martin Jambor <mjambor@suse.cz>
Date: Thu, 28 Apr 2016 16:35:04 +0200
Subject: [PATCH] Verify that context of local DECLs is the current function

2016-04-28  Martin Jambor  <mjambor@suse.cz>

	* tree-cfg.c (verify_expr): Verify that local declarations belong to
	this function.  Call verify_expr on MEM_REFs and bases of other
	handled_components.

From-SVN: r235570
---
 gcc/ChangeLog  |  6 ++++++
 gcc/tree-cfg.c | 28 +++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index b7237c2f7f3b..1b657f183ca2 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2016-04-28  Martin Jambor  <mjambor@suse.cz>
+
+	* tree-cfg.c (verify_expr): Verify that local declarations belong to
+	this function.  Call verify_expr on MEM_REFs and bases of other
+	handled_components.
+
 2016-04-28  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
 
 	* internal-fn.c (expand_arith_overflow): Convert preprocessor check
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index e1fdc9e2bce0..4ce1eaa32b02 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -2834,6 +2834,22 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
 	}
       break;
 
+    case PARM_DECL:
+    case VAR_DECL:
+    case RESULT_DECL:
+      {
+	tree context = decl_function_context (t);
+	if (context != cfun->decl
+	    && !SCOPE_FILE_SCOPE_P (context)
+	    && !TREE_STATIC (t)
+	    && !DECL_EXTERNAL (t))
+	  {
+	    error ("Local declaration from a different function");
+	    return t;
+	  }
+      }
+      break;
+
     case INDIRECT_REF:
       error ("INDIRECT_REF in gimple IL");
       return t;
@@ -2852,9 +2868,14 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
 	  error ("invalid offset operand of MEM_REF");
 	  return TREE_OPERAND (t, 1);
 	}
-      if (TREE_CODE (x) == ADDR_EXPR
-	  && (x = verify_address (x, TREE_OPERAND (x, 0))))
-	return x;
+      if (TREE_CODE (x) == ADDR_EXPR)
+	{
+	  tree va = verify_address (x, TREE_OPERAND (x, 0));
+	  if (va)
+	    return va;
+	  x = TREE_OPERAND (x, 0);
+	}
+      walk_tree (&x, verify_expr, data, NULL);
       *walk_subtrees = 0;
       break;
 
@@ -3016,6 +3037,7 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
 	  error ("invalid reference prefix");
 	  return t;
 	}
+      walk_tree (&t, verify_expr, data, NULL);
       *walk_subtrees = 0;
       break;
     case PLUS_EXPR:
-- 
GitLab