@chengzhao-dev/dsh-cpp-workflow 0.7.10 → 0.7.11
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/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/references/comments/01-comment-rules.md +7 -7
- package/skills/developing-cpp/references/comments/02-style.md +17 -8
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +1 -1
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +4 -4
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +14 -10
- package/skills/developing-cpp/references/comments/examples/06-declaration-member.md +2 -2
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +2 -2
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +7 -7
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +12 -11
- package/skills/developing-cpp/references/design/01-project-design.md +5 -5
- 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 +31 -31
- 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 +15 -15
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +9 -2
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +1 -1
- 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 +6 -5
- 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 +1 -1
- 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 +3 -3
- package/skills/developing-cpp/scripts/checking-comments.mjs +23 -9
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +24 -27
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +6 -6
- 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
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
<!-- 读取时机:统一命名术语时;读完能回答:position
|
|
2
|
-
# position
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:position 的写法;预算:一次只开这一份(23 行),不自动串联。 -->
|
|
2
|
+
# position 的写法
|
|
3
3
|
|
|
4
4
|
本文件是 `references/naming/01-identifier-names.md` 的附录,只讨论 `position` 这个词的写法;词表与加词流程见 [01-naming-terms.md](01-naming-terms.md)。
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
坐标参数、局部变量与公开数据成员都写完整名词,不按长短取舍。
|
|
7
7
|
|
|
8
8
|
| 语义 | 写法 | 依据 |
|
|
9
9
|
| --- | --- | --- |
|
|
10
|
-
| 标量下标或偏移 | `
|
|
11
|
-
| 坐标值 |
|
|
10
|
+
| 标量下标或偏移 | 按语义写 `index` 或 `offset` | ISO C++ 草案 `basic_string::find` 的 `size_type pos` 是标准库历史写法,本项目按语义取名 |
|
|
11
|
+
| 坐标值 | 参数、局部变量与公开数据成员都写 `position` | 本项目统一 `const Position& position`、`Move::position` |
|
|
12
12
|
| 迭代器式位置对象 | `position` | ISO C++ 草案 `vector::insert(const_iterator position, const T& x)`、`vector::erase(const_iterator position)` |
|
|
13
13
|
|
|
14
|
-
- `Position`
|
|
15
|
-
-
|
|
16
|
-
- 不采用的缩写:`pt`(point 是类型)、`loc`(编译器语境指 `source_location`)、`coord`(偏数学分量)、`idx`(index 语义不同)。
|
|
14
|
+
- `Position` 是坐标值,不是迭代器;参数与局部变量的写法见 [01-identifier-names.md](../01-identifier-names.md) 的参数条。
|
|
15
|
+
- 完整词是主流:SFML 写 `setPosition(const Vector2f& position)`,Godot 与 Unity 的属性叫 `position`,Unreal 用 `Location`;本项目统一采用完整词。
|
|
16
|
+
- 不采用的缩写:`pos`(`position` 的截短)、`pt`(point 是类型)、`loc`(编译器语境指 `source_location`)、`coord`(偏数学分量)、`idx`(index 语义不同)。
|
|
17
17
|
|
|
18
18
|
## 依据
|
|
19
19
|
|
|
20
20
|
- ISO C++ `[string.find]`:https://eel.is/c++draft/string.find
|
|
21
21
|
- ISO C++ `[vector.modifiers]`:https://eel.is/c++draft/vector.modifiers
|
|
22
22
|
- libstdc++ `basic_string.h`:https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h
|
|
23
|
-
-
|
|
23
|
+
- Google C++ Style 命名规则:https://google.github.io/styleguide/cppguide.html#General_Naming_Rules
|
|
24
24
|
- SFML `Transformable`:https://github.com/SFML/SFML/blob/master/include/SFML/Graphics/Transformable.hpp
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
|
|
23
23
|
## 光标
|
|
24
24
|
|
|
25
|
-
列表界面里「跟着按键移动、还没确认」的位置写 `cursor_index_`,不写 `selected_`;`selected_` 保存已确认的对象,下标写 `selected_index_`。判断用 `HasCursor(index)`,动作用 `MoveTo`、`MovePrevious`、`MoveNext`,不写 `IsCursorAt(index)`。光标是选择过程的中间量,确认后才转成 `selected_
|
|
25
|
+
列表界面里「跟着按键移动、还没确认」的位置写 `cursor_index_`,不写 `selected_`;`selected_` 保存已确认的对象,下标写 `selected_index_`。判断用 `HasCursor(index)`,动作用 `MoveTo`、`MovePrevious`、`MoveNext`,不写 `IsCursorAt(index)`。光标是选择过程的中间量,确认后才转成 `selected_`;字段名见 [01-identifier-names.md](../01-identifier-names.md) 的「界面与索引」。
|
|
26
26
|
|
|
27
27
|
依据见 [01-identifier-names.md](../01-identifier-names.md) 的「语义角色」与末尾 12 条来源。
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
| 语义 | 写法 | 依据 |
|
|
9
9
|
| --- | --- | --- |
|
|
10
|
-
| 棋盘上的一格 | `Square`;参数、局部变量 `square
|
|
10
|
+
| 棋盘上的一格 | `Square`;参数、局部变量 `square`;查询/写入 `GetSquareValue` / `SetSquareValue` / `IsSquareEmpty` | CPW `Squares`、Stockfish `enum Square`、chess.hpp `Square`、python-chess `chess.Square`;有道 `square`「(棋盘上的)方格」;汉典、海词「方格=方形的格子」 |
|
|
11
11
|
| 格中的内容 | `SquareValue`;查询/写入 `GetValue` / `SetValue`;取值 `kEmpty` / `kX` / `kO` | LucHermitte `enum class SquareValue` 与 `Square::value()`;tictacrustle `SquareValue` / `set_value`;React 井字棋 `value`;海词、必应、有道、汉典 `value=值/数值` |
|
|
12
|
-
| 玩家→格中内容 | `PlayerToSquareValue` | 本项目 `move.h
|
|
12
|
+
| 玩家→格中内容 | `PlayerToSquareValue` | 本项目 `move.h`;命名规则见 `01-identifier-names.md` 的自由函数条 |
|
|
13
13
|
| 一条线的三个值 | `line_values` | 本项目 `game.cpp`;容器用复数 |
|
|
14
14
|
| 目标方格的值 | `target_square_value` | 本项目 `game.cpp`;角色写进名字 |
|
|
15
15
|
| 避免 | `Mark`、`Cell`、`Token` | 井字棋英文常用 `mark`(Exploratorium「marks in empty squares」),但 `Value` 在 Stockfish 是搜索分值、中文项目 `Value[3][3]` 是点位评估分;本项目按裁决统一 `SquareValue`。`cell` 是「单元格」「格网单元」;`token` 是词法单元/占用物 |
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
- 英文棋类资料把 `Square` 当坐标或位置:CPW 的 `Squares`、Stockfish 的 `enum Square`、chess.hpp、python-chess、Chess.com 都这样用;`Square` 只表示「哪一格」,不表示「格中是什么」。
|
|
20
20
|
- 井字棋英文资料更常用 `mark`:Exploratorium 写「putting their marks in empty squares」,SciMigo 的 `enum class Mark { Empty, X, O }` 也是直接先例。`Mark` 比 `SquareValue` 短,也不会把容器名重复进值类型。
|
|
21
21
|
- `SquareValue` 也有先例:LucHermitte 的 C++ 井字棋用 `enum class SquareValue` 与 `Square::value()`;Rust 的 tictacrustle 用 `SquareValue` 与 `set_value`;React 井字棋用 `value`。它和 `Square` 配对,读作「方格的值」。
|
|
22
|
-
- 风险在 `Value` 的歧义:Stockfish 的 `using Value = int` 是搜索分值;中文项目的 `Value[3][3]` 是点位评估分。`SquareValue` 有被读成「分值」的可能,所以类型注释以「方格的值。」起句,可用全角冒号在同一句补取值范围;取值也可由枚举行的 `///<`
|
|
22
|
+
- 风险在 `Value` 的歧义:Stockfish 的 `using Value = int` 是搜索分值;中文项目的 `Value[3][3]` 是点位评估分。`SquareValue` 有被读成「分值」的可能,所以类型注释以「方格的值。」起句,可用全角冒号在同一句补取值范围;取值也可由枚举行的 `///<` 表达,不另起枚举项清单;函数名见 [01-identifier-names.md](../01-identifier-names.md) 的「谓词与查询命名」。
|
|
23
23
|
- 本项目按用户裁决使用 `SquareValue`,与插件规则、代码和书稿统一;`Mark` 只在英文资料的取舍说明里出现,不作为代码名。
|
|
24
24
|
- 中文用词:`Square` 写「方格」,`SquareValue` 写「方格的值」;`格子` 更泛(汉典「方形的空栏或框子」),`棋格` 是「下棋的技艺水平」(汉典),都不能替代「方格」;`棋盘` 是整块盘面。`值` 用「方格的值」「空」「X」「O」;`标记` 不再用于这个类型。
|
|
25
25
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<!-- 读取时机:统一命名术语时;读完能回答:终局评分与搜索分的命名;预算:一次只开这一份(26 行),不自动串联。 -->
|
|
2
2
|
# scoring:终局评分与搜索分
|
|
3
3
|
|
|
4
|
-
终局评分若作为规则查询单独提供,写成 `ScoreFinishedGame(const Game&)
|
|
4
|
+
终局评分若作为规则查询单独提供,写成 `ScoreFinishedGame(const Game&)`,返回 `int`;分数怎么定、读哪些字段、放在哪一层见 [session-layer.md](../../design/session-layer.md) 的终局评分条。独立评分查询是否提供由项目决定;不提供时就在搜索里按终局分支就地评分。
|
|
5
5
|
|
|
6
6
|
## 语义对照
|
|
7
7
|
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
## 取舍
|
|
16
16
|
|
|
17
|
-
- 有几档就写几个枚举子,不用 `bool
|
|
17
|
+
- 有几档就写几个枚举子,不用 `bool`、整数或字符串;新增一档时补一个枚举子,`switch` 覆盖全部值的要求见 [session-layer.md](../../design/session-layer.md) 的难度条。
|
|
18
18
|
- 不用把 `Computer` 与 `Strength` 拼起来的名字,也不用 `Level`、`Mode`:`Strength` 暗示连续强度,`Level` 暗示关卡,`Mode` 与 `GameMode` 撞词。
|
|
19
19
|
- 难度在电脑对象里,不写进 `Game`;规则层不知道电脑怎样选,换难度只改 `difficulty_`,不重新构造对象。
|
|
20
20
|
- 模式与难度分成两个枚举,不合并成 `Mode`;一个回答「谁走」,一个回答「电脑怎样走」,合并后调用点要猜。
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
| 输出流 | `output`、`output_` | 与 `input` 对称,参数与成员使用同一个词根 |
|
|
12
12
|
| 诊断流 | `diagnostics`、`diagnostics_` | 项目把该流叫「诊断」;`error` 已指异常对象,`err` 会与它混读 |
|
|
13
13
|
|
|
14
|
-
- 这是 [01-identifier-names.md](../01-identifier-names.md)
|
|
14
|
+
- 这是 [01-identifier-names.md](../01-identifier-names.md) 中「参数或局部变量不缩写类型名」的延伸:流的方向属于接口契约,不能因为类型已写出就省略词根。
|
|
15
15
|
- `[in]`、`[out]`、`[in,out]` 是 Doxygen 的方向标签,不是标识符,保持原样。
|
|
16
16
|
- 标准流仍写 `std::cin`、`std::cout`、`std::cerr`;`std::out_of_range`、`out of range` 等类型名或英文短语不适用本规则。
|
|
17
17
|
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
## 头文件
|
|
15
15
|
|
|
16
16
|
- 每个头文件用一行 `#pragma once` 去重,写在文件头之后、第一组 `#include` 之前;不写 `#ifndef` / `#define` 宏守卫。Google 要求路径式守卫,本工作流明确偏离,原因是宏名要把项目名、目录名和文件名拼在一起,组件改名或头文件跨工程复用时宏名会失效,而 `#pragma once` 按文件身份判断;GCC、Clang、MSVC 都支持。模板见 `references/layout/02-file-template.md`。
|
|
17
|
-
-
|
|
17
|
+
- 头文件必须自包含:自己用到的类型、宏、标准库头都直接包含,不依赖调用方的包含顺序;自检方式见 `references/layout/02-file-template.md`。「头文件里不放什么」同见该文件的禁项,不在这里另列一份。
|
|
18
18
|
- `.cpp` 第一个包含自己的配对头,然后按下面的顺序包含。
|
|
19
19
|
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序。clang-format 的 Google 预设自带 `IncludeCategories`(`^<.*\.h>` 第一组、其余尖括号头第二组、引号头最后)并开了 `IncludeBlocks: Regroup`,配置里不写 `SortIncludes: false` 时这份分组与排序会自动落实。模板资产把 `IncludeBlocks` 设为 `Preserve`,保留手写空行分组,组内仍按上述类别排序。clang-format 按 `IncludeIsMainRegex` 从文件名推断配对头(`board_test.cpp` 配 `board.h`);文件名不遵循 `<name>` 或 `<name>_test` 时要自己把配对头放在第一条。`#include` 之间夹独立注释或 `#if` 会切断分组,这类位置要自己保持次序。需要把第三方库单独成组(例如 `<gtest/gtest.h>` 与标准库之间空一行)时,才在工程 `.clang-format` 里追加优先于 `^<.*` 的类别,并说明理由;预设已覆盖的分组不要重复手写。`clang-tidy` 的 `llvm-include-order` 按 LLVM 分组,与这条不同,不要拿来替代 clang-format。
|
|
20
20
|
- 本项目头用引号写裸文件名,例如 `#include "logging.h"`。构建时把该组件的 `include/` 加入 include 搜索路径(`target_include_directories(<target> PUBLIC .../include)`),不要写 `"base/logging.h"` 这种从源码根起的长路径。
|
|
21
21
|
- 公共头文件直接放在组件的 `include/` 下;不要为了「整齐」再加一层命名空间目录。
|
|
22
|
-
- 跨文件共享的常量用 `inline constexpr`,放在命名空间里并带 `k`
|
|
22
|
+
- 跨文件共享的常量用 `inline constexpr`,放在命名空间里并带 `k` 前缀;不写非 inline 的普通全局变量定义(头文件禁项见 `references/layout/02-file-template.md`)。
|
|
23
23
|
- 头文件与实现文件的槽位见 `references/layout/02-file-template.md`。
|
|
24
|
-
-
|
|
24
|
+
- 头文件禁项的完整清单以 `references/layout/02-file-template.md` 为准;内部链接的实现放 `.cpp`。
|
|
25
25
|
|
|
26
26
|
## 命名
|
|
27
27
|
|
|
@@ -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'
|
|
@@ -57,12 +58,19 @@ const isPredicateFirstOk = (declaration, firstDocLine) =>
|
|
|
57
58
|
: firstDocLine.includes('是否') && !PREDICATE_BANNED_PREFIX.test(firstDocLine)
|
|
58
59
|
const DECL_KEYWORDS = /^(class|struct|union|enum|namespace|using|typedef|template|#|if|else|for|while|switch|return|case|default)\b/
|
|
59
60
|
|
|
61
|
+
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'
|
|
62
|
+
|
|
60
63
|
function usage(message) {
|
|
61
64
|
if (message) process.stderr.write(`错误:${message}\n`)
|
|
62
|
-
process.stderr.write(
|
|
65
|
+
process.stderr.write(USAGE)
|
|
63
66
|
return EXIT.usage
|
|
64
67
|
}
|
|
65
68
|
|
|
69
|
+
function help() {
|
|
70
|
+
process.stdout.write(USAGE)
|
|
71
|
+
return EXIT.ok
|
|
72
|
+
}
|
|
73
|
+
|
|
66
74
|
function parseArgs(argv) {
|
|
67
75
|
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
76
|
for (let index = 0; index < argv.length; index += 1) {
|
|
@@ -169,6 +177,7 @@ const TAG_GROUPS = {
|
|
|
169
177
|
throw: 'throws',
|
|
170
178
|
exception: 'throws',
|
|
171
179
|
note: 'note',
|
|
180
|
+
see: 'note',
|
|
172
181
|
pre: 'pre',
|
|
173
182
|
post: 'pre',
|
|
174
183
|
}
|
|
@@ -508,14 +517,14 @@ function mentionsParam(text, name) {
|
|
|
508
517
|
}
|
|
509
518
|
|
|
510
519
|
/** 标记每一行是否位于 class/struct 体内,供数据成员注释位置检查使用。 */
|
|
511
|
-
function classMemberLines(lines) {
|
|
520
|
+
function classMemberLines(lines, kinds = 'class|struct') {
|
|
512
521
|
const flags = new Array(lines.length).fill(false)
|
|
513
522
|
const starts = []
|
|
514
523
|
let depth = 0
|
|
515
524
|
let pending = false
|
|
516
525
|
for (let index = 0; index < lines.length; index += 1) {
|
|
517
526
|
const code = lines[index].replace(/\/\/.*$/, '').trim()
|
|
518
|
-
if (
|
|
527
|
+
if (new RegExp(`^(?:template\\s*<[^>]*>\\s*)?(?:${kinds})\\b`).test(code)) pending = true
|
|
519
528
|
const opens = (code.match(/\{/g) ?? []).length
|
|
520
529
|
const closes = (code.match(/\}/g) ?? []).length
|
|
521
530
|
if (pending && opens > 0) {
|
|
@@ -657,6 +666,7 @@ export function checkText(text, options = {}) {
|
|
|
657
666
|
const issues = []
|
|
658
667
|
const add = (line, rule, message) => issues.push({ line, rule, message })
|
|
659
668
|
const inClass = classMemberLines(lines)
|
|
669
|
+
const inStruct = classMemberLines(lines, 'struct')
|
|
660
670
|
|
|
661
671
|
const style = options.commentStyle === 'block' ? 'block' : 'slash'
|
|
662
672
|
const firstContent = lines.findIndex((line) => line.trim() !== '')
|
|
@@ -721,7 +731,7 @@ export function checkText(text, options = {}) {
|
|
|
721
731
|
add(index + 1, 'list-indent', '注释列表项要在 /// 后空三格再写 -:`/// - `,与总起句拉开层级')
|
|
722
732
|
}
|
|
723
733
|
for (const tag of FORBIDDEN_TAGS) {
|
|
724
|
-
if (options.allowTags?.has(tag.slice(1))) continue
|
|
734
|
+
if (PRIVACY_TAGS.includes(tag) && options.allowTags?.has(tag.slice(1))) continue
|
|
725
735
|
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
726
736
|
}
|
|
727
737
|
}
|
|
@@ -737,7 +747,7 @@ export function checkText(text, options = {}) {
|
|
|
737
747
|
}
|
|
738
748
|
for (const content of doc.contents) {
|
|
739
749
|
for (const tag of FORBIDDEN_TAGS) {
|
|
740
|
-
if (options.allowTags?.has(tag.slice(1))) continue
|
|
750
|
+
if (PRIVACY_TAGS.includes(tag) && options.allowTags?.has(tag.slice(1))) continue
|
|
741
751
|
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
742
752
|
}
|
|
743
753
|
}
|
|
@@ -938,7 +948,11 @@ export function checkText(text, options = {}) {
|
|
|
938
948
|
const member = names.at(-1)
|
|
939
949
|
const previous = index > 0 ? lines[index - 1].trim() : ''
|
|
940
950
|
if (commentAt !== -1) {
|
|
941
|
-
if (line.slice(commentAt).startsWith('///<'))
|
|
951
|
+
if (line.slice(commentAt).startsWith('///<')) {
|
|
952
|
+
if (inStruct[index]) continue
|
|
953
|
+
add(index + 1, 'member-comment-trailing', `数据成员 ${member} 只用行尾 ///< 注释:///< 只留给枚举项和 struct 的短字段,类数据成员的主注释写在声明上一行`)
|
|
954
|
+
continue
|
|
955
|
+
}
|
|
942
956
|
if (!previous.startsWith('//')) {
|
|
943
957
|
add(index + 1, 'member-comment-missing', `数据成员 ${member} 只有行尾注释:主注释写在声明上一行,行尾不写补充`)
|
|
944
958
|
} else {
|
|
@@ -1021,7 +1035,7 @@ function main(argv) {
|
|
|
1021
1035
|
} catch (error) {
|
|
1022
1036
|
return usage(error.message)
|
|
1023
1037
|
}
|
|
1024
|
-
if (options.help) return
|
|
1038
|
+
if (options.help) return help()
|
|
1025
1039
|
const root = options.root ? path.resolve(options.root) : process.cwd()
|
|
1026
1040
|
try {
|
|
1027
1041
|
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]`。
|
|
@@ -8,10 +8,10 @@
|
|
|
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
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` 引用,只处理能处理的类型,其余继续抛。
|
|
@@ -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` 的横幅紧贴其下的节头,节按文件后缀分组,不按本仓目录命名。
|