@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +11 -26
- package/README.md +13 -28
- package/cordis.patch.yml +1 -1
- package/lib/client.js +33 -21
- package/lib/comment-policy.js +15 -6
- package/lib/index.js +1 -1
- package/lib/workspace.js +0 -15
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +34 -21
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +28 -0
- package/skills/building-cpp/project/vscode/02-settings.md +39 -0
- package/skills/building-cpp/references/build-script.md +14 -7
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/steps/01-build.md +8 -8
- package/skills/building-cpp/steps/02-tooling.md +17 -9
- package/skills/building-cpp/steps/03-check.md +16 -14
- package/skills/developing-cpp/SKILL.md +37 -25
- package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
- package/skills/developing-cpp/references/comments/comment-style.md +80 -0
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
- package/skills/developing-cpp/references/comments/examples/declaration-file.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +14 -0
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +16 -0
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/examples/behavior-sentence.md +14 -0
- package/skills/developing-cpp/references/design/examples/coordinate-table.md +31 -0
- package/skills/developing-cpp/references/design/examples/header-place.md +26 -0
- package/skills/developing-cpp/references/design/examples/library-boundary.md +30 -0
- package/skills/developing-cpp/references/design/examples/library-split.md +7 -0
- package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
- package/skills/developing-cpp/references/design/examples/preview-read.md +40 -0
- package/skills/developing-cpp/references/{project-design.md → design/project-design.md} +57 -5
- package/skills/developing-cpp/references/{change-model.md → editing/01-edit-operations.md} +5 -3
- package/skills/developing-cpp/references/{priority.md → editing/02-rule-priority.md} +2 -2
- package/skills/developing-cpp/references/exceptions/examples/throw-boundary.md +28 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +38 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-catch.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-destructor.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-format.md +13 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-message.md +16 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-when.md +27 -0
- package/skills/developing-cpp/references/{exceptions.md → exceptions/throw-rules.md} +78 -23
- package/skills/developing-cpp/references/{glossary.md → language/glossary/language-terms.md} +2 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +12 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/10-checklist.md +10 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +13 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +14 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +28 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +17 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +8 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +29 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +18 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +26 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
- package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/10-checklist.md +9 -0
- package/skills/developing-cpp/references/language/value-types/01-width.md +17 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +26 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +17 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +22 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +15 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +15 -0
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +13 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +8 -0
- package/skills/developing-cpp/references/{layout.md → layout/01-source-layout.md} +10 -4
- package/skills/developing-cpp/references/{file-template.md → layout/02-file-template.md} +10 -4
- package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +17 -0
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/references/naming/examples/naming-current-prefix.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-lookup.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +39 -0
- package/skills/developing-cpp/references/naming/examples/naming-multiple-return.md +20 -0
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-rename.md +5 -0
- package/skills/developing-cpp/references/naming/examples/naming-responsibility.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-role.md +21 -0
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/references/{naming.md → naming/identifier-names.md} +37 -9
- package/skills/developing-cpp/references/{google-cpp-style.md → style/google-cpp-style.md} +11 -10
- package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +10 -10
- package/skills/developing-cpp/steps/03-declaration.md +16 -14
- package/skills/developing-cpp/steps/04-implementation.md +21 -17
- package/skills/engineering/SKILL.md +87 -0
- package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/CompileSettings.cmake +2 -0
- package/skills/{building-cpp/references/linking.md → engineering/references/cmake/linking/01-libraries.md} +7 -48
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +20 -0
- package/skills/engineering/references/cmake/linking/04-inspect.md +21 -0
- package/skills/{configuring-cmake/references/modules.md → engineering/references/cmake/modules/functions.md} +8 -5
- package/skills/{configuring-cmake/references → engineering/references/cmake/standard}/cxx-standard.md +5 -4
- package/skills/engineering/references/cmake/standard/examples/standard.md +19 -0
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md +9 -9
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/README.md +1 -1
- package/skills/engineering/references/cmake/targets/examples/glob.md +22 -0
- package/skills/engineering/references/cmake/targets/examples/rpath.md +24 -0
- package/skills/{configuring-cmake/examples/cmake.md → engineering/references/cmake/targets/examples/target.md} +2 -70
- package/skills/{configuring-cmake/references/cmake.md → engineering/references/cmake/targets/rules.md} +18 -11
- package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md} +11 -4
- package/skills/{building-cpp/references/encoding.md → engineering/references/formatting/01-encoding.md} +1 -1
- package/skills/engineering/references/formatting/02-clang-format.md +20 -0
- package/skills/engineering/references/formatting/03-clang-tidy.md +58 -0
- package/skills/engineering/references/formatting/04-clangd.md +90 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/references/formatting/examples/clang-tidy.md +43 -0
- package/skills/{building-cpp/examples/tooling.md → engineering/references/formatting/examples/clangd.md} +1 -80
- package/skills/{configuring-cmake → engineering}/steps/01-targets.md +6 -6
- package/skills/{configuring-cmake → engineering}/steps/02-modules.md +4 -4
- package/skills/{configuring-cmake → engineering}/steps/03-check.md +3 -2
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/skills/building-cpp/references/static-analysis.md +0 -148
- package/skills/configuring-cmake/SKILL.md +0 -68
- package/skills/developing-cpp/examples/comments-declaration.md +0 -313
- package/skills/developing-cpp/examples/comments-implementation.md +0 -66
- package/skills/developing-cpp/examples/exceptions.md +0 -173
- package/skills/developing-cpp/examples/naming.md +0 -168
- package/skills/developing-cpp/examples/structure.md +0 -194
- package/skills/developing-cpp/references/comment-terms.md +0 -75
- package/skills/developing-cpp/references/comments.md +0 -223
- package/skills/developing-cpp/references/declaration-order.md +0 -118
- package/skills/developing-cpp/references/language.md +0 -123
- package/skills/developing-cpp/references/performance.md +0 -83
- package/skills/developing-cpp/references/standard-library.md +0 -138
- package/skills/developing-cpp/references/types.md +0 -154
- /package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/LinkSettings.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp → engineering/references/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# CMake
|
|
2
2
|
|
|
3
|
-
目标:一个编译器、Ninja、target-based 规则、可用的编译数据库。下面是最小形状和常见边界;工程模板仍由 `building-cpp` 提供,在
|
|
3
|
+
目标:一个编译器、Ninja、target-based 规则、可用的编译数据库。下面是最小形状和常见边界;工程模板仍由 `building-cpp` 提供,在 `../../../../building-cpp/assets/templates/projects/`,可复用的 CMake 函数在本技能的 `assets/`:`CompileSettings.cmake` 收标准与警告,`LinkSettings.cmake` 收构建期 RPATH 与 GoogleTest 依赖。
|
|
4
4
|
|
|
5
5
|
## 排版
|
|
6
6
|
|
|
@@ -21,7 +21,7 @@ project({{PROJECT_NAME}} VERSION 0.1.0 LANGUAGES CXX)
|
|
|
21
21
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
22
22
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
23
23
|
|
|
24
|
-
# 先把 assets/
|
|
24
|
+
# 先把 assets/ 里的 CompileSettings.cmake 复制到工程的 cmake/。
|
|
25
25
|
include(CompileSettings)
|
|
26
26
|
|
|
27
27
|
add_executable(app src/main.cpp)
|
|
@@ -40,7 +40,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
|
40
40
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
41
41
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
42
42
|
|
|
43
|
-
# 先把 assets/
|
|
43
|
+
# 先把 assets/ 里的 CompileSettings.cmake 复制到工程的 cmake/。
|
|
44
44
|
include(CompileSettings)
|
|
45
45
|
|
|
46
46
|
add_subdirectory(greeting)
|
|
@@ -60,36 +60,36 @@ CompileSettings_SetWarnings(greeting PRIVATE LEVEL standard)
|
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
- 库的 `PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include` 让程序与链接它的测试写 `#include "greeting.h"` 就能找到公共头,不需要各自再写 include 路径。
|
|
63
|
-
- 标准用 `CompileSettings_SetCxxStandard`:库用 `PUBLIC`
|
|
63
|
+
- 标准用 `CompileSettings_SetCxxStandard`:库用 `PUBLIC` 传播“至少 C++NN”,程序用 `PRIVATE`;`CXX_STANDARD`/`CXX_EXTENSIONS` 不传播,每个会编译的 target 都要自己调用一次。
|
|
64
64
|
- `CXX_STANDARD_REQUIRED ON` 防止 CMake 在编译器不支持时静默 decay;`CXX_EXTENSIONS OFF` 把 `-std=gnu++NN` 收成 `-std=c++NN`。configure 后检查 `compile_commands.json` 的最后一条 `-std=`。
|
|
65
65
|
- 程序 target 固定 `app`;库 target 用组件名。
|
|
66
66
|
|
|
67
67
|
## 标准与复用
|
|
68
68
|
|
|
69
|
-
C++11–C++23 的两层声明、可见性、常见错误与验证见
|
|
69
|
+
C++11–C++23 的两层声明、可见性、常见错误与验证见 `../standard/cxx-standard.md`。通用函数 `CompileSettings_SetCxxStandard(target visibility c++NN)` 在 `../assets/CompileSettings.cmake`:
|
|
70
70
|
|
|
71
71
|
```cmake
|
|
72
72
|
CompileSettings_SetCxxStandard(app PRIVATE c++23)
|
|
73
73
|
CompileSettings_SetCxxStandard(greeting PUBLIC c++20)
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
函数名不绑定单一标准;新增档位只改函数里的支持列表。模块何时抽、文件怎么放、怎么导入与测试见
|
|
76
|
+
函数名不绑定单一标准;新增档位只改函数里的支持列表。模块何时抽、文件怎么放、怎么导入与测试见 `../modules/functions.md`。
|
|
77
77
|
|
|
78
78
|
## 警告等级
|
|
79
79
|
|
|
80
|
-
`cmake/CompileSettings.cmake` 的 `CompileSettings_SetWarnings(<target> <visibility> LEVEL basic|standard|strict)` 把常用 flag 收成三档;`visibility` 用 `PRIVATE` 或 `INTERFACE`,`PRIVATE` 不向使用方传播。警告与标准写在同一份编译设置模块里。
|
|
80
|
+
`references/cmake/assets/CompileSettings.cmake` 的 `CompileSettings_SetWarnings(<target> <visibility> LEVEL basic|standard|strict)` 把常用 flag 收成三档;`visibility` 用 `PRIVATE` 或 `INTERFACE`,`PRIVATE` 不向使用方传播。警告与标准写在同一份编译设置模块里。
|
|
81
81
|
|
|
82
82
|
- `basic`:`-Wall -Wextra`;适合刚开始接 CI 的旧代码。
|
|
83
83
|
- `standard`:`basic` 加 `-Werror`、`-Wshadow`、`-Wold-style-cast`、`-Wpedantic`、`-Wnon-virtual-dtor`、`-Woverloaded-virtual`;默认档。
|
|
84
84
|
- `strict`:`standard` 加 `-Wconversion`、`-Wsign-conversion`、`-Wcast-align`、`-Wcast-qual`、`-Wformat=2`、`-Wundef`、`-Wdouble-promotion`、`-Wimplicit-fallthrough`、`-Wnull-dereference`;适合新代码或质量闸门。
|
|
85
85
|
|
|
86
|
-
MSVC 分支用 `/W4`、`/permissive-`、`/WX
|
|
86
|
+
MSVC 分支用 `/W4`、`/permissive-`、`/WX`;其他编译器直接报错,避免静默地没有警告。警告默认走 `PRIVATE`,库不把自己的策略强加给消费者;只有纯头文件库需要把同一档警告传给使用方时才用 `INTERFACE`。
|
|
87
87
|
|
|
88
88
|
## target-based 规则
|
|
89
89
|
|
|
90
90
|
- 每个 target 自己声明 include、标准、编译选项、链接库;不要用全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS` 给所有 target 加料。
|
|
91
91
|
- `target_include_directories(<target> PUBLIC|PRIVATE <dir>)`:公共头给 `PUBLIC`,私有实现目录给 `PRIVATE`。
|
|
92
|
-
- `target_compile_features(<target> PUBLIC cxx_std_NN)
|
|
92
|
+
- `target_compile_features(<target> PUBLIC|PRIVATE cxx_std_NN)`:只传播“至少 C++NN”的最低要求;精确 `-std` 由 `CompileSettings_SetCxxStandard` 在会编译的 target 上设置,不靠这一步锁死。
|
|
93
93
|
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC`。
|
|
94
94
|
- 不要 `file(GLOB ...)`;显式列源文件,新增文件时改 CMake,构建系统才知道。`CONFIGURE_DEPENDS` 依赖构建系统每次检查目录,跨生成器行为也不完全一致,不作为工程规则。
|
|
95
95
|
- 不把 `main()` 编进库;程序只做装配和 I/O。
|
|
@@ -140,7 +140,7 @@ LinkSettings_SetBuildRpath(app)
|
|
|
140
140
|
- 函数按平台写 `BUILD_RPATH`:Linux 用 `$ORIGIN/../lib`,Apple 用 `@loader_path/../lib`。
|
|
141
141
|
- 只对需要直接运行构建产物的程序设置;安装规则另算。
|
|
142
142
|
- 不要用全局 `LD_LIBRARY_PATH` 包装所有命令;共享库的运行时路径由 target 的 RPATH 设置,不在脚本里全局改寻库路径。
|
|
143
|
-
- `$ORIGIN` 在 CMake 双引号里不是 `${...}
|
|
143
|
+
- `$ORIGIN` 在 CMake 双引号里不是 `${...}`,不会被展开,保持字面量,不需要 `\$` 转义;`\$` 只在把字符串交给 shell 时使用。
|
|
144
144
|
|
|
145
145
|
## 标准库与编译器一致
|
|
146
146
|
|
|
@@ -152,7 +152,7 @@ LinkSettings_SetBuildRpath(app)
|
|
|
152
152
|
|
|
153
153
|
## 警告与静态检查
|
|
154
154
|
|
|
155
|
-
-
|
|
155
|
+
- 警告选项统一由 `CompileSettings_SetWarnings` 按 `basic` / `standard` / `strict` 施加,不要另写一套 `-Wall -Wextra -Werror` 列表;各档的具体 flag 见上文「警告等级」。
|
|
156
156
|
- 不要把警告选项加到全局 `CMAKE_CXX_FLAGS`;每个 target 显式加,或用一个函数统一施加。
|
|
157
157
|
- `clang-tidy` 在构建后单独跑,不挂进 CMake 的编译命令;这样编译数据库稳定,诊断可复现。
|
|
158
158
|
- 需要 `clang-tidy` 集成时用 `CMAKE_CXX_CLANG_TIDY`,但本项目默认不启用,避免构建与检查耦合。
|
|
@@ -190,3 +190,10 @@ LinkSettings_FetchGoogleTest()
|
|
|
190
190
|
- 同时设 `CMAKE_CXX_COMPILER` 和 `CXX` 环境变量,互相覆盖。
|
|
191
191
|
- 在 `CMakeLists.txt` 里 `execute_process` 下载依赖或改源码。
|
|
192
192
|
- 用 `CMAKE_CXX_FLAGS` 加 `-std=c++23` 之类与声明不符的标准。
|
|
193
|
+
|
|
194
|
+
## 依据
|
|
195
|
+
|
|
196
|
+
- [CMake: `add_library`](https://cmake.org/cmake/help/latest/command/add_library.html):库 target 的类型与作用域。
|
|
197
|
+
- [CMake: `target_include_directories`](https://cmake.org/cmake/help/latest/command/target_include_directories.html):`PUBLIC` / `PRIVATE` / `INTERFACE` 的传递。
|
|
198
|
+
- [CMake: `CMAKE_EXPORT_COMPILE_COMMANDS`](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html):生成 `compile_commands.json`。
|
|
199
|
+
- [CMake: `CMAKE_CXX_STANDARD`](https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html):标准版本与 `CXX_STANDARD_REQUIRED`。
|
package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md}
RENAMED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# 测试与 CTest
|
|
2
2
|
|
|
3
|
-
测试属于构建域:测试 target 由 `
|
|
3
|
+
测试属于构建域:测试 target 由 `engineering` 写进 CMake,测试代码与 CTest 运行由本技能核对。条文依据 GoogleTest Primer、CMake 的 `add_test` / `gtest_discover_tests` 文档与 CTest 手册,访问日期 2026-10-27。
|
|
4
4
|
|
|
5
5
|
## 先决定测什么
|
|
6
6
|
|
|
7
7
|
- 一个公开入口至少一条成功用例、一条拒绝用例;拒绝路径核对返回值、异常类型与状态没有被改坏。
|
|
8
|
-
- 一条测试只核对一件事。用例名写行为,不写实现:`
|
|
8
|
+
- 一条测试只核对一件事。用例名写行为,不写实现:`PlaceRejectsOccupiedSquare`,不是 `TestPlace2`。
|
|
9
9
|
- 测试直接链接库、包含公共头,不经过 `main()`、终端或文件系统;要进程、端口、临时目录的用例归集成测试,单列标签。
|
|
10
10
|
- 不测私有成员与内部调用次数;测试锁定可观察行为,重构不该改测试。
|
|
11
11
|
|
|
@@ -41,10 +41,10 @@ gtest_discover_tests(tictactoe_test
|
|
|
41
41
|
|
|
42
42
|
| 现象 | 先看 | 转到 |
|
|
43
43
|
| --- | --- | --- |
|
|
44
|
-
| 找不到测试 target 或 `gtest` | CMake 的 `enable_testing()`、`add_subdirectory(tests)`、链接目标 | `
|
|
44
|
+
| 找不到测试 target 或 `gtest` | CMake 的 `enable_testing()`、`add_subdirectory(tests)`、链接目标 | `engineering` |
|
|
45
45
|
| 断言失败但行为符合预期 | 测试期望是否写错、是否依赖未初始化状态 | 本文件 |
|
|
46
46
|
| 断言失败且行为不对 | 被测函数的实现与契约 | `developing-cpp` |
|
|
47
|
-
| 用例数比预期少 | `gtest_discover_tests` 的发现超时、测试是否在正确 target | 本文件与
|
|
47
|
+
| 用例数比预期少 | `gtest_discover_tests` 的发现超时、测试是否在正确 target | 本文件与 `../../../steps/03-check.md` |
|
|
48
48
|
| 偶发失败 | 共享临时文件、固定端口、执行顺序依赖 | 本文件 |
|
|
49
49
|
|
|
50
50
|
## 不做什么
|
|
@@ -52,3 +52,10 @@ gtest_discover_tests(tictactoe_test
|
|
|
52
52
|
- 不为凑覆盖率写没有断言的测试;不用 `sleep` 等待异步结果。
|
|
53
53
|
- 不在测试里复制一份被测实现来对照。
|
|
54
54
|
- 不把测试输出、`Testing/` 目录或生成的可执行文件入库。
|
|
55
|
+
|
|
56
|
+
## 依据
|
|
57
|
+
|
|
58
|
+
- [CMake: `add_test`](https://cmake.org/cmake/help/latest/command/add_test.html):注册测试与参数。
|
|
59
|
+
- [CMake: `gtest_discover_tests`](https://cmake.org/cmake/help/latest/module/GoogleTest.html#command:gtest_discover_tests):构建后发现 GoogleTest 用例。
|
|
60
|
+
- [CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html):`-R`、`-L`、`--output-on-failure`、`--no-tests=error`。
|
|
61
|
+
- [GoogleTest Primer](https://google.github.io/googletest/primer.html):断言、fixture 与用例命名。
|
|
@@ -32,7 +32,7 @@ MSVC 默认按 BOM 判断源码编码。源码没有 BOM 时,它按当前用
|
|
|
32
32
|
在仓库根运行:
|
|
33
33
|
|
|
34
34
|
```bash
|
|
35
|
-
node "<skill-dir>/scripts/checking-encoding.mjs" --profile portable
|
|
35
|
+
node "<skill-dir>/references/formatting/scripts/checking-encoding.mjs" --profile portable
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
- `portable`(默认)要求所有文本文件 UTF-8 无 BOM、LF、末尾换行。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# clang-format
|
|
2
|
+
|
|
3
|
+
三件语言工具各管一段:`clang-format` 排版、`clang-tidy` 诊断、`clangd` 提供补全/跳转/诊断。它们都读同一份编译数据库,但职责不重叠。编辑器侧只保留 CMake Tools 作为配置、构建、运行和调试入口;C/C++ 扩展只做 `CMake: Debug` 的调试后端,不参与语言服务。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
clang-format --dry-run --Werror src/*.cpp include/*.h tests/*.cpp
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
- 检查用 `--dry-run --Werror`;需要自动修复时单独跑 `clang-format -i`,不要在构建脚本里悄悄改源码。
|
|
10
|
+
- 工程必须有根 `.clang-format`;缺失时先复制配置,不要回退到内置风格,回退会掩盖配置缺失。
|
|
11
|
+
- 只扫描项目源码:`src/`、`include/`、`apps/`、`tests/` 等实际存在的目录;不扫描 `build/`、第三方、生成文件。
|
|
12
|
+
- 配置文件在工程根 `.clang-format`;不要依赖用户主目录或编辑器内置配置。
|
|
13
|
+
- `BasedOnStyle: Google`、`Standard: Latest`;clang-format 21/22 不接受 `c++23`,`Latest` 按工具支持的最高标准解析。列宽、缩进与包含顺序以工程根 `.clang-format` 为准;工程显式写 `ColumnLimit` 时按工程值执行,模板资产不写 `ColumnLimit`。
|
|
14
|
+
- 格式化失败就是失败,不要用 `// clang-format off` 绕过;确需关闭时只包住无法排版的宏或表格,并写一句理由。
|
|
15
|
+
|
|
16
|
+
## 依据
|
|
17
|
+
|
|
18
|
+
- [ClangFormat: Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html):`BasedOnStyle`、`Standard`、`ColumnLimit` 等选项。
|
|
19
|
+
- [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html):`--dry-run`、`--Werror` 与 `-i` 的用法。
|
|
20
|
+
- [Google C++ Style Guide: Formatting](https://google.github.io/styleguide/cppguide.html#Formatting):Google 基线的排版约定。
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# clang-tidy 与静态检查门禁
|
|
2
|
+
|
|
3
|
+
## clang-tidy
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
clang-tidy -p build/clang \
|
|
7
|
+
--header-filter="^$(pwd -P)/(include|src|apps|tests)/" \
|
|
8
|
+
--warnings-as-errors='*' \
|
|
9
|
+
src/*.cpp apps/*.cpp tests/*.cpp
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- `-p` 指向当前编译器的构建目录,读取 `compile_commands.json`。
|
|
13
|
+
- `--header-filter` 控制哪些头文件的诊断被显示,不改变 clang-tidy 实际解析哪些头;模板实例化可能让一条根因产生多条警告,警告条数不等于缺陷条数。按根因修,不要按条数修。
|
|
14
|
+
- `--header-filter` 用项目头目录的白名单,不要用 `.*` 把系统头和第三方头一并纳入。clang-tidy 按绝对路径匹配,所以上面用 `$(pwd -P)` 前缀;上面是常见目录,工程用别的顶层目录时按实际替换。`--exclude-header-filter` 到 clang-tidy 19 才加入,16/17/18 都没有,不要依赖它。
|
|
15
|
+
- 系统头默认被排除,不要加 `--system-headers`。
|
|
16
|
+
- 只扫描项目源文件;不要把 `build/` 或第三方源码传给 clang-tidy。
|
|
17
|
+
- `--warnings-as-errors='*'` 把警告当失败;修代码,不要新增 `NOLINT`。确需抑制时写 `// NOLINT(检查名): 理由`,理由必须具体。
|
|
18
|
+
- 配置在工程根 `.clang-tidy`;默认集合是 `google-*`、`portability-avoid-pragma-once` 加 `readability-identifier-naming`(第二条去掉 `#pragma once`,与路径式守卫一致)。关闭某条时逐条写理由,不整族关闭;命名规则只配项目确实统一的项,不设 `FunctionCase`。
|
|
19
|
+
- 没有编译数据库时先回到 `building-cpp` 的定构建生成;不要用 `--` 手工拼编译参数绕过。
|
|
20
|
+
- `clang-tidy` 的诊断可能指向头文件;修头文件里的声明或实现,不要只改调用点。
|
|
21
|
+
|
|
22
|
+
## 门禁分级
|
|
23
|
+
|
|
24
|
+
`--warnings-as-errors='*'` 是默认门禁。若项目必须把一组诊断先设为观察项,按检查名逐条写进项目脚本,不按 `*` 整族降级:
|
|
25
|
+
|
|
26
|
+
| 级别 | 典型检查 | 处理 |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| 失败 | `google-*`、`readability-identifier-naming`、项目声明的致命集 | 修代码;不允许用 NOLINT 掩盖 |
|
|
29
|
+
| 失败 | 编译诊断、链接诊断、格式检查 | 直接失败 |
|
|
30
|
+
| 观察 | 迁移期暂时无法修完的个别 `google-*` 子项 | 在项目技能里列清单、写负责人和退出条件;不默认全仓关闭 |
|
|
31
|
+
|
|
32
|
+
- 致命集宁可小、可解释,也不要把所有检查一刀切。
|
|
33
|
+
- 观察项不是免死金牌:新代码不应新增观察项,已有项只能减少。
|
|
34
|
+
- 关闭某条检查时,在 `.clang-tidy` 里写一行理由,并在项目技能里记录退出条件。
|
|
35
|
+
|
|
36
|
+
## 版本与一致性
|
|
37
|
+
|
|
38
|
+
- `clang-format` / `clang-tidy` ≥ 16;clang 模式下与 `clang++`、`clangd` 同主版本。
|
|
39
|
+
- 混版本常见症状:格式化结果与 CI 不同、clangd 诊断与 tidy 不一致、选项被静默忽略。
|
|
40
|
+
- 检查器在 `--live` 时核对版本;不要用「能跑就行」绕过。
|
|
41
|
+
|
|
42
|
+
## 失败分诊
|
|
43
|
+
|
|
44
|
+
| 现象 | 可能原因 | 处理 |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| `clang-tidy` 报找不到编译数据库 | 没 configure 或路径错 | 回到 `building-cpp` 的定构建生成 `build/<compiler>/compile_commands.json` |
|
|
47
|
+
| clangd 报找不到系统头 | 编译数据库里的编译器是相对路径 | 用绝对路径重新 configure,或配 `--query-driver` |
|
|
48
|
+
| 格式化结果与 CI 不同 | 版本不同或配置不是工程根那份 | 统一版本,确认 `.clang-format` 在工程根 |
|
|
49
|
+
| tidy 报系统头 | 加了 `--system-headers` 或 header-filter 过宽 | 去掉 `--system-headers`,用项目头目录白名单 |
|
|
50
|
+
| 大量 `readability-*` | 代码风格未统一 | 回到 `developing-cpp` 的定声明 / 定实现修声明与实现,不要批量 NOLINT |
|
|
51
|
+
| clangd 与 tidy 诊断不一致 | 两者读的数据库/版本不同 | 统一 `CompilationDatabase` 与工具版本 |
|
|
52
|
+
|
|
53
|
+
## 依据
|
|
54
|
+
|
|
55
|
+
- [clang-tidy](https://clang.llvm.org/extra/clang-tidy/):命令行、`-p` 与 `--warnings-as-errors`。
|
|
56
|
+
- [clang-tidy Checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html):`google-*`、`bugprone-*` 与 `readability-identifier-naming` 的语义。
|
|
57
|
+
- [Clang-Tidy: `.clang-tidy`](https://clang.llvm.org/extra/clang-tidy/#using-clang-tidy):配置继承、`Checks` 与 `CheckOptions`。
|
|
58
|
+
- [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html):Google 检查族的规则来源。
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# clangd 与编辑器
|
|
2
|
+
|
|
3
|
+
## clangd
|
|
4
|
+
|
|
5
|
+
`.clangd`:
|
|
6
|
+
|
|
7
|
+
```yaml
|
|
8
|
+
CompileFlags:
|
|
9
|
+
CompilationDatabase: build/clang
|
|
10
|
+
Diagnostics:
|
|
11
|
+
Suppress:
|
|
12
|
+
- pp_file_not_found
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- `CompilationDatabase` 指向当前编译器目录;GCC 模式改 `build/gcc`。
|
|
16
|
+
- 不要在 `.clangd` 写 `-std`:它会覆盖 `compile_commands.json`,把 C++23 工程降成旧标准。标准只由构建声明和编译数据库决定。
|
|
17
|
+
- 静态检查规则只在 `.clang-tidy` 里维护;clangd 读取同一份配置,不要在这里复制检查列表。
|
|
18
|
+
- `Suppress` 只放确实无法避免的解析噪声(如 `pp_file_not_found`),并写理由。
|
|
19
|
+
|
|
20
|
+
`--query-driver`:
|
|
21
|
+
|
|
22
|
+
- 它是 clangd 的命令行参数,不是 `.clangd` 的 YAML 键。
|
|
23
|
+
- 作用:允许 clangd 执行编译器驱动,读取系统头路径;只对编译数据库里出现的驱动生效。
|
|
24
|
+
- 写在 `.vscode/settings.json`:
|
|
25
|
+
|
|
26
|
+
```jsonc
|
|
27
|
+
{
|
|
28
|
+
"clangd.arguments": [
|
|
29
|
+
"--query-driver=/usr/bin/clang++",
|
|
30
|
+
"--header-insertion=never",
|
|
31
|
+
"--background-index"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- 路径用绝对路径;多个驱动用逗号分隔。GCC 模式指向 `/usr/bin/g++`。
|
|
37
|
+
- 不要写 `--query-driver=*` 或宽泛的 glob;这等于允许 clangd 执行任意匹配的驱动。
|
|
38
|
+
- 如果编译数据库里的编译器是相对路径,clangd 可能拒绝执行;用绝对路径重新 configure。
|
|
39
|
+
- `--query-driver` 只影响 clangd,不影响 clang-tidy;tidy 用编译数据库里的命令。
|
|
40
|
+
|
|
41
|
+
## 编辑器
|
|
42
|
+
|
|
43
|
+
`.vscode/extensions.json` 推荐:
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"recommendations": [
|
|
48
|
+
"llvm-vs-code-extensions.vscode-clangd",
|
|
49
|
+
"ms-vscode.cmake-tools",
|
|
50
|
+
"ms-vscode.cpptools"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`.vscode/settings.json` 固定:
|
|
56
|
+
|
|
57
|
+
```jsonc
|
|
58
|
+
{
|
|
59
|
+
"clangd.arguments": [
|
|
60
|
+
"--query-driver=/usr/bin/clang++",
|
|
61
|
+
"--header-insertion=never",
|
|
62
|
+
"--background-index"
|
|
63
|
+
],
|
|
64
|
+
"cmake.buildDirectory": "${workspaceFolder}/build/clang",
|
|
65
|
+
"cmake.generator": "Ninja",
|
|
66
|
+
"cmake.buildBeforeRun": true,
|
|
67
|
+
"cmake.exportCompileCommandsFile": true,
|
|
68
|
+
"cmake.debugConfig": {
|
|
69
|
+
"type": "cppdbg",
|
|
70
|
+
"request": "launch",
|
|
71
|
+
"MIMode": "lldb",
|
|
72
|
+
"miDebuggerPath": "/usr/bin/lldb",
|
|
73
|
+
"stopAtEntry": false
|
|
74
|
+
},
|
|
75
|
+
"C_Cpp.intelliSenseEngine": "disabled"
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- 配置、构建、运行和调试只从 CMake Tools 进入;不要同时维护 `tasks.json` 或 `launch.json`,否则会出现第二套构建或启动路径。
|
|
80
|
+
- C/C++ 扩展只作为 CMake 快速调试的 `cppdbg` 后端;IntelliSense 关闭,补全、跳转、诊断和格式化只由 clangd 提供。
|
|
81
|
+
- 不推荐与 clangd 冲突的 C++ 智能提示插件;只保留一个语言服务。
|
|
82
|
+
- 不把 `compile_commands.json` 提交进仓库;它是构建产物。
|
|
83
|
+
- 切换编译器时同时改 `.clangd`、`settings.json` 和构建目录;三处不一致会让诊断指向旧代码。GCC 模式把 `MIMode` 改为 `gdb`、`miDebuggerPath` 改为 `/usr/bin/gdb`。
|
|
84
|
+
|
|
85
|
+
## 依据
|
|
86
|
+
|
|
87
|
+
- [clangd: Configuration](https://clangd.llvm.org/config):`.clangd` 的 `CompileFlags`、`Diagnostics` 与 `CompilationDatabase`。
|
|
88
|
+
- [clangd: `--query-driver`](https://clangd.llvm.org/design/compile-commands#query-driver):允许读取系统头的驱动白名单。
|
|
89
|
+
- [clangd: Editor Plugins](https://clangd.llvm.org/installation#editor-plugins):VS Code 扩展与语言服务配置。
|
|
90
|
+
- [CMake Tools for VS Code](https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/README.md):`cmake.buildDirectory`、`cmake.generator` 与调试配置。
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 正反例:clang-format
|
|
2
|
+
|
|
3
|
+
## 构建脚本里自动格式化
|
|
4
|
+
|
|
5
|
+
**差**:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
clang-format -i src/*.cpp # 构建时悄悄改源码
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**好**:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
clang-format --dry-run --Werror "${sources[@]}" # 只检查
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
需要自动修复时单独运行 `clang-format -i`,由人确认 diff。
|
|
18
|
+
|
|
19
|
+
## 配置文件堆策略头
|
|
20
|
+
|
|
21
|
+
**差**:
|
|
22
|
+
|
|
23
|
+
```yaml
|
|
24
|
+
# 本项目遵循 Google C++ Style Guide,详见 docs/...
|
|
25
|
+
# 所有检查必须通过,禁止 NOLINT,禁止 ...
|
|
26
|
+
ColumnLimit: 100
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**好**:
|
|
30
|
+
|
|
31
|
+
```yaml
|
|
32
|
+
# 全部取 Google 预设:80 列、2 空格、不用 tab、包含顺序。
|
|
33
|
+
Language: Cpp
|
|
34
|
+
BasedOnStyle: Google
|
|
35
|
+
Standard: Latest
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
配置只选预设和语言版本,不重复策略;列宽、缩进、包含顺序都由 Google 预设决定。需要新规则时先改技能文档并说明理由,不要在手写配置里堆本地约定。
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# 正反例:clang-tidy
|
|
2
|
+
|
|
3
|
+
## 整族关闭检查
|
|
4
|
+
|
|
5
|
+
**差**:
|
|
6
|
+
|
|
7
|
+
```yaml
|
|
8
|
+
Checks: >
|
|
9
|
+
-*,
|
|
10
|
+
bugprone-*
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
只留一族,其他全关,且没有理由。
|
|
14
|
+
|
|
15
|
+
**好**:
|
|
16
|
+
|
|
17
|
+
```yaml
|
|
18
|
+
Checks: >
|
|
19
|
+
-*,
|
|
20
|
+
google-*,
|
|
21
|
+
readability-identifier-naming
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
保留 Google 检查主体和项目命名规则;关闭项逐条写理由,不整族关闭。不设 `FunctionCase`,Google 允许 `count()` / `set_count()` 这类访问器。
|
|
25
|
+
|
|
26
|
+
## tidy 扫描系统头
|
|
27
|
+
|
|
28
|
+
**差**:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
clang-tidy -p build/clang --system-headers src/main.cpp
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
系统头也参与诊断,出现大量第三方告警。
|
|
35
|
+
|
|
36
|
+
**好**:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
clang-tidy -p build/clang --header-filter="^$(pwd -P)/(include|src)/" \
|
|
40
|
+
--warnings-as-errors='*' src/main.cpp
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`--header-filter` 只列项目顶层目录,避免把第三方头纳入;系统头默认排除。工程有别的顶层目录时按实际替换。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 正反例:clangd
|
|
2
2
|
|
|
3
3
|
## `query-driver` 放错位置
|
|
4
4
|
|
|
@@ -72,85 +72,6 @@ CompileFlags:
|
|
|
72
72
|
|
|
73
73
|
`.clangd` 的目录必须与 `environment.json` 的 `compiler` 一致;默认 clang 用 `build/clang`,切换 GCC 时改成 `build/gcc`,不要两套配置并存。
|
|
74
74
|
|
|
75
|
-
## 配置文件堆策略头
|
|
76
|
-
|
|
77
|
-
**差**:
|
|
78
|
-
|
|
79
|
-
```yaml
|
|
80
|
-
# 本项目遵循 Google C++ Style Guide,详见 docs/...
|
|
81
|
-
# 所有检查必须通过,禁止 NOLINT,禁止 ...
|
|
82
|
-
ColumnLimit: 100
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
**好**:
|
|
86
|
-
|
|
87
|
-
```yaml
|
|
88
|
-
# 全部取 Google 预设:80 列、2 空格、不用 tab、包含顺序。
|
|
89
|
-
Language: Cpp
|
|
90
|
-
BasedOnStyle: Google
|
|
91
|
-
Standard: Latest
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
配置只选预设和语言版本,不重复策略;列宽、缩进、包含顺序都由 Google 预设决定。需要新规则时先改技能文档并说明理由,不要在手写配置里堆本地约定。
|
|
95
|
-
|
|
96
|
-
## 整族关闭检查
|
|
97
|
-
|
|
98
|
-
**差**:
|
|
99
|
-
|
|
100
|
-
```yaml
|
|
101
|
-
Checks: >
|
|
102
|
-
-*,
|
|
103
|
-
bugprone-*
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
只留一族,其他全关,且没有理由。
|
|
107
|
-
|
|
108
|
-
**好**:
|
|
109
|
-
|
|
110
|
-
```yaml
|
|
111
|
-
Checks: >
|
|
112
|
-
-*,
|
|
113
|
-
google-*,
|
|
114
|
-
readability-identifier-naming
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
保留 Google 检查主体和项目命名规则;关闭项逐条写理由,不整族关闭。不设 `FunctionCase`,Google 允许 `count()` / `set_count()` 这类访问器。
|
|
118
|
-
|
|
119
|
-
## tidy 扫描系统头
|
|
120
|
-
|
|
121
|
-
**差**:
|
|
122
|
-
|
|
123
|
-
```bash
|
|
124
|
-
clang-tidy -p build/clang --system-headers src/main.cpp
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
系统头也参与诊断,出现大量第三方告警。
|
|
128
|
-
|
|
129
|
-
**好**:
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
clang-tidy -p build/clang --header-filter="^$(pwd -P)/(include|src)/" \
|
|
133
|
-
--warnings-as-errors='*' src/main.cpp
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
`--header-filter` 只列项目顶层目录,避免把第三方头纳入;系统头默认排除。工程有别的顶层目录时按实际替换。
|
|
137
|
-
|
|
138
|
-
## 构建脚本里自动格式化
|
|
139
|
-
|
|
140
|
-
**差**:
|
|
141
|
-
|
|
142
|
-
```bash
|
|
143
|
-
clang-format -i src/*.cpp # 构建时悄悄改源码
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
**好**:
|
|
147
|
-
|
|
148
|
-
```bash
|
|
149
|
-
clang-format --dry-run --Werror "${sources[@]}" # 只检查
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
需要自动修复时单独运行 `clang-format -i`,由人确认 diff。
|
|
153
|
-
|
|
154
75
|
## 版本混装
|
|
155
76
|
|
|
156
77
|
**差**:
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## 通用情况
|
|
8
8
|
|
|
9
|
-
先读 `environment.json` 的 `standard
|
|
9
|
+
先读 `environment.json` 的 `standard`,再把它映射成两层;调整标准前先打开 `../references/cmake/standard/standards/<standard>/README.md`,按「不能假设」「降档要改什么」逐条处理,不允许只改数字。降档还要对照 `standards/README.md` 的共用矩阵确认工具链下限。
|
|
10
10
|
|
|
11
11
|
| `standard` | `target_compile_features` | `CXX_STANDARD` | CMake 最低版本 |
|
|
12
12
|
| --- | --- | --- | --- |
|
|
@@ -23,8 +23,8 @@ target 规则:
|
|
|
23
23
|
- 用 `target_include_directories`、`target_compile_features`、`target_link_libraries`;禁止全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS`。
|
|
24
24
|
- 源文件显式列出;不写 `file(GLOB ...)`,新增文件时改 CMake。
|
|
25
25
|
- `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)`;可执行文件输出到 `build/<compiler>/bin`,库到 `build/<compiler>/lib`。
|
|
26
|
-
-
|
|
27
|
-
-
|
|
26
|
+
- 需要直接运行构建产物的共享库程序,调用 `LinkSettings_SetBuildRpath(app)`;函数在 Linux 写 `$ORIGIN/../lib`,Apple 写 `@loader_path/../lib`。
|
|
27
|
+
- 标准用工程根 `cmake/` 里的 `CompileSettings_SetCxxStandard(target visibility c++NN)`;没有这个函数时先从 `../references/cmake/assets/CompileSettings.cmake` 复制到工程根 `cmake/` 并导入,不要在每个 target 重复三属性。需要 `LinkSettings_SetBuildRpath` 或 `LinkSettings_FetchGoogleTest` 时,把同目录的 `LinkSettings.cmake` 一并复制并 `include(LinkSettings)`。
|
|
28
28
|
- 警告用同一份 `CompileSettings.cmake` 里的 `CompileSettings_SetWarnings(target visibility LEVEL basic|standard|strict)`;`visibility` 用 `PRIVATE` 或 `INTERFACE`,不要在每个 target 重复 `-Wall` 列表。
|
|
29
29
|
|
|
30
30
|
## 命令形状
|
|
@@ -36,7 +36,7 @@ cmake -S . -B build/clang -G Ninja \
|
|
|
36
36
|
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
GCC 模式把目录和编译器换成 `build/gcc`、`g++`。一个 configure
|
|
39
|
+
GCC 模式把目录和编译器换成 `build/gcc`、`g++`。一个 configure 只用一个编译器;确有 C 源时再补 `-DCMAKE_C_COMPILER=clang`(GCC 模式 `gcc`),C 与 C++ 同属一套。CMake 最低 3.20,低于目标标准所需版本时先升 `cmake_minimum_required`,不要静默降标准。
|
|
40
40
|
|
|
41
41
|
## 情况
|
|
42
42
|
|
|
@@ -46,8 +46,8 @@ GCC 模式把目录和编译器换成 `build/gcc`、`g++`。一个 configure 只
|
|
|
46
46
|
|
|
47
47
|
**库 + 程序**:库的公共 include 用 `PUBLIC` 传播;程序不直接包含库的 `src/`。静态库与动态库的 target 写法相同,动态库额外设运行时路径。
|
|
48
48
|
|
|
49
|
-
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 只链接被测库的公共接口,没有测试时不加空测试。
|
|
49
|
+
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 用 `LinkSettings_FetchGoogleTest()` 取固定版本框架,并用 `gtest_discover_tests(<target> DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit TIMEOUT 10)` 注册;不得用 `add_test(NAME all ...)` 包整个可执行文件。测试 target 只链接被测库的公共接口,没有测试时不加空测试。
|
|
50
50
|
|
|
51
51
|
**预设**:使用 preset 时名称固定 `clang` / `gcc`,与构建目录一致;没有 preset 时直接用上面的命令,不要两套并存。
|
|
52
52
|
|
|
53
|
-
下一步:target 图稳定后,进 `
|
|
53
|
+
下一步:target 图稳定后,进 `02-modules.md` 决定哪些片段抽成函数。
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## 产出
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
一个可以重复导入的工程根 `cmake/` 模块,或一份明确不抽函数的理由。模块只定义函数,目标目录不再复制同一段配置;技能里的 `references/cmake/` 只是模板源,不是工程落点。
|
|
6
6
|
|
|
7
7
|
## 什么时候抽
|
|
8
8
|
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
## 契约
|
|
28
28
|
|
|
29
29
|
- 公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` / `:keyword` 标参数;不写 `#[[.rst:` 与 `.. command::`。
|
|
30
|
-
- 完整写法见
|
|
31
|
-
- 签名、警告等级与错误消息以
|
|
30
|
+
- 完整写法见 `../references/cmake/targets/rules.md`;签名留在 `function()` 那一行。
|
|
31
|
+
- 签名、警告等级与错误消息以 `../references/cmake/assets/` 和 `../references/cmake/modules/functions.md` 为准;本步不复制第二份。
|
|
32
32
|
- 普通函数只改传入 target 的属性,不写全局变量、不读环境变量、不下载依赖;唯一例外是 `LinkSettings_FetchGoogleTest`,只在测试需要时调用。
|
|
33
33
|
- 先校验 target、可见性与标准,再调用 `target_*` 或 `set_target_properties`;失败用 `FATAL_ERROR`,消息带函数名与非法值。
|
|
34
34
|
|
|
35
35
|
## 退出
|
|
36
36
|
|
|
37
|
-
模块可被根文件导入且函数签名稳定后,进 `
|
|
37
|
+
模块可被根文件导入且函数签名稳定后,进 `03-check.md` 跑 configure、构建与数据库核对。排版、2 空格与注释形态按 `../references/cmake/targets/rules.md`。
|
|
@@ -24,16 +24,17 @@ GCC 模式把目录和编译器换成 `build/gcc`、`g++`。每次检查都重
|
|
|
24
24
|
- **模块导入**:根 `CMakeLists.txt` 只导入一次;函数名与文件名可追溯;删除旧模块后没有残留 `include()`。
|
|
25
25
|
- **排版**:`CMakeLists.txt` 与 `*.cmake` 无 tab、2 空格缩进、UTF-8 无 BOM、LF;抽查 80 列以上的行是否有必要。
|
|
26
26
|
- **产物位置**:可执行文件在 `build/<compiler>/bin`,库在 `build/<compiler>/lib`;源码目录没有新增构建产物。
|
|
27
|
+
- **产物可加载**:对 `build/<compiler>/bin/app` 跑 `file` 与 `ldd`(macOS 用 `otool -L`),不得出现 `not found`;出现缺失按 RPATH 回第 1 步。启动检查由 `building-cpp` 的入口命令负责。见 `../references/cmake/linking/04-inspect.md`。
|
|
27
28
|
|
|
28
29
|
## 失败分流
|
|
29
30
|
|
|
30
31
|
| 现象 | 回哪一步 |
|
|
31
32
|
| --- | --- |
|
|
32
33
|
| 找不到 target、链接失败、include 路径不对 | 第 1 步 |
|
|
33
|
-
| 目标标准没有生效、`-std` 与声明不一致 | 第 1 步,必要时重读
|
|
34
|
+
| 目标标准没有生效、`-std` 与声明不一致 | 第 1 步,必要时重读 `../references/cmake/standard/cxx-standard.md` |
|
|
34
35
|
| 函数参数没校验、模块重复导入、旧模块残留 | 第 2 步 |
|
|
35
36
|
| 只是 CTest 或运行失败 | 转 `building-cpp` 的通过检查 |
|
|
36
37
|
|
|
37
38
|
## 交回构建
|
|
38
39
|
|
|
39
|
-
本步的判据是 configure 与构建成功、数据库标准正确、结构规则无命中;工具链、入口脚本、`clang-tidy` 与 CTest 的完整检查见 `building-cpp` 的
|
|
40
|
+
本步的判据是 configure 与构建成功、数据库标准正确、结构规则无命中;工具链、入口脚本、`clang-tidy` 与 CTest 的完整检查见 `building-cpp` 的 `../../building-cpp/steps/03-check.md`。
|