From 6e4962810fe5de95b807d1ac675df45a725e31a2 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 16 Nov 2022 17:38:24 -0500 Subject: analyzer: log the stashing of named constants [PR107711] 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. 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 ++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'gcc/analyzer/analyzer-language.cc') diff --git a/gcc/analyzer/analyzer-language.cc b/gcc/analyzer/analyzer-language.cc index 0629b68..0fb4344 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. -- cgit v1.1