Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(source code): Refactor complete source code using clang-format #196

Merged
merged 8 commits into from
Dec 2, 2024

Conversation

Slider0007
Copy link
Owner

@Slider0007 Slider0007 commented Nov 29, 2024

  • Add automatic source code formatting rules (clang-format)
  • Refactor complete source code using these formatting rules

Goal: Consistent and uniform source code formatting without adding workload


Automatic Source Code Formatting Rules

Configuration

Pre-Condition

  • Development environment has an automatic formatter function (e.g. VSCode)
  • Formatting rule file (.clang-format) needs to be available in project root folder

VSCode development environment

  • No extention necessary (Using C++ default formatter which is able to handle clang format)
  • The formatting is applied automatically whenever pasting or saving the file by adding the following content to project specific settings.json file located in project subfolder .vscode.
    "editor.formatOnSave": false,
    "editor.formatOnPaste": false,
    "[cpp]": {
        "editor.formatOnSave": true,
        "editor.formatOnPaste": false // Optional
    }
    
  • With this settings it only applies per project and is enabled only for the language C++ (cpp, h files), but could also be configured globally.
  • Formatting exlusion: Formating of file defines.h is disabled (// clang-format off) to keep better readability (nested PPDirectives)

Formating rules (clang-format)

BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignConsecutiveMacros:
    Enabled: true
    AcrossEmptyLines: false
    AcrossComments: false
    AlignCompound: false
    AlignFunctionPointers: false
    PadOperators: true
AlignTrailingComments: true
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
BreakAfterReturnType: Automatic
BraceWrapping:
    AfterCaseLabel: false
    AfterClass: true
    AfterControlStatement: Never
    AfterEnum: false
    AfterFunction: true
    AfterNamespace: true
    AfterObjCDeclaration: false
    AfterStruct: false
    AfterUnion: false
    AfterExternBlock: false
    BeforeCatch: true
    BeforeElse: true
    BeforeLambdaBody: false
    BeforeWhile: false
    IndentBraces: false
    SplitEmptyFunction: false
    SplitEmptyRecord: false
    SplitEmptyNamespace: false
BreakBeforeBraces: Custom
ColumnLimit: 140
#IndentAccessModifiers: true    # If activated struct content is indented twice
IndentCaseLabels: true
IndentWidth: 4
InsertBraces: true
InsertNewlineAtEOF: true
MaxEmptyLinesToKeep: 2
PenaltyBreakAssignment : 60
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 50
PenaltyBreakOpenParenthesis: 60
PenaltyReturnTypeOnItsOwnLine: 200
SortIncludes: Never

Usage statistics:

Before (based on ESP32):
RAM: [= ] 14.3% (used 46972 bytes from 327680 bytes)
Flash: [========= ] 87.9% (used 1710965 bytes from 1945600 bytes)

After:
RAM: [= ] 14.3% (used 46972 bytes from 327680 bytes)
Flash: [========= ] 87.9% (used 1710949 bytes from 1945600 bytes)

@Slider0007 Slider0007 merged commit ab3ddc6 into develop Dec 2, 2024
10 checks passed
@Slider0007 Slider0007 deleted the clangformat branch December 2, 2024 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant