@chengzhao-dev/dsh-cpp-workflow 0.7.11 → 0.7.13
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 +4 -4
- package/README.md +4 -4
- package/lib/client.js +12 -11
- package/lib/comment-policy.js +12 -11
- package/package.json +1 -1
- package/skills/building-cpp/references/02-environment.md +2 -2
- package/skills/building-cpp/steps/04-vscode.md +1 -1
- package/skills/developing-cpp/SKILL.md +2 -2
- package/skills/developing-cpp/references/comments/01-comment-rules.md +42 -41
- package/skills/developing-cpp/references/comments/02-style.md +38 -16
- package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +55 -7
- package/skills/developing-cpp/references/comments/examples/02-declaration-constructor.md +12 -3
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +9 -5
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +26 -20
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +28 -11
- package/skills/developing-cpp/references/comments/examples/06-declaration-member.md +11 -4
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +16 -7
- package/skills/developing-cpp/references/comments/examples/08-declaration-struct.md +1 -1
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +21 -3
- package/skills/developing-cpp/references/comments/examples/11-declaration-constant.md +5 -5
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +26 -22
- package/skills/developing-cpp/references/design/01-project-design.md +3 -3
- package/skills/developing-cpp/references/design/examples/02-coordinate-table.md +3 -3
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +3 -3
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +11 -11
- package/skills/developing-cpp/references/exceptions/examples/02-throw-build-mode.md +1 -1
- package/skills/developing-cpp/references/index.md +16 -16
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +2 -2
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +3 -3
- package/skills/developing-cpp/references/language/language-basics/04-const.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +21 -12
- package/skills/developing-cpp/references/language/language-basics/10-language-checklist.md +4 -3
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +15 -7
- package/skills/developing-cpp/references/language/standard-library/06-format.md +5 -5
- package/skills/developing-cpp/references/language/standard-library/07-format-ecosystem.md +2 -2
- package/skills/developing-cpp/references/language/standard-library/11-library-checklist.md +1 -1
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +8 -8
- package/skills/developing-cpp/references/layout/02-file-template.md +4 -6
- package/skills/developing-cpp/references/layout/03-declaration-order.md +11 -7
- package/skills/developing-cpp/references/naming/01-identifier-names.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/04-square.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +3 -3
- package/skills/developing-cpp/scripts/checking-comments.mjs +71 -11
- package/skills/developing-cpp/steps/03-declaration.md +2 -2
- package/skills/developing-cpp/steps/04-implementation.md +1 -1
- package/skills/engineering/references/cmake/standards/index.md +1 -1
- package/skills/engineering/references/formatting/04-clangd.md +1 -1
- package/templates/cpp-workflow-project/SKILL.md +3 -11
package/README.en.md
CHANGED
|
@@ -19,7 +19,7 @@ The exception material follows the C++ Core Guidelines' Error handling section a
|
|
|
19
19
|
Fixed-version npm install:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.
|
|
22
|
+
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.13
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
This page follows each release; run `npm view @chengzhao-dev/dsh-cpp-workflow version` if you need to confirm the current version on the registry.
|
|
@@ -27,7 +27,7 @@ This page follows each release; run `npm view @chengzhao-dev/dsh-cpp-workflow ve
|
|
|
27
27
|
GitHub tag installs use the tag with the same version number:
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
|
-
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.
|
|
30
|
+
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.13
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
Replace `--profile` with the profile you actually run. The desktop app always runs the `desktop` profile, and that profile is owned by the app; if the command-line `add` is rejected, add the plugin in the app settings and use the same pinned npm version. A Git tag install uses the prebuilt `lib/` committed in the repository and runs no build on the target machine; the package has no install script that needs approval.
|
|
@@ -53,7 +53,7 @@ Copy [templates/cpp-workflow-project](templates/cpp-workflow-project) into the t
|
|
|
53
53
|
- project and component names, source root;
|
|
54
54
|
- library/program boundary and public header location;
|
|
55
55
|
- an entry command that runs from the repository root;
|
|
56
|
-
- stage-table path
|
|
56
|
+
- stage-table path.
|
|
57
57
|
|
|
58
58
|
Keep one `environment.json` per repository. Its first three environment axes are intentionally empty in the template and must be filled with the project's real combination after copying; `standard` defaults to `c++23` and may be changed to `c++11`–`c++23`. While the axes are empty, the four source steps still work, but build setup stops. The stage table records observable behavior, not internal refactors.
|
|
59
59
|
|
|
@@ -68,7 +68,7 @@ Keep one `environment.json` per repository. Its first three environment axes are
|
|
|
68
68
|
|
|
69
69
|
The standard lives in `environment.json` as `standard` and must match the CMake target and compile database; on the CMake side, prefer `engineering`'s `set_target_cxx_standard` function. New projects default to C++23; for C++11–C++20, lower `standard`, `cxx_std_*`, `CXX_STANDARD`, and the `.clang-format` parsing level together, not one at a time. Per-standard facilities, tooling, and downgrade tables are in the [standard directory](skills/engineering/references/cmake/standards/index.md); the CMake two-layer declaration is in [01-cxx-standard.md](skills/engineering/references/cmake/standards/01-cxx-standard.md).
|
|
70
70
|
|
|
71
|
-
The generator is always Ninja. Build directories default to `build/clang/` and `build/gcc/` and are never shared; a project skill may declare a single build directory
|
|
71
|
+
The generator is always Ninja. Build directories default to `build/clang/` and `build/gcc/` and are never shared; a project skill may declare a single build directory. Analysis tools are always `clang-format`, `clang-tidy`, and `clangd`; GCC mode does not switch to another analyzer.
|
|
72
72
|
|
|
73
73
|
Run the declaration check first, then the live check on the build host. `<skill-dir>` is the installed `building-cpp` directory, not a directory in the target project:
|
|
74
74
|
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
npm 安装固定版本:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.
|
|
22
|
+
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.13
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
本页版本号随发布更新;安装前可用 `npm view @chengzhao-dev/dsh-cpp-workflow version` 确认 registry 上的最新版本。
|
|
@@ -27,7 +27,7 @@ dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.11
|
|
|
27
27
|
GitHub 标签安装使用同一版本号的标签,地址中的版本与标签同号:
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
|
-
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.
|
|
30
|
+
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.13
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
`--profile` 换成你实际运行的那个 profile。桌面应用固定跑 `desktop` profile,且该 profile 由应用独占管理;命令行 `add` 被拒绝时,在应用设置里添加插件,地址填同一个 npm 版本。Git 标签安装使用仓库内已构建的 `lib/`,不在目标机器上重新构建;包内也不含需要授权执行的安装脚本。
|
|
@@ -53,7 +53,7 @@ dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.11
|
|
|
53
53
|
- 工程名、组件名和源码根;
|
|
54
54
|
- 库与程序的边界、公共头位置;
|
|
55
55
|
- 从仓库根能直接运行的入口命令;
|
|
56
|
-
-
|
|
56
|
+
- 阶段表路径。
|
|
57
57
|
|
|
58
58
|
`environment.json` 一个仓库只有一份。模板里的前三个环境轴故意留空,`standard` 预置为 `c++23`;复制后把环境轴填成工程的真实组合,需要旧标准时再改 `standard`。环境轴留空时源码四步仍能做,但定构建会停。阶段表只写可观察行为,不写内部重构。
|
|
59
59
|
|
|
@@ -68,7 +68,7 @@ dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.11
|
|
|
68
68
|
|
|
69
69
|
标准写在 `environment.json` 的 `standard`,并同步到 CMake target 和编译数据库;CMake 侧优先调用 `engineering` 提供的 `set_target_cxx_standard` 函数。新工程默认 C++23;C++11–C++20 工程把 `standard`、`cxx_std_*`、`CXX_STANDARD` 和 `.clang-format` 的解析档位一起降级,不要只改一处。每档的设施、工具和降档表见 [标准目录](skills/engineering/references/cmake/standards/index.md),CMake 两层声明见 [01-cxx-standard.md](skills/engineering/references/cmake/standards/01-cxx-standard.md)。
|
|
70
70
|
|
|
71
|
-
生成器固定 Ninja,默认构建目录是 `build/clang/` 与 `build/gcc
|
|
71
|
+
生成器固定 Ninja,默认构建目录是 `build/clang/` 与 `build/gcc/`,不共用同一目录;项目技能声明单一构建目录时以项目为准。诊断工具固定 `clang-format`、`clang-tidy`、`clangd`;GCC 模式也不换成别的分析器。
|
|
72
72
|
|
|
73
73
|
构建前先跑声明校验,再在构建宿主上跑活动校验。`<skill-dir>` 是插件安装后的 `building-cpp` 目录,不是目标工程里的目录:
|
|
74
74
|
|
package/lib/client.js
CHANGED
|
@@ -126,17 +126,21 @@ function renderFileHeader(raw, fileName) {
|
|
|
126
126
|
const config = normalizeConfig(raw);
|
|
127
127
|
const name = text(fileName) || SAMPLE_FILE;
|
|
128
128
|
const lines = [`@file ${name}`];
|
|
129
|
+
// 文件头分三段:@file、@brief 与元数据组,段间各留一行空注释。
|
|
129
130
|
if (config.brief.enabled) {
|
|
130
|
-
lines.push(config.brief.value ? `@brief ${config.brief.value}` : '@brief');
|
|
131
|
+
lines.push('', config.brief.value ? `@brief ${config.brief.value}` : '@brief');
|
|
131
132
|
}
|
|
132
|
-
|
|
133
|
+
const metadata = [];
|
|
134
|
+
for (const tag of ['author', 'version', 'date', 'copyright']) {
|
|
133
135
|
if (config[tag].enabled && config[tag].value)
|
|
134
|
-
|
|
136
|
+
metadata.push(`@${tag} ${config[tag].value}`);
|
|
135
137
|
}
|
|
138
|
+
if (metadata.length > 0)
|
|
139
|
+
lines.push('', ...metadata);
|
|
136
140
|
if (config.commentStyle === 'block') {
|
|
137
|
-
return ['/**', ...lines.map((line) =>
|
|
141
|
+
return ['/**', ...lines.map((line) => (line === '' ? separatorLine('block') : declarationLine('block', line))), ' */'];
|
|
138
142
|
}
|
|
139
|
-
return lines.map((line) =>
|
|
143
|
+
return lines.map((line) => (line === '' ? separatorLine('slash') : declarationLine('slash', line)));
|
|
140
144
|
}
|
|
141
145
|
/**
|
|
142
146
|
* 设置页的预览:文件头 + 一段函数注释,只显示当前配置会写出的标签。
|
|
@@ -184,8 +188,9 @@ function previewText(raw, sampleName) {
|
|
|
184
188
|
return lines.join('\n');
|
|
185
189
|
}
|
|
186
190
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
191
|
+
* 检查器参数:@brief 被关掉时跳过摘要检查,声明符号切到块注释时让检查器按块注释收口。
|
|
192
|
+
* 文件头里的元数据默认放行,不再生成 `--allow-tag`;该参数仍可手工用来放行
|
|
193
|
+
* 写在函数或其他声明注释里的同名标签。
|
|
189
194
|
*/
|
|
190
195
|
function checkerArgs(raw) {
|
|
191
196
|
const config = normalizeConfig(raw);
|
|
@@ -198,10 +203,6 @@ function checkerArgs(raw) {
|
|
|
198
203
|
args.push('--no-param');
|
|
199
204
|
if (!config.returns)
|
|
200
205
|
args.push('--no-return');
|
|
201
|
-
for (const tag of PRIVACY_TAGS) {
|
|
202
|
-
if (config[tag].enabled && config[tag].value)
|
|
203
|
-
args.push('--allow-tag', tag);
|
|
204
|
-
}
|
|
205
206
|
return args;
|
|
206
207
|
}
|
|
207
208
|
/**
|
package/lib/comment-policy.js
CHANGED
|
@@ -119,17 +119,21 @@ export function renderFileHeader(raw, fileName) {
|
|
|
119
119
|
const config = normalizeConfig(raw);
|
|
120
120
|
const name = text(fileName) || SAMPLE_FILE;
|
|
121
121
|
const lines = [`@file ${name}`];
|
|
122
|
+
// 文件头分三段:@file、@brief 与元数据组,段间各留一行空注释。
|
|
122
123
|
if (config.brief.enabled) {
|
|
123
|
-
lines.push(config.brief.value ? `@brief ${config.brief.value}` : '@brief');
|
|
124
|
+
lines.push('', config.brief.value ? `@brief ${config.brief.value}` : '@brief');
|
|
124
125
|
}
|
|
125
|
-
|
|
126
|
+
const metadata = [];
|
|
127
|
+
for (const tag of ['author', 'version', 'date', 'copyright']) {
|
|
126
128
|
if (config[tag].enabled && config[tag].value)
|
|
127
|
-
|
|
129
|
+
metadata.push(`@${tag} ${config[tag].value}`);
|
|
128
130
|
}
|
|
131
|
+
if (metadata.length > 0)
|
|
132
|
+
lines.push('', ...metadata);
|
|
129
133
|
if (config.commentStyle === 'block') {
|
|
130
|
-
return ['/**', ...lines.map((line) =>
|
|
134
|
+
return ['/**', ...lines.map((line) => (line === '' ? separatorLine('block') : declarationLine('block', line))), ' */'];
|
|
131
135
|
}
|
|
132
|
-
return lines.map((line) =>
|
|
136
|
+
return lines.map((line) => (line === '' ? separatorLine('slash') : declarationLine('slash', line)));
|
|
133
137
|
}
|
|
134
138
|
/**
|
|
135
139
|
* 设置页的预览:文件头 + 一段函数注释,只显示当前配置会写出的标签。
|
|
@@ -177,8 +181,9 @@ export function previewText(raw, sampleName) {
|
|
|
177
181
|
return lines.join('\n');
|
|
178
182
|
}
|
|
179
183
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
184
|
+
* 检查器参数:@brief 被关掉时跳过摘要检查,声明符号切到块注释时让检查器按块注释收口。
|
|
185
|
+
* 文件头里的元数据默认放行,不再生成 `--allow-tag`;该参数仍可手工用来放行
|
|
186
|
+
* 写在函数或其他声明注释里的同名标签。
|
|
182
187
|
*/
|
|
183
188
|
export function checkerArgs(raw) {
|
|
184
189
|
const config = normalizeConfig(raw);
|
|
@@ -191,10 +196,6 @@ export function checkerArgs(raw) {
|
|
|
191
196
|
args.push('--no-param');
|
|
192
197
|
if (!config.returns)
|
|
193
198
|
args.push('--no-return');
|
|
194
|
-
for (const tag of PRIVACY_TAGS) {
|
|
195
|
-
if (config[tag].enabled && config[tag].value)
|
|
196
|
-
args.push('--allow-tag', tag);
|
|
197
|
-
}
|
|
198
199
|
return args;
|
|
199
200
|
}
|
|
200
201
|
/**
|
package/package.json
CHANGED
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
| clang | `build/clang/` | `build/clang/compile_commands.json` |
|
|
73
73
|
| gcc | `build/gcc/` | `build/gcc/compile_commands.json` |
|
|
74
74
|
| 当前 | `build/` | `build/compile_commands.json` → 上面之一的符号链接 |
|
|
75
|
-
| 单一目录 |
|
|
75
|
+
| 单一目录 | 项目声明的目录 | `<build-dir>/compile_commands.json`,不建符号链接 |
|
|
76
76
|
|
|
77
|
-
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER`
|
|
77
|
+
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER` 后复用缓存。项目技能声明单一构建目录时以项目为准,此时不建 `build/clang` 与 `build/gcc`,编译数据库仍在 `<build-dir>/compile_commands.json`。
|
|
78
78
|
|
|
79
79
|
## 活动校验
|
|
80
80
|
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
|
|
34
34
|
键的作用与取值规则只有一份:`../../engineering/references/formatting/04-clangd.md` 的键表。本步只核对落地点:
|
|
35
35
|
|
|
36
|
-
- `cmake.buildDirectory` 与 `.clangd` 的 `CompilationDatabase`
|
|
36
|
+
- `cmake.buildDirectory` 与 `.clangd` 的 `CompilationDatabase` 必须指向同一目录;项目技能声明单一构建目录时两处都写该目录,不按编译器分子目录。
|
|
37
37
|
- 资产里 `files.exclude`、`search.exclude`、`files.encoding/eol`、`files.associations`、`editor.formatOnSave`、`[cpp]` / `[c]` 是隐藏与保存行为,按资产原样保留;只改与 `environment.json` 冲突的键。
|
|
38
38
|
|
|
39
39
|
## 不写什么
|
|
@@ -13,7 +13,7 @@ metadata:
|
|
|
13
13
|
- **负责**:类型、函数与文件的行为和契约;声明与实现;命名;异常;源码注释。
|
|
14
14
|
- **不负责**:`CMakeLists.txt` 与 `cmake/` 开 `engineering`;`running-build.sh`、`.clang-*`、`.vscode/`、`environment.json` 开 `building-cpp`,本技能只读其 `standard`;`.qmd`、仓库 README、Python 与其他文档不打开本技能。
|
|
15
15
|
- **交接**:一次改动跨域时按「源码 → engineering → building-cpp」逐段进入,前一段结论落下再开下一段。只改函数体、命名、异常或注释且入口命令可运行时,第 4 步后只开 `building-cpp` 的通过检查;新增或移动源文件、改库与程序的边界、补测试 target 时,完成前必须开 `engineering` 把文件写进 CMake。
|
|
16
|
-
- **前置**:存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md
|
|
16
|
+
- **前置**:存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md`:工程名、库与程序的边界、阶段表与入口命令。语言标准只读该项目 `environment.json` 的 `standard`,项目技能不再单独声明档位;文件缺失或为空时才用新工程默认 C++23;声明 C++20 的工程里不写 C++23 才有的设施。没有项目技能时门禁不会放出本技能,不猜项目约定。
|
|
17
17
|
- **编码**:源码与构建文件的编码以 `../engineering/references/formatting/01-encoding.md` 为准;行尾与 `.clang-format` 的 `LineEnding` 跟仓库根 `.gitattributes` 与 `.editorconfig`。
|
|
18
18
|
|
|
19
19
|
## 三种操作
|
|
@@ -89,7 +89,7 @@ metadata:
|
|
|
89
89
|
- [ ] 第 1 步的行为句能被用户或测试验收;有阶段表时只覆盖对应行,没有时只覆盖用户点名的行为。
|
|
90
90
|
- [ ] 第 2 步的库 target 不依赖终端、GUI 或脚本;程序 target 只做装配和输入输出;共享抽象有第二个真实使用方。
|
|
91
91
|
- [ ] 超出标准库的能力用维护中的三方库完成,库只出现在直接使用它的 target,规则库不依赖终端、GUI 或库类型。
|
|
92
|
-
- [ ] 第 3 步的公共头文件自包含,命名、声明顺序和异常声明符合 `references/`
|
|
92
|
+
- [ ] 第 3 步的公共头文件自包含,命名、声明顺序和异常声明符合 `references/` 的条文;类型注释按类型写:`@brief` 一句名词短语;类、结构体、格式化器默认再加一段「表示…」;纯数据结构、枚举和纯值容器最多两句;初始状态归默认构造函数。
|
|
93
93
|
- [ ] 第 4 步的实现只兑现声明;`.cpp` 定义顺序与头文件声明同序,参数类型、限定符与返回类型同声明一致;同一文件里的函数按同一套分段约定,需要分阶段的都有阶段注释与空行;资源由 RAII 管理;失败是正常结果时用返回值,函数无法完成任务时抛异常,不拿异常做正常分支。
|
|
94
94
|
- [ ] 配对头文件类的 `.cpp` 定义顺序、签名与谓词位置检查通过:`node "<skill-dir>/scripts/checking-declaration-order.mjs" --root <项目根>` 退出码为 0;有乱序或签名不一致先修,不能跳过。自由函数与没有配对头文件的 `.cpp` 不在覆盖内。
|
|
95
95
|
- [ ] 源码注释符合 `references/comments/01-comment-rules.md` 与 `02-style.md` 的规范,`node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>` 退出码为 0,末行是「注释检查通过:N 个文件。」。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:写或改注释时;读完能回答:注释:写什么;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:写或改注释时;读完能回答:注释:写什么;预算:一次只开这一份(239 行),不自动串联。 -->
|
|
2
2
|
# 注释:写什么
|
|
3
3
|
|
|
4
4
|
注释按内容、结构、版式、措辞四节组织。本文件写内容:哪些事实值得写、该落在文件头、类型、函数还是实现注释里;结构、版式与措辞见同目录的 `02-style.md`。两份文件对同一句给出不同说法时,内容层以本文件为准,符号、宽度、标签排版与措辞层以 `02-style.md` 为准;改动时只保留一处权威句,另一处改为指针。位置越靠外,越讲概念与契约;越靠里,越讲实现理由。同一件事只写一次。
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
## 内容:只写声明里看不出的事实
|
|
13
13
|
|
|
14
|
-
-
|
|
14
|
+
- **第一句先写函数做什么或正常结果**:类型第一行用 `@brief` 写类型名,或按「类型」节写成一句类型名词短语,下一段写它是什么、保存什么,可用名词短语、判断句、话题—评论句或「表示…」;普通函数、成员函数与运算符不写 `@brief`,第一段直接写动作或正常结果;构造函数与析构函数可选用 `@brief` 标签行(见「构造函数与析构函数」节);布尔查询写「主体是否状态」,也可以在句首加「查询」,写成「查询主体是否状态」;「查询」只读取状态、不产生诊断,句中有「是否」,句首不用「检查」「判断」「校验」「验证」,但 `Check`/`Validate` 这类会抛的诊断函数除外;失败返回值写 `@return`/`@retval`,异常只写 `@throws`;第一句不写抛出条件,也不写「返回 true/false」;`@return` 按「函数」节的「布尔查询」条写。条件、参数与实现理由放第二句以后。
|
|
15
15
|
- **不写否定式能力边界**:只写本函数保证什么、调用方必须满足什么。前置条件写成「调用方保证…」「仅接受…」或 `@pre`,布尔查询只写成立条件。状态结果可以说「不做修改」「保持不变」,因为它描述的是这次调用的结果,不是能力范围;否定式边界的通用规则见 `../../../shared/operations.md`,注释域落点以本条为准。`scripts/checking-comments.mjs` 会报 `negative-scope`。
|
|
16
16
|
- 公共注释回答「这是什么」和「调用方必须遵守什么」;实现注释回答「为什么这样写」和「边界在哪里」。设计取舍、方案对比与演进历史写到书稿、README 或提交说明。
|
|
17
17
|
- 用中文散文写完整句子,句号结尾;标识符、标准库类型、`@param` 这类标签保持原文。
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
- 命名、分文件、`enum class`、异常与结果类型这类全项目风格约定只写进项目文档与读者正文;注释不替项目解释风格。
|
|
20
20
|
- 状态结论必须带成立条件。「终局后空格仍在」没有说清哪种终局;写成「一方已三连而棋盘尚未占满时,空格仍然留着;平局时棋盘已满,没有空格」。删掉前提换来的短,不算润色。
|
|
21
21
|
|
|
22
|
-
###
|
|
22
|
+
### 类型:标题、职责与默认值
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
对象是类、结构体、枚举和类型别名。`@brief` 行只写类型名;下一段说明这个类型是什么、保存什么,可以写成名词短语,也可以用是字句、话题—评论句或「表示…」,不要求主谓完整;默认值或初始状态可选地再写一段,用一句短语,如「默认初始化为空方格。」「行号和列号的默认值均为 0。」。也可以不用标签,把类型名与解释写成一句名词短语,用全角冒号补说明。纯值容器在这一段补它保存什么,但不展开成成员或接口清单。纯数据结构和枚举写清这个取值是什么;枚举的摘要与取值说明见「枚举与只承载数据的结构体」一节。
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
类型注释按「标题 / 职责 / 可选默认值」组织:第二段写这个类型是什么、保存什么,句式可以是名词短语、是字句、话题—评论句或「表示…」,不强制「表示」;有不变量的类再加一段写不变量与生命周期(初始状态、状态迁移、清空条件),写在类注释,不拆到成员注释。默认值/初始状态用最后一段写,只写初始值,不写调用方后续行为;写了这一段,默认构造函数只写「默认构造函数。」,不重复。没有写在类注释时,初始状态归默认构造函数;写入与清空条件归对应函数。生命周期用概念句写,不点成员函数名;多个独立条件各自成段,用一行 `///` 隔开,不用逗号把独立条件串成一句。不加「限制:」这类标签;否定式能力边界见 `../../../shared/operations.md`。纯数据结构第二段说明它是什么、由什么组成,默认值写在第三段;字段名与类型已说清时不再逐项注释,需要补语境时才写。枚举的两行式见「枚举与只承载数据的结构体」一节。简单类可以只写类注释:只有一个数据成员、成员函数只做签名自解释的读取、写入或查询、没有自定义拷贝/移动/析构时,构造函数、成员函数、数据成员和 `public:`/`private:` 都可以不写注释,类型职责与默认状态由类注释承担;也可以保留构造函数、成员函数和数据成员注释,按后文各自规则写;复杂类仍按成员契约逐项写。
|
|
27
27
|
- **非拥有资源写进类注释**:类型持有 `std::span`、`std::string_view`、引用、裸指针或借用句柄时,类注释第二段写清资源由谁拥有,以及所有者必须保证的生命周期;成员注释只写该成员自身的事实,不重复类级生命周期。
|
|
28
28
|
|
|
29
29
|
```cpp
|
|
@@ -35,51 +35,51 @@ class Board {
|
|
|
35
35
|
|
|
36
36
|
- 不写成员清单,不按声明顺序复述接口。函数各自的契约写在函数上。连 `Place`、`Clear` 这样的成员名也不写进类注释;生命周期用「空方格才能写入」这类概念句。
|
|
37
37
|
- 不写「至多成功一次」「最多放置一次」这类次数结论:`Clear` 或清空之后状态可以再次变化,次数结论会失效;写当前操作与清空条件,例如「空方格才能放置;清空后才能再次放置」。
|
|
38
|
-
-
|
|
38
|
+
- 不写跨类分工。边界靠类型与所有权表达,注释只写本类型保证什么;用所属对象说明本类型是什么(如「棋盘上的一个方格」)不算分工,只要不写对方负责什么。
|
|
39
39
|
- 不写「进程内唯一」「全局唯一」这类没有类型证据的唯一性断言;同一类型可以构造多个对象,所有权写「本对象」「本会话」。
|
|
40
|
-
- 不写类型系统已经表达的性质(`值语义`、`所有查询都是 const
|
|
40
|
+
- 不写类型系统已经表达的性质(`值语义`、`所有查询都是 const`、`int` 成员可为负)。
|
|
41
41
|
- 不写尚未发生的用法、方案对比与取舍论证、广告句。
|
|
42
42
|
- 纯数据结构不写「只保存…,不拥有…,不含…」这类否定式边界;这些由成员类型和所有权表达。
|
|
43
|
-
-
|
|
44
|
-
-
|
|
43
|
+
- 领域尺寸只写进固定尺寸容器的类型注释:棋盘可以在职责段写「3x3」,方格等值类型不写。尺寸的权威声明仍是 `squares_` 与 `kRowCount`、`kColumnCount`;尺寸常量的注释写「棋盘的行数」「棋盘的列数」「棋盘的方格总数」,不写「方格棋盘」这类限定。
|
|
44
|
+
- 枚举与只承载数据的结构体:默认第一行是 `@brief` 类型名,下一段说明这个取值是什么;取值枚举可以写成「方格值枚举:方格的取值可以是空、X 或 O。」这类「类型名:……」的说明句,也可以先写 `@brief 方格值枚举。`,空一行后再写「方格的取值:空、X 或 O。」、「表示方格可能的取值:空、X 或 O。」或「枚举有三种取值:空、X 或 O。」;类与结构体写职责与不变量,可用「表示…」,也可用名词短语或判断句。取值枚举的短句写「方格值枚举。」,结果枚举写「一次落子的结果。」这类「<操作>的结果」;具体类型的规范句见 `glossary/01-comment-terms.md`,不要在两处各留一张表;需要时可以写成 `@brief` 行;两种写法都只写取值域,可以概括,也可以直接列出合法取值,但不逐项解释每个枚举项的含义;结果枚举不用「表示…结果。」开头,也不写成「方格的状态。」「对局的结果。」这种把所有者塞进短语的写法。行尾 `///<` 的句式见「枚举项与 `struct` 短字段:两种句式」。
|
|
45
45
|
|
|
46
46
|
### 构造函数与析构函数:先写标签,再写职责
|
|
47
47
|
|
|
48
|
-
特殊成员用标签开头:`默认构造函数:`、`拷贝构造函数:`、`移动构造函数:`、`析构函数:`;其他构造函数写
|
|
48
|
+
特殊成员用标签开头:`默认构造函数:`、`拷贝构造函数:`、`移动构造函数:`、`析构函数:`;其他构造函数写 `构造函数:`。可以一行写标签和后置条件,也可以先写 `@brief` 加标签,空一行后再写后置条件;两行写法里标签行只说明成员类别。标签后写构造完成后的后置条件,或析构函数负责什么;类注释已写默认状态时,默认构造函数不再写后置条件,需要保留成员注释时只写「默认构造函数。」,简单类可以整类不写。`= default` 配合成员初始化器建立的不变量,签名上看不见,必须写成后置条件(类注释已写默认状态时除外)。只写读者能观察到的事实,不写「创建对象」「初始化成员」这类复述;「构造空方格」「初始化为空方格」这类具体结果不算复述。
|
|
49
49
|
|
|
50
50
|
```cpp
|
|
51
51
|
/// 默认构造函数:初始化为空棋盘。
|
|
52
52
|
Board() = default;
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
默认构造与析构可以只写一行:标签后直接写后置条件或职责;也可以采用 `@brief` 标签行 + 空行 + 后置条件的写法,后置条件不能省。`= default` 的后置条件写在标签那一行就够,不再另起一段写「不抛异常」,也不为了注释加 `noexcept`。无参数、无返回值的动作只在签名看不出结果时再写一句,不把「不抛异常」当成这句;不写 `@throws` 就表示不抛。默认构造函数要写出具体的初始状态或结果,如「构造空方格」「初始化为空方格」「初始化为空棋盘」「初始化为空对局」;若类注释已写,不再写后置条件,保留成员注释时只写「默认构造函数。」,简单类可以整类不写;「建立初始状态」看不出具体值,不写。对象由多个成员组成时,写整个对象的初始状态,不逐项罗列成员。不写空的 `@param`、`@return`。有参数或有返回值的函数按「函数:两段式契约」一节写标签,构造与析构不写 `@return`。标签说明这是哪一类特殊成员,不重复类名;非默认构造同样写建立的不变量与失败方式。
|
|
56
56
|
|
|
57
57
|
### 数据成员:上一行 `///`,先写名词再写约定
|
|
58
58
|
|
|
59
|
-
类数据成员的主注释写在声明上一行,符号按 `02-style.md` 的设置,默认
|
|
59
|
+
类数据成员的主注释写在声明上一行,符号按 `02-style.md` 的设置,默认 `///`;私有成员也一样。第一句先写这个成员是什么,再在同一句里补类型和名字看不出、只属于这个成员的约定:轴向、单位、哨兵值、外部约定;所有权只在类型未表达时写。初值含义、状态迁移与清空规则若属于类级不变量,写在类注释,成员注释不重复;成员注释保持一句,名字和类型已经说清时可以不写;简单类省略成员注释时,数据成员也不写。
|
|
60
60
|
|
|
61
|
-
有补充时写成一句 `///
|
|
61
|
+
有补充时写成一句 `/// 名词:约定。`;需要补默认值、单位或哨兵值时可用逗号,如 `/// 方格值,默认为空。`;签名或成员初始化器已经说明默认值时只写名词,如 `/// 方格值。`;冒号用全角 `:`,句号结束;修饰必须依附于被修饰的名词,不能单独占一行。行尾不写 `//` 或 `///<`;行尾符号的适用范围见 `02-style.md`「结构」节,本文件不再重复。同一个类的成员注释取同一种句式:要么都只写名词,要么都用「名词:约定」,只补默认值时统一用「名词,默认为 …」;不留「有的带冒号、有的只写定语」的混排。注释正文用领域词(`X 玩家`、`O 玩家`),不写 `kX`、`kO` 这类枚举名;枚举名只出现在 `@param`、`@return`、`@retval` 与代码里。
|
|
62
62
|
|
|
63
63
|
```cpp
|
|
64
64
|
/// 方格数组:按行优先排列。
|
|
65
|
-
std::array<Square,
|
|
65
|
+
std::array<Square, kSquareCount> squares_{};
|
|
66
66
|
/// 当前玩家:开局由 X 玩家落子。
|
|
67
67
|
Player current_player_{Player::kX};
|
|
68
68
|
/// 落子历史:按落子先后保存。
|
|
69
69
|
std::vector<Move> history_{};
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
类有不变量时生命周期写在类注释;纯值容器的初始状态可以写在类注释末段,默认构造函数不再重复;成员注释只补签名看不出的约定,简单类也可以逐项写:
|
|
73
73
|
|
|
74
74
|
```cpp
|
|
75
|
-
///
|
|
75
|
+
/// 方格。
|
|
76
76
|
class Square {
|
|
77
|
-
///
|
|
77
|
+
/// 方格值。
|
|
78
78
|
SquareValue value_{SquareValue::kEmpty};
|
|
79
79
|
};
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
`board_` 这类名字已经说明存放对象的成员不写「对局棋盘」,也不写「当前棋盘」:时间词只留给确实随时间变化的成员,如 `current_player_` 写「当前玩家」;`squares_`
|
|
82
|
+
`board_` 这类名字已经说明存放对象的成员不写「对局棋盘」,也不写「当前棋盘」:时间词只留给确实随时间变化的成员,如 `current_player_` 写「当前玩家」;`squares_` 的注释先写它是方格数组,再补排列顺序;可写「方格数组:按行优先顺序存放棋盘上的方格。」;不写只重复声明、没有排列信息的空泛句。`clang-format` 不会把行尾注释搬到上一行,位置由本规则和检查器保证。
|
|
83
83
|
|
|
84
84
|
### 常量:控制序列写取值语义
|
|
85
85
|
|
|
@@ -87,28 +87,28 @@ class Square {
|
|
|
87
87
|
|
|
88
88
|
```cpp
|
|
89
89
|
/// 进入备用屏幕的控制序列:保存光标后切换到备用缓冲区。
|
|
90
|
-
inline constexpr std::string_view kEnterAlternateScreen
|
|
90
|
+
inline constexpr std::string_view kEnterAlternateScreen{"\033[?1049h"};
|
|
91
91
|
/// 隐藏光标的控制序列。
|
|
92
|
-
inline constexpr std::string_view kHideCursor
|
|
92
|
+
inline constexpr std::string_view kHideCursor{"\033[?25l"};
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
进入与退出、隐藏与显示这类成对序列取同一句式,便于并排核对;顺序与配对由组合它们的函数表达,不写进常量注释。`scripts/checking-comments.mjs` 对 `constexpr std::string_view`、`constexpr char*` / `constexpr const char*` 与 `constexpr char` 这类带 ESC 的控制序列常量检查注释存在与首句句式:缺注释报 `constant-comment-missing`,首句不合句式报 `constant-sequence-first`;`constexpr auto`、字符数组与非 `constexpr` 的指针不在检查范围。
|
|
96
96
|
|
|
97
97
|
### 枚举项与 `struct` 短字段:两种句式
|
|
98
98
|
|
|
99
|
-
枚举项与只承载数据的 `struct` 短字段可以写行尾 `///<`,同一枚举各行的 `///<` 对齐;`struct` 短字段也可以按数据成员的写法把注释写在声明上一行,同一个 `struct` 只用一种。枚举类型注释写它表示的取值集合或结果,具体类型的规范句见 `glossary/01-comment-terms.md
|
|
99
|
+
枚举项与只承载数据的 `struct` 短字段可以写行尾 `///<`,同一枚举各行的 `///<` 对齐;`struct` 短字段也可以按数据成员的写法把注释写在声明上一行,同一个 `struct` 只用一种。枚举类型注释写它表示的取值集合或结果,具体类型的规范句见 `glossary/01-comment-terms.md`;取值枚举可以写成「方格值枚举:方格的取值可以是空、X 或 O。」这类说明句,也可以先写 `@brief 方格值枚举。`,空一行后再写「方格的取值:空、X 或 O。」、「表示方格可能的取值:空、X 或 O。」或「枚举有三种取值:空、X 或 O。」;需要时写成 `@brief` 行;不把类型注释改写成「对局的一方」。
|
|
100
100
|
|
|
101
|
-
-
|
|
101
|
+
- 行尾短句:状态/结果枚举逐项写,如 `GameResult` 写 `///< 棋盘上 X 已连成一线,X 赢下这一局`;`SquareValue` 这类取值自解释的枚举可以全不写。
|
|
102
102
|
- 结果加说明:以结果类别开头,冒号后按固定顺序写「对局状态 → 落子玩家 → 目标方格」,某一项不适用时省略,不把「方格…」放到句首。如 `///< 成功:落子玩家是当前玩家,值已写入方格`、`///< 失败:目标方格已有值,没有写入`。「没有写入」这类共同后果可在每一项写明,也可由类型注释统一交代,同一枚举保持一致。
|
|
103
|
-
- 短字段写「字段:说明」:冒号前是字段的中文名,冒号后补名字看不出的语境,如 `///< 落子位置:棋盘上的行列坐标`、`///< 落子玩家:游戏中的玩家`;只补默认值或单位时用逗号,如 `///< 行号,默认值为 0`;写在上一行时去掉 `///<`,用全角冒号并以句号结束,如 `/// 行号:默认值为 0
|
|
103
|
+
- 短字段写「字段:说明」:冒号前是字段的中文名,冒号后补名字看不出的语境,如 `///< 落子位置:棋盘上的行列坐标`、`///< 落子玩家:游戏中的玩家`;只补默认值或单位时用逗号,如 `///< 行号,默认值为 0`;写在上一行时去掉 `///<`,用全角冒号并以句号结束,如 `/// 行号:默认值为 0。`,字段之间可以用空行分组。字段名与类型已说清、类型注释已说明组成和默认值时,字段可以不写注释,如 `Position` 的 `row`/`column`;需要补语境时才写。
|
|
104
104
|
|
|
105
|
-
行尾短句不收句号(检查器报 `enum-trailing-period`)。结果行与短字段行用全角 `:`:结果行冒号前写结果类别(成功、失败,或带主体的「落子成功」「落子失败」),短字段行冒号前写字段名;冒号后写这次调用的结果与原因,或字段名看不出的语境,两侧都不能为空。不写「X 成线。」「尚未结束。」这类没有主体、只有谓语的片段;禁止空冒号(`///< 成功:。`)、禁止把说明写在类别前面。无冒号的行尾是一句完整的话,可以有句内逗号;有冒号时不得用逗号代替冒号。同一枚举要么每一项都写
|
|
105
|
+
行尾短句不收句号(检查器报 `enum-trailing-period`)。结果行与短字段行用全角 `:`:结果行冒号前写结果类别(成功、失败,或带主体的「落子成功」「落子失败」),短字段行冒号前写字段名;冒号后写这次调用的结果与原因,或字段名看不出的语境,两侧都不能为空。不写「X 成线。」「尚未结束。」这类没有主体、只有谓语的片段;禁止空冒号(`///< 成功:。`)、禁止把说明写在类别前面。无冒号的行尾是一句完整的话,可以有句内逗号;有冒号时不得用逗号代替冒号。同一枚举要么每一项都写 `///<`,要么都不写。取值域已由类型注释说明、且名字本身能说明含义时可以全不写;只有取值含义不能从名字看出时才逐项写。
|
|
106
106
|
|
|
107
107
|
### 函数:两段式契约
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
有参数或有返回值的函数写两段:第一段写动作或正常结果;第二段按签名把标签写齐。
|
|
110
110
|
|
|
111
|
-
1.
|
|
111
|
+
1. 第一段:普通函数写动作或正常结果;失败时返回什么写 `@return`/`@retval`,抛什么只写 `@throws`;布尔查询按「内容」节第一条与本节「布尔查询」条写。
|
|
112
112
|
2. 第二段:每个参数一条 `@param`;有返回值就写 `@return`;会抛才写 `@throws`;`@pre`、`@post`、`@retval`、`@tparam`、`@note`、`@see` 出厂开关打开;只在确有需要时写,不需要可在设置里关掉。`@returns`、`@details`、`@since`、`@todo` 不写:`@returns` 统一写 `@return`,其余三项不进入源码注释。
|
|
113
113
|
|
|
114
114
|
```cpp
|
|
@@ -127,24 +127,24 @@ inline constexpr std::string_view kHideCursor = "\033[?25l";
|
|
|
127
127
|
[[nodiscard]] PlaceResult Place(Move move);
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
- 布尔查询:第二句写「当…时返回 true」。`@return` 在条件能用一句写清时写「……时返回 true
|
|
131
|
-
- `@param`
|
|
130
|
+
- 布尔查询:第二句写「当…时返回 true」。`@return` 在条件能用一句写清时写「……时返回 true,否则返回 false」;条件正是类型或函数名本身、无法再简述时(如 `operator==`)写「成立时返回 true,不成立时返回 false」。成立与不成立的条件都要写出,不省略后句。`Is`、`Has` 开头的名字与 `bool` 返回类型已经把成立条件说清时也要写 `@return`,不省略。
|
|
131
|
+
- `@param` 每个参数都写,写范围、单位或所有权,不写「索引」这类同义反复;参数类型已经限定取值集合时,写它在本次调用中的作用即可,如 `@param value 待设置的方格值`,仍不写「落子」这类只把参数名译成中文的说明;没有参数就不写空的 `@param`。会检查并抛出的参数写「待检查/读取/设置/更新/查询/清空/计算/写入/格式化的坐标」,不把「棋盘内」当前置条件。
|
|
132
132
|
- `@return` 默认写,写返回值怎么读或失败时返回什么;返回类型与函数名已把条件说清时也要写,不要用「见上」或重复第一段。
|
|
133
|
-
- `@return` 不重复签名里的返回类型:`std::size_t`、`bool`、`std::span`
|
|
134
|
-
- 查询函数(`Get`
|
|
133
|
+
- `@return` 不重复签名里的返回类型:`std::size_t`、`bool`、`std::span` 这类类型名交给签名,描述只写这个值怎么读、取值范围或失败时的取值;范围写成「取值范围为 `[起, 止)`」,`取值为` 只接单个值或枚举值,如「选项个数,至少为 1」「下标值,取值范围为 `[0, CountOptions())`」。
|
|
134
|
+
- 查询函数(`Get` 开头):第一句写「获取…」,不写抛出条件;会抛时异常只写 `@throws`。`@return` 写返回值怎么读;取值集合已由枚举注释说明时,不重复枚举项。返回副本时第一句和 `@return` 都写「副本」,不写「该位置的方格」这类可读成引用的句子。
|
|
135
135
|
- 函数摘要不写参数名:第一段用「指定坐标」「传入的值」「这一步落子」这类语义名词,`position`、`value`、`move` 等名字只在 `@param` 行出现。`@param` 描述里可以写 `kX`、`kO`、`kEmpty` 这类枚举值,枚举值本身是契约的一部分;签名、调用示例和类图里的参数名属于接口,不在此限。
|
|
136
136
|
- 返回多个明确结果(如 `PlaceResult`、`GameResult`)时用 `@retval`,每个值一行,值与说明之间空一格,不做列对齐(一行一项是本工作流的要求;Doxygen 会把相邻的 `\retval` 合成一段,但每项说明仍另起一行,clang-format 不维护标签列宽);只有一个返回值时用 `@return`,不把几种结果压进一句 `@return`。`@retval` 的说明写这个值何时返回:成功项写「成功:…」,失败项写「失败:…」,冒号后按「对局状态 → 落子玩家 → 目标方格」的顺序写,与枚举行尾注释说法一致即可,不要求逐字相同。
|
|
137
|
-
-
|
|
137
|
+
- 更新函数:第一段写动作,必要时在第二段写清楚何时更新、什么时候不改;失败方式只写 `@throws`,不重复「保持原值」;多个结果用 `@retval`,每个值一行,成功项写在最前,其余按固定顺序。这个固定顺序由 `@retval` 的排列表达,不用 `@note` 复述;空行与分组排版见 `02-style.md` 的「版式」节;`@note` 只写调用方额外要知道的运行时风险或前提。会抛的诊断函数(如 `ValidatePosition`)第一句写检查对象与范围,如「检查坐标是否落在棋盘内。」;抛出条件只写 `@throws`。
|
|
138
138
|
- `@throws` 必须与 `references/exceptions/01-exception-rules.md` 一致;不抛异常的函数不写。
|
|
139
139
|
- 私有函数同样写契约,不能默认调用者知道前置条件;确有 out 参数时标 `[out]`,既读又写标 `[in,out]`。
|
|
140
140
|
- 函数注释不写本函数被哪些成员调用:调用关系会随重构过期,契约只写这个函数做什么、参数、返回值与异常;需要说明协作时写前置条件或后置条件,不点名其他成员。
|
|
141
|
-
- `std::formatter<T>`
|
|
141
|
+
- `std::formatter<T>` 特化把类型说明写在 `template <>` 上方:`@brief` 写这个格式化器的类型名或简短称呼,如「行列坐标格式化器。」;第二段写输出形态,如「表示把行列坐标格式化为 `(行号, 列号)` 形式的文本。」,不再重复「格式化器」;`parse` 与 `format` 各写函数契约,不写 `@brief`:`parse` 第一段写「解析格式说明,只接受空格式说明。」,`@param context` 写「格式字符串解析上下文。」,失败方式用 `@throws`,写「格式说明非空时抛出。」;`format` 第一段写「格式化行列坐标并写入输出。」,`@param context` 写「格式化上下文,提供输出迭代器。」,其后只写 `@return`;format-spec 写「格式说明」,不写「格式说明符」;注释里不写「特化」,类型注释不写 `@par 异常`。
|
|
142
142
|
|
|
143
143
|
### 实现:阶段边界与为什么
|
|
144
144
|
|
|
145
|
-
`.cpp` / `.cc` 里,函数体用 `//`,不用 `///`:体内的 `///` 会接到该函数的 Doxygen 详细说明后面,成为误导性文档。重要函数按空行分段,每一段上方写一句 `//`;同一段里几条语句各对应一件独立事实时,事实写在该语句行尾。`//` 里的枚举值不包反引号,反引号只留给声明注释;一句赋值、单表达式函数(如 `IsEmpty`)不写注释;与 `if`
|
|
145
|
+
`.cpp` / `.cc` 里,函数体用 `//`,不用 `///`:体内的 `///` 会接到该函数的 Doxygen 详细说明后面,成为误导性文档。重要函数按空行分段,每一段上方写一句 `//`;同一段里几条语句各对应一件独立事实时,事实写在该语句行尾。`//` 里的枚举值不包反引号,反引号只留给声明注释;一句赋值、单表达式函数(如 `IsEmpty`)不写注释;与 `if` 同义的「检查…是否…」不写(值域白名单的合法值写在 `case` 上,不写注释),分支结果交给 `return`、`throw` 或赋值。只有次序依赖、恢复现场、调用方保证或防御分支的理由才写一句。局部值是上一行赋值取出、赋值与上一个阶段之间又有空行时,注释写在赋值那一行上方,说明赋值与紧随其后的分支这整块;`if` 上不再重复同一句。只写删掉会让下一次修改得到错误结果、而代码本身看不出的内容。
|
|
146
146
|
|
|
147
|
-
拒绝与写入分支默认不写注释:`return`、`throw`
|
|
147
|
+
拒绝与写入分支默认不写注释:`return`、`throw` 或赋值已经说明结果;值域白名单的 `case` 已经列出合法值,不再写允许范围。
|
|
148
148
|
|
|
149
149
|
```cpp
|
|
150
150
|
// 先判终局,再判玩家与方格:失败结果的优先级由此决定。
|
|
@@ -153,10 +153,10 @@ if (!IsInProgress()) {
|
|
|
153
153
|
}
|
|
154
154
|
```
|
|
155
155
|
|
|
156
|
-
- 只有次序依赖、恢复现场、调用方保证或防御分支的理由才写一句;不写与 `if`
|
|
156
|
+
- 只有次序依赖、恢复现场、调用方保证或防御分支的理由才写一句;不写与 `if` 同义的「检查…是否…」,值域白名单不在注释里重复允许范围。
|
|
157
157
|
- 分支只检查紧邻上一行取出的元素(如 `Square& square = ...` 后的 `if (!square.IsEmpty())`)时,说明写在取元素那一行上方;`if` 上不重复,赋值与 `if` 之间也不留注释。
|
|
158
|
-
-
|
|
159
|
-
-
|
|
158
|
+
- 普通拒绝分支不写「目标方格已有值时不覆盖。」这类只复述后果的句子;值域白名单也不在注释里写允许范围。
|
|
159
|
+
- 防御性分支写一句为什么这里还要判;值域白名单的合法取值已经写在 `case` 上,不再写允许范围,也不解释枚举类型为什么不封闭,也不写「方格只接受…,枚举类却允许…」这类对照;没有真实防御分支时不硬加。
|
|
160
160
|
- 多条独立拒绝(如 `Game::Place`)不逐条注释;只有失败结果的优先级需要说明时,在第一条 `if` 上方写一句总起。
|
|
161
161
|
- 一个 `if` 有多个条件时,按代码的判定顺序写:先确认的条件在前,后核对的在后。如 `// 先确认第一个方格有值,再核对三个方格的取值是否相同。`
|
|
162
162
|
- 成功路径含多步状态更新(记录这一步、轮到对方、计数)时,在第一步上方用一句阶段注释概括整段;不在每一步上方逐行解释。
|
|
@@ -187,10 +187,10 @@ if (!IsInProgress()) {
|
|
|
187
187
|
|
|
188
188
|
| 层 | 行数 | 第一句 | 写什么 |
|
|
189
189
|
| --- | --- | --- | --- |
|
|
190
|
-
| 文件头 |
|
|
191
|
-
| 类型注释 | 1–14 |
|
|
190
|
+
| 文件头 | 3–7;写了 `@version` 时再 +1(类型定义型头文件默认只写 `@file` 与 `@brief`;实现文件 2–3) | `@file` 文件名 + `@brief` 一句话(中间可空一行) | 本文件定义什么;有额外组成或边界时再补一段;元数据按 `@author`、`@version`、`@date`、`@copyright` 成组 |
|
|
191
|
+
| 类型注释 | 1–14 | 标题、职责、可选默认值 | 纯值容器与纯数据结构的默认值写第三段;只有类的不变量与生命周期才再加段 |
|
|
192
192
|
| 特殊成员 | 1–3 | 标签(默认/拷贝/移动构造函数、析构函数) | 后置条件或职责 |
|
|
193
|
-
| 函数注释 | 3–10(按标签数放宽) |
|
|
193
|
+
| 函数注释 | 3–10(按标签数放宽) | 动作或正常结果;失败返回写标签,异常只写 `@throws`;成立条件 | 每个参数、返回值与失败方式 |
|
|
194
194
|
| 无标签函数 | 1–3(两段时 3 行) | 两段:后置条件/职责,再补边界 | 仅限构造、析构、`void` 无参数 |
|
|
195
195
|
| 数据成员 | 上一行 1 句(`/// 名词:约定`) | 这个成员是什么;只属于它的轴向、哨兵或所有权 | 只写成员自身,不重复类注释的初值与状态迁移 |
|
|
196
196
|
| 常量 | 上一行 1–2 句(`/// 名词:说明`) | 名词短语:……的控制序列 | 序列在终端上的额外效果与前提;名字说全时只写一句 |
|
|
@@ -222,9 +222,10 @@ if (!IsInProgress()) {
|
|
|
222
222
|
| rule | 条文 |
|
|
223
223
|
| --- | --- |
|
|
224
224
|
| `header-missing`、`header-file-name`、`header-brief`、`header-cross-file` | `02-style.md` §文件头 |
|
|
225
|
+
| `header-tag-order`、`header-tag-gap`、`header-date` | `02-style.md` §文件头(元数据次序、空行与创建日期) |
|
|
225
226
|
| `comment-width` | `02-style.md` §版式(行宽) |
|
|
226
227
|
| `list-indent` | `02-style.md` §版式(列表) |
|
|
227
|
-
| `forbidden-tag` | `02-style.md`
|
|
228
|
+
| `forbidden-tag` | `02-style.md` §文件头(四个元数据标签在文件头默认放行);§函数:两段式契约(禁用标签见 01 §函数第 2 条) |
|
|
228
229
|
| `doc-gap`、`brief-tag-gap`、`tag-gap` | `02-style.md` §结构、§版式(标签空行) |
|
|
229
230
|
| `doc-on-definition`、`doc-in-body`、`cpp-contract-missing` | §实现;`02-style.md` §结构(函数体不写 `///`) |
|
|
230
231
|
| `member-comment-missing`、`member-comment-trailing`、`member-comment-slash` | §数据成员 |
|