Skip to content

Commit 36962b8

Browse files
authored
Merge pull request github#5468 from Marcono1234/marcono1234/floating-point-literal-value
Java: Add value predicates for float and double literals; improve tests
2 parents 07ca09e + fa98443 commit 36962b8

16 files changed

+248
-42
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,23 @@ class LongLiteral extends Literal, @longliteral {
653653

654654
/** A floating point literal. For example, `4.2f`. */
655655
class FloatingPointLiteral extends Literal, @floatingpointliteral {
656+
/**
657+
* Gets the value of this literal as CodeQL 64-bit `float`. The value will
658+
* be parsed as Java 32-bit `float` and then converted to a CodeQL `float`.
659+
*/
660+
float getFloatValue() { result = getValue().toFloat() }
661+
656662
override string getAPrimaryQlClass() { result = "FloatingPointLiteral" }
657663
}
658664

659665
/** A double literal. For example, `4.2`. */
660666
class DoubleLiteral extends Literal, @doubleliteral {
667+
/**
668+
* Gets the value of this literal as CodeQL 64-bit `float`. The result will
669+
* have the same effective value as the Java `double` literal.
670+
*/
671+
float getDoubleValue() { result = getValue().toFloat() }
672+
661673
override string getAPrimaryQlClass() { result = "DoubleLiteral" }
662674
}
663675

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
| literals/Literals.java:11:22:11:25 | true |
1+
| literals/Literals.java:11:22:11:25 | true | true | true |
2+
| literals/Literals.java:16:3:16:6 | true | true | true |
3+
| literals/Literals.java:17:3:17:7 | false | false | false |

java/ql/test/library-tests/literals/literalBoolean.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import semmle.code.java.Expr
22

33
from BooleanLiteral lit
44
where lit.getCompilationUnit().fromSource()
5-
select lit
5+
select lit, lit.getValue(), lit.getBooleanValue()
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
| literals/Literals.java:12:22:12:24 | 'x' |
1+
| literals/Literals.java:12:22:12:24 | 'x' | x |
2+
| literals/Literals.java:21:3:21:5 | 'a' | a |
3+
| literals/Literals.java:22:3:22:10 | '\\u0061' | a |
4+
| literals/Literals.java:23:3:23:10 | '\\u0000' | \u0000 |
5+
| literals/Literals.java:24:3:24:6 | '\\0' | \u0000 |
6+
| literals/Literals.java:25:3:25:6 | '\\n' | \n |
7+
| literals/Literals.java:26:3:26:6 | '\\0' | \u0000 |
8+
| literals/Literals.java:27:3:27:6 | '\\\\' | \\ |
9+
| literals/Literals.java:28:3:28:6 | '\\'' | ' |
10+
| literals/Literals.java:29:3:29:8 | '\\123' | S |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import semmle.code.java.Expr
22

33
from CharacterLiteral lit
4-
select lit
4+
select lit, lit.getValue()
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
| literals/Literals.java:10:22:10:27 | 456.0D |
1+
| literals/Literals.java:10:22:10:27 | 456.0D | 456.0 | 456.0 |
2+
| literals/Literals.java:33:3:33:5 | 0.0 | 0.0 | 0.0 |
3+
| literals/Literals.java:34:3:34:4 | 0d | 0.0 | 0.0 |
4+
| literals/Literals.java:35:3:35:5 | .0d | 0.0 | 0.0 |
5+
| literals/Literals.java:36:3:36:4 | .0 | 0.0 | 0.0 |
6+
| literals/Literals.java:37:4:37:6 | 0.d | 0.0 | 0.0 |
7+
| literals/Literals.java:38:4:38:6 | 0.d | 0.0 | 0.0 |
8+
| literals/Literals.java:39:3:39:22 | 1.234567890123456789 | 1.2345678901234567 | 1.2345678901234567 |
9+
| literals/Literals.java:40:3:40:24 | 1.55555555555555555555 | 1.5555555555555556 | 1.5555555555555556 |
10+
| literals/Literals.java:42:3:42:5 | 1e1 | 10.0 | 10.0 |
11+
| literals/Literals.java:43:3:43:24 | 1.7976931348623157E308 | 1.7976931348623157E308 | 1.7976931348623157E308 |
12+
| literals/Literals.java:44:4:44:25 | 1.7976931348623157E308 | 1.7976931348623157E308 | 1.7976931348623157E308 |
13+
| literals/Literals.java:45:3:45:28 | 0x1.f_ffff_ffff_ffffP+1023 | 1.7976931348623157E308 | 1.7976931348623157E308 |
14+
| literals/Literals.java:46:3:46:10 | 4.9e-324 | 4.9E-324 | 4.9E-324 |
15+
| literals/Literals.java:47:3:47:28 | 0x0.0_0000_0000_0001P-1022 | 4.9E-324 | 4.9E-324 |
16+
| literals/Literals.java:48:3:48:13 | 0x1.0P-1074 | 4.9E-324 | 4.9E-324 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import semmle.code.java.Expr
22

33
from DoubleLiteral lit
4-
select lit
4+
select lit, lit.getValue(), lit.getDoubleValue()
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
| literals/Literals.java:9:22:9:27 | 123.0F |
1+
| literals/Literals.java:9:22:9:27 | 123.0F | 123.0 | 123.0 |
2+
| literals/Literals.java:52:3:52:6 | 0.0f | 0.0 | 0.0 |
3+
| literals/Literals.java:53:3:53:4 | 0f | 0.0 | 0.0 |
4+
| literals/Literals.java:54:3:54:5 | .0f | 0.0 | 0.0 |
5+
| literals/Literals.java:55:4:55:6 | 0.f | 0.0 | 0.0 |
6+
| literals/Literals.java:56:4:56:6 | 0.f | 0.0 | 0.0 |
7+
| literals/Literals.java:57:3:57:10 | 1_0_0.0f | 100.0 | 100.0 |
8+
| literals/Literals.java:58:3:58:23 | 1.234567890123456789f | 1.2345679 | 1.2345679 |
9+
| literals/Literals.java:59:3:59:25 | 1.55555555555555555555f | 1.5555556 | 1.5555556 |
10+
| literals/Literals.java:61:3:61:6 | 1e1f | 10.0 | 10.0 |
11+
| literals/Literals.java:62:3:62:15 | 3.4028235e38f | 3.4028235E38 | 3.4028235E38 |
12+
| literals/Literals.java:63:4:63:16 | 3.4028235e38f | 3.4028235E38 | 3.4028235E38 |
13+
| literals/Literals.java:64:3:64:18 | 0x1.fffffeP+127f | 3.4028235E38 | 3.4028235E38 |
14+
| literals/Literals.java:65:3:65:10 | 1.4e-45f | 1.4E-45 | 1.4E-45 |
15+
| literals/Literals.java:66:3:66:18 | 0x0.000002P-126f | 1.4E-45 | 1.4E-45 |
16+
| literals/Literals.java:67:3:67:13 | 0x1.0P-149f | 1.4E-45 | 1.4E-45 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import semmle.code.java.Expr
22

33
from FloatingPointLiteral lit
4-
select lit
4+
select lit, lit.getValue(), lit.getFloatValue()
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
| literals/Literals.java:7:22:7:24 | 123 |
2-
| literals/Literals.java:14:16:14:26 | -2147483648 |
3-
| literals/Literals.java:16:21:16:30 | 2147483647 |
4-
| literals/Literals.java:18:20:18:29 | 0x80000000 |
5-
| literals/Literals.java:20:10:20:11 | 23 |
6-
| literals/Literals.java:20:15:20:16 | 19 |
7-
| literals/Literals.java:21:10:21:11 | 23 |
8-
| literals/Literals.java:21:15:21:16 | 19 |
1+
| literals/Literals.java:7:22:7:24 | 123 | 123 | 123 |
2+
| literals/Literals.java:71:3:71:3 | 0 | 0 | 0 |
3+
| literals/Literals.java:72:3:72:5 | 0_0 | 0 | 0 |
4+
| literals/Literals.java:73:3:73:7 | 0___0 | 0 | 0 |
5+
| literals/Literals.java:74:3:74:6 | 0_12 | 10 | 10 |
6+
| literals/Literals.java:75:3:75:7 | 0X012 | 18 | 18 |
7+
| literals/Literals.java:76:3:76:10 | 0xaBcDeF | 11259375 | 11259375 |
8+
| literals/Literals.java:77:3:77:6 | 0B11 | 3 | 3 |
9+
| literals/Literals.java:78:3:78:12 | 0x80000000 | -2147483648 | -2147483648 |
10+
| literals/Literals.java:79:3:79:12 | 2147483647 | 2147483647 | 2147483647 |
11+
| literals/Literals.java:80:3:80:13 | -2147483648 | -2147483648 | -2147483648 |
12+
| literals/Literals.java:82:3:82:13 | 0x7fff_ffff | 2147483647 | 2147483647 |
13+
| literals/Literals.java:83:3:83:16 | 0177_7777_7777 | 2147483647 | 2147483647 |
14+
| literals/Literals.java:84:3:84:43 | 0b0111_1111_1111_1111_1111_1111_1111_1111 | 2147483647 | 2147483647 |
15+
| literals/Literals.java:85:3:85:13 | 0x8000_0000 | -2147483648 | -2147483648 |
16+
| literals/Literals.java:86:3:86:16 | 0200_0000_0000 | -2147483648 | -2147483648 |
17+
| literals/Literals.java:87:3:87:43 | 0b1000_0000_0000_0000_0000_0000_0000_0000 | -2147483648 | -2147483648 |
18+
| literals/Literals.java:88:3:88:13 | 0xffff_ffff | -1 | -1 |
19+
| literals/Literals.java:89:3:89:16 | 0377_7777_7777 | -1 | -1 |
20+
| literals/Literals.java:90:3:90:43 | 0b1111_1111_1111_1111_1111_1111_1111_1111 | -1 | -1 |

0 commit comments

Comments
 (0)