diff options
author | Martin Sebor <msebor@redhat.com> | 2017-12-07 16:32:03 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2017-12-07 09:32:03 -0700 |
commit | 5d9ae53d70c72991e26648d915e7fb8e00b8e811 (patch) | |
tree | a586e44e1f5c41fd8ae4cb8fd80446c763cc595d /gcc/attribs.c | |
parent | 1d8b0222b15f2188b659de4a731d8fd5ea23bed0 (diff) |
PR c/81544 - attribute noreturn and warn_unused_result on the same function accepted
PR c/81544 - attribute noreturn and warn_unused_result on the same function accepted
PR c/81566 - invalid attribute aligned accepted on functions
gcc/ada/ChangeLog:
PR c/81544
* gcc-interface/utils.c (gnat_internal_attribute_table): Initialize
new member of struct attribute_spec.
gcc/c/ChangeLog:
PR c/81544
* c-decl.c (c_decl_attributes): Look up existing declaration and
pass it to decl_attributes.
gcc/c-family/ChangeLog:
PR c/81544
PR c/81566
* c-attribs.c (attr_aligned_exclusions): New array.
(attr_alloc_exclusions, attr_cold_hot_exclusions): Same.
(attr_common_exclusions, attr_const_pure_exclusions): Same.
(attr_gnu_inline_exclusions, attr_inline_exclusions): Same.
(attr_noreturn_exclusions, attr_returns_twice_exclusions): Same.
(attr_warn_unused_result_exclusions): Same.
(handle_hot_attribute, handle_cold_attribute): Simplify.
(handle_const_attribute): Warn on function returning void.
(handle_pure_attribute): Same.
(handle_aligned_attribute): Diagnose conflicting attribute
specifications.
* c-warn.c (diagnose_mismatched_attributes): Simplify.
gcc/cp/ChangeLog:
PR c/81544
* cp-tree.h (decls_match): Add default argument.
* decl.c (decls_match): Avoid calling into the target back end
and triggering an error.
* decl2.c (cplus_decl_attributes): Look up existing declaration and
pass it to decl_attributes.
* tree.c (cxx_attribute_table): Initialize new member of struct
attribute_spec.
gcc/fortran/ChangeLog:
PR c/81544
* f95-lang.c (gfc_attribute_table): Initialize new member of struct
attribute_spec.
gcc/lto/ChangeLog:
PR c/81544
* lto-lang.c (lto_attribute_table): Initialize new member of struct
attribute_spec.
gcc/ChangeLog:
PR c/81544
* attribs.c (empty_attribute_table): Initialize new member of
struct attribute_spec.
(decl_attributes): Add argument. Handle mutually exclusive
combinations of attributes.
(selftests::test_attribute_exclusions): New function.
(selftests::attribute_c_tests): Ditto.
* attribs.h (decl_attributes): Add default argument.
* selftest.h (attribute_c_tests): Declare.
* selftest-run-tests.c (selftest::run_tests): Call attribute_c_tests.
* tree-core.h (attribute_spec::exclusions, exclude): New type and
member.
* doc/extend.texi (Common Function Attributes): Update const and pure.
gcc/testsuite/ChangeLog:
PR c/81544
* c-c++-common/Wattributes-2.c: New test.
* c-c++-common/Wattributes.c: New test.
* c-c++-common/attributes-3.c: Adjust.
* gcc.dg/Wattributes-6.c: New test.
* gcc.dg/Wattributes-7.c: New test.
* gcc.dg/attr-noinline.c
* gcc.dg/pr44964.c: Same.
* gcc.dg/torture/pr42363.c: Same.
* gcc.dg/tree-ssa/ssa-ccp-2.c: Same.
From-SVN: r255469
Diffstat (limited to 'gcc/attribs.c')
-rw-r--r-- | gcc/attribs.c | 277 |
1 files changed, 268 insertions, 9 deletions
diff --git a/gcc/attribs.c b/gcc/attribs.c index 809f4c3a8d5..f65fd15814f 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -28,6 +28,8 @@ along with GCC; see the file COPYING3. If not see #include "stor-layout.h" #include "langhooks.h" #include "plugin.h" +#include "selftest.h" +#include "hash-set.h" /* Table of the tables of attributes (common, language, format, machine) searched. */ @@ -94,7 +96,7 @@ static bool attributes_initialized = false; static const struct attribute_spec empty_attribute_table[] = { - { NULL, 0, 0, false, false, false, NULL, false } + { NULL, 0, 0, false, false, false, NULL, false, NULL } }; /* Return base name of the attribute. Ie '__attr__' is turned into 'attr'. @@ -343,6 +345,97 @@ get_attribute_namespace (const_tree attr) return get_identifier ("gnu"); } +/* Check LAST_DECL and NODE of the same symbol for attributes that are + recorded in SPEC to be mutually exclusive with ATTRNAME, diagnose + them, and return true if any have been found. NODE can be a DECL + or a TYPE. */ + +static bool +diag_attr_exclusions (tree last_decl, tree node, tree attrname, + const attribute_spec *spec) +{ + const attribute_spec::exclusions *excl = spec->exclude; + + tree_code code = TREE_CODE (node); + + if ((code == FUNCTION_DECL && !excl->function + && (!excl->type || !spec->affects_type_identity)) + || (code == VAR_DECL && !excl->variable + && (!excl->type || !spec->affects_type_identity)) + || (((code == TYPE_DECL || RECORD_OR_UNION_TYPE_P (node)) && !excl->type))) + return false; + + /* True if an attribute that's mutually exclusive with ATTRNAME + has been found. */ + bool found = false; + + if (last_decl && last_decl != node && TREE_TYPE (last_decl) != node) + { + /* Check both the last DECL and its type for conflicts with + the attribute being added to the current decl or type. */ + found |= diag_attr_exclusions (last_decl, last_decl, attrname, spec); + tree decl_type = TREE_TYPE (last_decl); + found |= diag_attr_exclusions (last_decl, decl_type, attrname, spec); + } + + /* NODE is either the current DECL to which the attribute is being + applied or its TYPE. For the former, consider the attributes on + both the DECL and its type. */ + tree attrs[2]; + + if (DECL_P (node)) + { + attrs[0] = DECL_ATTRIBUTES (node); + attrs[1] = TYPE_ATTRIBUTES (TREE_TYPE (node)); + } + else + { + attrs[0] = TYPE_ATTRIBUTES (node); + attrs[1] = NULL_TREE; + } + + /* Iterate over the mutually exclusive attribute names and verify + that the symbol doesn't contain it. */ + for (unsigned i = 0; i != sizeof attrs / sizeof *attrs; ++i) + { + if (!attrs[i]) + continue; + + for ( ; excl->name; ++excl) + { + /* Avoid checking the attribute against itself. */ + if (is_attribute_p (excl->name, attrname)) + continue; + + if (!lookup_attribute (excl->name, attrs[i])) + continue; + + found = true; + + /* Print a note? */ + bool note = last_decl != NULL_TREE; + + if (TREE_CODE (node) == FUNCTION_DECL + && DECL_BUILT_IN (node)) + note &= warning (OPT_Wattributes, + "ignoring attribute %qE in declaration of " + "a built-in function %qD because it conflicts " + "with attribute %qs", + attrname, node, excl->name); + else + note &= warning (OPT_Wattributes, + "ignoring attribute %qE because " + "it conflicts with attribute %qs", + attrname, excl->name); + + if (note) + inform (DECL_SOURCE_LOCATION (last_decl), + "previous declaration here"); + } + } + + return found; +} /* Process the attributes listed in ATTRIBUTES and install them in *NODE, which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, @@ -354,7 +447,8 @@ get_attribute_namespace (const_tree attr) a decl attribute to the declaration rather than to its type). */ tree -decl_attributes (tree *node, tree attributes, int flags) +decl_attributes (tree *node, tree attributes, int flags, + tree last_decl /* = NULL_TREE */) { tree a; tree returned_attrs = NULL_TREE; @@ -433,6 +527,8 @@ decl_attributes (tree *node, tree attributes, int flags) targetm.insert_attributes (*node, &attributes); + /* Note that attributes on the same declaration are not necessarily + in the same order as in the source. */ for (a = attributes; a; a = TREE_CHAIN (a)) { tree ns = get_attribute_namespace (a); @@ -441,7 +537,6 @@ decl_attributes (tree *node, tree attributes, int flags) tree *anode = node; const struct attribute_spec *spec = lookup_scoped_attribute_spec (ns, name); - bool no_add_attrs = 0; int fn_ptr_quals = 0; tree fn_ptr_tmp = NULL_TREE; @@ -490,7 +585,8 @@ decl_attributes (tree *node, tree attributes, int flags) | (int) ATTR_FLAG_ARRAY_NEXT)) { /* Pass on this attribute to be tried again. */ - returned_attrs = tree_cons (name, args, returned_attrs); + tree attr = tree_cons (name, args, NULL_TREE); + returned_attrs = chainon (returned_attrs, attr); continue; } else @@ -535,7 +631,8 @@ decl_attributes (tree *node, tree attributes, int flags) else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) { /* Pass on this attribute to be tried again. */ - returned_attrs = tree_cons (name, args, returned_attrs); + tree attr = tree_cons (name, args, NULL_TREE); + returned_attrs = chainon (returned_attrs, attr); continue; } @@ -557,15 +654,56 @@ decl_attributes (tree *node, tree attributes, int flags) continue; } + bool no_add_attrs = false; + if (spec->handler != NULL) { int cxx11_flag = cxx11_attribute_p (a) ? ATTR_FLAG_CXX11 : 0; - returned_attrs = chainon ((*spec->handler) (anode, name, args, - flags|cxx11_flag, - &no_add_attrs), - returned_attrs); + /* Pass in an array of the current declaration followed + by the last pushed/merged declaration if one exists. + If the handler changes CUR_AND_LAST_DECL[0] replace + *ANODE with its value. */ + tree cur_and_last_decl[] = { *anode, last_decl }; + tree ret = (spec->handler) (cur_and_last_decl, name, args, + flags|cxx11_flag, &no_add_attrs); + + *anode = cur_and_last_decl[0]; + if (ret == error_mark_node) + { + warning (OPT_Wattributes, "%qE attribute ignored", name); + no_add_attrs = true; + } + else + returned_attrs = chainon (ret, returned_attrs); + } + + /* If the attribute was successfully handled on its own and is + about to be added check for exclusions with other attributes + on the current declation as well as the last declaration of + the same symbol already processed (if one exists). */ + bool built_in = flags & ATTR_FLAG_BUILT_IN; + if (spec->exclude + && !no_add_attrs + && (flag_checking || !built_in)) + { + /* Always check attributes on user-defined functions. + Check them on built-ins only when -fchecking is set. + Ignore __builtin_unreachable -- it's both const and + noreturn. */ + + if (!built_in + || !DECL_P (*anode) + || (DECL_FUNCTION_CODE (*anode) != BUILT_IN_UNREACHABLE + && (DECL_FUNCTION_CODE (*anode) + != BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE))) + { + bool no_add = diag_attr_exclusions (last_decl, *anode, name, spec); + if (!no_add && anode != node) + no_add = diag_attr_exclusions (last_decl, *node, name, spec); + no_add_attrs |= no_add; + } } /* Layout the decl in case anything changed. */ @@ -1647,3 +1785,124 @@ private_lookup_attribute (const char *attr_name, size_t attr_len, tree list) return list; } + +#if CHECKING_P + +namespace selftest +{ + +/* Helper types to verify the consistency attribute exclusions. */ + +typedef std::pair<const char *, const char *> excl_pair; + +struct excl_hash_traits: typed_noop_remove<excl_pair> +{ + typedef excl_pair value_type; + typedef value_type compare_type; + + static hashval_t hash (const value_type &x) + { + hashval_t h1 = htab_hash_string (x.first); + hashval_t h2 = htab_hash_string (x.second); + return h1 ^ h2; + } + + static bool equal (const value_type &x, const value_type &y) + { + return !strcmp (x.first, y.first) && !strcmp (x.second, y.second); + } + + static void mark_deleted (value_type &x) + { + x = value_type (NULL, NULL); + } + + static void mark_empty (value_type &x) + { + x = value_type ("", ""); + } + + static bool is_deleted (const value_type &x) + { + return !x.first && !x.second; + } + + static bool is_empty (const value_type &x) + { + return !*x.first && !*x.second; + } +}; + + +/* Self-test to verify that each attribute exclusion is symmetric, + meaning that if attribute A is encoded as incompatible with + attribute B then the opposite relationship is also encoded. + This test also detects most cases of misspelled attribute names + in exclusions. */ + +static void +test_attribute_exclusions () +{ + /* Iterate over the array of attribute tables first (with TI0 as + the index) and over the array of attribute_spec in each table + (with SI0 as the index). */ + const size_t ntables = ARRAY_SIZE (attribute_tables); + + /* Set of pairs of mutually exclusive attributes. */ + typedef hash_set<excl_pair, excl_hash_traits> exclusion_set; + exclusion_set excl_set; + + for (size_t ti0 = 0; ti0 != ntables; ++ti0) + for (size_t s0 = 0; attribute_tables[ti0][s0].name; ++s0) + { + const attribute_spec::exclusions *excl + = attribute_tables[ti0][s0].exclude; + + /* Skip each attribute that doesn't define exclusions. */ + if (!excl) + continue; + + const char *attr_name = attribute_tables[ti0][s0].name; + + /* Iterate over the set of exclusions for every attribute + (with EI0 as the index) adding the exclusions defined + for each to the set. */ + for (size_t ei0 = 0; excl[ei0].name; ++ei0) + { + const char *excl_name = excl[ei0].name; + + if (!strcmp (attr_name, excl_name)) + continue; + + excl_set.add (excl_pair (attr_name, excl_name)); + } + } + + /* Traverse the set of mutually exclusive pairs of attributes + and verify that they are symmetric. */ + for (exclusion_set::iterator it = excl_set.begin (); + it != excl_set.end (); + ++it) + { + if (!excl_set.contains (excl_pair ((*it).second, (*it).first))) + { + /* An exclusion for an attribute has been found that + doesn't have a corresponding exclusion in the opposite + direction. */ + char desc[120]; + sprintf (desc, "'%s' attribute exclusion '%s' must be symmetric", + (*it).first, (*it).second); + fail (SELFTEST_LOCATION, desc); + } + } +} + +void +attribute_c_tests () +{ + test_attribute_exclusions (); +} + +} /* namespace selftest */ + +#endif /* CHECKING_P */ |