-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathShouldNotBeDefinedWithExternalLinkage.ql
More file actions
61 lines (55 loc) · 2.31 KB
/
ShouldNotBeDefinedWithExternalLinkage.ql
File metadata and controls
61 lines (55 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @id c/misra/should-not-be-defined-with-external-linkage
* @name RULE-8-7: Functions and objects should not be defined with external linkage if they are referenced in only one
* @description Declarations with external linkage that are referenced in only one translation unit
* can indicate an intention to only have those identifiers accessible in that
* translation unit and accidental future accesses in other translation units can lead
* to confusing program behaviour.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-8-7
* correctness
* maintainability
* readability
* external/misra/c/2012/third-edition-first-revision
* external/misra/obligation/advisory
*/
import cpp
import codingstandards.c.misra
import codingstandards.cpp.Identifiers
import codingstandards.cpp.Scope
ExternalIdentifiers getExternalIdentifierTarget(NameQualifiableElement nqe) {
result = nqe.(Access).getTarget()
or
result = nqe.(FunctionCall).getTarget()
}
/**
* A reference to an external identifier, either as an `Access` or a `FunctionCall`.
*/
class ExternalIdentifierReference extends NameQualifiableElement {
ExternalIdentifierReference() { exists(getExternalIdentifierTarget(this)) }
ExternalIdentifiers getExternalIdentifierTarget() { result = getExternalIdentifierTarget(this) }
}
predicate isReferencedInTranslationUnit(
ExternalIdentifiers e, ExternalIdentifierReference r, TranslationUnit t
) {
r.getExternalIdentifierTarget() = e and
// 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()