Revert #8360, "Add CompileTimeConstantExpr.getStringified method"

This commit is contained in:
Chris Smowton
2022-03-11 11:12:16 +00:00
parent f006cd0e37
commit 46cd85c70b
14 changed files with 12 additions and 343 deletions

View File

@@ -1,6 +0,0 @@
---
category: minorAnalysis
---
* Add new predicate `CompileTimeConstantExpr.getStringifiedValue` which attempts to compute the
`String.valueOf` string rendering of a constant expression. This predicate is now used to
compute the string value of an `AddExpr` that has the type `String`.

View File

@@ -161,38 +161,6 @@ class CompileTimeConstantExpr extends Expr {
)
}
/**
* Gets the stringified value of this expression, where possible.
*
* The stringified version of a compile-time constant expression is the equivalent to
* the result of calling `String.valueOf(expr)` on the expression.
*
* Note that this does not handle the following cases:
*
* - mathematical computations of type `long`, `float`, or `double`.
*/
pragma[nomagic]
string getStringifiedValue() {
result = this.getStringValue()
or
result = this.(Literal).getValue()
or
result = this.getBooleanValue().toString()
or
result = this.getIntValue().toString()
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |
ce = this and
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getStringifiedValue()
)
or
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getStringifiedValue()
)
}
/**
* Gets the string value of this expression, where possible.
*/
@@ -200,11 +168,11 @@ class CompileTimeConstantExpr extends Expr {
string getStringValue() {
result = this.(StringLiteral).getValue()
or
this.getType() instanceof TypeString and // When the expression type is `String`
result = this.(CharacterLiteral).getValue()
or
result =
// Then the resultant string is the addition of both operands stringified value, regardless of type.
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringifiedValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringifiedValue()
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |

View File

@@ -1,4 +1,4 @@
import java
import semmle.code.java.Expr
from CompileTimeConstantExpr constant, RefType tpe
where

View File

@@ -133,151 +133,6 @@ constants/Initializers.java:
# 37| 0: [AssignExpr] ...=...
# 37| 0: [VarAccess] f
# 37| 1: [IntegerLiteral] 42
constants/Stringified.java:
# 0| [CompilationUnit] Stringified
# 3| 1: [Class] Stringified
# 4| 2: [Method] stringified
# 4| 3: [TypeAccess] void
#-----| 4: (Parameters)
# 4| 0: [Parameter] notConstant
# 4| 0: [TypeAccess] String
# 4| 5: [BlockStmt] { ... }
# 5| 0: [LocalVariableDeclStmt] var ...;
# 5| 0: [TypeAccess] String
# 5| 1: [LocalVariableDeclExpr] withNotConstant
# 5| 0: [AddExpr] ... + ...
# 5| 0: [StringLiteral] "a"
# 5| 1: [VarAccess] notConstant
# 6| 1: [LocalVariableDeclStmt] var ...;
# 6| 0: [TypeAccess] String
# 6| 1: [LocalVariableDeclExpr] string
# 6| 0: [StringLiteral] "a" + "b"
# 7| 2: [LocalVariableDeclStmt] var ...;
# 7| 0: [TypeAccess] String
# 7| 1: [LocalVariableDeclExpr] stringWithChar
# 7| 0: [AddExpr] ... + ...
# 7| 0: [StringLiteral] "ab"
# 7| 1: [CharacterLiteral] 'c'
# 8| 3: [LocalVariableDeclStmt] var ...;
# 8| 0: [TypeAccess] String
# 8| 1: [LocalVariableDeclExpr] stringWithBool
# 8| 0: [AddExpr] ... + ...
# 8| 0: [StringLiteral] "ab"
# 8| 1: [BooleanLiteral] true
# 9| 4: [LocalVariableDeclStmt] var ...;
# 9| 0: [TypeAccess] String
# 9| 1: [LocalVariableDeclExpr] stringWithInt
# 9| 0: [AddExpr] ... + ...
# 9| 0: [StringLiteral] "ab"
# 9| 1: [IntegerLiteral] 42
# 10| 5: [LocalVariableDeclStmt] var ...;
# 10| 0: [TypeAccess] String
# 10| 1: [LocalVariableDeclExpr] stringWithDouble
# 10| 0: [AddExpr] ... + ...
# 10| 0: [StringLiteral] "ab"
# 10| 1: [DoubleLiteral] 43.0
# 11| 6: [LocalVariableDeclStmt] var ...;
# 11| 0: [TypeAccess] String
# 11| 1: [LocalVariableDeclExpr] stringWithFloat
# 11| 0: [AddExpr] ... + ...
# 11| 0: [StringLiteral] "ab"
# 11| 1: [FloatingPointLiteral] 44.0f
# 12| 7: [LocalVariableDeclStmt] var ...;
# 12| 0: [TypeAccess] String
# 12| 1: [LocalVariableDeclExpr] stringWithLong
# 12| 0: [AddExpr] ... + ...
# 12| 0: [StringLiteral] "ab"
# 12| 1: [LongLiteral] 45L
# 13| 8: [LocalVariableDeclStmt] var ...;
# 13| 0: [TypeAccess] String
# 13| 1: [LocalVariableDeclExpr] stringWithShort
# 13| 0: [AddExpr] ... + ...
# 13| 0: [StringLiteral] "ab"
# 13| 1: [CastExpr] (...)...
# 13| 0: [TypeAccess] short
# 13| 1: [IntegerLiteral] 46
# 14| 9: [LocalVariableDeclStmt] var ...;
# 14| 0: [TypeAccess] String
# 14| 1: [LocalVariableDeclExpr] stringWithByte
# 14| 0: [AddExpr] ... + ...
# 14| 0: [StringLiteral] "ab"
# 14| 1: [CastExpr] (...)...
# 14| 0: [TypeAccess] byte
# 14| 1: [IntegerLiteral] 47
# 15| 10: [LocalVariableDeclStmt] var ...;
# 15| 0: [TypeAccess] String
# 15| 1: [LocalVariableDeclExpr] charWithString
# 15| 0: [AddExpr] ... + ...
# 15| 0: [CharacterLiteral] 'a'
# 15| 1: [StringLiteral] "bc"
# 16| 11: [LocalVariableDeclStmt] var ...;
# 16| 0: [TypeAccess] String
# 16| 1: [LocalVariableDeclExpr] boolWithString
# 16| 0: [AddExpr] ... + ...
# 16| 0: [BooleanLiteral] true
# 16| 1: [StringLiteral] "bc"
# 17| 12: [LocalVariableDeclStmt] var ...;
# 17| 0: [TypeAccess] String
# 17| 1: [LocalVariableDeclExpr] intWithString
# 17| 0: [AddExpr] ... + ...
# 17| 0: [IntegerLiteral] 42
# 17| 1: [StringLiteral] "bc"
# 18| 13: [LocalVariableDeclStmt] var ...;
# 18| 0: [TypeAccess] String
# 18| 1: [LocalVariableDeclExpr] doubleWithString
# 18| 0: [AddExpr] ... + ...
# 18| 0: [DoubleLiteral] 43.0
# 18| 1: [StringLiteral] "bc"
# 19| 14: [LocalVariableDeclStmt] var ...;
# 19| 0: [TypeAccess] String
# 19| 1: [LocalVariableDeclExpr] floatWithString
# 19| 0: [AddExpr] ... + ...
# 19| 0: [FloatingPointLiteral] 44.0f
# 19| 1: [StringLiteral] "bc"
# 20| 15: [LocalVariableDeclStmt] var ...;
# 20| 0: [TypeAccess] String
# 20| 1: [LocalVariableDeclExpr] longWithString
# 20| 0: [AddExpr] ... + ...
# 20| 0: [LongLiteral] 45L
# 20| 1: [StringLiteral] "bc"
# 21| 16: [LocalVariableDeclStmt] var ...;
# 21| 0: [TypeAccess] String
# 21| 1: [LocalVariableDeclExpr] shortWithString
# 21| 0: [AddExpr] ... + ...
# 21| 0: [CastExpr] (...)...
# 21| 0: [TypeAccess] short
# 21| 1: [IntegerLiteral] 46
# 21| 1: [StringLiteral] "bc"
# 22| 17: [LocalVariableDeclStmt] var ...;
# 22| 0: [TypeAccess] String
# 22| 1: [LocalVariableDeclExpr] byteWithString
# 22| 0: [AddExpr] ... + ...
# 22| 0: [CastExpr] (...)...
# 22| 0: [TypeAccess] byte
# 22| 1: [IntegerLiteral] 47
# 22| 1: [StringLiteral] "bc"
# 24| 18: [LocalVariableDeclStmt] var ...;
# 24| 0: [TypeAccess] String
# 24| 1: [LocalVariableDeclExpr] stringWithExponent
# 24| 0: [AddExpr] ... + ...
# 24| 0: [StringLiteral] "a"
# 24| 1: [DoubleLiteral] 10e1
# 25| 19: [LocalVariableDeclStmt] var ...;
# 25| 0: [TypeAccess] String
# 25| 1: [LocalVariableDeclExpr] stringWithBooleanOr
# 25| 0: [AddExpr] ... + ...
# 25| 0: [StringLiteral] "a"
# 25| 1: [OrLogicalExpr] ... || ...
# 25| 0: [BooleanLiteral] true
# 25| 1: [BooleanLiteral] false
# 26| 20: [LocalVariableDeclStmt] var ...;
# 26| 0: [TypeAccess] String
# 26| 1: [LocalVariableDeclExpr] stringWithIntDivide
# 26| 0: [AddExpr] ... + ...
# 26| 0: [StringLiteral] "a"
# 26| 1: [DivExpr] ... / ...
# 26| 0: [IntegerLiteral] 168
# 26| 1: [IntegerLiteral] 4
constants/Values.java:
# 0| [CompilationUnit] Values
# 4| 1: [Class] Values
@@ -671,9 +526,3 @@ constants/Values.java:
# 92| 0: [AddExpr] ... + ...
# 92| 0: [StringLiteral] "ab"
# 92| 1: [CharacterLiteral] 'c'
# 94| 70: [LocalVariableDeclStmt] var ...;
# 94| 0: [TypeAccess] int
# 94| 1: [LocalVariableDeclExpr] charWithChar
# 94| 0: [AddExpr] ... + ...
# 94| 0: [CharacterLiteral] 'a'
# 94| 1: [CharacterLiteral] 'b'

View File

@@ -1,28 +0,0 @@
package constants;
public class Stringified {
void stringified(final String notConstant) {
String withNotConstant = "a" + notConstant;
String string = "a" + "b"; //ab
String stringWithChar = "ab" + 'c'; //abc
String stringWithBool = "ab" + true; //abtrue
String stringWithInt = "ab" + 42; //ab42
String stringWithDouble = "ab" + 43.0; //ab43.0
String stringWithFloat = "ab" + 44.0f; //ab44.0
String stringWithLong = "ab" + 45L; //ab45
String stringWithShort = "ab" + (short) 46; //ab46
String stringWithByte = "ab" + (byte) 47; //ab47
String charWithString = 'a' + "bc"; //abc
String boolWithString = true + "bc"; //truebc
String intWithString = 42 + "bc"; //42bc
String doubleWithString = 43.0 + "bc"; //43.0bc
String floatWithString = 44.0f + "bc"; //44.0bc
String longWithString = 45L + "bc"; //45bc
String shortWithString = (short) 46 + "bc"; //46bc
String byteWithString = (byte) 47 + "bc"; //47bc
String stringWithExponent = "a" + 10e1; //a100
String stringWithBooleanOr = "a" + (true || false); //atrue
String stringWithIntDivide = "a" + (168 / 4); //a42
}
}

View File

@@ -90,7 +90,5 @@ class Values {
int var_nonfinald_local = var_field; //Not constant
String concatenatedString = "a" + "b"; //ab
String concatenatedChar = "ab" + 'c'; //abc
int charWithChar = 'a' + 'b'; //195
}
}

View File

@@ -1,4 +1,4 @@
import java
import semmle.code.java.Variable
from Variable v, CompileTimeConstantExpr init, RefType enclosing, boolean constant
where

View File

@@ -1,4 +1,4 @@
import java
import semmle.code.java.Variable
from Variable v, Expr init, RefType enclosing
where

View File

@@ -37,4 +37,3 @@
| constants/Values.java:86:25:86:35 | final_field | 42 |
| constants/Values.java:87:33:87:34 | 42 | 42 |
| constants/Values.java:88:25:88:35 | final_local | 42 |
| constants/Values.java:94:28:94:36 | ... + ... | 195 |

View File

@@ -1,9 +1,9 @@
import java
import semmle.code.java.Variable
from Variable v, CompileTimeConstantExpr init, RefType enclosing, int constant
where
v.getInitializer() = init and
init.getEnclosingCallable().getDeclaringType() = enclosing and
enclosing.hasQualifiedName("constants", ["Values", "Stringified"]) and
enclosing.hasQualifiedName("constants", "Values") and
constant = init.getIntValue()
select init, constant

View File

@@ -1,25 +1,3 @@
| constants/Stringified.java:6:25:6:33 | "a" + "b" | ab |
| constants/Stringified.java:7:33:7:42 | ... + ... | ab99 |
| constants/Stringified.java:7:33:7:42 | ... + ... | abc |
| constants/Stringified.java:8:33:8:43 | ... + ... | abtrue |
| constants/Stringified.java:9:32:9:40 | ... + ... | ab42 |
| constants/Stringified.java:10:35:10:45 | ... + ... | ab43.0 |
| constants/Stringified.java:11:34:11:45 | ... + ... | ab44.0 |
| constants/Stringified.java:12:33:12:42 | ... + ... | ab45 |
| constants/Stringified.java:13:34:13:50 | ... + ... | ab46 |
| constants/Stringified.java:14:33:14:48 | ... + ... | ab47 |
| constants/Stringified.java:15:33:15:42 | ... + ... | 97bc |
| constants/Stringified.java:15:33:15:42 | ... + ... | abc |
| constants/Stringified.java:16:33:16:43 | ... + ... | truebc |
| constants/Stringified.java:17:32:17:40 | ... + ... | 42bc |
| constants/Stringified.java:18:35:18:45 | ... + ... | 43.0bc |
| constants/Stringified.java:19:34:19:45 | ... + ... | 44.0bc |
| constants/Stringified.java:20:33:20:42 | ... + ... | 45bc |
| constants/Stringified.java:21:34:21:50 | ... + ... | 46bc |
| constants/Stringified.java:22:33:22:48 | ... + ... | 47bc |
| constants/Stringified.java:24:37:24:46 | ... + ... | a100.0 |
| constants/Stringified.java:25:38:25:58 | ... + ... | atrue |
| constants/Stringified.java:26:38:26:52 | ... + ... | a42 |
| constants/Values.java:19:29:19:31 | '*' | * |
| constants/Values.java:91:37:91:45 | "a" + "b" | ab |
| constants/Values.java:92:35:92:44 | ... + ... | ab99 |
| constants/Values.java:92:35:92:44 | ... + ... | abc |

View File

@@ -1,9 +1,9 @@
import java
import semmle.code.java.Variable
from Variable v, CompileTimeConstantExpr init, RefType enclosing, string constant
where
v.getInitializer() = init and
init.getEnclosingCallable().getDeclaringType() = enclosing and
enclosing.hasQualifiedName("constants", ["Values", "Stringified"]) and
enclosing.hasQualifiedName("constants", "Values") and
constant = init.getStringValue()
select init, constant

View File

@@ -1,80 +0,0 @@
| constants/Stringified.java:6:25:6:33 | "a" + "b" | ab |
| constants/Stringified.java:7:33:7:42 | ... + ... | ab99 |
| constants/Stringified.java:7:33:7:42 | ... + ... | abc |
| constants/Stringified.java:8:33:8:43 | ... + ... | abtrue |
| constants/Stringified.java:9:32:9:40 | ... + ... | ab42 |
| constants/Stringified.java:10:35:10:45 | ... + ... | ab43.0 |
| constants/Stringified.java:11:34:11:45 | ... + ... | ab44.0 |
| constants/Stringified.java:12:33:12:42 | ... + ... | ab45 |
| constants/Stringified.java:13:34:13:50 | ... + ... | ab46 |
| constants/Stringified.java:14:33:14:48 | ... + ... | ab47 |
| constants/Stringified.java:15:33:15:42 | ... + ... | 97bc |
| constants/Stringified.java:15:33:15:42 | ... + ... | abc |
| constants/Stringified.java:16:33:16:43 | ... + ... | truebc |
| constants/Stringified.java:17:32:17:40 | ... + ... | 42bc |
| constants/Stringified.java:18:35:18:45 | ... + ... | 43.0bc |
| constants/Stringified.java:19:34:19:45 | ... + ... | 44.0bc |
| constants/Stringified.java:20:33:20:42 | ... + ... | 45bc |
| constants/Stringified.java:21:34:21:50 | ... + ... | 46bc |
| constants/Stringified.java:22:33:22:48 | ... + ... | 47bc |
| constants/Stringified.java:24:37:24:46 | ... + ... | a100.0 |
| constants/Stringified.java:25:38:25:58 | ... + ... | atrue |
| constants/Stringified.java:26:38:26:52 | ... + ... | a42 |
| constants/Values.java:6:29:6:30 | 42 | 42 |
| constants/Values.java:9:27:9:28 | 42 | 42 |
| constants/Values.java:10:36:10:46 | -2147483648 | -2147483648 |
| constants/Values.java:11:29:11:31 | 052 | 42 |
| constants/Values.java:12:38:12:41 | -... | -42 |
| constants/Values.java:13:27:13:30 | 0x2A | 42 |
| constants/Values.java:14:36:14:40 | -... | -42 |
| constants/Values.java:15:39:15:43 | 0x2_A | 42 |
| constants/Values.java:16:30:16:37 | 0b101010 | 42 |
| constants/Values.java:17:39:17:47 | -... | -42 |
| constants/Values.java:18:42:18:51 | 0b1_0101_0 | 42 |
| constants/Values.java:19:29:19:31 | '*' | 42 |
| constants/Values.java:19:29:19:31 | '*' | * |
| constants/Values.java:20:29:20:31 | 42L | 42 |
| constants/Values.java:21:35:21:38 | true | true |
| constants/Values.java:25:20:25:27 | (...)... | 42 |
| constants/Values.java:26:25:26:33 | (...)... | 42 |
| constants/Values.java:27:32:27:43 | (...)... | -42 |
| constants/Values.java:28:32:28:40 | (...)... | 42 |
| constants/Values.java:29:32:29:41 | (...)... | 42 |
| constants/Values.java:30:32:30:41 | (...)... | -42 |
| constants/Values.java:31:32:31:44 | (...)... | 42 |
| constants/Values.java:32:32:32:44 | (...)... | -32768 |
| constants/Values.java:33:36:33:44 | (...)... | 42 |
| constants/Values.java:36:26:36:28 | +... | 42 |
| constants/Values.java:39:27:39:29 | -... | -42 |
| constants/Values.java:42:23:42:27 | !... | false |
| constants/Values.java:43:27:43:28 | ~... | -1 |
| constants/Values.java:45:19:45:23 | ... * ... | 63 |
| constants/Values.java:48:19:48:25 | ... / ... | 42 |
| constants/Values.java:52:19:52:26 | ... % ... | 42 |
| constants/Values.java:56:20:56:26 | ... + ... | 42 |
| constants/Values.java:59:21:59:29 | ... - ... | 42 |
| constants/Values.java:62:22:62:28 | ... << ... | 84 |
| constants/Values.java:65:22:65:28 | ... >> ... | -1 |
| constants/Values.java:66:23:66:30 | ... >>> ... | 2147483647 |
| constants/Values.java:67:27:67:33 | ... & ... | 42 |
| constants/Values.java:68:26:68:32 | ... \| ... | 42 |
| constants/Values.java:69:27:69:33 | ... ^ ... | 42 |
| constants/Values.java:70:23:70:35 | ... && ... | false |
| constants/Values.java:71:22:71:34 | ... \|\| ... | true |
| constants/Values.java:72:22:72:28 | ... < ... | false |
| constants/Values.java:73:22:73:29 | ... <= ... | true |
| constants/Values.java:74:22:74:28 | ... > ... | false |
| constants/Values.java:75:23:75:30 | ... >= ... | true |
| constants/Values.java:76:22:76:29 | ... == ... | true |
| constants/Values.java:77:22:77:29 | ... != ... | false |
| constants/Values.java:78:23:78:41 | ...?...:... | false |
| constants/Values.java:80:23:80:36 | ... == ... | true |
| constants/Values.java:81:24:81:37 | ... != ... | false |
| constants/Values.java:83:20:83:21 | 42 | 42 |
| constants/Values.java:86:25:86:35 | final_field | 42 |
| constants/Values.java:87:33:87:34 | 42 | 42 |
| constants/Values.java:88:25:88:35 | final_local | 42 |
| constants/Values.java:91:37:91:45 | "a" + "b" | ab |
| constants/Values.java:92:35:92:44 | ... + ... | ab99 |
| constants/Values.java:92:35:92:44 | ... + ... | abc |
| constants/Values.java:94:28:94:36 | ... + ... | 195 |

View File

@@ -1,9 +0,0 @@
import java
from Variable v, CompileTimeConstantExpr init, RefType enclosing, string constant
where
v.getInitializer() = init and
init.getEnclosingCallable().getDeclaringType() = enclosing and
enclosing.hasQualifiedName("constants", ["Values", "Stringified"]) and
constant = init.getStringifiedValue()
select init, constant