简体   繁体   中英

clang-format - Format long macro

I'm here because honestly, I can't figure out this one.
My problem lies in how clang-format rearranges long preprocessor definition.

Here's what my code look like, after formating.

#define CMD_FRAME_PACKET_HEADER_SIZE                                                                                   \
    (CMD_FRAME_SOF_SIZE + CMD_FRAME_MSG_ID_SIZE + CMD_FRAME_CMD_CODE_SIZE + CMD_FRAME_CMD_STATE_SIZE +                 \
     CMD_FRAME_DATA_LENGTH_SIZE)
#define CMD_FRAME_PACKET_FOOTER_SIZE      (CMD_FRAME_CRC_SIZE + CMD_FRAME_EOF_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE (2 * CMD_FRAME_DATA_FIELDS_MAX_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE (0)
#define CMD_FRAME_PACKET_MAX_SIZE                                                                                      \
    (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE + CMD_FRAME_PACKET_FOOTER_SIZE)
#define CMD_FRAME_PACKET_MIN_SIZE                                                                                      \
    (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE + CMD_FRAME_PACKET_FOOTER_SIZE)

Here's what I like to see:

#define CMD_FRAME_PACKET_HEADER_SIZE      (CMD_FRAME_SOF_SIZE + CMD_FRAME_MSG_ID_SIZE + CMD_FRAME_CMD_CODE_SIZE \
                                          + CMD_FRAME_CMD_STATE_SIZE + CMD_FRAME_DATA_LENGTH_SIZE)
#define CMD_FRAME_PACKET_FOOTER_SIZE      (CMD_FRAME_CRC_SIZE + CMD_FRAME_EOF_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE (2 * CMD_FRAME_DATA_FIELDS_MAX_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE (0)
#define CMD_FRAME_PACKET_MAX_SIZE         (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE \
                                          + CMD_FRAME_PACKET_FOOTER_SIZE)
#define CMD_FRAME_PACKET_MIN_SIZE         (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE \
                                          + CMD_FRAME_PACKET_FOOTER_SIZE)

For those interested, here is my .clang-format

---
#
# Global style
BasedOnStyle: Microsoft
#
# Basic
IndentWidth: 4
TabWidth: 4
ColumnLimit: 120
UseTab: Never
#
# Function and macro
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
BinPackParameters: true
AlignConsecutiveMacros: Consecutive
AllowAllArgumentsOnNextLine: false
#
# if and switch case
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AlignTrailingComments: true
IndentCaseLabels: true
BraceWrapping:
  AfterCaseLabel: true
#
# Other
SpaceInEmptyBlock: true
AlignConsecutiveAssignments: Consecutive
SpacesInContainerLiterals: true
# AlignArrayOfStructures: Right

"what I like to see" involves the layout of one macro to depend on others. Should that "other" macros only depend on nearby macros, all macros in the .c file, all macros in the .c and .h files or what?

Consider how one long macro would impact the others

#define CMD_FRAME_PACKET_FOOTER_SIZE_AALKJSLFAKJFALFEUEUFELUFEU (0)
#define CMD_FRAME_PACKET_HEADER_SIZE                            (CMD_FRAME_SOF_SIZE \
                                                                 + CMD_FRAME_MSG_ID_SIZE \
                                                                 + CMD_FRAME_CMD_CODE_SIZE \
                                                                 + CMD_FRAME_CMD_STATE_SIZE \
                                                                 + CMD_FRAME_DATA_LENGTH_SIZE)

The desired format is higher maintenance and not effective use of valuable coder time. OTOH, your own time is yours to do as you will. Yet since much programming is paid by others, payers would not likely want to pay for this and so Clang (or other simple auto formatting) becomes more common and familiar/understandable.

I recommend to format code based on your group's coding standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM