Skip to content

Commit 04cd0db

Browse files
committed
[Java] Add CharacterLiteral to CompileTimeConstantExpr.getStringValue
1 parent 1181779 commit 04cd0db

File tree

8 files changed

+37
-1
lines changed

8 files changed

+37
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Add support for `CharacterLiteral` in `CompileTimeConstantExpr.getStringValue()`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class CompileTimeConstantExpr extends Expr {
168168
string getStringValue() {
169169
result = this.(StringLiteral).getValue()
170170
or
171+
result = this.(CharacterLiteral).getValue()
172+
or
171173
result =
172174
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
173175
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()

java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
| constants/Constants.java:20:22:20:22 | 5 |
1616
| constants/Constants.java:20:27:20:27 | 1 |
1717
| constants/Constants.java:20:31:20:31 | 2 |
18+
| constants/Constants.java:21:22:21:24 | 'a' |

java/ql/test/library-tests/constants/PrintAst.expected

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ constants/Constants.java:
6666
# 20| 1: [IntegerLiteral] 5
6767
# 20| 1: [IntegerLiteral] 1
6868
# 20| 2: [IntegerLiteral] 2
69-
# 22| 11: [ReturnStmt] return ...
69+
# 21| 11: [LocalVariableDeclStmt] var ...;
70+
# 21| 0: [TypeAccess] char
71+
# 21| 1: [LocalVariableDeclExpr] charLiteral
72+
# 21| 0: [CharacterLiteral] 'a'
73+
# 23| 12: [ReturnStmt] return ...
7074
constants/Initializers.java:
7175
# 0| [CompilationUnit] Initializers
7276
# 3| 1: [Class] Initializers
@@ -512,3 +516,13 @@ constants/Values.java:
512516
# 90| 0: [TypeAccess] int
513517
# 90| 1: [LocalVariableDeclExpr] var_nonfinald_local
514518
# 90| 0: [VarAccess] var_field
519+
# 91| 68: [LocalVariableDeclStmt] var ...;
520+
# 91| 0: [TypeAccess] String
521+
# 91| 1: [LocalVariableDeclExpr] concatinatedString
522+
# 91| 0: [StringLiteral] "a" + "b"
523+
# 92| 69: [LocalVariableDeclStmt] var ...;
524+
# 92| 0: [TypeAccess] String
525+
# 92| 1: [LocalVariableDeclExpr] concatinatedChar
526+
# 92| 0: [AddExpr] ... + ...
527+
# 92| 0: [StringLiteral] "ab"
528+
# 92| 1: [CharacterLiteral] 'c'

java/ql/test/library-tests/constants/constants/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void constants(final int notConstant) {
1818
int paren = (12);
1919
String string = "a string";
2020
int ternary = (3 < 5) ? 1 : 2;
21+
char charLiteral = 'a';
2122

2223
return;
2324
}

java/ql/test/library-tests/constants/constants/Values.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ void values(final int notConstant) {
8888
int var_local = final_local; //42
8989
int var_param = notConstant; //Not constant
9090
int var_nonfinald_local = var_field; //Not constant
91+
String concatinatedString = "a" + "b"; //ab
92+
String concatinatedChar = "ab" + 'c'; //abc
9193
}
9294
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| constants/Values.java:19:29:19:31 | '*' | * |
2+
| constants/Values.java:91:37:91:45 | "a" + "b" | ab |
3+
| constants/Values.java:92:29:92:38 | ... + ... | abc |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import semmle.code.java.Variable
2+
3+
from Variable v, Expr init, RefType enclosing, string constant
4+
where
5+
v.getInitializer() = init and
6+
init.getEnclosingCallable().getDeclaringType() = enclosing and
7+
enclosing.hasQualifiedName("constants", "Values") and
8+
constant = init.(CompileTimeConstantExpr).getStringValue()
9+
select init, constant

0 commit comments

Comments
 (0)