-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathCompatibleDeclarationFunctionDefined.ql
63 lines (60 loc) · 2.2 KB
/
CompatibleDeclarationFunctionDefined.ql
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
62
63
/**
* @id c/misra/compatible-declaration-function-defined
* @name RULE-8-4: A compatible declaration shall be visible when a function with external linkage is defined
* @description A compatible declaration shall be visible when a function with external linkage is
* defined, otherwise program behaviour may be undefined.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-8-4
* readability
* maintainability
* correctness
* external/misra/c/2012/third-edition-first-revision
* external/misra/obligation/required
*/
import cpp
import codingstandards.c.misra
import codingstandards.cpp.Identifiers
import codingstandards.cpp.types.Compatible
predicate interestedInFunctions(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
f1.getDeclaration() instanceof ExternalIdentifiers and
f1.isDefinition() and
f1.getName() = f2.getName() and
f1.getDeclaration() = f2.getDeclaration() and
not f2.isDefinition() and
not f1.isFromTemplateInstantiation(_) and
not f2.isFromTemplateInstantiation(_)
}
from FunctionDeclarationEntry f1
where
not isExcluded(f1, Declarations4Package::compatibleDeclarationFunctionDefinedQuery()) and
f1.isDefinition() and
f1.getDeclaration() instanceof ExternalIdentifiers and
//no declaration matches exactly
(
not exists(FunctionDeclarationEntry f2 |
not f2.isDefinition() and
f2.getDeclaration() = f1.getDeclaration()
)
or
//or one exists that is close but incompatible in some way
exists(FunctionDeclarationEntry f2 |
f1.getName() = f2.getName() and
not f2.isDefinition() and
f2.getDeclaration() = f1.getDeclaration() and
(
//return types differ
not FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>::equalReturnTypes(f1,
f2)
or
//parameter types differ
not FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>::equalParameterTypes(f1,
f2)
or
//parameter names differ
parameterNamesUnmatched(f1, f2)
)
)
)
select f1, "No separate compatible declaration found for this definition."