Debian Codegraph @

gcc/c/c-errors.cc

raw
1 /* Various diagnostic subroutines for the GNU C language.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "c-tree.h"
26 #include "opts.h"
27
28 /* Issue an ISO C23 pedantic warning MSGID if -pedantic outside C2Y mode,
29 otherwise issue warning MSGID if -Wc23-c2y-compat is specified.
30 This function is supposed to be used for matters that are allowed in
31 ISO C2Y but not supported in ISO C23, thus we explicitly don't pedwarn
32 when C2Y is specified. */
33
34 bool
35 pedwarn_c23 (location_t location,
36 diagnostic_option_id option_id,
37 const char *gmsgid, ...)
38 {
39 diagnostic_info diagnostic;
40 va_list ap;
41 bool warned = false;
42 rich_location richloc (line_table, location);
43
44 va_start (ap, gmsgid);
45 /* If desired, issue the C23/C2Y compat warning, which is more specific
46 than -pedantic. */
47 if (warn_c23_c2y_compat > 0)
48 {
49 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
50 (pedantic && !flag_isoc2y)
51 ? DK_PEDWARN : DK_WARNING);
52 diagnostic.option_id = OPT_Wc23_c2y_compat;
53 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
54 }
55 /* -Wno-c23-c2y-compat suppresses even the pedwarns. */
56 else if (warn_c23_c2y_compat == 0)
57 ;
58 /* For -pedantic outside C2Y, issue a pedwarn. */
59 else if (pedantic && !flag_isoc2y)
60 {
61 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
62 diagnostic.option_id = option_id;
63 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
64 }
65 va_end (ap);
66 return warned;
67 }
68
69 /* Issue an ISO C11 pedantic warning MSGID if -pedantic outside C23 mode,
70 otherwise issue warning MSGID if -Wc11-c23-compat is specified.
71 This function is supposed to be used for matters that are allowed in
72 ISO C23 but not supported in ISO C11, thus we explicitly don't pedwarn
73 when C23 is specified.
74
75 Additionally, warn if OPTION_ID is not OPT_Wpedantic nor
76 OPT_Wc11_c23_compat. */
77
78 bool
79 pedwarn_c11 (location_t location,
80 diagnostic_option_id option_id,
81 const char *gmsgid, ...)
82 {
83 diagnostic_info diagnostic;
84 va_list ap;
85 bool warned = false;
86 rich_location richloc (line_table, location);
87
88 va_start (ap, gmsgid);
89 /* If desired, issue the C11/C23 compat warning, which is more specific than
90 -pedantic, or the warning specified by option_id. */
91 if (warn_c11_c23_compat > 0 || (option_id.m_idx != OPT_Wpedantic
92 && option_id.m_idx != OPT_Wc11_c23_compat))
93 {
94 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
95 (pedantic && !flag_isoc23)
96 ? DK_PEDWARN : DK_WARNING);
97 if (option_id == OPT_Wpedantic)
98 diagnostic.option_id = OPT_Wc11_c23_compat;
99 else
100 diagnostic.option_id = option_id;
101 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
102 }
103 /* -Wno-c11-c23-compat suppresses even the pedwarns. */
104 else if (warn_c11_c23_compat == 0)
105 ;
106 /* For -pedantic outside C23, issue a pedwarn. */
107 else if (pedantic && !flag_isoc23)
108 {
109 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
110 diagnostic.option_id = option_id;
111 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
112 }
113 va_end (ap);
114 return warned;
115 }
116
117 /* Issue an ISO C99 pedantic warning MSGID if -pedantic outside C11 mode,
118 otherwise issue warning MSGID if -Wc99-c11-compat is specified.
119 This function is supposed to be used for matters that are allowed in
120 ISO C11 but not supported in ISO C99, thus we explicitly don't pedwarn
121 when C11 is specified. */
122
123 bool
124 pedwarn_c99 (location_t location,
125 diagnostic_option_id option_id,
126 const char *gmsgid, ...)
127 {
128 diagnostic_info diagnostic;
129 va_list ap;
130 bool warned = false;
131 rich_location richloc (line_table, location);
132
133 va_start (ap, gmsgid);
134 /* If desired, issue the C99/C11 compat warning, which is more specific
135 than -pedantic. */
136 if (warn_c99_c11_compat > 0)
137 {
138 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
139 (pedantic && !flag_isoc11)
140 ? DK_PEDWARN : DK_WARNING);
141 diagnostic.option_id = OPT_Wc99_c11_compat;
142 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
143 }
144 /* -Wno-c99-c11-compat suppresses even the pedwarns. */
145 else if (warn_c99_c11_compat == 0)
146 ;
147 /* For -pedantic outside C11, issue a pedwarn. */
148 else if (pedantic && !flag_isoc11)
149 {
150 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
151 diagnostic.option_id = option_id;
152 warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
153 }
154 va_end (ap);
155 return warned;
156 }
157
158 /* Issue an ISO C90 pedantic warning MSGID if -pedantic outside C99 mode,
159 otherwise issue warning MSGID if -Wc90-c99-compat is specified, or if
160 a specific option such as -Wlong-long is specified.
161 This function is supposed to be used for matters that are allowed in
162 ISO C99 but not supported in ISO C90, thus we explicitly don't pedwarn
163 when C99 is specified. (There is no flag_c90.) */
164
165 bool
166 pedwarn_c90 (location_t location,
167 diagnostic_option_id option_id,
168 const char *gmsgid, ...)
169 {
170 diagnostic_info diagnostic;
171 va_list ap;
172 bool warned = false;
173 rich_location richloc (line_table, location);
174
175 va_start (ap, gmsgid);
176 /* Warnings such as -Wvla are the most specific ones. */
177 if (option_id.m_idx != OPT_Wpedantic)
178 {
179 int opt_var = *(int *) option_flag_var (option_id.m_idx, &global_options);
180 if (opt_var == 0)
181 goto out;
182 else if (opt_var > 0)
183 {
184 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
185 (pedantic && !flag_isoc99)
186 ? DK_PEDWARN : DK_WARNING);
187 diagnostic.option_id = option_id;
188 diagnostic_report_diagnostic (global_dc, &diagnostic);
189 warned = true;
190 goto out;
191 }
192 }
193 /* Maybe we want to issue the C90/C99 compat warning, which is more
194 specific than -pedantic. */
195 if (warn_c90_c99_compat > 0)
196 {
197 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
198 (pedantic && !flag_isoc99)
199 ? DK_PEDWARN : DK_WARNING);
200 diagnostic.option_id = OPT_Wc90_c99_compat;
201 diagnostic_report_diagnostic (global_dc, &diagnostic);
202 }
203 /* -Wno-c90-c99-compat suppresses the pedwarns. */
204 else if (warn_c90_c99_compat == 0)
205 ;
206 /* For -pedantic outside C99, issue a pedwarn. */
207 else if (pedantic && !flag_isoc99)
208 {
209 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
210 diagnostic.option_id = option_id;
211 diagnostic_report_diagnostic (global_dc, &diagnostic);
212 warned = true;
213 }
214 out:
215 va_end (ap);
216 return warned;
217 }
218
219 /* Determine in which version of the standard IDENTIFIER
220 became a keyword. */
221
222 static const char *
223 get_std_for_keyword (tree identifier)
224 {
225 switch (C_RID_CODE (identifier))
226 {
227 default:
228 gcc_unreachable ();
229 case RID_FALSE:
230 case RID_TRUE:
231 return "-std=c23";
232 case RID_BOOL:
233 if (IDENTIFIER_POINTER (identifier)[0] == 'b')
234 /* "bool". */
235 return "-std=c23";
236 else
237 /* "_Bool". */
238 return "-std=c99";
239 }
240 }
241
242 /* Issue a note to the user at LOC that KEYWORD_ID is a keyword
243 in STD_OPTION version of the standard onwards. */
244
245 void
246 add_note_about_new_keyword (location_t loc,
247 tree keyword_id)
248 {
249 gcc_assert (TREE_CODE (keyword_id) == IDENTIFIER_NODE);
250 const char *std_option = get_std_for_keyword (keyword_id);
251 inform (loc, "%qs is a keyword with %qs onwards",
252 IDENTIFIER_POINTER (keyword_id), std_option);
253 }