Skip to content
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

PS: Copy existing AST classes from internal repo #83

Merged
merged 6 commits into from
Aug 26, 2024
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
70 changes: 70 additions & 0 deletions powershell/ql/lib/powershell.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import semmle.code.powershell.File
import semmle.code.powershell.Location
import semmle.code.powershell.SourceLocation
import semmle.code.powershell.Ast
import semmle.code.powershell.Statement
import semmle.code.powershell.Expression
import semmle.code.powershell.CommandBase
import semmle.code.powershell.AttributeBase
import semmle.code.powershell.PipelineBase
import semmle.code.powershell.BaseConstantExpression
import semmle.code.powershell.ConstantExpression
import semmle.code.powershell.MemberExpressionBase
import semmle.code.powershell.Attribute
import semmle.code.powershell.NamedAttributeArgument
import semmle.code.powershell.TypeConstraint
import semmle.code.powershell.VariableExpression
import semmle.code.powershell.Parameter
import semmle.code.powershell.ModuleSpecification
import semmle.code.powershell.ParamBlock
import semmle.code.powershell.NamedBlock
import semmle.code.powershell.ScriptBlock
import semmle.code.powershell.StringLiteral
import semmle.code.powershell.AssignmentStatement
import semmle.code.powershell.BinaryExpression
import semmle.code.powershell.ScriptBlockExpr
import semmle.code.powershell.TernaryExpression
import semmle.code.powershell.UsingExpression
import semmle.code.powershell.TrapStatement
import semmle.code.powershell.StatementBlock
import semmle.code.powershell.ArrayExpression
import semmle.code.powershell.ArrayLiteral
import semmle.code.powershell.CommandElement
import semmle.code.powershell.Redirection
import semmle.code.powershell.FileRedirection
import semmle.code.powershell.MergingRedirection
import semmle.code.powershell.LoopStmt
import semmle.code.powershell.DoWhileStmt
import semmle.code.powershell.DoUntilStmt
import semmle.code.powershell.WhileStmt
import semmle.code.powershell.ForStmt
import semmle.code.powershell.ForEachStmt
import semmle.code.powershell.GotoStmt
import semmle.code.powershell.ContinueStmt
import semmle.code.powershell.BreakStmt
import semmle.code.powershell.ReturnStmt
import semmle.code.powershell.UsingStmt
import semmle.code.powershell.Type
import semmle.code.powershell.Member
import semmle.code.powershell.PropertyMember
import semmle.code.powershell.FunctionMember
import semmle.code.powershell.TryStmt
import semmle.code.powershell.IfStmt
import semmle.code.powershell.ExitStmt
import semmle.code.powershell.LabeledStmt
import semmle.code.powershell.DynamicStmt
import semmle.code.powershell.DataStmt
import semmle.code.powershell.Configuration
import semmle.code.powershell.CatchClause
import semmle.code.powershell.Command
import semmle.code.powershell.CommandExpression
import semmle.code.powershell.CommandParameter
import semmle.code.powershell.ExpandableStringExpression
import semmle.code.powershell.TypeExpression
import semmle.code.powershell.ParenExpression
import semmle.code.powershell.Chainable
import semmle.code.powershell.Pipeline
import semmle.code.powershell.StringConstantExpression
import semmle.code.powershell.FunctionDefinition
import semmle.code.powershell.InvokeMemberExpression
import semmle.code.powershell.CommentEntity
9 changes: 9 additions & 0 deletions powershell/ql/lib/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: microsoft-sdl/powershell-all
version: 0.0.1
groups:
- powershell
- microsoft-all
dbscheme: semmlecode.powershell.dbscheme
extractor: powershell
library: true
warnOnImplicitThis: true
9 changes: 9 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/ArrayExpression.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import powershell

