This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix multiple preprocessor if sections parsing incorrectly
AbandonedPublic

Authored by sstwcw on Oct 11 2022, 7:25 PM.

Details

Summary

In 2183fe2 I didn't notice that PPLevelBranchIndex was common to the
entire file. The commit caused problems when there are multiple
preprocessor if sections. Using a single PPLevelBranchIndex also
brings the problem that the extra branches in an if section with more
branches than other sections are parsed without other sections.

This patch fixes the problem by storing the structure of the
preprocessor if sections. This way, PPLevelBranchIndex is checked
against the stored structure, and if a nonexistent branch is called for
because another section has more branches, the first or second branch
will be parsed. The change in 2183fe2 setting PPLevelBranchIndex is
reverted. Instead the stored branch structure is used to determine when
the second branch should be parsed instead of the first.

The logic for determining when a branch is skipped in moved from
conditionalCompilationStart and conditionalCompilationAlternative
into conditionalCompilationCondition because the two cases now have
more overlap. PPChainbranchIndex is changed to include the section
number in addition to the branch number.

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

Diff Detail

Event Timeline

sstwcw created this revision.Oct 11 2022, 7:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 11 2022, 7:25 PM
sstwcw requested review of this revision.Oct 11 2022, 7:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 11 2022, 7:25 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sstwcw added a project: Restricted Project.Oct 11 2022, 7:26 PM

Should I add a test with comments to be aligned like in the bug report? I was not sure because I didn't find an existing test for alignment of comments.

Should I add a test with comments to be aligned like in the bug report? I was not sure because I didn't find an existing test for alignment of comments.

Yes you should.

Did you test this with nested #if #endif?

sstwcw updated this revision to Diff 467829.Oct 14 2022, 10:25 AM
  • add tests

Did you test this with nested #if #endif?

No. I added the tests now.

sstwcw updated this revision to Diff 467833.Oct 14 2022, 10:32 AM
  • correct spelling
sstwcw updated this revision to Diff 470016.Oct 23 2022, 3:00 PM
  • Merge
  • use ArrayRef

I'm lost at this topic. Is this change still needed? Is it superseded by or additional to D137052?

sstwcw abandoned this revision.EditedNov 3 2022, 6:36 AM

It does some more than D137052. But the additional stuff is not very important and no one can review it. So I am closing it.