From fccc959e2a557fa7fe1323b3f0b88c50df350903 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 21 Jun 2019 14:02:41 -0400
Subject: [PATCH 43/47] FIXME: warning fixes and cleanups

---
 gcc/analyzer/analyzer.h                      | 12 ++++++++++
 gcc/analyzer/impl-2.cc                       | 17 +++++++++-----
 gcc/analyzer/impl-3.cc                       | 11 ++++-----
 gcc/analyzer/impl-4.cc                       | 27 +++++++++++++---------
 gcc/analyzer/region-model.cc                 | 34 +++++++++++++++-------------
 gcc/analyzer/sm-file.cc                      |  3 ++-
 gcc/analyzer/sm-malloc.cc                    |  3 ++-
 gcc/analyzer/sm.cc                           |  2 +-
 gcc/analyzer/sm.h                            |  5 ++--
 gcc/testsuite/gcc.dg/analyzer/data-model-1.c |  2 ++
 10 files changed, 71 insertions(+), 45 deletions(-)

diff --git a/gcc/analyzer/analyzer.h b/gcc/analyzer/analyzer.h
index cc04d3e..5a2ce06 100644
--- a/gcc/analyzer/analyzer.h
+++ b/gcc/analyzer/analyzer.h
@@ -44,6 +44,18 @@ public:
   ~auto_cfun () { pop_cfun (); }
 };
 
+/* FIXME.  */
+
+#define PUSH_IGNORE_WFORMAT \
+  _Pragma("GCC diagnostic push") \
+  _Pragma("GCC diagnostic ignored \"-Wformat\"") \
+  _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\"")
+
+/* FIXME.  */
+
+#define POP_IGNORE_WFORMAT \
+  _Pragma("GCC diagnostic pop")
+
 #if CHECKING_P
 
 namespace selftest {
diff --git a/gcc/analyzer/impl-2.cc b/gcc/analyzer/impl-2.cc
index 57b3d9f..beb1b9d 100644
--- a/gcc/analyzer/impl-2.cc
+++ b/gcc/analyzer/impl-2.cc
@@ -2475,8 +2475,8 @@ private:
   }
 
   const backtrace &m_b;
-  int m_node_idx;
-  int m_stmt_idx;
+  unsigned m_node_idx;
+  unsigned m_stmt_idx;
   bool m_phis; // TODO: use this
 };
 
