Skip to content

Commit 39f7c94

Browse files
committed
RULE-8-7: Require the reference to exist in the same TU
The translation unit of the definition needs to be the same as the translation unit of the reference, otherwise it is not eligible for internal linkage.
1 parent 32fd091 commit 39f7c94

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ where
5151
not exists(TranslationUnit t2 |
5252
isReferencedInTranslationUnit(e, _, t2) and
5353
not t1 = t2
54-
)
54+
) and
55+
// Definition is also in the same translation unit
56+
e.getDefinition().getFile() = t1.getAUserFile()
5557
select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1,
5658
a1.toString()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.c:3:5:3:6 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:10:3:10:4 | i1 | i1 |
2-
| test.c:4:5:4:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:11:3:11:4 | i2 | i2 |
3-
| test.c:6:6:6:7 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:13:3:13:4 | call to f2 | call to f2 |
1+
| 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 |
2+
| 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 |
3+
| 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ int i2; // NON_COMPLIANT - accessed one translation unit
55
void f1() {} // Definition
66
void f2() {} // Definition
77
static void f3() {}; // COMPLIANT - internal linkage
8+
void f4() {} // Definition
89
void f() {
910
i = 0;
1011
i1 = 0;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern int i; // COMPLIANT - accessed multiple translation units
22
extern int i1; // NON_COMPLIANT - accessed one translation unit
33
extern void f1(); // COMPLIANT - accessed multiple translation units
4-
extern void f2(); // NON_COMPLIANT - accessed one translation unit
4+
extern void f2(); // NON_COMPLIANT - accessed one translation unit
5+
extern void f4(); // COMPLIANT - accessed across translation units

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
void f() {
33
i = 0;
44
f1();
5+
f4();
56
}

0 commit comments

Comments
 (0)