Skip to content

[Java] Add CharacterLiteral to CompileTimeConstantExpr.getStringValue #8325

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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Add support for `CharacterLiteral` in `CompileTimeConstantExpr.getStringValue()`
2 changes: 2 additions & 0 deletions java/ql/lib/semmle/code/java/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class CompileTimeConstantExpr extends Expr {
string getStringValue() {
result = this.(StringLiteral).getValue()
or
result = this.(CharacterLiteral).getValue()
or
result =
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
| constants/Constants.java:20:22:20:22 | 5 |
| constants/Constants.java:20:27:20:27 | 1 |
| constants/Constants.java:20:31:20:31 | 2 |
| constants/Constants.java:21:22:21:24 | 'a' |
16 changes: 15 additions & 1 deletion java/ql/test/library-tests/constants/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ constants/Constants.java:
# 20| 1: [IntegerLiteral] 5
# 20| 1: [IntegerLiteral] 1
# 20| 2: [IntegerLiteral] 2
# 22| 11: [ReturnStmt] return ...
# 21| 11: [LocalVariableDeclStmt] var ...;
# 21| 0: [TypeAccess] char
# 21| 1: [LocalVariableDeclExpr] charLiteral
# 21| 0: [CharacterLiteral] 'a'
# 23| 12: [ReturnStmt] return ...
constants/Initializers.java:
# 0| [CompilationUnit] Initializers
# 3| 1: [Class] Initializers
Expand Down Expand Up @@ -512,3 +516,13 @@ constants/Values.java:
# 90| 0: [TypeAccess] int
# 90| 1: [LocalVariableDeclExpr] var_nonfinald_local
# 90| 0: [VarAccess] var_field
# 91| 68: [LocalVariableDeclStmt] var ...;
# 91| 0: [TypeAccess] String
# 91| 1: [LocalVariableDeclExpr] concatenatedString
# 91| 0: [StringLiteral] "a" + "b"
# 92| 69: [LocalVariableDeclStmt] var ...;
# 92| 0: [TypeAccess] String
# 92| 1: [LocalVariableDeclExpr] concatenatedChar
# 92| 0: [AddExpr] ... + ...
# 92| 0: [StringLiteral] "ab"
# 92| 1: [CharacterLiteral] 'c'
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void constants(final int notConstant) {
int paren = (12);
String string = "a string";
int ternary = (3 < 5) ? 1 : 2;
char charLiteral = 'a';

return;
}
Comment on lines 18 to 24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file tab delimited when the rest are space delimited? 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to fix this as a part of this PR 😆 someone might want to make a sweep though, just my 10c

Expand Down
2 changes: 2 additions & 0 deletions java/ql/test/library-tests/constants/constants/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ void values(final int notConstant) {
int var_local = final_local; //42
int var_param = notConstant; //Not constant
int var_nonfinald_local = var_field; //Not constant
String concatenatedString = "a" + "b"; //ab
String concatenatedChar = "ab" + 'c'; //abc
}
}
4 changes: 2 additions & 2 deletions java/ql/test/library-tests/constants/getBooleanValue.ql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import semmle.code.java.Variable

from Variable v, Expr init, RefType enclosing, boolean constant
from Variable v, CompileTimeConstantExpr init, RefType enclosing, boolean constant
where
v.getInitializer() = init and
init.getEnclosingCallable().getDeclaringType() = enclosing and
enclosing.hasQualifiedName("constants", "Values") and
constant = init.(CompileTimeConstantExpr).getBooleanValue()
constant = init.getBooleanValue()
select init, constant
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 semmle.code.java.Variable

from Variable v, Expr init, RefType enclosing, int constant
from Variable v, CompileTimeConstantExpr init, RefType enclosing, int constant
where
v.getInitializer() = init and
init.getEnclosingCallable().getDeclaringType() = enclosing and
enclosing.hasQualifiedName("constants", "Values") and
constant = init.(CompileTimeConstantExpr).getIntValue()
constant = init.getIntValue()
select init, constant
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| constants/Values.java:19:29:19:31 | '*' | * |
| constants/Values.java:91:37:91:45 | "a" + "b" | ab |
| constants/Values.java:92:35:92:44 | ... + ... | abc |
9 changes: 9 additions & 0 deletions java/ql/test/library-tests/constants/getStringValue.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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") and
constant = init.getStringValue()
select init, constant