Skip to content

[clang-format] Doesn't align properly when using Java-style declaration. #55931

@StrangeRanger

Description

@StrangeRanger

I have clang-format set up to align the declaration of variables, such that the type, variable name, etc., all align with one another. Though when using the Java-style array declaration, instead of the C-style array declaration, clang-format aligns the type String[] with the variables, instead of with the other types.

Java-style declaration and clang-format output:

private final                String[] args;  // java-style declaration
private final A_ParserHelper parserHelper;
private final int            numOfCmdArgs;

C-style declaration and clang-format output:

private final String         args[];  // c-style declaration
private final A_ParserHelper parserHelper;
private final int            numOfCmdArgs;

Below is my clang-format configurations. My clang-format version is 14.0.4.

---
Language:        Java
# BasedOnStyle:  Chromium
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: Consecutive
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: Left
AlignOperands:   Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false  # Maybe: Do some testing
AllowAllParametersOfDeclarationOnNextLine: false # Maybe: Do some testing
AllowShortEnumsOnASingleLine: true  # Maybe
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false # Maybe
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never # Maybe
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
  - __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterCaseLabel:  false
  AfterClass:      false
  AfterControlStatement: Never
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  BeforeLambdaBody: false
  BeforeWhile:     false
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: NonAssignment  # OR None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon  # OR AfterComma
BreakBeforeTernaryOperators: true  # Maybe
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon  # OR AfterColon
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: true
ColumnLimit:     88
CommentPragmas:  '^ IWYU pragma:'
QualifierAlignment: Leave  # Maybe (WARNING: Warning provided on website)
CompactNamespaces: false  # Maybe
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat:   false
EmptyLineAfterAccessModifier: Leave
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: NextLine  # OR BinPack
FixNamespaceComments: false
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IfMacros:
  - KJ_IF_MAYBE
IncludeBlocks:   Merge
IncludeCategories:
  - Regex:           '^<ext/.*\.h>'
    Priority:        2
    SortPriority:    0
    CaseSensitive:   false
  - Regex:           '^<.*\.h>'
    Priority:        1
    SortPriority:    0
    CaseSensitive:   false
  - Regex:           '^<.*'
    Priority:        2
    SortPriority:    0
    CaseSensitive:   false
  - Regex:           '.*'
    Priority:        3
    SortPriority:    0
    CaseSensitive:   false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: AfterHash
IndentExternBlock: Indent
IndentRequires:  false  # Maybe
IndentWidth:     4
IndentWrappedFunctionNames: true
InsertTrailingCommas: None
JavaScriptQuotes: Double
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: OuterScope
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true  # TODO
ObjCSpaceAfterProperty: false  # TODO
ObjCSpaceBeforeProtocolList: true  # TODO
PenaltyBreakAssignment: 2  # TODO
PenaltyBreakBeforeFirstCallParameter: 1  # TODO
PenaltyBreakComment: 300  # TODO
PenaltyBreakFirstLessLess: 120  # TODO
PenaltyBreakOpenParenthesis: 120  # TODO
PenaltyBreakString: 1000  # TODO
PenaltyBreakTemplateDeclaration: 10  # TODO
PenaltyExcessCharacter: 1000000  # TODO
PenaltyReturnTypeOnItsOwnLine: 200  # TODO
PenaltyIndentedWhitespace: 0  # TODO
PointerAlignment: Right
PPIndentWidth:   -1
ReferenceAlignment: Pointer
ReflowComments:  true
RemoveBracesLLVM: false  # Maybe (WARNING: Warning provided on website)
SeparateDefinitionBlocks: Leave  # Maybe
ShortNamespaceLines: 1
SortIncludes:    CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: true
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterControlStatements: true
  AfterForeachMacros: true
  AfterFunctionDefinitionName: false
  AfterFunctionDeclarationName: false
  AfterIfMacros:   true
  AfterOverloadedOperator: false
  BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
  Minimum:         1
  Maximum:         -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both  # Maybe
Standard:        Auto
StatementAttributeLikeMacros:
  - Q_EMIT
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
TabWidth:        4
UseCRLF:         false
UseTab:          Never
WhitespaceSensitiveMacros:
  - STRINGIZE
  - PP_STRINGIZE
  - BOOST_PP_STRINGIZE
  - NS_SWIFT_NAME
  - CF_SWIFT_NAME
...

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorclang-formatgood first issuehttps://github.com/llvm/llvm-project/contribute

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions