How to discourage clang-format to break after = signs? - c++

I'm using clang-format with a fairly minimal configuration file, and I'm not very familiar with the options. For the sake of the question, consider this piece of unformatted code:
int msgResult = ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE, MB_STYLE_ERROR);
When I run clang-format on this snippet, I get
int msgResult
= ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE, MB_STYLE_ERROR);
But I'd prefer
int msgResult = ShowMBox(R_MESSAGE, msgText, MB_OK_ENABLE | MB_CANCEL_ENABLE,
MB_STYLE_ERROR);
Is there a way to enforce not breaking after =, or at least prefer not to?

I believe you want these two style options:
BinPackArguments: true
AlignAfterOpenBracket: Align
Without BinPackArguments, clang-format will try to either put all function arguments on one line, or if it can't will put one function argument per line. Most of the pre-defined styles already set BinPackArguments to true, but the Mozilla style does not.
Without AlignAfterOpenBracket set to Align, clang-format will sometimes prefer to put all arguments on the second line, rather than just a single argument that doesn't line up with the other arguments. Most of the pre-defined styles already set AlignAfterOpenBracket to Align, but the WebKit style does not.
See the documentation for more details about these settings.
My testing was done with clang-format 6.0.0. Perhaps you had an earlier version that didn't support all this.

Related

uncrustify: how to combine arguments of multiline C function call on to a single line?

