From: Dodji Seketeli <dodji@redhat.com>
To: Gabriel Dos Reis <gdr@integrable-solutions.net>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
Tom Tromey <tromey@redhat.com>,
Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH 08/11] Make conversion warnings work on NULL with -ftrack-macro-expansion
Date: Wed, 25 Apr 2012 13:42:00 -0000 [thread overview]
Message-ID: <m3zka0orlt.fsf@redhat.com> (raw)
In-Reply-To: <CAAiZkiBeYymQCajghoF5wYnJczeXOQnxxMuCCsMgg+G5UnV9gQ@mail.gmail.com> (Gabriel Dos Reis's message of "Wed, 11 Apr 2012 08:32:50 -0500")
Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
> On Wed, Apr 11, 2012 at 3:46 AM, Dodji Seketeli <dodji@redhat.com> wrote:
>> There are various conversion related warnings that trigger on
>> potentially dangerous uses of NULL (or __null). NULL is defined as a
>> macro in a system header, so calling warning or warning_at on a virtual
>> location of NULL yields no diagnostic. So the test accompanying this
>> patch (as well as others), was failling when run with
>> -ftrack-macro-expansion.
>>
>> I think it's necessary to use the location of NULL that is in the main
>> source code (instead of, e.g, the spelling location that is in the
>> system header where the macro is defined) in those cases. Note that for
>> __null, we don't have the issue.
>
> I like the idea. However, you should write a separate function
> (get_null_ptr_cst_location?) that computes the location of the null
> pointer constant token and call it from where you need the location.
OK. I have introduced such a new function and I gave it the slightly
more generic name expansion_point_location_if_in_system_header as I
suspect it can be useful for macros that are similar to NULL. I haven't
spotted such macros (from test regressions) yet, though.
Here is the updated patch that passes bootstrap with
-ftrack-macro-expansion turned off; it also passes bootstrap with
-ftrack-macro-expansion turned on with the whole series of patches I
have locally on my tree.
gcc/
* input.h (expansion_point_location_if_in_system_header): Declare
new function.
* input.c (expansion_point_location_if_in_system_header): Define it.
gcc/cp/
* call.c (conversion_null_warnings): Use the new
expansion_point_location_if_in_system_header.
* cvt.c (build_expr_type_conversion): Likewise.
* typeck.c (cp_build_binary_op): Likewise.
gcc/testsuite/
* g++.dg/warn/Wconversion-null-2.C: Add testing for __null,
alongside the previous testing for NULL.
---
gcc/cp/call.c | 16 ++++++++++-
gcc/cp/cvt.c | 16 ++++++++++-
gcc/cp/typeck.c | 18 +++++++++++--
gcc/input.c | 14 ++++++++++
gcc/input.h | 1 +
gcc/testsuite/g++.dg/warn/Wconversion-null-2.C | 31 +++++++++++++++++++++++-
6 files changed, 88 insertions(+), 8 deletions(-)
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index f9a7f08..1dc57fc 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -5598,12 +5598,24 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
&& ARITHMETIC_TYPE_P (totype))
{
+ /* The location of the warning here is most certainly the one for
+ the token that represented null_node in the source code.
+ That is either NULL or __null. If it is NULL, then that
+ macro is defined in a system header and, as a consequence,
+ warning_at won't emit any diagnostic for it. In this case,
+ we are going to resolve that location to the point in the
+ source code where the expression that triggered this error
+ message is, rather than the point where the NULL macro is
+ defined. */
+ source_location loc =
+ expansion_point_location_if_in_system_header (input_location);
+
if (fn)
- warning_at (input_location, OPT_Wconversion_null,
+ warning_at (loc, OPT_Wconversion_null,
"passing NULL to non-pointer argument %P of %qD",
argnum, fn);
else
- warning_at (input_location, OPT_Wconversion_null,
+ warning_at (loc, OPT_Wconversion_null,
"converting to non-pointer type %qT from NULL", totype);
}
diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 3dab372..8defc61 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -1472,8 +1472,20 @@ build_expr_type_conversion (int desires, tree expr, bool complain)
if (expr == null_node
&& (desires & WANT_INT)
&& !(desires & WANT_NULL))
- warning_at (input_location, OPT_Wconversion_null,
- "converting NULL to non-pointer type");
+ {
+ /* The location of the warning here is the one for the
+ (expansion of the) NULL macro, or for __null. If it is for
+ NULL, then, as that that macro is defined in a system header,
+ warning_at won't emit any diagnostic. In this case, we are
+ going to resolve that location to the point in the source
+ code where the expression that triggered this warning is,
+ rather than the point where the NULL macro is defined. */
+ source_location loc =
+ expansion_point_location_if_in_system_header (input_location);
+
+ warning_at (loc, OPT_Wconversion_null,
+ "converting NULL to non-pointer type");
+ }
basetype = TREE_TYPE (expr);
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index fb2f1bc..f453536 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3838,9 +3838,21 @@ cp_build_binary_op (location_t location,
|| (!null_ptr_cst_p (orig_op1)
&& !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
&& (complain & tf_warning))
- /* Some sort of arithmetic operation involving NULL was
- performed. */
- warning (OPT_Wpointer_arith, "NULL used in arithmetic");
+ {
+ /* Some sort of arithmetic operation involving NULL was
+ performed. The location of the warning here is the one for
+ the (expansion of the) NULL macro, or for __null. If it is
+ for NULL, then, as that that macro is defined in a system
+ header, warning_at won't emit any diagnostic. In this case,
+ we are going to resolve that location to the point in the
+ source code where the expression that triggered this warning
+ is, rather than the point where the NULL macro is
+ defined. */
+ source_location loc =
+ expansion_point_location_if_in_system_header (input_location);
+
+ warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
+ }
switch (code)
{
diff --git a/gcc/input.c b/gcc/input.c
index 260be7e..75457a3 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -162,6 +162,20 @@ expand_location_to_spelling_point (source_location loc)
return expand_location_1 (loc, /*expansion_piont_p=*/false);
}
+/* If LOCATION is in a sytem header and if it's a virtual location for
+ a token coming from the expansion of a macro M, unwind it to the
+ location of the expansion point of M. Otherwise, just return
+ LOCATION. */
+
+source_location
+expansion_point_location_if_in_system_header (source_location location)
+{
+ if (in_system_header_at (location))
+ location = linemap_resolve_location (line_table, location,
+ LRK_MACRO_EXPANSION_POINT,
+ NULL);
+ return location;
+}
#define ONE_K 1024
#define ONE_M (ONE_K * ONE_K)
diff --git a/gcc/input.h b/gcc/input.h
index f755cdf..f588838 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -40,6 +40,7 @@ extern char builtins_location_check[(BUILTINS_LOCATION
extern expanded_location expand_location (source_location);
extern const char * location_get_source_line(expanded_location xloc);
extern expanded_location expand_location_to_spelling_point (source_location);
+extern source_location expansion_point_location_if_in_system_header (source_location);
/* Historically GCC used location_t, while cpp used source_location.
This could be removed but it hardly seems worth the effort. */
diff --git a/gcc/testsuite/g++.dg/warn/Wconversion-null-2.C b/gcc/testsuite/g++.dg/warn/Wconversion-null-2.C
index dd498c1..a71551f 100644
--- a/gcc/testsuite/g++.dg/warn/Wconversion-null-2.C
+++ b/gcc/testsuite/g++.dg/warn/Wconversion-null-2.C
@@ -25,7 +25,7 @@ void l(long) {}
template <>
void l(long long) {}
-int main()
+void warn_for_NULL()
{
int i = NULL; // { dg-warning "" } converting NULL to non-pointer type
float z = NULL; // { dg-warning "" } converting NULL to non-pointer type
@@ -47,3 +47,32 @@ int main()
l(NULL); // No warning: NULL is used to implicitly instantiate the template
NULL && NULL; // No warning: converting NULL to bool is OK
}
+
+int warn_for___null()
+{
+ int i = __null; // { dg-warning "" } converting __null to non-pointer type
+ float z = __null; // { dg-warning "" } converting __null to non-pointer type
+ int a[2];
+
+ i != __null; // { dg-warning "" } __null used in arithmetic
+ __null != z; // { dg-warning "" } __null used in arithmetic
+ k != __null; // No warning: decay conversion
+ __null != a; // Likewise.
+ -__null; // { dg-warning "" } converting __null to non-pointer type
+ +__null; // { dg-warning "" } converting __null to non-pointer type
+ ~__null; // { dg-warning "" } converting __null to non-pointer type
+ a[__null] = 3; // { dg-warning "" } converting __null to non-pointer-type
+ i = __null; // { dg-warning "" } converting __null to non-pointer type
+ z = __null; // { dg-warning "" } converting __null to non-pointer type
+ k(__null); // { dg-warning "" } converting __null to int
+ g(__null); // { dg-warning "" } converting __null to int
+ h<__null>(); // No warning: __null bound to integer template parameter
+ l(__null); // No warning: __null is used to implicitly instantiate the template
+ __null && __null; // No warning: converting NULL to bool is OK
+}
+
+int main()
+{
+ warn_for_NULL();
+ warn_for___null();
+}
--
1.7.6.5
--
Dodji
next prev parent reply other threads:[~2012-04-25 13:42 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-10 14:53 Patches to enable -ftrack-macro-expansion by default Dodji Seketeli
2012-04-10 14:55 ` [PATCH 01/11] Fix cpp_sys_macro_p with -ftrack-macro-expansion Dodji Seketeli
2012-04-11 13:40 ` Jason Merrill
2012-04-14 18:05 ` Dodji Seketeli
2012-04-15 3:52 ` Jason Merrill
2012-04-10 14:57 ` [PATCH 02/11] Fix token pasting " Dodji Seketeli
2012-04-11 13:41 ` Jason Merrill
2012-04-14 18:07 ` Dodji Seketeli
2012-04-10 15:08 ` [PATCH 03/11] Fix PCH crash on GTYed pointer-to-scalar field of a Dodji Seketeli
2012-04-11 3:15 ` Laurynas Biveinis
2012-04-10 19:43 ` [PATCH 04/11] Fix expansion point loc for macro-like tokens Dodji Seketeli
2012-04-11 13:46 ` Jason Merrill
2012-04-25 9:07 ` Dodji Seketeli
2012-04-29 4:08 ` Jason Merrill
2012-04-29 16:55 ` Dodji Seketeli
2012-04-30 3:20 ` Jason Merrill
2012-04-10 19:50 ` [PATCH 05/11] Make expand_location resolve to locus in main source file Dodji Seketeli
2012-04-11 13:49 ` Jason Merrill
2012-04-25 9:50 ` Dodji Seketeli
2012-04-25 15:31 ` Dodji Seketeli
2012-04-29 4:11 ` Jason Merrill
2012-04-29 16:57 ` Dodji Seketeli
2012-04-10 19:56 ` [PATCH 06/11] Strip "<built-in>" loc from displayed expansion context Dodji Seketeli
2012-04-11 13:50 ` Jason Merrill
2012-04-10 20:31 ` [PATCH 07/11] Fix -Wuninitialized for -ftrack-macro-expansion Dodji Seketeli
2012-04-11 13:52 ` Jason Merrill
2012-04-11 9:00 ` [PATCH 09/11] Fix va_arg type location Dodji Seketeli
2012-04-11 13:36 ` Gabriel Dos Reis
2012-04-11 9:26 ` [PATCH 08/11] Make conversion warnings work on NULL with -ftrack-macro-expansion Dodji Seketeli
2012-04-11 13:33 ` Gabriel Dos Reis
2012-04-25 13:42 ` Dodji Seketeli [this message]
2012-04-25 14:07 ` Gabriel Dos Reis
2012-04-25 14:50 ` Dodji Seketeli
2012-04-25 15:22 ` Gabriel Dos Reis
2012-04-25 13:55 ` [PATCH 10/13] Fix location for static class members Dodji Seketeli
2012-04-25 14:13 ` Gabriel Dos Reis
2012-04-25 14:04 ` [PATCH 11/13] Fix va_start related location Dodji Seketeli
2012-04-25 14:11 ` Gabriel Dos Reis
2012-04-25 15:20 ` Dodji Seketeli
2012-04-25 15:23 ` Gabriel Dos Reis
2012-04-27 15:06 ` Dodji Seketeli
2012-04-27 21:46 ` Dodji Seketeli
2012-04-28 23:30 ` Gabriel Dos Reis
2012-04-25 14:17 ` [PATCH 12/13] Adjust relevant test cases wrt -ftrack-macro-expansion=[0|2] Dodji Seketeli
2012-04-25 15:25 ` Gabriel Dos Reis
2012-04-29 17:38 ` Dodji Seketeli
2012-04-30 6:23 ` Dodji Seketeli
2012-04-30 7:34 ` Gabriel Dos Reis
2012-04-30 16:09 ` Mike Stump
2012-05-02 17:22 ` Greta Yorsh
2012-04-25 23:23 ` Benjamin Kosnik
2012-04-25 14:33 ` [PATCH 13/13] Switch -ftrack-macro-expansion=2 on by default Dodji Seketeli
2012-04-25 15:27 ` Gabriel Dos Reis
2012-04-25 17:50 ` Tom Tromey
2012-04-30 11:47 ` Patches to enable -ftrack-macro-expansion " Dodji Seketeli
2012-05-08 10:31 ` Andreas Krebbel
2012-08-26 0:28 ` Gerald Pfeifer
2012-08-26 0:41 ` Gabriel Dos Reis
2012-08-26 8:32 ` Dodji Seketeli
2012-08-26 20:01 ` Gerald Pfeifer
2012-08-26 20:14 ` Gabriel Dos Reis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m3zka0orlt.fsf@redhat.com \
--to=dodji@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=gdr@integrable-solutions.net \
--cc=jason@redhat.com \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).