Skip to content

Commit 6109ef5

Browse files
authored
Merge pull request github#5475 from Marcono1234/marcono1234/minus-literal
Java: Improve documentation regarding minus in front of numeric literals
2 parents 716568e + d42a01c commit 6109ef5

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,20 +638,54 @@ class BooleanLiteral extends Literal, @booleanliteral {
638638
override string getAPrimaryQlClass() { result = "BooleanLiteral" }
639639
}
640640

641-
/** An integer literal. For example, `23`. */
641+
/**
642+
* An integer literal. For example, `23`.
643+
*
644+
* An integer literal can never be negative except when:
645+
* - It is written in binary, octal or hexadecimal notation
646+
* - It is written in decimal notation, has the value `2147483648` and is preceded
647+
* by a minus; in this case the value of the IntegerLiteral is -2147483648 and
648+
* the preceding minus will *not* be modeled as `MinusExpr`.
649+
*
650+
* In all other cases the preceding minus, if any, will be modeled as a separate
651+
* `MinusExpr`.
652+
*
653+
* The last exception is necessary because `2147483648` on its own would not be
654+
* a valid integer literal (and could also not be parsed as CodeQL `int`).
655+
*/
642656
class IntegerLiteral extends Literal, @integerliteral {
643657
/** Gets the int representation of this literal. */
644658
int getIntValue() { result = getValue().toInt() }
645659

646660
override string getAPrimaryQlClass() { result = "IntegerLiteral" }
647661
}
648662

649-
/** A long literal. For example, `23l`. */
663+
/**
664+
* A long literal. For example, `23L`.
665+
*
666+
* A long literal can never be negative except when:
667+
* - It is written in binary, octal or hexadecimal notation
668+
* - It is written in decimal notation, has the value `9223372036854775808` and
669+
* is preceded by a minus; in this case the value of the LongLiteral is
670+
* -9223372036854775808 and the preceding minus will *not* be modeled as
671+
* `MinusExpr`.
672+
*
673+
* In all other cases the preceding minus, if any, will be modeled as a separate
674+
* `MinusExpr`.
675+
*
676+
* The last exception is necessary because `9223372036854775808` on its own
677+
* would not be a valid long literal.
678+
*/
650679
class LongLiteral extends Literal, @longliteral {
651680
override string getAPrimaryQlClass() { result = "LongLiteral" }
652681
}
653682

654-
/** A floating point literal. For example, `4.2f`. */
683+
/**
684+
* A float literal. For example, `4.2f`.
685+
*
686+
* A float literal is never negative; a preceding minus, if any, will always
687+
* be modeled as separate `MinusExpr`.
688+
*/
655689
class FloatingPointLiteral extends Literal, @floatingpointliteral {
656690
/**
657691
* Gets the value of this literal as CodeQL 64-bit `float`. The value will
@@ -662,7 +696,12 @@ class FloatingPointLiteral extends Literal, @floatingpointliteral {
662696
override string getAPrimaryQlClass() { result = "FloatingPointLiteral" }
663697
}
664698

665-
/** A double literal. For example, `4.2`. */
699+
/**
700+
* A double literal. For example, `4.2`.
701+
*
702+
* A double literal is never negative; a preceding minus, if any, will always
703+
* be modeled as separate `MinusExpr`.
704+
*/
666705
class DoubleLiteral extends Literal, @doubleliteral {
667706
/**
668707
* Gets the value of this literal as CodeQL 64-bit `float`. The result will
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class NumericLiterals {
2+
void negativeLiterals() {
3+
float f = -1f;
4+
double d = -1d;
5+
int i1 = -2147483647;
6+
int i2 = -2147483648; // CodeQL models minus as part of literal
7+
int i3 = -0b10000000000000000000000000000000; // binary
8+
int i4 = -020000000000; // octal
9+
int i5 = -0x80000000; // hex
10+
long l1 = -9223372036854775807L;
11+
long l2 = -9223372036854775808L; // CodeQL models minus as part of literal
12+
long l3 = -0b1000000000000000000000000000000000000000000000000000000000000000L; // binary
13+
long l4 = -01000000000000000000000L; // octal
14+
long l5 = -0x8000000000000000L; // hex
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| NumericLiterals.java:3:14:3:15 | 1f | 1.0 | NumericLiterals.java:3:13:3:15 | -... |
2+
| NumericLiterals.java:4:15:4:16 | 1d | 1.0 | NumericLiterals.java:4:14:4:16 | -... |
3+
| NumericLiterals.java:5:13:5:22 | 2147483647 | 2147483647 | NumericLiterals.java:5:12:5:22 | -... |
4+
| NumericLiterals.java:6:12:6:22 | -2147483648 | -2147483648 | NumericLiterals.java:6:7:6:22 | i2 |
5+
| NumericLiterals.java:7:13:7:46 | 0b10000000000000000000000000000000 | -2147483648 | NumericLiterals.java:7:12:7:46 | -... |
6+
| NumericLiterals.java:8:13:8:24 | 020000000000 | -2147483648 | NumericLiterals.java:8:12:8:24 | -... |
7+
| NumericLiterals.java:9:13:9:22 | 0x80000000 | -2147483648 | NumericLiterals.java:9:12:9:22 | -... |
8+
| NumericLiterals.java:10:14:10:33 | 9223372036854775807L | 9223372036854775807 | NumericLiterals.java:10:13:10:33 | -... |
9+
| NumericLiterals.java:11:13:11:33 | -9223372036854775808L | -9223372036854775808 | NumericLiterals.java:11:8:11:33 | l2 |
10+
| NumericLiterals.java:12:14:12:80 | 0b1000000000000000000000000000000000000000000000000000000000000000L | -9223372036854775808 | NumericLiterals.java:12:13:12:80 | -... |
11+
| NumericLiterals.java:13:14:13:37 | 01000000000000000000000L | -9223372036854775808 | NumericLiterals.java:13:13:13:37 | -... |
12+
| NumericLiterals.java:14:14:14:32 | 0x8000000000000000L | -9223372036854775808 | NumericLiterals.java:14:13:14:32 | -... |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java
2+
3+
from Literal l
4+
where
5+
l instanceof IntegerLiteral or
6+
l instanceof LongLiteral or
7+
l instanceof FloatingPointLiteral or
8+
l instanceof DoubleLiteral
9+
select l, l.getValue(), l.getParent()

0 commit comments

Comments
 (0)