Skip to content

Commit f6aec28

Browse files
authored
Merge pull request #216 from microsoft/ps-string-literals
PS: Lift string literals to the public AST
2 parents 64dd13d + dbfd070 commit f6aec28

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

powershell/ql/lib/semmle/code/powershell/ast/internal/Internal.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module Public {
6666
import ParenExpr
6767
import Pipeline
6868
import StringConstantExpression
69+
import StringLiteral
6970
import MemberExpr
7071
import InvokeMemberExpression
7172
import ObjectCreation
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
private import AstImport
2+
3+
// TODO: A string literal should ideally be the string constants that are
4+
// surrounded by quotes (single or double), but we don't yet extract that
5+
// information. So for now we just use the StringConstExpr class, which is
6+
// a bit more general than we want.
7+
class StringLiteral instanceof StringConstExpr {
8+
/** Get the string representation of this string literal. */
9+
string toString() { result = this.getValue() }
10+
11+
/** Get the value of this string literal. */
12+
string getValue() { result = super.getValueString() }
13+
14+
/** Get the location of this string literal. */
15+
SourceLocation getLocation() { result = super.getLocation() }
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$x = "abc"
2+
Foo
3+
$y = 'def'
4+
$z = @"ghi
5+
"@
6+
$t = @'j"k"l
7+
'@
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
stringLiteral
2+
| String.ps1:1:6:1:10 | abc |
3+
| String.ps1:2:1:2:3 | Foo |
4+
| String.ps1:3:6:3:10 | def |
5+
| String.ps1:4:6:4:10 | |
6+
| String.ps1:6:10:8:0 | '@\nkl |
7+
stringConstantExpression
8+
| String.ps1:1:6:1:10 | abc |
9+
| String.ps1:2:1:2:3 | Foo |
10+
| String.ps1:3:6:3:10 | def |
11+
| String.ps1:4:6:4:10 | |
12+
| String.ps1:6:10:8:0 | '@\nkl |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import powershell
2+
3+
query predicate stringLiteral(StringLiteral sl) { any() }
4+
5+
query predicate stringConstantExpression(StringConstExpr sce) { any() }

powershell/ql/test/library-tests/ast/parent.expected

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,25 @@
429429
| Statements/UseProcessBlockForPipelineCommand.ps1:6:9:6:13 | int | Statements/UseProcessBlockForPipelineCommand.ps1:5:9:7:15 | number |
430430
| Statements/UseProcessBlockForPipelineCommand.ps1:10:5:10:11 | Number | Statements/UseProcessBlockForPipelineCommand.ps1:10:5:10:11 | [Stmt] Number |
431431
| Statements/UseProcessBlockForPipelineCommand.ps1:10:5:10:11 | [Stmt] Number | Statements/UseProcessBlockForPipelineCommand.ps1:4:5:10:11 | {...} |
432+
| Strings/String.ps1:1:1:1:2 | x | Strings/String.ps1:1:1:1:10 | ...=... |
433+
| Strings/String.ps1:1:1:1:2 | x | Strings/String.ps1:1:1:8:0 | {...} |
434+
| Strings/String.ps1:1:1:1:10 | ...=... | Strings/String.ps1:1:1:8:0 | {...} |
435+
| Strings/String.ps1:1:1:8:0 | {...} | Strings/String.ps1:1:1:8:0 | toplevel function for String.ps1 |
436+
| Strings/String.ps1:1:1:8:0 | {...} | Strings/String.ps1:1:1:8:0 | {...} |
437+
| Strings/String.ps1:1:6:1:10 | abc | Strings/String.ps1:1:1:1:10 | ...=... |
438+
| Strings/String.ps1:2:1:2:3 | Call to foo | Strings/String.ps1:2:1:2:3 | [Stmt] Call to foo |
439+
| Strings/String.ps1:2:1:2:3 | Foo | Strings/String.ps1:2:1:2:3 | Call to foo |
440+
| Strings/String.ps1:2:1:2:3 | [Stmt] Call to foo | Strings/String.ps1:1:1:8:0 | {...} |
441+
| Strings/String.ps1:3:1:3:2 | y | Strings/String.ps1:1:1:8:0 | {...} |
442+
| Strings/String.ps1:3:1:3:2 | y | Strings/String.ps1:3:1:3:10 | ...=... |
443+
| Strings/String.ps1:3:1:3:10 | ...=... | Strings/String.ps1:1:1:8:0 | {...} |
444+
| Strings/String.ps1:3:6:3:10 | def | Strings/String.ps1:3:1:3:10 | ...=... |
445+
| Strings/String.ps1:4:1:4:2 | z | Strings/String.ps1:1:1:8:0 | {...} |
446+
| Strings/String.ps1:4:1:4:2 | z | Strings/String.ps1:4:1:4:10 | ...=... |
447+
| Strings/String.ps1:4:1:4:10 | ...=... | Strings/String.ps1:1:1:8:0 | {...} |
448+
| Strings/String.ps1:4:6:4:10 | | Strings/String.ps1:4:1:4:10 | ...=... |
449+
| Strings/String.ps1:5:1:6:9 | $t = @'j\n@ | Strings/String.ps1:5:1:6:9 | [Stmt] $t = @'j\n@ |
450+
| Strings/String.ps1:5:1:6:9 | [Stmt] $t = @'j\n@ | Strings/String.ps1:1:1:8:0 | {...} |
451+
| Strings/String.ps1:6:10:8:0 | '@\nkl | Strings/String.ps1:6:10:8:0 | Call to kl\r\n'@\r\n |
452+
| Strings/String.ps1:6:10:8:0 | Call to kl\r\n'@\r\n | Strings/String.ps1:6:10:8:0 | [Stmt] Call to kl\r\n'@\r\n |
453+
| Strings/String.ps1:6:10:8:0 | [Stmt] Call to kl\r\n'@\r\n | Strings/String.ps1:1:1:8:0 | {...} |

0 commit comments

Comments
 (0)