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 517461532 : 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 517461532 : switch (opt)
41 : {
42 61712 : case no_warning:
43 61712 : m_bits = 0;
44 61712 : break;
45 :
46 26524288 : case all_warnings:
47 26524288 : m_bits = -1;
48 26524288 : break;
49 :
50 : /* Flow-sensitive warnings about pointer problems issued by both
51 : front ends and the middle end. */
52 310757 : case OPT_Waddress:
53 310757 : case OPT_Wnonnull:
54 310757 : m_bits = NW_NONNULL;
55 310757 : break;
56 :
57 : /* Flow-sensitive warnings about arithmetic overflow issued by both
58 : front ends and the middle end. */
59 8008558 : case OPT_Woverflow:
60 8008558 : case OPT_Wshift_count_negative:
61 8008558 : case OPT_Wshift_count_overflow:
62 8008558 : case OPT_Wstrict_overflow:
63 8008558 : m_bits = NW_VFLOW;
64 8008558 : break;
65 :
66 : /* Lexical warnings issued by front ends. */
67 136043661 : case OPT_Wabi:
68 136043661 : case OPT_Wlogical_op:
69 136043661 : case OPT_Wparentheses:
70 136043661 : case OPT_Wreturn_type:
71 136043661 : case OPT_Wsizeof_array_div:
72 136043661 : case OPT_Wstrict_aliasing:
73 136043661 : case OPT_Wunused:
74 136043661 : case OPT_Wunused_function:
75 136043661 : case OPT_Wunused_but_set_variable_:
76 136043661 : case OPT_Wunused_variable:
77 136043661 : case OPT_Wunused_but_set_parameter_:
78 136043661 : m_bits = NW_LEXICAL;
79 136043661 : break;
80 :
81 : /* Access warning group. */
82 759969 : case OPT_Warray_bounds_:
83 759969 : case OPT_Wformat_overflow_:
84 759969 : case OPT_Wformat_truncation_:
85 759969 : case OPT_Wrestrict:
86 759969 : case OPT_Wsizeof_pointer_memaccess:
87 759969 : case OPT_Wstrict_aliasing_:
88 759969 : case OPT_Wstringop_overflow_:
89 759969 : case OPT_Wstringop_overread:
90 759969 : case OPT_Wstringop_truncation:
91 759969 : m_bits = NW_ACCESS;
92 759969 : break;
93 :
94 : /* Initialization warning group. */
95 1877129 : case OPT_Winit_self:
96 1877129 : case OPT_Wuninitialized:
97 1877129 : case OPT_Wmaybe_uninitialized:
98 1877129 : m_bits = NW_UNINIT;
99 1877129 : break;
100 :
101 855475 : case OPT_Wdangling_pointer_:
102 855475 : case OPT_Wreturn_local_addr:
103 855475 : case OPT_Wuse_after_free:
104 855475 : case OPT_Wuse_after_free_:
105 855475 : m_bits = NW_DANGLING;
106 855475 : break;
107 :
108 340316692 : case OPT_Wpessimizing_move:
109 340316692 : case OPT_Wredundant_move:
110 340316692 : m_bits = NW_REDUNDANT;
111 340316692 : break;
112 :
113 2703291 : default:
114 : /* A catchall group for everything else. */
115 2703291 : m_bits = NW_OTHER;
116 : }
117 517461532 : }
118 :
119 : /* A mapping from a 'location_t' to the warning spec set for it. */
120 :
121 : GTY(()) nowarn_map_t *nowarn_map;
122 :
123 : /* Return the no-warning disposition for location LOC and option OPT
124 : or for all/any otions by default. */
125 :
126 : bool
127 42411 : warning_suppressed_at (location_t loc, opt_code opt /* = all_warnings */)
128 : {
129 42411 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
130 :
131 42411 : if (!nowarn_map)
132 : return false;
133 :
134 42234 : if (const nowarn_spec_t* const pspec = nowarn_map->get (loc))
135 : {
136 22059 : const nowarn_spec_t optspec (opt);
137 22059 : return *pspec & optspec;
138 : }
139 :
140 : return false;
141 : }
142 :
143 : /* Change the supression of warnings for location LOC.
144 : OPT controls which warnings are affected.
145 : The wildcard OPT of -1 controls all warnings.
146 : If SUPP is true (the default), enable the suppression of the warnings.
147 : If SUPP is false, disable the suppression of the warnings. */
148 :
149 : bool
150 381343499 : suppress_warning_at (location_t loc, opt_code opt /* = all_warnings */,
151 : bool supp /* = true */)
152 : {
153 381343499 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
154 :
155 381405211 : const nowarn_spec_t optspec (supp ? opt : opt_code ());
156 :
157 381343499 : if (nowarn_spec_t *pspec = nowarn_map ? nowarn_map->get (loc) : nullptr)
158 : {
159 108748758 : if (supp)
160 : {
161 108745785 : *pspec |= optspec;
162 108745785 : return true;
163 : }
164 :
165 2973 : *pspec &= optspec;
166 2973 : if (*pspec)
167 : return true;
168 :
169 2973 : nowarn_map->remove (loc);
170 2973 : return false;
171 : }
172 :
173 272594741 : if (!supp || opt == no_warning)
174 : return false;
175 :
176 272536002 : if (!nowarn_map)
177 181572 : nowarn_map = nowarn_map_t::create_ggc (32);
178 :
179 272536002 : nowarn_map->put (loc, optspec);
180 272536002 : return true;
181 : }
182 :
183 : /* Change the warning disposition for LOC to match OPTSPEC. */
184 :
185 : void
186 473166 : put_warning_spec_at (location_t loc, unsigned bits)
187 : {
188 473166 : gcc_checking_assert (!RESERVED_LOCATION_P (loc));
189 :
190 473166 : nowarn_spec_t optspec = nowarn_spec_t::from_bits (bits);
191 473166 : if (!optspec)
192 : {
193 197773 : if (nowarn_map)
194 197679 : nowarn_map->remove (loc);
195 : }
196 : else
197 : {
198 275393 : if (!nowarn_map)
199 200 : nowarn_map = nowarn_map_t::create_ggc (32);
200 275393 : nowarn_map->put (loc, optspec);
201 : }
202 473166 : }
203 :
204 : /* Copy the no-warning disposition from one location to another. */
205 :
206 : void
207 238743500 : copy_warning (location_t to, location_t from)
208 : {
209 238743500 : if (!nowarn_map)
210 : return;
211 :
212 225690084 : nowarn_spec_t *from_spec;
213 225690084 : if (RESERVED_LOCATION_P (from))
214 : from_spec = nullptr;
215 : else
216 205147451 : from_spec = nowarn_map->get (from);
217 225690084 : if (RESERVED_LOCATION_P (to))
218 : /* We cannot set no-warning dispositions for 'to', so we have no chance but
219 : lose those potentially set for 'from'. */
220 : ;
221 : else
222 : {
223 205700990 : if (from_spec)
224 : {
225 60146037 : nowarn_spec_t tem = *from_spec;
226 60146037 : nowarn_map->put (to, tem);
227 : }
228 : else
229 145554953 : nowarn_map->remove (to);
230 : }
231 : }
|