If I have a function with arguments too long for the column width it will put them all on new lines as expected. However, it appears that this sometimes breaks my pointer alignment:
Before formatting:
struct ConnectionResult *connect_to_server(struct Folders *start_folder, struct Config *config, struct CTypet *conn_type)
After formatting:
struct ConnectionResult *connect_to_server(struct Folders *start_folder
struct Config * config,
struct CTypet * conn_type);
It seems like it wants to prefer aligning the actual variable name and ignore the pointer alignment rule. This causes my functions to sometimes look inconsistently styled relative to other functions. My .clang_format
is
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignTrailingComments: 'true'
BreakBeforeBraces: Allman
IndentCaseLabels: 'true'
IndentWidth: '4'
Language: Cpp
PointerAlignment: Right
SortIncludes: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
BinPackParameters: 'false'
BinPackArguments: 'false'
UseTab: Never
ColumnLimit: 80
As a side note this configuration also sometimes breaks long for loops in a very ugly way:
Before:
for(size_t current_conn = initial_conn; current_connection <= max_connections; current_conn += connection_offset) { ...}
After format:
for (size_t current_conn = initial_conn; current_conn <= max_connections;
current_conn += connection_offset)
{
...
}
When in reality I would like to see each argument of the for loop on its own line.
I am relatively new to using clang format for C and I'm not exactly sure how to fix this. Is there a good way to get my clang format to work as I'd expect in both cases?
Short answer: no,
clang-format
won't do either of those.It is
AlignConsecutiveDeclarations
which causes the parameter declarations to align, andPointerAlignment
which controls spacing around the*
and&
in a declaration. But it appears thatAlignConsecutiveDeclarations
ignoresPointerAlignment
, andPointerAlignment: Right
really just means "add a space before the*
or&
".None of this seems to be documented, unfortunately.
For the for-loop, it seems like you would need a hypothetical new style option called something like
AllowBinPackForLoop
, which would default to true (for backwards compatibility), but false would allow the for-loop to be split like you're asking if it needs more than one line. But nothing like that exists as far as I know.