I am using uncrustify 0.69.0 (I can't get 0.71.0 to compile yet). I have the following:
sendto(cmd_sock, buf, strlen(buf),
0, (struct sockaddr *)&fromCmd,
sizeof(struct sockaddr_in));
And I want uncrustify to combine them to a single line:
sendto(cmd_sock, buf, strlen(buf), 0, (struct sockaddr *)&fromCmd, sizeof(struct sockaddr_in));
This old 2012 question is similar but existed during a much older version of uncrustify and did not have a solution:
Uncrustify Collapse Multiline Function Call
Is there a solution today?
2020-07-27- I tried to make a comment but it limited me to mostly nothing of value. So I'm updating my original question....
strange- nl_func_call_args is not found in my config files.... but this is:
# Whether to add a newline after each ',' in a function call if '(' and ')'
# are in different lines.
nl_func_call_args_multi_line = false # true/false
Notice that the options dont allow to "delete". Doing a search in all the config files I have back to .67 does not find nl_func_call_args. if one removes any .uncrustify.cfg and runs uncrustify --update-config one will NOT find nl_func_call_args. When I get a new version of uncrustify I ALWAYS run a default and analyze what changed. This is the only way I can trust the configs. My original question still stands. This is about creating a standard for C code. I see a LOT of crazy function call formatting, especially in recent years. The only way to get it into shape is to FIRST remove all the multiline crap. Then I can edit if needed to a better format. Otherwise I'm editing EVERY STINKING line. For the record I have been writing C since 1981 and almost 100% adhere to K&R. I have a handful of deviations. I also use VI so ALL of the format features that were in the original VI I still want code to adhere to so this editor will work properly.
regards
oldunixguy
# Add or remove newline after each ',' in a function call.
nl_func_call_args = remove # ignore/add/remove/force
That option was introduced on 2016-06-27 (796b5ba5)

clang-format: disable formatting for macros?

I am using clang-format as the auto-formatting tool for my codebase. But some of its features are bugging me.
For instance, I don't want it to format my macro definitions, since most of the time, it's more clear to just formatting them manually. But I don't know how to disable it in clang-format.
Another minor issue is pointer alignment. Sometimes, it's clear to make it aligned left, sometimes it's right. So I'd rather do it by hand. But disable it from clang-format seems impossible?
Any help on these issues?
You can wrap your macros in
// clang-format off
#define ... \
...
// clang-format on
To escape manually editing of each file you can use the regex
search: ^([ \t]*#[ \t]*define[ \t]+.+?\\\r?\n(?:.*?\\\r?\n)*.*?\r?\n)
replace: // clang-format off\r\n$1// clang-format on\r\n
for instance, in Notepad++, Ctrl+Shift+F - "Find in Files - "Replace in Files".
To date (up to v11) there is no way to disable pointer alignment. You can either set the style, or derive the style (clang-format will analyze a file for the most common alignment of & and * and will use it).

How to reset code formatting with Uncrustify

I am using Uncrustify to formatting my C++ code and I am making some experiments with infinite list of settings.
Because of some bad settings my code now has a lot of new line that split statement in more lines (mostly due to a short line width).
I would like to reformat the code in order to have one statement per line and reformat it in another way.
How can I do it?
There is no undo function in Uncrustify and if your SCM can not help you, you are unfortunately out of luck.
Regarding to the newlines, which seem to cause the most problems for you, you could try to remove and reapply them with the 'nl_remove_extra_newlines' option (see a short discussion about it in the uncrustify issue #994) in combination with other options that are influencing newlines.

clang-format Overriding multi-line comments for WebKit style

I am trying to use clang-format to clean up code in my repository. We are using the WebKit style as the basis for formatting, however we also want to make sure that multi line comments are formatted correctly.
From my understanding it is possible override the formatting rules of given style by define the .clang-format file as such:
BasedOnStyle: WebKit
AlignTrailingComments: true
This way clang-format should align the trailing comments.
Given the input file:
/**
* This is a multi-line comment
*/
void function() {
/**
* This is comment inside the function
*/
}
My expectation is the following output
/**
* This is a multi-line comment
*/
void function()
{
/**
* This is comment inside the function
*/
}
However what I get is:
/**
* This is a multi-line comment
*/
void function()
{
/**
* This is comment inside the function
*/
}
I've tried dumping out the formatting options of Webkit into a .clang-format file and changing the AlignTrailingComments from false to true. This doesn't make and difference either.
Is there some option in the Webkit style that is interfering with the AlignTrailingComments option?
AlignTrailingComments aligns comments trailing code in consecutive lines:
int short; // short
int longlonglong; // long
The real issue here is the pre-defined WebKit style setting ColumnLimit: 0. This somehow disables the indentation of non-first-line-part of multi-line comments. (And this doesn't seem to be documented anywhere - I think it's a bug.)
One workaround would be to set the column limit to something reasonable, like ColumnLimit: 80 or possibly ColumnLimit: 120. Possibly you could do this once, and then switch back to your usual ColumnLimit: 0 - but setting the column limit once will likely change formatting on many code lines, which will not be restored when you change the column limit back to zero, so probably not what you want.
AlignTrailingComments does not relate to this at all. As another answer explains, this is for aligning the trailing comments at the end of code lines. See the documentation for more details.
I don't believe CommentPragmas will help either. I'm pretty sure this only prevents clang-format from adding line breaks to those comments, but does not prevent indentation changes. (And this is not really documented either.) Anyway, what is needed here is adjusting the indentation, not leaving it alone, so CommentPragmas almost seems like the opposite of what is needed.
A possible workaround: use CommentPragmas with a regex to mark those comments as untouchable.

clang-format breaks lint annotations

We use lint in our codebase at work for C/C++, I'm trying to start integrating clang-format in my workflow as well.
Unfortunately, lint occasionally requires annotations to ignore a specific check, either of the format:
/*lint -[annotation] */
or
//lint -[annotation]
Specifically, if there's a space between the opening token for the comment and 'lint', it doesn't recognize it as an annotation directive. Unfortunately, the default settings I have for clang-format see that as an error and helpfully insert the space.
Is there any way to get clang-format to recognize comments matching that pattern and leave them alone? Right now I'm using 3.4, but could upgrade if needed.
Clang-format has a `CommentPragmas' option that is
A regular expression that describes comments with special meaning, which should not be split into lines or otherwise changed.
When I put the following line in my .clang-format file, my Lint comments remain untouched.
CommentPragmas: '^lint'
Other comments that still have "lint" in them, but are not Lint comments still get formatted.
You can disable clang-format for that section of your file by using:
int formatted_code;
// clang-format off
void unformatted_code ;
// clang-format on
void formatted_code_again;
See the Disabling formating on a piece of code section.