diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -3549,6 +3549,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) { SmallVector Comments; + bool PreviousWasComment = false; + bool FirstNonCommentOnLine = false; do { FormatTok = Tokens->getNextToken(); assert(FormatTok); @@ -3565,8 +3567,23 @@ FormatTok->MustBreakBefore = true; } + auto IsFirstOnLine = [](const FormatToken &Tok) { + return Tok.HasUnescapedNewline || Tok.IsFirst; + }; + + if (PreviousWasComment) { + // Consider preprocessor directives preceded by block comments as first on + // line. + FirstNonCommentOnLine = + FirstNonCommentOnLine || IsFirstOnLine(*FormatTok); + } else { + FirstNonCommentOnLine = IsFirstOnLine(*FormatTok); + } + + PreviousWasComment = FormatTok->Tok.is(tok::comment); + while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && - (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { + FirstNonCommentOnLine) { distributeComments(Comments, FormatTok); Comments.clear(); // If there is an unfinished unwrapped line, we flush the preprocessor @@ -3585,6 +3602,14 @@ Line->Level += PPBranchLevel; flushComments(isOnNewLine(*FormatTok)); parsePPDirective(); + PreviousWasComment = FormatTok->Tok.is(tok::comment); + if (PreviousWasComment) { + // Consider preprocessor directives preceded by block comments as first + // on line. + FirstNonCommentOnLine |= IsFirstOnLine(*FormatTok); + } else { + FirstNonCommentOnLine = IsFirstOnLine(*FormatTok); + } } if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1797,6 +1797,17 @@ TEST_F(FormatTest, UnderstandsMacros) { verifyFormat("#define A (parentheses)"); + verifyFormat("/* comment */ #define A (parentheses)"); + verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); + // Even the partial code should never be merged. + EXPECT_EQ("/* comment */ #define A (parentheses)\n" + "#", + format("/* comment */ #define A (parentheses)\n" + "#")); + verifyFormat("/* comment */ #define A (parentheses)\n" + "#\n"); + verifyFormat("/* comment */ #define A (parentheses)\n" + "#define B (parentheses)"); verifyFormat("#define true ((int)1)"); verifyFormat("#define and(x)"); verifyFormat("#define if(x) x");