Skip to content

Commit

Permalink
[clang-format] PR50326 AlignAfterOpenBracket AlwaysBreak does not kee…
Browse files Browse the repository at this point in the history
…p to the ColumnLimit

https://bugs.llvm.org/show_bug.cgi?id=50326

{D93626} caused a regression in terms of formatting a function ptr, incorrectly thinking it was a C-Style cast.

This cased a formatter regression between clang-format-11 and clang-format-12

```
void bar()
{
    size_t foo = function(Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong);

    size_t foo = function(
        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, BarrrrrrrrrrrrLong,
        FoooooooooLooooong);

    size_t foo = (*(function))(Foooo, Barrrrr, Foooo, FoooooooooLooooong);

    size_t foo = (*(
        function))(Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong,
        BarrrrrrrrrrrrLong, FoooooooooLooooong);
}
```

became

```
void bar()
{
    size_t foo1 = function(Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong);

    size_t foo2 = function(
        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, BarrrrrrrrrrrrLong,
        FoooooooooLooooong);

    size_t foo3 = (*(function))(Foooo, Barrrrr, Foooo, FoooooooooLooooong);

    size_t foo4 = (*(
        function))(Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, BarrrrrrrrrrrrLong, FoooooooooLooooong);
}
```

This fixes this issue by simplify the clause to be specific about what is wanted rather than what is not.

Reviewed By: curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D102392

(cherry picked from commit eae445f)
  • Loading branch information
mydeveloperday authored and tstellar committed Jun 4, 2021
1 parent f2ce10d commit e673593
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,12 +1917,12 @@ class AnnotatingParser {
if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
return true;

if (Tok.Next->is(tok::l_paren) &&
!(Tok.Previous && Tok.Previous->is(tok::identifier) &&
Tok.Previous->Previous &&
Tok.Previous->Previous->isOneOf(tok::arrowstar, tok::arrow,
tok::star)))
return true;
// Look for a cast `( x ) (`.
if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
if (Tok.Previous->is(tok::identifier) &&
Tok.Previous->Previous->is(tok::l_paren))
return true;
}

if (!Tok.Next->Next)
return false;
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12368,13 +12368,33 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
Spaces.ColumnLimit = 80;
Spaces.IndentWidth = 4;
Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
verifyFormat("void foo( ) {\n"
" size_t foo = (*(function))(\n"
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
"BarrrrrrrrrrrrLong,\n"
" FoooooooooLooooong);\n"
"}",
Spaces);
Spaces.SpaceAfterCStyleCast = false;
verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
verifyFormat("size_t idx = (size_t)a;", Spaces);
verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);

verifyFormat("void foo( ) {\n"
" size_t foo = (*(function))(\n"
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
"BarrrrrrrrrrrrLong,\n"
" FoooooooooLooooong);\n"
"}",
Spaces);
}

TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
Expand Down

0 comments on commit e673593

Please sign in to comment.