-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathIncompatibleFunctionDeclarations.ql
53 lines (50 loc) · 1.97 KB
/
IncompatibleFunctionDeclarations.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
/**
* @id c/cert/incompatible-function-declarations
* @name DCL40-C: Do not create incompatible declarations of the same function or object
* @description Declaring incompatible functions, in other words same named function of different
* return types or with different numbers of parameters or parameter types, then
* accessing those functions can lead to undefined behaviour.
* @kind problem
* @precision high
* @problem.severity error
* @tags external/cert/id/dcl40-c
* correctness
* maintainability
* readability
* external/cert/obligation/rule
*/
import cpp
import codingstandards.c.cert
import codingstandards.cpp.types.Compatible
import ExternalIdentifiers
predicate interestedInFunctions(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
not f1 = f2 and
f1.getDeclaration() = f2.getDeclaration() and
f1.getName() = f2.getName()
}
from ExternalIdentifiers d, FunctionDeclarationEntry f1, FunctionDeclarationEntry f2
where
not isExcluded(f1, Declarations2Package::incompatibleFunctionDeclarationsQuery()) and
not isExcluded(f2, Declarations2Package::incompatibleFunctionDeclarationsQuery()) and
not f1 = f2 and
f1.getDeclaration() = d and
f2.getDeclaration() = d and
f1.getName() = f2.getName() and
(
//return type check
not FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>::equalReturnTypes(f1,
f2)
or
//parameter type check
not FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>::equalParameterTypes(f1,
f2)
) and
// Apply ordering on start line, trying to avoid the optimiser applying this join too early
// in the pipeline
exists(int f1Line, int f2Line |
f1.getLocation().hasLocationInfo(_, f1Line, _, _, _) and
f2.getLocation().hasLocationInfo(_, f2Line, _, _, _) and
f1Line >= f2Line
)
select f1, "The object $@ is not compatible with re-declaration $@", f1, f1.getName(), f2,
f2.getName()