Skip to content

Commit 8ef4be4

Browse files
authored
Merge pull request #21412 from aschackmull/java/binary-assignment
Java: Make Assignment extend BinaryExpr.
2 parents 3e7a966 + ea77c0d commit 8ef4be4

File tree

28 files changed

+5032
-110
lines changed

28 files changed

+5032
-110
lines changed

java/downgrades/de4ded61c8ae83f829aedaf05be73307ba25ca40/old.dbscheme

Lines changed: 1241 additions & 0 deletions
Large diffs are not rendered by default.

java/downgrades/de4ded61c8ae83f829aedaf05be73307ba25ca40/semmlecode.dbscheme

Lines changed: 1240 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
description: Remove inclusion of @assignment in @binaryexpr
2+
compatibility: full

java/ql/consistency-queries/BinaryExpr.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ where
1010
e.isNthChildOf(be, i) and i != 0 and i != 1 and reason = "Unexpected operand " + i.toString()
1111
)
1212
or
13-
be.getOp() = " ?? " and reason = "No operator name"
13+
be.getOp() = "??" and reason = "No operator name"
1414
select be, reason
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The class `Assignment` now extends `BinaryExpr`. Uses of `BinaryExpr` may in some cases need slight adjustment.

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ when_branch_else(unique int id: @whenbranch ref);
815815
| @geexpr
816816
| @eqexpr
817817
| @neexpr
818+
| @assignment
818819
| @valueeqexpr
819820
| @valueneexpr;
820821

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class ArrayInit extends Expr, @arrayinit {
392392
* element assignments since there the assignment destination is not directly
393393
* the array variable but instead an `ArrayAccess`.
394394
*/
395-
class Assignment extends Expr, @assignment {
395+
class Assignment extends BinaryExpr, @assignment {
396396
/** Gets the destination (left-hand side) of the assignment. */
397397
Expr getDest() { result.isNthChildOf(this, 0) }
398398

@@ -417,6 +417,8 @@ class Assignment extends Expr, @assignment {
417417
* For example, `x = 23`.
418418
*/
419419
class AssignExpr extends Assignment, @assignexpr {
420+
override string getOp() { result = "=" }
421+
420422
override string getAPrimaryQlClass() { result = "AssignExpr" }
421423
}
422424

@@ -445,7 +447,7 @@ class AssignOp extends Assignment, @assignop {
445447
override Expr getSource() { result.getParent() = this }
446448

447449
/** Gets a string representation of the assignment operator of this compound assignment. */
448-
/*abstract*/ string getOp() { result = "??=" }
450+
/*abstract*/ override string getOp() { result = "??=" }
449451

450452
/** Gets a printable representation of this expression. */
451453
override string toString() { result = "..." + this.getOp() + "..." }
@@ -739,155 +741,155 @@ class BinaryExpr extends Expr, @binaryexpr {
739741
}
740742

741743
/** Gets a printable representation of this expression. */
742-
override string toString() { result = "..." + this.getOp() + "..." }
744+
override string toString() { result = "... " + this.getOp() + " ..." }
743745

744746
/** Gets a string representation of the operator of this binary expression. */
745-
/*abstract*/ string getOp() { result = " ?? " }
747+
/*abstract*/ string getOp() { result = "??" }
746748
}
747749

748750
/** A binary expression using the `*` operator. */
749751
class MulExpr extends BinaryExpr, @mulexpr {
750-
override string getOp() { result = " * " }
752+
override string getOp() { result = "*" }
751753

752754
override string getAPrimaryQlClass() { result = "MulExpr" }
753755
}
754756

755757
/** A binary expression using the `/` operator. */
756758
class DivExpr extends BinaryExpr, @divexpr {
757-
override string getOp() { result = " / " }
759+
override string getOp() { result = "/" }
758760

759761
override string getAPrimaryQlClass() { result = "DivExpr" }
760762
}
761763

762764
/** A binary expression using the `%` operator. */
763765
class RemExpr extends BinaryExpr, @remexpr {
764-
override string getOp() { result = " % " }
766+
override string getOp() { result = "%" }
765767

766768
override string getAPrimaryQlClass() { result = "RemExpr" }
767769
}
768770

769771
/** A binary expression using the `+` operator. */
770772
class AddExpr extends BinaryExpr, @addexpr {
771-
override string getOp() { result = " + " }
773+
override string getOp() { result = "+" }
772774

773775
override string getAPrimaryQlClass() { result = "AddExpr" }
774776
}
775777

776778
/** A binary expression using the `-` operator. */
777779
class SubExpr extends BinaryExpr, @subexpr {
778-
override string getOp() { result = " - " }
780+
override string getOp() { result = "-" }
779781

780782
override string getAPrimaryQlClass() { result = "SubExpr" }
781783
}
782784

783785
/** A binary expression using the `<<` operator. */
784786
class LeftShiftExpr extends BinaryExpr, @lshiftexpr {
785-
override string getOp() { result = " << " }
787+
override string getOp() { result = "<<" }
786788

787789
override string getAPrimaryQlClass() { result = "LeftShiftExpr" }
788790
}
789791

790792
/** A binary expression using the `>>` operator. */
791793
class RightShiftExpr extends BinaryExpr, @rshiftexpr {
792-
override string getOp() { result = " >> " }
794+
override string getOp() { result = ">>" }
793795

794796
override string getAPrimaryQlClass() { result = "RightShiftExpr" }
795797
}
796798

797799
/** A binary expression using the `>>>` operator. */
798800
class UnsignedRightShiftExpr extends BinaryExpr, @urshiftexpr {
799-
override string getOp() { result = " >>> " }
801+
override string getOp() { result = ">>>" }
800802

801803
override string getAPrimaryQlClass() { result = "UnsignedRightShiftExpr" }
802804
}
803805

804806
/** A binary expression using the `&` operator. */
805807
class AndBitwiseExpr extends BinaryExpr, @andbitexpr {
806-
override string getOp() { result = " & " }
808+
override string getOp() { result = "&" }
807809

808810
override string getAPrimaryQlClass() { result = "AndBitwiseExpr" }
809811
}
810812

811813
/** A binary expression using the `|` operator. */
812814
class OrBitwiseExpr extends BinaryExpr, @orbitexpr {
813-
override string getOp() { result = " | " }
815+
override string getOp() { result = "|" }
814816

815817
override string getAPrimaryQlClass() { result = "OrBitwiseExpr" }
816818
}
817819

818820
/** A binary expression using the `^` operator. */
819821
class XorBitwiseExpr extends BinaryExpr, @xorbitexpr {
820-
override string getOp() { result = " ^ " }
822+
override string getOp() { result = "^" }
821823

822824
override string getAPrimaryQlClass() { result = "XorBitwiseExpr" }
823825
}
824826

825827
/** A binary expression using the `&&` operator. */
826828
class AndLogicalExpr extends BinaryExpr, @andlogicalexpr {
827-
override string getOp() { result = " && " }
829+
override string getOp() { result = "&&" }
828830

829831
override string getAPrimaryQlClass() { result = "AndLogicalExpr" }
830832
}
831833

832834
/** A binary expression using the `||` operator. */
833835
class OrLogicalExpr extends BinaryExpr, @orlogicalexpr {
834-
override string getOp() { result = " || " }
836+
override string getOp() { result = "||" }
835837

836838
override string getAPrimaryQlClass() { result = "OrLogicalExpr" }
837839
}
838840

839841
/** A binary expression using the `<` operator. */
840842
class LTExpr extends BinaryExpr, @ltexpr {
841-
override string getOp() { result = " < " }
843+
override string getOp() { result = "<" }
842844

843845
override string getAPrimaryQlClass() { result = "LTExpr" }
844846
}
845847

846848
/** A binary expression using the `>` operator. */
847849
class GTExpr extends BinaryExpr, @gtexpr {
848-
override string getOp() { result = " > " }
850+
override string getOp() { result = ">" }
849851

850852
override string getAPrimaryQlClass() { result = "GTExpr" }
851853
}
852854

853855
/** A binary expression using the `<=` operator. */
854856
class LEExpr extends BinaryExpr, @leexpr {
855-
override string getOp() { result = " <= " }
857+
override string getOp() { result = "<=" }
856858

857859
override string getAPrimaryQlClass() { result = "LEExpr" }
858860
}
859861

860862
/** A binary expression using the `>=` operator. */
861863
class GEExpr extends BinaryExpr, @geexpr {
862-
override string getOp() { result = " >= " }
864+
override string getOp() { result = ">=" }
863865

864866
override string getAPrimaryQlClass() { result = "GEExpr" }
865867
}
866868

867869
/** A binary expression using Java's `==` or Kotlin's `===` operator. */
868870
class EQExpr extends BinaryExpr, @eqexpr {
869-
override string getOp() { result = " == " }
871+
override string getOp() { result = "==" }
870872

871873
override string getAPrimaryQlClass() { result = "EQExpr" }
872874
}
873875

874876
/** A binary expression using the Kotlin `==` operator, semantically equivalent to `Objects.equals`. */
875877
class ValueEQExpr extends BinaryExpr, @valueeqexpr {
876-
override string getOp() { result = " (value equals) " }
878+
override string getOp() { result = "(value equals)" }
877879

878880
override string getAPrimaryQlClass() { result = "ValueEQExpr" }
879881
}
880882

881883
/** A binary expression using Java's `!=` or Kotlin's `!==` operator. */
882884
class NEExpr extends BinaryExpr, @neexpr {
883-
override string getOp() { result = " != " }
885+
override string getOp() { result = "!=" }
884886

885887
override string getAPrimaryQlClass() { result = "NEExpr" }
886888
}
887889

888890
/** A binary expression using the Kotlin `!=` operator, semantically equivalent to `Objects.equals`. */
889891
class ValueNEExpr extends BinaryExpr, @valueneexpr {
890-
override string getOp() { result = " (value not-equals) " }
892+
override string getOp() { result = "(value not-equals)" }
891893

892894
override string getAPrimaryQlClass() { result = "ValueNEExpr" }
893895
}

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,29 +207,12 @@ private class PpArrayInit extends PpAst, ArrayInit {
207207
override PpAst getChild(int i) { exists(int j | result = this.getInit(j) and i = 1 + 2 * j) }
208208
}
209209

210-
private class PpAssignment extends PpAst, Assignment {
211-
override string getPart(int i) {
212-
i = 1 and
213-
this instanceof AssignExpr and
214-
result = " = "
215-
or
216-
i = 1 and
217-
result = " " + this.(AssignOp).getOp() + " "
218-
}
219-
220-
override PpAst getChild(int i) {
221-
i = 0 and result = this.getDest()
222-
or
223-
i = 2 and result = this.getRhs()
224-
}
225-
}
226-
227210
private class PpLiteral extends PpAst, Literal {
228211
override string getPart(int i) { i = 0 and result = this.getLiteral() }
229212
}
230213

231214
private class PpBinaryExpr extends PpAst, BinaryExpr {
232-
override string getPart(int i) { i = 1 and result = this.getOp() }
215+
override string getPart(int i) { i = 1 and result = " " + this.getOp() + " " }
233216

234217
override PpAst getChild(int i) {
235218
i = 0 and result = this.getLeftOperand()

java/ql/lib/semmle/code/java/arithmetic/Overflow.qll

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ class ArithExpr extends Expr {
9393
) and
9494
forall(Expr e |
9595
e = this.(BinaryExpr).getAnOperand() or
96-
e = this.(UnaryAssignExpr).getOperand() or
97-
e = this.(AssignOp).getSource()
96+
e = this.(UnaryAssignExpr).getOperand()
9897
|
9998
e.getType() instanceof NumType
10099
)
@@ -114,21 +113,17 @@ class ArithExpr extends Expr {
114113
*/
115114
Expr getLeftOperand() {
116115
result = this.(BinaryExpr).getLeftOperand() or
117-
result = this.(UnaryAssignExpr).getOperand() or
118-
result = this.(AssignOp).getDest()
116+
result = this.(UnaryAssignExpr).getOperand()
119117
}
120118

121119
/**
122120
* Gets the right-hand operand if this is a binary expression.
123121
*/
124-
Expr getRightOperand() {
125-
result = this.(BinaryExpr).getRightOperand() or result = this.(AssignOp).getRhs()
126-
}
122+
Expr getRightOperand() { result = this.(BinaryExpr).getRightOperand() }
127123

128124
/** Gets an operand of this arithmetic expression. */
129125
Expr getAnOperand() {
130126
result = this.(BinaryExpr).getAnOperand() or
131-
result = this.(UnaryAssignExpr).getOperand() or
132-
result = this.(AssignOp).getSource()
127+
result = this.(UnaryAssignExpr).getOperand()
133128
}
134129
}

java/ql/lib/semmle/code/java/controlflow/Guards.qll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,15 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
179179
}
180180
}
181181

182-
abstract private class BinExpr extends Expr {
183-
Expr getAnOperand() {
184-
result = this.(BinaryExpr).getAnOperand() or result = this.(AssignOp).getSource()
185-
}
186-
}
187-
188-
class AndExpr extends BinExpr {
182+
class AndExpr extends BinaryExpr {
189183
AndExpr() {
190184
this instanceof AndBitwiseExpr or
191185
this instanceof AndLogicalExpr or
192186
this instanceof AssignAndExpr
193187
}
194188
}
195189

196-
class OrExpr extends BinExpr {
190+
class OrExpr extends BinaryExpr {
197191
OrExpr() {
198192
this instanceof OrBitwiseExpr or
199193
this instanceof OrLogicalExpr or

0 commit comments

Comments
 (0)