class ArrayExpr extends @array_expression, Expr {
override SourceLocation getLocation() { array_expression_location(this, result) }

StmtBlock getStatementBlock() { array_expression(this, result) }

override string toString() { result = "ArrayExpression at: " + this.getLocation().toString() }
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/ArrayLiteral.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class ArrayLiteral extends @array_literal, Expr {
override SourceLocation getLocation() { array_literal_location(this, result) }

Expr getElement(int index) { array_literal_element(this, index, result) }

Expr getAnElement() { array_literal_element(this, _, result) }

override string toString() { result = "ArrayLiteral at: " + this.getLocation().toString() }
}
13 changes: 13 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/AssignmentStatement.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import powershell

class AssignStmt extends @assignment_statement, Stmt {
override SourceLocation getLocation() { assignment_statement_location(this, result) }

int getKind() { assignment_statement(this, result, _, _) }

Expr getLeftHandSide() { assignment_statement(this, _, result, _) }

Stmt getRightHandSide() { assignment_statement(this, _, _, result) }

override string toString() { result = "AssignmentStatement at: " + this.getLocation().toString() }
}
9 changes: 9 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Ast.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import powershell

class Ast extends @ast {
string toString() { none() }

Ast getParent() { parent(result, this) }

Location getLocation() { none() }
}
21 changes: 21 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Attribute.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import powershell

class Attribute extends @attribute, AttributeBase {
override string toString() { result = this.getName() }

override SourceLocation getLocation() { attribute_location(this, result) }

string getName() { attribute(this, result, _, _) }

int getNumNamedArguments() { attribute(this, _, result, _) }

int getNumPositionalArguments() { attribute(this, _, _, result) }

NamedAttributeArgument getNamedArgument(int i) { attribute_named_argument(this, i, result) }

NamedAttributeArgument getANamedArgument() { result = this.getNamedArgument(_) }

Expr getPositionalArgument(int i) { attribute_positional_argument(this, i, result) }

Expr getAPositionalArgument() { result = this.getPositionalArgument(_) }
}
3 changes: 3 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/AttributeBase.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class AttributeBase extends @attribute_base, Ast { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class BaseConstExpr extends @base_constant_expression, Expr { }
15 changes: 15 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/BinaryExpression.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import powershell

class BinaryExpr extends @binary_expression, Expr {
override string toString() {
result = "...+..." // TODO
}

override SourceLocation getLocation() { binary_expression_location(this, result) }

private int getKind() { binary_expression(this, result, _, _) }

Expr getLeft() { binary_expression(this, _, result, _) }

Expr getRight() { binary_expression(this, _, _, result) }
}
7 changes: 7 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/BreakStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import powershell

class BreakStmt extends GotoStmt, Stmt {
override SourceLocation getLocation() { break_statement_location(this, result) }

override string toString() { result = "continue" }
}
17 changes: 17 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CatchClause.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import powershell

class CatchClause extends @catch_clause, Ast {
override SourceLocation getLocation() { catch_clause_location(this, result) }

override string toString() { result = "catch {...}" }

StmtBlock getBody() { catch_clause(this, result, _) } // TODO: Change @ast to @stmt_block in dbscheme

TypeConstraint getCatchType(int i) { catch_clause_catch_type(this, i, result) } // TODO: Change @ast to @type_constraint in dbscheme

TypeConstraint getACatchType() { result = this.getCatchType(_) }

predicate isCatchAll() { catch_clause(this, _, true) } // TODO: Should be equivalent to not exists(this.getACatchType())

TryStmt getTryStmt() { result.getACatchClause() = this }
}
3 changes: 3 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Chainable.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class Chainable extends @chainable, PipelineBase { }
23 changes: 23 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Command.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import powershell

class Cmd extends @command, CmdBase {
override string toString() { result = this.getName() }

override SourceLocation getLocation() { command_location(this, result) }

string getName() { command(this, result, _, _, _) }

int getKind() { command(this, _, result, _, _) }

int getNumElements() { command(this, _, _, result, _) }

int getNumRedirection() { command(this, _, _, _, result) }

CmdElement getElement(int i) { command_command_element(this, i, result) }

Redirection getRedirection(int i) { command_redirection(this, i, result) }

CmdElement getAnElement() { result = this.getElement(_) }

Redirection getARedirection() { result = this.getRedirection(_) }
}
3 changes: 3 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CommandBase.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class CmdBase extends @command_base, Stmt { }
3 changes: 3 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CommandElement.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class CmdElement extends @command_element, Ast { }
15 changes: 15 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CommandExpression.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import powershell

class CmdExpr extends @command_expression, CmdBase {
override SourceLocation getLocation() { command_expression_location(this, result) }

Expr getExpression() { command_expression(this, result, _) }

int getNumRedirections() { command_expression(this, _, result) }

Redirection getRedirection(int i) { command_expression_redirection(this, i, result) }

Redirection getARedirection() { result = this.getRedirection(_) }

override string toString() { result = "CommandExpression at: " + this.getLocation().toString() }
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CommandParameter.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class CmdParameter extends @command_parameter, CmdElement {
override SourceLocation getLocation() { command_parameter_location(this, result) }

string getName() { command_parameter(this, result) }

Expr getArgument() { command_parameter_argument(this, result) }

override string toString() { command_parameter(this, result) }
}
9 changes: 9 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/CommentEntity.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import powershell

class Comment extends @comment_entity {
SourceLocation getLocation() { comment_entity_location(this, result) }

StringLiteral getCommentContents() { comment_entity(this, result) }

string toString() { result = "Comment at: " + this.getLocation().toString() }
}
15 changes: 15 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Configuration.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import powershell

class Configuration extends @configuration_definition, Stmt {
override SourceLocation getLocation() { configuration_definition_location(this, result) }

override string toString() { result = "Configuration" }

Expr getName() { configuration_definition(this, _, _, result) } // TODO: Change @ast to @expression in dbscheme

ScriptBlockExpr getBody() { configuration_definition(this, result, _, _) } // TODO: Change @ast to @script_block in dbscheme

predicate isMeta() { configuration_definition(this, _, 1, _) }

predicate isResource() { configuration_definition(this, _, 0, _) }
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/ConstantExpression.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class ConstExpr extends @constant_expression, BaseConstExpr {
override SourceLocation getLocation() { constant_expression_location(this, result) }

string getType() { constant_expression(this, result) }

StringLiteral getValue() { constant_expression_value(this, result) }

override string toString() { result = "ConstantExpression at: " + this.getLocation().toString() }
}
7 changes: 7 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/ContinueStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import powershell

class ContinueStmt extends GotoStmt, Stmt {
override SourceLocation getLocation() { continue_statement_location(this, result) }

override string toString() { result = "continue" }
}
15 changes: 15 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/DataStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import powershell

class DataStmt extends @data_statement, Stmt {
override SourceLocation getLocation() { data_statement_location(this, result) }

override string toString() { result = "data {...}" }

string getVariableName() { data_statement_variable(this, result) }

Expr getCmdAllowed(int i) { data_statement_commands_allowed(this, i, result) }

Expr getACmdAllowed() { result = this.getCmdAllowed(_) }

StmtBlock getBody() { data_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/DoUntilStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class DoUntilStmt extends @do_until_statement, LoopStmt {
override SourceLocation getLocation() { do_until_statement_location(this, result) }

override string toString() { result = "DoUntil" }

PipelineBase getCondition() { do_until_statement_condition(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme

StmtBlock getBody() { do_until_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/DoWhileStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class DoWhileStmt extends @do_while_statement, LoopStmt {
override SourceLocation getLocation() { do_while_statement_location(this, result) }

override string toString() { result = "DoWhile" }

PipelineBase getCondition() { do_while_statement_condition(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme

StmtBlock getBody() { do_while_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
}
11 changes: 11 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/DynamicStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import powershell

class DynamicStmt extends @dynamic_keyword_statement, Stmt {
override SourceLocation getLocation() { dynamic_keyword_statement_location(this, result) }

override string toString() { result = "&..." }

CmdElement getCmd(int i) { dynamic_keyword_statement_command_elements(this, i, result) }

CmdElement getACmd() { result = this.getCmd(_) }
}
12 changes: 12 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/ExitStmt.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import powershell

class ExitStmt extends @exit_statement, Stmt {
override SourceLocation getLocation() { exit_statement_location(this, result) }

override string toString() { if this.hasPipeline() then result = "exit ..." else result = "exit" }

/** ..., if any. */
PipelineBase getPipeline() { exit_statement_pipeline(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme

predicate hasPipeline() { exists(this.getPipeline()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import powershell

class ExpandableStringExpression extends @expandable_string_expression, Expr {
override SourceLocation getLocation() { expandable_string_expression_location(this, result) }

override string toString() {
result = "ExpandableStringExpression at: " + this.getLocation().toString()
}

private int getKind() { expandable_string_expression(this, _, result, _) }

int getNumExprs() { expandable_string_expression(this, _, _, result) }

Expr getExpr(int i) { expandable_string_expression_nested_expression(this, i, result) }

Expr getAnExpr() { result = this.getExpr(_) }
}
3 changes: 3 additions & 0 deletions powershell/ql/lib/semmle/code/powershell/Expression.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import powershell

class Expr extends @expression, CmdElement { }
Loading