Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita7955d8

Browse files
authored
Merge pull request#868 from github/lcartey/rule-8-7
`RULE-8-7`: Report external identifiers only referenced in single translation units
2 parents2c26288 +ce45538 commita7955d8

File tree

7 files changed

+30
-9
lines changed

7 files changed

+30
-9
lines changed

‎c/misra/src/rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,22 @@ predicate isReferencedInTranslationUnit(
4040
ExternalIdentifierse,ExternalIdentifierReferencer,TranslationUnitt
4141
){
4242
r.getExternalIdentifierTarget()=eand
43-
r.getFile()=t
43+
// Used within the translation unit or an included header
44+
r.getFile()=t.getAUserFile()
4445
}
4546

4647
fromExternalIdentifierse,ExternalIdentifierReferencea1,TranslationUnitt1
4748
where
4849
notisExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery())and
50+
// Only report external identifiers where we see the definition
51+
e.hasDefinition()and
4952
isReferencedInTranslationUnit(e,a1,t1)and
5053
// Not referenced in any other translation unit
5154
notexists(TranslationUnitt2|
5255
isReferencedInTranslationUnit(e, _,t2)and
5356
nott1=t2
54-
)
57+
)and
58+
// Definition is also in the same translation unit
59+
e.getDefinition().getFile()=t1.getAUserFile()
5560
selecte,"Declaration with external linkage is accessed in only one translation unit $@.",a1,
5661
a1.toString()
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
| test.h:2:12:2:13 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:4:3:4:4 | i1 | i1 |
2-
| test.h:3:5:3:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:5:3:5:4 | i2 | i2 |
3-
| test.h:5:13:5:14 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:7:3:7:4 | call to f2 | call to f2 |
1+
| test2.h:2:13:2:14 | f6 | Declaration with external linkage is accessed in only one translation unit $@. | test2.h:3:22:3:23 | call to f6 | call to f6 |
2+
| test.c:3:5:3:6 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:11:3:11:4 | i1 | i1 |
3+
| test.c:4:5:4:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:12:3:12:4 | i2 | i2 |
4+
| test.c:6:6:6:7 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:14:3:14:4 | call to f2 | call to f2 |

‎c/misra/test/rules/RULE-8-7/test.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#include"test.h"
2+
inti=0;
3+
inti1=0;
4+
inti2;// NON_COMPLIANT - accessed one translation unit
5+
voidf1() {}// Definition
6+
voidf2() {}// Definition
7+
staticvoidf3(){};// COMPLIANT - internal linkage
8+
voidf4() {}// Definition
29
voidf() {
310
i=0;
411
i1=0;

‎c/misra/test/rules/RULE-8-7/test.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
externinti;// COMPLIANT - accessed multiple translation units
22
externinti1;// NON_COMPLIANT - accessed one translation unit
3-
inti2;// NON_COMPLIANT - accessed one translation unit
43
externvoidf1();// COMPLIANT - accessed multiple translation units
54
externvoidf2();// NON_COMPLIANT - accessed one translation unit
6-
staticvoidf3();// COMPLIANT -internal linkage
7-
externvoidf3();// COMPLIANT -internal linkage
5+
externvoidf4();// COMPLIANT -accessed across translation units
6+
externvoidf5();// COMPLIANT -no definition
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include"test.h"
1+
#include"test2.h"
22
voidf() {
33
i=0;
44
f1();
5+
f4();
6+
f5();
57
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include"test.h"
2+
externvoidf6() {}// NON_COMPLIANT
3+
staticvoidtest() {f6(); }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-`RULE-8-7` -`ShouldNotBeDefinedWithExternalLinkage.ql`:
2+
- Remove false positives where the declaration is not defined in the database.
3+
- Remove false positives where the definition and reference are in different translation units.
4+
- Remove false positives where the reference occurs in a header file.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp