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 @@ -2006,7 +2006,8 @@ if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { - PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro); + if (PreviousToken->isNot(TT_UntouchableMacroFunc)) + PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro); 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 @@ -23826,6 +23826,10 @@ FormatStyle Style = getLLVMStyle(); Style.WhitespaceSensitiveMacros.push_back("FOO"); + // Newlines are important here. + verifyFormat("FOO(1+2 )\n", Style); + verifyFormat("FOO(a:b:c)\n", Style); + // Don't use the helpers here, since 'mess up' will change the whitespace // and these are all whitespace sensitive by definition EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -273,6 +273,25 @@ EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_BinaryOperator); } +TEST_F(TokenAnnotatorTest, UnderstandsWhitespaceSensitiveMacros) { + FormatStyle Style = getLLVMStyle(); + Style.WhitespaceSensitiveMacros.push_back("FOO"); + + auto Tokens = annotate("FOO(1+2 )\n", Style); + EXPECT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::identifier, TT_UntouchableMacroFunc); + + Tokens = annotate("FOO(a:b:c)\n", Style); + EXPECT_EQ(Tokens.size(), 9u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::identifier, TT_UntouchableMacroFunc); +} + +TEST_F(TokenAnnotatorTest, UnderstandsLBracesInMacroDefinition) { + auto Tokens = annotate("#define BEGIN NS {"); + EXPECT_EQ(Tokens.size(), 6u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown); +} + TEST_F(TokenAnnotatorTest, UnderstandsDelete) { auto Tokens = annotate("delete (void *)p;"); EXPECT_EQ(Tokens.size(), 8u) << Tokens;