From 751f6922376dfe9432795b65a3649179e4ef5cf5 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 7 Sep 2010 14:51:08 +0000 Subject: [PATCH] Improve recovery when a comma is missing between enumerators in an enumeration definition. Fixes . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113201 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticParseKinds.td | 2 ++ lib/Parse/ParseDecl.cpp | 8 ++++++++ lib/Parse/ParseDeclCXX.cpp | 10 +++++----- test/FixIt/fixit.c | 7 +++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 50e98706f5..28cd2d98f0 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -55,6 +55,8 @@ def ext_c99_compound_literal : Extension< def ext_enumerator_list_comma : Extension< "commas at the end of enumerator lists are a %select{C99|C++0x}0-specific " "feature">; +def err_enumerator_list_missing_comma : Error< + "missing ',' between enumerators">; def ext_gnu_indirect_goto : Extension< "use of GNU indirect-goto extension">, InGroup; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 555fcf0dec..cf0cc9c4aa 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2127,6 +2127,14 @@ void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { EnumConstantDecls.push_back(EnumConstDecl); LastEnumConstDecl = EnumConstDecl; + if (Tok.is(tok::identifier)) { + // We're missing a comma between enumerators. + SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); + Diag(Loc, diag::err_enumerator_list_missing_comma) + << FixItHint::CreateInsertion(Loc, ", "); + continue; + } + if (Tok.isNot(tok::comma)) break; SourceLocation CommaLoc = ConsumeToken(); diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 6a63986f4a..67d4985239 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -1737,11 +1737,11 @@ void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { break; // If the next token looks like a base or member initializer, assume that // we're just missing a comma. - else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) - Diag(Tok.getLocation(), diag::err_ctor_init_missing_comma) - << FixItHint::CreateInsertion(PP.getLocForEndOfToken(PrevTokLocation), - ", "); - else { + else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { + SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); + Diag(Loc, diag::err_ctor_init_missing_comma) + << FixItHint::CreateInsertion(Loc, ", "); + } else { // Skip over garbage, until we get to '{'. Don't eat the '{'. Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); SkipUntil(tok::l_brace, true, true); diff --git a/test/FixIt/fixit.c b/test/FixIt/fixit.c index 890fb10b41..9c74435942 100644 --- a/test/FixIt/fixit.c +++ b/test/FixIt/fixit.c @@ -41,3 +41,10 @@ int test_cond(int y, int fooBar) { // CHECK: typedef int int_t; typedef typedef int int_t; + +// +enum Color { + Red // expected-error{{missing ',' between enumerators}} + Green = 17 // expected-error{{missing ',' between enumerators}} + Blue, +}; -- 2.40.0