Missing comma diagnostics patch

Zaid Alkhishman via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 29 20:44:12 PDT 2017


Hello,

This patch adds missing comma diagnostics to initializer lists.

I'm attaching
the .diff (git),
an input .c
and the compilation output .out

Let me know if there are any required changes.

Regards,

Zaid

zaid.al.khishman at gmail.com

(Note, I tried subscribing to cfe-commits but it said the subscription has
to be confirmed first, so I'm not even sure if this email is gonna go
through, but if it does for now I won't be getting any replies I suppose :/)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170429/a50c8b09/attachment.html>
-------------- next part --------------
diff --git include/clang/Basic/DiagnosticParseKinds.td include/clang/Basic/DiagnosticParseKinds.td
index f04ed8ed4c..ef913ce947 100644
--- include/clang/Basic/DiagnosticParseKinds.td
+++ include/clang/Basic/DiagnosticParseKinds.td
@@ -164,6 +164,7 @@ def err_function_declared_typedef : Error<
 def err_at_defs_cxx : Error<"@defs is not supported in Objective-C++">;
 def err_at_in_class : Error<"unexpected '@' in member specification">;
 def err_unexpected_semi : Error<"unexpected ';' before %0">;
+def err_expected_comma : Error<"expected ',' before initializer">;
 
 def err_expected_fn_body : Error<
   "expected function body after function declarator">;
diff --git include/clang/Parse/RAIIObjectsForParser.h include/clang/Parse/RAIIObjectsForParser.h
index 0422b038da..938df69b95 100644
--- include/clang/Parse/RAIIObjectsForParser.h
+++ include/clang/Parse/RAIIObjectsForParser.h
@@ -440,6 +440,13 @@ namespace clang {
       
       return diagnoseMissingClose();
     }
+
+    bool willClose(){
+      if (P.Tok.is(Close))
+        return false;
+      return true;
+    }
+
     void skipToEnd();
   };
 
diff --git lib/Parse/ParseInit.cpp lib/Parse/ParseInit.cpp
index f48d01e0f6..2454162b2e 100644
--- lib/Parse/ParseInit.cpp
+++ lib/Parse/ParseInit.cpp
@@ -409,6 +409,8 @@ ExprResult Parser::ParseBraceInitializer() {
       Actions, EnterExpressionEvaluationContext::InitList);
 
   bool InitExprsOk = true;
+  bool maybeMissingComma = false;
+  Token startOfNextIni;
 
   while (1) {
     // Handle Microsoft __if_exists/if_not_exists if necessary.
@@ -427,6 +429,8 @@ ExprResult Parser::ParseBraceInitializer() {
     // If we know that this cannot be a designation, just parse the nested
     // initializer directly.
     ExprResult SubElt;
+    startOfNextIni = Tok;
+
     if (MayBeDesignationStart())
       SubElt = ParseInitializerWithPotentialDesignator();
     else
@@ -457,8 +461,24 @@ ExprResult Parser::ParseBraceInitializer() {
       }
     }
 
-    // If we don't have a comma continued list, we're done.
-    if (Tok.isNot(tok::comma)) break;
+    if(maybeMissingComma){
+      //Here we would have checked if InitExprsOk is true,
+      //but it's implied to be ok because of the previous break
+      //Now we know the compilee  very likely forgot the comma
+      Diag(startOfNextIni.getLocation(), diag::err_expected_comma)
+           << FixItHint::CreateInsertion(startOfNextIni.getLocation().getLocWithOffset(-1), ",");
+      maybeMissingComma = false;
+    }
+
+    // If we don't have a comma continued list, we're done (maybe).
+    if (Tok.isNot(tok::comma)){
+      if(!T.willClose()){
+        //This is a ok list, no missing commas.
+        break;
+      }
+      maybeMissingComma = true;
+      continue;
+    }
 
     // TODO: save comma locations if some client cares.
     ConsumeToken();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: missingCommasDiag.in
Type: application/octet-stream
Size: 150 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170429/a50c8b09/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: missingCommasDiag.out
Type: application/octet-stream
Size: 1160 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170429/a50c8b09/attachment-0001.obj>


More information about the cfe-commits mailing list