public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number
@ 2023-03-31 10:00 jg at jguk dot org
  2023-03-31 10:17 ` [Bug c++/109356] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-03-31 10:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

            Bug ID: 109356
           Summary: Enhancement idea to provide clearer missing brace line
                    number
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jg at jguk dot org
  Target Milestone: ---

Sometimes due to human error there is a missing brace.
Enhancement idea to provide the missing brace line number. Is that possible?

Godbolt trunk gcc does show there is an error, but doesn't pick out line 5.
Would be great if it could

https://godbolt.org/z/nE13h1r13

<source>:8:2: error: expected '}' before ';' token
    8 | };
      |  ^
<source>:2:1: note: to match this '{'
    2 | {
      | ^
<source>:8:2: error: too many initializers for 'const char* [2]'
    8 | };
      |  ^



static const char * list[][2] =
{
    {"A", "B"},
    {"C", "D"},
    {"E", "F",
    {"G", "H"},
    {"I", "J"}
};

int main()
{
}

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
@ 2023-03-31 10:17 ` redi at gcc dot gnu.org
  2023-03-31 10:50 ` xry111 at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-31 10:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |diagnostic

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
  2023-03-31 10:17 ` [Bug c++/109356] " redi at gcc dot gnu.org
@ 2023-03-31 10:50 ` xry111 at gcc dot gnu.org
  2023-03-31 19:52 ` jg at jguk dot org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-03-31 10:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
I believe attempting to doing so would result exponential time complexity.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
  2023-03-31 10:17 ` [Bug c++/109356] " redi at gcc dot gnu.org
  2023-03-31 10:50 ` xry111 at gcc dot gnu.org
@ 2023-03-31 19:52 ` jg at jguk dot org
  2023-04-04 20:31 ` jg at jguk dot org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-03-31 19:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #2 from Jonny Grant <jg at jguk dot org> ---
(In reply to Xi Ruoyao from comment #1)
> I believe attempting to doing so would result exponential time complexity.


Ah ok, I didn't realise it would be complex.

I don't know enough about the internals, I was just thinking it could use the
[2] to know that it had got 2 elements, and that } closing brace was needed
before the comma.

