From patchwork Fri Dec 13 18:11:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1209378 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-515960-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Qnj4iXI5"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Kj9CKeEY"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47ZJxh2GxHz9sPJ for ; Sat, 14 Dec 2019 05:25:28 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=Yb1Md08XsHNcLIlwlhmCWM3tlxKA49d8TB+VMuwSU10tPKIqL7G3L zUVq/MpmcyvM/NnG4H5jb2eIyIXNWXnQpnrA7Yi7Id4c/Lavw7iH2R6ZPEhcxeiI P0LIB71Wvoe+gH6VAXUj18TM6deHcRoPSdmk0/I9eomB3px4Drhlas= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=default; bh=6ZQ9m3LPGbD03vBdOxo46TDdj+o=; b=Qnj4iXI5bpMnX8gHHfMKtquI7V8B CzwWwW4YDSeZzC66ElfD6DRBWedcNQUPZmYzEB7X3/WfZ0bELYILA2fPnE8XoQgX DRXT9+OrV4exJR1UegSLd7viI2PBKt7+IFioETc4vhBJCjGy2QCXba+TPV8/CtdL B/2vYn7NvGwBTwk= Received: (qmail 129892 invoked by alias); 13 Dec 2019 18:15:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 108852 invoked by uid 89); 13 Dec 2019 18:13:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Dec 2019 18:13:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576260793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8J4eok/ZiFiaHI6oTC3h0MK/iR4fXgdv57B2jIEE5mI=; b=Kj9CKeEYAE4WY1IkdG8mT1k9RL4wGt2F9qPaN5CQpPasagRB4k0pzu35YbvkLKrQN4Qno9 8LfZKiqZ+YGpoCmoOzDm1WJ+SiOIR9jioGhMqFKiTWanMJfAN+of+U2SGXhDNo3QDtU0Nr 7MfvlaD5vhUBfdCk8gVh3JHThK1Svss= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-biYgTVJYNQ2dJWv7vlj2xA-1; Fri, 13 Dec 2019 13:11:58 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ACFB38017DF for ; Fri, 13 Dec 2019 18:11:57 +0000 (UTC) Received: from t470.redhat.com (ovpn-117-164.phx2.redhat.com [10.3.117.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B6595C219; Fri, 13 Dec 2019 18:11:57 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 31/45] analyzer: new file: sm-pattern-test.cc Date: Fri, 13 Dec 2019 13:11:20 -0500 Message-Id: <20191213181134.1830-32-dmalcolm@redhat.com> In-Reply-To: <20191213181134.1830-1-dmalcolm@redhat.com> References: <20191213181134.1830-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes Changed in v4: - Remove include of gcc-plugin.h, reworking includes accordingly. - Wrap everything in #if ENABLE_ANALYZER - Remove /// comment lines - Rework on_leak vfunc: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg02028.html This patch adds a custom state machine checker intended purely for DejaGnu testing of the sm "machinery". gcc/ChangeLog: * analyzer/sm-pattern-test.cc: New file. --- gcc/analyzer/sm-pattern-test.cc | 149 ++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 gcc/analyzer/sm-pattern-test.cc diff --git a/gcc/analyzer/sm-pattern-test.cc b/gcc/analyzer/sm-pattern-test.cc new file mode 100644 index 000000000000..f7fc21f04049 --- /dev/null +++ b/gcc/analyzer/sm-pattern-test.cc @@ -0,0 +1,149 @@ +/* A state machine for use in DejaGnu tests, to check that + pattern-matching works as expected. + + Copyright (C) 2019 Free Software Foundation, Inc. + Contributed by David Malcolm . + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#include "system.h" +#include "coretypes.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 "analyzer/analyzer.h" +#include "analyzer/pending-diagnostic.h" +#include "analyzer/sm.h" + +#if ENABLE_ANALYZER + +namespace { + +/* A state machine for use in DejaGnu tests, to check that + pattern-matching works as expected. */ + +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 on_stmt (sm_context *sm_ctxt, + const supernode *node, + const gimple *stmt) const FINAL OVERRIDE; + + void on_condition (sm_context *sm_ctxt, + const supernode *node, + const gimple *stmt, + tree lhs, + enum tree_code op, + tree rhs) const FINAL OVERRIDE; + + bool can_purge_p (state_t s) const FINAL OVERRIDE; + +private: + state_t m_start; +}; + +class pattern_match : public pending_diagnostic_subclass +{ +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"; } + + bool operator== (const pattern_match &other) const + { + return (m_lhs == other.m_lhs + && m_op == other.m_op + && m_rhs == other.m_rhs); + } + + bool emit (rich_location *rich_loc) FINAL OVERRIDE + { + return warning_at (rich_loc, 0, "pattern match on %<%E %s %E%>", + m_lhs, op_symbol_code (m_op), m_rhs); + } + +private: + tree m_lhs; + enum tree_code m_op; + tree m_rhs; +}; + +pattern_test_state_machine::pattern_test_state_machine (logger *logger) +: state_machine ("pattern-test", logger) +{ + m_start = add_state ("start"); +} + +bool +pattern_test_state_machine::on_stmt (sm_context *sm_ctxt ATTRIBUTE_UNUSED, + const supernode *node ATTRIBUTE_UNUSED, + const gimple *stmt ATTRIBUTE_UNUSED) const +{ + return false; +} + +/* Implementation of state_machine::on_condition vfunc for + pattern_test_state_machine. + + Queue a pattern_match diagnostic for any comparison against a + constant. */ + +void +pattern_test_state_machine::on_condition (sm_context *sm_ctxt, + const supernode *node, + const gimple *stmt, + tree lhs, + enum tree_code op, + tree rhs) const +{ + if (stmt == NULL) + return; + + if (!CONSTANT_CLASS_P (rhs)) + return; + + pending_diagnostic *diag = new pattern_match (lhs, op, rhs); + sm_ctxt->warn_for_state (node, stmt, lhs, m_start, diag); +} + +bool +pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const +{ + return true; +} + +} // anonymous namespace + +/* Internal interface to this file. */ + +state_machine * +make_pattern_test_state_machine (logger *logger) +{ + return new pattern_test_state_machine (logger); +} + +#endif /* #if ENABLE_ANALYZER */