Add a few useful shortcuts.

This commit is contained in:
Max Schaefer
2020-09-25 16:58:28 +01:00
parent 8667b64a1c
commit 56f295f741
3 changed files with 17 additions and 11 deletions

View File

@@ -124,9 +124,7 @@ class FuncDef extends @funcdef, StmtParent, ExprParent {
*/
Parameter getParameter(int i) { result.isParameterOf(this, i) }
/**
* Gets a parameter of this function.
*/
/** Gets a parameter of this function. */
Parameter getAParameter() { result.getFunction() = this }
/**

View File

@@ -412,13 +412,19 @@ class Function extends ValueEntity, @functionobject {
Type getResultType(int i) { result = getType().(SignatureType).getResultType(i) }
/** Gets the body of this function, if any. */
BlockStmt getBody() { none() }
BlockStmt getBody() { result = getFuncDecl().getBody() }
/** Gets the `i`th parameter of this function. */
Parameter getParameter(int i) { none() }
Parameter getParameter(int i) { result.isParameterOf(getFuncDecl(), i) }
/** Gets a parameter of this function. */
Parameter getAParameter() { result = getParameter(_) }
/** Gets the `i`th reslt variable of this function. */
ResultVariable getResult(int i) { result.isResultOf(getFuncDecl(), i) }
/** Gets a result variable of this function. */
ResultVariable getAResult() { result = getResult(_) }
}
/**
@@ -528,10 +534,6 @@ class Method extends Function {
class DeclaredFunction extends Function, DeclaredEntity, @declfunctionobject {
override FuncDecl getFuncDecl() { result.getNameExpr() = this.getDeclaration() }
override BlockStmt getBody() { result = getFuncDecl().getBody() }
override Parameter getParameter(int i) { result = getFuncDecl().getParameter(i) }
override predicate mayHaveSideEffects() {
not exists(getBody())
or

View File

@@ -245,6 +245,13 @@ abstract class FunctionNode extends Node {
* Gets the data-flow node corresponding to the `i`th result of this function.
*/
ResultNode getResult(int i) { result = getAResult() and result.getIndex() = i }
/**
* Gets the function entity this node corresponds to.
*
* Note that this predicate has no result for function literals.
*/
Function getFunction() { none() }
}
/** A representation of a function that is declared in the module scope. */
@@ -257,8 +264,7 @@ class GlobalFunctionNode extends FunctionNode, MkGlobalFunctionNode {
override string getName() { result = func.getName() }
/** Gets the function this node corresponds to. */
Function getFunction() { result = func }
override Function getFunction() { result = func }
override ReceiverNode getReceiver() { result = receiverNode(func.(Method).getReceiver()) }