aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2003-09-16 03:58:27 -0400
committerJakub Jelinek <jakub@gcc.gnu.org>2003-09-16 09:58:27 +0200
commit72954a4f4456cbdc8e1ff01e27f488d304ba32ad (patch)
treea6e02ea18c49961a7fc38f54f53192e537f6e3af /gcc/c-common.c
parentc9fbef12bee8504ff48a45e3c67099af6772857d (diff)
downloadgcc-72954a4f4456cbdc8e1ff01e27f488d304ba32ad.zip
gcc-72954a4f4456cbdc8e1ff01e27f488d304ba32ad.tar.gz
gcc-72954a4f4456cbdc8e1ff01e27f488d304ba32ad.tar.bz2
c-common.c (handle_warn_unused_result_attribute): New function.
* c-common.c (handle_warn_unused_result_attribute): New function. (c_common_attribute_table): Add warn_unused_result. (c_expand_expr): Issue warning when result of inlined function with warn_unused_result attribute is ignored. * calls.c (expand_call): Issue warning when result of function with warn_unused_result attribute is ignored. * c-common.h (STMT_EXPR_WARN_UNUSED_RESULT): Define. * expr.c (expr_wfl_stack): Define. (expand_expr) <case EXPR_WITH_FILE_LOCATION>: If ignore, pass const0_rtx as target. Chain locations into expr_wfl_stack. * tree-inline.c (expand_call_inline): Set STMT_EXPR_WARN_UNUSED_RESULT bit if inlined function has warn_unused_result attribute. * input.h (expr_wfl_stack): Declare. * doc/extend.texi: Document warn_unused_result attribute. * gcc.dg/attr-warn-unused-result.c: New test. Co-Authored-By: Jakub Jelinek <jakub@redhat.com> From-SVN: r71424
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r--gcc/c-common.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 5268806..49c6aca 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -773,6 +773,8 @@ static tree handle_vector_size_attribute (tree *, tree, tree, int,
static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
+static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
+ bool *);
static tree vector_size_helper (tree, tree);
static void check_function_nonnull (tree, tree);
@@ -850,6 +852,8 @@ const struct attribute_spec c_common_attribute_table[] =
{ "may_alias", 0, 0, false, true, false, NULL },
{ "cleanup", 1, 1, true, false, false,
handle_cleanup_attribute },
+ { "warn_unused_result", 0, 0, false, true, true,
+ handle_warn_unused_result_attribute },
{ NULL, 0, 0, false, false, false, NULL }
};
@@ -4007,6 +4011,26 @@ c_expand_expr (tree exp, rtx target, enum machine_mode tmode, int modifier)
bool preserve_result = false;
bool return_target = false;
+ if (STMT_EXPR_WARN_UNUSED_RESULT (exp) && target == const0_rtx)
+ {
+ tree stmt = STMT_EXPR_STMT (exp);
+ tree scope;
+
+ for (scope = COMPOUND_BODY (stmt);
+ scope && TREE_CODE (scope) != SCOPE_STMT;
+ scope = TREE_CHAIN (scope));
+
+ if (scope && SCOPE_STMT_BLOCK (scope))
+ warning ("%Hignoring return value of `%D', "
+ "declared with attribute warn_unused_result",
+ &expr_wfl_stack->location,
+ BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope)));
+ else
+ warning ("%Hignoring return value of function "
+ "declared with attribute warn_unused_result",
+ &expr_wfl_stack->location);
+ }
+
/* Since expand_expr_stmt calls free_temp_slots after every
expression statement, we must call push_temp_slots here.
Otherwise, any temporaries in use now would be considered
@@ -5496,6 +5520,23 @@ handle_cleanup_attribute (tree *node, tree name, tree args,
return NULL_TREE;
}
+
+/* Handle a "warn_unused_result" attribute. No special handling. */
+
+static tree
+handle_warn_unused_result_attribute (tree *node, tree name,
+ tree args ATTRIBUTE_UNUSED,
+ int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
+{
+ /* Ignore the attribute for functions not returning any value. */
+ if (VOID_TYPE_P (TREE_TYPE (*node)))
+ {
+ warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+ *no_add_attrs = true;
+ }
+
+ return NULL_TREE;
+}
/* Check for valid arguments being passed to a function. */
void