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 549835324 : 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 549835324 : switch (opt)
41 : {
42 62107 : case no_warning:
43 62107 : m_bits = 0;
44 62107 : break;
45 :
46 28065588 : case all_warnings:
47 28065588 : m_bits = -1;
48 28065588 : break;
49 :
50 : /* Flow-sensitive warnings about pointer problems issued by both
51 : front ends and the middle end. */
52 321511 : case OPT_Waddress:
53 321511 : case OPT_Wnonnull:
54 321511 : m_bits = NW_NONNULL;
55 321511 : break;
56 :
57 : /* Flow-sensitive warnings about arithmetic overflow issued by both
58 : front ends and the middle end. */
59 681737 : case OPT_Woverflow:
60 681737 : case OPT_Wshift_count_negative:
61 681737 : case OPT_Wshift_count_overflow:
62 681737 : m_bits = NW_VFLOW;
63 681737 : break;
64 :
65 : /* Lexical warnings issued by front ends. */
66 142199125 : case OPT_Wabi:
67 142199125 : case OPT_Wlogical_op:
68 142199125 : case OPT_Wparentheses:
69 142199125 : case OPT_Wreturn_type:
70 142199125 : case OPT_Wsizeof_array_div:
71 142199125 : case OPT_Wstrict_aliasing:
72 142199125 : case OPT_Wunused:
73 142199125 : case OPT_Wunused_function:
74 142199125 : case OPT_Wunused_but_set_variable_:
75 142199125 : case OPT_Wunused_variable:
76 142199125 : case OPT_Wunused_but_set_parameter_:
77 142199125 : m_bits = NW_LEXICAL;
78 142199125 : break;
79 :
80 : /* Access warning group. */
81 799759 : case OPT_Warray_bounds_:
82 799759 : case OPT_Wformat_overflow_:
83 799759 : case OPT_Wformat_truncation_:
84 799759 : case OPT_Wrestrict:
85 799759 : case OPT_Wsizeof_pointer_memaccess:
86 799759 : case OPT_Wstrict_aliasing_:
87 799759 : case OPT_Wstringop_overflow_:
88 799759 : case OPT_Wstringop_overread:
89 799759 : case OPT_Wstringop_truncation:
90 799759 : m_bits = NW_ACCESS;
91 799759 : break;
92 :
93 : /* Initialization warning group. */
94 2056772 : case OPT_Winit_self:
95 2056772 : case OPT_Wuninitialized:
96 2056772 : case OPT_Wmaybe_uninitialized:
97 2056772 : m_bits = NW_UNINIT;
98 2056772 : break;
99 :
100 923443 : case OPT_Wdangling_pointer_:
101 923443 : case OPT_Wreturn_local_addr:
102 923443 : case OPT_Wuse_after_free_:
103 923443 : m_bits = NW_DANGLING;
104 923443 : break;
105 :
106 371754050 : case OPT_Wpessimizing_move:
107 371754050 : case OPT_Wredundant_move:
108 371754050 : m_bits = NW_REDUNDANT;
109 371754050 : break;
110 :
111 2971232 : default:
112 : /* A catchall group for everything else. */
113 2971232 : m_bits = NW_OTHER;
114 : }
115 549835324 : }
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 183240 : warning_suppressed_at (location_t loc, opt_code opt /* = all_warnings */)
126 : {
127 183240 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
128 :
129 183240 : if (!nowarn_map)
130 : return false;
131 :
132 183047 : if (const nowarn_spec_t* const pspec = nowarn_map->get (loc))
133 : {
134 26467 : const nowarn_spec_t optspec (opt);
135 26467 : 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 405768158 : suppress_warning_at (location_t loc, opt_code opt /* = all_warnings */,
149 : bool supp /* = true */)
150 : {
151 405768158 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
152 :
153 405830265 : const nowarn_spec_t optspec (supp ? opt : opt_code ());
154 :
155 405768158 : if (nowarn_spec_t *pspec = nowarn_map ? nowarn_map->get (loc) : nullptr)
156 : {
157 119605931 : if (supp)
158 : {
159 119602946 : *pspec |= optspec;
160 119602946 : 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 286162227 : if (!supp || opt == no_warning)
172 : return false;
173 :
174 286103105 : if (!nowarn_map)
175 190412 : nowarn_map = nowarn_map_t::create_ggc (32);
176 :
177 286103105 : nowarn_map->put (loc, optspec);
178 286103105 : return true;
179 : }
180 :
181 : /* Change the warning disposition for LOC to match OPTSPEC. */
182 :
183 : void
184 484385 : put_warning_spec_at (location_t loc, unsigned bits)
185 : {
186 484385 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
187 :
188 484385 : nowarn_spec_t optspec = nowarn_spec_t::from_bits (bits);
189 484385 : if (!optspec)
190 : {
191 195776 : if (nowarn_map)
192 195625 : nowarn_map->remove (loc);
193 : }
194 : else
195 : {
196 288609 : if (!nowarn_map)
197 213 : nowarn_map = nowarn_map_t::create_ggc (32);
198 288609 : nowarn_map->put (loc, optspec);
199 : }
200 484385 : }
201 :
202 : /* Copy the no-warning disposition from one location to another. */
203 :
204 : void
205 257137059 : copy_warning (location_t to, location_t from)
206 : {
207 257137059 : if (!nowarn_map)
208 : return;
209 :
210 243735508 : nowarn_spec_t *from_spec;
211 243735508 : if (RESERVED_LOCATION_P (from))
212 : from_spec = nullptr;
213 : else
214 222207250 : from_spec = nowarn_map->get (from);
215 243735508 : 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 222822333 : if (from_spec)
222 : {
223 65856679 : nowarn_spec_t tem = *from_spec;
224 65856679 : nowarn_map->put (to, tem);
225 : }
226 : else
227 156965654 : nowarn_map->remove (to);
228 : }
229 : }
|