aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/known-headers.cc
blob: b0763cfe984871257b5f7a2252db152c2abe23c6 (plain)
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
/* Support for suggestions about missing #include directives.
   Copyright (C) 2017-2018 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"
#define INCLUDE_UNIQUE_PTR
#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"

/* 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 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>"} },

    /* <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>.  */
    {"free", {"<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>"} },
    {"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>"} },

    /* <wchar.h>.  */
    {"WCHAR_MAX", {"<wchar.h>", "<cwchar>"} },
    {"WCHAR_MIN", {"<wchar.h>", "<cwchar>"} }
  };
  const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
  for (size_t i = 0; i < num_hints; i++)
    if (strcmp (name, hints[i].name) == 0)
      return hints[i].header[lib];
  return NULL;
}

/* 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);
}

/* 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;"
	  " did you forget to %<#include %s%>?",
	  m_name_str, m_header_hint, m_header_hint);
}