[clang] [clang-format] Fix a bug in SkipMacroDefinitionBody (PR #154787)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 21 08:59:54 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->154683
---
Full diff: https://github.com/llvm/llvm-project/pull/154787.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+6-3)
- (modified) clang/unittests/Format/FormatTest.cpp (+4)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/154787
More information about the cfe-commits
mailing list