Patchwork [10/13] Fix location for static class members

login
register
mail settings
Submitter Dodji Seketeli
Date April 25, 2012, 1:55 p.m.
Message ID <m3liljq5km.fsf@redhat.com>
Download mbox | patch
Permalink /patch/154929/
State New
Headers show

Comments

Dodji Seketeli - April 25, 2012, 1:55 p.m.
Consider the test case g++.dg/other/offsetof5.C:

    #include <stddef.h>

    struct A
    {
      char c;
      int &i;
    };

    int j = offsetof (A, i);		// { dg-warning "invalid access|offsetof" }

    template <typename T>
    struct S
    {
      T h;
      T &i;
      static const int j = offsetof (S, i);	// { dg-warning "invalid access|offsetof" }
    };

    int k = S<int>::j;			// { dg-message "required from here" }

The second warning (that involves the instantiation of the S template)
is not emitted when -ftrack-macro-expansion is on.

This is because during the instantiation of the member j of S
template, the location that is used for the warning is the one for the
DECL j (set by instantiate_decl).  And that location is inaccurately
set to the locus of 'offsetof', which is a macro defined in a system
header, so it's discarded by the diagnostics machinery.

Note that when we reach the point where we emit the warning in
build_class_member_access_expr offsetof expression has long been
folded, so we cannot use e.g, the location of the ')' token that would
have been in the source code.  So I believe the location of 'j' is the
best we can get at this point.

The patch below sets the location of the DECL for 'j' to what I
believe is its precise location; with that, the test case passes with
and without -ftrack-macro-expansion.  But I had to adjust
g++.dg/template/sfinae6_neg.C for that.

Tested on x86_64-unknown-linux-gnu against trunk.

gcc/cp

	* decl.c (grokdeclarator): Use the location carried by the
	declarator for the DECL of the static class member.

gcc/testsuite/

	* g++.dg/template/sfinae6_neg.C: Adjust.
---
 gcc/cp/decl.c                               |    3 ++-
 gcc/testsuite/g++.dg/template/sfinae6_neg.C |    4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)
Gabriel Dos Reis - April 25, 2012, 2:13 p.m.
On Wed, Apr 25, 2012 at 8:55 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> Consider the test case g++.dg/other/offsetof5.C:
>
>    #include <stddef.h>
>
>    struct A
>    {
>      char c;
>      int &i;
>    };
>
>    int j = offsetof (A, i);            // { dg-warning "invalid access|offsetof" }
>
>    template <typename T>
>    struct S
>    {
>      T h;
>      T &i;
>      static const int j = offsetof (S, i);     // { dg-warning "invalid access|offsetof" }
>    };
>
>    int k = S<int>::j;                  // { dg-message "required from here" }
>
> The second warning (that involves the instantiation of the S template)
> is not emitted when -ftrack-macro-expansion is on.
>
> This is because during the instantiation of the member j of S
> template, the location that is used for the warning is the one for the
> DECL j (set by instantiate_decl).  And that location is inaccurately
> set to the locus of 'offsetof', which is a macro defined in a system
> header, so it's discarded by the diagnostics machinery.
>
> Note that when we reach the point where we emit the warning in
> build_class_member_access_expr offsetof expression has long been
> folded, so we cannot use e.g, the location of the ')' token that would
> have been in the source code.  So I believe the location of 'j' is the
> best we can get at this point.
>
> The patch below sets the location of the DECL for 'j' to what I
> believe is its precise location; with that, the test case passes with
> and without -ftrack-macro-expansion.  But I had to adjust
> g++.dg/template/sfinae6_neg.C for that.
>
> Tested on x86_64-unknown-linux-gnu against trunk.

OK.

>
> gcc/cp
>
>        * decl.c (grokdeclarator): Use the location carried by the
>        declarator for the DECL of the static class member.
>
> gcc/testsuite/
>
>        * g++.dg/template/sfinae6_neg.C: Adjust.
> ---
>  gcc/cp/decl.c                               |    3 ++-
>  gcc/testsuite/g++.dg/template/sfinae6_neg.C |    4 ++--
>  2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 28c7cee..40818a3 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -10267,7 +10267,8 @@ grokdeclarator (const cp_declarator *declarator,
>              {
>                /* C++ allows static class members.  All other work
>                   for this is done by grokfield.  */
> -               decl = build_lang_decl (VAR_DECL, unqualified_id, type);
> +               decl = build_lang_decl_loc (declarator->id_loc,
> +                                           VAR_DECL, unqualified_id, type);
>                set_linkage_for_static_data_member (decl);
>                /* Even if there is an in-class initialization, DECL
>                   is considered undefined until an out-of-class
> diff --git a/gcc/testsuite/g++.dg/template/sfinae6_neg.C b/gcc/testsuite/g++.dg/template/sfinae6_neg.C
> index d4be5dd..9b7bdfd1 100644
> --- a/gcc/testsuite/g++.dg/template/sfinae6_neg.C
> +++ b/gcc/testsuite/g++.dg/template/sfinae6_neg.C
> @@ -21,9 +21,9 @@ no_type check_is_callable2(...);
>  template<typename F, typename T1, typename T2 = T1>
>  struct is_callable2
>  {
> -  static const bool value =
> +  static const bool value = // { dg-error "within this context" }
>     (sizeof(check_is_callable2(type<F>(), type<T1>(), type<T2>()))
> -     == sizeof(yes_type)); // { dg-error "within this context" }
> +     == sizeof(yes_type));
>  };
>
>  #define JOIN( X, Y ) DO_JOIN( X, Y )
> --
>                Dodji

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 28c7cee..40818a3 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -10267,7 +10267,8 @@  grokdeclarator (const cp_declarator *declarator,
 	      {
 		/* C++ allows static class members.  All other work
 		   for this is done by grokfield.  */
-		decl = build_lang_decl (VAR_DECL, unqualified_id, type);
+		decl = build_lang_decl_loc (declarator->id_loc,
+					    VAR_DECL, unqualified_id, type);
 		set_linkage_for_static_data_member (decl);
 		/* Even if there is an in-class initialization, DECL
 		   is considered undefined until an out-of-class
diff --git a/gcc/testsuite/g++.dg/template/sfinae6_neg.C b/gcc/testsuite/g++.dg/template/sfinae6_neg.C
index d4be5dd..9b7bdfd1 100644
--- a/gcc/testsuite/g++.dg/template/sfinae6_neg.C
+++ b/gcc/testsuite/g++.dg/template/sfinae6_neg.C
@@ -21,9 +21,9 @@  no_type check_is_callable2(...);
 template<typename F, typename T1, typename T2 = T1>
 struct is_callable2
 {
-  static const bool value = 
+  static const bool value = // { dg-error "within this context" }
     (sizeof(check_is_callable2(type<F>(), type<T1>(), type<T2>()))
-     == sizeof(yes_type)); // { dg-error "within this context" }
+     == sizeof(yes_type));
 };
 
 #define JOIN( X, Y ) DO_JOIN( X, Y )