2

I am working on setting up a style format check for a large library. Currently, our team styleguide prefers that instead of one line if statements, we use a "2 line" statement, which looks like this

if(statementIsTrue)
{ printf("True"); }
else
{
 anExampleBlock = true;
 elsestatement.function();
}

Note how the if block is partially indented to 2 spaces, to line up with the else statement. The style's indentation is 3 spaces. In addition, there are 2 spaces between the semicolon and the close brace for symmetry purposes.

Other thing to note about the style. braces always go on a new line. That is the intention behind the above. For all if statements, functions, classes, etc. the opening brace goes on a new line.

How can we adjust clang-format to perform this task?

EDIT: More examples of blocks

/**
 * This is just example code for formatting, not meant to be useful code
 */
bool testFunction(int largeNumber, int randomNumber)
{
 for (int j = 0; j < largeNumber; j++)
 {
 if (j == randomNumber)
 { return true; }
 else
 { print("not found"); }
 }
}
int main()
{
 //Three space indentation (tab characters should not be used anywhere)
 //Single space should exist between if, else if, for, while, etc. and (...)
 if (testFunction(testParm1, testParm2))
 {
 print("Hello");
 return true;
 }
}
asked Oct 2, 2023 at 16:41
12
  • 5
    My advice is change your style guide. That is very hard to read. Make the if like the else. Commented Oct 2, 2023 at 16:49
  • Posted another example to clarify. I understand this is not a common style, but this is a very old library, that would not be easy to change style for. Looking for a solution to actually format this Commented Oct 2, 2023 at 17:00
  • If you use clang-format you can write your own rules, relatively easily. Commented Oct 2, 2023 at 17:00
  • 2
    Oh my god. OP is asking how to do a specific, objective thing in a common tool. Everyone is saying in comments, "You do it by doing it." How do they do it? This question should never have been closed. It's not opinion based. It's a clear question with an objective answer. Commented Oct 2, 2023 at 17:40
  • 1
    @JohnFilleau When I voted to close this, the question was "Is there any formatter that can handle this? And what would the configuration look like?" - but I'll vote to reopen now that it's specifically about clang-format. Commented Oct 2, 2023 at 19:02

1 Answer 1

0

I think, if you edit your .clang-format file in your project repo with the following settings, it should work.

Please note - I haven't tested this, but reading the documentation, I felt this would be suitable.

BreakBeforeBraces:Allman,
AllowShortBlocksOnASingleLine: Always,
AllowShortIfStatementsOnASingleLine: WithoutElse

If this doesn't work, experiment with possible options for these fields available at Clang-format style options

Hope this helps!

Here is an online tool which I found where you could play around with your code trying out various options: clang-format configurator

Sample experiment with your code snippet

answered Oct 5, 2023 at 16:13
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, this does not work. The settings you provided result in this ``` if (_c) { anotherVariable = nullptr; } if (_a) { anotherVariable = "baz"; } else { anotherVariable = "bar"; } ```
The formatting is not working, but the blocks end up being 4 lines long, instead of the intended 2
The expected output is not clear from the original question. Please post snippet of how your current code look like and how you want it to be displayed.
Added some more information in the my response. Hope it helps.
This does not help. Notice how your output is different than my expected output

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.