This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Link the braces of a block in UnwrappedLineParser
ClosedPublic

Authored by owenpan on Dec 3 2022, 7:51 AM.

Details

Summary

This includes TT_MacroBlockBegin and TT_MacroBlockEnd as well.

We can no longer use MatchingParen of FormatToken as an indicator to mark optional braces. Instead, we directly set Optional of an l_brace first and reset it later if it turns out that the braces are not optional.

Also added a test case for deeply nested loops.

Diff Detail

Event Timeline

owenpan created this revision.Dec 3 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2022, 7:51 AM
owenpan requested review of this revision.Dec 3 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2022, 7:51 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
clang/lib/Format/UnwrappedLineParser.cpp
2714

This should be named differently. For me reset means assigning false.

owenpan updated this revision to Diff 479884.Dec 3 2022, 5:55 PM

Updated resetOptional() to make resetting Optional more obvious.

owenpan edited the summary of this revision. (Show Details)Dec 3 2022, 5:56 PM
owenpan retitled this revision from [clang-format][NFC] Link braces of a block in UnwrappedLineParser to [clang-format] Link the braces of a block in UnwrappedLineParser.
This revision is now accepted and ready to land.Dec 4 2022, 5:37 AM
This revision was landed with ongoing or failed builds.Dec 4 2022, 12:01 PM
This revision was automatically updated to reflect the committed changes.

I have second thoughts about linking block braces in UnwrappedLineParser. Consider the following example:

#if A
while (a) { // Linked to the r_brace below
#else
while(b) { // Also linked to the r_brace below
#endif
  foo();
}  // Linked to the l_brace of while(b) above

The l_brace of while (a) is still linked to the r_brace that has been re-linked to another l_bace.

Because MatchingParen will be reset in the annotator anyway, linking it for block braces in UnwrappedLineParser has little or no value. I will revert this patch if no one objects.

I have second thoughts about linking block braces in UnwrappedLineParser. Consider the following example:

#if A
while (a) { // Linked to the r_brace below
#else
while(b) { // Also linked to the r_brace below
#endif
  foo();
}  // Linked to the l_brace of while(b) above

The l_brace of while (a) is still linked to the r_brace that has been re-linked to another l_bace.

Because MatchingParen will be reset in the annotator anyway, linking it for block braces in UnwrappedLineParser has little or no value. I will revert this patch if no one objects.

I don't like it, but that seems to be the reasonable approach.