This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix bug with parsing of function/variable names.
ClosedPublic

Authored by gedare on Jul 26 2023, 2:21 PM.

Details

Summary

Function and variable names are not detected correctly when there is an
__attribute__((x)) preceding the name.

Fixes Github Issue #64137
https://github.com/llvm/llvm-project/issues/64137

Diff Detail

Event Timeline

gedare created this revision.Jul 26 2023, 2:21 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJul 26 2023, 2:21 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gedare requested review of this revision.Jul 26 2023, 2:21 PM
gedare edited the summary of this revision. (Show Details)Jul 26 2023, 2:22 PM

Does this result in a different annotation? Could you add a test for that?

clang/lib/Format/TokenAnnotator.cpp
2249–2252

Does this result in a different annotation? Could you add a test for that?

If I understand you correctly, it does, for example:
$ echo "void __attribute__((naked)) foo(int bar);" | clang-format -style=llvm -debug 2>&1 | grep \'foo\'

Without the change:
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=23 Name=identifier L=31 PPK=2 FakeLParens= FakeRParens=0 II=0x555d17efdce0 Text='foo'

With the change:
M=0 C=1 T=FunctionDeclarationName S=1 F=0 B=0 BK=0 P=80 Name=identifier L=31 PPK=2 FakeLParens= FakeRParens=0 II=0x559855502ce0 Text='foo'

I don't how to test for that.

Does this result in a different annotation? Could you add a test for that?

If I understand you correctly, it does, for example:
$ echo "void __attribute__((naked)) foo(int bar);" | clang-format -style=llvm -debug 2>&1 | grep \'foo\'

Without the change:
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=23 Name=identifier L=31 PPK=2 FakeLParens= FakeRParens=0 II=0x555d17efdce0 Text='foo'

With the change:
M=0 C=1 T=FunctionDeclarationName S=1 F=0 B=0 BK=0 P=80 Name=identifier L=31 PPK=2 FakeLParens= FakeRParens=0 II=0x559855502ce0 Text='foo'

I don't how to test for that.

Yes that stuff. Tests are in: https://github.com/llvm/llvm-project/blob/main/clang/unittests/Format/TokenAnnotatorTest.cpp
If there is none that matches, just create a new one.

gedare planned changes to this revision.Aug 1 2023, 10:43 AM

Yes that stuff. Tests are in: https://github.com/llvm/llvm-project/blob/main/clang/unittests/Format/TokenAnnotatorTest.cpp
If there is none that matches, just create a new one.

@gedare, I think this is the only missing piece for your patch to get accepted. 🙂

gedare updated this revision to Diff 558061.Nov 9 2023, 7:21 AM

Add TokenAnnotator tests and light refactoring.

gedare marked an inline comment as done.Nov 9 2023, 7:22 AM
This revision is now accepted and ready to land.Nov 9 2023, 12:12 PM
owenpan accepted this revision.Nov 9 2023, 2:28 PM
owenpan added inline comments.
clang/lib/Format/TokenAnnotator.cpp
2248–2252

We should annotate parens separately as LParen and RParen like we recently did with braces. Then we could simply do:

if (PreviousNotConst->isOneOf(TT_TypeDeclarationRParen, TT_AttributeRParen))
This revision was automatically updated to reflect the committed changes.