This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix misformatted macro definitions after D86959
ClosedPublic

Authored by arichardson on Oct 7 2020, 4:33 AM.

Details

Summary

[clang-format] Fix misformatted macro definitions after D86959

After D86959 the code #define lambda [](const decltype(x) &ptr) {}
was formatted as #define lambda [](const decltype(x) & ptr) {} due to
now parsing the '&' token as a BinaryOperator. The problem was caused by
the condition Line.InPPDirective && (!Left->Previous || !Left->Previous->is(tok::identifier))) {
being matched and therefore not performing the checks for "previous token
is one of decltype/_Atomic/etc.". This patch moves those checks after the
existing if/else chain to ensure the left-parent token classification is
always run after checking whether the contents of the parens is an
expression or not.

This change also introduces a new TokenAnnotatorTest that checks the
token kind and Role of Tokens after analyzing them. This is used to check
for TT_PointerOrReference, in addition to indirectly testing this based
on the resulting formatting.

Diff Detail

Event Timeline

arichardson created this revision.Oct 7 2020, 4:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 7 2020, 4:33 AM
arichardson requested review of this revision.Oct 7 2020, 4:33 AM

remove unneccessary include

Harbormaster completed remote builds in B74253: Diff 296640.
klimek added inline comments.Oct 7 2020, 5:28 AM
clang/unittests/Format/FormatTest.cpp
8249

I'd put this into a new test file that specifically tests annotations, perhaps AnnotationTest.cpp?

Also, the test name seems misleading, as not all of these are macro definitions?

arichardson added inline comments.Oct 7 2020, 5:30 AM
clang/unittests/Format/FormatTest.cpp
8249

The regression is the second definition which is a macro definition, I guess I could drop the first test.

8249

Moving it to a separate file probably makes sense, but then I'd have to drop the verifyFormat() calls.

klimek added inline comments.Oct 7 2020, 5:59 AM
clang/unittests/Format/FormatTest.cpp
8249

If we start a new file, we can have 2 tests.

I'd leave the verifyFormat tests here, potentially putting them into already existing test functions that test similar things.

I have noticed that clang-format has a tendency to over eagerly make * and & a TT_BinaryOperator, I know its too late to change it now, but sometimes I wonder if they should be detected as TT_PointerOrReference and then the clauses try and expose when they are being used as BinaryOperators we might have better luck.. or maybe not.

MyDeveloperDay added inline comments.Oct 7 2020, 9:31 AM
clang/unittests/Format/FormatTest.cpp
8281

I like this style of test, I think sometimes even the tests seem to work even if the type is incorrectly determined, but generally that leads to problems further down the road. I can see this construct being useful

arichardson edited the summary of this revision. (Show Details)Oct 19 2020, 6:43 AM

Split the token annotion test into a separate file

fix name of test class

This revision is now accepted and ready to land.Oct 21 2020, 1:15 AM
This revision was landed with ongoing or failed builds.Oct 27 2020, 5:18 AM
This revision was automatically updated to reflect the committed changes.