]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/known-headers.cc
diagnostics: Add function call parens matching to c_parser.
[thirdparty/gcc.git] / gcc / c-family / known-headers.cc
CommitLineData
26edace6 1/* Support for suggestions about missing #include directives.
8d9254fc 2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
26edace6
DM
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#define INCLUDE_UNIQUE_PTR
22#include "system.h"
23#include "coretypes.h"
24#include "c-family/c-common.h"
25#include "c-family/name-hint.h"
26#include "c-family/known-headers.h"
27#include "gcc-rich-location.h"
28
29/* An enum for distinguishing between the C and C++ stdlibs. */
30
31enum stdlib
32{
33 STDLIB_C,
34 STDLIB_CPLUSPLUS,
35
36 NUM_STDLIBS
37};
38
39/* A struct for associating names in a standard library with the header
40 that should be included to locate them, for each of the C and C++ stdlibs
41 (or NULL, for names that aren't in a header for a particular stdlib). */
42
43struct stdlib_hint
44{
45 const char *name;
46 const char *header[NUM_STDLIBS];
47};
48
49/* Given non-NULL NAME, return the header name defining it within either
50 the standard library (with '<' and '>'), or NULL.
51 Only handles a subset of the most common names within the stdlibs. */
52
53static const char *
54get_stdlib_header_for_name (const char *name, enum stdlib lib)
55{
56 gcc_assert (name);
57 gcc_assert (lib < NUM_STDLIBS);
58
59 static const stdlib_hint hints[] = {
62e98ef1
DM
60 /* <assert.h> and <cassert>. */
61 {"assert", {"<assert.h>", "<cassert>"} },
62
26edace6
DM
63 /* <errno.h> and <cerrno>. */
64 {"errno", {"<errno.h>", "<cerrno>"} },
65
66 /* <limits.h> and <climits>. */
67 {"CHAR_BIT", {"<limits.h>", "<climits>"} },
68 {"CHAR_MAX", {"<limits.h>", "<climits>"} },
69 {"CHAR_MIN", {"<limits.h>", "<climits>"} },
70 {"INT_MAX", {"<limits.h>", "<climits>"} },
71 {"INT_MIN", {"<limits.h>", "<climits>"} },
72 {"LLONG_MAX", {"<limits.h>", "<climits>"} },
73 {"LLONG_MIN", {"<limits.h>", "<climits>"} },
74 {"LONG_MAX", {"<limits.h>", "<climits>"} },
75 {"LONG_MIN", {"<limits.h>", "<climits>"} },
76 {"MB_LEN_MAX", {"<limits.h>", "<climits>"} },
77 {"SCHAR_MAX", {"<limits.h>", "<climits>"} },
78 {"SCHAR_MIN", {"<limits.h>", "<climits>"} },
79 {"SHRT_MAX", {"<limits.h>", "<climits>"} },
80 {"SHRT_MIN", {"<limits.h>", "<climits>"} },
81 {"UCHAR_MAX", {"<limits.h>", "<climits>"} },
82 {"UINT_MAX", {"<limits.h>", "<climits>"} },
83 {"ULLONG_MAX", {"<limits.h>", "<climits>"} },
84 {"ULONG_MAX", {"<limits.h>", "<climits>"} },
85 {"USHRT_MAX", {"<limits.h>", "<climits>"} },
86
ec2be203
DM
87 /* <float.h> and <cfloat>. */
88 {"DBL_MAX", {"<float.h>", "<cfloat>"} },
89 {"DBL_MIN", {"<float.h>", "<cfloat>"} },
90 {"FLT_MAX", {"<float.h>", "<cfloat>"} },
91 {"FLT_MIN", {"<float.h>", "<cfloat>"} },
92 {"LDBL_MAX", {"<float.h>", "<cfloat>"} },
93 {"LDBL_MIN", {"<float.h>", "<cfloat>"} },
94
26edace6
DM
95 /* <stdarg.h> and <cstdarg>. */
96 {"va_list", {"<stdarg.h>", "<cstdarg>"} },
97
98 /* <stddef.h> and <cstddef>. */
99 {"NULL", {"<stddef.h>", "<cstddef>"} },
100 {"nullptr_t", {NULL, "<cstddef>"} },
101 {"offsetof", {"<stddef.h>", "<cstddef>"} },
102 {"ptrdiff_t", {"<stddef.h>", "<cstddef>"} },
103 {"size_t", {"<stddef.h>", "<cstddef>"} },
104 {"wchar_t", {"<stddef.h>", NULL /* a keyword in C++ */} },
105
62e98ef1 106 /* <stdio.h> and <cstdio>. */
26edace6
DM
107 {"BUFSIZ", {"<stdio.h>", "<cstdio>"} },
108 {"EOF", {"<stdio.h>", "<cstdio>"} },
109 {"FILE", {"<stdio.h>", "<cstdio>"} },
110 {"FILENAME_MAX", {"<stdio.h>", "<cstdio>"} },
62e98ef1 111 {"fopen", {"<stdio.h>", "<cstdio>"} },
26edace6 112 {"fpos_t", {"<stdio.h>", "<cstdio>"} },
62e98ef1
DM
113 {"getchar", {"<stdio.h>", "<cstdio>"} },
114 {"printf", {"<stdio.h>", "<cstdio>"} },
115 {"snprintf", {"<stdio.h>", "<cstdio>"} },
116 {"sprintf", {"<stdio.h>", "<cstdio>"} },
26edace6
DM
117 {"stderr", {"<stdio.h>", "<cstdio>"} },
118 {"stdin", {"<stdio.h>", "<cstdio>"} },
119 {"stdout", {"<stdio.h>", "<cstdio>"} },
120
62e98ef1
DM
121 /* <stdlib.h> and <cstdlib>. */
122 {"free", {"<stdlib.h>", "<cstdlib>"} },
123 {"malloc", {"<stdlib.h>", "<cstdlib>"} },
124 {"realloc", {"<stdlib.h>", "<cstdlib>"} },
125
126 /* <string.h> and <cstring>. */
127 {"memchr", {"<string.h>", "<cstring>"} },
128 {"memcmp", {"<string.h>", "<cstring>"} },
129 {"memcpy", {"<string.h>", "<cstring>"} },
130 {"memmove", {"<string.h>", "<cstring>"} },
131 {"memset", {"<string.h>", "<cstring>"} },
132 {"strcat", {"<string.h>", "<cstring>"} },
133 {"strchr", {"<string.h>", "<cstring>"} },
134 {"strcmp", {"<string.h>", "<cstring>"} },
135 {"strcpy", {"<string.h>", "<cstring>"} },
136 {"strlen", {"<string.h>", "<cstring>"} },
137 {"strncat", {"<string.h>", "<cstring>"} },
138 {"strncmp", {"<string.h>", "<cstring>"} },
139 {"strncpy", {"<string.h>", "<cstring>"} },
140 {"strrchr", {"<string.h>", "<cstring>"} },
141 {"strspn", {"<string.h>", "<cstring>"} },
142 {"strstr", {"<string.h>", "<cstring>"} },
143
26edace6
DM
144 /* <stdint.h>. */
145 {"PTRDIFF_MAX", {"<stdint.h>", "<cstdint>"} },
146 {"PTRDIFF_MIN", {"<stdint.h>", "<cstdint>"} },
147 {"SIG_ATOMIC_MAX", {"<stdint.h>", "<cstdint>"} },
148 {"SIG_ATOMIC_MIN", {"<stdint.h>", "<cstdint>"} },
149 {"SIZE_MAX", {"<stdint.h>", "<cstdint>"} },
150 {"WINT_MAX", {"<stdint.h>", "<cstdint>"} },
151 {"WINT_MIN", {"<stdint.h>", "<cstdint>"} },
152
153 /* <wchar.h>. */
154 {"WCHAR_MAX", {"<wchar.h>", "<cwchar>"} },
155 {"WCHAR_MIN", {"<wchar.h>", "<cwchar>"} }
156 };
157 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
158 for (size_t i = 0; i < num_hints; i++)
01512446 159 if (strcmp (name, hints[i].name) == 0)
26edace6 160 return hints[i].header[lib];
45c50b6a 161
af114c38
MW
162 static const stdlib_hint c99_cxx11_hints[] = {
163 /* <stdbool.h>. Defined natively in C++. */
164 {"bool", {"<stdbool.h>", NULL} },
165 {"true", {"<stdbool.h>", NULL} },
166 {"false", {"<stdbool.h>", NULL} },
167
168 /* <stdint.h> and <cstdint>. */
169 {"int8_t", {"<stdint.h>", "<cstdint>"} },
170 {"uint8_t", {"<stdint.h>", "<cstdint>"} },
171 {"int16_t", {"<stdint.h>", "<cstdint>"} },
172 {"uint16_t", {"<stdint.h>", "<cstdint>"} },
173 {"int32_t", {"<stdint.h>", "<cstdint>"} },
174 {"uint32_t", {"<stdint.h>", "<cstdint>"} },
175 {"int64_t", {"<stdint.h>", "<cstdint>"} },
176 {"uint64_t", {"<stdint.h>", "<cstdint>"} },
177 {"intptr_t", {"<stdint.h>", "<cstdint>"} },
178 {"uintptr_t", {"<stdint.h>", "<cstdint>"} },
179 {"INT8_MAX", {"<stdint.h>", "<cstdint>"} },
180 {"INT16_MAX", {"<stdint.h>", "<cstdint>"} },
181 {"INT32_MAX", {"<stdint.h>", "<cstdint>"} },
182 {"INT64_MAX", {"<stdint.h>", "<cstdint>"} },
183 {"UINT8_MAX", {"<stdint.h>", "<cstdint>"} },
184 {"UINT16_MAX", {"<stdint.h>", "<cstdint>"} },
185 {"UINT32_MAX", {"<stdint.h>", "<cstdint>"} },
186 {"UINT64_MAX", {"<stdint.h>", "<cstdint>"} },
187 {"INTPTR_MAX", {"<stdint.h>", "<cstdint>"} },
188 {"UINTPTR_MAX", {"<stdint.h>", "<cstdint>"} }
189 };
190
191 const size_t num_c99_cxx11_hints = sizeof (c99_cxx11_hints)
192 / sizeof (c99_cxx11_hints[0]);
193 if ((lib == STDLIB_C && flag_isoc99)
194 || (lib == STDLIB_CPLUSPLUS && cxx_dialect >= cxx11 ))
195 for (size_t i = 0; i < num_c99_cxx11_hints; i++)
196 if (strcmp (name, c99_cxx11_hints[i].name) == 0)
197 return c99_cxx11_hints[i].header[lib];
45c50b6a 198
26edace6
DM
199 return NULL;
200}
201
202/* Given non-NULL NAME, return the header name defining it within the C
203 standard library (with '<' and '>'), or NULL. */
204
205const char *
206get_c_stdlib_header_for_name (const char *name)
207{
208 return get_stdlib_header_for_name (name, STDLIB_C);
209}
210
211/* Given non-NULL NAME, return the header name defining it within the C++
212 standard library (with '<' and '>'), or NULL. */
213
214const char *
215get_cp_stdlib_header_for_name (const char *name)
216{
217 return get_stdlib_header_for_name (name, STDLIB_CPLUSPLUS);
218}
219
220/* Implementation of class suggest_missing_header. */
221
222/* suggest_missing_header's ctor. */
223
224suggest_missing_header::suggest_missing_header (location_t loc,
225 const char *name,
226 const char *header_hint)
227: deferred_diagnostic (loc), m_name_str (name), m_header_hint (header_hint)
228{
229 gcc_assert (name);
230 gcc_assert (header_hint);
231}
232
233/* suggest_missing_header's dtor. */
234
235suggest_missing_header::~suggest_missing_header ()
236{
237 if (is_suppressed_p ())
238 return;
239
240 gcc_rich_location richloc (get_location ());
85204e23 241 maybe_add_include_fixit (&richloc, m_header_hint, true);
26edace6
DM
242 inform (&richloc,
243 "%qs is defined in header %qs;"
244 " did you forget to %<#include %s%>?",
245 m_name_str, m_header_hint, m_header_hint);
246}