Skip to content

RULE-8-7: Report external identifiers only referenced in single translation units #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@ predicate isReferencedInTranslationUnit(
ExternalIdentifiers e, ExternalIdentifierReference r, TranslationUnit t
) {
r.getExternalIdentifierTarget() = e and
r.getFile() = t
// Used within the translation unit or an included header
r.getFile() = t.getAUserFile()
}

from ExternalIdentifiers e, ExternalIdentifierReference a1, TranslationUnit t1
where
not isExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery()) and
// Only report external identifiers where we see the definition
e.hasDefinition() and
isReferencedInTranslationUnit(e, a1, t1) and
// Not referenced in any other translation unit
not exists(TranslationUnit t2 |
isReferencedInTranslationUnit(e, _, t2) and
not t1 = t2
)
) and
// Definition is also in the same translation unit
e.getDefinition().getFile() = t1.getAUserFile()
select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1,
a1.toString()
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
7 changes: 7 additions & 0 deletions c/misra/test/rules/RULE-8-7/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include "test.h"
int i = 0;
int i1 = 0;
int i2; // NON_COMPLIANT - accessed one translation unit
void f1() {} // Definition
void f2() {} // Definition
static void f3() {}; // COMPLIANT - internal linkage
void f4() {} // Definition
void f() {
i = 0;
i1 = 0;
Expand Down
5 changes: 2 additions & 3 deletions c/misra/test/rules/RULE-8-7/test.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern int i; // COMPLIANT - accessed multiple translation units
extern int i1; // NON_COMPLIANT - accessed one translation unit
int i2; // NON_COMPLIANT - accessed one translation unit
extern void f1(); // COMPLIANT - accessed multiple translation units
extern void f2(); // NON_COMPLIANT - accessed one translation unit
static void f3(); // COMPLIANT - internal linkage
extern void f3(); // COMPLIANT - internal linkage
extern void f4(); // COMPLIANT - accessed across translation units
extern void f5(); // COMPLIANT - no definition
4 changes: 3 additions & 1 deletion c/misra/test/rules/RULE-8-7/test1.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "test.h"
#include "test2.h"
void f() {
i = 0;
f1();
f4();
f5();
}
3 changes: 3 additions & 0 deletions c/misra/test/rules/RULE-8-7/test2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "test.h"
extern void f6() {} // NON_COMPLIANT
static void test() { f6(); }
4 changes: 4 additions & 0 deletions change_notes/2025-03-09-rule-8-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `RULE-8-7` - `ShouldNotBeDefinedWithExternalLinkage.ql`:
- Remove false positives where the declation is not defined in the database.
- Remove false positives where the definition and reference are in different translation units.
- Remove false positives where the reference occurs in a header file.
Loading