1 /* Some code common to C and ObjC front ends.
2 Copyright (C) 2001-2019 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "c-tree.h"
24 #include "intl.h"
25 #include "c-family/c-pretty-print.h"
26 #include "tree-pretty-print.h"
27 #include "gimple-pretty-print.h"
28 #include "langhooks.h"
29 #include "c-objc-common.h"
30 #include "gcc-rich-location.h"
31
32 static bool c_tree_printer (pretty_printer *, text_info *, const char *,
33 int, bool, bool, bool, bool *, const char **);
34
35 bool
c_missing_noreturn_ok_p(tree decl)36 c_missing_noreturn_ok_p (tree decl)
37 {
38 /* A missing noreturn is not ok for freestanding implementations and
39 ok for the `main' function in hosted implementations. */
40 return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
41 }
42
43 /* Called from check_global_declaration. */
44
45 bool
c_warn_unused_global_decl(const_tree decl)46 c_warn_unused_global_decl (const_tree decl)
47 {
48 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
49 return false;
50 if (DECL_IN_SYSTEM_HEADER (decl))
51 return false;
52
53 return true;
54 }
55
56 /* Initialization common to C and Objective-C front ends. */
57 bool
c_objc_common_init(void)58 c_objc_common_init (void)
59 {
60 c_init_decl_processing ();
61
62 return c_common_init ();
63 }
64
65 /* Print T to CPP. */
66
67 static void
print_type(c_pretty_printer * cpp,tree t,bool * quoted)68 print_type (c_pretty_printer *cpp, tree t, bool *quoted)
69 {
70 gcc_assert (TYPE_P (t));
71 struct obstack *ob = pp_buffer (cpp)->obstack;
72 char *p = (char *) obstack_base (ob);
73 /* Remember the end of the initial dump. */
74 int len = obstack_object_size (ob);
75
76 tree name = TYPE_NAME (t);
77 if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
78 pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
79 else
80 cpp->type_id (t);
81
82 /* If we're printing a type that involves typedefs, also print the
83 stripped version. But sometimes the stripped version looks
84 exactly the same, so we don't want it after all. To avoid
85 printing it in that case, we play ugly obstack games. */
86 if (TYPE_CANONICAL (t) && t != TYPE_CANONICAL (t))
87 {
88 c_pretty_printer cpp2;
89 /* Print the stripped version into a temporary printer. */
90 cpp2.type_id (TYPE_CANONICAL (t));
91 struct obstack *ob2 = cpp2.buffer->obstack;
92 /* Get the stripped version from the temporary printer. */
93 const char *aka = (char *) obstack_base (ob2);
94 int aka_len = obstack_object_size (ob2);
95 int type1_len = obstack_object_size (ob) - len;
96
97 /* If they are identical, bail out. */
98 if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0)
99 return;
100
101 /* They're not, print the stripped version now. */
102 if (*quoted)
103 pp_end_quote (cpp, pp_show_color (cpp));
104 pp_c_whitespace (cpp);
105 pp_left_brace (cpp);
106 pp_c_ws_string (cpp, _("aka"));
107 pp_c_whitespace (cpp);
108 if (*quoted)
109 pp_begin_quote (cpp, pp_show_color (cpp));
110 cpp->type_id (TYPE_CANONICAL (t));
111 if (*quoted)
112 pp_end_quote (cpp, pp_show_color (cpp));
113 pp_right_brace (cpp);
114 /* No further closing quotes are needed. */
115 *quoted = false;
116 }
117 }
118
119 /* Called during diagnostic message formatting process to print a
120 source-level entity onto BUFFER. The meaning of the format specifiers
121 is as follows:
122 %D: a general decl,
123 %E: an identifier or expression,
124 %F: a function declaration,
125 %G: a Gimple statement,
126 %K: a CALL_EXPR,
127 %T: a type.
128 %V: a list of type qualifiers from a tree.
129 %v: an explicit list of type qualifiers
130 %#v: an explicit list of type qualifiers of a function type.
131
132 Please notice when called, the `%' part was already skipped by the
133 diagnostic machinery. */
134 static bool
c_tree_printer(pretty_printer * pp,text_info * text,const char * spec,int precision,bool wide,bool set_locus,bool hash,bool * quoted,const char **)135 c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
136 int precision, bool wide, bool set_locus, bool hash,
137 bool *quoted, const char **)
138 {
139 tree t = NULL_TREE;
140 // FIXME: the next cast should be a dynamic_cast, when it is permitted.
141 c_pretty_printer *cpp = (c_pretty_printer *) pp;
142 pp->padding = pp_none;
143
144 if (precision != 0 || wide)
145 return false;
146
147 if (*spec == 'G')
148 {
149 percent_G_format (text);
150 return true;
151 }
152
153 if (*spec == 'K')
154 {
155 t = va_arg (*text->args_ptr, tree);
156 percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t));
157 return true;
158 }
159
160 if (*spec != 'v')
161 {
162 t = va_arg (*text->args_ptr, tree);
163 if (set_locus)
164 text->set_location (0, DECL_SOURCE_LOCATION (t),
165 SHOW_RANGE_WITH_CARET);
166 }
167
168 switch (*spec)
169 {
170 case 'D':
171 if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
172 {
173 t = DECL_DEBUG_EXPR (t);
174 if (!DECL_P (t))
175 {
176 cpp->expression (t);
177 return true;
178 }
179 }
180 /* FALLTHRU */
181
182 case 'F':
183 if (DECL_NAME (t))
184 {
185 pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
186 return true;
187 }
188 break;
189
190 case 'T':
191 print_type (cpp, t, quoted);
192 return true;
193
194 case 'E':
195 if (TREE_CODE (t) == IDENTIFIER_NODE)
196 pp_identifier (cpp, IDENTIFIER_POINTER (t));
197 else
198 cpp->expression (t);
199 return true;
200
201 case 'V':
202 pp_c_type_qualifier_list (cpp, t);
203 return true;
204
205 case 'v':
206 pp_c_cv_qualifiers (cpp, va_arg (*text->args_ptr, int), hash);
207 return true;
208
209 default:
210 return false;
211 }
212
213 pp_string (cpp, _("({anonymous})"));
214 return true;
215 }
216
217 /* C-specific implementation of range_label::get_text () vfunc for
218 range_label_for_type_mismatch. */
219
220 label_text
get_text(unsigned)221 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
222 {
223 if (m_labelled_type == NULL_TREE)
224 return label_text (NULL, false);
225
226 c_pretty_printer cpp;
227 bool quoted = false;
228 print_type (&cpp, m_labelled_type, "ed);
229 return label_text (xstrdup (pp_formatted_text (&cpp)), true);
230 }
231
232
233 /* In C and ObjC, all decls have "C" linkage. */
234 bool
has_c_linkage(const_tree decl ATTRIBUTE_UNUSED)235 has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
236 {
237 return true;
238 }
239
240 void
c_initialize_diagnostics(diagnostic_context * context)241 c_initialize_diagnostics (diagnostic_context *context)
242 {
243 pretty_printer *base = context->printer;
244 c_pretty_printer *pp = XNEW (c_pretty_printer);
245 context->printer = new (pp) c_pretty_printer ();
246
247 /* It is safe to free this object because it was previously XNEW()'d. */
248 base->~pretty_printer ();
249 XDELETE (base);
250
251 c_common_diagnostics_set_defaults (context);
252 diagnostic_format_decoder (context) = &c_tree_printer;
253 }
254
255 int
c_types_compatible_p(tree x,tree y)256 c_types_compatible_p (tree x, tree y)
257 {
258 return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
259 }
260
261 /* Determine if the type is a vla type for the backend. */
262
263 bool
c_vla_unspec_p(tree x,tree fn ATTRIBUTE_UNUSED)264 c_vla_unspec_p (tree x, tree fn ATTRIBUTE_UNUSED)
265 {
266 return c_vla_type_p (x);
267 }
268
269 /* Special routine to get the alias set of T for C. */
270
271 alias_set_type
c_get_alias_set(tree t)272 c_get_alias_set (tree t)
273 {
274 /* Allow aliasing between enumeral types and the underlying
275 integer type. This is required since those are compatible types. */
276 if (TREE_CODE (t) == ENUMERAL_TYPE)
277 {
278 tree t1 = c_common_type_for_size (tree_to_uhwi (TYPE_SIZE (t)),
279 /* short-cut commoning to signed
280 type. */
281 false);
282 return get_alias_set (t1);
283 }
284
285 return c_common_get_alias_set (t);
286 }
287