Skip to content

Commit fed1bce

Browse files
committed
C++: Make vararg utilities internal for now.
1 parent 4fce201 commit fed1bce

File tree

6 files changed

+69
-56
lines changed

6 files changed

+69
-56
lines changed

cpp/ql/src/semmle/code/cpp/Function.qll

-6
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,6 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
364364
/** Holds if this function is a varargs function. */
365365
predicate isVarargs() { hasSpecifier("varargs") }
366366

367-
/**
368-
* Gets the index of the `...` parameter, if any. If present, the value will always be equal to
369-
* `getNumberOfParameters()`.
370-
*/
371-
int getEllipsisParameterIndex() { isVarargs() and result = getNumberOfParameters() }
372-
373367
/** Gets a type that is specified to be thrown by the function. */
374368
Type getAThrownType() { result = getADeclarationEntry().getAThrownType() }
375369

cpp/ql/src/semmle/code/cpp/exprs/Call.qll

+1-44
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import semmle.code.cpp.exprs.Expr
22
import semmle.code.cpp.Function
33
private import semmle.code.cpp.dataflow.EscapesTree
44

5-
/**
6-
* Gets the index of the `...` parameter, if any.
7-
*/
8-
private int getEllipsisParameterIndex(RoutineType type) {
9-
type.getParameterType(result) instanceof UnknownType
10-
}
11-
125
/**
136
* A C/C++ call.
147
*
@@ -85,32 +78,6 @@ abstract class Call extends Expr, NameQualifiableElement {
8578

8679
override string toString() { none() }
8780

88-
/**
89-
* Gets the index of the `...` parameter, if any. This will be one greater than the index of the
90-
* last declared positional parameter.
91-
*/
92-
abstract int getEllipsisParameterIndex();
93-
94-
/**
95-
* Gets the index of the parameter that will be initialized with the value of the argument
96-
* specified by `argIndex`. For ordinary positional parameters, the argument and parameter indices
97-
* will be equal. For a call to a varargs function, all arguments passed to the `...` will be
98-
* mapped to the index returned by `getEllipsisParameterIndex()`.
99-
*/
100-
final int getParameterIndexForArgument(int argIndex) {
101-
exists(getArgument(argIndex)) and
102-
if argIndex >= getEllipsisParameterIndex()
103-
then result = getEllipsisParameterIndex()
104-
else result = argIndex
105-
}
106-
107-
/**
108-
* Holds if the argument specified by `index` is an argument to the `...` of a varargs function.
109-
*/
110-
final predicate isEllipsisArgumentIndex(int index) {
111-
exists(getArgument(index)) and index >= getEllipsisParameterIndex()
112-
}
113-
11481
/**
11582
* Holds if this call passes the variable accessed by `va` by
11683
* reference as the `i`th argument.
@@ -229,7 +196,7 @@ class FunctionCall extends Call, @funbindexpr {
229196
* constructor calls, this predicate instead gets the `Class` of the constructor
230197
* being called.
231198
*/
232-
private Type getTargetType() { result = Call.super.getType().stripType() }
199+
Type getTargetType() { result = Call.super.getType().stripType() }
233200

