Skip to content

Commit f6487d7

Browse files
committed
Python: Rename StrConst to StringLiteral
Does a few things: - Renames `StrConst` to `StringLiteral`, and deprecates the former. - Also deprecates `Str`. - Adds an override of `StringLiteral::toString` making it output `"StringLiteral"` rather than the inherited `"Str"`. This ensures that the AST viewer shows these nodes as the former type, not the latter. There are a large number of uses of `StrConst` in the codebase. These will be fixed in a later commit.
1 parent bea7b94 commit f6487d7

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

python/ql/lib/semmle/python/Exprs.qll

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class Call extends Call_ {
236236
string getANamedArgumentName() {
237237
result = this.getAKeyword().getArg()
238238
or
239-
result = this.getKwargs().(Dict).getAKey().(StrConst).getText()
239+
result = this.getKwargs().(Dict).getAKey().(StringLiteral).getText()
240240
}
241241

242242
/** Gets the positional argument count of this call, provided there is no more than one tuple (*) argument. */
@@ -299,7 +299,7 @@ class Repr extends Repr_ {
299299
* A bytes constant, such as `b'ascii'`. Note that unadorned string constants such as
300300
* `"hello"` are treated as Bytes for Python2, but Unicode for Python3.
301301
*/
302-
class Bytes extends StrConst {
302+
class Bytes extends StringLiteral {
303303
/* syntax: b"hello" */
304304
Bytes() { not this.isUnicode() }
305305

@@ -446,7 +446,7 @@ class NegativeIntegerLiteral extends ImmutableLiteral, UnaryExpr {
446446
* A unicode string expression, such as `u"\u20ac"`. Note that unadorned string constants such as
447447
* "hello" are treated as Bytes for Python2, but Unicode for Python3.
448448
*/
449-
class Unicode extends StrConst {
449+
class Unicode extends StringLiteral {
450450
/* syntax: "hello" */
451451
Unicode() { this.isUnicode() }
452452

@@ -599,7 +599,7 @@ class Slice extends Slice_ {
599599
/**
600600
* Returns all string prefixes in the database that are explicitly marked as Unicode strings.
601601
*
602-
* Helper predicate for `StrConst::isUnicode`.
602+
* Helper predicate for `StringLiteral::isUnicode`.
603603
*/
604604
pragma[nomagic]
605605
private string unicode_prefix() {
@@ -610,19 +610,22 @@ private string unicode_prefix() {
610610
/**
611611
* Returns all string prefixes in the database that are _not_ explicitly marked as bytestrings.
612612
*
613-
* Helper predicate for `StrConst::isUnicode`.
613+
* Helper predicate for `StringLiteral::isUnicode`.
614614
*/
615615
pragma[nomagic]
616616
private string non_byte_prefix() {
617617
result = any(Str_ s).getPrefix() and
618618
not result.charAt(_) in ["b", "B"]
619619
}
620620

621-
/** A string constant. This is a placeholder class -- use `StrConst` instead. */
622-
class Str = StrConst;
621+
/** DEPRECATED. Use `StringLiteral` instead. */
622+
deprecated class Str = StringLiteral;
623+
624+
/** DEPRECATED. Use `StringLiteral` instead. */
625+
deprecated class StrConst = StringLiteral;
623626

624627
/** A string constant. */
625-
class StrConst extends Str_, ImmutableLiteral {
628+
class StringLiteral extends Str_, ImmutableLiteral {
626629
/* syntax: "hello" */
627630
predicate isUnicode() {
628631
this.getPrefix() = unicode_prefix()
@@ -652,6 +655,8 @@ class StrConst extends Str_, ImmutableLiteral {
652655
}
653656

654657
override Object getLiteralObject() { none() }
658+
659+
override string toString() { result = "StringLiteral" }
655660
}
656661

657662
private predicate name_consts(Name_ n, string id) {

0 commit comments

Comments
 (0)