Patch adds a fix-it hint to a pre-existing pedwarn to make
it easier for IDEs to assist in fixing the mistake.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.

OK for trunk?

gcc/c/ChangeLog:
        * c-parser.c (c_parser_struct_or_union_specifier): Add fix-it
        hint for removing extra semicolon.

gcc/testsuite/ChangeLog:
        * gcc.dg/semicolon-fixits.c: New test case.
---
 gcc/c/c-parser.c                        |  9 +++++++--
 gcc/testsuite/gcc.dg/semicolon-fixits.c | 17 +++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/semicolon-fixits.c

diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 988369e..9398652 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -2948,8 +2948,13 @@ c_parser_struct_or_union_specifier (c_parser *parser)
          /* Parse any stray semicolon.  */
          if (c_parser_next_token_is (parser, CPP_SEMICOLON))
            {
-             pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
-                      "extra semicolon in struct or union specified");
+             location_t semicolon_loc
+               = c_parser_peek_token (parser)->location;
+             gcc_rich_location richloc (semicolon_loc);
+             richloc.add_fixit_remove ();
+             pedwarn_at_rich_loc
+               (&richloc, OPT_Wpedantic,
+                "extra semicolon in struct or union specified");
              c_parser_consume_token (parser);
              continue;
            }
diff --git a/gcc/testsuite/gcc.dg/semicolon-fixits.c 
b/gcc/testsuite/gcc.dg/semicolon-fixits.c
new file mode 100644
index 0000000..e7d5322
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/semicolon-fixits.c
@@ -0,0 +1,17 @@
+/* { dg-options "-fdiagnostics-show-caret -Wpedantic" } */
+
+/* Struct with extra semicolon.  */
+struct s1 { int i;; }; /* { dg-warning "19: extra semicolon in struct or union 
specified" } */
+/* { dg-begin-multiline-output "" }
+ struct s1 { int i;; };
+                   ^
+                   -
+   { dg-end-multiline-output "" } */
+
+/* Union with extra semicolon.  */
+union u1 { int i;; }; /* { dg-warning "18: extra semicolon in struct or union 
specified" } */
+/* { dg-begin-multiline-output "" }
+ union u1 { int i;; };
+                  ^
+                  -
+   { dg-end-multiline-output "" } */
-- 
1.8.5.3

Reply via email to