llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.87k stars 11.92k forks source link

Format: Inconsistency in C99 structure init #58103

Open MaJerle opened 2 years ago

MaJerle commented 2 years ago

This (original) code

static const osThreadAttr_t modem_thread_attr = {
    .name = "modem_app",
    .stack_size = 2048,
    .priority = osPriorityNormal
};
static const osThreadAttr_t sensors_thread_attr = {
    .name = "sensors",
    .stack_size = 1024,
    .priority = osPriorityNormal,
};

is formatted as

static const osThreadAttr_t modem_thread_attr = {.name = "modem_app", .stack_size = 2048, .priority = osPriorityNormal};
static const osThreadAttr_t sensors_thread_attr = {
    .name = "sensors",
    .stack_size = 1024,
    .priority = osPriorityNormal,
};

Why only first one is in-line, while second one remains as-is? Even if I swap the var order, it will always format only modem_thread_attr variable

$ clang-format --version
clang-format version 15.0.0

Format file

---
Language: Cpp
# BasedOnStyle:  LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros:
  Enabled: true
  AcrossEmptyLines: true
  AcrossComments: true
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields:
  Enabled: true
  AcrossEmptyLines: true
  AcrossComments: true
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
InsertBraces: true # Control statements must have curly brackets
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
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
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: "^ IWYU pragma:"
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ""
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IfMacros:
  - KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
  - Regex: "^<(.*)>"
    Priority: 0
  - Regex: '^"(.*)"'
    Priority: 1
  - Regex: "(.*)"
    Priority: 2
IncludeIsMainRegex: "(Test)?$"
IncludeIsMainSourceRegex: ""
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ""
MacroBlockEnd: ""
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: false
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 1
SortIncludes: false
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
  AfterControlStatements: true
  AfterForeachMacros: true
  AfterFunctionDefinitionName: false
  AfterFunctionDeclarationName: false
  AfterIfMacros: true
  AfterOverloadedOperator: false
  BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
  Minimum: 1
  Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementAttributeLikeMacros:
  - Q_EMIT
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
  - STRINGIZE
  - PP_STRINGIZE
  - BOOST_PP_STRINGIZE
  - NS_SWIFT_NAME
  - CF_SWIFT_NAME
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
---
llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-format

MaJerle commented 2 years ago

Furthermore, there seems no consistency between formatting of different structures. Looks like random:

Using same configuration, and same format version, I get:

static const pp_can_cfg_t can_cfg_default = {.clk_presc = 4, /* Decrease input clock to 20 MHz */
                                             .br_nominal =
                                                 {                /* Configure br nominal to 125 kHz */
                                                  .clk_presc = 8, /* Set nominal tq time to 2.5 MHz */
                                                  .swj = 1,
                                                  .ts1 = 16,
                                                  .ts2 = 3},
                                             .br_data =
                                                 {                /* Configure br data to 1 MHz */
                                                  .clk_presc = 1, /* Set nominal tq time to 20 MHz */
                                                  .swj = 1,
                                                  .ts1 = 16,
                                                  .ts2 = 3},
                                             .operation_mode = PP_CAN_OP_MODE_LOOPBACK_INTERNAL,
                                             .flags = {
                                                 .fd = 1,
                                                 .brs = 1,
                                                 .auto_retransmission = 1,
                                                 .protocol_exception = 0,
                                                 .transmit_pause = 0,
                                                 .enable_termination = 0,
                                                 .start = 1,
                                             }};

static const lwbg95_conn_connect_t connection_info_ssl = {
    .type = LWBG95_CONN_TYPE_SSL,
    .type_data = {.ssl = {
                      .cipher_suite = LWBG95_SSL_CIPHER_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                      .ca_cert_path = TLS_CERTIFICATE_KEY_NAME,
                      .client_key_path = TLS_PRIVATE_KEY_NAME,
                      .negotiate_time = 300,
                      .sslversion = LWBG95_SSL_VERSION_ALL,
                      .sec_level = 0,
                      .ignore_ltime = 0,
                  }}};

See part br_nominal =. Opening curly bracket is not in the same line as this is the case in the .ssl = { in the second structure.

MaJerle commented 2 years ago

Is there an option to get JSON-like structure?

Like that

/* Very default FDCAN configuration - used when invalid parameter is used or during startup */
static const pp_can_cfg_t can_cfg_default = {
    .clk_presc = 4, /* Decrease input clock to 20 MHz */
    .br_nominal = {                /* Configure br nominal to 125 kHz */
        .clk_presc = 8, /* Set nominal tq time to 2.5 MHz */
        .swj = 1,
        .ts1 = 16,
        .ts2 = 3},
    .br_data = {                /* Configure br data to 1 MHz */
        .clk_presc = 1, /* Set nominal tq time to 20 MHz */
        .swj = 1,
        .ts1 = 16,
        .ts2 = 3},
    .operation_mode = PP_CAN_OP_MODE_LOOPBACK_INTERNAL,
    .flags = {
        .fd = 1,
        .brs = 1,
        .auto_retransmission = 1,
        .protocol_exception = 0,
        .transmit_pause = 0,
        .enable_termination = 0,
        .start = 1,
    }
};
gedare commented 1 year ago

Original problem doesn't seem to exist in 16.0.0.

Second problem/question is related to array of structures initialization, and the number of nesting levels you have in the structures.

MaJerle commented 1 year ago

Second problem/question is related to array of structures initialization, and the number of nesting levels you have in the structures.

Is there a way to fix it?

gedare commented 1 year ago

I'm not certain. The Array of Structures support is complex and brittle.