@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,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// 只读注释审计:按 references/comments.md 的硬规则检查 C/C++ 源文件。
|
|
2
|
+
// 只读注释审计:按 references/comments/comment-rules.md 的硬规则检查 C/C++ 源文件。
|
|
3
3
|
//
|
|
4
4
|
// 检查项:Doxygen 文件头、注释行宽度、列表缩进、标签、标签间空行、注释与声明的贴合、
|
|
5
|
-
// 类型注释存在性、成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param
|
|
5
|
+
// 类型注释存在性、成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param 完整性、摘要裸参数名、枚举成员注释一致性、枚举行尾句式、数据成员主注释位置。
|
|
6
6
|
// 不重写任何文件。
|
|
7
7
|
//
|
|
8
8
|
// 用法:
|
|
@@ -40,6 +40,8 @@ const NEGATED_BOUNDARY = /只保存|不拥有|不含/
|
|
|
40
40
|
const NEGATIVE_SCOPE = /不(?:检查|校验|验证|负责|判断)/
|
|
41
41
|
const DOMAIN_SIZE = /\d+\s*[×xX]\s*\d+|九格|九个格子/
|
|
42
42
|
const PREDICATE_DECL = /^(?:\[\[nodiscard\]\]\s*)?(?:static\s+)?bool\s+(?:Is|Has|Can|Completes)[A-Za-z0-9_]*\s*\(|operator==/
|
|
43
|
+
const isPredicateFirstOk = (declaration, firstDocLine) =>
|
|
44
|
+
firstDocLine.startsWith('检查') || (declaration.includes('operator==') && firstDocLine.startsWith('相等运算符'))
|
|
43
45
|
const DECL_KEYWORDS = /^(class|struct|union|enum|namespace|using|typedef|template|#|if|else|for|while|switch|return|case|default)\b/
|
|
44
46
|
|
|
45
47
|
function usage(message) {
|
|
@@ -135,16 +137,44 @@ export function displayWidth(text) {
|
|
|
135
137
|
return width
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
|
|
140
|
+
const TAG_GROUPS = {
|
|
141
|
+
param: 'param',
|
|
142
|
+
tparam: 'param',
|
|
143
|
+
return: 'return',
|
|
144
|
+
returns: 'return',
|
|
145
|
+
retval: 'return',
|
|
146
|
+
result: 'return',
|
|
147
|
+
throws: 'throws',
|
|
148
|
+
throw: 'throws',
|
|
149
|
+
exception: 'throws',
|
|
150
|
+
note: 'note',
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 取标签所属分组;参数、返回值、异常、说明各成一组。 */
|
|
154
|
+
function tagGroupOf(tag) {
|
|
155
|
+
const name = tag.slice(1).toLowerCase().split(/[\s{[(]/)[0]
|
|
156
|
+
return TAG_GROUPS[name] ?? 'other'
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 分组之间允许空行;同一分组内出现空行才算标签粘连。描述与首个标签之间的空行不算。 */
|
|
139
160
|
function hasTagGap(contents) {
|
|
140
|
-
let
|
|
161
|
+
let lastGroup = null
|
|
162
|
+
let pendingGap = false
|
|
141
163
|
for (const content of contents) {
|
|
142
164
|
const trimmed = content.trim()
|
|
143
165
|
if (trimmed.startsWith('@')) {
|
|
144
|
-
|
|
166
|
+
const group = tagGroupOf(trimmed)
|
|
167
|
+
if (pendingGap && lastGroup === group) return true
|
|
168
|
+
pendingGap = false
|
|
169
|
+
lastGroup = group
|
|
145
170
|
continue
|
|
146
171
|
}
|
|
147
|
-
if (
|
|
172
|
+
if (trimmed === '') {
|
|
173
|
+
if (lastGroup !== null) pendingGap = true
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
// 空行后接正文是标签说明内的段落,不是分组分隔,取消待定空行
|
|
177
|
+
pendingGap = false
|
|
148
178
|
}
|
|
149
179
|
return false
|
|
150
180
|
}
|
|
@@ -210,6 +240,23 @@ export function functionParams(declaration) {
|
|
|
210
240
|
return splitParams(decl.slice(open + 1, close)).map(paramName).filter(Boolean)
|
|
211
241
|
}
|
|
212
242
|
|
|
243
|
+
/** 取函数注释第一段:遇到空行或 Doxygen 标签就结束。 */
|
|
244
|
+
function summaryParagraph(contents) {
|
|
245
|
+
const paragraph = []
|
|
246
|
+
for (const line of contents) {
|
|
247
|
+
const text = line.trim()
|
|
248
|
+
if (text === '' || text.startsWith('@')) break
|
|
249
|
+
paragraph.push(text)
|
|
250
|
+
}
|
|
251
|
+
return paragraph.join(' ')
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** 判断摘要中是否直接出现了参数名。 */
|
|
255
|
+
function mentionsParam(text, name) {
|
|
256
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
257
|
+
return new RegExp(`(^|[^A-Za-z0-9_])${escaped}([^A-Za-z0-9_]|$)`).test(text)
|
|
258
|
+
}
|
|
259
|
+
|
|
213
260
|
/** 标记每一行是否位于 class/struct 体内,供数据成员注释位置检查使用。 */
|
|
214
261
|
function classMemberLines(lines) {
|
|
215
262
|
const flags = new Array(lines.length).fill(false)
|
|
@@ -330,7 +377,7 @@ export function checkText(text, options = {}) {
|
|
|
330
377
|
}
|
|
331
378
|
for (const tag of FORBIDDEN_TAGS) {
|
|
332
379
|
if (options.allowTags?.has(tag.slice(1))) continue
|
|
333
|
-
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments.md 的标签表改写`)
|
|
380
|
+
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/comment-rules.md 的标签表改写`)
|
|
334
381
|
}
|
|
335
382
|
}
|
|
336
383
|
}
|
|
@@ -344,7 +391,7 @@ export function checkText(text, options = {}) {
|
|
|
344
391
|
if (displayWidth(rendered) > width) add(doc.start + 1, 'comment-width', `注释行 ${displayWidth(rendered)} 列,超过 ${width} 列`)
|
|
345
392
|
for (const tag of FORBIDDEN_TAGS) {
|
|
346
393
|
if (options.allowTags?.has(tag.slice(1))) continue
|
|
347
|
-
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments.md 的标签表改写`)
|
|
394
|
+
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/comment-rules.md 的标签表改写`)
|
|
348
395
|
}
|
|
349
396
|
}
|
|
350
397
|
for (let raw = doc.start; raw <= doc.end; raw += 1) {
|
|
@@ -353,7 +400,7 @@ export function checkText(text, options = {}) {
|
|
|
353
400
|
}
|
|
354
401
|
}
|
|
355
402
|
if (!doc.contents.some((content) => content.includes('@file')) && hasTagGap(doc.contents)) {
|
|
356
|
-
add(doc.start + 1, 'tag-gap', 'Doxygen
|
|
403
|
+
add(doc.start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
357
404
|
}
|
|
358
405
|
index = doc.end
|
|
359
406
|
}
|
|
@@ -382,7 +429,7 @@ export function checkText(text, options = {}) {
|
|
|
382
429
|
const block = lines.slice(start, end + 1).join('\n')
|
|
383
430
|
const docContents = lines.slice(start, end + 1).map((line) => line.trim().replace(/^\/\/\/\s?/, ''))
|
|
384
431
|
if (hasTagGap(docContents)) {
|
|
385
|
-
add(start + 1, 'tag-gap', 'Doxygen
|
|
432
|
+
add(start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
386
433
|
}
|
|
387
434
|
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
388
435
|
if (RATIONALE.test(block)) {
|
|
@@ -404,14 +451,19 @@ export function checkText(text, options = {}) {
|
|
|
404
451
|
add(start + 1, 'negative-scope', '注释用「不检查/不校验/不负责/不判断」写能力边界:改成正向限定,前置条件写「调用方保证…」或 @pre')
|
|
405
452
|
}
|
|
406
453
|
if (isType && DOMAIN_SIZE.test(block)) {
|
|
407
|
-
add(start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由
|
|
454
|
+
add(start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由 squares_ 之类成员声明表达,删掉这句')
|
|
408
455
|
}
|
|
409
456
|
const firstDocLine = lines[start].trim().replace(/^\/\/\/\s*/, '')
|
|
410
|
-
if (!isType && PREDICATE_DECL.test(declaration) && !firstDocLine
|
|
457
|
+
if (!isType && PREDICATE_DECL.test(declaration) && !isPredicateFirstOk(declaration, firstDocLine)) {
|
|
411
458
|
add(start + 1, 'predicate-first', '布尔查询或 operator== 的第一句应写「检查…是否…」')
|
|
412
459
|
}
|
|
413
460
|
const params = functionParams(declaration)
|
|
414
461
|
if (!params) continue
|
|
462
|
+
const summary = summaryParagraph(docContents)
|
|
463
|
+
const named = params.filter((name) => mentionsParam(summary, name))
|
|
464
|
+
if (named.length > 0) {
|
|
465
|
+
add(start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
466
|
+
}
|
|
415
467
|
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
416
468
|
if (documented.length === 0) continue
|
|
417
469
|
for (const name of params) {
|
|
@@ -446,13 +498,18 @@ export function checkText(text, options = {}) {
|
|
|
446
498
|
if (isType && TYPE_SYSTEM.test(block)) add(doc.start + 1, 'type-system', '类型注释在复述类型系统已表达的性质:可拷贝与 const 查询由声明给出,删掉这句')
|
|
447
499
|
if (isType && NEGATED_BOUNDARY.test(block)) add(doc.start + 1, 'negated-boundary', '类型注释用「只保存/不拥有/不含」写否定式边界:改成名词短语,边界交给成员类型表达')
|
|
448
500
|
if (NEGATIVE_SCOPE.test(block)) add(doc.start + 1, 'negative-scope', '注释用「不检查/不校验/不负责/不判断」写能力边界:改成正向限定,前置条件写「调用方保证…」或 @pre')
|
|
449
|
-
if (isType && DOMAIN_SIZE.test(block)) add(doc.start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由
|
|
501
|
+
if (isType && DOMAIN_SIZE.test(block)) add(doc.start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由 squares_ 之类成员声明表达,删掉这句')
|
|
450
502
|
const firstDocLine = doc.contents[0] ?? ''
|
|
451
|
-
if (!isType && PREDICATE_DECL.test(declaration) && !firstDocLine
|
|
503
|
+
if (!isType && PREDICATE_DECL.test(declaration) && !isPredicateFirstOk(declaration, firstDocLine)) {
|
|
452
504
|
add(doc.start + 1, 'predicate-first', '布尔查询或 operator== 的第一句应写「检查…是否…」')
|
|
453
505
|
}
|
|
454
506
|
const params = functionParams(declaration)
|
|
455
507
|
if (!params) continue
|
|
508
|
+
const summary = summaryParagraph(doc.contents)
|
|
509
|
+
const named = params.filter((name) => mentionsParam(summary, name))
|
|
510
|
+
if (named.length > 0) {
|
|
511
|
+
add(doc.start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
512
|
+
}
|
|
456
513
|
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
457
514
|
if (documented.length === 0) continue
|
|
458
515
|
for (const name of params) {
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 只读检查 `.cpp` 中成员函数的定义顺序是否跟随配对头文件的声明顺序。
|
|
3
|
+
//
|
|
4
|
+
// 规则见 references/layout/03-declaration-order.md「`.cpp` 的定义顺序」:
|
|
5
|
+
// 先类型常量、再自由函数、再成员函数;成员按类内声明顺序;构造函数、析构函数、
|
|
6
|
+
// 拷贝与移动放在成员组最前;不为先编译而重排。
|
|
7
|
+
//
|
|
8
|
+
// 本脚本只比较能在配对头文件里找到声明的类成员;自由函数与匿名命名空间不参与,
|
|
9
|
+
// 头文件里没有声明的定义直接跳过,避免把另一个头文件的实现误报成乱序。
|
|
10
|
+
//
|
|
11
|
+
// 用法:
|
|
12
|
+
// node checking-declaration-order.mjs [--root DIR] [--json] [--quiet]
|
|
13
|
+
//
|
|
14
|
+
// 退出码:0 无问题,1 发现乱序,2 用法或读取错误。
|
|
15
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
import { pathToFileURL } from 'node:url'
|
|
18
|
+
|
|
19
|
+
export const EXIT = { ok: 0, issues: 1, usage: 2 }
|
|
20
|
+
|
|
21
|
+
const HEADER_EXT = new Set(['.h', '.hh', '.hpp', '.hxx', '.h++', '.inl', '.ipp', '.tpp'])
|
|
22
|
+
const SOURCE_EXT = new Set(['.cpp', '.cc', '.cxx', '.c++'])
|
|
23
|
+
const SKIP_DIR = new Set([
|
|
24
|
+
'.git', '.hg', '.svn', 'build', 'build-release', 'out', 'node_modules', '_book', '.quarto',
|
|
25
|
+
'.cache', '.tmp', 'temp', 'third_party', 'third-party', 'external', 'vendor', 'deps',
|
|
26
|
+
'cmake-build-debug', 'cmake-build-release', '.vs', '.idea',
|
|
27
|
+
])
|
|
28
|
+
|
|
29
|
+
function usage(message) {
|
|
30
|
+
if (message) process.stderr.write(`错误:${message}\n`)
|
|
31
|
+
process.stderr.write('用法:node checking-declaration-order.mjs [--root DIR] [--json] [--quiet]\n')
|
|
32
|
+
return EXIT.usage
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseArgs(argv) {
|
|
36
|
+
const options = { root: undefined, json: false, quiet: false, help: false }
|
|
37
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
38
|
+
const arg = argv[index]
|
|
39
|
+
if (arg === '--root') {
|
|
40
|
+
options.root = argv[index + 1]
|
|
41
|
+
if (!options.root) throw new Error('--root 需要目录')
|
|
42
|
+
index += 1
|
|
43
|
+
} else if (arg === '--json') {
|
|
44
|
+
options.json = true
|
|
45
|
+
} else if (arg === '--quiet') {
|
|
46
|
+
options.quiet = true
|
|
47
|
+
} else if (arg === '--help' || arg === '-h') {
|
|
48
|
+
options.help = true
|
|
49
|
+
} else {
|
|
50
|
+
throw new Error(`未知参数:${arg}`)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return options
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 去掉注释与字符串,保留换行,便于按行号定位。 */
|
|
57
|
+
export function stripCommentsAndStrings(text) {
|
|
58
|
+
const out = text.split('')
|
|
59
|
+
let index = 0
|
|
60
|
+
while (index < text.length) {
|
|
61
|
+
const char = text[index]
|
|
62
|
+
const next = text[index + 1]
|
|
63
|
+
if (char === '/' && next === '/') {
|
|
64
|
+
while (index < text.length && text[index] !== '\n') out[index++] = ' '
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
if (char === '/' && next === '*') {
|
|
68
|
+
out[index++] = ' '
|
|
69
|
+
out[index++] = ' '
|
|
70
|
+
while (index < text.length && !(text[index] === '*' && text[index + 1] === '/')) {
|
|
71
|
+
if (text[index] !== '\n') out[index] = ' '
|
|
72
|
+
index += 1
|
|
73
|
+
}
|
|
74
|
+
if (index < text.length) {
|
|
75
|
+
out[index++] = ' '
|
|
76
|
+
out[index++] = ' '
|
|
77
|
+
}
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
if (char === 'R' && next === '"') {
|
|
81
|
+
const open = text.indexOf('(', index + 2)
|
|
82
|
+
if (open !== -1) {
|
|
83
|
+
const delimiter = text.slice(index + 2, open)
|
|
84
|
+
const close = text.indexOf(`)${delimiter}"`, open + 1)
|
|
85
|
+
const end = close === -1 ? text.length : close + delimiter.length + 2
|
|
86
|
+
for (; index < end; index += 1) if (text[index] !== '\n') out[index] = ' '
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (char === '"' || char === "'") {
|
|
91
|
+
const quote = char
|
|
92
|
+
out[index++] = ' '
|
|
93
|
+
while (index < text.length) {
|
|
94
|
+
if (text[index] === '\\') {
|
|
95
|
+
out[index++] = ' '
|
|
96
|
+
if (index < text.length) out[index++] = ' '
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
if (text[index] === quote) {
|
|
100
|
+
out[index++] = ' '
|
|
101
|
+
break
|
|
102
|
+
}
|
|
103
|
+
if (text[index] !== '\n') out[index] = ' '
|
|
104
|
+
index += 1
|
|
105
|
+
}
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
index += 1
|
|
109
|
+
}
|
|
110
|
+
return out.join('')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function lineAt(text, index) {
|
|
114
|
+
let line = 1
|
|
115
|
+
for (let cursor = 0; cursor < index && cursor < text.length; cursor += 1) {
|
|
116
|
+
if (text[cursor] === '\n') line += 1
|
|
117
|
+
}
|
|
118
|
+
return line
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function matchBrace(text, open) {
|
|
122
|
+
let depth = 0
|
|
123
|
+
for (let index = open; index < text.length; index += 1) {
|
|
124
|
+
if (text[index] === '{') depth += 1
|
|
125
|
+
else if (text[index] === '}') {
|
|
126
|
+
depth -= 1
|
|
127
|
+
if (depth === 0) return index
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return -1
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 收集头文件里的 class/struct 名称与类体范围。 */
|
|
134
|
+
export function findClasses(text) {
|
|
135
|
+
const code = stripCommentsAndStrings(text)
|
|
136
|
+
const classes = []
|
|
137
|
+
const pattern = /\b(class|struct)\s+([A-Za-z_]\w*)\b[^;{}]*\{/g
|
|
138
|
+
let match
|
|
139
|
+
while ((match = pattern.exec(code)) !== null) {
|
|
140
|
+
const keyword = match[1]
|
|
141
|
+
const name = match[2]
|
|
142
|
+
const before = code.slice(Math.max(0, match.index - 6), match.index)
|
|
143
|
+
if (keyword === 'class' && /enum\s*$/.test(before)) continue
|
|
144
|
+
const open = code.indexOf('{', match.index)
|
|
145
|
+
const close = matchBrace(code, open)
|
|
146
|
+
if (close === -1) continue
|
|
147
|
+
classes.push({ name, body: code.slice(open + 1, close), bodyStart: open + 1 })
|
|
148
|
+
pattern.lastIndex = close + 1
|
|
149
|
+
}
|
|
150
|
+
return classes
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function normalizeStatement(statement) {
|
|
154
|
+
return statement
|
|
155
|
+
.replace(/\[\[[^\]]*\]\]/g, ' ')
|
|
156
|
+
.replace(/\s+/g, ' ')
|
|
157
|
+
.trim()
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 从一个类体里按声明顺序提取可定义的成员函数名。 */
|
|
161
|
+
export function classMembers(body) {
|
|
162
|
+
const clean = body.replace(/\b(public|private|protected)\s*:/g, ' ')
|
|
163
|
+
const members = []
|
|
164
|
+
let index = 0
|
|
165
|
+
while (index < clean.length) {
|
|
166
|
+
while (index < clean.length && /\s/.test(clean[index])) index += 1
|
|
167
|
+
if (index >= clean.length) break
|
|
168
|
+
if (clean[index] === '{') {
|
|
169
|
+
const close = matchBrace(clean, index)
|
|
170
|
+
if (close === -1) break
|
|
171
|
+
index = close + 1
|
|
172
|
+
while (index < clean.length && /[\s;]/.test(clean[index])) index += 1
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
const start = index
|
|
176
|
+
let paren = 0
|
|
177
|
+
let angle = 0
|
|
178
|
+
let brace = 0
|
|
179
|
+
for (; index < clean.length; index += 1) {
|
|
180
|
+
const char = clean[index]
|
|
181
|
+
if (char === '(') paren += 1
|
|
182
|
+
else if (char === ')') paren = Math.max(0, paren - 1)
|
|
183
|
+
else if (char === '<') angle += 1
|
|
184
|
+
else if (char === '>') angle = Math.max(0, angle - 1)
|
|
185
|
+
else if (char === '{') brace += 1
|
|
186
|
+
else if (char === '}') {
|
|
187
|
+
if (brace === 0) break
|
|
188
|
+
brace -= 1
|
|
189
|
+
} else if (char === ';' && paren === 0 && brace === 0) break
|
|
190
|
+
}
|
|
191
|
+
const statement = clean.slice(start, index)
|
|
192
|
+
index += 1
|
|
193
|
+
const text = normalizeStatement(statement)
|
|
194
|
+
if (!text || /^(using|typedef|friend\s+class|static_assert|enum|class|struct|union|namespace)\b/.test(text)) {
|
|
195
|
+
continue
|
|
196
|
+
}
|
|
197
|
+
if (!text.includes('(')) continue
|
|
198
|
+
const name = memberName(text)
|
|
199
|
+
if (name) members.push({ name, text })
|
|
200
|
+
}
|
|
201
|
+
return members
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function memberName(text) {
|
|
205
|
+
let value = text
|
|
206
|
+
.replace(/^template\s*<[^>]*>\s*/, '')
|
|
207
|
+
.replace(/^(?:static|inline|virtual|constexpr|consteval|explicit|friend|extern)\s+/g, '')
|
|
208
|
+
.trim()
|
|
209
|
+
const operator = /(?:^|\s)(operator\s*[^\s(]+)\s*\(/.exec(value)
|
|
210
|
+
if (operator) return operator[1].replace(/\s+/g, ' ')
|
|
211
|
+
const destructor = /(?:^|\s)(~[A-Za-z_]\w*)\s*\(/.exec(value)
|
|
212
|
+
if (destructor) return destructor[1]
|
|
213
|
+
const constructor = /(?:^|\s)([A-Za-z_]\w*)\s*\(/.exec(value)
|
|
214
|
+
if (constructor && (value.startsWith(`${constructor[1]}(`) || /\bexplicit\s+$/.test(value.slice(0, constructor.index + 1)))) {
|
|
215
|
+
return constructor[1]
|
|
216
|
+
}
|
|
217
|
+
const fn = /([A-Za-z_]\w*)\s*\(/.exec(value)
|
|
218
|
+
return fn ? fn[1] : undefined
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 找出 `.cpp` 中形如 `Type Class::Name(` 的定义及其行号。 */
|
|
222
|
+
export function cppDefinitions(text) {
|
|
223
|
+
const code = stripCommentsAndStrings(text)
|
|
224
|
+
const definitions = []
|
|
225
|
+
const lines = code.split('\n')
|
|
226
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
227
|
+
const line = lines[index]
|
|
228
|
+
const match = /^\s*(?:template\s*<[^>]*>\s*)?(?:[A-Za-z_][\w:<>,\s*&]*?\s+)?([A-Za-z_]\w*)::(~?[A-Za-z_]\w*|operator\s*[^\s(]+)\s*\(/.exec(line)
|
|
229
|
+
if (!match) continue
|
|
230
|
+
definitions.push({
|
|
231
|
+
className: match[1],
|
|
232
|
+
name: match[2].replace(/\s+/g, ' '),
|
|
233
|
+
line: index + 1,
|
|
234
|
+
text: line.trim(),
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
return definitions
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** 比较一个头文件与一个实现文件;返回乱序问题。 */
|
|
241
|
+
export function checkPair(headerText, cppText, fileName = '') {
|
|
242
|
+
const classes = findClasses(headerText)
|
|
243
|
+
const definitions = cppDefinitions(cppText)
|
|
244
|
+
const issues = []
|
|
245
|
+
for (const item of classes) {
|
|
246
|
+
const members = classMembers(item.body)
|
|
247
|
+
if (members.length === 0) continue
|
|
248
|
+
const declared = members.map((member) => member.name)
|
|
249
|
+
const defined = definitions.filter((definition) => definition.className === item.name)
|
|
250
|
+
let cursor = 0
|
|
251
|
+
let previous
|
|
252
|
+
for (const definition of defined) {
|
|
253
|
+
let found = declared.indexOf(definition.name, cursor)
|
|
254
|
+
if (found === -1 && declared.includes(definition.name)) {
|
|
255
|
+
issues.push({
|
|
256
|
+
line: definition.line,
|
|
257
|
+
rule: 'definition-order',
|
|
258
|
+
message: `${item.name}::${definition.name} 出现在 ${previous ? `${item.name}::${previous} 之后` : '成员组开头'};头文件声明的顺序是 ${declared.join(' → ')}。`,
|
|
259
|
+
})
|
|
260
|
+
continue
|
|
261
|
+
}
|
|
262
|
+
if (found === -1) continue
|
|
263
|
+
cursor = found + 1
|
|
264
|
+
previous = definition.name
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return issues.map((issue) => ({ file: fileName, ...issue }))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function collectFiles(root) {
|
|
271
|
+
const files = { headers: [], sources: [] }
|
|
272
|
+
const walk = (dir) => {
|
|
273
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
274
|
+
const full = path.join(dir, entry.name)
|
|
275
|
+
if (entry.isDirectory()) {
|
|
276
|
+
if (SKIP_DIR.has(entry.name.toLowerCase()) || entry.name.startsWith('.')) continue
|
|
277
|
+
walk(full)
|
|
278
|
+
continue
|
|
279
|
+
}
|
|
280
|
+
if (!entry.isFile()) continue
|
|
281
|
+
const extension = path.extname(entry.name).toLowerCase()
|
|
282
|
+
if (HEADER_EXT.has(extension)) files.headers.push(full)
|
|
283
|
+
if (SOURCE_EXT.has(extension)) files.sources.push(full)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (statSync(root).isFile()) {
|
|
287
|
+
const extension = path.extname(root).toLowerCase()
|
|
288
|
+
if (HEADER_EXT.has(extension)) files.headers.push(root)
|
|
289
|
+
if (SOURCE_EXT.has(extension)) files.sources.push(root)
|
|
290
|
+
} else {
|
|
291
|
+
walk(root)
|
|
292
|
+
}
|
|
293
|
+
files.headers.sort()
|
|
294
|
+
files.sources.sort()
|
|
295
|
+
return files
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function checkRoot(root) {
|
|
299
|
+
const files = collectFiles(root)
|
|
300
|
+
const headers = new Map()
|
|
301
|
+
for (const file of files.headers) {
|
|
302
|
+
const stem = path.basename(file, path.extname(file))
|
|
303
|
+
const bucket = headers.get(stem) ?? []
|
|
304
|
+
bucket.push(file)
|
|
305
|
+
headers.set(stem, bucket)
|
|
306
|
+
}
|
|
307
|
+
const pairs = []
|
|
308
|
+
const issues = []
|
|
309
|
+
for (const file of files.sources) {
|
|
310
|
+
const stem = path.basename(file, path.extname(file))
|
|
311
|
+
const candidates = headers.get(stem) ?? []
|
|
312
|
+
if (candidates.length !== 1) continue
|
|
313
|
+
const header = candidates[0]
|
|
314
|
+
const headerText = readFileSync(header, 'utf8')
|
|
315
|
+
const cppText = readFileSync(file, 'utf8')
|
|
316
|
+
const relative = path.relative(root, file) || file
|
|
317
|
+
const found = checkPair(headerText, cppText, relative)
|
|
318
|
+
pairs.push({ header, source: file, issues: found })
|
|
319
|
+
issues.push(...found)
|
|
320
|
+
}
|
|
321
|
+
return { pairs, issues }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function main(argv) {
|
|
325
|
+
let options
|
|
326
|
+
try {
|
|
327
|
+
options = parseArgs(argv)
|
|
328
|
+
} catch (error) {
|
|
329
|
+
return usage(error.message)
|
|
330
|
+
}
|
|
331
|
+
if (options.help) return usage()
|
|
332
|
+
const root = options.root ? path.resolve(options.root) : process.cwd()
|
|
333
|
+
try {
|
|
334
|
+
statSync(root)
|
|
335
|
+
} catch {
|
|
336
|
+
return usage(`根目录不存在或无法读取:${root}`)
|
|
337
|
+
}
|
|
338
|
+
let result
|
|
339
|
+
try {
|
|
340
|
+
result = checkRoot(root)
|
|
341
|
+
} catch (error) {
|
|
342
|
+
return usage(`读取 ${root} 失败:${error.message}`)
|
|
343
|
+
}
|
|
344
|
+
if (options.json) {
|
|
345
|
+
process.stdout.write(`${JSON.stringify({ root, pairs: result.pairs.length, issues: result.issues }, null, 2)}\n`)
|
|
346
|
+
} else if (result.issues.length > 0) {
|
|
347
|
+
for (const issue of result.issues) process.stdout.write(`${issue.file}:${issue.line}: [${issue.rule}] ${issue.message}\n`)
|
|
348
|
+
process.stdout.write(`发现 ${result.issues.length} 处定义顺序问题。\n`)
|
|
349
|
+
} else if (!options.quiet) {
|
|
350
|
+
process.stdout.write(`定义顺序检查通过:${result.pairs.length} 对头文件与实现文件。\n`)
|
|
351
|
+
}
|
|
352
|
+
return result.issues.length === 0 ? EXIT.ok : EXIT.issues
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
|
|
356
|
+
process.exitCode = main(process.argv.slice(2))
|
|
357
|
+
}
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
- 可观察的是终端输出、文件内容、退出码和库函数的返回值;内部状态不算可观察行为。
|
|
16
16
|
- 一次只定一个可验收的切片;切片太大就拆成两句行为,各走一遍四步。
|
|
17
17
|
- 失败方式与成功路径同写:非法参数、空输入、越界、文件不存在时,是返回错误值、打印提示,还是抛异常。
|
|
18
|
-
- 新增输出用 `std::format` 组合消息;`std::print` 只在 C++23 且位于程序边界(`apps/`、`main`)时使用,设施选型见 `references/standard-library.md`。
|
|
19
|
-
- 操作与范围按 `references/
|
|
18
|
+
- 新增输出用 `std::format` 组合消息;`std::print` 只在 C++23 且位于程序边界(`apps/`、`main`)时使用,设施选型见 `references/language/standard-library/06-format.md`。
|
|
19
|
+
- 操作与范围按 `references/editing/01-edit-operations.md` 判:本步是写作的入口;改写只在行为被打破时回到这里。
|
|
20
20
|
|
|
21
21
|
## 情况
|
|
22
22
|
|
|
23
23
|
**新功能**:先写最小可观察切片,例如「输入名字,输出一句问候」。交互、存档、排行榜不一次写进第 1 步。
|
|
24
24
|
|
|
25
|
-
**领域规则**:先把动词和名词写成领域值——任务、状态、动作、结果。取值封闭时用 `enum class`;规则有歧义时列出至少两个解释并问用户,不静默选一个。领域值怎么选见 `references/project-design.md`。
|
|
25
|
+
**领域规则**:先把动词和名词写成领域值——任务、状态、动作、结果。取值封闭时用 `enum class`;规则有歧义时列出至少两个解释并问用户,不静默选一个。领域值怎么选见 `references/design/project-design.md`。
|
|
26
26
|
|
|
27
27
|
**修缺陷**:行为句写成「现在观察到 X,期望观察到 Y」,并保留复现输入。修完只验证 X → Y。
|
|
28
28
|
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
|
|
33
33
|
## 正反例指针
|
|
34
34
|
|
|
35
|
-
- 边界怎么切、库和程序怎么分:`examples/
|
|
36
|
-
- 行为句写得太大或混入实现:`examples/
|
|
35
|
+
- 边界怎么切、库和程序怎么分:`references/design/examples/library-boundary.md`。
|
|
36
|
+
- 行为句写得太大或混入实现:`references/design/examples/behavior-sentence.md`。
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## 产出
|
|
4
4
|
|
|
5
|
-
一张边界表:每个组件的职责、哪些头文件是公共接口、库依赖什么、程序依赖什么,以及终端输入输出落在哪一层。目录形态与文件归属以 `references/layout.md` 为准,本步只决定取舍。
|
|
5
|
+
一张边界表:每个组件的职责、哪些头文件是公共接口、库依赖什么、程序依赖什么,以及终端输入输出落在哪一层。目录形态与文件归属以 `references/layout/01-source-layout.md` 为准,本步只决定取舍。
|
|
6
6
|
|
|
7
7
|
```text
|
|
8
8
|
<component> 职责:<一句话> 公共头:<name>.h 依赖:<标准库 / 其他组件>
|
|
@@ -11,29 +11,29 @@ app 职责:参数、输入输出、装配 依赖:<component>
|
|
|
11
11
|
|
|
12
12
|
## 通用情况
|
|
13
13
|
|
|
14
|
-
- 库接收值、返回值,在无法完成任务时抛异常;终端、GUI、网络脚本与全局单例留在程序层。返回值与异常的取舍见 `references/exceptions.md`。
|
|
14
|
+
- 库接收值、返回值,在无法完成任务时抛异常;终端、GUI、网络脚本与全局单例留在程序层。返回值与异常的取舍见 `references/exceptions/throw-rules.md`。
|
|
15
15
|
- 程序链接库,负责读参数、读标准输入、打印结果,并把退出码翻译成进程退出码。
|
|
16
16
|
- 依赖方向从程序指向库;组件之间只经由公共头互相依赖。
|
|
17
|
-
- 公共头放在组件的 `include/` 下,包含时写裸文件名;搜索路径由 target 的 `target_include_directories(... PUBLIC .../include)` 提供,写法见 `
|
|
18
|
-
- 测试放 `tests
|
|
17
|
+
- 公共头放在组件的 `include/` 下,包含时写裸文件名;搜索路径由 target 的 `target_include_directories(... PUBLIC .../include)` 提供,写法见 `engineering` 的 `cmake/targets/rules.md`。
|
|
18
|
+
- 测试放 `tests/`,链接被测库,从公共头进入;文件命名 `<被测>_test.cpp`,一个文件对应一个行为组,测试名描述行为,不复制实现、不依赖执行顺序、不读写固定绝对路径。
|
|
19
19
|
- 只有一个可执行文件、没有复用需求时不分库;出现第二个真实调用方,或测试需要直接调用时再拆。
|
|
20
20
|
- 抽共享抽象前先确认第二个真实使用方和相同语义;抽象理由写进项目技能或设计注释。
|
|
21
|
-
- 本步只决定边界:`CMakeLists.txt` 与 target 图由 `
|
|
22
|
-
- 一个查询该成为成员还是同命名空间的函数,按 `references/project-design.md` 的「谁回答这个问题」判:掌握数据的一方回答。
|
|
21
|
+
- 本步只决定边界:`CMakeLists.txt` 与 target 图由 `engineering` 的 `steps/01-targets.md` 写,构建与工具配置由 `building-cpp` 定。
|
|
22
|
+
- 一个查询该成为成员还是同命名空间的函数,按 `references/design/project-design.md` 的「谁回答这个问题」判:掌握数据的一方回答。
|
|
23
23
|
|
|
24
24
|
## 情况
|
|
25
25
|
|
|
26
26
|
**单文件程序**:`src/main.cpp` 可以有 `main()` 和私有辅助函数;辅助逻辑需要测试时,把它移到库组件。
|
|
27
27
|
|
|
28
|
-
**领域核心 +
|
|
28
|
+
**领域核心 + 外围**:规则核心(状态、合法性、结果)只依赖标准和领域类型;核心不读取当前时间、随机数种子、环境变量或工作目录,这些由程序层注入;终端、GUI、Web、脚本都在适配层,依赖方向从外向内;调用方不该替换的策略不放进公共参数。
|
|
29
29
|
|
|
30
30
|
**库 + 程序**:库放在 `<component>/`,程序放在 `app/` 或根 `src/main.cpp`;程序只包含库的公共头,实现细节留在 `.cpp` 或私有头。
|
|
31
31
|
|
|
32
|
-
**静态库 / 动态库**:接口相同,差别在链接方式和运行时库路径;都遵守「库不依赖终端」。选哪种由 `
|
|
32
|
+
**静态库 / 动态库**:接口相同,差别在链接方式和运行时库路径;都遵守「库不依赖终端」。选哪种由 `engineering` 的定 target 步骤决定,运行时路径写法见其 `cmake/targets/rules.md`。
|
|
33
33
|
|
|
34
34
|
**测试**:测试通过公共头调用库;需要测私有逻辑时,先判断它是否应该成为公共行为。
|
|
35
35
|
|
|
36
36
|
## 正反例指针
|
|
37
37
|
|
|
38
|
-
- 终端输出被塞进库、程序反向依赖:`examples/
|
|
39
|
-
- 公共头放在 `src/`、测试被迫包含私有头:`examples/
|
|
38
|
+
- 终端输出被塞进库、程序反向依赖:`references/design/examples/library-boundary.md`。
|
|
39
|
+
- 公共头放在 `src/`、测试被迫包含私有头:`references/design/examples/header-place.md`。
|
|
@@ -7,16 +7,16 @@
|
|
|
7
7
|
## 通用情况
|
|
8
8
|
|
|
9
9
|
- 语言标准由项目声明决定,新工程默认 C++23(C++11–C++23);`environment.json` 的 `standard` 是唯一来源。
|
|
10
|
-
- 文件槽位、包含守卫与扩展名以 `references/file-template.md` 为准;本步只决定声明写什么。
|
|
11
|
-
- 命名以 `references/naming.md`
|
|
12
|
-
- 声明顺序以 `references/declaration-order.md` 为准:类型 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 →
|
|
13
|
-
-
|
|
14
|
-
- 单参数构造函数写 `explicit`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,标注位置与 `friend` 的例外见 `references/types.md
|
|
15
|
-
- 头文件只就地定义约 10 行以内的短函数;模板与 `constexpr` 例外,其余函数体在 `.cpp
|
|
16
|
-
- 失败是调用方的正常结果时,用结果类型、`bool` + 输出参数或 `std::optional` 表达;函数无法完成自己的任务、或构造函数无法建立不变量时才抛,异常类型的选择与消息见 `references/exceptions.md
|
|
17
|
-
- 一次操作有多种必须区分的正常结果时用 `enum class` 返回(如落子:成功、越界、类型非法、已占),不用 `bool` 把原因压掉;成功还要附带数据时再考虑 `std::expected` 或结果结构。纯查询谓词仍用 `bool
|
|
18
|
-
- 声明注释按 `references/comments.md`
|
|
19
|
-
- 自定义类型的 `std::formatter` 特化放在类型头文件;`parse` 的默认格式限制、`format` 的复用与非法值回退见 `references/standard-library.md` 的「自定义类型的格式化」。
|
|
10
|
+
- 文件槽位、包含守卫与扩展名以 `references/layout/02-file-template.md` 为准;本步只决定声明写什么。
|
|
11
|
+
- 命名以 `references/naming/identifier-names.md` 为准:规则表、语义角色、谓词与查询命名都在那里;字符集、保留标识符与不向 `std` 添加声明见同文件的「字符集与保留标识符」。
|
|
12
|
+
- 声明顺序以 `references/layout/03-declaration-order.md` 为准:类型 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 → 数据成员。组内次序也按同一份的「其他函数的组内次序」:`operator==` 开头、`Get` 按数据成员顺序、先 `ClearSquare` 后 `Clear`;声明区空行按「空行」表,组间 1 行、数据成员间 0 行、`private:` 前 1 行、连续两空行是缺陷。数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序;先声明先初始化,初始化器只能安全使用已经完成初始化的成员。
|
|
13
|
+
- 公共头自包含:自己用到的类型、宏和标准库头都直接包含。验证方式:建一个只包含该头的空 `.cpp` 编译一次,不通过就补 include;五条自检见 `references/layout/02-file-template.md` 的「头文件自包含」。
|
|
14
|
+
- 单参数构造函数写 `explicit`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`;默认成员初始化写在声明处。默认用花括号初始化:成员声明处一律写 `{}`,只有拷贝初始化或 `auto` 推导时用 `=`,不用 `()` 初始化普通对象。特殊成员(含 `= default`)按声明顺序放在成员组最前,定义位置见 `references/layout/03-declaration-order.md`。
|
|
15
|
+
- 头文件只就地定义约 10 行以内的短函数;模板与 `constexpr` 例外,其余函数体在 `.cpp`。头文件里不写 `using namespace`、非 `inline` 普通全局变量、匿名命名空间或 `static` 定义。
|
|
16
|
+
- 失败是调用方的正常结果时,用结果类型、`bool` + 输出参数或 `std::optional` 表达;函数无法完成自己的任务、或构造函数无法建立不变量时才抛,异常类型的选择与消息见 `references/exceptions/throw-rules.md`。析构与 `swap` 标 `noexcept`,异常类型的拷贝/移动不抛;「拒绝」与「抛」并存时在公共头各写一行。
|
|
17
|
+
- 一次操作有多种必须区分的正常结果时用 `enum class` 返回(如落子:成功、越界、类型非法、已占),不用 `bool` 把原因压掉;成功还要附带数据时再考虑 `std::expected` 或结果结构。纯查询谓词仍用 `bool`。枚举项逐个写值,状态与结果枚举把 0 留给正常态(空、进行中或成功),失败态按检查顺序随后;身份枚举按领域编号(如 X=1、O=2),不套用 0 = 正常态;封闭状态码的枚举默认写底层类型 `: std::uint8_t`,普通枚举用默认底层类型;都不靠第一项默认为 0(见 `references/language/value-types/04-enum.md`);不要用 `bool` 开关参数承载两种以上行为(见 `references/language/value-types/10-float-bool.md`)。
|
|
18
|
+
- 声明注释按 `references/comments/comment-rules.md`(内容)与 `comment-style.md`(结构、版式、措辞)写:类型注释、特殊成员、函数注释、数据成员。本步只决定「写哪些事实」:结果、失败方式、检查项、参数与返回值契约;句式、标签、宽度和示例由后两份定,行宽跟 `.clang-format` 的 `ColumnLimit`(默认 80,中文按两列),枚举项与 `struct` 短字段的行尾注释用 `///<` 并与声明对齐。判不准用词时再开 `references/comments/glossary/comment-terms.md`,它只列词表,不重复句式。私有函数同样写声明契约,不能默认调用者知道前置条件;输出参数标 `[out]`,既读又写标 `[in,out]`。
|
|
19
|
+
- 自定义类型的 `std::formatter` 特化放在类型头文件;`parse` 的默认格式限制、`format` 的复用与非法值回退见 `references/language/standard-library/06-format.md` 的「自定义类型的格式化」。
|
|
20
20
|
- 组合消息用 `std::format`;声明里只承诺 `std::format`,`std::print` 是 C++23 的程序边界设施。
|
|
21
21
|
|
|
22
22
|
## 情况
|
|
@@ -25,9 +25,11 @@
|
|
|
25
25
|
|
|
26
26
|
**改公共接口**:先找所有调用方;改签名、改返回类型、改异常声明都算破坏性变更。能加默认参数或重载时优先加,不静默改变旧调用的含义。
|
|
27
27
|
|
|
28
|
+
**改名**:改名是一次独立改动,先搜全部调用点,再改声明、定义、测试和注释;不与改行为混,改完测试保持绿色;公共 API 改名按破坏性变更处理。
|
|
29
|
+
|
|
28
30
|
**异常声明**:构造函数、`Get` 类查询、解析函数最容易抛;在声明注释里写「当……时抛出 `std::out_of_range`」这样的具体类型,不给每个参数错误新建类型。
|
|
29
31
|
|
|
30
|
-
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments.md`。写完运行:
|
|
32
|
+
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments/comment-rules.md` 与 `comment-style.md`。写完运行:
|
|
31
33
|
|
|
32
34
|
```bash
|
|
33
35
|
node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>
|
|
@@ -37,6 +39,6 @@ node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>
|
|
|
37
39
|
|
|
38
40
|
## 正反例指针
|
|
39
41
|
|
|
40
|
-
- 命名、访问器、语义角色:`
|
|
41
|
-
- 异常声明与返回值选择:`
|
|
42
|
-
- 声明注释写成了实现说明:`examples/
|
|
42
|
+
- 命名、访问器、语义角色:`references/naming/examples/`。
|
|
43
|
+
- 异常声明与返回值选择:`references/exceptions/examples/`。
|
|
44
|
+
- 声明注释写成了实现说明:`references/comments/examples/declaration-function.md`。
|