Clang-Format: Breaking line in the initializers between members rather than within a member? - clang-format

I'm trying to format a C++ code with clang-format.
// input.hpp
namespace Foo{
class Bar{
Bar(bool is_zzz, int nn) : foobar(nullptr), xx{nullptr, nullptr}, yy{0, 0}, zzz(is_zzz), value(xxx_yyy_zzz) {}
};
}
// .clang-format
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 64
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AllowShortFunctionsOnASingleLine: All
ContinuationIndentWidth: 8
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
Cpp11BracedListStyle: false
FixNamespaceComments: true
IndentCaseLabels: false
IndentWrappedFunctionNames: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PointerAlignment: Middle
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
clang-format version 9.0.0 produces
// output.hpp
namespace Foo {
class Bar {
Bar(bool is_zzz, int nn)
: foobar(nullptr), xx{ nullptr, nullptr }, yy{ 0,
0 },
zzz(is_zzz), value(xxx_yyy_zzz) {}
};
} // namespace Foo
This breaks a line within the initializer for yy ({0, 0}). However, I prefer the following:
// output2.hpp
namespace Foo {
class Bar {
Bar(bool is_zzz, int nn)
: foobar(nullptr), xx{ nullptr, nullptr },
yy{ 0, 0 }, zzz(is_zzz), value(xxx_yyy_zzz) {}
};
} // namespace Foo
that is, prefer breaking a line in the initializers between members rather than within a member. How should I set up .clang-format?

Related

Curly brace ( { ) in new line

I use clang-format (v6) for formating C/C++ code.
I configure "BraceWrapping" in .clang-format that all be in new line.
But I have any problem in one case.
See Picture for better understand. I want to make as left on picture.
.clang-format file is:
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true

Clang-format syntax to get initializer opening brace on its own line

I'm writing C-code using Visual Studio Code with a .clang-format file. I've got all of the formatting the way I want it, with the exception of structure initializations. I would like this format -
static ifc_t _ifc =
{
.ps_read = NULL,
};
but instead, I'm getting this -
static ifc_t _ifc = {
.ps_read = NULL,
};
My clang-format file having to do with opening braces is this -
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterExternBlock: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
Is there another clang-format setting to control the position of the opening brace on a structure initialization?
Thanks...
Appears there's an existing PR to address this deficiency by adding the missing BraceWrapping / BeforeStructInitialization (https://reviews.llvm.org/D91949), but it's sadly stalled since 2021-05-27 with "revisions needed".

local a = true print(a and false or true) why result always be true whether a==false or a == true?

Why is a not correct?
I can't understand.
Code:
C:\Users\Administrator>lua53
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
> local a = true print(a and false or true)
true
> local a = false print(a and false or true)
true
> local a = false print(a == true and false or true)
true
> local a = true print(a == true and false or true)
true
> local a = true print(a == false and false or true)
true
> local a = false print(a == false and false or true)
true
> local a = false print((a == false) and false or true)
true
> local a = false print(not a )
true
> local a = true print(not a )
false
>
In Lua, and and or have the lowest operator precedence, with or being lower than and. Therefore, any expression of the form X and false or true will be interpreted as (X and false) or true.
Well, anything logically and-ed with false is false; that's how logical and works. And anything logically or-ed with true is true. And since or comes last, all of those expressions are just over-complicated ways of saying true.

Clang-format "AlwaysBreakAfterReturnType : None" doesn't work

I want to use clang-format, but it always starts with a new line after the returnType. I read the documentation and tried
"AlwaysBreakAfterReturnType : None"
but this seams to have no effect. I'm using clang-format 6.0 in ubuntu 17.10 inside QT creator.
is:
int
main() {
...
}
expected:
int main() {
...
}
Version: clang-format 6.0, clang-format config file:
BasedOnStyle: Mozilla
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments : true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakStringLiterals : false
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: true
FixNamespaceComments: true
IndentCaseLabels: false
IndentPPDirectives: AfterHash
IndentWidth: 4
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword : false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 3
SpacesInAngles: false
SpacesInCStyleCastParentheses: true
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
Any ideas?
Just try to assign the deprecated property 'AlwaysBreakAfterDefinitionReturnType' to 'None' too.
This works well for me.
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
You should read the documentation carefully:
RTBS_None (in configuration: None) Break after return type
automatically. PenaltyReturnTypeOnItsOwnLine is taken into account.
So "None" doesn't mean that it will never break after return type, it will take into account other settings like BinPackParameters, BraceWrapping, etc. and their penalties.
To make breaking after return type less likely or "almost" turned off you can set PenaltyReturnTypeOnItsOwnLine to a very high value, for example:
PenaltyReturnTypeOnItsOwnLine: 1000000
Please note that I'm not that familiar what exactly penalties numbers mean, how they relate to each other and how the final line breaking is calculated. You'll have to find that information elsewhere (source code of clang-format?).
I can't reproduce your issue on the minimal example you provide as comment:
$ cat .clang-format
BasedOnStyle: Mozilla
AlwaysBreakAfterReturnType: None
$ cat main.c
int main() {
return 0;
}
$ clang-format main.c
int
main()
{
return 0;
}
$ clang-format --version
clang-format version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Indeed, you have also set IndentWrappedFunctionNames, whose behavior is exactly the one you obtain. If you don't want this behavior, then don't set IndentWrappedFunctionNames.

How can hash.values.all? == false return true when all the values are not false?

How can #filters.values.all? == false return true when `#filters.values' clearly shows three true values?
EDIT: And how do you check to see if every value is false?
EDIT2: Because people want to copy and paste an arbitrary code snippet:
f = {
self: true,
clear: true,
something1: false,
something2: true
}
f.all? == false
==> false
f.values.all? == false
==> true
f.values
==> [true, true, false, true]
Enumerable#all?, from the docs:
Passes each element of the collection to the given block. The method
returns true if the block never returns false or nil. If the block is
not given, Ruby adds an implicit block of { |obj| obj } which will
cause all? to return true when none of the collection members are
false or nil.
That means that #filters.values.all? will return false unless every attribute is set to true (or truthy, to be more accurate) so, if you want to know when every item is false, then you will have to pass a block to all? and check each value, like this:
#filters.values.all? { |value| value == false } #=> true
UPDATE
Previous answer stated that also !#filters.values.all? will return true when all values are truthy, and that's true, but it will also return true if only one is set to false; in fact, it will always return true unless all values are set to true.
I guess the all? method will call the block with each
For eaxmple enumerable.all? will be executed like this:
enumerable.each {|x| x }
hash_enumerable.each {|k, _v| k }
So when the enumerable is a hash the block firt params will be the key....
To check if all the values in your hash is false, try this:
temp = {self: false, clear: false, lorem: false, ipsum: false}
temp.values.uniq == [false]
#=> true
temp2 = {self: true, clear: true, lorem: false, ipsum: true}
temp2.values.uniq == [false]
#=> false
A shorter, though slightly less clear, way to determine if all of the values are false is to simply ask if any of them are true and negate the response:
#filters = {foo: false, bar: true, baz: false}
!#filters.values.any? #=> false
#filters = {foo: false, bar: false, baz: false}
!#filters.values.any? #=> true

Resources