Skip to content

Commit 30805d9

Browse files
authored
add ThisArgumentOperand special case
1 parent 9b818a0 commit 30805d9

File tree

11 files changed

+378
-362
lines changed

11 files changed

+378
-362
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Operand.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Operand extends TStageOperand {
4646
/**
4747
* Gets the location of the source code for this operand.
4848
* By default this is where the operand is used, but some subclasses may override this
49-
* using getAnyDef() if it makes more sense.
49+
* using `getAnyDef()` if it makes more sense.
5050
*/
5151
Language::Location getLocation() { result = this.getUse().getLocation() }
5252

@@ -271,8 +271,8 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand {
271271

272272
final override string toString() { result = tag.toString() }
273273

274-
// most RegisterOperands have a more meaningful location at the definition
275-
// the only exception is ThisArgumentOperand
274+
// most `RegisterOperands` have a more meaningful location at the definition
275+
// the only exception are specific cases of `ThisArgumentOperand`
276276
override Language::Location getLocation() { result = this.getAnyDef().getLocation() }
277277

278278
final override Instruction getAnyDef() { result = defInstr }
@@ -407,16 +407,19 @@ class ArgumentOperand extends RegisterOperand {
407407
}
408408

409409
/**
410-
* An operand representing the implicit 'this' argument to a member function
410+
* An operand representing the implicit `this` argument to a member function
411411
* call.
412412
*/
413413
class ThisArgumentOperand extends ArgumentOperand {
414414
override ThisArgumentOperandTag tag;
415415

416-
// in some cases the def location seems to make more sense, but most of the
417-
// time it does not really make a difference, and on some occations the def
418-
// has no location at all, so that in general it is better use the use location
419-
override Language::Location getLocation() { result = this.getUse().getLocation() }
416+
// in most cases the def location makes more sense, but in some corner cases it
417+
// does not have a location: in those cases we fall back to the use location
418+
override Language::Location getLocation() {
419+
if exists(Language::Location loc | loc = this.getAnyDef().getLocation())
420+
then result = this.getAnyDef().getLocation()
421+
else result = this.getUse().getLocation()
422+
}
420423
}
421424

422425
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/Operand.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Operand extends TStageOperand {
4646
/**
4747
* Gets the location of the source code for this operand.
4848
* By default this is where the operand is used, but some subclasses may override this
49-
* using getAnyDef() if it makes more sense.
49+
* using `getAnyDef()` if it makes more sense.
5050
*/
5151
Language::Location getLocation() { result = this.getUse().getLocation() }
5252

@@ -271,8 +271,8 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand {
271271

272272
final override string toString() { result = tag.toString() }
273273

274-
// most RegisterOperands have a more meaningful location at the definition
275-
// the only exception is ThisArgumentOperand
274+
// most `RegisterOperands` have a more meaningful location at the definition
275+
// the only exception are specific cases of `ThisArgumentOperand`
276276
override Language::Location getLocation() { result = this.getAnyDef().getLocation() }
277277

278278
final override Instruction getAnyDef() { result = defInstr }
@@ -407,16 +407,19 @@ class ArgumentOperand extends RegisterOperand {
407407
}
408408

409409
/**
410-
* An operand representing the implicit 'this' argument to a member function
410+
* An operand representing the implicit `this` argument to a member function
411411
* call.
412412
*/
413413
class ThisArgumentOperand extends ArgumentOperand {
414414
override ThisArgumentOperandTag tag;
415415

416-
// in some cases the def location seems to make more sense, but most of the
417-
// time it does not really make a difference, and on some occations the def
418-
// has no location at all, so that in general it is better use the use location
419-
override Language::Location getLocation() { result = this.getUse().getLocation() }
416+
// in most cases the def location makes more sense, but in some corner cases it
417+
// returns no location: in those cases we fall back to the use location
418+
override Language::Location getLocation() {
419+
if exists(Language::Location loc | loc = this.getAnyDef().getLocation())
420+
then result = this.getAnyDef().getLocation()
421+
else result = this.getUse().getLocation()
422+
}
420423
}
421424

422425
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/Operand.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Operand extends TStageOperand {
4646
/**
4747
* Gets the location of the source code for this operand.
4848
* By default this is where the operand is used, but some subclasses may override this
49-
* using getAnyDef() if it makes more sense.
49+
* using `getAnyDef()` if it makes more sense.
5050
*/
5151
Language::Location getLocation() { result = this.getUse().getLocation() }
5252

@@ -271,8 +271,8 @@ class RegisterOperand extends NonPhiOperand, TRegisterOperand {
271271

272272
final override string toString() { result = tag.toString() }
273273

274-
// most RegisterOperands have a more meaningful location at the definition
275-
// the only exception is ThisArgumentOperand
274+
// most `RegisterOperands` have a more meaningful location at the definition
275+
// the only exception are specific cases of `ThisArgumentOperand`
276276
override Language::Location getLocation() { result = this.getAnyDef().getLocation() }
277277

278278
final override Instruction getAnyDef() { result = defInstr }
@@ -407,16 +407,19 @@ class ArgumentOperand extends RegisterOperand {
407407
}
408408

409409
/**
410-
* An operand representing the implicit 'this' argument to a member function
410+
* An operand representing the implicit `this` argument to a member function
411411
* call.
412412
*/
413413
class ThisArgumentOperand extends ArgumentOperand {
414414
override ThisArgumentOperandTag tag;
415415

416-
// in some cases the def location seems to make more sense, but most of the
417-
// time it does not really make a difference, and on some occations the def
418-
// has no location at all, so that in general it is better use the use location
419-
override Language::Location getLocation() { result = this.getUse().getLocation() }
416+
// in most cases the def location makes more sense, but in some corner cases it
417+
// does not have a location: in those cases we fall back to the use location
418+
override Language::Location getLocation() {
419+
if exists(Language::Location loc | loc = this.getAnyDef().getLocation())
420+
then result = this.getAnyDef().getLocation()
421+
else result = this.getUse().getLocation()
422+
}
420423
}
421424

422425
/**

0 commit comments

Comments
 (0)