From 15f121e853846d7c74c243a4249a069dd67ca166 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 4 Dec 2022 13:52:01 -0800 Subject: [PATCH] [clang-format] Fix an assertion failure in block parsing This assertion failure was introduced in 9ed2e68c9ae5 and is manifested when both RemoveBracesLLVM and MacroBlockBegin are set. Fixes #59335. Differential Revision: https://reviews.llvm.org/D139281 --- clang/lib/Format/FormatToken.h | 6 ------ clang/lib/Format/UnwrappedLineParser.cpp | 14 +++++++++++--- clang/lib/Format/UnwrappedLineParser.h | 1 + clang/unittests/Format/FormatTest.cpp | 7 +++++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 8751537..e8c6cd9 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -1785,12 +1785,6 @@ struct AdditionalKeywords { 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.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index d665a4b..df8cff6 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2739,6 +2739,14 @@ bool UnwrappedLineParser::handleCppAttributes() { 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 *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind, 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 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind, } 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::parseNew() { 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/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 88810ad..ce59180 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -143,6 +143,7 @@ private: 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/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f895eb4..00f5c4b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6602,6 +6602,13 @@ TEST_F(FormatTest, FormatBeginBlockEndMacros) { " x = 1;\n" "FOO_END(Baz)", Style); + + Style.RemoveBracesLLVM = true; + verifyNoCrash("for (;;)\n" + " FOO_BEGIN\n" + " foo();\n" + " FOO_END", + Style); } //===----------------------------------------------------------------------===// -- 2.7.4