Bug 96068 (cwg569) - Extra semicolon outside of a function should be allowed after c++11?
Summary: Extra semicolon outside of a function should be allowed after c++11?
Status: RESOLVED FIXED
Alias: cwg569
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: 11.0
Assignee: Jonathan Wakely
URL:
Keywords: patch, rejects-valid
: 67013 103131 (view as bug list)
Depends on:
Blocks: c++-core-issues
  Show dependency treegraph
 
Reported: 2020-07-05 15:33 UTC by Haoxin Tu
Modified: 2024-02-05 07:19 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-07-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Haoxin Tu 2020-07-05 15:33:52 UTC
Hi, all.

As the summary, extra ";" should be allowed in GCC after -std=c++11 and shouldn't give a diagnostic message about it? I know this is forbidden in c++98.

For example,

$cat test.cc
void foo() { };

$g++ -c -std=c++98 -pedantic-errors test.cc
test.cc:1:16: error: extra ';' [-Wpedantic]
    1 | void foo () { };
      |                ^

$g++ -c -std=c++11 -pedantic-errors test.cc
test.cc:1:16: error: extra ';' [-Wpedantic]
    1 | void foo () { };
      |                ^

While in Clang, this is accepted in c++11 or later.

$clang++ -c -std=c++98 -pedantic-errors test.cc
test.cc:1:15: error: extra ';' outside of a function is a C++11 extension [-Werror,-Wc++11-extra-semi]
void foo() { };
              ^
1 error generated.

There is also a related bug report in llvm: 

https://bugs.llvm.org/show_bug.cgi?id=46288

They suggested that "If GCC rejects this under -pedantic-errors in C++11 or later, I think that's a GCC bug."

I try to find a similar bug report in GCC but failed. 

Did I miss something? Thank you very much.
Comment 1 Jonathan Wakely 2020-07-06 10:48:54 UTC
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -13502,10 +13502,11 @@ cp_parser_toplevel_declaration (cp_parser* parser)
     cp_parser_pragma (parser, pragma_external, NULL);
   else if (token->type == CPP_SEMICOLON)
     {
-      /* A declaration consisting of a single semicolon is
-        invalid.  Allow it unless we're being pedantic.  */
       cp_lexer_consume_token (parser->lexer);
-      pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
+      /* A declaration consisting of a single semicolon is invalid
+       * before C++11.  Allow it unless we're being pedantic.  */
+      if (cxx_dialect < cxx11)
+       pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
     }
   else
     /* Parse the declaration itself.  */
Comment 2 Jonathan Wakely 2020-07-06 15:02:33 UTC
Patch submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549453.html
Comment 3 GCC Commits 2020-07-06 16:12:55 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:92414bb6b077642eefc24080637b6bc766499391

commit r11-1852-g92414bb6b077642eefc24080637b6bc766499391
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Jul 6 15:58:33 2020 +0100

    c++: Allow empty-declaration in C++11 and later (PR 96068)
    
    Since C++11 a semim-colon on its own at namespace scope is not invalid,
    so do not give a pedantic diagnostic about it.
    
    gcc/cp/ChangeLog:
    
            PR c++/96068
            * parser.c (cp_parser_toplevel_declaration): Only do pedwarn for
            empty-declaration in C++98.
    
    gcc/testsuite/ChangeLog:
    
            * g++.old-deja/g++.bugs/900404_04.C: Add c++98_only selector to
            dg-error for extra ';'.
            * g++.old-deja/g++.law/missed-error2.C: Likewise.
Comment 4 Jonathan Wakely 2020-07-06 16:13:46 UTC
Fixed on trunk
Comment 5 Andrew Pinski 2021-11-08 10:31:59 UTC
*** Bug 103131 has been marked as a duplicate of this bug. ***
Comment 6 Andrew Pinski 2021-12-07 05:13:10 UTC
*** Bug 67013 has been marked as a duplicate of this bug. ***
Comment 7 Jakub Jelinek 2022-05-09 16:19:01 UTC
I wonder if we shouldn't have a style warning for this though, if the empty statement's ; is immediately after } in the source, tell user it is unnecessary,
while not warning if there is some comment or newline or whitespace etc. in between.
Ran into this while backporting PR105256 to gcc-10 branch which doesn't have this fix and the testcase contains such a useless semicolon:
void S::Prefs::Load() {
  *this = {};
};