This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix short functions being considered as inline inside an indented namespace.
ClosedPublic

Authored by curdeius on Jan 12 2022, 12:25 PM.

Details

Summary

Fixes https://github.com/llvm/llvm-project/issues/24784.

With config:

AllowShortFunctionsOnASingleLine: Inline
NamespaceIndentation: All

The code:

namespace Test
{
    void f()
    {
        return;
    }
}

was incorrectly formatted to:

namespace Test
{
    void f() { return; }
}

since the function f was considered being inside a class/struct/record.
That's because the check was simplistic and only checked for a non-zero indentation level of the line starting f.

Diff Detail

Event Timeline

curdeius requested review of this revision.Jan 12 2022, 12:25 PM
curdeius created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2022, 12:25 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Still WIP but I'd like to have some feedback.

clang/lib/Format/UnwrappedLineFormatter.cpp
283

Because of this I consider this patch as a WIP still.
I'm all ears if anyone has an idea how to efficiently find the "parent" line of the current one, i.e. a line with level smaller by one.
There are places where we use a stack for similar purposes. I'm not sure if there's already something existing I could use.

As a last resort, I'll add a stack of Lines that start a new level in LevelIndentTracker and update it in nextLine().

curdeius added inline comments.Jan 12 2022, 12:31 PM
clang/lib/Format/UnwrappedLineFormatter.cpp
278

TODO: typo

clang/lib/Format/UnwrappedLineFormatter.cpp
297

Is union used in another language and behaves then differently?

304

Is there a test for that?

curdeius marked 2 inline comments as done.Jan 13 2022, 2:08 AM
curdeius added inline comments.
clang/lib/Format/UnwrappedLineFormatter.cpp
297

At least in C#, union is not a keyword and can be e.g. a name of a namespace. There's a test for this.

304
This revision is now accepted and ready to land.Jan 14 2022, 12:39 AM
curdeius marked 2 inline comments as done.Jan 14 2022, 3:08 AM
curdeius updated this revision to Diff 399945.Jan 14 2022, 3:11 AM

Minor clean up.

curdeius updated this revision to Diff 399946.Jan 14 2022, 3:14 AM

Rebase to fix Windows CI.