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 @@ -903,11 +903,11 @@ // Here we blacklist certain tokens that are not usually the first token in an // unwrapped line. This is used in attempt to distinguish macro calls without // trailing semicolons from other constructs split to several lines. -static bool tokenCanStartNewLine(const clang::Token &Tok) { +static bool tokenCanStartNewLine(const FormatToken &Tok) { // Semicolon can be a null-statement, l_square can be a start of a macro or // a C++11 attribute, but this doesn't seem to be common. return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && - Tok.isNot(tok::l_square) && + Tok.isNot(TT_AttributeSquare) && // Tokens that can only be used as binary operators and a part of // overloaded operator names. Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && @@ -1441,7 +1441,7 @@ : CommentsBeforeNextToken.front()->NewlinesBefore > 0; if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && - tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { + tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { addUnwrappedLine(); return; } 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 @@ -8040,6 +8040,37 @@ Style); } +TEST_F(FormatTest, AttributesAfterMacro) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("MACRO;\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}"); + + verifyFormat("MACRO;\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}"); + + EXPECT_EQ("MACRO\n\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}", + format("MACRO\n\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}")); + + EXPECT_EQ("MACRO\n\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}", + format("MACRO\n\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}")); +} + TEST_F(FormatTest, AttributePenaltyBreaking) { FormatStyle Style = getLLVMStyle(); verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"