[clang] 15f121e - [clang-format] Fix an assertion failure in block parsing
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 6 14:14:50 PST 2022
Author: Owen Pan
Date: 2022-12-06T14:14:20-08:00
New Revision: 15f121e853846d7c74c243a4249a069dd67ca166
URL: https://github.com/llvm/llvm-project/commit/15f121e853846d7c74c243a4249a069dd67ca166
DIFF: https://github.com/llvm/llvm-project/commit/15f121e853846d7c74c243a4249a069dd67ca166.diff
LOG: [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
Added:
Modified:
clang/lib/Format/FormatToken.h
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 87515372046d2..e8c6cd9a5d4bb 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<IdentifierInfo *> JsExtraKeywords;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index d665a4b329e94..df8cff6748b17 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 88810ad996d35..ce59180e9032e 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -143,6 +143,7 @@ class UnwrappedLineParser {
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 f895eb4f8df89..00f5c4bc80c7e 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);
}
//===----------------------------------------------------------------------===//
More information about the cfe-commits
mailing list