This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add an option for breaking after C++11 attributes
ClosedPublic

Authored by owenpan on Jan 4 2023, 12:05 AM.

Details

Summary

This patch only handles C++11 attributes before a function declaration/definition name. It doesn't cover attributes in variable declarations, which may be supported in the future.

Fixes:
https://github.com/llvm/llvm-project/issues/45968
https://github.com/llvm/llvm-project/issues/54265
https://github.com/llvm/llvm-project/issues/58102

Diff Detail

Event Timeline

owenpan created this revision.Jan 4 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 12:05 AM
owenpan requested review of this revision.Jan 4 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 12:05 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
owenpan added inline comments.Jan 4 2023, 1:43 AM
clang/lib/Format/FormatToken.h
323

Can be removed.

This LGTM

Should we care about other forms of attributes like __stdcall etc..? I personally don't, just I assume from the tests this isn't covered (maybe just add that limitation to the doc if it is indeed a limitation)

MyDeveloperDay accepted this revision.Jan 4 2023, 4:31 AM
This revision is now accepted and ready to land.Jan 4 2023, 4:31 AM
owenpan updated this revision to Diff 486445.Jan 4 2023, 8:48 PM
owenpan retitled this revision from [clang-format] Add an option for breaking after C++ attributes to [clang-format] Add an option for breaking after C++11 attributes.
owenpan edited the summary of this revision. (Show Details)
  • Delete the unused StartsCppAttribute.
  • Strengthen test cases by covering nested C++11 attributes.
  • Update documentation to make it clear that this option only applies to C++11 attributes.

Should we care about other forms of attributes like __stdcall etc..? I personally don't, just I assume from the tests this isn't covered (maybe just add that limitation to the doc if it is indeed a limitation)

This option only handles C++11 attributes as mentioned in the summary. I've updated the doc to make it clear.

MyDeveloperDay accepted this revision.Jan 5 2023, 2:15 AM

I'm good with that. I like small pieces of contained work and not open ended reviews that try to cover everything. If someone wants to extend this to include the old form, then that can be a completely different review. But this is a great start (especially as I heavily use [[nodiscard]])

Go for it..

If someone wants to extend this to include the old form, then that can be a completely different review.

Better yet, use another tool like clang-tidy to replace the old with the new.

This revision was landed with ongoing or failed builds.Jan 5 2023, 4:10 AM
This revision was automatically updated to reflect the committed changes.

If someone wants to extend this to include the old form, then that can be a completely different review.

Better yet, use another tool like clang-tidy to replace the old with the new.

indeed

It looks like this regressed the following example:

% cat test.cc  # formatted with older clang-format
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
% clang-format --version
clang-format version 16.0.0 (https://github.com/llvm/llvm-project.git a28f0747c2f3728bd8a6f64f7c8ba80b4e0cda9f)
% clang-format -style=google test.cc
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb> &
cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
%

@owenpan, I'm planning to temporarily revert this until you have a chance to investigate.

Before

M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=identifier L=25 PPK=2 FakeLParens= FakeRParens=0 II=0x1aa92d99900 Text='aaaaaaaaaaaaaaaaaaaaaaaaa'
M=0 C=0 T=TemplateOpener S=0 F=0 B=0 BK=0 P=30 Name=less L=26 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='<'
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=360 Name=identifier L=37 PPK=2 FakeLParens= FakeRParens=0 II=0x1aa92d99938 Text='bbbbbbbbbbb'
M=0 C=0 T=TemplateCloser S=0 F=0 B=0 BK=0 P=270 Name=greater L=38 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='>'
M=0 C=1 T=PointerOrReference S=1 F=0 B=0 BK=0 P=210 Name=amp L=40 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='&'
M=0 C=1 T=FunctionDeclarationName S=0 F=0 B=0 BK=0 P=220 Name=identifier L=78 PPK=2 FakeLParens= FakeRParens=0 II=0x1aa92d99988 Text='cccccccccccccccccccccccccccccccccccccc'

After

M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=identifier L=25 PPK=2 FakeLParens= FakeRParens=0 II=0x206f1adc870 Text='aaaaaaaaaaaaaaaaaaaaaaaaa'
M=0 C=0 T=TemplateOpener S=0 F=0 B=0 BK=0 P=30 Name=less L=26 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='<'
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=360 Name=identifier L=37 PPK=2 FakeLParens= FakeRParens=0 II=0x206f1adc8a8 Text='bbbbbbbbbbb'
M=0 C=0 T=TemplateCloser S=0 F=0 B=0 BK=0 P=270 Name=greater L=38 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='>'
M=0 C=0 T=PointerOrReference S=1 F=0 B=0 BK=0 P=210 Name=amp L=40 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='&'
M=0 C=1 T=FunctionDeclarationName S=0 F=0 B=0 BK=0 P=220 Name=identifier L=78 PPK=2 FakeLParens= FakeRParens=0 II=0x206f1adc8f8 Text='cccccccccccccccccccccccccccccccccccccc'

Looks like the "C" (Can Break?) goes from 1->0

"M=0 C=0 T=PointerOrReference"

It looks like this regressed the following example:

% cat test.cc  # formatted with older clang-format
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
% clang-format --version
clang-format version 16.0.0 (https://github.com/llvm/llvm-project.git a28f0747c2f3728bd8a6f64f7c8ba80b4e0cda9f)
% clang-format -style=google test.cc
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb> &
cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
%

@owenpan, I'm planning to temporarily revert this until you have a chance to investigate.

Was it actually a regression or did this patch also fix a bug? It seems that the continuation indent before the & in your example is inconsistent with other similar function declarations.

Before this patch:

% cat test-all.cc
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
    &cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
    _cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    _cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}

aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
    &cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
    _cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    _cccccccccccccccccccccccccccccccccccccc();
% clang-format -version
clang-format version 16.0.0 (https://github.com/llvm/llvm-project f2891086f4b64434ecf471960b6daf6f29fd4328)
% clang-format -style=google test-all.cc
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_ &
cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
_cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
_cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}

aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
    &cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_ &
cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
_cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
_cccccccccccccccccccccccccccccccccccccc();

After this patch:

% cf -version
clang-format version 16.0.0 (https://github.com/llvm/llvm-project a28f0747c2f3728bd8a6f64f7c8ba80b4e0cda9f)
% cf -style=google test-all.cc
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb> &
cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_ &
cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
_cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
_cccccccccccccccccccccccccccccccccccccc() {
  return 1;
}

aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb> &
cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_ &
cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa_bbbbbbbbbbb_
_cccccccccccccccccccccccccccccccccccccc();
aaaaaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbb>
_cccccccccccccccccccccccccccccccccccccc();

Was it actually a regression or did this patch also fix a bug? It seems that the continuation indent before the & in your example is inconsistent with other similar function declarations.

Nice catch! I fully agree with you!
Thank you for looking into this, sorry for the false alarm!

I'll go ahead and revert my revert.

Was it actually a regression or did this patch also fix a bug? It seems that the continuation indent before the & in your example is inconsistent with other similar function declarations.

Nice catch! I fully agree with you!
Thank you for looking into this, sorry for the false alarm!

Np!