Warn for ignored ASM labels on typdef declarations

Message ID CAE+MWFv1LXf7KBMMRCcwvD9ux4wd4yVbjBc=0KtKxkWwUVUc0w@mail.gmail.com
State New
Headers show
Series
  • Warn for ignored ASM labels on typdef declarations
Related show

Commit Message

Will Hawkins April 18, 2018, 9:51 p.m.
Hello everyone!

First, let me ask for your patience -- this is the first patch I am
submitting to gcc. I tried very hard to follow the proper procedure,
but I am sure that I've missed something.

According to https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html#Asm-Labels
asm labels have no effect on anything other than variable and function
declarations. When an asm label is included on a typedef, the compiler
happily goes along but does not indicate anything to the user.

For instance, when parsing

typedef struct {} x asm ("X");

the compiler silently discards the asm label. This patch will generate
a warning (at -Wpedantic) for such a situation. This is in Bugzilla as
85444.

Again, I hope that I have done all the proper formatting so that it is
inline with all the contribution guidelines. Thank you for your
patience!

Will

2018-04-18  Will Hawkins  <whh8b@virginia.edu>

    * gcc/c/c-decl.c: Warn about ignored asm label for
    typedef declaration
    * gcc/cp/decl.c: Warn about ignored asm label for
    typedef declaration

     {

Comments

Joseph Myers April 24, 2018, 7:52 p.m. | #1
This patch is missing testcases, which need to be added to the testsuite 
for any such new feature, and when submitting a patch you should say 
explicitly in what configuration it was bootstrapped and tested without 
regressions.  Also, it's always incorrect to use -Wpedantic as the option 
controlling a warning that's not a warning about something not being part 
of the standard (since asm specifiers aren't part of the standard at all, 
it's thus wrong for -Wpedantic to control any diagnostic about them other 
than for them being used at all).

-- 
Joseph S. Myers
joseph@codesourcery.com

Patch

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index f0198ec..e9c0a72 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -5166,7 +5166,11 @@  finish_decl (tree decl, location_t init_loc, tree init,
       if (!DECL_FILE_SCOPE_P (decl)
       && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
     add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
-
+      if (asmspec_tree != NULL_TREE)
+    {
+      warning (OPT_Wpedantic, "asm-specifier is ignored in "
+           "typedef declaration");
+    }
       rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
     }

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 44a152b..88b4b94 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7069,6 +7069,11 @@  cp_finish_decl (tree decl, tree init, bool
init_const_expr_p,
   /* Take care of TYPE_DECLs up front.  */
   if (TREE_CODE (decl) == TYPE_DECL)
     {
+      if (asmspec_tree != NULL_TREE)
+    {
+      warning (OPT_Wpedantic, "asm-specifier is ignored for "
+           "typedef declarations");
+    }
       if (type != error_mark_node
       && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))