@@ -3163,7 +3163,7 @@ engine::prune_path (checker_path *path,
   /* TODO: go backwards, skipping state change events that aren't var
      but update the pertinent var when state-copying occurs.  */
   int idx = path->m_events.length () - 1;
-  while (idx >= 0 && idx < path->m_events.length ())
+  while (idx >= 0 && idx < (signed)path->m_events.length ())
     {
       checker_event *base_event = path->m_events[idx];
       if (get_logger ())
@@ -3238,7 +3238,7 @@ engine::prune_path (checker_path *path,
 		  if (TREE_CODE (var) == SSA_NAME)
 		    if (SSA_NAME_DEF_STMT (var)->bb == dest->m_bb)
 		      {
-			if (const gphi *phi
+			if (gphi *phi
 			    = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (var)))
 			  {
 			    /* Update var based on its phi node.  */
@@ -3249,7 +3249,8 @@ engine::prune_path (checker_path *path,
 			    if (get_logger ())
 			      {
 				pretty_printer pp;
-				pp_gimple_stmt_1 (&pp, (gimple *)phi, 0, (dump_flags_t)0);
+				pp_gimple_stmt_1 (&pp, (gimple *)phi,
+						  0, (dump_flags_t)0);
 				// FIXME: const_cast!
 				log ("  phi: %s", pp_formatted_text (&pp));
 			      }
@@ -3299,7 +3300,9 @@ engine::prune_path (checker_path *path,
 
 	case EK_CALL_SUMMARY:
 	  {
+#if 0
 	    call_summary *summary_event = (call_summary *)base_event;
+#endif
 #if 0
 	    inform (event->m_loc, "considering call_summary %i: %s",
 		    idx, base_event->m_desc);
@@ -3325,7 +3328,7 @@ engine::prune_path (checker_path *path,
     {
       // FIXME
       int idx = path->m_events.length () - 1;
-      while (idx >= 0 && idx < path->m_events.length ())
+      while (idx >= 0 && idx < (signed)path->m_events.length ())
 	{
 	  checker_event *base_event = path->m_events[idx];
 	  if (base_event->m_kind == EK_FUNCTION_ENTRY)
@@ -3361,7 +3364,9 @@ engine::is_valid_path (const checker_path &path)
 	{
 	case EK_STMT:
 	  {
+#if 0
 	    statement_event *stmt_ev = (statement_event *)base_event;
+#endif
 	    //const gimple *stmt = stmt_ev->m_stmt;
 	    // TODO: this will largely be a duplicate of on_stmt
 	  }
diff --git a/gcc/analyzer/impl-3.cc b/gcc/analyzer/impl-3.cc
index 83b927d..1095b72 100644
--- a/gcc/analyzer/impl-3.cc
+++ b/gcc/analyzer/impl-3.cc
@@ -1144,7 +1144,7 @@ engine::traverse (checker3_path path)
   for (gphi_iterator gpi = const_cast<supernode *>(node)->start_phis ();
        !gsi_end_p (gpi); gsi_next (&gpi))
     {
-      gphi *phi = gpi.phi ();
+      //gphi *phi = gpi.phi ();
       // TODO
     }
 
@@ -1311,7 +1311,7 @@ engine::prune_path (checker3_path *path,
   /* TODO: go backwards, skipping state change events that aren't var
      but update the pertinent var when state-copying occurs.  */
   int idx = path->m_events.length () - 1;
-  while (idx >= 0 && idx < path->m_events.length ())
+  while (idx >= 0 && idx < (signed)path->m_events.length ())
     {
       checker3_event *base_event = path->m_events[idx];
       if (get_logger ())
@@ -1386,7 +1386,7 @@ engine::prune_path (checker3_path *path,
 		  if (TREE_CODE (var) == SSA_NAME)
 		    if (SSA_NAME_DEF_STMT (var)->bb == dest->m_bb)
 		      {
-			if (const gphi *phi
+			if (gphi *phi
 			    = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (var)))
 			  {
 			    /* Update var based on its phi node.  */
@@ -1397,8 +1397,7 @@ engine::prune_path (checker3_path *path,
 			    if (get_logger ())
 			      {
 				pretty_printer pp;
-				pp_gimple_stmt_1 (&pp, (gimple *)phi, 0, (dump_flags_t)0);
-				// FIXME: const_cast!
+				pp_gimple_stmt_1 (&pp, phi, 0, (dump_flags_t)0);
 				log ("  phi: %s", pp_formatted_text (&pp));
 			      }
 			  }
@@ -1457,7 +1456,7 @@ engine::prune_path (checker3_path *path,
     {
       // FIXME
       int idx = path->m_events.length () - 1;
-      while (idx >= 0 && idx < path->m_events.length ())
+      while (idx >= 0 && idx < (signed)path->m_events.length ())
 	{
 	  checker3_event *base_event = path->m_events[idx];
 	  if (base_event->m_kind == EK_FUNCTION_ENTRY)
diff --git a/gcc/analyzer/impl-4.cc b/gcc/analyzer/impl-4.cc
index 7fb6d9e..498bec6 100644
--- a/gcc/analyzer/impl-4.cc
+++ b/gcc/analyzer/impl-4.cc
@@ -2588,7 +2588,7 @@ public:
 	/* What was the next write to the underlying var
 	   after the SSA name was set? (if any).  */
 
-	for (int idx = idx_of_def_stmt + 1;
+	for (unsigned idx = idx_of_def_stmt + 1;
 	     idx < epath.m_edges.length ();
 	     ++idx)
 	  {
@@ -4315,7 +4315,7 @@ diagnostic_manager::prune_path (checker4_path *path,
      Iterate backwards through PATH, skipping state change events that aren't
      VAR but update the pertinent VAR when state-copying occurs.  */
   int idx = path->m_events.length () - 1;
-  while (idx >= 0 && idx < path->m_events.length ())
+  while (idx >= 0 && idx < (signed)path->m_events.length ())
     {
       checker4_event *base_event = path->m_events[idx];
       if (get_logger ())
@@ -4402,8 +4402,10 @@ diagnostic_manager::prune_path (checker4_path *path,
 	  {
 	    edge_event *event = (edge_event *)base_event;
 
+#if 0
 	    const program_state &src_state
 	      = event->m_eedge->m_src->get_state ();
+#endif
 #if 0
 	      svalue_id src_sid
 		= src_state.m_region_model->get_rvalue (dst_rep, NULL);
@@ -4427,7 +4429,7 @@ diagnostic_manager::prune_path (checker4_path *path,
 		  if (var && TREE_CODE (var) == SSA_NAME)
 		    if (SSA_NAME_DEF_STMT (var)->bb == dest->m_bb)
 		      {
-			if (const gphi *phi
+			if (gphi *phi
 			    = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (var)))
 			  {
 			    /* Update var based on its phi node.  */
@@ -4438,8 +4440,7 @@ diagnostic_manager::prune_path (checker4_path *path,
 			    if (get_logger ())
 			      {
 				pretty_printer pp;
-				pp_gimple_stmt_1 (&pp, (gimple *)phi, 0, (dump_flags_t)0);
-				// FIXME: const_cast!
+				pp_gimple_stmt_1 (&pp, phi, 0, (dump_flags_t)0);
 				log ("  phi: %s", pp_formatted_text (&pp));
 			      }
 			  }
@@ -4542,7 +4543,7 @@ diagnostic_manager::prune_path (checker4_path *path,
     {
       changed = false;
       int idx = path->m_events.length () - 3;
-      while (idx >= 0 && idx < path->m_events.length ())
+      while (idx >= 0 && idx < (signed)path->m_events.length ())
 	{
 	  if (path->m_events[idx]->is_call_p ()
 	      && path->m_events[idx + 1]->is_function_entry_p ()
@@ -4573,7 +4574,7 @@ diagnostic_manager::prune_path (checker4_path *path,
     {
       // FIXME
       int idx = path->m_events.length () - 1;
-      while (idx >= 0 && idx < path->m_events.length ())
+      while (idx >= 0 && idx < (signed)path->m_events.length ())
 	{
 	  checker4_event *base_event = path->m_events[idx];
 	  if (base_event->m_kind == EK_FUNCTION_ENTRY)
@@ -4964,7 +4965,7 @@ exploded_graph::dump_stats (FILE *out) const
     }
 
   fprintf (out, "PK_AFTER_SUPERNODE per supernode:\n");
-  for (int i = 0; i < m_PK_AFTER_SUPERNODE_per_snode.length (); i++)
+  for (unsigned i = 0; i < m_PK_AFTER_SUPERNODE_per_snode.length (); i++)
     fprintf (out, "  SN %i: %3i\n", i, m_PK_AFTER_SUPERNODE_per_snode[i]);
 }
 
@@ -5588,7 +5589,7 @@ state_purge_per_ssa_name::state_purge_per_ssa_name (const state_purge_map &map,
 {
   JIT_LOG_FUNC (map.get_logger ());
 
-  map.log ("SSA name: %qE", var);
+  map.log ("SSA name: %qE within %qD", var, fun->decl);
   if (map.get_logger ())
     {
       /* Show def stmt.  */
@@ -5699,7 +5700,7 @@ state_purge_per_ssa_name::state_purge_per_ssa_name (const state_purge_map &map,
 
   if (map.get_logger ())
     {
-      map.log ("%qE is needed to process:", var);
+      map.log ("%qE in %qD is needed to process:", var, fun->decl);
       for (point_set_t::iterator iter = m_points_needing_var.begin ();
 	   iter != m_points_needing_var.end ();
 	   ++iter)
@@ -5995,7 +5996,9 @@ public:
       {
 	if (i > 0)
 	  pp_string (pp, ", ");
+PUSH_IGNORE_WFORMAT
 	pp_printf (pp, "%qE", var);
+POP_IGNORE_WFORMAT
       }
     pp_printf (pp, "}");
     pp_newline (pp);
@@ -6004,7 +6007,9 @@ public:
       {
 	if (i > 0)
 	  pp_string (pp, ", ");
+PUSH_IGNORE_WFORMAT
 	pp_printf (pp, "%qE", var);
+POP_IGNORE_WFORMAT
       }
     pp_newline (pp);
   }
@@ -6208,7 +6213,7 @@ test_sm_state_map ()
   /* Test equality and hashing.  */
   {
     region_model model;
-    svalue_id sid_x = model.get_rvalue (x, NULL);
+    //svalue_id sid_x = model.get_rvalue (x, NULL);
     svalue_id sid_y = model.get_rvalue (y, NULL);
     svalue_id sid_z = model.get_rvalue (z, NULL);
 
diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index c236537..8c0fdd9 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -287,14 +287,6 @@ id_set<region_id>::id_set (const region_model *model)
 ////////////////////////////////////////////////////////////////////////////
 
 
-#define PUSH_IGNORE_WFORMAT \
-  _Pragma("GCC diagnostic push") \
-  _Pragma("GCC diagnostic ignored \"-Wformat\"") \
-  _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\"")
-
-#define POP_IGNORE_WFORMAT \
-  _Pragma("GCC diagnostic pop")
-
 /* class svalue.  */
 
 /* FIXME.  */
@@ -407,7 +399,7 @@ svalue::to_str () const
 /* FIXME.  */
 
 void
-svalue::remap_region_ids (const region_id_map &map)
+svalue::remap_region_ids (const region_id_map &)
 {
   /* Empty.  */
 }
@@ -1348,7 +1340,7 @@ heap_region::heap_region (const heap_region &other)
 /* FIXME.  */
 
 bool
-heap_region::compare_fields (const heap_region &other) const
+heap_region::compare_fields (const heap_region &) const
 {
   /* Empty.  */
   return true;
@@ -1893,7 +1885,7 @@ region_model::validate () const
     {
       svalue_id sid = r->get_value_direct ();
       if (!sid.null_p ())
-	gcc_assert (sid.as_int () < m_svalues.length ());
+	gcc_assert (sid.as_int () < (signed)m_svalues.length ());
     }
 
   // TODO: anything else?
@@ -1952,15 +1944,19 @@ region_model::on_assignment (const gassign *assign, region_model_context *ctxt)
 	tree offset = gimple_assign_rhs2 (assign);
 
 	region_id buffer_rid = deref_rvalue (ptr, ctxt);
+#if 0
 	svalue_id offset_sid = get_rvalue (offset, ctxt);
 	/* FIXME: offset is in bytes; we presumably want to
 	   map back to an index.  */
+#endif
 
 	map_region *array_region = get_region<map_region> (buffer_rid);
 	tree element_type = TREE_TYPE (TREE_TYPE (ptr));
+#if 0
 	tree index = offset;
 	/* FIXME: offset is in bytes; we presumably want to
 	   map back to an index.  */
+#endif
 
 	region_id element_rid
 	  = array_region->get_or_create (this, buffer_rid, offset,
@@ -2119,7 +2115,7 @@ region_model::on_call (const gcall *call, region_model_context *ctxt)
 	= get_root_region ()->ensure_heap_region (this);
       region_id new_rid = add_region (new symbolic_region (heap_rid));
 
-      if (tree lhs = gimple_call_lhs (call))
+      if (!lhs_rid.null_p ())
 	{
 	  svalue_id ptr_sid
 	    = add_svalue (new region_svalue (lhs_type, new_rid));
@@ -2156,7 +2152,7 @@ region_model::on_call (const gcall *call, region_model_context *ctxt)
     {
       region_id frame_rid = get_current_frame_id ();
       region_id new_rid = add_region (new symbolic_region (frame_rid));
-      if (tree lhs = gimple_call_lhs (call))
+      if (!lhs_rid.null_p ())
 	{
 	  svalue_id ptr_sid
 	    = add_svalue (new region_svalue (lhs_type, new_rid));
@@ -2280,7 +2276,9 @@ region_model::get_lvalue (tree expr, region_model_context *ctxt)
 	gcc_assert (TREE_OPERAND (expr, 2) == NULL_TREE);
 	gcc_assert (TREE_OPERAND (expr, 3) == NULL_TREE);
 	region_id array_rid = get_lvalue (array, ctxt);
+#if 0
 	svalue_id index_sid = get_rvalue (index, ctxt);
+#endif
 	map_region *array_region = get_region<map_region> (array_rid);
 	tree element_type = TREE_TYPE (TREE_TYPE (array));
 	region_id element_rid
@@ -2294,8 +2292,10 @@ region_model::get_lvalue (tree expr, region_model_context *ctxt)
     case MEM_REF:
       {
 	tree ptr = TREE_OPERAND (expr, 0);
+#if 0
 	tree offset = TREE_OPERAND (expr, 1);
 	// FIXME: what about nonzero offset?
+#endif
 
 	return deref_rvalue (ptr, ctxt);
       }
@@ -2525,9 +2525,11 @@ region_model::deref_rvalue (tree ptr, region_model_context *ctxt)
 
     case SK_CONSTANT:
       {
+#if 0
 	constant_svalue *constant_sval
 	  = ptr_svalue->dyn_cast_constant_svalue ();
 	// FIXME: ^^ as_a
+#endif
 
 	/* TODO: issue a warning (presumably using some kind of context
 	   within the region_model?).  */
@@ -3295,7 +3297,7 @@ region_model::purge_unused_svalues (purge_stats *stats,
     }
 
   /* Drop the redundant svalues from the end of the vector.  */
-  while (m_svalues.length () > next_used_new_sid)
+  while ((signed)m_svalues.length () > next_used_new_sid)
     {
       if (logger)
 	{
@@ -3384,7 +3386,7 @@ region_model::remap_region_ids (const region_id_map &map)
 void
 region_model::purge_regions (const region_id_set &set_to_purge,
 			     purge_stats *stats,
-			     logger *logger)
+			     logger *)
 {
   /* Build a mapping from old-rid to new-rid so that we can preserve
      order of the used IDs and move all redundant ones to the end.
@@ -3409,7 +3411,7 @@ region_model::purge_regions (const region_id_set &set_to_purge,
   remap_region_ids (map);
 
   /* Drop the redundant regions from the end of the vector.  */
-  while (m_regions.length () > next_used_new_rid)
+  while ((signed)m_regions.length () > next_used_new_rid)
     {
       delete m_regions.pop ();
       stats->m_num_regions++;
diff --git a/gcc/analyzer/sm-file.cc b/gcc/analyzer/sm-file.cc
index b60312e..78a86e2 100644
--- a/gcc/analyzer/sm-file.cc
+++ b/gcc/analyzer/sm-file.cc
@@ -205,7 +205,8 @@ fileptr_state_machine::on_leak (sm_context *sm_ctxt,
 				const supernode *node,
 				const gimple *stmt,
 				tree var,
-				state_machine::state_t state) const
+				state_machine::state_t state ATTRIBUTE_UNUSED)
+  const
 {
   sm_ctxt->warn_for_state (node, stmt, var, m_unchecked,
 			   new file_leak (var));
diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc
index fa79f21..d2b4536 100644
--- a/gcc/analyzer/sm-malloc.cc
+++ b/gcc/analyzer/sm-malloc.cc
@@ -695,7 +695,8 @@ malloc_state_machine::on_leak (sm_context *sm_ctxt,
 			       const supernode *node,
 			       const gimple *stmt,
 			       tree var,
-			       state_machine::state_t state) const
+			       state_machine::state_t state ATTRIBUTE_UNUSED)
+  const
 {
   sm_ctxt->warn_for_state (node, stmt, var, m_unchecked,
 			   new malloc_leak (*this, var));
diff --git a/gcc/analyzer/sm.cc b/gcc/analyzer/sm.cc
index bb98bb8..dedf389 100644
--- a/gcc/analyzer/sm.cc
+++ b/gcc/analyzer/sm.cc
@@ -156,7 +156,7 @@ make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
 
   if (flag_analyzer_checker)
     {
-      int read_index, write_index;
+      unsigned read_index, write_index;
       state_machine **sm;
 
       // FIXME: this leaks the machines
diff --git a/gcc/analyzer/sm.h b/gcc/analyzer/sm.h
index 46a472f..1b9be15 100644
--- a/gcc/analyzer/sm.h
+++ b/gcc/analyzer/sm.h
@@ -43,7 +43,7 @@ class sm_context;
 class state_machine : public log_user
 {
 public:
-  typedef int state_t;
+  typedef unsigned state_t;
 
   state_machine (const char *name, logger *logger)
   : log_user (logger), m_name (name) {}
@@ -103,8 +103,7 @@ start_start_p (state_machine::state_t state)
 
 /* FIXME.  */
 
-// TODO: maybe capture which sm emitted the error
-
+// TODO: maybe capture which sm and pending_diagnostic emitted the error?
 // FIXME: is this needed?
 
 class metadata : public diagnostic_metadata
diff --git a/gcc/testsuite/gcc.dg/analyzer/data-model-1.c b/gcc/testsuite/gcc.dg/analyzer/data-model-1.c
index 9337c2e..de1bace 100644
--- a/gcc/testsuite/gcc.dg/analyzer/data-model-1.c
+++ b/gcc/testsuite/gcc.dg/analyzer/data-model-1.c
@@ -765,6 +765,8 @@ void test_41 (void)
   __analyzer_eval (u.i == 42); /* { dg-warning "TRUE" } */
 }
 
+// TODO: writes to a union member invalidating knowledge about other members
+
 void test_42 (void)
 {
   int i;
-- 
1.8.5.3

