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.
--- 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. */
Patch submitted: https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549453.html
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.
Fixed on trunk
*** Bug 103131 has been marked as a duplicate of this bug. ***
*** Bug 67013 has been marked as a duplicate of this bug. ***
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 = {}; };