Skip to content

Commit a7955d8

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 parents 2c26288 + ce45538 commit a7955d8

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,22 @@ predicate isReferencedInTranslationUnit(
4040
ExternalIdentifiers e, ExternalIdentifierReference r, TranslationUnit t
4141
) {
4242
r.getExternalIdentifierTarget() = e and
43-
r.getFile() = t
43+
// Used within the translation unit or an included header
44+
r.getFile() = t.getAUserFile()
4445
}
4546

4647
from ExternalIdentifiers e, ExternalIdentifierReference a1, TranslationUnit t1
4748
where
4849
not isExcluded(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
not exists(TranslationUnit t2 |
5255
isReferencedInTranslationUnit(e, _, t2) and
5356
not t1 = t2
54-
)
57+
) and
58+
// Definition is also in the same translation unit
59+
e.getDefinition().getFile() = t1.getAUserFile()
5560
select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1,
5661
a1.toString()
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

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#include "test.h"
2+
int i = 0;
3+
int i1 = 0;
4+
int i2; // NON_COMPLIANT - accessed one translation unit
5+
void f1() {} // Definition
6+
void f2() {} // Definition
7+
static void f3(){}; // COMPLIANT - internal linkage
8+
void f4() {} // Definition
29
void f() {
310
i = 0;
411
i1 = 0;

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern int i; // COMPLIANT - accessed multiple translation units
22
extern int i1; // NON_COMPLIANT - accessed one translation unit
3-
int i2; // NON_COMPLIANT - accessed one translation unit
43
extern void f1(); // COMPLIANT - accessed multiple translation units
54
extern void f2(); // NON_COMPLIANT - accessed one translation unit
6-
static void f3(); // COMPLIANT - internal linkage
7-
extern void f3(); // COMPLIANT - internal linkage
5+
extern void f4(); // COMPLIANT - accessed across translation units
6+
extern void f5(); // COMPLIANT - no definition

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include "test.h"
1+
#include "test2.h"
22
void f() {
33
i = 0;
44
f1();
5+
f4();
6+
f5();
57
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "test.h"
2+
extern void f6() {} // NON_COMPLIANT
3+
static void test() { f6(); }

change_notes/2025-03-09-rule-8-7.md

+4
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)