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.khish...@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 :/)
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();
missingCommasDiag.in
Description: Binary data
missingCommasDiag.out
Description: Binary data
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits