public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 0/3] C/C++: show pertinent open token when missing a close token
@ 2017-07-11 14:51 David Malcolm
  2017-07-11 14:51 ` [PATCH 1/3] matching tokens: c-family parts David Malcolm
                   ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: David Malcolm @ 2017-07-11 14:51 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

[This patch kit is effectively just one patch; I've split it up into
 3 parts, in the hope of making it easier to review:
 the c-family parts, the C parts, and the C++ parts]

This patch adds a hint to the user to various errors generated
in the C frontend by:

  c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")
  c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>")

etc (where there's a non-NULL msgid), and in the C++ frontend by:

  cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
  cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE)

The hint shows the user where the pertinent open paren or open brace
is, which ought to be very helpful for complicated nested collections
of parentheses, and somewhat helpful even for simple cases;
consider e.g.:

  ...lots of lines of code...
  extern "C" {
  ...lots of lines of code...
  int test ();
  EOF

where the user currently has to hunt through the source file to find
the unclosed '{':

test.cc:262:12: error: expected '}' at end of input
 int test ();
            ^

With this patch we tell them:

test.cc:262:12: error: expected '}' at end of input
 int test ();
            ^
test.cc:98:12: note: to match this '{'
 extern "C" {
            ^

The patch avoids using a note if the tokens are on the same line,
highlighting the unclosed open token with an underline:

test.c:3:32: error: expected ')' before ';' token
   return ((b * b) - (4 * a * c);
          ~                     ^

The bulk of the changes in the patch are to the parsers, done using
new classes "matching_braces" and "matching_parens", which stash the
location of the opening token during parsing, so that e.g.:

  if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
     return;
  ...do stuff...
  c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");

becomes:

  matching_parens parens;
  if (!parens.require_open (parser))
     return;
  ...do stuff...
  parens.require_close (parser);

The exact implementation of these classes varies somewhat between the
C and C++ frontends, to deal with implementation differences between
them (I tried to keep them as similar as possible).

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu;
adds 23 PASS results to gcc.sum; adds 99 PASS results to g++.sum.

OK for trunk?

 gcc/c-family/c-common.c                            |  17 +-
 gcc/c-family/c-common.h                            |   3 +-
 gcc/c/c-parser.c                                   | 647 ++++++++++------
 gcc/c/c-parser.h                                   |   8 +-
 gcc/cp/parser.c                                    | 821 ++++++++++++++-------
 gcc/testsuite/c-c++-common/missing-close-symbol.c  |  33 +
 gcc/testsuite/c-c++-common/missing-symbol.c        |  50 ++
 .../g++.dg/diagnostic/unclosed-extern-c.C          |   3 +
 .../g++.dg/diagnostic/unclosed-function.C          |   3 +
 .../g++.dg/diagnostic/unclosed-namespace.C         |   2 +
 gcc/testsuite/g++.dg/diagnostic/unclosed-struct.C  |   3 +
 gcc/testsuite/g++.dg/parse/pragma2.C               |   4 +-
 gcc/testsuite/gcc.dg/unclosed-init.c               |   3 +
 13 files changed, 1084 insertions(+), 513 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/missing-close-symbol.c
 create mode 100644 gcc/testsuite/c-c++-common/missing-symbol.c
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/unclosed-extern-c.C
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/unclosed-function.C
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/unclosed-namespace.C
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/unclosed-struct.C
 create mode 100644 gcc/testsuite/gcc.dg/unclosed-init.c

-- 
1.8.5.3

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

end of thread, other threads:[~2017-08-10 13:35 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-11 14:51 [PATCH 0/3] C/C++: show pertinent open token when missing a close token David Malcolm
2017-07-11 14:51 ` [PATCH 1/3] matching tokens: c-family parts David Malcolm
2017-07-18 17:23   ` Marek Polacek
2017-07-11 14:51 ` [PATCH 3/3] matching tokens: C++ parts David Malcolm
2017-07-12 13:13   ` Trevor Saunders
2017-07-12 15:12     ` Martin Sebor
2017-07-16 17:55       ` Trevor Saunders
2017-08-01 19:47     ` [PATCH 0/3 v2] C/C++: show pertinent open token when missing a close token David Malcolm
2017-08-01 19:47       ` [PATCH 1/3] matching tokens: c-family parts David Malcolm
2017-08-03 17:22         ` Jeff Law
2017-08-01 19:47       ` [PATCH 3/3] matching tokens: C++ parts (v2) David Malcolm
2017-08-07 18:25         ` Jason Merrill
2017-08-08 20:26           ` [PATCH] matching tokens: C++ parts (v3) David Malcolm
2017-08-08 20:49             ` [PATCH] Changes for v3 of the C++ patch David Malcolm
2017-08-09 19:26             ` [PATCH] matching tokens: C++ parts (v3) Jason Merrill
2017-08-01 19:47       ` [PATCH 2/3] Matching tokens: C parts (v2) David Malcolm
2017-08-03 17:34         ` Jeff Law
2017-08-04 14:32           ` David Malcolm
2017-08-04 18:09             ` Jeff Law
2017-08-08 20:37               ` David Malcolm
2017-08-09  6:50                 ` Marek Polacek
2017-08-02  3:03       ` [PATCH 0/3 v2] C/C++: show pertinent open token when missing a close token Trevor Saunders
2017-08-10 13:39       ` [committed, v3] " David Malcolm
2017-07-11 14:51 ` [PATCH 2/3] matching tokens: C parts David Malcolm
2017-07-11 17:28 ` [PATCH 0/3] C/C++: show pertinent open token when missing a close token Martin Sebor
2017-07-11 18:32   ` David Malcolm
2017-07-11 19:30     ` Martin Sebor

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).