@@ -489,18 +489,23 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
489489 };
490490 SmallVector<StackEntry, 8 > LBraceStack;
491491 assert (Tok->is (tok::l_brace));
492+
492493 do {
493- // Get next non-comment, non-preprocessor token.
494494 FormatToken *NextTok;
495495 do {
496496 NextTok = Tokens->getNextToken ();
497497 } while (NextTok->is (tok::comment));
498- while (NextTok->is (tok::hash) && !Line->InMacroBody ) {
499- NextTok = Tokens->getNextToken ();
500- do {
501- NextTok = Tokens->getNextToken ();
502- } while (NextTok->is (tok::comment) ||
503- (NextTok->NewlinesBefore == 0 && NextTok->isNot (tok::eof)));
498+
499+ if (!Line->InMacroBody ) {
500+ // Skip PPDirective lines and comments.
501+ while (NextTok->is (tok::hash)) {
502+ do {
503+ NextTok = Tokens->getNextToken ();
504+ } while (NextTok->NewlinesBefore == 0 && NextTok->isNot (tok::eof));
505+
506+ while (NextTok->is (tok::comment))
507+ NextTok = Tokens->getNextToken ();
508+ }
504509 }
505510
506511 switch (Tok->Tok .getKind ()) {
@@ -534,16 +539,6 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
534539 if (Style.Language == FormatStyle::LK_Proto) {
535540 ProbablyBracedList = NextTok->isOneOf (tok::comma, tok::r_square);
536541 } else {
537- // Skip NextTok over preprocessor lines, otherwise we may not
538- // properly diagnose the block as a braced intializer
539- // if the comma separator appears after the pp directive.
540- while (NextTok->is (tok::hash)) {
541- ScopedMacroState MacroState (*Line, Tokens, NextTok);
542- do {
543- NextTok = Tokens->getNextToken ();
544- } while (NextTok->isNot (tok::eof));
545- }
546-
547542 // Using OriginalColumn to distinguish between ObjC methods and
548543 // binary operators is a bit hacky.
549544 bool NextIsObjCMethod = NextTok->isOneOf (tok::plus, tok::minus) &&
@@ -602,6 +597,16 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
602597 NextTok = Tokens->getNextToken ();
603598 ProbablyBracedList = NextTok->isNot (tok::l_square);
604599 }
600+
601+ // Cpp macro definition body that is a nonempty braced list or block:
602+ if (Style.isCpp () && Line->InMacroBody && PrevTok != FormatTok &&
603+ !FormatTok->Previous && NextTok->is (tok::eof) &&
604+ // A statement can end with only `;` (simple statement), a block
605+ // closing brace (compound statement), or `:` (label statement).
606+ // If PrevTok is a block opening brace, Tok ends an empty block.
607+ !PrevTok->isOneOf (tok::semi, BK_Block, tok::colon)) {
608+ ProbablyBracedList = true ;
609+ }
605610 }
606611 if (ProbablyBracedList) {
607612 Tok->setBlockKind (BK_BracedInit);
@@ -631,6 +636,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
631636 default :
632637 break ;
633638 }
639+
634640 PrevTok = Tok;
635641 Tok = NextTok;
636642 } while (Tok->isNot (tok::eof) && !LBraceStack.empty ());
0 commit comments