While using clang-format (8.0.0) I am encountering weird formatting issues that I feel may be bugs. I don't want to jump the gun and report them as bugs without knowing if I'm doing something incorrectly or missed a configuration setting though. I have a few examples of where I feel my settings are not being adhered to and instead the formatter is going 'rogue' in a sense.
Here is my .clang-format settings file:
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...
Example 1 - Single line assignments with multiple parentheses:
Before formatting:
extern "C"
{
HBITMAP (WINAPI *Real_LoadBitmapW)(HINSTANCE, LPCWSTR) = LoadBitmapW;
HANDLE (WINAPI *Real_OpenMutexA)(DWORD, BOOL, LPCSTR) = OpenMutexA;
}
After formatting:
HBITMAP(WINAPI* Real_LoadBitmapW)
(HINSTANCE, LPCWSTR) = LoadBitmapW;
HANDLE(WINAPI* Real_OpenMutexA)
(DWORD, BOOL, LPCSTR) = OpenMutexA;
Expected results:
HBITMAP (WINAPI* Real_LoadBitmapW)(HINSTANCE, LPCWSTR) = LoadBitmapW;
HANDLE (WINAPI* Real_OpenMutexA)(DWORD, BOOL, LPCSTR) = OpenMutexA;
Here, the formatter is randomly breaking my assignments into new lines as it sees fit. The ColumnWidth setting here appears to not be honored. If I set the ColumnWidth to an actual number, say 200, then the lines to not break, but the formatting is still incorrect as now space is placed after the return type. But using ColumnWidth breaks other formatting. I want to have ColumnWidth be set to 0 overall. I'm not sure what rule is forcing this to a new line and removing spaces after the return type.
Example 2 - Not Honoring C-Style Casting Space Setting:
Before formatting:
auto prodVer = (LPVOID)nullptr;
auto prodVerSize = (UINT)0;
After formatting:
auto prodVer = (LPVOID) nullptr;
auto prodVerSize = (UINT)0;
Expected results:
auto prodVer = (LPVOID)nullptr;
auto prodVerSize = (UINT)0;
Here, a space is being added before the nullptr. This seems to only happen when using nullptr as the value being casted. If switched to a number or variable, it works fine. Also if switching the LPVOID to say, void*, it also fixes the issue. Also, with this issue, changing the 'Standard' setting from Cpp11 to Cpp03 fixes it as well but then introduces other formatting I do not want because of the standard changes. My code uses C++11,14,17,20 so using the Cpp11 Standard setting is needed here.
I just started using clang-format last night so I'm very new to this and trying to understand how the rules affect each other. I've read the docs page covering each rule and what it does, it just seems at certain times they are ignored like above.
I have other cases/issues, but I feel they may play into the above two. If I get these fixed first, the others may be fixed already as well so I can create another question later if more problems persist.