https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/154787
>From 04dab5e8cf9646e88970350ee602cc82535322bc Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Thu, 21 Aug 2025 08:58:12 -0700 Subject: [PATCH 1/2] [clang-format] Fix a bug in SkipMacroDefinitionBody Fixes #154683 --- clang/lib/Format/UnwrappedLineParser.cpp | 9 ++++++--- clang/unittests/Format/FormatTest.cpp | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 91b8fdc8a3c38..23938f8cb9d00 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1182,10 +1182,8 @@ void UnwrappedLineParser::parsePPDefine() { if (MaybeIncludeGuard && !eof()) IncludeGuard = IG_Rejected; - if (FormatTok->Tok.getKind() == tok::l_paren && - !FormatTok->hasWhitespaceBefore()) { + if (FormatTok->is(tok::l_paren) && !FormatTok->hasWhitespaceBefore()) parseParens(); - } if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) Line->Level += PPBranchLevel + 1; addUnwrappedLine(); @@ -1196,6 +1194,11 @@ void UnwrappedLineParser::parsePPDefine() { Line->InMacroBody = true; if (Style.SkipMacroDefinitionBody) { + if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) && + Prev->NewlinesBefore > 0 && + !Prev->HasUnescapedNewline) { + Prev->Finalized = true; + } while (!eof()) { FormatTok->Finalized = true; FormatTok = Tokens->getNextToken(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 83c664c3b81f3..1ef6877941c7d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -26022,6 +26022,10 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) { " A a \\\n " " A a", Style); + verifyNoChange("#define MY_MACRO \\\n" + " /* this is a comment */ \\\n" + " 1", + Style); } TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { >From efa660e867e7969e22ce21aa06197408df683068 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Thu, 21 Aug 2025 21:06:21 -0700 Subject: [PATCH 2/2] Cleanup --- clang/lib/Format/UnwrappedLineParser.cpp | 40 ++++++++++++++---------- clang/unittests/Format/FormatTest.cpp | 4 +-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 23938f8cb9d00..ca7e2da3799c4 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1191,28 +1191,34 @@ void UnwrappedLineParser::parsePPDefine() { Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1); assert((int)Line->PPLevel >= 0); + + if (eof()) + return; + Line->InMacroBody = true; - if (Style.SkipMacroDefinitionBody) { - if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) && - Prev->NewlinesBefore > 0 && - !Prev->HasUnescapedNewline) { - Prev->Finalized = true; - } - while (!eof()) { - FormatTok->Finalized = true; - FormatTok = Tokens->getNextToken(); - } - addUnwrappedLine(); + if (!Style.SkipMacroDefinitionBody) { + // Errors during a preprocessor directive can only affect the layout of the + // preprocessor directive, and thus we ignore them. An alternative approach + // would be to use the same approach we use on the file level (no + // re-indentation if there was a structural error) within the macro + // definition. + parseFile(); return; } - // Errors during a preprocessor directive can only affect the layout of the - // preprocessor directive, and thus we ignore them. An alternative approach - // would be to use the same approach we use on the file level (no - // re-indentation if there was a structural error) within the macro - // definition. - parseFile(); + if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) && + Prev->NewlinesBefore > 0 && + !Prev->HasUnescapedNewline) { + Prev->Finalized = true; + } + + do { + FormatTok->Finalized = true; + FormatTok = Tokens->getNextToken(); + } while (!eof()); + + addUnwrappedLine(); } void UnwrappedLineParser::parsePPPragma() { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 1ef6877941c7d..136039d255393 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -26022,8 +26022,8 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) { " A a \\\n " " A a", Style); - verifyNoChange("#define MY_MACRO \\\n" - " /* this is a comment */ \\\n" + verifyNoChange("#define MY_MACRO \\\n" + " /* comment */ \\\n" " 1", Style); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits