Skip to content

Commit 29f79ea

Browse files
authored
[clang-format] Handle token-pasted function decl names (#142251)
Fix #142178
1 parent 798058f commit 29f79ea

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3977,8 +3977,13 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39773977
Tok; Tok = Tok->Next) {
39783978
if (Tok->is(TT_StartOfName))
39793979
SeenName = true;
3980-
if (Tok->Previous->EndsCppAttributeGroup)
3980+
const auto *Previous = Tok->Previous;
3981+
if (Previous->EndsCppAttributeGroup) {
39813982
AfterLastAttribute = Tok;
3983+
} else if (Line.InMacroBody &&
3984+
Previous->endsSequence(tok::hashhash, TT_StartOfName)) {
3985+
Tok->setType(TT_StartOfName);
3986+
}
39823987
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
39833988
IsCtorOrDtor ||
39843989
isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
22572257
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
22582258
EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
22592259

2260+
Tokens = annotate("#define FUNC(foo, bar) \\\n"
2261+
" auto foo##bar() -> Type {}");
2262+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
2263+
EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
2264+
EXPECT_TOKEN(Tokens[12], tok::l_paren, TT_FunctionDeclarationLParen);
2265+
EXPECT_TOKEN(Tokens[14], tok::arrow, TT_TrailingReturnArrow);
2266+
22602267
Tokens = annotate("int iso_time(time_t);");
22612268
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
22622269
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

0 commit comments

Comments
 (0)