1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/* Support for suggestions about missing #include directives.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "c-family/c-common.h"
#include "c-family/name-hint.h"
#include "c-family/known-headers.h"
#include "gcc-rich-location.h"
#include "opts.h"
/* An enum for distinguishing between the C and C++ stdlibs. */
enum stdlib
{
STDLIB_C,
STDLIB_CPLUSPLUS,
NUM_STDLIBS
};
/* A struct for associating names in a standard library with the header
that should be included to locate them, for each of the C and C++ stdlibs
(or NULL, for names that aren't in a header for a particular stdlib). */
struct stdlib_hint
{
const char *name;
const char *header[NUM_STDLIBS];
};
/* Given non-NULL NAME, return the header name defining it (as literal
string) within either the standard library (with '<' and '>'), or
NULL.
Only handle string macros, so that this can be used for
get_stdlib_header_for_name and
get_c_stdlib_header_for_string_macro_name. */
static const char *
get_string_macro_hint (const char *name, enum stdlib lib)
{
/* <inttypes.h> and <cinttypes>. */
static const char *c99_cxx11_macros[] =
{ "PRId8", "PRId16", "PRId32", "PRId64",
"PRIi8", "PRIi16", "PRIi32", "PRIi64",
"PRIo8", "PRIo16", "PRIo32", "PRIo64",
"PRIu8", "PRIu16", "PRIu32", "PRIu64",
"PRIx8", "PRIx16", "PRIx32", "PRIx64",
"PRIX8", "PRIX16", "PRIX32", "PRIX64",
"PRIdPTR", "PRIiPTR", "PRIoPTR", "PRIuPTR", "PRIxPTR", "PRIXPTR",
"SCNd8", "SCNd16", "SCNd32", "SCNd64",
"SCNi8", "SCNi16", "SCNi32", "SCNi64",
"SCNo8", "SCNo16", "SCNo32", "SCNo64",
"SCNu8", "SCNu16", "SCNu32", "SCNu64",
"SCNx8", "SCNx16", "SCNx32", "SCNx64",
"SCNdPTR", "SCNiPTR", "SCNoPTR", "SCNuPTR", "SCNxPTR" };
if ((lib == STDLIB_C && flag_isoc99)
|| (lib == STDLIB_CPLUSPLUS && cxx_dialect >= cxx11 ))
{
const size_t num_c99_cxx11_macros = ARRAY_SIZE (c99_cxx11_macros);
for (size_t i = 0; i < num_c99_cxx11_macros; i++)
if (strcmp (name, c99_cxx11_macros[i]) == 0)
return lib == STDLIB_C ? "<inttypes.h>" : "<cinttypes>";
}
return NULL;
}
/* Given non-NULL NAME, return the header name defining it within either
the standard library (with '<' and '>'), or NULL.
Only handles a subset of the most common names within the stdlibs. */
static const char *
get_stdlib_header_for_name (const char *name, enum stdlib lib)
{
gcc_assert (name);
gcc_assert (lib < NUM_STDLIBS);
static const stdlib_hint hints[] = {
/* <assert.h> and <cassert>. */
{"assert", {"<assert.h>", "<cassert>"} },
/* <errno.h> and <cerrno>. */
{"errno", {"<errno.h>", "<cerrno>"} },
/* <limits.h> and <climits>. */
{"CHAR_BIT", {"<limits.h>", "<climits>"} },
{"CHAR_MAX", {"<limits.h>", "<climits>"} },
{"CHAR_MIN", {"<limits.h>", "<climits>"} },
{"INT_MAX", {"<limits.h>", "<climits>"} },
{"INT_MIN", {"<limits.h>", "<climits>"} },
{"LLONG_MAX", {"<limits.h>", "<climits>"} },
{"LLONG_MIN", {"<limits.h>", "<climits>"} },
{"LONG_MAX", {"<limits.h>", "<climits>"} },
{"LONG_MIN", {"<limits.h>", "<climits>"} },
{"MB_LEN_MAX", {"<limits.h>", "<climits>"} },
{"SCHAR_MAX", {"<limits.h>", "<climits>"} },
{"SCHAR_MIN", {"<limits.h>", "<climits>"} },
{"SHRT_MAX", {"<limits.h>", "<climits>"} },
{"SHRT_MIN", {"<limits.h>", "<climits>"} },
{"UCHAR_MAX", {"<limits.h>", "<climits>"} },
{"UINT_MAX", {"<limits.h>", "<climits>"} },
{"ULLONG_MAX", {"<limits.h>", "<climits>"} },
{"ULONG_MAX", {"<limits.h>", "<climits>"} },
{"USHRT_MAX", {"<limits.h>", "<climits>"} },
/* <float.h> and <cfloat>. */
{"DBL_MAX", {"<float.h>", "<cfloat>"} },
{"DBL_MIN", {"<float.h>", "<cfloat>"} },
{"FLT_MAX", {"<float.h>", "<cfloat>"} },
{"FLT_MIN", {"<float.h>", "<cfloat>"} },
{"LDBL_MAX", {"<float.h>", "<cfloat>"} },
{"LDBL_MIN", {"<float.h>", "<cfloat>"} },
/* <stdarg.h> and <cstdarg>. */
{"va_list", {"<stdarg.h>", "<cstdarg>"} },
/* <stddef.h> and <cstddef>. */
{"NULL", {"<stddef.h>", "<cstddef>"} },
{"nullptr_t", {NULL, "<cstddef>"} },
{"offsetof", {"<stddef.h>", "<cstddef>"} },
{"ptrdiff_t", {"<stddef.h>", "<cstddef>"} },
{"size_t", {"<stddef.h>", "<cstddef>"} },
{"wchar_t", {"<stddef.h>", NULL /* a keyword in C++ */} },
/* <stdio.h> and <cstdio>. */
{"BUFSIZ", {"<stdio.h>", "<cstdio>"} },
{"EOF", {"<stdio.h>", "<cstdio>"} },
{"FILE", {"<stdio.h>", "<cstdio>"} },
{"FILENAME_MAX", {"<stdio.h>", "<cstdio>"} },
{"fopen", {"<stdio.h>", "<cstdio>"} },
{"fpos_t", {"<stdio.h>", "<cstdio>"} },
{"getchar", {"<stdio.h>", "<cstdio>"} },
{"printf", {"<stdio.h>", "<cstdio>"} },
{"snprintf", {"<stdio.h>", "<cstdio>"} },
{"sprintf", {"<stdio.h>", "<cstdio>"} },
{"stderr", {"<stdio.h>", "<cstdio>"} },
{"stdin", {"<stdio.h>", "<cstdio>"} },
{"stdout", {"<stdio.h>", "<cstdio>"} },
/* <stdlib.h> and <cstdlib>. */
{"EXIT_FAILURE", {"<stdlib.h>", "<cstdlib>"} },
{"EXIT_SUCCESS", {"<stdlib.h>", "<cstdlib>"} },
{"abort", {"<stdlib.h>", "<cstdlib>"} },
{"atexit", {"<stdlib.h>", "<cstdlib>"} },
{"calloc", {"<stdlib.h>", "<cstdlib>"} },
{"exit", {"<stdlib.h>", "<cstdlib>"} },
{"free", {"<stdlib.h>", "<cstdlib>"} },
{"getenv", {"<stdlib.h>", "<cstdlib>"} },
{"malloc", {"<stdlib.h>", "<cstdlib>"} },
{"realloc", {"<stdlib.h>", "<cstdlib>"} },
/* <string.h> and <cstring>. */
{"memchr", {"<string.h>", "<cstring>"} },
{"memcmp", {"<string.h>", "<cstring>"} },
{"memcpy", {"<string.h>", "<cstring>"} },
{"memmove", {"<string.h>", "<cstring>"} },
{"memset", {"<string.h>", "<cstring>"} },
{"strcat", {"<string.h>", "<cstring>"} },
{"strchr", {"<string.h>", "<cstring>"} },
{"strcmp", {"<string.h>", "<cstring>"} },
{"strcpy", {"<string.h>", "<cstring>"} },
{"strerror", {"<string.h>", "<cstring>"} },
{"strlen", {"<string.h>", "<cstring>"} },
{"strncat", {"<string.h>", "<cstring>"} },
{"strncmp", {"<string.h>", "<cstring>"} },
{"strncpy", {"<string.h>", "<cstring>"} },
{"strrchr", {"<string.h>", "<cstring>"} },
{"strspn", {"<string.h>", "<cstring>"} },
{"strstr", {"<string.h>", "<cstring>"} },
/* <stdint.h>. */
{"PTRDIFF_MAX", {"<stdint.h>", "<cstdint>"} },
{"PTRDIFF_MIN", {"<stdint.h>", "<cstdint>"} },
{"SIG_ATOMIC_MAX", {"<stdint.h>", "<cstdint>"} },
{"SIG_ATOMIC_MIN", {"<stdint.h>", "<cstdint>"} },
{"SIZE_MAX", {"<stdint.h>", "<cstdint>"} },
{"WINT_MAX", {"<stdint.h>", "<cstdint>"} },
{"WINT_MIN", {"<stdint.h>", "<cstdint>"} },
/* <time.h>. */
{"asctime", {"<time.h>", "<ctime>"} },
{"clock", {"<time.h>", "<ctime>"} },
{"clock_t", {"<time.h>", "<ctime>"} },
{"ctime", {"<time.h>", "<ctime>"} },
{"difftime", {"<time.h>", "<ctime>"} },
{"gmtime", {"<time.h>", "<ctime>"} },
{"localtime", {"<time.h>", "<ctime>"} },
{"mktime", {"<time.h>", "<ctime>"} },
{"strftime", {"<time.h>", "<ctime>"} },
{"time", {"<time.h>", "<ctime>"} },
{"time_t", {"<time.h>", "<ctime>"} },
{"tm", {"<time.h>", "<ctime>"} },
/* <wchar.h>. */
{"WCHAR_MAX", {"<wchar.h>", "<cwchar>"} },
{"WCHAR_MIN", {"<wchar.h>", "<cwchar>"} }
};
const size_t num_hints = ARRAY_SIZE (hints);
for (size_t i = 0; i < num_hints; i++)
if (strcmp (name, hints[i].name) == 0)
return hints[i].header[lib];
static const stdlib_hint c99_cxx11_hints[] = {
/* <stdbool.h>. Defined natively in C++. */
{"bool", {"<stdbool.h>", NULL} },
{"true", {"<stdbool.h>", NULL} },
{"false", {"<stdbool.h>", NULL} },
/* <stdint.h> and <cstdint>. */
{"int8_t", {"<stdint.h>", "<cstdint>"} },
{"uint8_t", {"<stdint.h>", "<cstdint>"} },
{"int16_t", {"<stdint.h>", "<cstdint>"} },
{"uint16_t", {"<stdint.h>", "<cstdint>"} },
{"int32_t", {"<stdint.h>", "<cstdint>"} },
{"uint32_t", {"<stdint.h>", "<cstdint>"} },
{"int64_t", {"<stdint.h>", "<cstdint>"} },
{"uint64_t", {"<stdint.h>", "<cstdint>"} },
{"intptr_t", {"<stdint.h>", "<cstdint>"} },
{"uintptr_t", {"<stdint.h>", "<cstdint>"} },
{"INT8_MAX", {"<stdint.h>", "<cstdint>"} },
{"INT16_MAX", {"<stdint.h>", "<cstdint>"} },
{"INT32_MAX", {"<stdint.h>", "<cstdint>"} },
{"INT64_MAX", {"<stdint.h>", "<cstdint>"} },
{"UINT8_MAX", {"<stdint.h>", "<cstdint>"} },
{"UINT16_MAX", {"<stdint.h>", "<cstdint>"} },
{"UINT32_MAX", {"<stdint.h>", "<cstdint>"} },
{"UINT64_MAX", {"<stdint.h>", "<cstdint>"} },
{"INTPTR_MAX", {"<stdint.h>", "<cstdint>"} },
{"UINTPTR_MAX", {"<stdint.h>", "<cstdint>"} }
};
const size_t num_c99_cxx11_hints = sizeof (c99_cxx11_hints)
/ sizeof (c99_cxx11_hints[0]);
if ((lib == STDLIB_C && flag_isoc99)
|| (lib == STDLIB_CPLUSPLUS && cxx_dialect >= cxx11 ))
for (size_t i = 0; i < num_c99_cxx11_hints; i++)
if (strcmp (name, c99_cxx11_hints[i].name) == 0)
return c99_cxx11_hints[i].header[lib];
return get_string_macro_hint (name, lib);
}
/* Given non-NULL NAME, return the header name defining it within the C
standard library (with '<' and '>'), or NULL. */
const char *
get_c_stdlib_header_for_name (const char *name)
{
return get_stdlib_header_for_name (name, STDLIB_C);
}
/* Given non-NULL NAME, return the header name defining it within the C++
standard library (with '<' and '>'), or NULL. */
const char *
get_cp_stdlib_header_for_name (const char *name)
{
return get_stdlib_header_for_name (name, STDLIB_CPLUSPLUS);
}
/* Given non-NULL NAME, return the header name defining a string macro
within the C standard library (with '<' and '>'), or NULL. */
const char *
get_c_stdlib_header_for_string_macro_name (const char *name)
{
return get_string_macro_hint (name, STDLIB_C);
}
/* Given non-NULL NAME, return the header name defining a string macro
within the C++ standard library (with '<' and '>'), or NULL. */
const char *
get_cp_stdlib_header_for_string_macro_name (const char *name)
{
return get_string_macro_hint (name, STDLIB_CPLUSPLUS);
}
/* Implementation of class suggest_missing_header. */
/* suggest_missing_header's ctor. */
suggest_missing_header::suggest_missing_header (location_t loc,
const char *name,
const char *header_hint)
: deferred_diagnostic (loc), m_name_str (name), m_header_hint (header_hint)
{
gcc_assert (name);
gcc_assert (header_hint);
}
/* suggest_missing_header's dtor. */
suggest_missing_header::~suggest_missing_header ()
{
if (is_suppressed_p ())
return;
gcc_rich_location richloc (get_location ());
maybe_add_include_fixit (&richloc, m_header_hint, true);
inform (&richloc,
"%qs is defined in header %qs;"
" this is probably fixable by adding %<#include %s%>",
m_name_str, m_header_hint, m_header_hint);
}
/* Implementation of class suggest_missing_option. */
/* suggest_missing_option's ctor. */
suggest_missing_option::suggest_missing_option (location_t loc,
const char *macro_name,
diagnostic_option_id option_id)
: deferred_diagnostic (loc), m_name_str (macro_name), m_option_id (option_id)
{
gcc_assert (macro_name);
gcc_assert (option_id.m_idx > 0);
}
/* suggest_missing_option's dtor. */
suggest_missing_option::~suggest_missing_option ()
{
if (is_suppressed_p ())
return;
const char *option_name = cl_options[m_option_id.m_idx].opt_text;
inform (get_location (),
"%qs is defined when using option %qs;"
" this is probably fixable by adding %qs to the command-line options",
m_name_str, option_name, option_name);
}
|