Branch data Line data Source code
1 : : /* Interface between analyzer and frontends.
2 : : Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
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.
11 : :
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.
16 : :
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/>. */
20 : :
21 : : #include "analyzer/common.h"
22 : :
23 : : #include "diagnostic.h"
24 : : #include "stringpool.h"
25 : :
26 : : #include "analyzer/analyzer-language.h"
27 : : #include "analyzer/analyzer-logging.h"
28 : :
29 : : /* Map from identifier to INTEGER_CST. */
30 : : static GTY (()) hash_map <tree, tree> *analyzer_stashed_constants;
31 : :
32 : : #if ENABLE_ANALYZER
33 : :
34 : : namespace ana {
35 : : static vec<finish_translation_unit_callback>
36 : : *finish_translation_unit_callbacks;
37 : :
38 : : void
39 : 2 : register_finish_translation_unit_callback (
40 : : finish_translation_unit_callback callback)
41 : : {
42 : 2 : if (!finish_translation_unit_callbacks)
43 : 1 : vec_alloc (finish_translation_unit_callbacks, 1);
44 : 2 : finish_translation_unit_callbacks->safe_push (callback);
45 : 2 : }
46 : :
47 : : static void
48 : 3355 : run_callbacks (logger *logger, const translation_unit &tu)
49 : : {
50 : 3359 : for (auto const &cb : finish_translation_unit_callbacks)
51 : : {
52 : 2 : cb (logger, tu);
53 : : }
54 : 3355 : }
55 : :
56 : : /* Call into TU to try to find a value for NAME.
57 : : If found, stash its value within analyzer_stashed_constants. */
58 : :
59 : : static void
60 : 16775 : maybe_stash_named_constant (logger *logger,
61 : : const translation_unit &tu,
62 : : const char *name)
63 : : {
64 : 16775 : LOG_FUNC_1 (logger, "name: %qs", name);
65 : :
66 : 16775 : if (!analyzer_stashed_constants)
67 : 3355 : analyzer_stashed_constants = hash_map<tree, tree>::create_ggc ();
68 : :
69 : 16775 : tree id = get_identifier (name);
70 : 16775 : if (tree t = tu.lookup_constant_by_id (id))
71 : : {
72 : 78 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
73 : 78 : analyzer_stashed_constants->put (id, t);
74 : 78 : if (logger)
75 : 0 : logger->log ("%qs: %qE", name, t);
76 : : }
77 : : else
78 : : {
79 : 16697 : if (logger)
80 : 10 : logger->log ("%qs: not found", name);
81 : : }
82 : 16775 : }
83 : :
84 : : /* Call into TU to try to find values for the names we care about.
85 : : If found, stash their values within analyzer_stashed_constants. */
86 : :
87 : : static void
88 : 3355 : stash_named_constants (logger *logger, const translation_unit &tu)
89 : : {
90 : 3355 : LOG_SCOPE (logger);
91 : :
92 : : /* Stash named constants for use by sm-fd.cc */
93 : 3355 : maybe_stash_named_constant (logger, tu, "O_ACCMODE");
94 : 3355 : maybe_stash_named_constant (logger, tu, "O_RDONLY");
95 : 3355 : maybe_stash_named_constant (logger, tu, "O_WRONLY");
96 : 3355 : maybe_stash_named_constant (logger, tu, "SOCK_STREAM");
97 : 3355 : maybe_stash_named_constant (logger, tu, "SOCK_DGRAM");
98 : 3355 : }
99 : :
100 : : /* Hook for frontend to call into analyzer when TU finishes.
101 : : This exists so that the analyzer can stash named constant values from
102 : : header files (e.g. macros and enums) for later use when modeling the
103 : : behaviors of APIs.
104 : :
105 : : By doing it this way, the analyzer can use the precise values for those
106 : : constants from the user's headers, rather than attempting to model them
107 : : as properties of the target. */
108 : :
109 : : void
110 : 3355 : on_finish_translation_unit (const translation_unit &tu)
111 : : {
112 : : /* Bail if the analyzer isn't enabled. */
113 : 3355 : if (!flag_analyzer)
114 : 0 : return;
115 : :
116 : 3355 : FILE *logfile = get_or_create_any_logfile ();
117 : 3355 : log_user the_logger (NULL);
118 : 3355 : if (logfile)
119 : 2 : the_logger.set_logger (new logger (logfile, 0, 0,
120 : 2 : *global_dc->get_reference_printer ()));
121 : 3355 : stash_named_constants (the_logger.get_logger (), tu);
122 : :
123 : 3355 : run_callbacks (the_logger.get_logger (), tu);
124 : 3355 : }
125 : :
126 : : /* Lookup NAME in the named constants stashed when the frontend TU finished.
127 : : Return either an INTEGER_CST, or NULL_TREE. */
128 : :
129 : : tree
130 : 16659 : get_stashed_constant_by_name (const char *name)
131 : : {
132 : 16659 : if (!analyzer_stashed_constants)
133 : : return NULL_TREE;
134 : 16534 : tree id = get_identifier (name);
135 : 16534 : if (tree *slot = analyzer_stashed_constants->get (id))
136 : : {
137 : 97 : gcc_assert (TREE_CODE (*slot) == INTEGER_CST);
138 : : return *slot;
139 : : }
140 : : return NULL_TREE;
141 : : }
142 : :
143 : : /* Log all stashed named constants to LOGGER. */
144 : :
145 : : void
146 : 2 : log_stashed_constants (logger *logger)
147 : : {
148 : 2 : gcc_assert (logger);
149 : 2 : LOG_SCOPE (logger);
150 : 2 : if (analyzer_stashed_constants)
151 : 4 : for (auto iter : *analyzer_stashed_constants)
152 : 0 : logger->log ("%qE: %qE", iter.first, iter.second);
153 : 2 : }
154 : :
155 : : } // namespace ana
156 : :
157 : : #endif /* #if ENABLE_ANALYZER */
158 : :
159 : : #include "gt-analyzer-language.h"
|