[Java] Add CharacterLiteral to CompileTimeConstantExpr.getStringValue

This commit is contained in:
Jonathan Leitschuh
2022-03-03 17:49:43 -05:00
parent 1181779c10
commit 04cd0dbfe9
8 changed files with 37 additions and 1 deletions

View File

@@ -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' |

View File

@@ -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
@@ -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] concatinatedString
# 91| 0: [StringLiteral] "a" + "b"
# 92| 69: [LocalVariableDeclStmt] var ...;
# 92| 0: [TypeAccess] String
# 92| 1: [LocalVariableDeclExpr] concatinatedChar
# 92| 0: [AddExpr] ... + ...
# 92| 0: [StringLiteral] "ab"
# 92| 1: [CharacterLiteral] 'c'

View File

@@ -18,6 +18,7 @@ class Constants {
int paren = (12);
String string = "a string";
int ternary = (3 < 5) ? 1 : 2;
char charLiteral = 'a';
return;
}

View File

@@ -88,5 +88,7 @@ class Values {
int var_local = final_local; //42
int var_param = notConstant; //Not constant
int var_nonfinald_local = var_field; //Not constant
String concatinatedString = "a" + "b"; //ab
String concatinatedChar = "ab" + 'c'; //abc
}
}

View File

@@ -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:29:92:38 | ... + ... | abc |

View File

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