Skip to content

Commit 58673c4

Browse files
committed
C++: switch to TranslatedThisParameter
1 parent be74616 commit 58673c4

File tree

3 files changed

+62
-76
lines changed

3 files changed

+62
-76
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ private import cpp
22

33
newtype TInstructionTag =
44
OnlyInstructionTag() or // Single instruction (not including implicit Load)
5-
InitializeThisAddressTag() or
6-
InitializeThisTag() or
7-
InitializeThisIndirectionAddressTag() or
8-
InitializeThisIndirectionTag() or
95
InitializerVariableAddressTag() or
106
InitializerLoadStringTag() or
117
InitializerStoreTag() or

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ newtype TTranslatedElement =
400400
TTranslatedConstructorInitList(Function func) { translateFunction(func) } or
401401
// A destructor destruction list
402402
TTranslatedDestructorDestructionList(Function func) { translateFunction(func) } or
403+
TTranslatedThisParameter(Function func) {
404+
translateFunction(func) and func.isMember() and not func.isStatic()
405+
} or
403406
// A function parameter
404407
TTranslatedParameter(Parameter param) {
405408
exists(Function func |

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

Lines changed: 59 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
7373
final override Function getFunction() { result = func }
7474

7575
final override TranslatedElement getChild(int id) {
76-
id = -4 and result = getReadEffects()
76+
id = -5 and result = getReadEffects()
7777
or
78-
id = -3 and result = getConstructorInitList()
78+
id = -4 and result = getConstructorInitList()
7979
or
80-
id = -2 and result = getBody()
80+
id = -3 and result = getBody()
8181
or
82-
id = -1 and result = getDestructorDestructionList()
82+
id = -2 and result = getDestructorDestructionList()
8383
or
84-
id >= 0 and result = getParameter(id)
84+
id >= -1 and result = getParameter(id)
8585
}
8686

8787
final private TranslatedConstructorInitList getConstructorInitList() {
@@ -97,6 +97,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
9797
final private TranslatedReadEffects getReadEffects() { result = getTranslatedReadEffects(func) }
9898

9999
final private TranslatedParameter getParameter(int index) {
100+
result = getTranslatedThisParameter(func) and
101+
index = -1
102+
or
100103
result = getTranslatedParameter(func.getParameter(index))
101104
or
102105
index = getEllipsisParameterIndexForFunction(func) and
@@ -117,29 +120,13 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
117120
(
118121
tag = InitializeNonLocalTag() and
119122
if exists(getThisType())
120-
then result = getInstruction(InitializeThisAddressTag())
123+
then result = getParameter(-1).getFirstInstruction()
121124
else
122125
if exists(getParameter(0))
123126
then result = getParameter(0).getFirstInstruction()
124127
else result = getBody().getFirstInstruction()
125128
)
126129
or
127-
tag = InitializeThisAddressTag() and
128-
result = getInstruction(InitializeThisTag())
129-
or
130-
tag = InitializeThisTag() and
131-
result = getInstruction(InitializeThisIndirectionAddressTag())
132-
or
133-
tag = InitializeThisIndirectionAddressTag() and
134-
result = getInstruction(InitializeThisIndirectionTag())
135-
or
136-
(
137-
tag = InitializeThisIndirectionTag() and
138-
if exists(getParameter(0))
139-
then result = getParameter(0).getFirstInstruction()
140-
else result = getConstructorInitList().getFirstInstruction()
141-
)
142-
or
143130
tag = ReturnValueAddressTag() and
144131
result = getInstruction(ReturnTag())
145132
or
@@ -193,23 +180,6 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
193180
opcode instanceof Opcode::InitializeNonLocal and
194181
resultType = getUnknownType()
195182
or
196-
tag = InitializeThisAddressTag() and
197-
opcode instanceof Opcode::VariableAddress and
198-
resultType = getTypeForGLValue(any(UnknownType t)) and
199-
exists(getThisType())
200-
or
201-
tag = InitializeThisTag() and
202-
opcode instanceof Opcode::InitializeParameter and
203-
resultType = getTypeForGLValue(getThisType())
204-
or
205-
tag = InitializeThisIndirectionAddressTag() and
206-
opcode instanceof Opcode::Load and
207-
resultType = getTypeForGLValue(getThisType())
208-
or
209-
tag = InitializeThisIndirectionTag() and
210-
opcode instanceof Opcode::InitializeIndirection and
211-
resultType = getTypeForPRValue(getThisType())
212-
or
213183
tag = ReturnValueAddressTag() and
214184
opcode instanceof Opcode::VariableAddress and
215185
resultType = getTypeForGLValue(getReturnType()) and
@@ -252,21 +222,6 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
252222
hasReturnValue() and
253223
operandTag instanceof AddressOperandTag and
254224
result = getInstruction(ReturnValueAddressTag())
255-
or
256-
tag = InitializeThisTag() and
257-
exists(getThisType()) and
258-
operandTag instanceof AddressOperandTag and
259-
result = getInstruction(InitializeThisAddressTag())
260-
or
261-
tag = InitializeThisIndirectionAddressTag() and
262-
exists(getThisType()) and
263-
operandTag instanceof AddressOperandTag and
264-
result = getInstruction(InitializeThisAddressTag())
265-
or
266-
tag = InitializeThisIndirectionTag() and
267-
exists(getThisType()) and
268-
operandTag instanceof AddressOperandTag and
269-
result = getInstruction(InitializeThisIndirectionAddressTag())
270225
}
271226

272227
final override CppType getInstructionMemoryOperandType(
@@ -280,23 +235,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
280235
tag = AliasedUseTag() and
281236
operandTag instanceof SideEffectOperandTag and
282237
result = getUnknownType()
283-
or
284-
tag = InitializeThisIndirectionAddressTag() and
285-
exists(getThisType()) and
286-
operandTag instanceof LoadOperandTag and
287-
result = getTypeForGLValue(getThisType())
288238
}
289239

290240
final override IRVariable getInstructionVariable(InstructionTag tag) {
291-
tag = InitializeThisAddressTag() and
292-
result = getThisVariable()
293-
or
294-
tag = InitializeThisTag() and
295-
result = getThisVariable()
296-
or
297-
tag = InitializeThisIndirectionTag() and
298-
result = getThisVariable()
299-
or
300241
tag = ReturnValueAddressTag() and
301242
result = getReturnVariable()
302243
}
@@ -352,7 +293,9 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
352293
* Gets the single `InitializeThis` instruction for this function. Holds only
353294
* if the function is an instance member function, constructor, or destructor.
354295
*/
355-
final Instruction getInitializeThisInstruction() { result = getInstruction(InitializeThisTag()) }
296+
final Instruction getInitializeThisInstruction() {
297+
result = getTranslatedThisParameter(func).getInstruction(InitializerStoreTag())
298+
}
356299

357300
/**
358301
* Gets the type pointed to by the `this` pointer for this function (i.e. `*this`).
@@ -393,6 +336,11 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
393336
final Type getReturnType() { result = func.getType() }
394337
}
395338

339+
/**
340+
* Gets the `TranslatedThisParameter` for function `func`, if one exists.
341+
*/
342+
TranslatedThisParameter getTranslatedThisParameter(Function func) { result.getFunction() = func }
343+
396344
/**
397345
* Gets the `TranslatedPositionalParameter` that represents parameter `param`.
398346
*/
@@ -407,8 +355,9 @@ TranslatedEllipsisParameter getTranslatedEllipsisParameter(Function func) {
407355

408356
/**
409357
* The IR translation of a parameter to a function. This can be either a user-declared parameter
410-
* (`TranslatedPositionParameter`) or the synthesized parameter used to represent a `...` in a
411-
* varargs function (`TranslatedEllipsisParameter`).
358+
* (`TranslatedPositionParameter`), the synthesized parameter used to represent `this`, or the
359+
* synthesized parameter used to represent a `...` in a varargs function
360+
* (`TranslatedEllipsisParameter`).
412361
*/
413362
abstract class TranslatedParameter extends TranslatedElement {
414363
final override TranslatedElement getChild(int id) { none() }
@@ -455,7 +404,7 @@ abstract class TranslatedParameter extends TranslatedElement {
455404
hasIndirection() and
456405
tag = InitializerIndirectStoreTag() and
457406
opcode instanceof Opcode::InitializeIndirection and
458-
resultType = getUnknownType()
407+
resultType = getInitializationResultType()
459408
}
460409

461410
final override IRVariable getInstructionVariable(InstructionTag tag) {
@@ -492,9 +441,43 @@ abstract class TranslatedParameter extends TranslatedElement {
492441

493442
abstract CppType getPRValueType();
494443

444+
abstract CppType getInitializationResultType();
445+
495446
abstract IRAutomaticVariable getIRVariable();
496447
}
497448

449+
/**
450+
* The IR translation of the synthesized parameter used to represent the `...` in a varargs
451+
* function.
452+
*/
453+
class TranslatedThisParameter extends TranslatedParameter, TTranslatedThisParameter {
454+
Function func;
455+
456+
TranslatedThisParameter() { this = TTranslatedThisParameter(func) }
457+
458+
final override string toString() { result = "this" }
459+
460+
final override Locatable getAST() { result = func }
461+
462+
final override Function getFunction() { result = func }
463+
464+
final override predicate hasIndirection() { any() }
465+
466+
final override CppType getGLValueType() { result = getTypeForGLValue(any(UnknownType t)) }
467+
468+
final override CppType getPRValueType() {
469+
result = getTypeForGLValue(getTranslatedFunction(func).getThisType())
470+
}
471+
472+
final override CppType getInitializationResultType() {
473+
result = getTypeForPRValue(getTranslatedFunction(func).getThisType())
474+
}
475+
476+
final override IRThisVariable getIRVariable() {
477+
result = getTranslatedFunction(func).getThisVariable()
478+
}
479+
}
480+
498481
/**
499482
* Represents the IR translation of a function parameter, including the
500483
* initialization of that parameter with the incoming argument.
@@ -525,6 +508,8 @@ class TranslatedPositionalParameter extends TranslatedParameter, TTranslatedPara
525508

526509
final override CppType getPRValueType() { result = getTypeForPRValue(getVariableType(param)) }
527510

511+
final override CppType getInitializationResultType() { result = getUnknownType() }
512+
528513
final override IRAutomaticUserVariable getIRVariable() {
529514
result = getIRUserVariable(getFunction(), param)
530515
}
@@ -551,6 +536,8 @@ class TranslatedEllipsisParameter extends TranslatedParameter, TTranslatedEllips
551536

552537
final override CppType getPRValueType() { result = getEllipsisVariablePRValueType() }
553538

539+
final override CppType getInitializationResultType() { result = getUnknownType() }
540+
554541
final override IREllipsisVariable getIRVariable() {
555542
result = getTranslatedFunction(func).getEllipsisVariable()
556543
}

0 commit comments

Comments
 (0)