This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Parse the else part of `#if 0`
ClosedPublic

Authored by sstwcw on Sep 10 2022, 12:42 PM.

Details

Summary

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

Previously things outside of #if blocks were parsed as if only the
first branch of the conditional compilation branch existed. Unless the
first condition is 0, then the outer parts would be parsed as if
anything inside the conditional parts didn't exist. Now we use the
second conditional branch if the first condition is 0.

Diff Detail

Event Timeline

sstwcw created this revision.Sep 10 2022, 12:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 10 2022, 12:42 PM
sstwcw requested review of this revision.Sep 10 2022, 12:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 10 2022, 12:42 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sstwcw added a project: Restricted Project.Sep 10 2022, 12:43 PM
sstwcw edited the summary of this revision. (Show Details)Sep 10 2022, 12:46 PM
owenpan accepted this revision.Sep 13 2022, 1:32 AM

Thanks!

clang/lib/Format/UnwrappedLineParser.cpp
1128
This revision is now accepted and ready to land.Sep 13 2022, 1:32 AM
This revision was automatically updated to reflect the committed changes.

This seems to have regressed comment alignment on unrelated directives:

#if 0
#endif

#if X
int something_fairly_long;  // Align here please
#endif                      // Should be aligned

These comments were aligned before this patch, but are no longer.
I'm not sure why, maybe setting the taken branch to 1 has some negative side-effect when there's no branch 1?
Filed https://github.com/llvm/llvm-project/issues/58188