区分 C/C++ 的各个 format style

在VS Code编辑C/C++文件时,Alt+Shift+F 快捷键会默认使用拓展C/C++进行文档格式化

查看C_Cpp.formatting@ext:ms-vscode.cpptools这一配置项,发现VS Code默认使用clang-format进行格式化

Coding style, currently supports: Visual Studio, LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU. Use file to load the style from a .clang-format file in the current or parent directory, or use file:<path>/.clang-format to reference a specific path.

查询文档Clang-Format Style Options — Clang 20.0.0git documentation得知

1. LLVM Style

  • Indentation: 2 spaces.
  • Braces: Opening braces on the same line, closing braces on their own line.
  • Line Length: 80 columns.
  • Naming: CamelCase for types, lower_case for functions and variables.
  • Spacing: Spaces around operators, after commas, and in control structures.
  • Pointer Alignment: int *ptr (space before *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Focus on readability and consistency, with a preference for simplicity.

2. Google Style

  • Indentation: 2 spaces.
  • Braces: Opening braces on the same line, closing braces on their own line.
  • Line Length: 80 columns.
  • Naming: CamelCase for types, snake_case for functions and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int* ptr (space after *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Emphasizes readability and consistency, with a focus on large-scale codebases.

3. Chromium Style

  • Indentation: 2 spaces.
  • Braces: Opening braces on the same line, closing braces on their own line.
  • Line Length: 80 columns.
  • Naming: CamelCase for types, snake_case for functions and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int* ptr (space after *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Similar to Google Style but with some additional restrictions for Chromium-specific code.

4. Mozilla Style

  • Indentation: 2 spaces.
  • Braces: Opening braces on the same line, closing braces on their own line.
  • Line Length: 80 columns.
  • Naming: PascalCase for types, camelCase for functions and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int* ptr (space after *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Focuses on readability and maintainability, with a preference for clarity.

5. WebKit Style

  • Indentation: 4 spaces.
  • Braces: Opening braces on the same line, closing braces on their own line.
  • Line Length: 100 columns.
  • Naming: PascalCase for types, camelCase for functions and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int* ptr (space after *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Prioritizes readability and consistency, with a focus on web-related code.

6. Microsoft Style

  • Indentation: 4 spaces.
  • Braces: Opening braces on their own line, closing braces on their own line (Allman style).
  • Line Length: 120 columns.
  • Naming: PascalCase for types, camelCase for functions and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int* ptr (space after *).
  • Comments: Use // for single-line comments, /* */ for multi-line.
  • Philosophy: Emphasizes clarity and maintainability, with a focus on Windows development.

7. GNU Style

  • Indentation: 2 spaces (for C), 8 spaces (for C++).
  • Braces: Opening braces on their own line, closing braces on their own line (Allman style).
  • Line Length: 79 columns.
  • Naming: snake_case for types, functions, and variables.
  • Spacing: Spaces around operators and after commas.
  • Pointer Alignment: int *ptr (space before *).
  • Comments: Use /* */ for both single-line and multi-line comments.
  • Philosophy: Focuses on simplicity and adherence to GNU standards, with a preference for readability.

Key Differences

  1. Indentation:
    • LLVM, Google, Chromium, Mozilla: 2 spaces.
    • WebKit, Microsoft: 4 spaces.
    • GNU: 2 spaces (C), 8 spaces (C++).
  2. Braces:
    • LLVM, Google, Chromium, Mozilla, WebKit: Opening braces on the same line.
    • Microsoft, GNU: Opening braces on their own line (Allman style).
  3. Line Length:
    • LLVM, Google, Chromium, Mozilla: 80 columns.
    • WebKit: 100 columns.
    • Microsoft: 120 columns.
    • GNU: 79 columns.
  4. Naming Conventions:
    • LLVM, Google, Chromium: CamelCase for types, snake_case for functions and variables.
    • Mozilla, WebKit, Microsoft: PascalCase for types, camelCase for functions and variables.
    • GNU: snake_case for everything.
  5. Pointer Alignment:
    • LLVM, GNU: int *ptr (space before *).
    • Google, Chromium, Mozilla, WebKit, Microsoft: int* ptr (space after *).
  6. Comments:
    • Most use // for single-line and /* */ for multi-line.
    • GNU prefers /* */ for all comments.

Similarities

  • All styles emphasize readability and consistency.
  • Spaces around operators and after commas are common.
  • Most use // for single-line comments and /* */ for multi-line comments.
  • Most styles align braces and indentation consistently.