Skip to content

Commit f6d3920

Browse files
dave-bartolomeoRobert Marsh
authored andcommitted
C++: Replace getAnOperand().(XXXOperand) with getXXXOperand()
1 parent 4c23ad1 commit f6d3920

File tree

7 files changed

+388
-106
lines changed

7 files changed

+388
-106
lines changed

cpp/ql/src/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ private predicate compares_eq(Instruction test, Operand left, Operand right, int
389389

390390
/** Rearrange various simple comparisons into `left == right + k` form. */
391391
private predicate simple_comparison_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual) {
392-
left = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareEQInstruction and right = cmp.getAnOperand().(RightOperand) and k = 0 and areEqual = true
392+
left = cmp.getLeftOperand() and cmp instanceof CompareEQInstruction and right = cmp.getRightOperand() and k = 0 and areEqual = true
393393
or
394-
left = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareNEInstruction and right = cmp.getAnOperand().(RightOperand) and k = 0 and areEqual = false
394+
left = cmp.getLeftOperand() and cmp instanceof CompareNEInstruction and right = cmp.getRightOperand() and k = 0 and areEqual = false
395395
}
396396

397397
private predicate complex_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
@@ -432,13 +432,13 @@ private predicate compares_ge(Instruction test, Operand left, Operand right, int
432432

433433
/** Rearrange various simple comparisons into `left < right + k` form. */
434434
private predicate simple_comparison_lt(CompareInstruction cmp, Operand left, Operand right, int k) {
435-
left = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareLTInstruction and right = cmp.getAnOperand().(RightOperand) and k = 0
435+
left = cmp.getLeftOperand() and cmp instanceof CompareLTInstruction and right = cmp.getRightOperand() and k = 0
436436
or
437-
left = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareLEInstruction and right = cmp.getAnOperand().(RightOperand) and k = 1
437+
left = cmp.getLeftOperand() and cmp instanceof CompareLEInstruction and right = cmp.getRightOperand() and k = 1
438438
or
439-
right = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareGTInstruction and left = cmp.getAnOperand().(RightOperand) and k = 0
439+
right = cmp.getLeftOperand() and cmp instanceof CompareGTInstruction and left = cmp.getRightOperand() and k = 0
440440
or
441-
right = cmp.getAnOperand().(LeftOperand) and cmp instanceof CompareGEInstruction and left = cmp.getAnOperand().(RightOperand) and k = 1
441+
right = cmp.getLeftOperand() and cmp instanceof CompareGEInstruction and left = cmp.getRightOperand() and k = 1
442442
}
443443

444444
private predicate complex_lt(CompareInstruction cmp, Operand left, Operand right, int k, boolean isLt, boolean testIsTrue) {
@@ -452,12 +452,12 @@ private predicate complex_lt(CompareInstruction cmp, Operand left, Operand right
452452
left < (right - x) + c => left < right + (c-x) */
453453
private predicate sub_lt(CompareInstruction cmp, Operand left, Operand right, int k, boolean isLt, boolean testIsTrue) {
454454
exists(SubInstruction lhs, int c, int x | compares_lt(cmp, lhs.getAUse(), right, c, isLt, testIsTrue) and
455-
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
455+
left = lhs.getLeftOperand() and x = int_value(lhs.getRight())
456456
and k = c + x
457457
)
458458
or
459459
exists(SubInstruction rhs, int c, int x | compares_lt(cmp, left, rhs.getAUse(), c, isLt, testIsTrue) and
460-
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
460+
right = rhs.getLeftOperand() and x = int_value(rhs.getRight())
461461
and k = c - x
462462
)
463463
}
@@ -466,17 +466,17 @@ private predicate sub_lt(CompareInstruction cmp, Operand left, Operand right, in
466466
left < (right + x) + c => left < right + (c+x) */
467467
private predicate add_lt(CompareInstruction cmp, Operand left, Operand right, int k, boolean isLt, boolean testIsTrue) {
468468
exists(AddInstruction lhs, int c, int x | compares_lt(cmp, lhs.getAUse(), right, c, isLt, testIsTrue) and
469-
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
469+
(left = lhs.getLeftOperand() and x = int_value(lhs.getRight())
470470
or
471-
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeft())
471+
left = lhs.getRightOperand() and x = int_value(lhs.getLeft())
472472
)
473473
and k = c - x
474474
)
475475
or
476476
exists(AddInstruction rhs, int c, int x | compares_lt(cmp, left, rhs.getAUse(), c, isLt, testIsTrue) and
477-
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
477+
(right = rhs.getLeftOperand() and x = int_value(rhs.getRight())
478478
or
479-
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeft())
479+
right = rhs.getRightOperand() and x = int_value(rhs.getLeft())
480480
)
481481
and k = c + x
482482
)
@@ -487,12 +487,12 @@ private predicate add_lt(CompareInstruction cmp, Operand left, Operand right, in
487487
left == (right - x) + c => left == right + (c-x) */
488488
private predicate sub_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
489489
exists(SubInstruction lhs, int c, int x | compares_eq(cmp, lhs.getAUse(), right, c, areEqual, testIsTrue) and
490-
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
490+
left = lhs.getLeftOperand() and x = int_value(lhs.getRight())
491491
and k = c + x
492492
)
493493
or
494494
exists(SubInstruction rhs, int c, int x | compares_eq(cmp, left, rhs.getAUse(), c, areEqual, testIsTrue) and
495-
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
495+
right = rhs.getLeftOperand() and x = int_value(rhs.getRight())
496496
and k = c - x
497497
)
498498
}
@@ -502,17 +502,17 @@ private predicate sub_eq(CompareInstruction cmp, Operand left, Operand right, in
502502
left == (right + x) + c => left == right + (c+x) */
503503
private predicate add_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
504504
exists(AddInstruction lhs, int c, int x | compares_eq(cmp, lhs.getAUse(), right, c, areEqual, testIsTrue) and
505-
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
505+
(left = lhs.getLeftOperand() and x = int_value(lhs.getRight())
506506
or
507-
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeft())
507+
left = lhs.getRightOperand() and x = int_value(lhs.getLeft())
508508
)
509509
and k = c - x
510510
)
511511
or
512512
exists(AddInstruction rhs, int c, int x | compares_eq(cmp, left, rhs.getAUse(), c, areEqual, testIsTrue) and
513-
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
513+
(right = rhs.getLeftOperand() and x = int_value(rhs.getRight())
514514
or
515-
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeft())
515+
right = rhs.getRightOperand() and x = int_value(rhs.getLeft())
516516
)
517517
and k = c + x
518518
)

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 118 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,12 @@ class FieldAddressInstruction extends FieldInstruction {
665665
getOpcode() instanceof Opcode::FieldAddress
666666
}
667667

668+
final UnaryOperand getObjectAddressOperand() {
669+
result = getAnOperand()
670+
}
671+
668672
final Instruction getObjectAddress() {
669-
result = getAnOperand().(UnaryOperand).getDefinitionInstruction()
673+
result = getObjectAddressOperand().getDefinitionInstruction()
670674
}
671675
}
672676

@@ -710,8 +714,12 @@ class ReturnValueInstruction extends ReturnInstruction {
710714
getOpcode() instanceof Opcode::ReturnValue
711715
}
712716

717+
final ReturnValueOperand getReturnValueOperand() {
718+
result = getAnOperand()
719+
}
720+
713721
final Instruction getReturnValue() {
714-
result = getAnOperand().(ReturnValueOperand).getDefinitionInstruction()
722+
result = getReturnValueOperand().getDefinitionInstruction()
715723
}
716724
}
717725

