Line data Source code
1 : /* Functions to enable and disable individual warnings on an expression
2 : and statement basis.
3 : Copyright (C) 2021-2026 Free Software Foundation, Inc.
4 : Contributed by Martin Sebor <msebor@redhat.com>
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify it under
9 : the terms of the GNU General Public License as published by the Free
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
13 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "bitmap.h"
27 : #include "tree.h"
28 : #include "cgraph.h"
29 : #include "hash-map.h"
30 : #include "gcc-diagnostic-spec.h"
31 : #include "pretty-print.h"
32 : #include "options.h"
33 :
34 : /* Initialize *THIS from warning option OPT. */
35 :
36 550015227 : nowarn_spec_t::nowarn_spec_t (opt_code opt)
37 : {
38 : /* Create a very simple mapping based on testing and experience.
39 : It should become more refined with time. */
40 550015227 : switch (opt)
41 : {
42 62014 : case no_warning:
43 62014 : m_bits = 0;
44 62014 : break;
45 :
46 28040811 : case all_warnings:
47 28040811 : m_bits = -1;
48 28040811 : break;
49 :
50 : /* Flow-sensitive warnings about pointer problems issued by both
51 : front ends and the middle end. */
52 323411 : case OPT_Waddress:
53 323411 : case OPT_Wnonnull:
54 323411 : m_bits = NW_NONNULL;
55 323411 : break;
56 :
57 : /* Flow-sensitive warnings about arithmetic overflow issued by both
58 : front ends and the middle end. */
59 679609 : case OPT_Woverflow:
60 679609 : case OPT_Wshift_count_negative:
61 679609 : case OPT_Wshift_count_overflow:
62 679609 : m_bits = NW_VFLOW;
63 679609 : break;
64 :
65 : /* Lexical warnings issued by front ends. */
66 142271717 : case OPT_Wabi:
67 142271717 : case OPT_Wlogical_op:
68 142271717 : case OPT_Wparentheses:
69 142271717 : case OPT_Wreturn_type:
70 142271717 : case OPT_Wsizeof_array_div:
71 142271717 : case OPT_Wstrict_aliasing:
72 142271717 : case OPT_Wunused:
73 142271717 : case OPT_Wunused_function:
74 142271717 : case OPT_Wunused_but_set_variable_:
75 142271717 : case OPT_Wunused_variable:
76 142271717 : case OPT_Wunused_but_set_parameter_:
77 142271717 : m_bits = NW_LEXICAL;
78 142271717 : break;
79 :
80 : /* Access warning group. */
81 804530 : case OPT_Warray_bounds_:
82 804530 : case OPT_Wformat_overflow_:
83 804530 : case OPT_Wformat_truncation_:
84 804530 : case OPT_Wrestrict:
85 804530 : case OPT_Wsizeof_pointer_memaccess:
86 804530 : case OPT_Wstrict_aliasing_:
87 804530 : case OPT_Wstringop_overflow_:
88 804530 : case OPT_Wstringop_overread:
89 804530 : case OPT_Wstringop_truncation:
90 804530 : m_bits = NW_ACCESS;
91 804530 : break;
92 :
93 : /* Initialization warning group. */
94 2050747 : case OPT_Winit_self:
95 2050747 : case OPT_Wuninitialized:
96 2050747 : case OPT_Wmaybe_uninitialized:
97 2050747 : m_bits = NW_UNINIT;
98 2050747 : break;
99 :
100 923794 : case OPT_Wdangling_pointer_:
101 923794 : case OPT_Wreturn_local_addr:
102 923794 : case OPT_Wuse_after_free_:
103 923794 : m_bits = NW_DANGLING;
104 923794 : break;
105 :
106 371886531 : case OPT_Wpessimizing_move:
107 371886531 : case OPT_Wredundant_move:
108 371886531 : m_bits = NW_REDUNDANT;
109 371886531 : break;
110 :
111 2972063 : default:
112 : /* A catchall group for everything else. */
113 2972063 : m_bits = NW_OTHER;
114 : }
115 550015227 : }
116 :
117 : /* A mapping from a 'location_t' to the warning spec set for it. */
118 :
119 : GTY(()) nowarn_map_t *nowarn_map;
120 :
121 : /* Return the no-warning disposition for location LOC and option OPT
122 : or for all/any options by default. */
123 :
124 : bool
125 182304 : warning_suppressed_at (location_t loc, opt_code opt /* = all_warnings */)
126 : {
127 182304 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
128 :
129 182304 : if (!nowarn_map)
130 : return false;
131 :
132 182111 : if (const nowarn_spec_t* const pspec = nowarn_map->get (loc))
133 : {
134 26461 : const nowarn_spec_t optspec (opt);
135 26461 : return *pspec & optspec;
136 : }
137 :
138 : return false;
139 : }
140 :
141 : /* Change the suppression of warnings for location LOC.
142 : OPT controls which warnings are affected.
143 : The wildcard OPT of -1 controls all warnings.
144 : If SUPP is true (the default), enable the suppression of the warnings.
145 : If SUPP is false, disable the suppression of the warnings. */
146 :
147 : bool
148 405956800 : suppress_warning_at (location_t loc, opt_code opt /* = all_warnings */,
149 : bool supp /* = true */)
150 : {
151 405956800 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
152 :
153 406018814 : const nowarn_spec_t optspec (supp ? opt : opt_code ());
154 :
155 405956800 : if (nowarn_spec_t *pspec = nowarn_map ? nowarn_map->get (loc) : nullptr)
156 : {
157 119567462 : if (supp)
158 : {
159 119564477 : *pspec |= optspec;
160 119564477 : return true;
161 : }
162 :
163 2985 : *pspec &= optspec;
164 2985 : if (*pspec)
165 : return true;
166 :
167 2985 : nowarn_map->remove (loc);
168 2985 : return false;
169 : }
170 :
171 286389338 : if (!supp || opt == no_warning)
172 : return false;
173 :
174 286330309 : if (!nowarn_map)
175 187771 : nowarn_map = nowarn_map_t::create_ggc (32);
176 :
177 286330309 : nowarn_map->put (loc, optspec);
178 286330309 : return true;
179 : }
180 :
181 : /* Change the warning disposition for LOC to match OPTSPEC. */
182 :
183 : void
184 484460 : put_warning_spec_at (location_t loc, unsigned bits)
185 : {
186 484460 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
187 :
188 484460 : nowarn_spec_t optspec = nowarn_spec_t::from_bits (bits);
189 484460 : if (!optspec)
190 : {
191 195726 : if (nowarn_map)
192 195575 : nowarn_map->remove (loc);
193 : }
194 : else
195 : {
196 288734 : if (!nowarn_map)
197 213 : nowarn_map = nowarn_map_t::create_ggc (32);
198 288734 : nowarn_map->put (loc, optspec);
199 : }
200 484460 : }
201 :
202 : /* Copy the no-warning disposition from one location to another. */
203 :
204 : void
205 256341756 : copy_warning (location_t to, location_t from)
206 : {
207 256341756 : if (!nowarn_map)
208 : return;
209 :
210 243039913 : nowarn_spec_t *from_spec;
211 243039913 : if (RESERVED_LOCATION_P (from))
212 : from_spec = nullptr;
213 : else
214 221612393 : from_spec = nowarn_map->get (from);
215 243039913 : if (RESERVED_LOCATION_P (to))
216 : /* We cannot set no-warning dispositions for 'to', so we have no chance but
217 : lose those potentially set for 'from'. */
218 : ;
219 : else
220 : {
221 222230388 : if (from_spec)
222 : {
223 65552360 : nowarn_spec_t tem = *from_spec;
224 65552360 : nowarn_map->put (to, tem);
225 : }
226 : else
227 156678028 : nowarn_map->remove (to);
228 : }
229 : }
|