diff options
author | Joseph Myers <joseph@codesourcery.com> | 2020-11-04 06:48:46 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2020-11-04 06:48:46 +0000 |
commit | c19e44ac8dbc9af07e5e671edfa03ab5b08649c5 (patch) | |
tree | 042ddbf096456fbd03cf55ccbc034a061ffea5df /gcc/c/c-decl.c | |
parent | 2e0aa43fc6ae689c595902310baec604e7e0d695 (diff) | |
download | gcc-c19e44ac8dbc9af07e5e671edfa03ab5b08649c5.zip gcc-c19e44ac8dbc9af07e5e671edfa03ab5b08649c5.tar.gz gcc-c19e44ac8dbc9af07e5e671edfa03ab5b08649c5.tar.bz2 |
c: Implement C2x nodiscard attribute
C2x adds the nodiscard standard attribute, with an optional string
argument, as in C++; implement it for C.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
gcc/c/
2020-11-04 Joseph Myers <joseph@codesourcery.com>
* c-decl.c (handle_nodiscard_attribute): New.
(std_attribute_table): Add nodiscard.
* c-parser.c (c_parser_std_attribute): Expect argument to
nodiscard attribute to be a string. Do not special-case ignoring
nodiscard.
* c-typeck.c (maybe_warn_nodiscard): New.
(build_compound_expr, emit_side_effect_warnings): Call
maybe_warn_nodiscard.
(c_process_expr_stmt, c_finish_stmt_expr): Also call
emit_side_effect_warnings if warn_unused_result.
gcc/testsuite/
2020-11-04 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/c2x-attr-nodiscard-1.c, gcc.dg/c2x-attr-nodiscard-2.c,
gcc.dg/c2x-attr-nodiscard-3.c, gcc.dg/c2x-attr-nodiscard-4.c: New
tests.
* gcc.dg/c2x-attr-syntax-5.c: Remove nodiscard test.
Diffstat (limited to 'gcc/c/c-decl.c')
-rw-r--r-- | gcc/c/c-decl.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index a5d0b15..d4179aa 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -4400,6 +4400,31 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) } +/* Handle the standard [[nodiscard]] attribute. */ + +static tree +handle_nodiscard_attribute (tree *node, tree name, tree /*args*/, + int /*flags*/, bool *no_add_attrs) +{ + if (TREE_CODE (*node) == FUNCTION_DECL) + { + if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) + warning_at (DECL_SOURCE_LOCATION (*node), + OPT_Wattributes, "%qE attribute applied to %qD with void " + "return type", name, *node); + } + else if (RECORD_OR_UNION_TYPE_P (*node) + || TREE_CODE (*node) == ENUMERAL_TYPE) + /* OK */; + else + { + pedwarn (input_location, + OPT_Wattributes, "%qE attribute can only be applied to " + "functions or to structure, union or enumeration types", name); + *no_add_attrs = true; + } + return NULL_TREE; +} /* Table of supported standard (C2x) attributes. */ const struct attribute_spec std_attribute_table[] = { @@ -4411,6 +4436,8 @@ const struct attribute_spec std_attribute_table[] = handle_fallthrough_attribute, NULL }, { "maybe_unused", 0, 0, false, false, false, false, handle_unused_attribute, NULL }, + { "nodiscard", 0, 1, false, false, false, false, + handle_nodiscard_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; |