Skip to content

Commit

Permalink
精炼词句
Browse files Browse the repository at this point in the history
补充规则关系
  • Loading branch information
brotherbeer authored Jan 6, 2023
1 parent bc5682b commit d4fa13c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
7 changes: 5 additions & 2 deletions c-cpp-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@
"level": "warning",
"comment": "头文件中定义了匿名命名空间,即相当于在头文件中定义了静态数据,头文件被多个源文件包含时便会造成数据冗余。",
"tag": "global",
"related": "ID_staticInHeader",
"related": "ID_staticInHeader,ID_unsuitableDeclaration",
"standard": "ISO/IEC 14882:2011 7.3.1.1",
"reference": "C++ Core Guidelines SF.21,MISRA C++ 2008 7-3-3"
},
Expand Down Expand Up @@ -2121,6 +2121,7 @@
"level": "warning",
"comment": "头文件中由 static 关键字声明的对象、数组或函数,会在每个包含该头文件的翻译单元或模块中生成副本造成数据冗余,如果将静态数据误用作全局数据也会造成逻辑错误。",
"tag": "global",
"related": "ID_unsuitableDeclaration",
"standard": "ISO/IEC 14882:2011 3.5(3)"
},
"ID_nameTooShort": {
Expand Down Expand Up @@ -3630,8 +3631,10 @@
"ID_unsuitableDeclaration": {
"checkPoint": "在合理的位置声明",
"level": "suggestion",
"comment": "在合理的位置声明",
"comment": "如果声明的位置不合理会降低代码的可维护性,甚至会导致标准未定义的行为",
"tag": "declaration",
"related": "ID_staticInHeader,ID_anonymousNamespaceInHeader",
"standard": "ISO/IEC 9899:1999 6.7.1(5)-undefined,ISO/IEC 9899:2011 6.7.1(7)-undefined",
"reference": "MISRA C++ 2008 3-1-2,MISRA C++ 2008 3-3-1"
},
"ID_missingStatic": {
Expand Down
32 changes: 25 additions & 7 deletions c-cpp-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -4154,6 +4154,10 @@ const Arr& getArr() {
<br/>
<br/>

#### 相关
ID_unsuitableDeclaration
<br/>

#### 依据
ISO/IEC 14882:2011 3.5(3)
<br/>
Expand Down Expand Up @@ -4181,6 +4185,7 @@ namespace { // Non-compliant

#### 相关
ID_staticInHeader
ID_unsuitableDeclaration
<br/>

#### 依据
Expand Down Expand Up @@ -8765,23 +8770,36 @@ ID_unsuitableDeclaration&emsp;&emsp;&emsp;&emsp;&nbsp;:bulb: declaration suggest

<hr/>

声明时应遵循如下原则:
如果声明的位置不合理会降低代码的可维护性,甚至会导致标准未定义的行为。

应遵循如下原则:
- 外部链接的对象或函数应在头文件中声明
- 内部链接的对象或函数应在源文件中声明
- 不应在函数作用域内进行外部声明
- 内部链接的对象或函数应在源文件中声明,不应在头文件中声明
- 避免在头文件外手工书写外部声明
- 避免在局部作用域内声明函数或全局对象

示例:
```
int bar()
int fun()
{
extern int g; // Non-compliant
extern int fun(); // Non-compliant
extern int g; // Non-compliant, bad practice
extern int foo(); // Non-compliant, bad practice
static int bar(); // Non-compliant, undefined behavior
....
}
```
外部链接的对象或函数应通过头文件引入,如果分散在函数中声明是不便于统一管理和维护的。
外部链接的对象或函数应通过头文件引入,如果分散在函数中声明是不便于统一管理和维护的。另外,在局部作用域中使用除 extern 之外的存储类说明符声明函数会导致标准未定义的行为。
<br/>
<br/>

#### 相关
ID_staticInHeader
ID_anonymousNamespaceInHeader
<br/>

#### 依据
ISO/IEC 9899:1999 6.7.1(5)-undefined
ISO/IEC 9899:2011 6.7.1(7)-undefined
<br/>

#### 参考
Expand Down

0 comments on commit d4fa13c

Please sign in to comment.