Hi,

today I realized that if we move further up the "famous" location_t loc declaration in grokdeclarator we can often pass a precise location when building TYPE_DECLs for typedef names too, thus, in particular, profitably use DECL_SOURCE_LOCATION in a grokbitfield error and also enabling further improvements. Tested x86_64-linux.

Thanks, Paolo.

///////////////////////

/cp
2019-01-13  Paolo Carlini  <paolo.carl...@oracle.com>

        * decl.c (grokdeclarator): Move further up the location_t loc
        declaration and use the location when building a TYPE_DECL for
        a typedef name.
        * decl2.c (grokbitfield): Use DECL_SOURCE_LOCATION in the error
        about an ill-formed typedef as bit-field.

/testsuite
2019-01-13  Paolo Carlini  <paolo.carl...@oracle.com>

        * g++.dg/diagnostic/bitfld3.C: New.
Index: cp/decl.c
===================================================================
--- cp/decl.c   (revision 267903)
+++ cp/decl.c   (working copy)
@@ -11933,6 +11934,8 @@ grokdeclarator (const cp_declarator *declarator,
        }
     }
 
+  location_t loc = declarator ? declarator->id_loc : input_location;
+
   /* If this is declaring a typedef name, return a TYPE_DECL.  */
   if (typedef_p && decl_context != TYPENAME)
     {
@@ -11978,9 +11981,9 @@ grokdeclarator (const cp_declarator *declarator,
        }
 
       if (decl_context == FIELD)
-       decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
+       decl = build_lang_decl_loc (loc, TYPE_DECL, unqualified_id, type);
       else
-       decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
+       decl = build_decl (loc, TYPE_DECL, unqualified_id, type);
 
       if (decl_context != FIELD)
        {
@@ -12221,7 +12224,6 @@ grokdeclarator (const cp_declarator *declarator,
 
   {
     tree decl = NULL_TREE;
-    location_t loc = declarator ? declarator->id_loc : input_location;
 
     if (decl_context == PARM)
       {
Index: cp/decl2.c
===================================================================
--- cp/decl2.c  (revision 267903)
+++ cp/decl2.c  (working copy)
@@ -1038,7 +1039,8 @@ grokbitfield (const cp_declarator *declarator,
 
   if (TREE_CODE (value) == TYPE_DECL)
     {
-      error ("cannot declare %qD to be a bit-field type", value);
+      error_at (DECL_SOURCE_LOCATION (value),
+               "cannot declare %qD to be a bit-field type", value);
       return NULL_TREE;
     }
 
Index: testsuite/g++.dg/diagnostic/bitfld3.C
===================================================================
--- testsuite/g++.dg/diagnostic/bitfld3.C       (nonexistent)
+++ testsuite/g++.dg/diagnostic/bitfld3.C       (working copy)
@@ -0,0 +1,5 @@
+struct S
+{
+  typedef int i : 3;  // { dg-error "15:cannot declare .i." }
+  typedef int : 3;  // { dg-error "cannot declare" }
+};

Reply via email to