This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Don't skip #else/#elif of #if 0
ClosedPublic

Authored by owenpan on Oct 30 2022, 5:37 PM.

Diff Detail

Event Timeline

owenpan created this revision.Oct 30 2022, 5:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 30 2022, 5:37 PM
owenpan requested review of this revision.Oct 30 2022, 5:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 30 2022, 5:37 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This patch fixes the regression caused by 2183fe2 while introducing a new regression. But in my opinion the new regression is less of a problem than the old one. Therefore I think it is okay.

Take this piece of code similar to the example in 71814b4.

before

void SomeFunction(int param1,
                  template <
#ifdef A
#if 0
#endif
                      MyType<Some>>
#else
                      Type1, Type2>
#endif
                  param2,
                  param3) {
  f();
}

after

void SomeFunction(int param1,
                  template <
#ifdef A
#if 0
#endif
                      MyType<Some>>
#else
                  Type1,
                  Type2 >
#endif
                      param2,
                  param3) {
  f();
}
clang/lib/Format/UnwrappedLineParser.cpp
1150

Can you add a comment here saying this line is a nasty hack which breaks the assumption that PPChainBranchIndex should have as many entries as the number of nestings of preprocessor branches and we should probably come up with a better way?

owenpan updated this revision to Diff 472204.Oct 31 2022, 7:36 PM

Tweaked the fix and added a test.

This patch fixes the regression caused by 2183fe2 while introducing a new regression. But in my opinion the new regression is less of a problem than the old one. Therefore I think it is okay.

Take this piece of code similar to the example in 71814b4.

before

void SomeFunction(int param1,
                  template <
#ifdef A
#if 0
#endif
                      MyType<Some>>
#else
                      Type1, Type2>
#endif
                  param2,
                  param3) {
  f();
}

after

void SomeFunction(int param1,
                  template <
#ifdef A
#if 0
#endif
                      MyType<Some>>
#else
                  Type1,
                  Type2 >
#endif
                      param2,
                  param3) {
  f();
}

Fixed.

sstwcw accepted this revision.Nov 1 2022, 7:19 AM

Thanks for fixing the problem I caused.

This revision is now accepted and ready to land.Nov 1 2022, 7:19 AM

Can you also add a test for #elifdef and #elifndef?

sstwcw added a comment.Nov 1 2022, 7:28 AM

It just occurred to me that klimek is the one who added all this stuff. Why didn't you include him as a reviewer?

It just occurred to me that klimek is the one who added all this stuff. Why didn't you include him as a reviewer?

@djasper and @klimek name is likely all over the code, as they were the "founding fathers of clang-format", hopefully they are off doing something new and exciting, but those of us here on the reviewers list along with @curdeius are really the core clang-format team at this time. Hopefully we are qualified enough. (actually @owenpan is likely the most qualified ;-) )

We do our best to stay true to the original designers goals and be good custodians of clang-format, but my hope is they are watching over us even from afar but we assume they trust us with their baby!

We tend not to ask them to give back more unless they want to.

Can you also add a test for #elifdef and #elifndef?

I added a test (lines 6130-6136) for #elif but not #elifdef and #elifndef because all three are on the same execution path in the parser (lines 1113-1115). Actually, they all call the same function (via parsePPElIf()) that #else does. This is probably why none of them has a separate test in the LayoutStatementsAroundPreprocessorDirectives unit test. So I will do the opposite and remove the test for #elif. :)

MyDeveloperDay accepted this revision.Nov 1 2022, 1:10 PM
This revision was landed with ongoing or failed builds.Nov 2 2022, 1:33 PM
This revision was automatically updated to reflect the committed changes.