Skip to content

clang-format

Use clang-format in VSCode

Create the configuration file <sourcePath>/.clang-format

yaml
# ref: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# ref: https://google.github.io/styleguide/cppguide.html

BasedOnStyle: Google

# clang-format version 10.0.0 does not accept `Language=C`` or `Standard=c11`, we use the C++ standard version most close to instead.
Language: Cpp
Standard: c++11

# Include sorting and grouping
IncludeBlocks: Regroup
SortIncludes: true

IncludeCategories:
  # 1. C99/C11 standard library headers
  - Regex: '^(<assert\.h>|<complex\.h>|<ctype\.h>|<errno\.h>|<fenv\.h>|<float\.h>|<inttypes\.h>|<iso646\.h>|<limits\.h>|<locale\.h>|<math\.h>|<setjmp\.h>|<signal\.h>|<stdalign\.h>|<stdarg\.h>|<stdatomic\.h>|<stdbool\.h>|<stddef\.h>|<stdint\.h>|<stdio\.h>|<stdlib\.h>|<stdnoreturn\.h>|<string\.h>|<tgmath\.h>|<threads\.h>|<time\.h>|<uchar\.h>|<wchar\.h>|<wctype\.h>)$'
    Priority: 1

  # 2. Third-party library headers in <>
  - Regex: "^<[^>]+>$"
    Priority: 2

  # 3. Project headers in quotes
  - Regex: '^".*"$'
    Priority: 3

# Formatting preferences
ColumnLimit: 160
IndentWidth: 2
TabWidth: 2
UseTab: Never

# Pointer alignment (Google style uses Left)
DerivePointerAlignment: false
PointerAlignment: Left

BreakBeforeBraces: Attach
AllowShortFunctionsOnASingleLine: Inline
SpacesInParentheses: false
SpaceBeforeParens: ControlStatements

Create or update .vscode/settings.json

json
{
  "[c]": {
    "editor.defaultFormatter": "xaver.clang-format"
  },
  "[cmake]": {
    "editor.defaultFormatter": "KylinIdeTeam.cmake-intellisence"
  },
  "C_Cpp.clang_format_path": "C:\\Program Files\\LLVM\\bin\\clang-format.exe",
  "files.associations": {
    "*.c": "c",
    "*.h": "c"
  }
}

Install VSCode extension xaver.clang-format .

Install LLVM, clang-format is a part tool come from it .

refs

Released under the CC-BY-NC-4.0