Skip to content

Commit 0d56350

Browse files
committed
gccrs: Adapt attribute lang hook and do some cleanup
Since r14-6076, the LANG_HOOKS_COMMON_ATTRIBUTE_TABLE should not be used and LANG_HOOKS_ATTRIBUTE_TABLE replaces it. Add the "cold" attribute to fix PR120018 (and the matching "hot" attribute). Replace some gcc_assert() by explicit warnings (same as D frontend). Add some clang-format off/on comment around code that's not correctly formatted. gcc/rust/ChangeLog: PR rust/120018 * rust-attribs.cc (handle_noreturn_attribute): Reindent declaration. (handle_leaf_attribute): Likewise. (handle_const_attribute): Likewise. (handle_malloc_attribute): Likewise. (handle_pure_attribute): Likewise. (handle_novops_attribute): Likewise. (handle_nonnull_attribute): Likewise. (handle_nothrow_attribute): Likewise. (handle_type_generic_attribute): Likewise. (handle_transaction_pure_attribute): Likewise. (handle_returns_twice_attribute): Likewise. (handle_fnspec_attribute): Likewise. (handle_omp_declare_simd_attribute): Likewise. (handle_cold_attribute): New. (handle_hot_attribute): New. (attribute_spec::exclusions attr_cold_hot_exclusions): New. (grs_langhook_common_attributes): Make it static. (grs_langhook_common_attribute_table): New. (grs_langhook_gnu_attributes): New. (grs_langhook_gnu_attribute_table): New. (handle_malloc_attribute): Make it static. (handle_fnspec_attribute): Likewise. (handle_pure_attribute): Replace gcc_assert by explicit warning. (handle_novops_attribute): Likewise. (handle_nothrow_attribute): Likewise. (handle_returns_twice_attribute): Likewise. (handle_omp_declare_simd_attribute): Likewise and make it static. * rust-lang.cc (grs_langhook_gnu_attribute_table): New. (grs_langhook_common_attribute_table): Adjust type to new hook. (LANG_HOOKS_COMMON_ATTRIBUTE_TABLE): Remove. (LANG_HOOKS_ATTRIBUTE_TABLE): New. Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
1 parent d90770b commit 0d56350

File tree

2 files changed

+134
-47
lines changed

2 files changed

+134
-47
lines changed

gcc/rust/rust-attribs.cc

Lines changed: 125 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,44 @@ along with GCC; see the file COPYING3. If not see
3838
* future.
3939
*/
4040

41-
extern const attribute_spec grs_langhook_common_attribute_table[];
41+
extern const struct scoped_attribute_specs grs_langhook_gnu_attribute_table;
42+
extern const struct scoped_attribute_specs grs_langhook_common_attribute_table;
43+
44+
/* clang-format off */
45+
/* Disable clang-format because it insists in having the return type on a
46+
single line (that's for definitions) */
4247

4348
/* Internal attribute handlers for built-in functions. */
44-
static tree
45-
handle_noreturn_attribute (tree *, tree, tree, int, bool *);
46-
static tree
47-
handle_leaf_attribute (tree *, tree, tree, int, bool *);
48-
static tree
49-
handle_const_attribute (tree *, tree, tree, int, bool *);
50-
static tree
51-
handle_malloc_attribute (tree *, tree, tree, int, bool *);
52-
static tree
53-
handle_pure_attribute (tree *, tree, tree, int, bool *);
54-
static tree
55-
handle_novops_attribute (tree *, tree, tree, int, bool *);
56-
static tree
57-
handle_nonnull_attribute (tree *, tree, tree, int, bool *);
58-
static tree
59-
handle_nothrow_attribute (tree *, tree, tree, int, bool *);
60-
static tree
61-
handle_type_generic_attribute (tree *, tree, tree, int, bool *);
62-
static tree
63-
handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
64-
static tree
65-
handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
66-
static tree
67-
handle_fnspec_attribute (tree *, tree, tree, int, bool *);
68-
static tree
69-
handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *);
49+
static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
50+
static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
51+
static tree handle_const_attribute (tree *, tree, tree, int, bool *);
52+
static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
53+
static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
54+
static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
55+
static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
56+
static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
57+
static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
58+
static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
59+
static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
60+
static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
61+
static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *);
62+
63+
/* Rust attribute handlers for user defined attributes. */
64+
static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
65+
static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
66+
67+
/* clang-format on */
7068

7169
/* Helper to define attribute exclusions. */
7270
#define ATTR_EXCL(name, function, type, variable) \
7371
{ \
7472
name, function, type, variable \
7573
}
7674

75+
// clang-format off
76+
// Disabling clang-format because it insists in having several ATTR_EXCL() on a
77+
// single line.
78+
7779
static const struct attribute_spec::exclusions attr_noreturn_exclusions[] = {
7880
// ATTR_EXCL ("alloc_size", true, true, true),
7981
ATTR_EXCL ("const", true, true, true),
@@ -89,11 +91,22 @@ static const struct attribute_spec::exclusions attr_returns_twice_exclusions[]
8991
ATTR_EXCL (NULL, false, false, false),
9092
};
9193

94+
extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[] = {
95+
96+
ATTR_EXCL ("cold", true, true, true),
97+
ATTR_EXCL ("hot", true, true, true),
98+
ATTR_EXCL (NULL, false, false, false)
99+
};
100+
92101
static const struct attribute_spec::exclusions attr_const_pure_exclusions[] = {
93102
// ATTR_EXCL ("alloc_size", true, true, true),
94103
ATTR_EXCL ("const", true, true, true),
95104
ATTR_EXCL ("noreturn", true, true, true),
96-
ATTR_EXCL ("pure", true, true, true), ATTR_EXCL (NULL, false, false, false)};
105+
ATTR_EXCL ("pure", true, true, true),
106+
ATTR_EXCL (NULL, false, false, false)
107+
};
108+
109+
// clang-format on
97110

98111
/* Helper to define an attribute. */
99112
#define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req, \
@@ -105,7 +118,7 @@ static const struct attribute_spec::exclusions attr_const_pure_exclusions[] = {
105118

106119
/* Table of machine-independent attributes.
107120
For internal use (marking of built-ins) only. */
108-
const attribute_spec grs_langhook_common_attribute_table[] = {
121+
static const attribute_spec grs_langhook_common_attributes[] = {
109122
ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
110123
handle_noreturn_attribute, attr_noreturn_exclusions),
111124
ATTR_SPEC ("leaf", 0, 0, true, false, false, false, handle_leaf_attribute,
@@ -132,9 +145,21 @@ const attribute_spec grs_langhook_common_attribute_table[] = {
132145
NULL),
133146
ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
134147
handle_omp_declare_simd_attribute, NULL),
135-
ATTR_SPEC (NULL, 0, 0, false, false, false, false, NULL, NULL),
136148
};
137149

150+
const scoped_attribute_specs grs_langhook_common_attribute_table
151+
= {"gnu", {grs_langhook_common_attributes}};
152+
153+
static const attribute_spec grs_langhook_gnu_attributes[] = {
154+
ATTR_SPEC ("cold", 0, 0, true, false, false, false, handle_cold_attribute,
155+
attr_cold_hot_exclusions),
156+
ATTR_SPEC ("hot", 0, 0, true, false, false, false, handle_hot_attribute,
157+
attr_cold_hot_exclusions),
158+
};
159+
160+
const scoped_attribute_specs grs_langhook_gnu_attribute_table
161+
= {"gnu", {grs_langhook_gnu_attributes}};
162+
138163
/* Built-in attribute handlers.
139164
These functions take the arguments:
140165
(tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
@@ -204,7 +229,7 @@ handle_const_attribute (tree *node, tree, tree, int, bool *)
204229
/* Handle a "malloc" attribute; arguments as in
205230
struct attribute_spec.handler. */
206231

207-
tree
232+
static tree
208233
handle_malloc_attribute (tree *node, tree, tree, int, bool *)
209234
{
210235
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
@@ -217,9 +242,14 @@ handle_malloc_attribute (tree *node, tree, tree, int, bool *)
217242
struct attribute_spec.handler. */
218243

219244
static tree
220-
handle_pure_attribute (tree *node, tree, tree, int, bool *)
245+
handle_pure_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
221246
{
222-
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
247+
if (TREE_CODE (*node) != FUNCTION_DECL)
248+
{
249+
warning (OPT_Wattributes, "%qE attribute ignored", name);
250+
*no_add_attrs = true;
251+
}
252+
223253
DECL_PURE_P (*node) = 1;
224254
return NULL_TREE;
225255
}
@@ -228,9 +258,14 @@ handle_pure_attribute (tree *node, tree, tree, int, bool *)
228258
struct attribute_spec.handler. */
229259

230260
static tree
231-
handle_novops_attribute (tree *node, tree, tree, int, bool *)
261+
handle_novops_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
232262
{
233-
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
263+
if (TREE_CODE (*node) != FUNCTION_DECL)
264+
{
265+
warning (OPT_Wattributes, "%qE attribute ignored", name);
266+
*no_add_attrs = true;
267+
}
268+
234269
DECL_IS_NOVOPS (*node) = 1;
235270
return NULL_TREE;
236271
}
@@ -301,9 +336,14 @@ handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
301336
struct attribute_spec.handler. */
302337

303338
static tree
304-
handle_nothrow_attribute (tree *node, tree, tree, int, bool *)
339+
handle_nothrow_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
305340
{
306-
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
341+
if (TREE_CODE (*node) != FUNCTION_DECL)
342+
{
343+
warning (OPT_Wattributes, "%qE attribute ignored", name);
344+
*no_add_attrs = true;
345+
}
346+
307347
TREE_NOTHROW (*node) = 1;
308348
return NULL_TREE;
309349
}
@@ -339,9 +379,14 @@ handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
339379
struct attribute_spec.handler. */
340380

341381
static tree
342-
handle_returns_twice_attribute (tree *node, tree, tree, int, bool *)
382+
handle_returns_twice_attribute (tree *node, tree name, tree, int,
383+
bool *no_add_attrs)
343384
{
344-
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
385+
if (TREE_CODE (*node) != FUNCTION_DECL)
386+
{
387+
warning (OPT_Wattributes, "%qE attribute ignored", name);
388+
*no_add_attrs = true;
389+
}
345390

346391
DECL_IS_RETURNS_TWICE (*node) = 1;
347392

@@ -351,7 +396,7 @@ handle_returns_twice_attribute (tree *node, tree, tree, int, bool *)
351396
/* Handle a "fn spec" attribute; arguments as in
352397
struct attribute_spec.handler. */
353398

354-
tree
399+
static tree
355400
handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
356401
{
357402
gcc_assert (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST
@@ -362,9 +407,46 @@ handle_fnspec_attribute (tree *, tree, tree args, int, bool *)
362407
/* Handle an "omp declare simd" attribute; arguments as in
363408
struct attribute_spec.handler. */
364409

365-
tree
366-
handle_omp_declare_simd_attribute (tree *node, tree, tree, int, bool *)
410+
static tree
411+
handle_omp_declare_simd_attribute (tree *node, tree name, tree, int,
412+
bool *no_add_attrs)
413+
{
414+
if (TREE_CODE (*node) != FUNCTION_DECL)
415+
{
416+
warning (OPT_Wattributes, "%qE attribute ignored", name);
417+
*no_add_attrs = true;
418+
}
419+
420+
return NULL_TREE;
421+
}
422+
423+
/* Language specific attribute handlers.
424+
These functions take the arguments:
425+
(tree *node, tree name, tree args, int flags, bool *no_add_attrs) */
426+
427+
/* Handle a "cold" and attribute; arguments as in
428+
struct attribute_spec.handler. */
429+
430+
static tree
431+
handle_cold_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
367432
{
368-
gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
433+
if (TREE_CODE (*node) != FUNCTION_DECL)
434+
{
435+
warning (OPT_Wattributes, "%qE attribute ignored", name);
436+
*no_add_attrs = true;
437+
}
438+
439+
return NULL_TREE;
440+
}
441+
442+
static tree
443+
handle_hot_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
444+
{
445+
if (TREE_CODE (*node) != FUNCTION_DECL)
446+
{
447+
warning (OPT_Wattributes, "%qE attribute ignored", name);
448+
*no_add_attrs = true;
449+
}
450+
369451
return NULL_TREE;
370452
}

gcc/rust/rust-lang.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,13 @@ rust_localize_identifier (const char *ident)
376376
return identifier_to_locale (ident);
377377
}
378378

379-
extern const attribute_spec grs_langhook_common_attribute_table[];
379+
extern const struct scoped_attribute_specs grs_langhook_gnu_attribute_table;
380+
extern const struct scoped_attribute_specs grs_langhook_common_attribute_table;
381+
382+
const scoped_attribute_specs *const grs_langhook_attribute_table[] = {
383+
&grs_langhook_gnu_attribute_table,
384+
&grs_langhook_common_attribute_table,
385+
};
380386

381387
/* The language hooks data structure. This is the main interface between the GCC
382388
* front-end and the GCC middle-end/back-end. A list of language hooks could be
@@ -397,8 +403,7 @@ extern const attribute_spec grs_langhook_common_attribute_table[];
397403
#undef LANG_HOOKS_WRITE_GLOBALS
398404
#undef LANG_HOOKS_GIMPLIFY_EXPR
399405
#undef LANG_HOOKS_EH_PERSONALITY
400-
401-
#undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
406+
#undef LANG_HOOKS_ATTRIBUTE_TABLE
402407

403408
#define LANG_HOOKS_NAME "GNU Rust"
404409
#define LANG_HOOKS_INIT grs_langhook_init
@@ -420,7 +425,7 @@ extern const attribute_spec grs_langhook_common_attribute_table[];
420425
#define LANG_HOOKS_GIMPLIFY_EXPR grs_langhook_gimplify_expr
421426
#define LANG_HOOKS_EH_PERSONALITY grs_langhook_eh_personality
422427

423-
#define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE grs_langhook_common_attribute_table
428+
#define LANG_HOOKS_ATTRIBUTE_TABLE grs_langhook_attribute_table
424429

425430
#if CHECKING_P
426431

0 commit comments

Comments
 (0)