234201
/**
235202
* Gets the expected return type of the function called by this call.
@@ -292,12 +259,6 @@ class FunctionCall extends Call, @funbindexpr {
292259
else result = "call to unknown function"
293260
}
294261

295-
final override int getEllipsisParameterIndex() {
296-
if getTargetType() instanceof RoutineType
297-
then result = getEllipsisParameterIndex(getTargetType())
298-
else result = getTarget().getEllipsisParameterIndex()
299-
}
300-
301262
override predicate mayBeImpure() {
302263
this.getChild(_).mayBeImpure() or
303264
this.getTarget().mayHaveSideEffects() or
@@ -417,10 +378,6 @@ class ExprCall extends Call, @callexpr {
417378
override string toString() { result = "call to expression" }
418379

419380
override Function getTarget() { none() }
420-
421-
final override int getEllipsisParameterIndex() {
422-
result = getEllipsisParameterIndex(getExpr().getType().stripType())
423-
}
424381
}
425382

426383
/**

cpp/ql/src/semmle/code/cpp/exprs/ObjectiveC.qll

-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ deprecated class MessageExpr extends Expr, Call {
6363
override Expr getArgument(int n) { none() }
6464

6565
override int getPrecedence() { none() }
66-
67-
final override int getEllipsisParameterIndex() { none() }
6866
}
6967

7068
/**

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedFunction.qll

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semmle.code.cpp.ir.implementation.raw.IR
33
private import semmle.code.cpp.ir.implementation.Opcode
44
private import semmle.code.cpp.ir.internal.CppType
55
private import semmle.code.cpp.ir.internal.IRUtilities
6+
private import semmle.code.cpp.ir.internal.VarArgs
67
private import semmle.code.cpp.ir.implementation.internal.OperandTag
78
private import semmle.code.cpp.ir.internal.TempVariableTag
89
private import InstructionTag
@@ -28,7 +29,7 @@ private int getEllipsisVariableByteSize() {
2829
max(Call call, int callSize |
2930
callSize =
3031
sum(int argIndex |
31-
call.isEllipsisArgumentIndex(argIndex)
32+
isEllipsisArgumentIndex(call, argIndex)
3233
|
3334
call.getArgument(argIndex).getType().getSize()
3435
)
@@ -87,7 +88,7 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
8788
final private TranslatedParameter getParameter(int index) {
8889
result = getTranslatedParameter(func.getParameter(index))
8990
or
90-
index = func.getEllipsisParameterIndex() and
91+
index = getEllipsisParameterIndexForFunction(func) and
9192
result = getTranslatedEllipsisParameter(func)
9293
}
9394

@@ -144,7 +145,7 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
144145
child = getParameter(paramIndex) and
145146
if
146147
exists(func.getParameter(paramIndex + 1)) or
147-
func.getEllipsisParameterIndex() = paramIndex + 1
148+
getEllipsisParameterIndexForFunction(func) = paramIndex + 1
148149
then result = getParameter(paramIndex + 1).getFirstInstruction()
149150
else result = getConstructorInitList().getFirstInstruction()
150151
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Utilities for determining which parameters and arguments correspond to the `...` parameter for
3+
* varargs functions.
4+
*/
5+
6+
private import cpp
7+
8+
/**
9+
* Gets the index of the `...` parameter, if any. If present, the value will always be equal to
10+
* `func.getNumberOfParameters()`.
11+
*/
12+
int getEllipsisParameterIndexForFunction(Function func) {
13+
func.isVarargs() and result = func.getNumberOfParameters()
14+
}
15+
16+
/**
17+
* Gets the index of the `...` parameter, if any.
18+
*/
19+
int getEllipsisParameterIndexForRoutineType(RoutineType type) {
20+
// Since the extractor doesn't record this information directly, we look for routine types whose
21+
// last parameter type is `UnknownType`.
22+
type.getParameterType(result) instanceof UnknownType and
23+
result = strictcount(type.getAParameterType()) - 1
24+
}
25+
26+
/**
27+
* Gets the index of the `...` parameter, if any. This will be one greater than the index of the
28+
* last declared positional parameter.
29+
*/
30+
int getEllipsisParameterIndex(Call call) {
31+
exists(FunctionCall funcCall |
32+
funcCall = call and
33+
if funcCall.getTargetType() instanceof RoutineType
34+
then result = getEllipsisParameterIndexForRoutineType(funcCall.getTargetType())
35+
else result = getEllipsisParameterIndexForFunction(funcCall.getTarget())
36+
)
37+
or
38+
exists(ExprCall exprCall |
39+
exprCall = call and
40+
result = getEllipsisParameterIndexForRoutineType(exprCall.getExpr().getType().stripType())
41+
)
42+
}
43+
44+
/**
45+
* Gets the index of the parameter that will be initialized with the value of the argument
46+
* specified by `argIndex`. For ordinary positional parameters, the argument and parameter indices
47+
* will be equal. For a call to a varargs function, all arguments passed to the `...` will be
48+
* mapped to the index returned by `getEllipsisParameterIndex()`.
49+
*/
50+
int getParameterIndexForArgument(Call call, int argIndex) {
51+
exists(call.getArgument(argIndex)) and
52+
if argIndex >= getEllipsisParameterIndex(call)
53+
then result = getEllipsisParameterIndex(call)
54+
else result = argIndex
55+
}
56+
57+
/**
58+
* Holds if the argument specified by `index` is an argument to the `...` of a varargs function.
59+
*/
60+
predicate isEllipsisArgumentIndex(Call call, int index) {
61+
exists(call.getArgument(index)) and index >= getEllipsisParameterIndex(call)
62+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cpp
2+
import semmle.code.cpp.ir.internal.VarArgs
23

34
from Call call, int argIndex, int paramIndex
45
where
5-
paramIndex = call.getParameterIndexForArgument(argIndex)
6+
paramIndex = getParameterIndexForArgument(call, argIndex)
67
select call, argIndex, paramIndex

0 commit comments

Comments
 (0)