@@ -720,8 +728,12 @@ class CopyInstruction extends Instruction {
720728
getOpcode() instanceof CopyOpcode
721729
}
722730

731+
final CopySourceOperand getSourceValueOperand() {
732+
result = getAnOperand()
733+
}
734+
723735
final Instruction getSourceValue() {
724-
result = getAnOperand().(CopySourceOperand).getDefinitionInstruction()
736+
result = getSourceValueOperand().getDefinitionInstruction()
725737
}
726738
}
727739

@@ -736,8 +748,12 @@ class LoadInstruction extends CopyInstruction {
736748
getOpcode() instanceof Opcode::Load
737749
}
738750

751+
final AddressOperand getSourceAddressOperand() {
752+
result = getAnOperand()
753+
}
754+
739755
final Instruction getSourceAddress() {
740-
result = getAnOperand().(AddressOperand).getDefinitionInstruction()
756+
result = getSourceAddressOperand().getDefinitionInstruction()
741757
}
742758
}
743759

@@ -750,8 +766,12 @@ class StoreInstruction extends CopyInstruction {
750766
result instanceof IndirectMemoryAccess
751767
}
752768

769+
final AddressOperand getDestinationAddressOperand() {
770+
result = getAnOperand()
771+
}
772+
753773
final Instruction getDestinationAddress() {
754-
result = getAnOperand().(AddressOperand).getDefinitionInstruction()
774+
result = getDestinationAddressOperand().getDefinitionInstruction()
755775
}
756776
}
757777

