llvm / llvm-project

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

The macro formatting results for structure initialization are abnormal.[clang-format] #75568

Open Gaochenchang opened 9 months ago

Gaochenchang commented 9 months ago

clang-format --version

Ubuntu clang-format version 18.0.0 (++20231204064238+4d4c30a37c75-1~exp1~20231204184349.1666)

Problem Description The macro formatting results for structure initialization are abnormal. Is there any way to get the desired result? Thank you for your help. Format content

#define TEST_CFG_DEFAULT() {                                          \
    .type = VIDEO_STREAM_WRITER,                                      \
    .transmit_mode = VIDEO_MODE,                                      \
    .std_config = {                                                   \
        .clk_cfg = CLK_DEFAULT_CONFIG(44100),                         \
        .slot_cfg = SLOT_DEFAULT_CONFIG(DATA_BIT_WIDTH_16BIT),        \
        .gpio_cfg = {                                                 \
            .invert_flags = {                                         \
                .bclk_inv = false,                                    \
                .ws_inv = false,                                      \
            },                                                        \
        },                                                            \
    },                                                                \
    .chan_cfg = CHAN_TEST_DEFAULT_CONFIG(NUM_0, ROLE_MASTER),         \
    .use_alc = false,                                                 \
    .volume = 0,                                                      \
}

Desired formatting result

#define TEST_CFG_DEFAULT() {                                                     \
    .type          = VIDEO_STREAM_WRITER,                                        \
    .transmit_mode = VIDEO_MODE,                                                 \
    .std_config    = {                                                           \
            .clk_cfg  = CLK_DEFAULT_CONFIG(44100),                               \
            .slot_cfg = SLOT_DEFAULT_CONFIG(DATA_BIT_WIDTH_16BIT),               \
            .gpio_cfg = {                                                        \
                .invert_flags = {                                                \
                    .bclk_inv = false,                                           \
                    .ws_inv   = false,                                           \
            },                                                                   \
        },                                                                       \
    },                                                                           \
    .chan_cfg = CHAN_TEST_DEFAULT_CONFIG(NUM_0, ROLE_MASTER),                    \
    .use_alc  = false,                                                           \
    .volume   = 0,                                                               \
}

Actual formatting results

#define TEST_CFG_DEFAULT()                                                                                                       \
    {                                                                                                                            \
        .type          = VIDEO_STREAM_WRITER,                                                                                    \
        .transmit_mode = VIDEO_MODE,                                                                                             \
        .std_config    = {                                                                                                       \
               .clk_cfg  = CLK_DEFAULT_CONFIG(44100),                                                                            \
               .slot_cfg = SLOT_DEFAULT_CONFIG(DATA_BIT_WIDTH_16BIT),                                                            \
               .gpio_cfg = {                                                                                                     \
                   .invert_flags = {                                                                                             \
                       .bclk_inv = false,                                                                                        \
                       .ws_inv   = false,                                                                                        \
                },                                                                                                            \
            },                                                                                                                \
        },                                                                                                                       \
        .chan_cfg = CHAN_TEST_DEFAULT_CONFIG(NUM_0, ROLE_MASTER), .use_alc = false, .volume = 0, \
    }

.clang-format file content

BasedOnStyle: WebKit
Language: Cpp
IndentWidth: 4
MaxEmptyLinesToKeep: 1
IncludeCategories:
  - Regex: '^<.*\.h>'
    Priority: 1
  - Regex: '^"freertos.*\.h"'
    Priority: 2
  - Regex: '^"esp_.*\.h"'
    Priority: 3
  - Regex: '^"audio_.*\.h"'
    Priority: 4
SortIncludes: false
InsertBraces: true
PointerAlignment: Right
IndentCaseLabels: true
BraceWrapping:
  AfterFunction: true
  AfterStruct: false
BreakBeforeBraces: Custom
AlignConsecutiveMacros:
  Enabled: true
  AcrossComments: true
  AcrossEmptyLines: false
AlignConsecutiveAssignments:
  Enabled: true
  AcrossComments: true
  AcrossEmptyLines: false
  AlignCompound: false
  PadOperators: true
AlignConsecutiveDeclarations:
  Enabled: true
  AcrossComments: true
  AcrossEmptyLines: false
  AlignCompound: false
  PadOperators: true
