]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/analyzer/sm-pattern-test.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / analyzer / sm-pattern-test.cc
index 571e13eb47cde53491028eb1f4facaeaf9908ab3..866e07b43fb8dc9baddb8f73834758fe08b2ed7e 100644 (file)
@@ -1,7 +1,7 @@
 /* A state machine for use in DejaGnu tests, to check that
    pattern-matching works as expected.
 
-   Copyright (C) 2019-2020 Free Software Foundation, Inc.
+   Copyright (C) 2019-2024 Free Software Foundation, Inc.
    Contributed by David Malcolm <dmalcolm@redhat.com>.
 
 This file is part of GCC.
@@ -21,24 +21,30 @@ along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
+#define INCLUDE_MEMORY
 #include "system.h"
 #include "coretypes.h"
+#include "make-unique.h"
 #include "tree.h"
 #include "function.h"
 #include "basic-block.h"
 #include "gimple.h"
 #include "tree-pretty-print.h"
 #include "diagnostic-path.h"
-#include "diagnostic-metadata.h"
-#include "function.h"
 #include "analyzer/analyzer.h"
 #include "diagnostic-event-id.h"
 #include "analyzer/analyzer-logging.h"
 #include "analyzer/sm.h"
 #include "analyzer/pending-diagnostic.h"
+#include "analyzer/call-string.h"
+#include "analyzer/program-point.h"
+#include "analyzer/store.h"
+#include "analyzer/region-model.h"
 
 #if ENABLE_ANALYZER
 
+namespace ana {
+
 namespace {
 
 /* A state machine for use in DejaGnu tests, to check that
@@ -49,23 +55,20 @@ class pattern_test_state_machine : public state_machine
 public:
   pattern_test_state_machine (logger *logger);
 
-  bool inherited_state_p () const FINAL OVERRIDE { return false; }
+  bool inherited_state_p () const final override { return false; }
 
   bool on_stmt (sm_context *sm_ctxt,
                const supernode *node,
-               const gimple *stmt) const FINAL OVERRIDE;
+               const gimple *stmt) const final override;
 
   void on_condition (sm_context *sm_ctxt,
                     const supernode *node,
                     const gimple *stmt,
-                    tree lhs,
+                    const svalue *lhs,
                     enum tree_code op,
-                    tree rhs) const FINAL OVERRIDE;
+                    const svalue *rhs) const final override;
 
-  bool can_purge_p (state_t s) const FINAL OVERRIDE;
-
-private:
-  state_t m_start;
+  bool can_purge_p (state_t s) const final override;
 };
 
 class pattern_match : public pending_diagnostic_subclass<pattern_match>
@@ -74,7 +77,7 @@ public:
   pattern_match (tree lhs, enum tree_code op, tree rhs)
   : m_lhs (lhs), m_op (op), m_rhs (rhs) {}
 
-  const char *get_kind () const FINAL OVERRIDE { return "pattern_match"; }
+  const char *get_kind () const final override { return "pattern_match"; }
 
   bool operator== (const pattern_match &other) const
   {
@@ -83,10 +86,15 @@ public:
            && same_tree_p (m_rhs, other.m_rhs));
   }
 
-  bool emit (rich_location *rich_loc) FINAL OVERRIDE
+  int get_controlling_option () const final override
   {
-    return warning_at (rich_loc, 0, "pattern match on %<%E %s %E%>",
-                      m_lhs, op_symbol_code (m_op), m_rhs);
+    return 0;
+  }
+
+  bool emit (diagnostic_emission_context &ctxt) final override
+  {
+    return ctxt.warn ("pattern match on %<%E %s %E%>",
+                     m_lhs, op_symbol_code (m_op), m_rhs);
   }
 
 private:
@@ -98,7 +106,6 @@ private:
 pattern_test_state_machine::pattern_test_state_machine (logger *logger)
 : state_machine ("pattern-test", logger)
 {
-  m_start = add_state ("start");
 }
 
 bool
@@ -119,18 +126,22 @@ void
 pattern_test_state_machine::on_condition (sm_context *sm_ctxt,
                                          const supernode *node,
                                          const gimple *stmt,
-                                         tree lhs,
+                                         const svalue *lhs,
                                          enum tree_code op,
-                                         tree rhs) const
+                                         const svalue *rhs) const
 {
   if (stmt == NULL)
     return;
 
-  if (!CONSTANT_CLASS_P (rhs))
+  tree rhs_cst = rhs->maybe_get_constant ();
+  if (!rhs_cst)
     return;
 
-  pending_diagnostic *diag = new pattern_match (lhs, op, rhs);
-  sm_ctxt->warn_for_state (node, stmt, lhs, m_start, diag);
+  if (tree lhs_expr = sm_ctxt->get_diagnostic_tree (lhs))
+    {
+      sm_ctxt->warn (node, stmt, lhs_expr,
+                    make_unique<pattern_match> (lhs_expr, op, rhs_cst));
+    }
 }
 
 bool
@@ -149,4 +160,6 @@ make_pattern_test_state_machine (logger *logger)
   return new pattern_test_state_machine (logger);
 }
 
+} // namespace ana
+
 #endif /* #if ENABLE_ANALYZER */