[pushed] analyzer: support diagnostics that don't have a stmt

David Malcolm dmalcolm@redhat.com
Fri Sep 15 17:53:32 GMT 2023


Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r14-4042-gb09193fb0686b7.

gcc/analyzer/ChangeLog:
	* analyzer.cc (get_stmt_location): Handle null stmt.
	* diagnostic-manager.cc (saved_diagnostic::saved_diagnostic): Copy
	m_loc from ploc.
	(saved_diagnostic::operator==): Compare m_loc.
	(saved_diagnostic::calc_best_epath): Only use m_stmt_finder if
	m_loc is unknown.
	(dedupe_key::dedupe_key): Initialize m_loc.
	(dedupe_key::operator==): Compare m_loc.
	(dedupe_key::get_location): Use m_loc if it's known.
	(dedupe_key::m_loc): New field.
	(diagnostic_manager::emit_saved_diagnostic): Only call
	get_emission_location if m_loc is unknown, preferring to use m_loc
	if it's available.
	* diagnostic-manager.h (saved_diagnostic::m_loc): New field.
	(pending_location::pending_location): Initialize m_loc.  Add
	overload taking a location_t rather than a stmt/stmt_finder.
	(pending_location::m_loc): New field.
---
 gcc/analyzer/analyzer.cc           |  2 ++
 gcc/analyzer/diagnostic-manager.cc | 35 +++++++++++++++++++-----------
 gcc/analyzer/diagnostic-manager.h  | 19 +++++++++++++++-
 3 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/gcc/analyzer/analyzer.cc b/gcc/analyzer/analyzer.cc
index 5091fb7a583..94c5cf242b2 100644
--- a/gcc/analyzer/analyzer.cc
+++ b/gcc/analyzer/analyzer.cc
@@ -41,6 +41,8 @@ namespace ana {
 location_t
 get_stmt_location (const gimple *stmt, function *fun)
 {
+  if (!stmt)
+    return UNKNOWN_LOCATION;
   if (get_pure_location (stmt->location) == UNKNOWN_LOCATION)
     {
       /* Workaround for missing location information for clobber
diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index b652e7032e9..972413a751a 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -678,14 +678,13 @@ saved_diagnostic::saved_diagnostic (const state_machine *sm,
   /* stmt_finder could be on-stack; we want our own copy that can
      outlive that.  */
   m_stmt_finder (ploc.m_finder ? ploc.m_finder->clone () : NULL),
+  m_loc (ploc.m_loc),
   m_var (var), m_sval (sval), m_state (state),
   m_d (std::move (d)), m_trailing_eedge (NULL),
   m_idx (idx),
   m_best_epath (NULL), m_problem (NULL),
   m_notes ()
 {
-  gcc_assert (m_stmt || m_stmt_finder);
-
   /* We must have an enode in order to be able to look for paths
      through the exploded_graph to this diagnostic.  */
   gcc_assert (m_enode);
@@ -704,6 +703,7 @@ saved_diagnostic::operator== (const saved_diagnostic &other) const
 	  && m_snode == other.m_snode
 	  && m_stmt == other.m_stmt
 	  /* We don't compare m_stmt_finder.  */
+	  && m_loc == other.m_loc
 	  && pending_diagnostic::same_tree_p (m_var, other.m_var)
 	  && m_state == other.m_state
 	  && m_d->equal_p (*other.m_d)
@@ -833,8 +833,8 @@ saved_diagnostic::dump_as_dot_node (pretty_printer *pp) const
 
 /* Use PF to find the best exploded_path for this saved_diagnostic,
    and store it in m_best_epath.
-   If m_stmt is still NULL, use m_stmt_finder on the epath to populate
-   m_stmt.
+   If we don't have a specific location in m_loc and m_stmt is still NULL,
+   use m_stmt_finder on the epath to populate m_stmt.
    Return true if a best path was found.  */
 
 bool
@@ -853,12 +853,15 @@ saved_diagnostic::calc_best_epath (epath_finder *pf)
     return false;
 
   gcc_assert (m_best_epath);
-  if (m_stmt == NULL)
+  if (m_loc == UNKNOWN_LOCATION)
     {
-      gcc_assert (m_stmt_finder);
-      m_stmt = m_stmt_finder->find_stmt (*m_best_epath);
+      if (m_stmt == NULL)
+	{
+	  gcc_assert (m_stmt_finder);
+	  m_stmt = m_stmt_finder->find_stmt (*m_best_epath);
+	}
+      gcc_assert (m_stmt);
     }
-  gcc_assert (m_stmt);
 
   return true;
 }
@@ -1212,9 +1215,9 @@ class dedupe_key
 {
 public:
   dedupe_key (const saved_diagnostic &sd)
-  : m_sd (sd), m_stmt (sd.m_stmt)
+  : m_sd (sd), m_stmt (sd.m_stmt), m_loc (sd.m_loc)
   {
-    gcc_assert (m_stmt);
+    gcc_assert (m_stmt || m_loc != UNKNOWN_LOCATION);
   }
 
   hashval_t hash () const
@@ -1227,11 +1230,15 @@ public:
   bool operator== (const dedupe_key &other) const
   {
     return (m_sd == other.m_sd
-	    && m_stmt == other.m_stmt);
+	    && m_stmt == other.m_stmt
+	    && m_loc == other.m_loc);
   }
 
   location_t get_location () const
   {
+    if (m_loc != UNKNOWN_LOCATION)
+      return m_loc;
+    gcc_assert (m_stmt);
     return m_stmt->location;
   }
 
@@ -1260,6 +1267,7 @@ public:
 
   const saved_diagnostic &m_sd;
   const gimple *m_stmt;
+  location_t m_loc;
 };
 
 /* Traits for use by dedupe_winners.  */
@@ -1543,8 +1551,9 @@ diagnostic_manager::emit_saved_diagnostic (const exploded_graph &eg,
 
   emission_path.prepare_for_emission (sd.m_d.get ());
 
-  location_t loc
-    = get_emission_location (sd.m_stmt, sd.m_snode->m_fun, *sd.m_d);
+  location_t loc = sd.m_loc;
+  if (loc == UNKNOWN_LOCATION)
+    loc = get_emission_location (sd.m_stmt, sd.m_snode->m_fun, *sd.m_d);
 
   /* Allow the pending_diagnostic to fix up the locations of events.  */
   emission_path.fixup_locations (sd.m_d.get ());
diff --git a/gcc/analyzer/diagnostic-manager.h b/gcc/analyzer/diagnostic-manager.h
index f538d38298e..27ab9ed068e 100644
--- a/gcc/analyzer/diagnostic-manager.h
+++ b/gcc/analyzer/diagnostic-manager.h
@@ -73,6 +73,7 @@ public:
   const supernode *m_snode;
   const gimple *m_stmt;
   std::unique_ptr<stmt_finder> m_stmt_finder;
+  location_t m_loc;
   tree m_var;
   const svalue *m_sval;
   state_machine::state_t m_state;
@@ -111,15 +112,31 @@ public:
   : m_enode (enode),
     m_snode (snode),
     m_stmt (stmt),
-    m_finder (finder)
+    m_finder (finder),
+    m_loc (UNKNOWN_LOCATION)
   {
     gcc_assert (m_stmt || m_finder);
   }
 
+  /* ctor for cases where we have a location_t but there isn't any
+     gimple stmt associated with the diagnostic.  */
+
+  pending_location (exploded_node *enode,
+		    const supernode *snode,
+		    location_t loc)
+  : m_enode (enode),
+    m_snode (snode),
+    m_stmt (nullptr),
+    m_finder (nullptr),
+    m_loc (loc)
+  {
+  }
+
   exploded_node *m_enode;
   const supernode *m_snode;
   const gimple *m_stmt;
   const stmt_finder *m_finder;
+  location_t m_loc;
 };
 
 /* A class with responsibility for saving pending diagnostics, so that
-- 
2.26.3



More information about the Gcc-patches mailing list