1 /* Functions to enable and disable individual warnings on an expression
4 Copyright (C) 2021 Free Software Foundation, Inc.
6 This file is part of GCC.
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
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
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/>. */
24 #include "coretypes.h"
31 #include "diagnostic-spec.h"
33 /* Return the no-warning bit for EXPR. */
36 get_no_warning_bit (const_tree expr
)
38 return expr
->base
.nowarning_flag
;
41 /* Return the no-warning bit for statement STMT. */
44 get_no_warning_bit (const gimple
*stmt
)
46 return stmt
->no_warning
;
49 /* Set the no-warning bit for EXPR to VALUE. */
52 set_no_warning_bit (tree expr
, bool value
)
54 expr
->base
.nowarning_flag
= value
;
57 /* Set the no-warning bit for statement STMT to VALUE. */
60 set_no_warning_bit (gimple
*stmt
, bool value
)
62 stmt
->no_warning
= value
;
65 /* Return EXPR location or zero. */
67 static inline key_type_t
68 convert_to_key (const_tree expr
)
71 return DECL_SOURCE_LOCATION (expr
);
73 return EXPR_LOCATION (expr
);
77 /* Return STMT location (may be zero). */
79 static inline key_type_t
80 convert_to_key (const gimple
*stmt
)
82 return gimple_location (stmt
);
85 /* Return the no-warning bitmap for decl/expression EXPR. */
87 static nowarn_spec_t
*
88 get_nowarn_spec (const_tree expr
)
90 const key_type_t key
= convert_to_key (expr
);
92 if (!get_no_warning_bit (expr
) || !key
)
95 return nowarn_map
? nowarn_map
->get (key
) : NULL
;
98 /* Return the no-warning bitmap for stateemt STMT. */
100 static nowarn_spec_t
*
101 get_nowarn_spec (const gimple
*stmt
)
103 const key_type_t key
= convert_to_key (stmt
);
105 if (!get_no_warning_bit (stmt
))
108 return nowarn_map
? nowarn_map
->get (key
) : NULL
;
111 /* Return true if warning OPT is suppressed for decl/expression EXPR.
112 By default tests the disposition for any warning. */
115 warning_suppressed_p (const_tree expr
, opt_code opt
/* = all_warnings */)
117 const nowarn_spec_t
*spec
= get_nowarn_spec (expr
);
120 return get_no_warning_bit (expr
);
122 const nowarn_spec_t
optspec (opt
);
123 bool dis
= *spec
& optspec
;
124 gcc_assert (get_no_warning_bit (expr
) || !dis
);
128 /* Return true if warning OPT is suppressed for statement STMT.
129 By default tests the disposition for any warning. */
132 warning_suppressed_p (const gimple
*stmt
, opt_code opt
/* = all_warnings */)
134 const nowarn_spec_t
*spec
= get_nowarn_spec (stmt
);
137 /* Fall back on the single no-warning bit. */
138 return get_no_warning_bit (stmt
);
140 const nowarn_spec_t
optspec (opt
);
141 bool dis
= *spec
& optspec
;
142 gcc_assert (get_no_warning_bit (stmt
) || !dis
);
146 /* Enable, or by default disable, a warning for the expression.
147 The wildcard OPT of -1 controls all warnings. */
150 suppress_warning (tree expr
, opt_code opt
/* = all_warnings */,
151 bool supp
/* = true */)
153 if (opt
== no_warning
)
156 const key_type_t key
= convert_to_key (expr
);
158 supp
= suppress_warning_at (key
, opt
, supp
) || supp
;
159 set_no_warning_bit (expr
, supp
);
162 /* Enable, or by default disable, a warning for the statement STMT.
163 The wildcard OPT of -1 controls all warnings. */
166 suppress_warning (gimple
*stmt
, opt_code opt
/* = all_warnings */,
167 bool supp
/* = true */)
169 if (opt
== no_warning
)
172 const key_type_t key
= convert_to_key (stmt
);
174 supp
= suppress_warning_at (key
, opt
, supp
) || supp
;
175 set_no_warning_bit (stmt
, supp
);
178 /* Copy the warning disposition mapping between an expression and/or
181 template <class ToType
, class FromType
>
182 void copy_warning (ToType to
, FromType from
)
184 const key_type_t to_key
= convert_to_key (to
);
186 if (nowarn_spec_t
*from_map
= get_nowarn_spec (from
))
188 /* If there's an entry in the map the no-warning bit must be set. */
189 gcc_assert (get_no_warning_bit (from
));
192 nowarn_map
= xint_hash_map_t::create_ggc (32);
194 nowarn_map
->put (to_key
, *from_map
);
195 set_no_warning_bit (to
, true);
200 nowarn_map
->remove (to_key
);
202 /* The no-warning bit might be set even if there's no entry
204 set_no_warning_bit (to
, get_no_warning_bit (from
));
208 /* Copy the warning disposition mapping from one expression to another. */
211 copy_warning (tree to
, const_tree from
)
213 copy_warning
<tree
, const_tree
>(to
, from
);
216 /* Copy the warning disposition mapping from a statement to an expression. */
219 copy_warning (tree to
, const gimple
*from
)
221 copy_warning
<tree
, const gimple
*>(to
, from
);
224 /* Copy the warning disposition mapping from an expression to a statement. */
227 copy_warning (gimple
*to
, const_tree from
)
229 copy_warning
<gimple
*, const_tree
>(to
, from
);
232 /* Copy the warning disposition mapping from one statement to another. */
235 copy_warning (gimple
*to
, const gimple
*from
)
237 copy_warning
<gimple
*, const gimple
*>(to
, from
);