diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -1785,12 +1785,6 @@ kw_input, kw_output, kw_sequence))); } - /// Whether the token begins a block. - bool isBlockBegin(const FormatToken &Tok, const FormatStyle &Style) const { - return Tok.is(TT_MacroBlockBegin) || - (Style.isVerilog() ? isVerilogBegin(Tok) : Tok.is(tok::l_brace)); - } - private: /// The JavaScript keywords beyond the C++ keyword set. std::unordered_set JsExtraKeywords; diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -143,6 +143,7 @@ void parseUnbracedBody(bool CheckEOF = false); void handleAttributes(); bool handleCppAttributes(); + bool isBlockBegin(const FormatToken &Tok) const; FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false); void parseTryCatch(); void parseLoopBody(bool KeepBraces, bool WrapRightBrace); diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2739,6 +2739,14 @@ return false; } +/// Returns whether \c Tok begins a block. +bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const { + // FIXME: rename the function or make + // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work. + return Style.isVerilog() ? Keywords.isVerilogBegin(Tok) + : Tok.is(tok::l_brace); +} + FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces) { assert(FormatTok->is(tok::kw_if) && "'if' expected"); @@ -2764,7 +2772,7 @@ FormatToken *IfLeftBrace = nullptr; IfStmtKind IfBlockKind = IfStmtKind::NotIf; - if (Keywords.isBlockBegin(*FormatTok, Style)) { + if (isBlockBegin(*FormatTok)) { FormatTok->setFinalizedType(TT_ControlStatementLBrace); IfLeftBrace = FormatTok; CompoundStatementIndenter Indenter(this, Style, Line->Level); @@ -2796,7 +2804,7 @@ } nextToken(); handleAttributes(); - if (Keywords.isBlockBegin(*FormatTok, Style)) { + if (isBlockBegin(*FormatTok)) { const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if); FormatTok->setFinalizedType(TT_ElseLBrace); ElseLeftBrace = FormatTok; @@ -3063,7 +3071,7 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) { keepAncestorBraces(); - if (Keywords.isBlockBegin(*FormatTok, Style)) { + if (isBlockBegin(*FormatTok)) { if (!KeepBraces) FormatTok->setFinalizedType(TT_ControlStatementLBrace); FormatToken *LeftBrace = FormatTok; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6602,6 +6602,13 @@ " x = 1;\n" "FOO_END(Baz)", Style); + + Style.RemoveBracesLLVM = true; + verifyNoCrash("for (;;)\n" + " FOO_BEGIN\n" + " foo();\n" + " FOO_END", + Style); } //===----------------------------------------------------------------------===//