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;
 }
}
 - 
 5My advice is change your style guide. That is very hard to read. Make the if like the else.Retired Ninja– Retired Ninja2023年10月02日 16:49:21 +00:00Commented 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 thisSka– Ska2023年10月02日 17:00:24 +00:00Commented Oct 2, 2023 at 17:00
 - 
 If you use clang-format you can write your own rules, relatively easily.cigien– cigien2023年10月02日 17:00:48 +00:00Commented Oct 2, 2023 at 17:00
 - 
 2Oh 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.JohnFilleau– JohnFilleau2023年10月02日 17:40:11 +00:00Commented 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.Ted Lyngmo– Ted Lyngmo2023年10月02日 19:02:15 +00:00Commented Oct 2, 2023 at 19:02
 
1 Answer 1
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
5 Comments
Explore related questions
See similar questions with these tags.