Skip to content

Java: Revert #8360, "Add CompileTimeConstantExpr.getStringified method" #8402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

40 changes: 4 additions & 36 deletions java/ql/lib/semmle/code/java/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -161,50 +161,18 @@ 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.
*/
pragma[nomagic]
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 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java
import semmle.code.java.Expr

from CompileTimeConstantExpr constant, RefType tpe
where
Expand Down
151 changes: 0 additions & 151 deletions java/ql/test/library-tests/constants/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
28 changes: 0 additions & 28 deletions java/ql/test/library-tests/constants/constants/Stringified.java

This file was deleted.

2 changes: 0 additions & 2 deletions java/ql/test/library-tests/constants/constants/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,5 @@ void values(final int notConstant) {
int var_nonfinald_local = var_field; //Not constant
String concatenatedString = "a" + "b"; //ab
String concatenatedChar = "ab" + 'c'; //abc

int charWithChar = 'a' + 'b'; //195
}
}
2 changes: 1 addition & 1 deletion java/ql/test/library-tests/constants/getBooleanValue.ql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java
import semmle.code.java.Variable

from Variable v, CompileTimeConstantExpr init, RefType enclosing, boolean constant
where
Expand Down
2 changes: 1 addition & 1 deletion java/ql/test/library-tests/constants/getInitializer.ql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java
import semmle.code.java.Variable

from Variable v, Expr init, RefType enclosing
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
4 changes: 2 additions & 2 deletions java/ql/test/library-tests/constants/getIntValue.ql
Original file line number Diff line number Diff line change
@@ -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
24 changes: 1 addition & 23 deletions java/ql/test/library-tests/constants/getStringValue.expected
Original file line number Diff line number Diff line change
@@ -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 |
4 changes: 2 additions & 2 deletions java/ql/test/library-tests/constants/getStringValue.ql
Original file line number Diff line number Diff line change
@@ -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
Loading