[PATCH] Improve diagnostic message for misplaced square brackets

Richard Trieu rtrieu at google.com
Wed Feb 5 22:06:25 PST 2014


It is a common mistake to place square brackets before the identifier.  However, when this occurs, Clang merely says "expected unqualified-id" pointing at the first open square bracket.  Also, since the identifier isn't properly parsed, attempting to use it will result in additional undeclared identifier errors.

test.cc:
  const char[] str = "foo";
  char a = str[1];

Clang:
  test.cc:1:11: error: expected unqualified-id
  const char[] str = "foo";
            ^
  test.cc:2:10: error: use of undeclared identifier 'str'
  char a = str[1];
           ^
  2 errors generated.

With patch:
  test.cc:1:17: error: brackets go after the unqualified-id
  const char[] str = "foo";
            ~~    ^
                  []
  1 error generated.

Currently, the parser will process the identifier then the brackets.  Now, when a bracket appears instead of an identifier, the parser can process the brackets first, holding the location information.  Then when it finds the identifier, it can will produce a better diagnostic to move the brackets and also include a fix-it hint.  Other error messages have been updated to display in the correct spot.  Since the identifier can now be attached to the Declarator, this will also remove the undeclared identifier errors, too.

http://llvm-reviews.chandlerc.com/D2712

Files:
  test/Parser/brackets.c
  test/Parser/brackets.cpp
  include/clang/Basic/DiagnosticParseKinds.td
  lib/Parse/ParseDecl.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2712.1.patch
Type: text/x-patch
Size: 7601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140205/f95fcec9/attachment.bin>


More information about the cfe-commits mailing list