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

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".

Related

Why does my debounce not work, yet produce no errors?

Why does my debounce not work, yet produce no errors?
Here's my script so far, but it isn't working.
local UIS = game:GetService("UserInputService")
local toggle = false
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
end
end)
If toggle is false you set it to true
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
And then immediately set it to false as you run into your second conditional statement.
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
So your change is revoked immediately befor anything had a chance to update.
If you have two exclusive states use if / else.
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
else
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
As all you're doing is to assign boolean values you can simply get rid of your conditions and do this:
toggle = not toggle -- toggle your state
-- assign the state to the visible property
script.Parent.BackpackFrame.Visible = toggle
script.Parent.BackpackText.Visibile = toggle
The code is setting toggle to false immediately after setting it to true.
if toggle == false then
-- false part
toggle = true
end
if toggle == true then
-- true part
toggle = false
end
Just use an else instead of checking toggle twice
if toggle then
-- true part
toggle = false
else
-- false part
toggle = true
end

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: Breaking line in the initializers between members rather than within a member?

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?

Using a Lua Script to send inputs to BizHawk

I've tried using joypad.set(Inputs), but that doesn't seem to do anything. Also joypad.write(Inputs) doesn't even work. I'm stuck in a corner, and I don't know what to do.
This example works for when emulating a GBA. It will make the emulator press right every frame
input = {}
input['A'] = false
input['B'] = false
input['Down'] = false
input['L'] = false
input['Left'] = false
input['Power'] = false
input['R'] = false
input['Right'] = true
input['Select'] = false
input['Start'] = false
input['Up'] = false
while true do
joypad.set(input)
emu.frameadvance()
end
depending what you emulating this also might be useful
input['P1 A'] = false

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.

Resources