@chengzhao-dev/dsh-cpp-workflow 0.6.20 → 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 +10 -25
- package/README.md +12 -27
- package/cordis.patch.yml +1 -1
- package/lib/client.js +33 -21
- package/lib/comment-policy.js +15 -6
- package/lib/workspace.js +0 -15
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +23 -23
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +6 -0
- package/skills/building-cpp/project/vscode/02-settings.md +7 -1
- package/skills/building-cpp/references/build-script.md +10 -3
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/steps/01-build.md +4 -4
- package/skills/building-cpp/steps/02-tooling.md +10 -10
- package/skills/building-cpp/steps/03-check.md +13 -11
- package/skills/developing-cpp/SKILL.md +32 -36
- 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/{source/07-comments → references/comments}/examples/declaration-class.md +24 -2
- 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/{source/07-comments → references/comments}/examples/declaration-file.md +3 -3
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-member.md +7 -9
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-predicate.md +2 -1
- 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/glossary/comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
- package/skills/developing-cpp/{source/03-design → references/design}/examples/preview-read.md +7 -7
- package/skills/developing-cpp/{source/03-design/01-project-design.md → references/design/project-design.md} +57 -5
- package/skills/developing-cpp/{source/01-editing → references/editing}/01-edit-operations.md +4 -2
- package/skills/developing-cpp/{source/01-editing → references/editing}/02-rule-priority.md +2 -2
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-build-mode.md +2 -2
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-message.md +2 -3
- package/skills/developing-cpp/{source/08-exceptions/01-throw-rules.md → references/exceptions/throw-rules.md} +74 -21
- package/skills/developing-cpp/{source/05-language → references/language}/glossary/language-terms.md +2 -2
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/01-objects.md +7 -1
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/02-initialization.md +8 -1
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/03-deduction.md +6 -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/{source/05-language/01-language-basics → references/language/language-basics}/06-functions.md +7 -1
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/07-classes.md +7 -1
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/09-templates.md +6 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/10-checklist.md +2 -1
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/02-complexity.md +6 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/03-memory.md +7 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/04-undefined-behavior.md +9 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/05-pitfalls.md +6 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/01-containers.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/02-access.md +7 -1
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/03-iteration.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/04-algorithms.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/05-string.md +6 -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/{source/05-language/02-value-types → references/language/value-types}/01-width.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/02-signed.md +7 -1
- 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/{source/05-language/02-value-types → references/language/value-types}/06-const-method.md +5 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/07-nodiscard.md +6 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/09-subscript.md +2 -1
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/10-float-bool.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/11-checklist.md +1 -1
- package/skills/developing-cpp/{source/04-layout → references/layout}/01-source-layout.md +8 -2
- package/skills/developing-cpp/{source/04-layout → references/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-basic.md +3 -0
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lower-layer.md +9 -9
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/{source/06-naming/01-identifier-names.md → references/naming/identifier-names.md} +34 -8
- package/skills/developing-cpp/{source/02-style/01-google-cpp-style.md → references/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 +9 -9
- 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 +41 -38
- package/skills/engineering/{cmake → references/cmake}/assets/CompileSettings.cmake +2 -0
- package/skills/engineering/{cmake → references/cmake}/linking/01-libraries.md +6 -0
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/{cmake → references/cmake}/linking/03-undefined-reference.md +6 -0
- package/skills/engineering/{cmake → references/cmake}/linking/04-inspect.md +6 -0
- package/skills/engineering/{cmake/02-modules/01-functions.md → references/cmake/modules/functions.md} +8 -5
- package/skills/engineering/{cmake/03-standard/01-cxx-standard.md → references/cmake/standard/cxx-standard.md} +5 -4
- package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/README.md +8 -8
- package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/target.md +1 -1
- package/skills/engineering/{cmake/01-targets/01-rules.md → references/cmake/targets/rules.md} +18 -11
- package/skills/engineering/{cmake → references/cmake}/testing/ctest.md +9 -2
- package/skills/engineering/{formatting → references/formatting}/01-encoding.md +1 -1
- package/skills/engineering/{formatting → references/formatting}/02-clang-format.md +6 -0
- package/skills/engineering/{formatting → references/formatting}/03-clang-tidy.md +7 -0
- package/skills/engineering/{formatting → references/formatting}/04-clangd.md +7 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/{formatting → references/formatting}/examples/clang-tidy.md +0 -21
- package/skills/engineering/{cmake/steps → steps}/01-targets.md +6 -6
- package/skills/engineering/{cmake/steps → steps}/02-modules.md +4 -4
- package/skills/engineering/{cmake/steps → steps}/03-check.md +3 -2
- package/skills/developing-cpp/source/03-design/examples/move-result.md +0 -39
- package/skills/developing-cpp/source/04-layout/03-declaration-order.md +0 -118
- package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +0 -15
- package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +0 -8
- package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +0 -9
- package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +0 -23
- package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +0 -17
- package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +0 -4
- package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +0 -44
- package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +0 -4
- package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +0 -15
- package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +0 -6
- package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +0 -17
- package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +0 -57
- package/skills/developing-cpp/source/07-comments/01-comment-rules.md +0 -239
- package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +0 -22
- package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +0 -24
- package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +0 -100
- package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +0 -18
- package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +0 -42
- package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +0 -75
- package/skills/engineering/cmake/linking/02-rpath.md +0 -17
- package/skills/engineering/formatting/examples/clang-format.md +0 -17
- /package/skills/developing-cpp/{source/07-comments → references/comments}/examples/implementation-todo.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/behavior-sentence.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/coordinate-table.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/header-place.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-boundary.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-split.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-boundary.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-catch.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-destructor.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-format.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-when.md +0 -0
- /package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/07-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/10-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/05-strong-type.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-accessor.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-current-prefix.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lookup.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-multiple-return.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-predicate.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-rename.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-responsibility.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-role.md +0 -0
- /package/skills/engineering/{cmake → references/cmake}/assets/LinkSettings.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/examples/standard.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++11/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++14/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++17/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++20/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++23/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/glob.md +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/rpath.md +0 -0
- /package/skills/engineering/{formatting → references/formatting}/examples/clangd.md +0 -0
- /package/skills/engineering/{formatting → references/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -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`)时使用,设施选型见 `
|
|
19
|
-
- 操作与范围按 `
|
|
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`;规则有歧义时列出至少两个解释并问用户,不静默选一个。领域值怎么选见 `
|
|
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
|
-
- 边界怎么切、库和程序怎么分:`
|
|
36
|
-
- 行为句写得太大或混入实现:`
|
|
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
|
-
一张边界表:每个组件的职责、哪些头文件是公共接口、库依赖什么、程序依赖什么,以及终端输入输出落在哪一层。目录形态与文件归属以 `
|
|
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、网络脚本与全局单例留在程序层。返回值与异常的取舍见 `
|
|
14
|
+
- 库接收值、返回值,在无法完成任务时抛异常;终端、GUI、网络脚本与全局单例留在程序层。返回值与异常的取舍见 `references/exceptions/throw-rules.md`。
|
|
15
15
|
- 程序链接库,负责读参数、读标准输入、打印结果,并把退出码翻译成进程退出码。
|
|
16
16
|
- 依赖方向从程序指向库;组件之间只经由公共头互相依赖。
|
|
17
|
-
- 公共头放在组件的 `include/` 下,包含时写裸文件名;搜索路径由 target 的 `target_include_directories(... PUBLIC .../include)` 提供,写法见 `engineering` 的 `cmake/
|
|
18
|
-
- 测试放 `tests
|
|
17
|
+
- 公共头放在组件的 `include/` 下,包含时写裸文件名;搜索路径由 target 的 `target_include_directories(... PUBLIC .../include)` 提供,写法见 `engineering` 的 `cmake/targets/rules.md`。
|
|
18
|
+
- 测试放 `tests/`,链接被测库,从公共头进入;文件命名 `<被测>_test.cpp`,一个文件对应一个行为组,测试名描述行为,不复制实现、不依赖执行顺序、不读写固定绝对路径。
|
|
19
19
|
- 只有一个可执行文件、没有复用需求时不分库;出现第二个真实调用方,或测试需要直接调用时再拆。
|
|
20
20
|
- 抽共享抽象前先确认第二个真实使用方和相同语义;抽象理由写进项目技能或设计注释。
|
|
21
21
|
- 本步只决定边界:`CMakeLists.txt` 与 target 图由 `engineering` 的 `steps/01-targets.md` 写,构建与工具配置由 `building-cpp` 定。
|
|
22
|
-
- 一个查询该成为成员还是同命名空间的函数,按 `
|
|
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
|
-
**静态库 / 动态库**:接口相同,差别在链接方式和运行时库路径;都遵守「库不依赖终端」。选哪种由 `engineering` 的定 target 步骤决定,运行时路径写法见其 `cmake/
|
|
32
|
+
**静态库 / 动态库**:接口相同,差别在链接方式和运行时库路径;都遵守「库不依赖终端」。选哪种由 `engineering` 的定 target 步骤决定,运行时路径写法见其 `cmake/targets/rules.md`。
|
|
33
33
|
|
|
34
34
|
**测试**:测试通过公共头调用库;需要测私有逻辑时,先判断它是否应该成为公共行为。
|
|
35
35
|
|
|
36
36
|
## 正反例指针
|
|
37
37
|
|
|
38
|
-
- 终端输出被塞进库、程序反向依赖:`
|
|
39
|
-
- 公共头放在 `src/`、测试被迫包含私有头:`
|
|
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
|
-
- 文件槽位、包含守卫与扩展名以 `
|
|
11
|
-
- 命名以 `
|
|
12
|
-
- 声明顺序以 `
|
|
13
|
-
-
|
|
14
|
-
- 单参数构造函数写 `explicit`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,标注位置与 `friend` 的例外见 `
|
|
15
|
-
- 头文件只就地定义约 10 行以内的短函数;模板与 `constexpr` 例外,其余函数体在 `.cpp
|
|
16
|
-
- 失败是调用方的正常结果时,用结果类型、`bool` + 输出参数或 `std::optional` 表达;函数无法完成自己的任务、或构造函数无法建立不变量时才抛,异常类型的选择与消息见 `
|
|
17
|
-
- 一次操作有多种必须区分的正常结果时用 `enum class` 返回(如落子:成功、越界、类型非法、已占),不用 `bool` 把原因压掉;成功还要附带数据时再考虑 `std::expected` 或结果结构。纯查询谓词仍用 `bool
|
|
18
|
-
- 声明注释按 `
|
|
19
|
-
- 自定义类型的 `std::formatter` 特化放在类型头文件;`parse` 的默认格式限制、`format` 的复用与非法值回退见 `
|
|
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`。文件头、标签、参数完整性与设计论证的规则见 `
|
|
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
|
-
- 声明注释写成了实现说明:`
|
|
42
|
+
- 命名、访问器、语义角色:`references/naming/examples/`。
|
|
43
|
+
- 异常声明与返回值选择:`references/exceptions/examples/`。
|
|
44
|
+
- 声明注释写成了实现说明:`references/comments/examples/declaration-function.md`。
|
|
@@ -7,19 +7,22 @@
|
|
|
7
7
|
## 通用情况
|
|
8
8
|
|
|
9
9
|
- 函数保持短小,一个函数只做一件事。超过约 40 行时先找可以命名的中间步骤,不靠注释把长函数切成几段。
|
|
10
|
-
- 按「准备 → 操作 →
|
|
11
|
-
- `.cpp`
|
|
12
|
-
-
|
|
10
|
+
- 按「准备 → 操作 → 收尾」排列语句,用空行分组;组与组之间的空行表达阶段,同一组内的守卫与结果处理不插空行。
|
|
11
|
+
- `.cpp` 里的定义顺序跟配对头文件:先类型与常量,再自由函数,再成员函数;成员函数按类内声明顺序。头文件把构造函数、析构函数、拷贝/移动声明在成员组最前时,定义也放最前;如果头文件把它们声明在别的成员之后,仍按头文件顺序,不硬提。不为先编译重排,也不按调用频率重排。`.cpp` 里不需要外部引用的 `constexpr` 放具名命名空间,仅一个函数使用时才用函数内 `static constexpr`;函数与类型放唯一的匿名命名空间,公开函数定义放在其后。见 `references/layout/03-declaration-order.md`「`.cpp` 的定义顺序」与 `references/style/google-cpp-style.md`。
|
|
12
|
+
- 同一文件里的函数按同一套分段约定:需要分阶段的非单表达式函数按空行分段、每段上方一句;读入、单值转换、一行输出这类一条直路的短函数不加分段注释;不能结构相近的函数一半分段、一半完全不留注释。阶段怎么分见 `references/layout/03-declaration-order.md`「函数体内的空行」。
|
|
13
|
+
- `.cpp` 的第一个 include 是自己的配对头,然后直接包含本文件用到的标准库头,不依赖公共头转发声明。包含顺序见 `references/style/google-cpp-style.md`,文件槽位见 `references/layout/02-file-template.md`。
|
|
14
|
+
- 函数体注释用 `//`,不用 `///`:体内 `///` 会接到该函数的 Doxygen 详细说明后面。重要函数按空行分阶段,每个阶段上方写一句 `//`,写这一支的后果,可以点明处理了哪些枚举值,不逐词翻译下一行的条件;`//` 里的枚举值不加反引号。单表达式函数(如 `IsFull`)不配注释;关键局部表(查表、分派表)上方写一句,说明表的角色与分组。只写删掉会让下一次修改得到错误结果、而代码本身看不出的内容;变更史留给提交说明。`.cpp` 的自由函数在定义上一行用 `///` 写一句职责,与配对头文件的声明注释一致;类方法定义不重复声明处已有的事实。
|
|
13
15
|
- 什么时候在 `if` 上方加一句:多条独立拒绝(如 `CheckPlaceable`)每个 `if` 一句,不同条件的 `if` 不合并成一句;成功写入前一句;只有次序依赖的一对 `if` 在第一个上方说明;循环上方已有阶段说明时,循环里的 `if` 不再加;单表达式谓词不加。
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
+
- `switch` 覆盖枚举全部取值时不写 `default`,新增取值交给 `-Wswitch` 报错;编译器需要出口时在 `switch` 后写 `std::unreachable()`,不写 `throw` 兜底。见 `references/language/language-basics/05-expressions.md`。
|
|
17
|
+
- 失败是调用方正常结果时返回结果类型、`bool` 或 `std::optional`;函数无法完成自己的任务时抛异常,不拿异常做正常分支。Debug 与 Release 用同一套返回值与抛出条件,只有诊断详略不同。两种模式都要守的不变量用 `Check`(项目保留时),只在开发期做的自检用 `DebugCheck`;项目没有这两个宏时分别用 `assert` 与 `std::terminate`。`Check` 不是 `[[noreturn]]`,条件成立时正常返回;`DebugCheck` 的参数在 Release 不求值,不能放入有副作用的表达式。捕获时按 `const` 引用,只处理能处理的类型,其余继续抛。异常类型、消息格式与「不抛的边界」见 `references/exceptions/throw-rules.md`。
|
|
18
|
+
- 参数校验、调用方状态检查与正常结果返回的次序见 `references/exceptions/throw-rules.md` 的「检查次序」;本层的抛出检查先于 `kDenied` 之类的正常失败,下层检查不重复。改完 `throw` 再走同文件的「检查清单」:异常类型、消息标签、`std::format` 直接进 `throw`、不吞异常。
|
|
16
19
|
- 声明 `noexcept` 前核对函数体:出现 `at()` 或其他可能抛出的调用就去掉。越界用 `at()` 抛 `std::out_of_range`,这个函数就不能标 `noexcept`。普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;它留给移动、交换、析构,以及已经标上的纯谓词。
|
|
17
20
|
- 组合输出用 `std::format`,再写入 `std::cout` / `std::cerr`;库实现里不直接打印。C++23 且库支持时程序边界可用 `std::print`,C++20 及更早没有。
|
|
18
21
|
- 转换用具名转换 `static_cast` / `std::bit_cast`,不用 C 风格强制转换。
|
|
19
|
-
- 所有权用 `std::unique_ptr
|
|
22
|
+
- 所有权用 `std::unique_ptr`;禁止裸 `new` / `delete`,没有共享所有权的理由时不用 `std::shared_ptr`。
|
|
20
23
|
- 只改注释时,不改可观察行为;只改实现时,不改公共声明。两者同时需要时,先改声明,再改实现。
|
|
21
|
-
- 清空成员数组:元素类型提供收回空取值的函数时(如 `
|
|
22
|
-
- 默认构造只做「建立初值」时用成员初值(如 `
|
|
24
|
+
- 清空成员数组:元素类型提供收回空取值的函数时(如 `Square::Clear`),容器的 `Clear` 逐个调用它;没有这种函数时才用 `std::ranges::fill(squares_ | std::views::join, SquareValue::kEmpty)` 或 `for` 循环逐格赋空值。两种写法都不直接写 `squares_ = {}`,也不从外部把枚举赋成空值。撤销只回退受影响的那一处,不清空整个容器再重放全部历史,见 `references/design/project-design.md` 的「撤销与回退」。
|
|
25
|
+
- 默认构造只做「建立初值」时用成员初值(如 `squares_{}`),不在构造函数里再调一遍 `Clear()`;`Clear()` 只负责把已经写入的值清空为 `kEmpty`。
|
|
23
26
|
|
|
24
27
|
## 情况
|
|
25
28
|
|
|
@@ -29,18 +32,19 @@
|
|
|
29
32
|
|
|
30
33
|
**重构**:一次只动一种东西(改名、抽函数、换容器),每步都让测试保持绿色,行为句不变。
|
|
31
34
|
|
|
32
|
-
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format("错误: {}", error.what())`
|
|
35
|
+
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format("错误: {}", error.what())` 打印,返回项目约定的非零退出码。`catch` 子句按派生到基类、具体到通用排序,`catch (...)` 只能放最后,且不要静默返回。不吞掉异常后继续使用无效状态。
|
|
33
36
|
|
|
34
37
|
## 交回构建
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
实现改完,先对照配对头文件自检:每个声明都有定义,定义顺序与声明顺序一致;新增或改动的函数按同一套分段约定,需要分阶段的都有阶段注释与空行;只改过语言细节时再过对应目录的完成清单(`references/language/value-types/11-checklist.md`、`language-basics/10-checklist.md`、`standard-library/10-checklist.md`、`performance/07-checklist.md`);行宽与缩进以工程根 `.clang-format` 为准,注释版式见 `references/comments/comment-style.md`。再跑 `node <skill-dir>/scripts/checking-declaration-order.mjs --root .`(`<skill-dir>` 换成插件 `developing-cpp` 的实际目录),有乱序先修再交回下游:需要新增或移动源文件时,先由 `engineering` 的 `steps/01-targets.md` 把文件显式写进 target;target 齐了再由 `building-cpp` 的 `steps/01-build.md` configure、构建,最后走 `steps/03-check.md`。不要只改完函数体就宣称完成。
|
|
37
40
|
|
|
38
41
|
## 正反例指针
|
|
39
42
|
|
|
40
|
-
- 实现注释复述代码、分组混乱:`
|
|
41
|
-
- 已经证明非负的坐标把下标收成 `std::size_t` 再 `[]`,不转成 `int`:`
|
|
42
|
-
- 越界消息的标签取自调用点:`
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
-
|
|
43
|
+
- 实现注释复述代码、分组混乱:`references/comments/examples/implementation-body.md`。
|
|
44
|
+
- 已经证明非负的坐标把下标收成 `std::size_t` 再 `[]`,不转成 `int`:`references/language/value-types/09-subscript.md`。
|
|
45
|
+
- 越界消息的标签取自调用点:`references/exceptions/throw-rules.md`「消息」的 `std::source_location` 用法。
|
|
46
|
+
- 多个入口重复同一范围检查:抽私有函数,函数名显式传入、`source_location` 取调用点,见 `references/exceptions/throw-rules.md`「重复的入口检查」。
|
|
47
|
+
- 为了保住 `noexcept` 再包一层只转发下标的私有函数:`references/exceptions/throw-rules.md`「不抛的边界」。
|
|
48
|
+
- 用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的落子结果,或丢掉调用方给的一手、在 `Game` 内重新拼装:`references/design/examples/move-result.md`。
|
|
49
|
+
- 异常类型与结果类型混用:`references/exceptions/examples/throw-when.md`。
|
|
50
|
+
- `std::format` 与流输出混用得当:`references/exceptions/examples/throw-message.md`。
|