1 /* Support for suggestions about missing #include directives.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #define INCLUDE_UNIQUE_PTR
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"
29 /* An enum for distinguishing between the C and C++ stdlibs. */
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). */
46 const char *header
[NUM_STDLIBS
];
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. */
54 get_stdlib_header_for_name (const char *name
, enum stdlib lib
)
57 gcc_assert (lib
< NUM_STDLIBS
);
59 static const stdlib_hint hints
[] = {
60 /* <assert.h> and <cassert>. */
61 {"assert", {"<assert.h>", "<cassert>"} },
63 /* <errno.h> and <cerrno>. */
64 {"errno", {"<errno.h>", "<cerrno>"} },
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>"} },
87 /* <stdarg.h> and <cstdarg>. */
88 {"va_list", {"<stdarg.h>", "<cstdarg>"} },
90 /* <stddef.h> and <cstddef>. */
91 {"NULL", {"<stddef.h>", "<cstddef>"} },
92 {"nullptr_t", {NULL
, "<cstddef>"} },
93 {"offsetof", {"<stddef.h>", "<cstddef>"} },
94 {"ptrdiff_t", {"<stddef.h>", "<cstddef>"} },
95 {"size_t", {"<stddef.h>", "<cstddef>"} },
96 {"wchar_t", {"<stddef.h>", NULL
/* a keyword in C++ */} },
98 /* <stdio.h> and <cstdio>. */
99 {"BUFSIZ", {"<stdio.h>", "<cstdio>"} },
100 {"EOF", {"<stdio.h>", "<cstdio>"} },
101 {"FILE", {"<stdio.h>", "<cstdio>"} },
102 {"FILENAME_MAX", {"<stdio.h>", "<cstdio>"} },
103 {"fopen", {"<stdio.h>", "<cstdio>"} },
104 {"fpos_t", {"<stdio.h>", "<cstdio>"} },
105 {"getchar", {"<stdio.h>", "<cstdio>"} },
106 {"printf", {"<stdio.h>", "<cstdio>"} },
107 {"snprintf", {"<stdio.h>", "<cstdio>"} },
108 {"sprintf", {"<stdio.h>", "<cstdio>"} },
109 {"stderr", {"<stdio.h>", "<cstdio>"} },
110 {"stdin", {"<stdio.h>", "<cstdio>"} },
111 {"stdout", {"<stdio.h>", "<cstdio>"} },
113 /* <stdlib.h> and <cstdlib>. */
114 {"free", {"<stdlib.h>", "<cstdlib>"} },
115 {"malloc", {"<stdlib.h>", "<cstdlib>"} },
116 {"realloc", {"<stdlib.h>", "<cstdlib>"} },
118 /* <string.h> and <cstring>. */
119 {"memchr", {"<string.h>", "<cstring>"} },
120 {"memcmp", {"<string.h>", "<cstring>"} },
121 {"memcpy", {"<string.h>", "<cstring>"} },
122 {"memmove", {"<string.h>", "<cstring>"} },
123 {"memset", {"<string.h>", "<cstring>"} },
124 {"strcat", {"<string.h>", "<cstring>"} },
125 {"strchr", {"<string.h>", "<cstring>"} },
126 {"strcmp", {"<string.h>", "<cstring>"} },
127 {"strcpy", {"<string.h>", "<cstring>"} },
128 {"strlen", {"<string.h>", "<cstring>"} },
129 {"strncat", {"<string.h>", "<cstring>"} },
130 {"strncmp", {"<string.h>", "<cstring>"} },
131 {"strncpy", {"<string.h>", "<cstring>"} },
132 {"strrchr", {"<string.h>", "<cstring>"} },
133 {"strspn", {"<string.h>", "<cstring>"} },
134 {"strstr", {"<string.h>", "<cstring>"} },
137 {"PTRDIFF_MAX", {"<stdint.h>", "<cstdint>"} },
138 {"PTRDIFF_MIN", {"<stdint.h>", "<cstdint>"} },
139 {"SIG_ATOMIC_MAX", {"<stdint.h>", "<cstdint>"} },
140 {"SIG_ATOMIC_MIN", {"<stdint.h>", "<cstdint>"} },
141 {"SIZE_MAX", {"<stdint.h>", "<cstdint>"} },
142 {"WINT_MAX", {"<stdint.h>", "<cstdint>"} },
143 {"WINT_MIN", {"<stdint.h>", "<cstdint>"} },
146 {"WCHAR_MAX", {"<wchar.h>", "<cwchar>"} },
147 {"WCHAR_MIN", {"<wchar.h>", "<cwchar>"} }
149 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
150 for (size_t i
= 0; i
< num_hints
; i
++)
151 if (strcmp (name
, hints
[i
].name
) == 0)
152 return hints
[i
].header
[lib
];
156 /* Given non-NULL NAME, return the header name defining it within the C
157 standard library (with '<' and '>'), or NULL. */
160 get_c_stdlib_header_for_name (const char *name
)
162 return get_stdlib_header_for_name (name
, STDLIB_C
);
165 /* Given non-NULL NAME, return the header name defining it within the C++
166 standard library (with '<' and '>'), or NULL. */
169 get_cp_stdlib_header_for_name (const char *name
)
171 return get_stdlib_header_for_name (name
, STDLIB_CPLUSPLUS
);
174 /* Implementation of class suggest_missing_header. */
176 /* suggest_missing_header's ctor. */
178 suggest_missing_header::suggest_missing_header (location_t loc
,
180 const char *header_hint
)
181 : deferred_diagnostic (loc
), m_name_str (name
), m_header_hint (header_hint
)
184 gcc_assert (header_hint
);
187 /* suggest_missing_header's dtor. */
189 suggest_missing_header::~suggest_missing_header ()
191 if (is_suppressed_p ())
194 gcc_rich_location
richloc (get_location ());
195 maybe_add_include_fixit (&richloc
, m_header_hint
);
197 "%qs is defined in header %qs;"
198 " did you forget to %<#include %s%>?",
199 m_name_str
, m_header_hint
, m_header_hint
);