From 1e7444775fe41e3880e40a8aaab4442017092cfe Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 20 Oct 2017 08:50:09 -0400
Subject: [PATCH 42/46] FIXME: use wrapper nodes in C FE

---
 gcc/c/c-fold.c                                     |   1 +
 gcc/c/c-typeck.c                                   |   8 +-
 .../gcc.dg/plugin/diagnostic-test-expressions-1.c  | 140 +++++++++++++++++++++
 .../gcc.dg/plugin/diagnostic-test-show-trees-1.c   |  41 ++++++
 4 files changed, 187 insertions(+), 3 deletions(-)

diff --git a/gcc/c/c-fold.c b/gcc/c/c-fold.c
index 0abc4ac..ea628d9 100644
--- a/gcc/c/c-fold.c
+++ b/gcc/c/c-fold.c
@@ -590,6 +590,7 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
      have been done by this point, so remove them again.  */
   nowarning |= TREE_NO_WARNING (ret);
   STRIP_TYPE_NOPS (ret);
+  STRIP_DECL_USAGE_EXPR (ret);
   if (nowarning && !TREE_NO_WARNING (ret))
     {
       if (!CAN_HAVE_LOCATION_P (ret))
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 4bdc48a..ea4f7cc 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -1925,6 +1925,7 @@ mark_exp_read (tree exp)
     case MODIFY_EXPR:
     case REALPART_EXPR:
     case IMAGPART_EXPR:
+    case DECL_USAGE_EXPR:
     CASE_CONVERT:
     case ADDR_EXPR:
     case VIEW_CONVERT_EXPR:
@@ -2829,7 +2830,7 @@ build_external_ref (location_t loc, tree id, bool fun, tree *type)
     record_inline_static (loc, current_function_decl, ref,
 			  csi_internal);
 
-  return ref;
+  return build1_loc (loc, DECL_USAGE_EXPR, TREE_TYPE (ref), ref);
 }
 
 /* Record details of decls possibly used inside sizeof or typeof.  */
@@ -4727,9 +4728,10 @@ lvalue_p (const_tree ref)
     case INDIRECT_REF:
     case ARRAY_REF:
     case ARRAY_NOTATION_REF:
-    case VAR_DECL:
-    case PARM_DECL:
+    case VAR_DECL: //FIXME
+    case PARM_DECL:  //FIXME
     case RESULT_DECL:
+    case DECL_USAGE_EXPR:
     case ERROR_MARK:
       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
 	      && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
index 03b7804..bd33580 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
@@ -19,6 +19,113 @@ extern void __emit_expression_range (int dummy, ...);
 
 int global;
 
+void test_global (void)
+{
+  __emit_expression_range (0, global); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, global);
+                               ^~~~~~
+   { dg-end-multiline-output "" } */
+}
+
+void test_param (int param)
+{
+  __emit_expression_range (0, param); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, param);
+                               ^~~~~
+   { dg-end-multiline-output "" } */
+}
+
+void test_local (void)
+{
+  int local = 5;
+
+  __emit_expression_range (0, local); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, local);
+                               ^~~~~
+   { dg-end-multiline-output "" } */
+}
+
+void test_integer_constants (void)
+{
+  __emit_expression_range (0, 1234); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 1234);
+                               ^~~~
+   { dg-end-multiline-output "" } */
+
+  /* Ensure that zero works.  */
+
+  __emit_expression_range (0, 0); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 0);
+                               ^
+   { dg-end-multiline-output "" } */
+}
+
+void test_character_constants (void)
+{
+  __emit_expression_range (0, 'a'); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 'a');
+                               ^~~
+   { dg-end-multiline-output "" } */
+}
+
+void test_floating_constants (void)
+{
+  __emit_expression_range (0, 98.6); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 98.6);
+                               ^~~~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, .6); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, .6);
+                               ^~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, 98.); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 98.);
+                               ^~~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, 6.022140857e23 ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 6.022140857e23 );
+                               ^~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, 98.6f ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 98.6f );
+                               ^~~~~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, 6.022140857e23l ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, 6.022140857e23l );
+                               ^~~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+
+enum test_enum {
+  TEST_ENUM_VALUE
+};
+
+void test_enumeration_constant (void)
+{
+  __emit_expression_range (0, TEST_ENUM_VALUE ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, TEST_ENUM_VALUE );
+                               ^~~~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+
 void test_parentheses (int a, int b)
 {
   __emit_expression_range (0, (a + b) ); /* { dg-warning "range" } */
@@ -40,6 +147,15 @@ void test_parentheses (int a, int b)
    { dg-end-multiline-output "" } */
 }
 
+void test_string_literal (void)
+{
+  __emit_expression_range (0, "0123456789"); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, "0123456789");
+                               ^~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+
 /* Postfix expressions.  ************************************************/
 
 void test_array_reference (int *arr)
@@ -134,6 +250,15 @@ void test_indirection (int *ptr)
    { dg-end-multiline-output "" } */
 }
 
+void test_unary_plus (int i)
+{
+  __emit_expression_range (0, +i ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, +i );
+                               ^~
+   { dg-end-multiline-output "" } */
+}
+
 void test_unary_minus (int i)
 {
   __emit_expression_range (0, -i ); /* { dg-warning "range" } */
@@ -161,6 +286,21 @@ void test_logical_negation (int flag)
    { dg-end-multiline-output "" } */
 }
 
+void test_sizeof (int i)
+{
+  __emit_expression_range (0, sizeof i ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, sizeof i );
+                               ^~~~~~~~
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, sizeof (char) ); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, sizeof (char) );
+                               ^~~~~~~~~~~~~
+   { dg-end-multiline-output "" } */
+}
+
 /* Casts.  ****************************************************/
 
 void test_cast (void *ptr)
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-trees-1.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-trees-1.c
index 7473a07..3088db2 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-trees-1.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-trees-1.c
@@ -50,6 +50,16 @@ void test_quadratic (double a, double b, double c)
 
 /* { dg-begin-multiline-output "" }
       (-b + sqrt (b * b - 4 * a * c))
+                  ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
+                      ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
                           ~~~~~~^~~
    { dg-end-multiline-output "" } */
 
@@ -59,7 +69,38 @@ void test_quadratic (double a, double b, double c)
    { dg-end-multiline-output "" } */
 
 /* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
+                          ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
+                              ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
+                                  ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      (-b + sqrt (b * b - 4 * a * c))
+        ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
       / (2 * a));
         ~~~^~~~
    { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      / (2 * a));
+         ^
+   { dg-end-multiline-output "" } */
+
+/* { dg-begin-multiline-output "" }
+      / (2 * a));
+             ^
+   { dg-end-multiline-output "" } */
+
 }
-- 
1.8.5.3