ie.
<source>:5:10: error: expected '}' before ',' token
    5 | {"E", "F",
      |          ^

Or I thought it could just count the number of braces, and when it got to the
3rd open brace {


ie.
<source>:6:2: error: expected '}' before '{' token
    5 | {"E", "F",
      | ^

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (2 preceding siblings ...)
  2023-03-31 19:52 ` jg at jguk dot org
@ 2023-04-04 20:31 ` jg at jguk dot org
  2023-04-05  9:22 ` xry111 at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-04-04 20:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #3 from Jonny Grant <jg at jguk dot org> ---
A different example where GCC does a good job of indicating the line number of
a missing comma problem.


https://godbolt.org/z/asGhE3W17


<source>:6:5: error: expected '}' before '{' token
    6 |     {"G", "H"},
      |     ^
<source>:2:1: note: to match this '{'
    2 | {
      | ^
<source>:6:5: error: expected ',' or ';' before '{' token
    6 |     {"G", "H"},
      |     ^

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (3 preceding siblings ...)
  2023-04-04 20:31 ` jg at jguk dot org
@ 2023-04-05  9:22 ` xry111 at gcc dot gnu.org
  2023-04-06 22:19 ` jg at jguk dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-04-05  9:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |WONTFIX
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Jonny Grant from comment #3)
> A different example where GCC does a good job of indicating the line number
> of a missing comma problem.
> 
> 
> https://godbolt.org/z/asGhE3W17
> 
> 
> <source>:6:5: error: expected '}' before '{' token
>     6 |     {"G", "H"},
>       |     ^
> <source>:2:1: note: to match this '{'
>     2 | {
>       | ^
> <source>:6:5: error: expected ',' or ';' before '{' token
>     6 |     {"G", "H"},
>       |     ^

This is easy because the parser has to insert a comma here to correct the
syntax.  But for a missing } it can be added at one of multiple places to make
the syntax correct.

The parser always keeps progressing unless it's sure there is a syntax error. 
Note that 

static const char * list[][2] =
{
    {"A", "B"},
    {"C", "D"},
    {"E", "F",
    {"G", "H"},
    {"I", "J"}
}};

is NOT a syntax error.  It's a semantic error (violating a constraint) but the
parser does not know about semantics (i.e. the parser does not know what "[2]"
means at all).  So the parser cannot be sure about the syntax error until the
last line.

Mixing the semantic analysis into the parser is not acceptable because it's not
how a compiler is implemented.

Make the parser try different locations and pass all possible syntax tree to
the further passes is at least quadratic behavior and not acceptable.

I'll say this WONTFIX.  If someone knows how to do this in a rational way
please reopen.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (4 preceding siblings ...)
  2023-04-05  9:22 ` xry111 at gcc dot gnu.org
@ 2023-04-06 22:19 ` jg at jguk dot org
  2023-04-08 14:57 ` jg at jguk dot org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-04-06 22:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #5 from Jonny Grant <jg at jguk dot org> ---
I see it is more complicated than I imagined. Thank you for looking into it.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (5 preceding siblings ...)
  2023-04-06 22:19 ` jg at jguk dot org
@ 2023-04-08 14:57 ` jg at jguk dot org
  2023-04-09  9:04 ` xry111 at gcc dot gnu.org
  2023-04-09 20:10 ` jg at jguk dot org
  8 siblings, 0 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-04-08 14:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #6 from Jonny Grant <jg at jguk dot org> ---

Tried a few other compilers on godbolt. 

ICC gets the warning on line 6
https://godbolt.org/z/fYb9c8f61

nvc++ gives the warning on line 6 
https://godbolt.org/z/xvh67odzY

MSVC and Clang don't.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (6 preceding siblings ...)
  2023-04-08 14:57 ` jg at jguk dot org
@ 2023-04-09  9:04 ` xry111 at gcc dot gnu.org
  2023-04-09 20:10 ` jg at jguk dot org
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-04-09  9:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #7 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Jonny Grant from comment #6)
> Tried a few other compilers on godbolt. 
> 
> ICC gets the warning on line 6
> https://godbolt.org/z/fYb9c8f61
> 
> nvc++ gives the warning on line 6 
> https://godbolt.org/z/xvh67odzY
> 
> MSVC and Clang don't.

This is just blind luck.  All of these compilers attempt to "fix" the syntax
error by adding "}" at line 8 (not line 6!) and produce further warnings.  It's
just ICC and nvc++ report "excess elements in initializer" at the beginning of
the initializer, while the other compilers report it at the end.

Strictly speaking all of them are false warnings as there is no excess elements
in the initializer (according to the programmer's mind).  But the only thing
the parser can do is guessing the programmer wanted a "}" at line 8.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/109356] Enhancement idea to provide clearer missing brace line number
  2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
                   ` (7 preceding siblings ...)
  2023-04-09  9:04 ` xry111 at gcc dot gnu.org
@ 2023-04-09 20:10 ` jg at jguk dot org
  8 siblings, 0 replies; 10+ messages in thread
From: jg at jguk dot org @ 2023-04-09 20:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109356

--- Comment #8 from Jonny Grant <jg at jguk dot org> ---
(In reply to Xi Ruoyao from comment #7)
> (In reply to Jonny Grant from comment #6)
> > Tried a few other compilers on godbolt. 
> > 
> > ICC gets the warning on line 6
> > https://godbolt.org/z/fYb9c8f61
> > 
> > nvc++ gives the warning on line 6 
> > https://godbolt.org/z/xvh67odzY
> > 
> > MSVC and Clang don't.
> 
> This is just blind luck.  All of these compilers attempt to "fix" the syntax
> error by adding "}" at line 8 (not line 6!) and produce further warnings. 
> It's just ICC and nvc++ report "excess elements in initializer" at the
> beginning of the initializer, while the other compilers report it at the end.
> 
> Strictly speaking all of them are false warnings as there is no excess
> elements in the initializer (according to the programmer's mind).  But the
> only thing the parser can do is guessing the programmer wanted a "}" at line
> 8.

ok, thank you for your further replies

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-04-09 20:10 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-31 10:00 [Bug c++/109356] New: Enhancement idea to provide clearer missing brace line number jg at jguk dot org
2023-03-31 10:17 ` [Bug c++/109356] " redi at gcc dot gnu.org
2023-03-31 10:50 ` xry111 at gcc dot gnu.org
2023-03-31 19:52 ` jg at jguk dot org
2023-04-04 20:31 ` jg at jguk dot org
2023-04-05  9:22 ` xry111 at gcc dot gnu.org
2023-04-06 22:19 ` jg at jguk dot org
2023-04-08 14:57 ` jg at jguk dot org
2023-04-09  9:04 ` xry111 at gcc dot gnu.org
2023-04-09 20:10 ` jg at jguk dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).