clang-format BinPackArguments not working as expected - clang

clang-format has 2 options called BinPackParameters and
BinPackArguments. They seem to control how function declarations and function calls are indented.
BinPackParameters seems to provide the expected result for a function declaration but BinPackArguments does not seem to work as one would expect for a function call.
Here is a simple test file:
#include <stdbool.h>
void function_with_a_huge_name_that_should_just_not_be(unsigned int a, char *b, unsigned int c, unsigned int d, unsigned int e)
{
return;
}
int main()
{
function_with_a_huge_name_that_should_just_not_be(13, "bb", 1234234, 4324324, 2355345);
}
And this is how it is formatted:
#include <stdbool.h>
void function_with_a_huge_name_that_should_just_not_be(unsigned int a,
char *b,
unsigned int c,
unsigned int d,
unsigned int e)
{
return;
}
int main()
{
function_with_a_huge_name_that_should_just_not_be(
13, "bb", 1234234, 4324324, 2355345);
}
My .clang-format file is as follows:
---
AccessModifierOffset: -2
AlignAfterOpenBracket: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BinPackArguments: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
My clang-format version is: 3.6.0 (tags/RELEASE_360/final)
With both BinPackParameters and BinPackArguments being false I would have expected to get the same indentation for the function call as I am getting for the function declaration.
Any idea what I am doing wrong?

I don't think you are doing anything wrong. What happens is that clang-format realizes that the line where you are calling the function is longer than your column limit (80 chars in your settings). Your AlignAfterOpenBracket is set to false, so clang-format places the arguments on a new line (Note that AlignAfterOpenBracket has gained additional possibilities in later versions of clang-format).
You have set both BinPack... settings to false, however there is an additional setting that controls the function declaration vs the function call, AllowAllParametersOfDeclarationOnNextLine (set to false in your example). For the function declaration, this will cause all parameters to be on separate lines if they do not fit on the same line as the function name. For the function call there is no corresponding setting.
In your case, the arguments that you give to the function are placed on the next line after the function name. The length of the second line is < 80, so clang-format does not do anything more with it. If the arguments line would have been longer than your column limit, clang-format would have placed them on separate lines.
So the answer is that as of version 3.9, there is no way to configure clang-format to place each argument on a separate line if they fit on one line.

Try to set ColumnLimit to 0. It looks like this option "overrides" or has a higher priority over BinPackParameters and BinPackArguments options.

BinPack* options set to false will force parameters/arguments to be either all on one line or each on separate line. Both cases are allowed, but not mixing, eg. two parameters on one line and the rest on another line is not allowed.
clang-format seems to choose all-on-one-line vs. each-on-separate-line format individually for each case.

Related

How to check if a number have integer representation

How to check if a number have integer representation
number can be a float or double
In Lua 5.3+, use math.tointeger:
math.tointeger (x)
If the value x is convertible to an integer, returns that integer. Otherwise, returns nil.
Maybe check numbers for dot to see if it is a float or integer?
Then check this out...
dotchk=function(...);
local args={...}
assert(args[1],'Need argument')
local _,b=tostring(args[1]):gsub('%.','') -- Check for dot
if b>0 then
print('floating point found in:',args[1])
return true
else
print('No dot in:',args[1])
return false
end
end
Three examples...
>dotchk(math.pi)
floating point found in: 3.1415926535898
true
>dotchk(math.maxinteger)
No dot in: 9223372036854775807
false
>dotchk(math.mininteger)
No dot in: -9223372036854775808
false

clang-format how to ignore extern C?

