From 68da13cdc2c091f35ec5618aa2dc174cdf0ea1ea Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 28 Aug 2019 14:28:04 +0100 Subject: [PATCH] Python remove a couple of small AST related modules, moving contents to more appropriate modules. --- python/ql/src/python.qll | 2 - python/ql/src/semmle/python/AST.qll | 57 --------------------- python/ql/src/semmle/python/Assigns.qll | 19 ------- python/ql/src/semmle/python/AstExtended.qll | 56 ++++++++++++++++++++ python/ql/src/semmle/python/Stmts.qll | 12 +++++ 5 files changed, 68 insertions(+), 78 deletions(-) delete mode 100644 python/ql/src/semmle/python/AST.qll delete mode 100644 python/ql/src/semmle/python/Assigns.qll diff --git a/python/ql/src/python.qll b/python/ql/src/python.qll index 9f104a3d240..032da8e5165 100644 --- a/python/ql/src/python.qll +++ b/python/ql/src/python.qll @@ -3,7 +3,6 @@ import semmle.python.Operations import semmle.python.Variables import semmle.python.AstGenerated import semmle.python.AstExtended -import semmle.python.AST import semmle.python.Function import semmle.python.Module import semmle.python.Class @@ -28,7 +27,6 @@ import semmle.python.types.Version import semmle.python.types.Descriptors import semmle.python.protocols import semmle.python.SSA -import semmle.python.Assigns import semmle.python.SelfAttribute import semmle.python.types.Properties import semmle.python.xml.XML diff --git a/python/ql/src/semmle/python/AST.qll b/python/ql/src/semmle/python/AST.qll deleted file mode 100644 index 0ac8db03e32..00000000000 --- a/python/ql/src/semmle/python/AST.qll +++ /dev/null @@ -1,57 +0,0 @@ -import python - -/** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */ -abstract class AstNode extends AstNode_ { - - /** Gets the scope that this node occurs in */ - abstract Scope getScope(); - - /** Gets a flow node corresponding directly to this node. - * NOTE: For some statements and other purely syntactic elements, - * there may not be a `ControlFlowNode` */ - ControlFlowNode getAFlowNode() { - py_flow_bb_node(result, this, _, _) - } - - /** Gets the location for this AST node */ - Location getLocation() { - none() - } - - /** Whether this syntactic element is artificial, that is it is generated - * by the compiler and is not present in the source */ - predicate isArtificial() { - none() - } - - /** Gets a child node of this node in the AST. This predicate exists to aid exploration of the AST - * and other experiments. The child-parent relation may not be meaningful. - * For a more meaningful relation in terms of dependency use - * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or - * Scope.getAStmt(). - */ - abstract AstNode getAChildNode(); - - /** Gets the parent node of this node in the AST. This predicate exists to aid exploration of the AST - * and other experiments. The child-parent relation may not be meaningful. - * For a more meaningful relation in terms of dependency use - * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or - * Scope.getAStmt() applied to the parent. - */ - AstNode getParentNode() { - result.getAChildNode() = this - } - - /** Whether this contains `inner` syntactically */ - predicate contains(AstNode inner) { - this.getAChildNode+() = inner - } - - /** Whether this contains `inner` syntactically and `inner` has the same scope as `this` */ - predicate containsInScope(AstNode inner) { - this.contains(inner) and - this.getScope() = inner.getScope() and - not inner instanceof Scope - } - -} diff --git a/python/ql/src/semmle/python/Assigns.qll b/python/ql/src/semmle/python/Assigns.qll deleted file mode 100644 index ad72645ffd5..00000000000 --- a/python/ql/src/semmle/python/Assigns.qll +++ /dev/null @@ -1,19 +0,0 @@ -/** - * In order to handle data flow and other analyses efficiently the extractor transforms various statements which perform binding in assignments. - * These classes provide a wrapper to provide a more 'natural' interface to the syntactic elements transformed to assignments. - */ - -import python - - -/** An assignment statement */ -class AssignStmt extends Assign { - - AssignStmt() { - not this instanceof FunctionDef and not this instanceof ClassDef - } - - override string toString() { - result = "AssignStmt" - } -} diff --git a/python/ql/src/semmle/python/AstExtended.qll b/python/ql/src/semmle/python/AstExtended.qll index ecc0d8b3ae3..c0c44adcf6e 100644 --- a/python/ql/src/semmle/python/AstExtended.qll +++ b/python/ql/src/semmle/python/AstExtended.qll @@ -1,5 +1,61 @@ import python +/** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */ +abstract class AstNode extends AstNode_ { + + /** Gets the scope that this node occurs in */ + abstract Scope getScope(); + + /** Gets a flow node corresponding directly to this node. + * NOTE: For some statements and other purely syntactic elements, + * there may not be a `ControlFlowNode` */ + ControlFlowNode getAFlowNode() { + py_flow_bb_node(result, this, _, _) + } + + /** Gets the location for this AST node */ + Location getLocation() { + none() + } + + /** Whether this syntactic element is artificial, that is it is generated + * by the compiler and is not present in the source */ + predicate isArtificial() { + none() + } + + /** Gets a child node of this node in the AST. This predicate exists to aid exploration of the AST + * and other experiments. The child-parent relation may not be meaningful. + * For a more meaningful relation in terms of dependency use + * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or + * Scope.getAStmt(). + */ + abstract AstNode getAChildNode(); + + /** Gets the parent node of this node in the AST. This predicate exists to aid exploration of the AST + * and other experiments. The child-parent relation may not be meaningful. + * For a more meaningful relation in terms of dependency use + * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or + * Scope.getAStmt() applied to the parent. + */ + AstNode getParentNode() { + result.getAChildNode() = this + } + + /** Whether this contains `inner` syntactically */ + predicate contains(AstNode inner) { + this.getAChildNode+() = inner + } + + /** Whether this contains `inner` syntactically and `inner` has the same scope as `this` */ + predicate containsInScope(AstNode inner) { + this.contains(inner) and + this.getScope() = inner.getScope() and + not inner instanceof Scope + } + +} + /* Parents */ /** Internal implementation class */ diff --git a/python/ql/src/semmle/python/Stmts.qll b/python/ql/src/semmle/python/Stmts.qll index b6e0a2b7945..640f9f8cda0 100644 --- a/python/ql/src/semmle/python/Stmts.qll +++ b/python/ql/src/semmle/python/Stmts.qll @@ -95,6 +95,18 @@ class Assign extends Assign_ { } } +/** An assignment statement */ +class AssignStmt extends Assign { + + AssignStmt() { + not this instanceof FunctionDef and not this instanceof ClassDef + } + + override string toString() { + result = "AssignStmt" + } +} + /** An augmented assignment statement, such as `x += y` */ class AugAssign extends AugAssign_ {