* [C PATCH] Improve warn msg (PR c/43395)
@ 2014-04-29 21:18 Marek Polacek
2014-04-30 21:16 ` Jeff Law
2014-05-02 17:01 ` Joseph S. Myers
0 siblings, 2 replies; 5+ messages in thread
From: Marek Polacek @ 2014-04-29 21:18 UTC (permalink / raw)
To: GCC Patches; +Cc: Jason Merrill, Joseph S. Myers
It's correct to warn about returning an address of a local label,
but it's clumsy to say it's "local variable. We can easily distinguish
between a label and a variable.
Regtested/bootstrapped on x86_64-linux, ok for trunk?
2014-04-29 Marek Polacek <polacek@redhat.com>
PR c/43395
c/
* c-typeck.c (c_finish_return): Distinguish between label and variable
when warning about returning local address.
cp/
* typeck.c (maybe_warn_about_returning_address_of_local): Distinguish
between label and variable when warning about returning local address.
testsuite/
* c-c++-common/pr43395.c: New test.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 62c72df..6c34203 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -9264,7 +9264,8 @@ c_finish_return (location_t loc, tree retval, tree origtype)
&& DECL_CONTEXT (inner) == current_function_decl)
warning_at (loc,
OPT_Wreturn_local_addr, "function returns address "
- "of local variable");
+ "of %s", TREE_CODE (inner) == LABEL_DECL
+ ? "label" : "local variable");
break;
default:
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index 9a80727..5eff7b9 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -8315,8 +8315,9 @@ maybe_warn_about_returning_address_of_local (tree retval)
warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
whats_returned);
else
- warning (OPT_Wreturn_local_addr, "address of local variable %q+D returned",
- whats_returned);
+ warning (OPT_Wreturn_local_addr, "address of %s %q+D returned",
+ TREE_CODE (whats_returned) == LABEL_DECL
+ ? "label" : "local variable", whats_returned);
return;
}
}
diff --git gcc/testsuite/c-c++-common/pr43395.c gcc/testsuite/c-c++-common/pr43395.c
index e69de29..92f048d 100644
--- gcc/testsuite/c-c++-common/pr43395.c
+++ gcc/testsuite/c-c++-common/pr43395.c
@@ -0,0 +1,30 @@
+/* PR c/43395 */
+/* { dg-do compile } */
+
+void *
+foo (void)
+{
+lab:
+ return &&lab;
+/* { dg-warning "function returns address of label" "" { target c } 8 } */
+/* { dg-warning "address of label" "" { target c++ } 7 } */
+}
+
+void *
+bar (void)
+{
+ __label__ lab;
+lab:
+ return &&lab;
+/* { dg-warning "function returns address of label" "" { target c } 18 } */
+/* { dg-warning "address of label" "" { target c++ } 17 } */
+}
+
+void *
+baz (void)
+{
+ int i;
+ return &i;
+/* { dg-warning "function returns address of local variable" "" { target c } 27 } */
+/* { dg-warning "address of local variable" "" { target c++ } 26 } */
+}
Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [C PATCH] Improve warn msg (PR c/43395)
2014-04-29 21:18 [C PATCH] Improve warn msg (PR c/43395) Marek Polacek
@ 2014-04-30 21:16 ` Jeff Law
2014-05-02 17:01 ` Joseph S. Myers
1 sibling, 0 replies; 5+ messages in thread
From: Jeff Law @ 2014-04-30 21:16 UTC (permalink / raw)
To: Marek Polacek, GCC Patches; +Cc: Jason Merrill, Joseph S. Myers
On 04/29/14 15:17, Marek Polacek wrote:
> It's correct to warn about returning an address of a local label,
> but it's clumsy to say it's "local variable. We can easily distinguish
> between a label and a variable.
>
> Regtested/bootstrapped on x86_64-linux, ok for trunk?
>
> 2014-04-29 Marek Polacek <polacek@redhat.com>
>
> PR c/43395
> c/
> * c-typeck.c (c_finish_return): Distinguish between label and variable
> when warning about returning local address.
> cp/
> * typeck.c (maybe_warn_about_returning_address_of_local): Distinguish
> between label and variable when warning about returning local address.
> testsuite/
> * c-c++-common/pr43395.c: New test.
OK. Thanks.
Jeff
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [C PATCH] Improve warn msg (PR c/43395)
2014-04-29 21:18 [C PATCH] Improve warn msg (PR c/43395) Marek Polacek
2014-04-30 21:16 ` Jeff Law
@ 2014-05-02 17:01 ` Joseph S. Myers
2014-05-02 17:35 ` Marek Polacek
1 sibling, 1 reply; 5+ messages in thread
From: Joseph S. Myers @ 2014-05-02 17:01 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jason Merrill
On Tue, 29 Apr 2014, Marek Polacek wrote:
> It's correct to warn about returning an address of a local label,
> but it's clumsy to say it's "local variable. We can easily distinguish
> between a label and a variable.
>
> Regtested/bootstrapped on x86_64-linux, ok for trunk?
You always need to have complete sentences in diagnostics for the sake of
translation. Thus, you need two separate warning_at calls, one with each
version of the message. (Using ? : for the whole format string isn't
sufficient; I think xgettext only extracts one of the two alternatives for
translation if you do that.)
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [C PATCH] Improve warn msg (PR c/43395)
2014-05-02 17:01 ` Joseph S. Myers
@ 2014-05-02 17:35 ` Marek Polacek
2014-05-02 17:57 ` Joseph S. Myers
0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2014-05-02 17:35 UTC (permalink / raw)
To: Joseph S. Myers; +Cc: GCC Patches, Jason Merrill
On Fri, May 02, 2014 at 05:01:18PM +0000, Joseph S. Myers wrote:
> On Tue, 29 Apr 2014, Marek Polacek wrote:
>
> > It's correct to warn about returning an address of a local label,
> > but it's clumsy to say it's "local variable. We can easily distinguish
> > between a label and a variable.
> >
> > Regtested/bootstrapped on x86_64-linux, ok for trunk?
>
> You always need to have complete sentences in diagnostics for the sake of
> translation. Thus, you need two separate warning_at calls, one with each
> version of the message. (Using ? : for the whole format string isn't
> sufficient; I think xgettext only extracts one of the two alternatives for
> translation if you do that.)
Ooops. Is it ok to fix it up with this patch then?
2014-05-02 Marek Polacek <polacek@redhat.com>
c/
* c-typeck.c (c_finish_return): Separate warning_at calls.
cp/
* typeck.c (maybe_warn_about_returning_address_of_local): Separate
warning_at calls.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index b95fd89..f7ad91e 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -9273,10 +9273,14 @@ c_finish_return (location_t loc, tree retval, tree origtype)
&& !DECL_EXTERNAL (inner)
&& !TREE_STATIC (inner)
&& DECL_CONTEXT (inner) == current_function_decl)
- warning_at (loc,
- OPT_Wreturn_local_addr, "function returns address "
- "of %s", TREE_CODE (inner) == LABEL_DECL
- ? "label" : "local variable");
+ {
+ if (TREE_CODE (inner) == LABEL_DECL)
+ warning_at (loc, OPT_Wreturn_local_addr,
+ "function returns address of label");
+ else
+ warning_at (loc, OPT_Wreturn_local_addr,
+ "function returns address of local variable");
+ }
break;
default:
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index 8b7cb8d..7b28a9a 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -8309,10 +8309,12 @@ maybe_warn_about_returning_address_of_local (tree retval)
if (TREE_CODE (valtype) == REFERENCE_TYPE)
warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
whats_returned);
+ else if (TREE_CODE (whats_returned) == LABEL_DECL)
+ warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
+ whats_returned);
else
- warning (OPT_Wreturn_local_addr, "address of %s %q+D returned",
- TREE_CODE (whats_returned) == LABEL_DECL
- ? "label" : "local variable", whats_returned);
+ warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
+ "returned", whats_returned);
return;
}
}
Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [C PATCH] Improve warn msg (PR c/43395)
2014-05-02 17:35 ` Marek Polacek
@ 2014-05-02 17:57 ` Joseph S. Myers
0 siblings, 0 replies; 5+ messages in thread
From: Joseph S. Myers @ 2014-05-02 17:57 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Jason Merrill
On Fri, 2 May 2014, Marek Polacek wrote:
> Ooops. Is it ok to fix it up with this patch then?
>
> 2014-05-02 Marek Polacek <polacek@redhat.com>
>
> c/
> * c-typeck.c (c_finish_return): Separate warning_at calls.
> cp/
> * typeck.c (maybe_warn_about_returning_address_of_local): Separate
> warning_at calls.
OK.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-05-02 17:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-29 21:18 [C PATCH] Improve warn msg (PR c/43395) Marek Polacek
2014-04-30 21:16 ` Jeff Law
2014-05-02 17:01 ` Joseph S. Myers
2014-05-02 17:35 ` Marek Polacek
2014-05-02 17:57 ` Joseph S. Myers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).