d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / analyzer / analyzer-language.cc
blob2c8910906eeb84c2021c94a20e3a3b1b5fc53c56
1 /* Interface between analyzer and frontends.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "analyzer/analyzer.h"
28 #include "analyzer/analyzer-language.h"
29 #include "analyzer/analyzer-logging.h"
30 #include "diagnostic.h"
32 /* Map from identifier to INTEGER_CST. */
33 static GTY (()) hash_map <tree, tree> *analyzer_stashed_constants;
35 #if ENABLE_ANALYZER
37 namespace ana {
39 /* Call into TU to try to find a value for NAME.
40 If found, stash its value within analyzer_stashed_constants. */
42 static void
43 maybe_stash_named_constant (logger *logger,
44 const translation_unit &tu,
45 const char *name)
47 LOG_FUNC_1 (logger, "name: %qs", name);
49 if (!analyzer_stashed_constants)
50 analyzer_stashed_constants = hash_map<tree, tree>::create_ggc ();
52 tree id = get_identifier (name);
53 if (tree t = tu.lookup_constant_by_id (id))
55 gcc_assert (TREE_CODE (t) == INTEGER_CST);
56 analyzer_stashed_constants->put (id, t);
57 if (logger)
58 logger->log ("%qs: %qE", name, t);
60 else
62 if (logger)
63 logger->log ("%qs: not found", name);
67 /* Call into TU to try to find values for the names we care about.
68 If found, stash their values within analyzer_stashed_constants. */
70 static void
71 stash_named_constants (logger *logger, const translation_unit &tu)
73 LOG_SCOPE (logger);
75 /* Stash named constants for use by sm-fd.cc */
76 maybe_stash_named_constant (logger, tu, "O_ACCMODE");
77 maybe_stash_named_constant (logger, tu, "O_RDONLY");
78 maybe_stash_named_constant (logger, tu, "O_WRONLY");
79 maybe_stash_named_constant (logger, tu, "SOCK_STREAM");
80 maybe_stash_named_constant (logger, tu, "SOCK_DGRAM");
83 /* Hook for frontend to call into analyzer when TU finishes.
84 This exists so that the analyzer can stash named constant values from
85 header files (e.g. macros and enums) for later use when modeling the
86 behaviors of APIs.
88 By doing it this way, the analyzer can use the precise values for those
89 constants from the user's headers, rather than attempting to model them
90 as properties of the target. */
92 void
93 on_finish_translation_unit (const translation_unit &tu)
95 /* Bail if the analyzer isn't enabled. */
96 if (!flag_analyzer)
97 return;
99 FILE *logfile = get_or_create_any_logfile ();
100 log_user the_logger (NULL);
101 if (logfile)
102 the_logger.set_logger (new logger (logfile, 0, 0,
103 *global_dc->printer));
104 stash_named_constants (the_logger.get_logger (), tu);
107 /* Lookup NAME in the named constants stashed when the frontend TU finished.
108 Return either an INTEGER_CST, or NULL_TREE. */
110 tree
111 get_stashed_constant_by_name (const char *name)
113 if (!analyzer_stashed_constants)
114 return NULL_TREE;
115 tree id = get_identifier (name);
116 if (tree *slot = analyzer_stashed_constants->get (id))
118 gcc_assert (TREE_CODE (*slot) == INTEGER_CST);
119 return *slot;
121 return NULL_TREE;
124 /* Log all stashed named constants to LOGGER. */
126 void
127 log_stashed_constants (logger *logger)
129 gcc_assert (logger);
130 LOG_SCOPE (logger);
131 if (analyzer_stashed_constants)
132 for (auto iter : *analyzer_stashed_constants)
133 logger->log ("%qE: %qE", iter.first, iter.second);
136 } // namespace ana
138 #endif /* #if ENABLE_ANALYZER */
140 #include "gt-analyzer-language.h"