[PATCH] Improve diagnostic message for misplaced square brackets

Richard Smith richard at metafoo.co.uk
Sun Feb 23 21:04:33 PST 2014

Comment at: lib/Parse/ParseDecl.cpp:4731
@@ -4715,3 +4730,3 @@
   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
     // ParseDeclaratorInternal might already have parsed the scope.
     if (D.getCXXScopeSpec().isEmpty()) {
In this case, I think you probably shouldn't allow square brackets. That is, if someone writes:

  int foo::[3]

we shouldn't activate the special case.

Comment at: lib/Parse/ParseDecl.cpp:4721-4728
@@ +4720,10 @@
+  // save some information for a diagnostic later if the identifer is found.
+  if (Tok.is(tok::l_square) && !D.mayOmitIdentifier()) {
+    UnhandledError = true;
+    StartLoc = Tok.getLocation();
+    while (Tok.is(tok::l_square)) {
+      ParseBracketDeclarator(D);
+    }
+    EndLoc = PP.getLocForEndOfToken(D.getLocEnd());
+  }
It looks like this will behave strangely if the remaining declarator is not just a simple identifier:

  int[3] (*p);

... will form an 'array of three pointers to int' type, rather than a 'pointer to array of three ints' type. Instead, to get this sort of thing right, I suggest you consume the array bound, then call ParseDeclarator, then add the array bound chunks to the declarator and issue your diagnostic.

To get the diagnostic location right, you could store the location of the '[' as the identifier location in the Declarator object or similar.


More information about the cfe-commits mailing list