aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2021-11-19 09:44:31 -0700
committerMartin Sebor <msebor@redhat.com>2021-11-19 09:47:57 -0700
commit16137fbb9256ef365dd498d39024eb33de1a4cd8 (patch)
tree27fd0ecb6e5cdf0b6d36a242539bfbbcd4a73df1 /gcc/c-family
parentee448a523d377f9ed882dac806d2f5001bfa2432 (diff)
downloadgcc-16137fbb9256ef365dd498d39024eb33de1a4cd8.zip
gcc-16137fbb9256ef365dd498d39024eb33de1a4cd8.tar.gz
gcc-16137fbb9256ef365dd498d39024eb33de1a4cd8.tar.bz2
Restore ancient -Waddress for weak symbols [PR33925].
Resolves: PR c/33925 - gcc -Waddress lost some useful warnings PR c/102867 - -Waddress from macro expansion in readelf.c gcc/c-family/ChangeLog: PR c++/33925 PR c/102867 * c-common.c (decl_with_nonnull_addr_p): Call maybe_nonzero_address and improve handling tof defined symbols. gcc/c/ChangeLog: PR c++/33925 PR c/102867 * c-typeck.c (maybe_warn_for_null_address): Suppress warnings for code resulting from macro expansion. gcc/cp/ChangeLog: PR c++/33925 PR c/102867 * typeck.c (warn_for_null_address): Suppress warnings for code resulting from macro expansion. gcc/ChangeLog: PR c++/33925 PR c/102867 * doc/invoke.texi (-Waddress): Update. gcc/testsuite/ChangeLog: PR c++/33925 PR c/102867 * g++.dg/warn/Walways-true-2.C: Adjust to avoid a valid warning. * c-c++-common/Waddress-5.c: New test. * c-c++-common/Waddress-6.c: New test. * g++.dg/warn/Waddress-7.C: New test. * gcc.dg/Walways-true-2.c: Adjust to avoid a valid warning. * gcc.dg/weak/weak-3.c: Expect a warning.
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/c-common.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 86c007f..a25d59f 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -3413,16 +3413,43 @@ c_wrap_maybe_const (tree expr, bool non_const)
/* Return whether EXPR is a declaration whose address can never be NULL.
The address of the first struct member could be NULL only if it were
- accessed through a NULL pointer, and such an access would be invalid. */
+ accessed through a NULL pointer, and such an access would be invalid.
+ The address of a weak symbol may be null unless it has a definition. */
bool
decl_with_nonnull_addr_p (const_tree expr)
{
- return (DECL_P (expr)
- && (TREE_CODE (expr) == FIELD_DECL
- || TREE_CODE (expr) == PARM_DECL
- || TREE_CODE (expr) == LABEL_DECL
- || !DECL_WEAK (expr)));
+ if (!DECL_P (expr))
+ return false;
+
+ if (TREE_CODE (expr) == FIELD_DECL
+ || TREE_CODE (expr) == PARM_DECL
+ || TREE_CODE (expr) == LABEL_DECL)
+ return true;
+
+ if (!VAR_OR_FUNCTION_DECL_P (expr))
+ return false;
+
+ if (!DECL_WEAK (expr))
+ /* Ordinary (non-weak) symbols have nonnull addresses. */
+ return true;
+
+ if (DECL_INITIAL (expr) && DECL_INITIAL (expr) != error_mark_node)
+ /* Initialized weak symbols have nonnull addresses. */
+ return true;
+
+ if (DECL_EXTERNAL (expr) || !TREE_STATIC (expr))
+ /* Uninitialized extern weak symbols and weak symbols with no
+ allocated storage might have a null address. */
+ return false;
+
+ tree attribs = DECL_ATTRIBUTES (expr);
+ if (lookup_attribute ("weakref", attribs))
+ /* Weakref symbols might have a null address unless their referent
+ is known not to. Don't bother following weakref targets here. */
+ return false;
+
+ return true;
}
/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,