This is my current sample code
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
BUY = 1,
SELL = 2
} OrderAction_e;
#ifdef __cplusplus
}
#endif
After I run the clang format it is changing as below.
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
BUY = 1,
SELL = 2
} OrderAction_e;
#ifdef __cplusplus
}
#endif
It is adding an additional two spaces for all my functions and typedefs.
Is there an option which I can use to ignore the extern C braces so that, my code looks unchanged like the first version of code I pasted above.
Following is the clang version used in my company
LLVM (http://llvm.org/):
LLVM version 3.4.2
If you were using a later version of clang-format, you could get very close. But with 3.4.2, I don't think so.
With version 6.0.0, you can get very close, but it seems to be necessary to put the brace on the same line as the extern "C" in order to disable the indentation of the extern "C" section. Doing this requires using the Custom setting for BreakBeforeBraces. This behavior of disabling indenting the extern "C" block doesn't appear to be documented anywhere, but it does work for me.
Try modifying your .clang-format file to contain this:
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true # <-- You need this
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: false # <-- And this
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom # <-- And this
Note that there are a bunch of options there which you can leave however you normally set them. Only the values of AfterEnum, AfterExternBlock, and BreakBeforeBraces matter for this. See the documentation for more details about these settings.
If you don't already have a .clang-format file, you would start by doing clang-format -dump-config > .clang-format and then edit the file.

Make left align colon clang-format in #pragma

How can I always align colon to the left with clang-format.
I don't want it to be disable : 1234, but disable: 1234.
#pragma warning(disable: 1234)
I guess you need this.
SpaceBeforeRangeBasedForLoopColon (Boolean) clang-format 7
If false, spaces will be removed before range-based for loop colon.
true: false:
for (auto v : values) {} vs. for(auto v: values) {}

ternary works different in local and heroku

I have this method it's very simple and almost all of the time the isTrue param should be false and return "2".
def test(isTrue = false)
isTrue ? 1 : 2
end
this works fine in my dev env but when I push it to heroku suddenly it starts returning as if it is true, and Im absolutly positive that its false. I think it possibly be checking if the var is nil ( or something like that)
I changed the ternary to:
isTrue == true ? 1 : 2
And it corrects the problem, I don't understand why this happens.
Can someone explain it?
thanks!
This is definitely wrong:
isTrue = true ? 1 : 2
It sets the variable isTrue to true and uses the result of that statement (true) as the input of the ternary operator, so this will always return true.
Change it to:
isTrue == true ? 1 : 2
Regarding the differences between development and production mode: check that you really feed booleans into the method and not integers (0 or 1), strings ('0', '1', 't', 'f', 'y', 'n', etc) or nil.
isTrue = true ? 1 : 2
this will always return 1 as Mark Meeus commented.
= is the assignment operator in ruby, used to make assign a variable a given value.
==, however is a comparison operator.
so with your code as it is currently, you're assigning "isTrue = true" and then telling the code to return 1 if isTrue is true.

Why would you add "or false" to the end of your boolean expression?

I have seen code in lua like this
if (a==b or false) then
what is the purpose of the "or false"?
This is the original code:
Function = function(self, aura, auraID)
-- Store the version.
-- aura.Version = 50000;
-- Fix texmode/glow.
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
-- Texture source.
if(aura.owntex or aura.SourceType == PowaAuras.SourceTypes.Icon) then
aura.SourceType = PowaAuras.SourceTypes.Icon;
elseif(aura.wowtex or aura.SourceType == PowaAuras.SourceTypes.WoW) then
aura.SourceType = PowaAuras.SourceTypes.WoW;
elseif(aura.customtex or aura.SourceType == PowaAuras.SourceTypes.Custom) then
aura.SourceType = PowaAuras.SourceTypes.Custom;
elseif(aura.textaura or aura.SourceType == PowaAuras.SourceTypes.Text) then
aura.SourceType = PowaAuras.SourceTypes.Text;
else
aura.SourceType = PowaAuras.SourceTypes.Default;
end
end
And here is another example
-- Iterate over element children.
visible, hidden = self:UpdateScrollList(key, level+1, visible, hidden,
(parentShowing == true and item:GetExpanded() or false));
This looks like a behavior that comes from the syntax:
a=b or false
to initialize a to the value of b or false if b isn't defined.
Within the if statement as you wrote it, I don't see any purpose since the == is evaluated before the or. If they used parenthesis to change the order of operations, then it could be used to validate that b has been defined, e.g.:
> a=nil
> b=nil
> if (a == (b or false)) then print("yikes") else print("aok") end
aok
> if (a == b or false) then print("yikes") else print("aok") end
yikes
It is used to temporarily disable a block of Code.
It's semantics are technically the same as:
#if 0
dead code goes here ...
#endif
It's to explicitly state the value assigned to the expression when none of its prior conditions are true.
In Lua, the and and or expressions return the values that determine their truth values, rather than the flat truth value itself, like this:
function And(A,B)
if A then return B --if A is true, then the agreement of both depends on B
else return A end --otherwise, they're not, because A wasn't
end
function Or(a,b)
if A then return A --if A is true, then it doesn't matter if B was true
else return B end --otherwise, the truth hinges on B's value
end
In Lua, all values other than false and nil evaluate to true in conditional constructions, so this behavior is commonly used as an idiom to describe default values:
local color = color or 'blue' -- if `color` is defined to any value other than
-- `false` (such as any string), it will evaluate
-- as true, and the value will be returned from
-- the `or` statement (so `color` will be assigned
-- its own value and no change will result) -
-- if `color` is not defined, the `or` statement
-- will evaluate its `nil` value as false and
-- return the second value, 'blue'
This idiom can be combined and extended in various ways:
local color = color or self.color
or (favorites and favorites.color)
or (type(favourites)=='table' and favourites.colour)
or get_defaults('color')
or {'red','blue','green','yellow'}[math.random(1,4)]
Traditionally, though, they stay fairly simple, with maybe a few chained or cases before the default value.
That's what we're seeing here: the cases where or false is being used are for the last-case value for the expression.
So, while this:
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
could have been written as this with exactly the same effect (since the == comparisons will return either true or false):
aura.texmode = (aura.texmode == 1 or aura.texmode == true);
adding the final or false makes it more obvious when looking at that line that the value can be false.
The same goes for the second example, in which the value is being assigned to an argument.
There is no use, because if a~=b then it will try false, which is ... false and return false anyway ...

Resources