This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix namespace format when the name is followed by a macro
ClosedPublic

Authored by zequanwu on Mar 8 2022, 7:04 PM.

Details

Summary

Example:

$ cat a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}

$ clang-format a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}// namespace my_namespace::yeahAPI_AVAILABLE(macos(10.15))

After:

$ clang-format a.cpp
namespace my_namespace::yeah API_AVAILABLE(macos(10.15)) {
void test() {}
}// namespace my_namespace::yeah

Diff Detail

Event Timeline

zequanwu created this revision.Mar 8 2022, 7:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 7:04 PM
zequanwu requested review of this revision.Mar 8 2022, 7:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 7:04 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MyDeveloperDay requested changes to this revision.Mar 8 2022, 11:28 PM
MyDeveloperDay added inline comments.
clang/lib/Format/NamespaceEndCommentsFixer.cpp
145

Elide braces

This revision now requires changes to proceed.Mar 8 2022, 11:28 PM
MyDeveloperDay accepted this revision.Mar 8 2022, 11:32 PM
MyDeveloperDay added inline comments.
clang/lib/Format/NamespaceEndCommentsFixer.cpp
145

Ok ignore that

clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
192

Can you test the A B case? We can’t have a space right?

This revision is now accepted and ready to land.Mar 8 2022, 11:32 PM

Please wait for the other reviewers

MyDeveloperDay added a project: Restricted Project.
curdeius retitled this revision from [clang-format] Fix namepsace format when the name is after by a macro to [clang-format] Fix namespace format when the name is followed by a macro.Mar 8 2022, 11:36 PM
thakis resigned from this revision.Mar 9 2022, 6:06 AM

(deferring to MyDeveloperDay and curdeius)

curdeius added inline comments.Mar 9 2022, 6:54 AM
clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
192–211

What's the rationale behind keeping M(x) in one case and not the other?
How can you decide?

zequanwu updated this revision to Diff 414149.Mar 9 2022, 10:28 AM
zequanwu marked an inline comment as done.

Add a test for namespace A B {.

clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
192

Added a test with A B.
In that case, we will have // namespace A B, since we don't know which one is the real name. Either one could be a macro that expands to an attribute.

192–211

In the first one, we keep M(x) because we don't know that is A or M(x) the name.
In the second one, because we see a ::, so we know that's the name and discard M(x).

curdeius added inline comments.Mar 9 2022, 1:20 PM
clang/lib/Format/NamespaceEndCommentsFixer.cpp
96–97

Nit: Personally, I'd put these bools further down, closer to the place where they are used and after AddMacro lambda.

126

This loop seems very similar to the one that skips the attributes on lines 45-53, could you please refactor?
A lambda taking a pair of token kinds (l_paren/r_paren, l_square/r_square) should be fine.

clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
192

In a different test case, could you add A to AttributeMacros and test that we skip it like other attributes?

192

Euh, actually, it's out of scope of this patch. Please ignore.

zequanwu updated this revision to Diff 414222.Mar 9 2022, 3:05 PM
zequanwu marked 2 inline comments as done.

Refactor.
Add a test for namespace A __attribute__((availability(macos, introduced=10.15))) {.

curdeius accepted this revision.Mar 10 2022, 12:25 AM

LGTM % nits. Thanks for working on this!

clang/lib/Format/NamespaceEndCommentsFixer.cpp
54–55

Nit: add braces around else if block to match the if.

76–77

Comment not necessary anymore. Please remove.

127

Comment not necessary anymore. Please remove.

132
owenpan added inline comments.Mar 10 2022, 3:09 AM
clang/lib/Format/NamespaceEndCommentsFixer.cpp
33

And other places below where applicable.

76–77

Early return.

131–134

Something like that.

139
zequanwu updated this revision to Diff 414442.Mar 10 2022, 11:17 AM
zequanwu marked 7 inline comments as done.

Address comments.

owenpan accepted this revision.Mar 10 2022, 1:26 PM
owenpan added inline comments.
clang/lib/Format/NamespaceEndCommentsFixer.cpp
108
116
135–139
zequanwu marked 3 inline comments as done.Mar 10 2022, 3:00 PM