@@ -760,8 +780,12 @@ class ConditionalBranchInstruction extends Instruction {
760780
getOpcode() instanceof Opcode::ConditionalBranch
761781
}
762782

783+
final ConditionOperand getConditionOperand() {
784+
result = getAnOperand()
785+
}
786+
763787
final Instruction getCondition() {
764-
result = getAnOperand().(ConditionOperand).getDefinitionInstruction()
788+
result = getConditionOperand().getDefinitionInstruction()
765789
}
766790

767791
final Instruction getTrueSuccessor() {
@@ -818,21 +842,29 @@ class BinaryInstruction extends Instruction {
818842
getOpcode() instanceof BinaryOpcode
819843
}
820844

845+
final LeftOperand getLeftOperand() {
846+
result = getAnOperand()
847+
}
848+
849+
final RightOperand getRightOperand() {
850+
result = getAnOperand()
851+
}
852+
821853
final Instruction getLeft() {
822-
result = getAnOperand().(LeftOperand).getDefinitionInstruction()
854+
result = getLeftOperand().getDefinitionInstruction()
823855
}
824856

825857
final Instruction getRight() {
826-
result = getAnOperand().(RightOperand).getDefinitionInstruction()
858+
result = getRightOperand().getDefinitionInstruction()
827859
}
828860

829861
/**
830862
* Holds if this instruction's operands are `op1` and `op2`, in either order.
831863
*/
832864
final predicate hasOperands(Operand op1, Operand op2) {
833-
op1 = getAnOperand().(LeftOperand) and op2 = getAnOperand().(RightOperand)
865+
op1 = getLeftOperand() and op2 = getRightOperand()
834866
or
835-
op1 = getAnOperand().(RightOperand) and op2 = getAnOperand().(LeftOperand)
867+
op1 = getRightOperand() and op2 = getLeftOperand()
836868
}
837869
}
838870

@@ -948,8 +980,12 @@ class UnaryInstruction extends Instruction {
948980
getOpcode() instanceof UnaryOpcode
949981
}
950982

983+
final UnaryOperand getUnaryOperand() {
984+
result = getAnOperand()
985+
}
986+
951987
final Instruction getUnary() {
952-
result = getAnOperand().(UnaryOperand).getDefinitionInstruction()
988+
result = getUnaryOperand().getDefinitionInstruction()
953989
}
954990
}
955991

@@ -1174,8 +1210,12 @@ class SwitchInstruction extends Instruction {
11741210
getOpcode() instanceof Opcode::Switch
11751211
}
11761212

1213+
final ConditionOperand getExpressionOperand() {
1214+
result = getAnOperand()
1215+
}
1216+
11771217
final Instruction getExpression() {
1178-
result = getAnOperand().(ConditionOperand).getDefinitionInstruction()
1218+
result = getExpressionOperand().getDefinitionInstruction()
11791219
}
11801220

