Author: Marek Kurdej Date: 2022-05-09T10:59:33+02:00 New Revision: 50cd52d9357224cce66a9e00c9a0417c658a5655
URL: https://github.com/llvm/llvm-project/commit/50cd52d9357224cce66a9e00c9a0417c658a5655 DIFF: https://github.com/llvm/llvm-project/commit/50cd52d9357224cce66a9e00c9a0417c658a5655.diff LOG: [clang-format] Fix WhitespaceSensitiveMacros not being honoured when macro closing parenthesis is followed by a newline. Fixes https://github.com/llvm/llvm-project/issues/54522. This fixes regression introduced in https://github.com/llvm/llvm-project/commit/5e5efd8a91f2e340e79a73bedbc6ab66ad4a4281. Before the culprit commit, macros in WhitespaceSensitiveMacros were correctly formatted even if their closing parenthesis weren't followed by semicolon (or, to be precise, when they were followed by a newline). That commit changed the type of the macro token type from TT_UntouchableMacroFunc to TT_FunctionLikeOrFreestandingMacro. Correct formatting (with `WhitespaceSensitiveMacros = ['FOO']`): ``` FOO(1+2) FOO(1+2); ``` Regressed formatting: ``` FOO(1 + 2) FOO(1+2); ``` Reviewed By: HazardyKnusperkeks, owenpan, ksyx Differential Revision: https://reviews.llvm.org/D123676 Added: Modified: clang/lib/Format/FormatTokenLexer.cpp clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 187b30fd55a7e..d1d236cf032b2 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -1027,7 +1027,10 @@ FormatToken *FormatTokenLexer::getNextToken() { Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() == tok::pp_define) && it != Macros.end()) { - FormatTok->setType(it->second); + if (it->second == TT_UntouchableMacroFunc) + FormatTok->setFinalizedType(TT_UntouchableMacroFunc); + else + FormatTok->setType(it->second); if (it->second == TT_IfMacro) { // The lexer token currently has type tok::kw_unknown. However, for this // substitution to be treated correctly in the TokenAnnotator, faking diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 52ce0fff251c2..f13e3d725cbd3 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1787,7 +1787,8 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind, : CommentsBeforeNextToken.front()->NewlinesBefore > 0; if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && - tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { + tokenCanStartNewLine(*FormatTok) && Text == Text.upper() && + !PreviousToken->isTypeFinalized()) { PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro); addUnwrappedLine(); return; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index d7d69f950a323..98da4f773de95 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -23606,6 +23606,11 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) { // Don't use the helpers here, since 'mess up' will change the whitespace // and these are all whitespace sensitive by definition + + // Newlines are important here. + EXPECT_EQ("FOO(1+2 );\n", format("FOO(1+2 );\n", Style)); + EXPECT_EQ("FOO(1+2 )\n", format("FOO(1+2 )\n", Style)); + EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); EXPECT_EQ( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits