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