Skip to content

Commit

Permalink
[clang-format] Allow trailing return types in macros
Browse files Browse the repository at this point in the history
The trailing return type arrow checker verifies that a declaration is
being parsed, however, this isn't true when inside of macros.

It turns out the existence of the auto keyword is enough to make
sure that we're dealing with a trailing return type, and whether we're
in a declaration doesn't matter.

Fixes #47664

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D141811
  • Loading branch information
rymiel committed Mar 23, 2023
1 parent 6a2a5f0 commit c70e360
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,8 @@ class AnnotatingParser {
} else if (Current.is(tok::arrow) &&
Style.Language == FormatStyle::LK_Java) {
Current.setType(TT_LambdaArrow);
} else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
} else if (Current.is(tok::arrow) && AutoFound &&
(Line.MustBeDeclaration || Line.InPPDirective) &&
Current.NestingLevel == 0 &&
!Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
// not auto operator->() -> xxx;
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8010,6 +8010,11 @@ TEST_F(FormatTest, TrailingReturnType) {
"auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
" -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");

FormatStyle Style = getLLVMStyleWithColumns(60);
verifyFormat("#define MAKE_DEF(NAME) \\\n"
" auto NAME() -> int { return 42; }",
Style);

// Not trailing return types.
verifyFormat("void f() { auto a = b->c(); }");
verifyFormat("auto a = p->foo();");
Expand Down

0 comments on commit c70e360

Please sign in to comment.