11811221
final Instruction getACaseSuccessor() {
@@ -1197,38 +1237,63 @@ class CallInstruction extends Instruction {
11971237
getOpcode() instanceof Opcode::Call
11981238
}
11991239

1240+
/**
1241+
* Gets the operand the specifies the target function of the call.
1242+
*/
1243+
final CallTargetOperand getCallTargetOperand() {
1244+
result = getAnOperand()
1245+
}
1246+
12001247
/**
12011248
* Gets the `Instruction` that computes the target function of the call. This is usually a
12021249
* `FunctionAddress` instruction, but can also be an arbitrary instruction that produces a
12031250
* function pointer.
12041251
*/
12051252
final Instruction getCallTarget() {
1206-
result = getAnOperand().(CallTargetOperand).getDefinitionInstruction()
1253+
result = getCallTargetOperand().getDefinitionInstruction()
1254+
}
1255+
1256+
/**
1257+
* Gets all of the argument operands of the call, including the `this` pointer, if any.
1258+
*/
1259+
final ArgumentOperand getAnArgumentOperand() {
1260+
result = getAnOperand()
12071261
}
12081262

12091263
/**
12101264
* Gets all of the arguments of the call, including the `this` pointer, if any.
12111265
*/
12121266
final Instruction getAnArgument() {
1213-
result = getAnOperand().(ArgumentOperand).getDefinitionInstruction()
1267+
result = getAnArgumentOperand().getDefinitionInstruction()
1268+
}
1269+
1270+
/**
1271+
* Gets the `this` pointer argument operand of the call, if any.
1272+
*/
1273+
final ThisArgumentOperand getThisArgumentOperand() {
1274+
result = getAnOperand()
12141275
}
12151276

12161277
/**
12171278
* Gets the `this` pointer argument of the call, if any.
12181279
*/
12191280
final Instruction getThisArgument() {
1220-
result = getAnOperand().(ThisArgumentOperand).getDefinitionInstruction()
1281+
result = getThisArgumentOperand().getDefinitionInstruction()
1282+
}
1283+
1284+
/**
1285+
* Gets the argument operand at the specified index.
1286+
*/
1287+
final PositionalArgumentOperand getPositionalArgumentOperand(int index) {
1288+
result = getAnOperand() and
1289+
result.getIndex() = index
12211290
}
12221291

12231292
/**
12241293
* Gets the argument at the specified index.
12251294
*/
12261295
final Instruction getPositionalArgument(int index) {
1227-
exists(PositionalArgumentOperand operand |
1228-
operand = getAnOperand() and
1229-
operand.getIndex() = index and
1230-
result = operand.getDefinitionInstruction()
1231-
)
1296+
result = getPositionalArgumentOperand(index).getDefinitionInstruction()
12321297
}
12331298
}
12341299

@@ -1360,18 +1425,32 @@ class ThrowValueInstruction extends ThrowInstruction {
13601425
getOpcode() instanceof Opcode::ThrowValue
13611426
}
13621427

1428+
/**
1429+
* Gets the address operand of the exception thrown by this instruction.
1430+
*/
1431+
final AddressOperand getExceptionAddressOperand() {
1432+
result = getAnOperand()
1433+
}
1434+
13631435
/**
13641436
* Gets the address of the exception thrown by this instruction.
13651437
*/
13661438
final Instruction getExceptionAddress() {
1367-
result = getAnOperand().(AddressOperand).getDefinitionInstruction()
1439+
result = getExceptionAddressOperand().getDefinitionInstruction()
1440+
}
1441+
1442+
/**
1443+
* Gets the operand for the exception thrown by this instruction.
1444+
*/
1445+
final ExceptionOperand getExceptionOperand() {
1446+
result = getAnOperand()
13681447
}
13691448

13701449
/**
13711450
* Gets the exception thrown by this instruction.
13721451
*/
13731452
final Instruction getException() {
1374-
result = getAnOperand().(ExceptionOperand).getDefinitionInstruction()
1453+
result = getExceptionOperand().getDefinitionInstruction()
13751454
}
13761455
}
13771456

@@ -1549,19 +1628,34 @@ class ChiInstruction extends Instruction {
15491628
result instanceof ChiTotalMemoryAccess
15501629
}
15511630

1631+
/**
1632+
* Gets the operand that represents the previous state of all memory that might be aliased by the
1633+
* memory write.
1634+
*/
1635+
final ChiTotalOperand getTotalOperand() {
1636+
result = getAnOperand()
1637+
}
1638+
15521639
/**
15531640
* Gets the operand that represents the previous state of all memory that might be aliased by the
15541641
* memory write.
15551642
*/
15561643
final Instruction getTotal() {
1557-
result = getAnOperand().(ChiTotalOperand).getDefinitionInstruction()
1644+
result = getTotalOperand().getDefinitionInstruction()
1645+
}
1646+
1647+
/**
1648+
* Gets the operand that represents the new value written by the memory write.
1649+
*/
1650+
final ChiPartialOperand getPartialOperand() {
1651+
result = getAnOperand()
15581652
}
15591653

15601654
/**
15611655
* Gets the operand that represents the new value written by the memory write.
15621656
*/
15631657
final Instruction getPartial() {
1564-
result = getAnOperand().(ChiPartialOperand).getDefinitionInstruction()
1658+
result = getPartialOperand().getDefinitionInstruction()
15651659
}
15661660
}
15671661

0 commit comments

Comments
 (0)