From patchwork Wed Nov 16 22:51:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 60724 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE863396E42E for ; Wed, 16 Nov 2022 22:51:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE863396E42E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668639113; bh=uP+sSbbs1Scg85fqnbP5FkzvrkbQFlKyMqoYOwpWA20=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yv7dWaw2TvBHpkmgYQWY91PyVuc5vaITkFYgiLl7BJy1nHKbejy2QfV42YFB/6/Al U1XGU0w68FmjeyPBdMv78Ypasxv+ndNYSPIkdQJVf8pFasX8Z6/txsUfeG03VsNLhy 4KWX/1YL/5W33ryXnwFtxkQhk7wNgX8EdC9csDgE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1F7A8395444A for ; Wed, 16 Nov 2022 22:51:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1F7A8395444A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-145-9mQClkyqPBWV1_Lh4Isnuw-1; Wed, 16 Nov 2022 17:51:20 -0500 X-MC-Unique: 9mQClkyqPBWV1_Lh4Isnuw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39F578339C5 for ; Wed, 16 Nov 2022 22:51:20 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0FBD440C2086; Wed, 16 Nov 2022 22:51:19 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer: log the stashing of named constants [PR107711] Date: Wed, 16 Nov 2022 17:51:15 -0500 Message-Id: <20221116225115.2985194-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" PR analyzer/107711 seems to be a bug in how named constants are looked up by the analyzer in the C frontend. To help debug this, this patch extends -fdump-analyzer and -fdump-analyzer-stderr so that they dump this part of the analyzer's startup. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-4114-g6e4962810fe5de. gcc/analyzer/ChangeLog: PR analyzer/107711 * analyzer-language.cc: Include "diagnostic.h". (maybe_stash_named_constant): Add logger param and use it to log the name being looked up, and the result. (stash_named_constants): New, splitting out from... (on_finish_translation_unit): ...this function. Call get_or_create_logfile and use the result to create a logger instance, passing it to stash_named_constants. * analyzer.h (get_or_create_any_logfile): New decl. * engine.cc (dump_fout, owns_dump_fout): New globals, split out from run_checkers. (get_or_create_any_logfile): New function, split out from... (run_checkers): ...here, so that the logfile can be opened by on_finish_translation_unit. Clear the globals when closing the dump file. gcc/testsuite/ChangeLog: PR analyzer/107711 * gcc.dg/analyzer/fdump-analyzer-1.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/analyzer-language.cc | 42 +++++++++++++--- gcc/analyzer/analyzer.h | 2 + gcc/analyzer/engine.cc | 50 +++++++++++++------ .../gcc.dg/analyzer/fdump-analyzer-1.c | 14 ++++++ 4 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/fdump-analyzer-1.c diff --git a/gcc/analyzer/analyzer-language.cc b/gcc/analyzer/analyzer-language.cc index 0629b681e7c..0fb43443ffd 100644 --- a/gcc/analyzer/analyzer-language.cc +++ b/gcc/analyzer/analyzer-language.cc @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/analyzer.h" #include "analyzer/analyzer-language.h" #include "analyzer/analyzer-logging.h" +#include "diagnostic.h" /* Map from identifier to INTEGER_CST. */ static GTY (()) hash_map *analyzer_stashed_constants; @@ -39,8 +40,12 @@ namespace ana { If found, stash its value within analyzer_stashed_constants. */ static void -maybe_stash_named_constant (const translation_unit &tu, const char *name) +maybe_stash_named_constant (logger *logger, + const translation_unit &tu, + const char *name) { + LOG_FUNC_1 (logger, "name: %qs", name); + if (!analyzer_stashed_constants) analyzer_stashed_constants = hash_map::create_ggc (); @@ -49,9 +54,32 @@ maybe_stash_named_constant (const translation_unit &tu, const char *name) { gcc_assert (TREE_CODE (t) == INTEGER_CST); analyzer_stashed_constants->put (id, t); + if (logger) + logger->log ("%qs: %qE", name, t); + } + else + { + if (logger) + logger->log ("%qs: not found", name); } } +/* Call into TU to try to find values for the names we care about. + If found, stash their values within analyzer_stashed_constants. */ + +static void +stash_named_constants (logger *logger, const translation_unit &tu) +{ + LOG_SCOPE (logger); + + /* Stash named constants for use by sm-fd.cc */ + maybe_stash_named_constant (logger, tu, "O_ACCMODE"); + maybe_stash_named_constant (logger, tu, "O_RDONLY"); + maybe_stash_named_constant (logger, tu, "O_WRONLY"); + maybe_stash_named_constant (logger, tu, "SOCK_STREAM"); + maybe_stash_named_constant (logger, tu, "SOCK_DGRAM"); +} + /* Hook for frontend to call into analyzer when TU finishes. This exists so that the analyzer can stash named constant values from header files (e.g. macros and enums) for later use when modeling the @@ -68,12 +96,12 @@ on_finish_translation_unit (const translation_unit &tu) if (!flag_analyzer) return; - /* Stash named constants for use by sm-fd.cc */ - maybe_stash_named_constant (tu, "O_ACCMODE"); - maybe_stash_named_constant (tu, "O_RDONLY"); - maybe_stash_named_constant (tu, "O_WRONLY"); - maybe_stash_named_constant (tu, "SOCK_STREAM"); - maybe_stash_named_constant (tu, "SOCK_DGRAM"); + FILE *logfile = get_or_create_any_logfile (); + log_user the_logger (NULL); + if (logfile) + the_logger.set_logger (new logger (logfile, 0, 0, + *global_dc->printer)); + stash_named_constants (the_logger.get_logger (), tu); } /* Lookup NAME in the named constants stashed when the frontend TU finished. diff --git a/gcc/analyzer/analyzer.h b/gcc/analyzer/analyzer.h index 99a1d0690d5..1b56745aba2 100644 --- a/gcc/analyzer/analyzer.h +++ b/gcc/analyzer/analyzer.h @@ -324,6 +324,8 @@ public: extern tree get_stashed_constant_by_name (const char *name); extern void log_stashed_constants (logger *logger); +extern FILE *get_or_create_any_logfile (); + } // namespace ana extern bool is_special_named_call_p (const gcall *call, const char *funcname, diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc index fe2b9c69221..b52753da793 100644 --- a/gcc/analyzer/engine.cc +++ b/gcc/analyzer/engine.cc @@ -6144,6 +6144,34 @@ impl_run_checkers (logger *logger) delete purge_map; } +/* Handle -fdump-analyzer and -fdump-analyzer-stderr. */ +static FILE *dump_fout = NULL; + +/* Track if we're responsible for closing dump_fout. */ +static bool owns_dump_fout = false; + +/* If dumping is enabled, attempt to create dump_fout if it hasn't already + been opened. Return it. */ + +FILE * +get_or_create_any_logfile () +{ + if (!dump_fout) + { + if (flag_dump_analyzer_stderr) + dump_fout = stderr; + else if (flag_dump_analyzer) + { + char *dump_filename = concat (dump_base_name, ".analyzer.txt", NULL); + dump_fout = fopen (dump_filename, "w"); + free (dump_filename); + if (dump_fout) + owns_dump_fout = true; + } + } + return dump_fout; +} + /* External entrypoint to the analysis "engine". Set up any dumps, then call impl_run_checkers. */ @@ -6153,23 +6181,9 @@ run_checkers () /* Save input_location. */ location_t saved_input_location = input_location; - /* Handle -fdump-analyzer and -fdump-analyzer-stderr. */ - FILE *dump_fout = NULL; - /* Track if we're responsible for closing dump_fout. */ - bool owns_dump_fout = false; - if (flag_dump_analyzer_stderr) - dump_fout = stderr; - else if (flag_dump_analyzer) - { - char *dump_filename = concat (dump_base_name, ".analyzer.txt", NULL); - dump_fout = fopen (dump_filename, "w"); - free (dump_filename); - if (dump_fout) - owns_dump_fout = true; - } - { log_user the_logger (NULL); + get_or_create_any_logfile (); if (dump_fout) the_logger.set_logger (new logger (dump_fout, 0, 0, *global_dc->printer)); @@ -6182,7 +6196,11 @@ run_checkers () } if (owns_dump_fout) - fclose (dump_fout); + { + fclose (dump_fout); + owns_dump_fout = false; + dump_fout = NULL; + } /* Restore input_location. Subsequent passes may assume that input_location is some arbitrary value *not* in the block tree, which might be violated diff --git a/gcc/testsuite/gcc.dg/analyzer/fdump-analyzer-1.c b/gcc/testsuite/gcc.dg/analyzer/fdump-analyzer-1.c new file mode 100644 index 00000000000..8962adbf79e --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fdump-analyzer-1.c @@ -0,0 +1,14 @@ +/* { dg-additional-options "-fdump-analyzer" } */ + +void test (void) +{ +} + +/* Verify that we log the named constants that we look up in the TU + (before the analysis pass runs). + { dg-final { scan-file fdump-analyzer-1.c.analyzer.txt "maybe_stash_named_constant: name: 'O_ACCMODE'" } } + + Verify that we log the main part of the analysis (part of the + analysis pass): + { dg-final { scan-file fdump-analyzer-1.c.analyzer.txt "ana::run_checkers" } } + */