Skip to content

Commit 4ab01a2

Browse files
committed
Refactor EXP37-C rule to simplify function argument checks and improve performance
1 parent a98c55c commit 4ab01a2

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

c/cert/src/rules/EXP37-C/DoNotCallFunctionsWithIncompatibleArguments.ql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import cpp
2020
import codingstandards.c.cert
2121
import codingstandards.cpp.MistypedFunctionArguments
2222

23-
from FunctionCall fc, Function f, Parameter p
23+
from FunctionCall fc, Parameter p
2424
where
2525
not isExcluded(fc, ExpressionsPackage::doNotCallFunctionsWithIncompatibleArgumentsQuery()) and
26+
p = fc.getTarget().getAParameter() and
2627
(
27-
mistypedFunctionArguments(fc, f, p)
28+
mistypedFunctionArguments(fc, p)
2829
or
29-
complexArgumentPassedToRealParameter(fc, f, p)
30+
complexArgumentPassedToRealParameter(fc, p)
3031
)
3132
select fc,
32-
"Argument $@ in call to " + f.toString() + " is incompatible with parameter " + p.getTypedName() +
33-
".", fc.getArgument(p.getIndex()) as arg, arg.toString()
33+
"Argument $@ in " + fc.toString() + " is incompatible with parameter " + p.getTypedName() + ".",
34+
fc.getArgument(p.getIndex()) as arg, arg.toString()

cpp/common/src/codingstandards/cpp/MistypedFunctionArguments.qll

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,22 @@ private predicate isTypeInComplexDomain(FloatingPointType type) {
9191
type.getUnderlyingType().(FloatingPointType).getDomain() instanceof ComplexDomain
9292
}
9393

94-
predicate mistypedFunctionArguments(FunctionCall fc, Function f, Parameter p) {
95-
f = fc.getTarget() and
96-
p = f.getAParameter() and
97-
hasZeroParamDecl(f) and
98-
isCompiledAsC(f.getFile()) and
99-
not f.isVarargs() and
100-
not f instanceof BuiltInFunction and
101-
p.getIndex() < fc.getNumberOfArguments() and
102-
// Parameter p and its corresponding call argument must have mismatched types
103-
not argMayBeUsed(fc.getArgument(p.getIndex()), p)
94+
predicate mistypedFunctionArguments(FunctionCall fc, Parameter p) {
95+
exists(Function f |
96+
f = fc.getTarget() and
97+
p = f.getAParameter() and
98+
hasZeroParamDecl(f) and
99+
isCompiledAsC(f.getFile()) and
100+
not f.isVarargs() and
101+
not f instanceof BuiltInFunction and
102+
p.getIndex() < fc.getNumberOfArguments() and
103+
// Parameter p and its corresponding call argument must have mismatched types
104+
not argMayBeUsed(fc.getArgument(p.getIndex()), p)
105+
)
104106
}
105107

106-
predicate complexArgumentPassedToRealParameter(FunctionCall fc, Function f, Parameter p) {
107-
f = fc.getTarget() and
108-
p = f.getAParameter() and
108+
predicate complexArgumentPassedToRealParameter(FunctionCall fc, Parameter p) {
109+
p = fc.getTarget().getAParameter() and
109110
// Some implementations implicitly convert complex floating point values by
110111
// extracting the real part of the complex number (in-place or via a creal() call).
111112
// This predicate holds in those cases unless the value is explicitly converted.

0 commit comments

Comments
 (0)