2024-12-16 V1 31826

IDE #

Visual Studio Code

插件 #

  • C/C++ IntelliSense, debugging, and code browsing.
  • C/C++ Extension Pack Popular extensions for C++ development in Visual Studio Code.

Formatter & Linter #

  • .clang-format :只格式化代码,不去理解代码意图。

  • .clang-tidy :不只优化格式,还能识别错误实践。

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. clang-tidy is modular and provides a convenient interface for writing new checks.

.clang-format #

来源:Android Studio 导出(Editor -> Code Style -> C/C++ -> … -> Export -> clang-format File)。

插件配置:

设置名称 类型 说明
C_Cpp.clang_format_style String file 寻找项目根目录的 .clang-format 文件,文件不存在时,则使用 C_Cpp.clang_format_fallbackStyle 中的默认配置。
C_Cpp.clang_format_fallbackStyle String LLVM 找不到 .clang-format 文件时的缺省配置。

运行:

  • 使用快捷键:Shift + Alt + F
  • 使用命令: Ctrl + Shift + P 搜索并运行 Format Document

格式:key: value,如 IndentWidth: 4 代表四个空格缩进。

.clang-tidy #

来源:Google cloud 项目。

插件配置:

设置名称 类型 说明
C_Cpp.codeAnalysis.clangTidy.enabled Boolean True 是否开启 clangTidy。
C_Cpp.codeAnalysis.runAutomatically Boolean True 是否自动执行代码分析,勾选后保存和打开文件时会自动分析文件。

运行:

  • 自动运行。
  • 使用命令: Ctrl + Shift + P 搜索并运行 Run Code Analysis on Active File

格式:

# 控制开关,`-` 代表不检查
Checks:
  -*,
  readability-identifier-naming

# key-value pair,配置具体的 Options
CheckOptions:
  - { key: readability-identifier-naming.VariableCase,          value: lower_case }

readability-identifier-naming 可以设置命名规范。

如果 Quick fix 不提示修复方法(v1.22.11),升级插件到 C/C++ v1.23.2 (pre-release)

找不到头文件时也报告错误的方法: Extentions -> C/C++ -> IntelliScene -> Error Squiggles: enabled

编辑器竖线 #

// settings.json
"editor.rulers": [
    {
        "column": 80,
        "color": "#14c50e"
      },
      100,  // a ruler with the default or editorRuler.foreground color at column 100
      {
        "column": 120,
        "color": "#ff0000"
      },
]

CMakePresets.json 编译器的配法

资源 #