@chengzhao-dev/dsh-cpp-workflow 0.7.10 → 0.7.12
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 +3 -3
- package/README.md +3 -3
- package/lib/client.js +12 -11
- package/lib/comment-policy.js +12 -11
- package/lib/workspace.js +0 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -4
- package/skills/building-cpp/SKILL.md +2 -2
- package/skills/building-cpp/assets/config/.clang-tidy +4 -5
- package/skills/building-cpp/assets/config/.clangd +5 -7
- package/skills/building-cpp/assets/config/.vscode/settings.json +4 -6
- package/skills/building-cpp/references/01-build-script.md +2 -2
- package/skills/building-cpp/references/02-environment.md +4 -4
- package/skills/building-cpp/steps/01-build.md +1 -1
- package/skills/building-cpp/steps/02-tooling.md +3 -3
- package/skills/building-cpp/steps/03-check.md +2 -2
- package/skills/developing-cpp/SKILL.md +1 -1
- package/skills/developing-cpp/references/comments/01-comment-rules.md +39 -38
- package/skills/developing-cpp/references/comments/02-style.md +34 -20
- package/skills/developing-cpp/references/comments/examples/01-declaration-class.md +53 -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 +19 -9
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +22 -16
- package/skills/developing-cpp/references/comments/examples/06-declaration-member.md +2 -2
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +5 -5
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +9 -9
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +24 -23
- package/skills/developing-cpp/references/design/01-project-design.md +6 -6
- package/skills/developing-cpp/references/design/02-raii.md +2 -2
- package/skills/developing-cpp/references/design/examples/06-move-result.md +7 -6
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +12 -11
- package/skills/developing-cpp/references/design/examples/08-screen-enter-exit.md +2 -1
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +33 -33
- package/skills/developing-cpp/references/exceptions/examples/02-throw-build-mode.md +8 -7
- package/skills/developing-cpp/references/exceptions/examples/04-throw-destructor.md +6 -2
- package/skills/developing-cpp/references/index.md +22 -22
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +20 -5
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/10-language-checklist.md +3 -2
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +10 -9
- package/skills/developing-cpp/references/language/standard-library/07-format-ecosystem.md +2 -2
- package/skills/developing-cpp/references/language/standard-library/08-time.md +1 -1
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +13 -1
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +3 -5
- package/skills/developing-cpp/references/layout/03-declaration-order.md +38 -11
- package/skills/developing-cpp/references/naming/01-identifier-names.md +17 -16
- package/skills/developing-cpp/references/naming/examples/06-naming-lower-layer.md +1 -1
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +6 -5
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +1 -1
- package/skills/developing-cpp/references/naming/glossary/02-position.md +9 -9
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +1 -1
- package/skills/developing-cpp/references/naming/glossary/04-square.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +1 -1
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +1 -1
- package/skills/developing-cpp/references/naming/glossary/07-stream.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +4 -4
- package/skills/developing-cpp/scripts/checking-comments.mjs +66 -11
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +24 -27
- package/skills/developing-cpp/steps/03-declaration.md +6 -6
- package/skills/developing-cpp/steps/04-implementation.md +7 -7
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +1 -1
- package/skills/engineering/references/cmake/linking/02-rpath.md +1 -1
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +5 -5
- package/skills/engineering/references/cmake/standards/index.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +4 -4
- package/skills/engineering/references/formatting/01-encoding.md +3 -3
- package/skills/engineering/references/formatting/02-clang-format.md +1 -1
- package/skills/engineering/references/formatting/03-clang-tidy.md +3 -3
- package/skills/engineering/references/formatting/04-clangd.md +1 -1
- package/skills/engineering/references/formatting/05-config-comments.md +1 -1
- package/skills/engineering/steps/01-targets.md +1 -1
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
// 检查项:Doxygen 文件头、注释行宽度(含行首缩进)、列表缩进、标签、标签间空行、注释与声明的贴合、
|
|
5
5
|
// 成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param 完整性、@return 完整性、
|
|
6
6
|
// @retval 顺序、特殊成员首句、函数体内 ///、摘要裸参数名、枚举成员注释一致性、枚举行尾句式(不收句号)、
|
|
7
|
-
// 数据成员主注释位置、控制序列常量注释、定义处重复声明注释、只在 .cpp
|
|
7
|
+
// 数据成员主注释位置、控制序列常量注释、定义处重复声明注释、只在 .cpp 出现的函数契约;
|
|
8
|
+
// 另查实现注释复述下一行声明、指向别的头文件或目录、注释代替类型、能力边界、枚举行尾空冒号。
|
|
8
9
|
// 不重写任何文件。
|
|
9
10
|
//
|
|
10
11
|
// 用法:
|
|
11
|
-
// node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--json] [--quiet]
|
|
12
|
+
// node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--no-brief] [--no-param] [--no-return] [--allow-tag TAG] [--json] [--quiet] [--help]
|
|
12
13
|
//
|
|
13
14
|
// 退出码:0 无问题,1 发现问题,2 用法或读取错误。
|
|
14
15
|
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
|
@@ -31,6 +32,9 @@ const SKIP_DIR = new Set([
|
|
|
31
32
|
const STATIC_FORBIDDEN_TAGS = ['@returns', '@details', '@since', '@todo']
|
|
32
33
|
const PRIVACY_TAGS = ['@author', '@date', '@copyright', '@version']
|
|
33
34
|
const FORBIDDEN_TAGS = [...STATIC_FORBIDDEN_TAGS, ...PRIVACY_TAGS]
|
|
35
|
+
const HEADER_METADATA_ORDER = ['author', 'version', 'date', 'copyright']
|
|
36
|
+
const HEADER_METADATA = /^@(author|version|date|copyright)\b\s*(.*)$/
|
|
37
|
+
const HEADER_DATE = /^\d{4}-\d{2}-\d{2}$/
|
|
34
38
|
const DOXY_FILE = /^\/\/\/\s*@file\s+\S/
|
|
35
39
|
const DOXY_BRIEF = /^\/\/\/\s*@brief\s+\S/
|
|
36
40
|
const BLOCK_FILE = /^@file\s+\S/
|
|
@@ -57,12 +61,19 @@ const isPredicateFirstOk = (declaration, firstDocLine) =>
|
|
|
57
61
|
: firstDocLine.includes('是否') && !PREDICATE_BANNED_PREFIX.test(firstDocLine)
|
|
58
62
|
const DECL_KEYWORDS = /^(class|struct|union|enum|namespace|using|typedef|template|#|if|else|for|while|switch|return|case|default)\b/
|
|
59
63
|
|
|
64
|
+
const USAGE = '用法:node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--no-brief] [--no-param] [--no-return] [--allow-tag TAG] [--json] [--quiet] [--help]\n'
|
|
65
|
+
|
|
60
66
|
function usage(message) {
|
|
61
67
|
if (message) process.stderr.write(`错误:${message}\n`)
|
|
62
|
-
process.stderr.write(
|
|
68
|
+
process.stderr.write(USAGE)
|
|
63
69
|
return EXIT.usage
|
|
64
70
|
}
|
|
65
71
|
|
|
72
|
+
function help() {
|
|
73
|
+
process.stdout.write(USAGE)
|
|
74
|
+
return EXIT.ok
|
|
75
|
+
}
|
|
76
|
+
|
|
66
77
|
function parseArgs(argv) {
|
|
67
78
|
const options = { root: undefined, width: 120, widthExplicit: false, commentStyle: 'slash', requireHeader: true, requireBrief: true, requireParam: true, requireReturn: true, allowTags: new Set(), json: false, quiet: false }
|
|
68
79
|
for (let index = 0; index < argv.length; index += 1) {
|
|
@@ -169,6 +180,7 @@ const TAG_GROUPS = {
|
|
|
169
180
|
throw: 'throws',
|
|
170
181
|
exception: 'throws',
|
|
171
182
|
note: 'note',
|
|
183
|
+
see: 'note',
|
|
172
184
|
pre: 'pre',
|
|
173
185
|
post: 'pre',
|
|
174
186
|
}
|
|
@@ -508,14 +520,14 @@ function mentionsParam(text, name) {
|
|
|
508
520
|
}
|
|
509
521
|
|
|
510
522
|
/** 标记每一行是否位于 class/struct 体内,供数据成员注释位置检查使用。 */
|
|
511
|
-
function classMemberLines(lines) {
|
|
523
|
+
function classMemberLines(lines, kinds = 'class|struct') {
|
|
512
524
|
const flags = new Array(lines.length).fill(false)
|
|
513
525
|
const starts = []
|
|
514
526
|
let depth = 0
|
|
515
527
|
let pending = false
|
|
516
528
|
for (let index = 0; index < lines.length; index += 1) {
|
|
517
529
|
const code = lines[index].replace(/\/\/.*$/, '').trim()
|
|
518
|
-
if (
|
|
530
|
+
if (new RegExp(`^(?:template\\s*<[^>]*>\\s*)?(?:${kinds})\\b`).test(code)) pending = true
|
|
519
531
|
const opens = (code.match(/\{/g) ?? []).length
|
|
520
532
|
const closes = (code.match(/\}/g) ?? []).length
|
|
521
533
|
if (pending && opens > 0) {
|
|
@@ -608,11 +620,14 @@ function functionDocIssues(declaration, docContents, options = {}) {
|
|
|
608
620
|
const issues = []
|
|
609
621
|
const label = specialMemberLabel(declaration)
|
|
610
622
|
const firstLine = docContents.find((content) => content.trim() !== '')?.trim() ?? ''
|
|
623
|
+
const firstLineText = firstLine.replace(/^@brief\s+/, '')
|
|
624
|
+
const firstLineLabel = firstLineText.replace(/[::。.]$/, '')
|
|
625
|
+
const labelText = label ? label.replace(/[::]$/, '') : ''
|
|
611
626
|
if (label) {
|
|
612
|
-
if (!
|
|
627
|
+
if (!firstLineLabel.startsWith(labelText)) {
|
|
613
628
|
issues.push({ rule: 'special-member-first', message: `特殊成员注释首句要以「${label}」开头:先写这是哪一类构造或析构,再写后置条件` })
|
|
614
629
|
}
|
|
615
|
-
if (SPECIAL_MEMBER_VAGUE.test(
|
|
630
|
+
if (SPECIAL_MEMBER_VAGUE.test(firstLineText)) {
|
|
616
631
|
issues.push({ rule: 'special-member-vague', message: '特殊成员注释只在复述「创建对象」「初始化成员」「建立初始状态」:改成具体的后置条件或职责' })
|
|
617
632
|
}
|
|
618
633
|
return issues
|
|
@@ -657,6 +672,7 @@ export function checkText(text, options = {}) {
|
|
|
657
672
|
const issues = []
|
|
658
673
|
const add = (line, rule, message) => issues.push({ line, rule, message })
|
|
659
674
|
const inClass = classMemberLines(lines)
|
|
675
|
+
const inStruct = classMemberLines(lines, 'struct')
|
|
660
676
|
|
|
661
677
|
const style = options.commentStyle === 'block' ? 'block' : 'slash'
|
|
662
678
|
const firstContent = lines.findIndex((line) => line.trim() !== '')
|
|
@@ -709,6 +725,41 @@ export function checkText(text, options = {}) {
|
|
|
709
725
|
}
|
|
710
726
|
if (/\b(?:app|src|include|tests)\//.test(text)) add(headerLine, 'header-cross-file', '文件头指向了另一个目录:只介绍本文件')
|
|
711
727
|
}
|
|
728
|
+
const metadata = []
|
|
729
|
+
let firstMetadata = -1
|
|
730
|
+
let lastMetadata = -1
|
|
731
|
+
const headerText = (entry) => entry.text.replace(/^\/\/\/\s?/, '').trim()
|
|
732
|
+
for (let index = 0; index < headerLines.length; index += 1) {
|
|
733
|
+
const content = headerText(headerLines[index])
|
|
734
|
+
const match = HEADER_METADATA.exec(content)
|
|
735
|
+
if (!match) continue
|
|
736
|
+
if (firstMetadata === -1) firstMetadata = index
|
|
737
|
+
lastMetadata = index
|
|
738
|
+
metadata.push({ line: headerLines[index].line, tag: match[1], value: match[2].trim() })
|
|
739
|
+
}
|
|
740
|
+
for (let index = 1; index < metadata.length; index += 1) {
|
|
741
|
+
const previous = HEADER_METADATA_ORDER.indexOf(metadata[index - 1].tag)
|
|
742
|
+
if (HEADER_METADATA_ORDER.indexOf(metadata[index].tag) < previous) {
|
|
743
|
+
add(metadata[index].line, 'header-tag-order', '文件头元数据按 @author、@version、@date、@copyright 排列,缺项跳过')
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (firstMetadata !== -1) {
|
|
747
|
+
const before = firstMetadata === 0 ? '' : headerText(headerLines[firstMetadata - 1])
|
|
748
|
+
if (before !== '') {
|
|
749
|
+
add(metadata[0].line, 'header-tag-gap', '元数据组前要空一行注释:@brief 或组成段之后补一行空注释,再写元数据')
|
|
750
|
+
}
|
|
751
|
+
for (let index = firstMetadata; index <= lastMetadata; index += 1) {
|
|
752
|
+
if (!HEADER_METADATA.test(headerText(headerLines[index]))) {
|
|
753
|
+
add(headerLines[index].line, 'header-tag-gap', '元数据组内不得有空行或其他内容:@author、@version、@date、@copyright 连续写')
|
|
754
|
+
break
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
for (const entry of metadata) {
|
|
758
|
+
if (entry.tag === 'date' && !HEADER_DATE.test(entry.value)) {
|
|
759
|
+
add(entry.line, 'header-date', '@date 写创建日期,格式 YYYY-MM-DD,例如 2026-12-01')
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
712
763
|
}
|
|
713
764
|
|
|
714
765
|
for (let index = 0; index < lines.length; index += 1) {
|
|
@@ -721,7 +772,7 @@ export function checkText(text, options = {}) {
|
|
|
721
772
|
add(index + 1, 'list-indent', '注释列表项要在 /// 后空三格再写 -:`/// - `,与总起句拉开层级')
|
|
722
773
|
}
|
|
723
774
|
for (const tag of FORBIDDEN_TAGS) {
|
|
724
|
-
if (options.allowTags?.has(tag.slice(1))) continue
|
|
775
|
+
if (PRIVACY_TAGS.includes(tag) && (index <= doxyHeaderEnd || options.allowTags?.has(tag.slice(1)))) continue
|
|
725
776
|
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
726
777
|
}
|
|
727
778
|
}
|
|
@@ -737,7 +788,7 @@ export function checkText(text, options = {}) {
|
|
|
737
788
|
}
|
|
738
789
|
for (const content of doc.contents) {
|
|
739
790
|
for (const tag of FORBIDDEN_TAGS) {
|
|
740
|
-
if (options.allowTags?.has(tag.slice(1))) continue
|
|
791
|
+
if (PRIVACY_TAGS.includes(tag) && (doc.start === headerBlock?.start || options.allowTags?.has(tag.slice(1)))) continue
|
|
741
792
|
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
742
793
|
}
|
|
743
794
|
}
|
|
@@ -938,7 +989,11 @@ export function checkText(text, options = {}) {
|
|
|
938
989
|
const member = names.at(-1)
|
|
939
990
|
const previous = index > 0 ? lines[index - 1].trim() : ''
|
|
940
991
|
if (commentAt !== -1) {
|
|
941
|
-
if (line.slice(commentAt).startsWith('///<'))
|
|
992
|
+
if (line.slice(commentAt).startsWith('///<')) {
|
|
993
|
+
if (inStruct[index]) continue
|
|
994
|
+
add(index + 1, 'member-comment-trailing', `数据成员 ${member} 只用行尾 ///< 注释:///< 只留给枚举项和 struct 的短字段,类数据成员的主注释写在声明上一行`)
|
|
995
|
+
continue
|
|
996
|
+
}
|
|
942
997
|
if (!previous.startsWith('//')) {
|
|
943
998
|
add(index + 1, 'member-comment-missing', `数据成员 ${member} 只有行尾注释:主注释写在声明上一行,行尾不写补充`)
|
|
944
999
|
} else {
|
|
@@ -1021,7 +1076,7 @@ function main(argv) {
|
|
|
1021
1076
|
} catch (error) {
|
|
1022
1077
|
return usage(error.message)
|
|
1023
1078
|
}
|
|
1024
|
-
if (options.help) return
|
|
1079
|
+
if (options.help) return help()
|
|
1025
1080
|
const root = options.root ? path.resolve(options.root) : process.cwd()
|
|
1026
1081
|
try {
|
|
1027
1082
|
statSync(root)
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
//
|
|
4
4
|
// 规则见 references/layout/03-declaration-order.md「`.cpp` 的定义顺序」与
|
|
5
5
|
// 「声明与定义的签名」:先类型常量、再自由函数、再成员函数;成员按类内声明顺序;
|
|
6
|
-
//
|
|
7
|
-
//
|
|
6
|
+
// 构造函数、析构函数、拷贝与移动通常放在成员组最前(校验对 `Is*` / `Check*` 前置时
|
|
7
|
+
// 例外,见该文档「校验对的位置」);参数类型、限定符与返回类型必须与声明一致;
|
|
8
|
+
// 不为先编译而重排。
|
|
8
9
|
//
|
|
9
10
|
// 本脚本只比较配对头文件里存在的类;自由函数与匿名命名空间不参与。类内没有声明
|
|
10
11
|
// 的成员定义报 definition-undeclared,签名不一致报 definition-signature。
|
|
@@ -28,12 +29,19 @@ const SKIP_DIR = new Set([
|
|
|
28
29
|
])
|
|
29
30
|
const PREDICATE_NAME = /^(?:Is|Has|Can|Completes|Would)[A-Za-z0-9_]*$/
|
|
30
31
|
|
|
32
|
+
const USAGE = '用法:node checking-declaration-order.mjs [--root DIR] [--json] [--quiet] [--help]\n'
|
|
33
|
+
|
|
31
34
|
function usage(message) {
|
|
32
35
|
if (message) process.stderr.write(`错误:${message}\n`)
|
|
33
|
-
process.stderr.write(
|
|
36
|
+
process.stderr.write(USAGE)
|
|
34
37
|
return EXIT.usage
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
function help() {
|
|
41
|
+
process.stdout.write(USAGE)
|
|
42
|
+
return EXIT.ok
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
function parseArgs(argv) {
|
|
38
46
|
const options = { root: undefined, json: false, quiet: false, help: false }
|
|
39
47
|
for (let index = 0; index < argv.length; index += 1) {
|
|
@@ -429,29 +437,17 @@ export function checkHeaderOrder(headerText) {
|
|
|
429
437
|
const close = declaration.lastIndexOf(')')
|
|
430
438
|
return close !== -1 && declaration.slice(close + 1).trimStart().startsWith('const')
|
|
431
439
|
}
|
|
432
|
-
const
|
|
433
|
-
(
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
)
|
|
439
|
-
const modifier = hasAdjacentPair
|
|
440
|
-
? -1
|
|
441
|
-
: members.findIndex(
|
|
442
|
-
(candidate, candidateIndex) =>
|
|
443
|
-
candidateIndex < index &&
|
|
444
|
-
!isPredicate(candidate) &&
|
|
445
|
-
!isGet(candidate) &&
|
|
446
|
-
candidate.text.includes('(') &&
|
|
447
|
-
!isConstMember(candidate) &&
|
|
448
|
-
members.slice(0, candidateIndex).some(isGet),
|
|
449
|
-
)
|
|
450
|
-
if (modifier !== -1) {
|
|
440
|
+
const isModifier = (member) =>
|
|
441
|
+
!isGet(member) &&
|
|
442
|
+
!/^Set[A-Z0-9_]/.test(member.name) &&
|
|
443
|
+
!member.name.startsWith('operator') &&
|
|
444
|
+
!isConstMember(member)
|
|
445
|
+
const previous = members.slice(0, index).reverse().find((candidate) => !isPredicate(candidate))
|
|
446
|
+
if (previous && isModifier(previous)) {
|
|
451
447
|
issues.push({
|
|
452
448
|
line,
|
|
453
449
|
rule: 'header-order',
|
|
454
|
-
message: `${item.name}::${member.name} 是谓词,却排在修改接口 ${
|
|
450
|
+
message: `${item.name}::${member.name} 是谓词,却排在修改接口 ${previous.name} 之后;谓词应在读取接口或 Get/Set 成对之后、修改接口之前。`,
|
|
455
451
|
})
|
|
456
452
|
}
|
|
457
453
|
continue
|
|
@@ -526,7 +522,7 @@ function checkDefinitionSignatures(headerText, cppText) {
|
|
|
526
522
|
}
|
|
527
523
|
|
|
528
524
|
/** 比较一个头文件与一个实现文件;返回乱序与签名问题。 */
|
|
529
|
-
export function checkPair(headerText, cppText, fileName = '') {
|
|
525
|
+
export function checkPair(headerText, cppText, fileName = '', headerFile = fileName) {
|
|
530
526
|
const classes = findClasses(headerText)
|
|
531
527
|
const definitions = cppDefinitions(cppText)
|
|
532
528
|
const issues = checkHeaderOrder(headerText)
|
|
@@ -553,7 +549,7 @@ export function checkPair(headerText, cppText, fileName = '') {
|
|
|
553
549
|
}
|
|
554
550
|
}
|
|
555
551
|
issues.push(...checkDefinitionSignatures(headerText, cppText))
|
|
556
|
-
return issues.map((issue) => ({ file: fileName, ...issue }))
|
|
552
|
+
return issues.map((issue) => ({ file: issue.rule === 'header-order' ? headerFile : fileName, ...issue }))
|
|
557
553
|
}
|
|
558
554
|
|
|
559
555
|
function collectFiles(root) {
|
|
@@ -612,7 +608,8 @@ export function checkRoot(root) {
|
|
|
612
608
|
const headerText = readFileSync(header, 'utf8')
|
|
613
609
|
const cppText = readFileSync(file, 'utf8')
|
|
614
610
|
const relative = path.relative(root, file) || file
|
|
615
|
-
const
|
|
611
|
+
const headerRelative = path.relative(root, header) || header
|
|
612
|
+
const found = checkPair(headerText, cppText, relative, headerRelative)
|
|
616
613
|
pairs.push({ header, source: file, issues: found })
|
|
617
614
|
issues.push(...found)
|
|
618
615
|
}
|
|
@@ -626,7 +623,7 @@ function main(argv) {
|
|
|
626
623
|
} catch (error) {
|
|
627
624
|
return usage(error.message)
|
|
628
625
|
}
|
|
629
|
-
if (options.help) return
|
|
626
|
+
if (options.help) return help()
|
|
630
627
|
const root = options.root ? path.resolve(options.root) : process.cwd()
|
|
631
628
|
try {
|
|
632
629
|
statSync(root)
|
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
|
|
7
7
|
## 通用情况
|
|
8
8
|
|
|
9
|
-
-
|
|
9
|
+
- 语言标准由项目技能目录下的 `environment.json` 的 `standard` 决定,不另在技能正文写一份;文件不存在时用新工程默认 C++23(C++11–C++23),不回退其他推断。
|
|
10
10
|
- 文件槽位、`#pragma once` 与扩展名以 `references/layout/02-file-template.md` 为准;本步只决定声明写什么。
|
|
11
11
|
- 命名以 `references/naming/01-identifier-names.md` 为准:规则表、语义角色、谓词与查询命名都在那里;字符集、保留标识符与不向 `std` 添加声明见同文件的「字符集与保留标识符」。
|
|
12
|
-
-
|
|
12
|
+
- 声明顺序、组内次序、数据成员次序与空行以 `references/layout/03-declaration-order.md` 为准。
|
|
13
13
|
- 公共头自包含:自己用到的类型、宏和标准库头都直接包含。验证方式:建一个只包含该头的空 `.cpp` 编译一次,不通过就补 include;五条自检见 `references/layout/02-file-template.md` 的「自检」。
|
|
14
|
-
- 单参数构造函数默认写 `explicit`,例外见 `references/language/language-basics/06-functions.md`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,位置与 `friend` 例外见 `references/language/value-types/07-nodiscard.md`。默认成员初始化写在声明处;初始化默认用花括号,为什么保留 `std::vector<int>(n)` 这类容量构造的 `()` 见 `references/language/language-basics/02-initialization.md`。特殊成员(含 `= default
|
|
15
|
-
-
|
|
14
|
+
- 单参数构造函数默认写 `explicit`,例外见 `references/language/language-basics/06-functions.md`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,位置与 `friend` 例外见 `references/language/value-types/07-nodiscard.md`。默认成员初始化写在声明处;初始化默认用花括号,为什么保留 `std::vector<int>(n)` 这类容量构造的 `()` 见 `references/language/language-basics/02-initialization.md`。特殊成员(含 `= default`)放在成员组最前;校验对 `Is*` / `Check*` 前置时排在它们之前,见 `references/layout/03-declaration-order.md`「校验对的位置」。定义位置同见该文件。
|
|
15
|
+
- 头文件与实现文件的槽位、内联与头文件禁项见 `references/layout/02-file-template.md`;本步只守一条:不要一部分内联、一部分外移。
|
|
16
16
|
- 失败是调用方的正常结果时,用结果类型或 `std::optional` 表达;输出参数只留给热循环里复用容器容量的例外,终端读入不使用。函数无法完成自己的任务、或构造函数无法建立不变量时才抛;异常类型与消息见 `references/exceptions/01-exception-rules.md`。析构与 `swap` 标 `noexcept`;「拒绝」与「抛」并存时在公共头各写一行。
|
|
17
17
|
- 一次操作有多种必须区分的正常结果时用 `enum class` 返回(如落子:成功、玩家不对、终局、占用),不用 `bool` 把原因压掉;成功还要附带数据时再考虑 `std::expected` 或结果结构;纯查询谓词仍用 `bool`。枚举项默认不显式写值;确需编号时状态与结果枚举把 0 留给正常态、失败态按固定顺序随后,身份枚举按领域编号。封闭状态码默认 `: std::uint8_t`,普通枚举用默认底层;不要用 `bool` 开关参数承载两种以上行为(见 `references/language/value-types/04-enum.md` 与 `10-float-bool.md`)。
|
|
18
18
|
- 注释内容按 `references/comments/01-comment-rules.md`、结构版式按 `02-style.md`。本步只决定「写哪些事实」:结果、失败方式、检查项、参数与返回值契约。行宽跟 `.clang-format` 的 `ColumnLimit`(默认 120,中文按两列、tab 按 4 列);枚举项与 `struct` 短字段的 `///<` 与声明对齐;私有函数同样写声明契约;out 参数标 `[out]`,既读又写标 `[in,out]`。
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
|
|
29
29
|
**异常声明**:构造函数、`Get` 类查询、解析函数最容易抛;在声明注释里写「当……时抛出 `std::out_of_range`」这样的具体类型,不给每个参数错误新建类型。
|
|
30
30
|
|
|
31
|
-
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments/01-comment-rules.md` 与 `references/comments/02-style.md
|
|
31
|
+
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments/01-comment-rules.md` 与 `references/comments/02-style.md`。类型注释先写一句名词短语,再按类型补「表示…」段;类、结构体、格式化器默认两句,纯数据结构、枚举和纯值容器最多两句,不写成员清单;初始状态归默认构造函数。写完运行:
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
34
|
node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
需要关掉简报时加 `--no-brief`;关掉参数或返回值文档时加 `--no-param` 或 `--no-return
|
|
37
|
+
需要关掉简报时加 `--no-brief`;关掉参数或返回值文档时加 `--no-param` 或 `--no-return`;文件头里的 `@author`、`@version`、`@date`、`@copyright` 默认放行,要把同名标签写进函数或其他声明注释时加 `--allow-tag <标签>`。完整参数用 `node <skill-dir>/scripts/checking-comments.mjs --help` 查看。
|
|
38
38
|
|
|
39
39
|
## 正反例指针
|
|
40
40
|
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
|
|
9
9
|
- 函数保持短小,一个函数只做一件事。长度以 `references/style/01-google-cpp-style.md` 的「函数」节为准:需要拆分时先找可以命名的中间步骤,不靠注释把长函数切成几段。
|
|
10
10
|
- 按「准备 → 操作 → 收尾」排列语句,用空行分组;组与组之间的空行表达阶段,同一次检查的取值、判断与返回不插空行,守卫之间怎么空见 `references/layout/03-declaration-order.md`「函数体内的空行」。
|
|
11
|
-
- `.cpp`
|
|
12
|
-
-
|
|
11
|
+
- `.cpp` 里的定义顺序跟配对头文件,常量与匿名命名空间的归属同见 `references/layout/03-declaration-order.md`「`.cpp` 的定义顺序」与 `references/style/01-google-cpp-style.md`;不为先编译、也不按调用频率重排。
|
|
12
|
+
- 同一文件里的函数按同一套分段约定,不一半分段、一半完全不留注释;一条直路的短函数不加分段注释,阶段怎么分见 `references/layout/03-declaration-order.md`「函数体内的空行」。
|
|
13
13
|
- `.cpp` 的第一个 include 是自己的配对头,然后直接包含本文件用到的标准库头,不依赖公共头转发声明。包含顺序见 `references/style/01-google-cpp-style.md`,文件槽位见 `references/layout/02-file-template.md`。
|
|
14
|
-
- 函数体注释用 `//`,不用 `///`:体内 `///` 会接到该函数的 Doxygen
|
|
14
|
+
- 函数体注释用 `//`,不用 `///`:体内 `///` 会接到该函数的 Doxygen 详细说明后面。写哪些句、阶段注释、单表达式函数与关键局部表的处理见 `references/comments/01-comment-rules.md`「实现:阶段边界与为什么」。`.cpp` 里只在 .cpp 出现的函数才写 `///` 完整契约;头文件已声明的定义处不写 `///`。
|
|
15
15
|
- 什么时候在 `if` 上方加一句:多条独立拒绝(如 `Game::Place`)不逐条注释,只有失败结果的优先级需要说明时在第一条 `if` 上方写一句总起;成功写入前一句;只有次序依赖的一对 `if` 在第一个上方说明;循环上方已有阶段说明时,循环里的 `if` 不再加;单表达式谓词不加。
|
|
16
|
-
- 枚举全覆盖不写 `default`,单条 `return` 不加花括号,`std::unreachable()` 紧跟 `switch`
|
|
16
|
+
- 枚举全覆盖不写 `default`,单条 `return` 不加花括号,`std::unreachable()` 紧跟 `switch` 右花括号;值域白名单的规则见 `references/language/language-basics/05-expressions.md`,不在这里重复说明。
|
|
17
17
|
- 失败是调用方正常结果时返回结果类型、`bool` 或 `std::optional`;函数无法完成自己的任务时抛异常,不拿异常做正常分支。失败方式与 `Check`/`DebugCheck` 按 `references/exceptions/01-exception-rules.md` 的三层合同。捕获时按 `const` 引用,只处理能处理的类型,其余继续抛。
|
|
18
18
|
- 参数校验、调用方状态检查与正常结果返回的次序见 `references/exceptions/01-exception-rules.md` 的「检查次序」:先整局状态,再进行中的玩家,最后交给下层检查坐标与占用,下层检查不重复。改完 `throw` 再走同文件的「检查清单」:异常类型、消息标签、跨行消息先存变量、不吞异常。
|
|
19
19
|
- 声明 `noexcept` 前核对函数体:出现 `at()` 或其他可能抛出的调用就去掉。越界用 `at()` 抛 `std::out_of_range`,这个函数就不能标 `noexcept`。`noexcept` 不是默认标注项:没有明确收益(ABI、泛型/容器行为、接口契约)就不补;普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;它留给移动、交换、析构,以及已经标上的纯谓词。
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
|
|
32
32
|
**重构**:一次只动一种东西(改名、抽函数、换容器),每步都让测试保持绿色,行为句不变。
|
|
33
33
|
|
|
34
|
-
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format
|
|
34
|
+
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format` 在 `error.what()` 前加项目约定的前缀打印(示例 `错误: {}`),返回项目约定的非零退出码。`catch` 子句按派生到基类、具体到通用排序,`catch (...)` 只能放最后,且不要静默返回。不吞掉异常后继续使用无效状态。
|
|
35
35
|
|
|
36
36
|
## 交回构建
|
|
37
37
|
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
|
|
42
42
|
- 实现注释复述代码、分组混乱:`references/comments/examples/09-implementation-body.md`。
|
|
43
43
|
- 已经证明非负的坐标把下标收成 `std::size_t` 再 `[]`,不转成 `int`:`references/language/value-types/09-subscript.md`。
|
|
44
|
-
-
|
|
45
|
-
-
|
|
44
|
+
- 越界消息带 `Type::Function:` 标签,句子从失败的值开始、限度用 `for` 接在句末:`references/exceptions/01-exception-rules.md`「消息」。
|
|
45
|
+
- 多个入口重复同一范围检查:抽成同一层的检查函数,参数与 `IsInRange` 相同;对局层要调用时放 public,见 `references/exceptions/01-exception-rules.md`「重复的入口检查」。
|
|
46
46
|
- 为了保住 `noexcept` 再包一层只转发下标的私有函数:`references/exceptions/01-exception-rules.md`「不抛的边界」。
|
|
47
47
|
- 用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的落子结果,或丢掉调用方给的一手、在 `Game` 内重新拼装:`references/design/examples/06-move-result.md`。
|
|
48
48
|
- 异常类型与结果类型混用:`references/exceptions/examples/07-throw-when.md`。
|
|
@@ -15,7 +15,7 @@ build/<compiler>/
|
|
|
15
15
|
- Linux:`-Wl,-rpath,'$ORIGIN/../lib'`
|
|
16
16
|
- macOS:`-Wl,-rpath,@loader_path/../lib`
|
|
17
17
|
- CMake 用 `BUILD_RPATH` / `INSTALL_RPATH` 设置,不要手工拼链接命令;见 [`LinkSettings.cmake`](../assets/LinkSettings.cmake)。
|
|
18
|
-
- `$ORIGIN`
|
|
18
|
+
- `$ORIGIN` 的转义与设置位置只有一份权威:`../targets/01-target-rules.md` 的「共享库运行时路径」节,本文件不重复。
|
|
19
19
|
|
|
20
20
|
## 依据
|
|
21
21
|
|
|
@@ -10,10 +10,10 @@ CMake 有两套相关机制,必须同时写:
|
|
|
10
10
|
- `target_compile_features(target visibility cxx_std_NN)`:声明目标至少需要该标准。它是 meta-feature,编译器默认标准更高时可能不产生 `-std=`;只写这一项,不能保证实际使用哪个标准。
|
|
11
11
|
- `CXX_STANDARD NN` + `CXX_STANDARD_REQUIRED ON` + `CXX_EXTENSIONS OFF`:把请求标准写死,禁止不可用时静默降级,并关闭 `gnu++NN` 扩展。
|
|
12
12
|
|
|
13
|
-
| `standard` | meta-feature | `CXX_STANDARD` | `__cplusplus` |
|
|
13
|
+
| `standard` | meta-feature | `CXX_STANDARD` | `__cplusplus` | `cxx_std_*` 最低版本 |
|
|
14
14
|
| --- | --- | --- | --- | --- |
|
|
15
|
-
| `c++11` | `cxx_std_11` | `11` | `201103L` | 3.
|
|
16
|
-
| `c++14` | `cxx_std_14` | `14` | `201402L` | 3.
|
|
15
|
+
| `c++11` | `cxx_std_11` | `11` | `201103L` | 3.8 |
|
|
16
|
+
| `c++14` | `cxx_std_14` | `14` | `201402L` | 3.8 |
|
|
17
17
|
| `c++17` | `cxx_std_17` | `17` | `201703L` | 3.8 |
|
|
18
18
|
| `c++20` | `cxx_std_20` | `20` | `202002L` | 3.12 |
|
|
19
19
|
| `c++23` | `cxx_std_23` | `23` | `202302L` | 3.20 |
|
|
@@ -26,7 +26,7 @@ CMake 有两套相关机制,必须同时写:
|
|
|
26
26
|
|
|
27
27
|
```cmake
|
|
28
28
|
set_target_cxx_standard(app PRIVATE c++23)
|
|
29
|
-
set_target_cxx_standard(greeting PUBLIC c++
|
|
29
|
+
set_target_cxx_standard(greeting PUBLIC c++23)
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
第一个参数是已由 `add_library` 或 `add_executable` 创建的目标,第二个是 `PUBLIC` / `PRIVATE` / `INTERFACE`,第三个是 `c++11`–`c++23`。函数内部做三步:校验目标与参数,调用 `target_compile_features`,再设置 `CXX_STANDARD` 三属性。
|
|
@@ -48,7 +48,7 @@ set_target_cxx_standard(greeting PUBLIC c++20)
|
|
|
48
48
|
| 只写 `target_compile_features(... cxx_std_23)` | 编译器默认更高时可能不加 `-std`,实际标准不可见 |
|
|
49
49
|
| 只写 `CXX_STANDARD 23`,不写 `CXX_STANDARD_REQUIRED` | 编译器不支持时静默降级 |
|
|
50
50
|
| 不写 `CXX_EXTENSIONS OFF` | 得到 `-std=gnu++23`,用了非标准扩展 |
|
|
51
|
-
| 用 `CMAKE_CXX_STANDARD` 全局设一次 |
|
|
51
|
+
| 用 `CMAKE_CXX_STANDARD` 全局设一次 | 只改默认值,不产生精确的 `-std=c++NN`,也无法逐 target 核对数据库 |
|
|
52
52
|
| 在 `CMAKE_CXX_FLAGS` 里手写 `-std=c++23` | 与 target 属性冲突,数据库不可信 |
|
|
53
53
|
| 把 `cxx_std_23` 写成 `cxx_std_2023` | meta-feature 名字错误,configure 失败 |
|
|
54
54
|
|
|
@@ -72,7 +72,7 @@ set_target_properties(app PROPERTIES
|
|
|
72
72
|
- `cxx_std_23` 是“至少”,会向依赖它的 target 传播。
|
|
73
73
|
- `CXX_STANDARD` 是请求值;`CXX_STANDARD_REQUIRED ON` 防止编译器不支持时静默降级。
|
|
74
74
|
- `CXX_EXTENSIONS OFF` 把 `-std=gnu++23` 收成 `-std=c++23`。
|
|
75
|
-
- configure 后读 `compile_commands.json` 中每条 `-std=`;同一 target 的各条取值必须相同,且都是 `-std=c++NN`,不能是 `gnu++NN
|
|
75
|
+
- configure 后读 `compile_commands.json` 中每条 `-std=`;同一 target 的各条取值必须相同,且都是 `-std=c++NN`,不能是 `gnu++NN`,不同值即失败。矩阵脚本逐条断言每条编译命令恰好一个 `-std=`,且等于本档期望值。
|
|
76
76
|
- CMake 最低 3.20(模板统一要求);只单独使用低档片段时,C++11/14 的 `cxx_std_*` meta-feature 最低 3.1,C++17 为 3.8,C++20 为 3.12。C++23 的 `CXX_STANDARD 23` 自 3.20 才有。
|
|
77
77
|
|
|
78
78
|
### clang-format
|
|
@@ -74,7 +74,7 @@ set_target_cxx_standard(app PRIVATE c++23)
|
|
|
74
74
|
set_target_cxx_standard(greeting PUBLIC c++23)
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
函数名不绑定单一标准;新增档位只改函数里的支持列表。示例里库与程序同档:一个工程只有一个标准(见 `building-cpp/steps/01-build.md
|
|
77
|
+
函数名不绑定单一标准;新增档位只改函数里的支持列表。示例里库与程序同档:一个工程只有一个标准(见 `building-cpp/steps/01-build.md`);工程整体降档时所有会编译的 target 一起改,并同步 `environment.json`。不要给单个 target 写更低的值:数据库里出现两条不同的 `-std=` 就回第 1 步。模块何时抽、文件怎么放、怎么导入与测试见 `../modules/01-functions.md`。
|
|
78
78
|
|
|
79
79
|
## 警告等级
|
|
80
80
|
|
|
@@ -119,7 +119,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
119
119
|
|
|
120
120
|
- 只对 Makefile 和 Ninja 生成器生效;本项目固定 Ninja。
|
|
121
121
|
- 生成位置是构建目录根部:`<构建目录>/compile_commands.json`。
|
|
122
|
-
-
|
|
122
|
+
- 构建脚本只在多构建目录布局下把它链接到 `build/compile_commands.json`;clangd 由工程根 `.clangd` 的 `CompilationDatabase` 指向构建目录(见 `building-cpp` 的 `formatting/04-clangd.md`),clang-tidy 用 `-p <构建目录>`。
|
|
123
123
|
- 换编译器时,旧目录的数据库不能复用;清理或换目录。
|
|
124
124
|
|
|
125
125
|
## 输出目录
|
|
@@ -176,7 +176,7 @@ include(LinkSettings)
|
|
|
176
176
|
fetch_googletest()
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
- 函数内部固定 GoogleTest
|
|
179
|
+
- 函数内部固定 GoogleTest 的 URL 与 SHA-256,避免供应链漂移;版本号只在 `LinkSettings.cmake` 的 URL 里写一处,升级时同步哈希。
|
|
180
180
|
- 只在 `tests/` 需要时调用;库和程序不依赖测试框架。
|
|
181
181
|
- 没有网络时,项目可以自带 vendored 副本;不要构建时静默跳过测试。
|
|
182
182
|
|
|
@@ -189,7 +189,7 @@ fetch_googletest()
|
|
|
189
189
|
| `static-library` | 库需要被测试或第二个程序链接 | 根 + `greeting/` 子目录 |
|
|
190
190
|
| `shared-library` | 需要运行时加载/替换 | 同上,`SHARED` + RPATH |
|
|
191
191
|
|
|
192
|
-
模板里的 `{{PROJECT_NAME}}`
|
|
192
|
+
模板里的 `{{PROJECT_NAME}}` 必须替换;不要留下占位符。三份带 `greeting` 演示的模板各自保持可独立复制,内容相同,改演示时三处同步。
|
|
193
193
|
|
|
194
194
|
## 正反例
|
|
195
195
|
|
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
本工作流不在 MSVC 上构建,本节只用于阅读或校验外部 MSVC 工程。MSVC 默认按 BOM 判断源码编码。源码没有 BOM 时,它按当前用户代码页解释,除非显式传入 `/utf-8` 或 `/source-charset:utf-8`。因此 BOM 不是 MSVC 的硬要求。
|
|
18
18
|
|
|
19
19
|
- 首选做法是加编译选项:在目标上写 `target_compile_options(<target> PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8>)`,源码继续 UTF-8 无 BOM、LF。
|
|
20
|
-
- 只有在无法改编译选项时,才把 MSVC 编译的 C/C++ 源文件保存为 UTF-8 BOM。BOM
|
|
20
|
+
- 只有在无法改编译选项时,才把 MSVC 编译的 C/C++ 源文件保存为 UTF-8 BOM。BOM 只允许出现在这类源文件里;脚本按 `.editorconfig` 的 `charset = utf-8-bom` 声明放行该文件,所以配置写 `utf-8-bom` 时要收窄到这类源文件。
|
|
21
21
|
- `.sh`、`.bat`、`.cmd`、`.json`、`.yml`、`.gitattributes`、`.editorconfig` 和 CMake 文件不要加 BOM。
|
|
22
22
|
- MSVC 接受 LF 源码;CRLF 不是必需项。只有 `.bat` / `.cmd` 固定 CRLF。
|
|
23
23
|
- WSL 与 Linux 构建按默认组合,不要把源码转成 CRLF 来迁就 Windows。
|
|
24
24
|
|
|
25
25
|
## 工具
|
|
26
26
|
|
|
27
|
-
- `.gitattributes` 决定 Git 入库和检出;它覆盖贡献者自己的 `core.autocrlf
|
|
27
|
+
- `.gitattributes` 决定 Git 入库和检出;它覆盖贡献者自己的 `core.autocrlf`。`.bat` / `.cmd` 的 CRLF 与 `.sh` 的 LF 也要在这里写同一档(`*.bat text eol=crlf`、`*.sh text eol=lf`);脚本按声明判定。
|
|
28
28
|
- `.editorconfig` 决定编辑器保存;`charset` 与 `end_of_line` 必须和 `.gitattributes` 同一档。
|
|
29
29
|
- `.editorconfig` 的文件名由 EditorConfig 规范固定,不能改名:插件从目标文件所在目录逐级向上
|
|
30
30
|
查找全小写的 `.editorconfig`,改名后不会被识别;放进子目录只对该子树生效。
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
node "<skill-dir>/scripts/checking-encoding.mjs" --profile portable
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
- 脚本按文件路径读取逐级 `.gitattributes`(`text`、`binary`、`eol`、`working-tree-encoding`)与逐级向上的 `.editorconfig`(`charset`、`end_of_line`、`insert_final_newline
|
|
43
|
+
- 脚本按文件路径读取逐级 `.gitattributes`(`text`、`binary`、`eol`、`working-tree-encoding`)与逐级向上的 `.editorconfig`(`charset`、`end_of_line`、`insert_final_newline`),以它们为准;两者都没有声明时才用默认。`charset` 或 `working-tree-encoding` 声明为非 UTF-8 的文件由转码工具负责,脚本跳过不判。
|
|
44
44
|
- `portable`(默认)的默认组合是 UTF-8 无 BOM、LF、末尾换行。`msvc` 只放宽 MSVC 编译的 C/C++ 源文件:允许 UTF-8 BOM 与 CRLF;Markdown、配置与脚本仍按 `portable` 检查。
|
|
45
45
|
- 退出码:0 无问题,1 有问题,2 参数或 Git 错误。加 `--json` 输出机器可读结果。
|
|
46
46
|
- 脚本只写 stdout,不生成报告文件;不要把输出重定向成仓库根下的日志。要留证据写 `temp/`,否则只引用终端输出。
|
|
@@ -12,7 +12,7 @@ clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
|
12
12
|
- 工程必须有根 `.clang-format`;缺失时先复制配置,不要回退到内置风格,回退会掩盖配置缺失。
|
|
13
13
|
- 只扫描项目源码:`src/`、`include/`、`app/`、`tests/` 等实际存在的目录;不扫描 `build/`、第三方、生成文件。
|
|
14
14
|
- 配置文件在工程根 `.clang-format`;不要依赖用户主目录或编辑器内置配置。
|
|
15
|
-
- `BasedOnStyle: Google`、`Standard` 按 `../cmake/standards/index.md` 的映射表:C++11–C++20 写 `c++NN`,C++23 写 `Latest`(clang-format 21/22 不认 `c++23`,直接写会报 `error: unknown enumerated scalar
|
|
15
|
+
- `BasedOnStyle: Google`、`Standard` 按 `../cmake/standards/index.md` 的映射表:C++11–C++20 写 `c++NN`,C++23 写 `Latest`(clang-format 21/22 不认 `c++23`,直接写会报 `error: unknown enumerated scalar`)。列宽、缩进与包含分块的键值以 `skills/building-cpp/assets/config/.clang-format` 为唯一清单,不在这里重抄;工程显式写 `ColumnLimit` 时按工程值执行。
|
|
16
16
|
- 构造函数初始化列表写 `PackConstructorInitializers: BinPack`:放得下时并入一行,超出列宽再按成员换行。Google 预设逐项换行,成员一多就占五六行,与「列宽内保持一行」冲突;这条只在配置里定,不在条文里规定手写位置。
|
|
17
17
|
- 格式化失败就是失败,不要用 `// clang-format off` 绕过;确需关闭时只包住无法排版的宏或表格,并写一句理由。
|
|
18
18
|
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
|
|
6
6
|
```bash
|
|
7
7
|
clang-tidy -p build/clang \
|
|
8
|
-
--header-filter="^$(pwd -P)/
|
|
8
|
+
--header-filter="^$(pwd -P)/" \
|
|
9
9
|
--warnings-as-errors='*' \
|
|
10
10
|
src/*.cpp app/*.cpp tests/*.cpp
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
- `-p` 指向当前编译器的构建目录,读取 `compile_commands.json`。
|
|
14
14
|
- `--header-filter` 控制哪些头文件的诊断被显示,不改变 clang-tidy 实际解析哪些头;模板实例化可能让一条根因产生多条警告,警告条数不等于缺陷条数。按根因修,不要按条数修。
|
|
15
|
-
- `--header-filter`
|
|
15
|
+
- `--header-filter` 默认用工程根前缀 `^$(pwd -P)/`,只显示工程内头文件的诊断;工程根里还放着第三方头时,收窄成源码根白名单,如 `(include|src|app|tests)`,不要用 `.*` 把系统头和第三方头一并纳入。它是对头文件名的正则;`$(pwd -P)` 只是拼出绝对路径前缀的写法,不是文档强制。`--exclude-header-filter` 到 clang-tidy 19 才加入,16/17/18 都没有,不要依赖它。
|
|
16
16
|
- 系统头默认被排除,不要加 `--system-headers`。
|
|
17
17
|
- 只扫描项目源文件;不要把 `build/` 或第三方源码传给 clang-tidy。
|
|
18
18
|
- `--warnings-as-errors='*'` 把警告当失败;修代码,不要新增 `NOLINT`。确需抑制时写 `// NOLINT(检查名): 理由`,理由必须具体。
|
|
@@ -36,7 +36,7 @@ clang-tidy -p build/clang \
|
|
|
36
36
|
|
|
37
37
|
## 版本与一致性
|
|
38
38
|
|
|
39
|
-
-
|
|
39
|
+
- 工具版本下限与 clang/gcc 模式的组合要求见 `building-cpp` 的 `02-environment.md`「版本与能力」,本文件不重复数值。Apple Clang 与 Homebrew LLVM 混装时,仍按同一 LLVM 主版本要求处理:工具版本不一致就统一到同一套工具链,不改检查项来迁就。
|
|
40
40
|
- 混版本常见症状:格式化结果与 CI 不同、clangd 诊断与 tidy 不一致、选项被静默忽略。
|
|
41
41
|
- 检查器在 `--live` 时核对版本;不要用「能跑就行」绕过。
|
|
42
42
|
|
|
@@ -57,7 +57,7 @@ Diagnostics:
|
|
|
57
57
|
|
|
58
58
|
| 键 | 作用 | 取值规则 |
|
|
59
59
|
| --- | --- | --- |
|
|
60
|
-
| `clangd.arguments` | 传给 clangd 的参数 | `--query-driver=<绝对路径>`
|
|
60
|
+
| `clangd.arguments` | 传给 clangd 的参数 | `--query-driver=<绝对路径>` 与 `environment.json` 的编译器一致;只列当前构建用的一个驱动,确有多个时按上文用逗号分隔,不写 `*`;`--header-insertion=never`;`--background-index` |
|
|
61
61
|
| `cmake.buildDirectory` | 构建目录与编译数据库位置 | 与 `.clangd` 的 `CompilationDatabase` 同一目录:默认 `build/<compiler>`,工程声明单一目录时写该目录 |
|
|
62
62
|
| `cmake.generator` | 生成器 | `Ninja`,与入口脚本一致 |
|
|
63
63
|
| `cmake.configureOnOpen` | 打开工程时自动配置 | `false`,配置时机由入口脚本决定 |
|
|
@@ -36,7 +36,7 @@ build_tool_min_version = "1.11"
|
|
|
36
36
|
|
|
37
37
|
## 注释语法
|
|
38
38
|
|
|
39
|
-
- TOML 配置只有 `#` 一种注释;`#` 后空一格,句末用句号。JSON
|
|
39
|
+
- TOML 配置只有 `#` 一种注释;`#` 后空一格,句末用句号。JSON 只有 `.vscode/` 下的 `settings.json` 与 `extensions.json` 按 JSONC 写 `//`(见 `01-encoding.md`),其他 `.json`(如 `environment.json`)按 RFC 8259 不写注释;`.clang-format` 等 YAML 用 `#`。
|
|
40
40
|
- 注释紧贴键的上一行,键与注释之间不空行;不写行尾注释。
|
|
41
41
|
- 键块(注释 + 键)内部不空行,键块之间空一行。
|
|
42
42
|
- 文件有两个及以上对象时,每区第一行写一行 80 列等号横幅,标题居中,前后各空一行;`.editorconfig` 的横幅紧贴其下的节头,节按文件后缀分组,不按本仓目录命名。
|
|
@@ -39,7 +39,7 @@ configure 的目录与编译器写法以 `building-cpp` 的 `steps/01-build.md`
|
|
|
39
39
|
|
|
40
40
|
**库 + 程序**:库的公共 include 用 `PUBLIC` 传播;程序不直接包含库的 `src/`。静态库与动态库的 target 写法相同,动态库额外设运行时路径。
|
|
41
41
|
|
|
42
|
-
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 用 `fetch_googletest()`
|
|
42
|
+
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 用 `fetch_googletest()` 取固定版本框架,用 `include(GoogleTest)` 加 `gtest_discover_tests` 注册,命令参数以 `../references/cmake/testing/01-ctest.md`「注册给 CTest」为唯一出处;不得用 `add_test(NAME all ...)` 包整个可执行文件。测试 target 只链接被测库的公共接口,没有测试时不加空测试。
|
|
43
43
|
|
|
44
44
|
**预设**:使用 preset 时名称固定 `clang` / `gcc`,与构建目录一致;没有 preset 时直接用上面的命令,不要两套并存。
|
|
45
45
|
|