This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix crash on invalid requires expression
ClosedPublic

Authored by HazardyKnusperkeks on Mar 13 2022, 5:22 AM.

Details

Summary

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

As driveby rename Left to OpeningParen, this gives a better overview what it is, and we have a loop in the function, where Left is not adjusted.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2022, 5:22 AM
HazardyKnusperkeks requested review of this revision.Mar 13 2022, 5:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2022, 5:22 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
clang/lib/Format/TokenAnnotator.cpp
424–426

This is the actual fix. It reset to many types, although the comment said what it should reset.

clang/unittests/Format/FormatTest.cpp
23894–23897

I know we don't like changing tests, but I think there was a bug before which were never hit in our tests but here.

Before

M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=130 Name=decltype L=42 PPK=2 FakeLParens=5/ FakeRParens=0 II=0xf136c40 Text='decltype'
M=0 C=0 T=Unknown S=0 F=0 B=1 BK=0 P=23 Name=l_paren L=43 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=1 T=LambdaLSquare S=0 F=0 B=0 BK=0 P=140 Name=l_square L=44 PPK=2 FakeLParens=1/ FakeRParens=0 II=0x0 Text='['
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=79 Name=r_square L=45 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=']'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=l_paren L=46 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=160 Name=r_paren L=47 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=1 T=LambdaArrow S=1 F=0 B=0 BK=0 P=150 Name=arrow L=50 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='->'
M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=43 Name=identifier L=54 PPK=2 FakeLParens= FakeRParens=0 II=0xf130c60 Text='std'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=coloncolon L=56 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='::'
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=540 Name=identifier L=65 PPK=2 FakeLParens= FakeRParens=0 II=0xf130c88 Text='true_type'
M=0 C=0 T=LambdaLBrace S=1 F=0 B=0 BK=1 P=43 Name=l_brace L=67 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='{'
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=61 Name=r_brace L=80 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='}'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=l_paren L=81 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=160 Name=r_paren L=82 PPK=2 FakeLParens= FakeRParens=1 II=0x0 Text=')'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=83 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=coloncolon L=85 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='::'
M=0 C=1 T=TrailingAnnotation S=0 F=0 B=0 BK=0 P=520 Name=identifier L=90 PPK=2 FakeLParens= FakeRParens=0 II=0xf130ca8 Text='value'
M=0 C=1 T=BinaryOperator S=1 F=0 B=0 BK=0 P=25 Name=ampamp L=93 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='&&'

after

M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=130 Name=decltype L=42 PPK=2 FakeLParens=5/ FakeRParens=0 II=0xee66c40 Text='decltype'
M=0 C=0 T=TypeDeclarationParen S=0 F=0 B=1 BK=0 P=23 Name=l_paren L=43 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=LambdaLSquare S=0 F=0 B=0 BK=0 P=140 Name=l_square L=44 PPK=2 FakeLParens=1/ FakeRParens=0 II=0x0 Text='['
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=79 Name=r_square L=45 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=']'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=l_paren L=46 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=160 Name=r_paren L=47 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=1 T=LambdaArrow S=1 F=0 B=0 BK=0 P=150 Name=arrow L=50 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='->'
M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=43 Name=identifier L=54 PPK=2 FakeLParens= FakeRParens=0 II=0xee60c60 Text='std'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=coloncolon L=56 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='::'
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=540 Name=identifier L=65 PPK=2 FakeLParens= FakeRParens=0 II=0xee60c88 Text='true_type'
M=0 C=0 T=LambdaLBrace S=1 F=0 B=0 BK=1 P=43 Name=l_brace L=67 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='{'
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=61 Name=r_brace L=80 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='}'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=l_paren L=81 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=160 Name=r_paren L=82 PPK=2 FakeLParens= FakeRParens=1 II=0x0 Text=')'
M=0 C=0 T=TypeDeclarationParen S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=83 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=coloncolon L=85 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='::'
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=520 Name=identifier L=90 PPK=2 FakeLParens= FakeRParens=0 II=0xee60ca8 Text='value'
M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=25 Name=ampamp L=93 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='&&'

Because of the missing check it just reset all paren with a brace in it, in this case the TypeDeclarationParen. which results in a miss labeling of the value as an annotation.

curdeius accepted this revision.Mar 13 2022, 10:19 AM
curdeius added inline comments.
clang/lib/Format/TokenAnnotator.cpp
215

Maybe you can commit the rename as a NFC commit and rebase the patch to remove the noise?

clang/unittests/Format/FormatTest.cpp
23894–23897

Looks legit to me.

clang/unittests/Format/TokenAnnotatorTest.cpp
335

I'd prefer a valid C++ code (if it's not too complicated) if possible but that's acceptable.

This revision is now accepted and ready to land.Mar 13 2022, 10:19 AM
HazardyKnusperkeks marked 2 inline comments as done.

Split rename and bug fix

clang/lib/Format/TokenAnnotator.cpp
215

Done in D121557

clang/unittests/Format/TokenAnnotatorTest.cpp
335

I thought I'd stick to the bug report. The only thing that comes to my mind does compile on gcc, but clang gives a specific error (which I support, without having looked into the standard). https://gcc.godbolt.org/z/WvT9861b6

LGTM. And thanks for splitting the patch.

clang/unittests/Format/TokenAnnotatorTest.cpp
335

Ok, now I see that it's actual test code. It's perfectly fine the.

curdeius accepted this revision.Mar 13 2022, 2:22 PM
owenpan accepted this revision.Mar 13 2022, 4:21 PM

After applying this patch, we got a different assertion failure on a bunch of files in clang/test, e.g.:

~/llvm-project/clang$ for f in $(find . -name \*.cpp -o -name \*.c) ; do clang-format $f > /dev/null ; done
Assertion failed: (OpeningParen.is(tok::l_paren)), function parseParens, file TokenAnnotator.cpp, line 216.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.	Program arguments: cf ./test/Driver/ignore-xcoff-visibility.cpp
...
clang/lib/Format/TokenAnnotator.cpp
216

This patch either uncovers or causes an assertion failure here on a bunch of files in clang/test.

clang/lib/Format/TokenAnnotator.cpp
216

I was wrong! Please ignore it.

Phew. I thought I needed a fix for the fix, for the fix, for the fix.