AlignTrailingComments:
  Kind: Always
  OverEmptyLines:  2
AlignAfterOpenBracket: true
IndentPPDirectives: BeforeHash
Gaochenchang commented 8 months ago

Have some suggestions? Or we can discuss it together

Gaochenchang commented 6 months ago

Can anyone help?I will be very grateful.

Gaochenchang commented 5 months ago

I have encountered some small problems, but I haven't found a solution yet.

  1. Is there a configuration for customizing types that can be replaced with macros? image

  2. How can I remove the space between * and function name? Please note that formatting can be done correctly without using the attribute modifier. image

  3. There is an extra space before the} at the end, indicating that clang format-16 does not support this formatting style. image

  4. There are extra spaces near the ", but no relevant configuration options were found. image

  5. Is there an option to leave two spaces before the end of line comment/? I believe using//is possible, but why can't/ meet the requirements? image

There is also a great feature. If the configuration can distinguish between function definitions, structure definitions, enumeration definitions, and macro definitions, it would be great because one set of configurations cannot be fully applicable to the entire code. Currently, I have to use two sets of configurations to format a file.

BasedOnStyle: WebKit
Language: Cpp
IndentWidth: 4
MaxEmptyLinesToKeep: 1
IncludeCategories:
  - Regex: '^<.*\.h>'
    Priority: 1
  - Regex: '^"freertos.*\.h"'
    Priority: 2
  - Regex: '^"esp_.*\.h"'
    Priority: 3
  - Regex: '^"audio_.*\.h"'
    Priority: 4
SortIncludes: false
InsertBraces: true
PointerAlignment: Right
IndentCaseLabels: true
BraceWrapping:
  AfterFunction: true
  AfterStruct: false
BreakBeforeBraces: Custom
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: true
AlignTrailingComments:
  Kind: Always
  OverEmptyLines:  2
AlignAfterOpenBracket: true
IndentPPDirectives: None
AlignConsecutiveBitFields:
    Enabled: true
    AcrossEmptyLines: true
    AcrossComments: false
SpaceAfterCStyleCast: false
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
Cpp11BracedListStyle: true
SpacesInContainerLiterals: false
SpacesBeforeTrailingComments: 2
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
    AfterControlStatements: true
    AfterForeachMacros: true
    AfterFunctionDeclarationName: false
    AfterFunctionDefinitionName: false
    AfterIfMacros: true
    AfterOverloadedOperator: false
    AfterRequiresInClause: false
    AfterRequiresInExpression: true
    BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: After
Gaochenchang commented 5 months ago

If these questions need to be asked elsewhere, please let me know.

Gaochenchang commented 5 months ago

@hnakamura5 Hello~, sorry to bother you, or do you know who can answer some questions.

Gaochenchang commented 5 months ago

@llvmbot Hi!

hnakamura5 commented 5 months ago

It seems the paren is required around the initializer. I took a glance at the annotation routine in clang-format, I resulted that it cannot recognize the designated initializer outside the context of expression.

#define TEST_CFG_DEFAULT()                                            \
    ({                                                                \
        .type          = VIDEO_STREAM_WRITER,                         \
        .transmit_mode = VIDEO_MODE,                                  \
        .std_config    = {                                            \
               .clk_cfg  = CLK_DEFAULT_CONFIG(44100),                 \
               .slot_cfg = SLOT_DEFAULT_CONFIG(DATA_BIT_WIDTH_16BIT), \
               .gpio_cfg = {                                          \
                   .invert_flags = {                                  \
                       .bclk_inv = false,                             \
                       .ws_inv   = false,                             \
                },                                                 \
            },                                                     \
        },                                                         \
        .chan_cfg = CHAN_TEST_DEFAULT_CONFIG(NUM_0, ROLE_MASTER),     \
        .use_alc  = false,                                            \
        .volume   = 0,                                                \
    })

Still have a problem that \ at the end of line is strangely aligned. Disabling AlignConsecutiveAssignments solves this problem. Or setting other base rules (e.g.LLVM, Google) also solves this. But you may not want to take either of these solutions.

These indicate it is possibly a issue of using Webkit style and AlignConsecutiveAssignments at the same time.