Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Forced line-break in pre-processor statements #123382

Closed
tl-photography opened this issue Jan 17, 2025 · 4 comments
Closed

[clang-format] Forced line-break in pre-processor statements #123382

tl-photography opened this issue Jan 17, 2025 · 4 comments
Labels
clang-format question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@tl-photography
Copy link

Lets assume we have this PP directive:

#if ((CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED))
[...]
#endif

It would be nice to configure clang-format in a way that I would format this like:

#if (   (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) )
[...]
#endif

The advantage of this would be that when SCM is used and lines would be added or removed, less or at least easier conflicts would occur.

In a large code base with a lot of feature switches, this would be very helpful.

I have checked the code if I could file a PR myself, but tbh I didn't find a place to start.

@tl-photography tl-photography changed the title Forced line-break in pre-processor statements [clang-format] Forced line-break in pre-processor statements Jan 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2025

@llvm/issue-subscribers-clang-format

Author: None (tl-photography)

Lets assume we have this PP directive:
#if ((CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED) || (CONDITION_A_SWITCH == ENABLED))
[...]
#endif

It would be nice to configure clang-format in a way that I would format this like:

#if (   (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) \
     || (CONDITION_A_SWITCH == ENABLED) )
[...]
#endif

The advantage of this would be that when SCM is used and lines would be added or removed, less or at least easier conflicts would occur.

In a large code base with a lot of feature switches, this would be very helpful.

I have checked the code if I could file a PR myself, but tbh I didn't find a place to start.

@owenca
Copy link
Contributor

owenca commented Feb 25, 2025

You can use BreakBinaryOperations, e.g. clang-format -style='{BreakBinaryOperations: OnePerLine}' will give you:

#if ((CONDITION_A_SWITCH == ENABLED) ||                                        \
     (CONDITION_A_SWITCH == ENABLED) ||                                        \
     (CONDITION_A_SWITCH == ENABLED))
#endif

@tl-photography
Copy link
Author

Unfortunately this does not work if you use a Column limit setting.

@tl-photography
Copy link
Author

Forget what I said. I did something wrong in my test.

This seems to work :)

@owenca owenca closed this as completed Mar 25, 2025
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

4 participants