Skip to content

Commit 3dde501

Browse files
authored
Merge pull request github#356 from max-schaefer/api-cleanup
Add and move around a few convenience predicates
2 parents 2ba9bbf + 97fb967 commit 3dde501

File tree

8 files changed

+96
-23
lines changed

8 files changed

+96
-23
lines changed

ql/src/semmle/go/Decls.qll

+2-11
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,7 @@ class FuncDef extends @funcdef, StmtParent, ExprParent {
112112
DeferStmt getADeferStmt() { result.getEnclosingFunction() = this }
113113

114114
/** Gets the `i`th result variable of this function. */
115-
ResultVariable getResultVar(int i) {
116-
result =
117-
rank[i + 1](ResultVariable res, int j, int k |
118-
res.getDeclaration() = getTypeExpr().getResultDecl(j).getNameExpr(k)
119-
|
120-
res order by j, k
121-
)
122-
}
115+
ResultVariable getResultVar(int i) { result.isResultOf(this, i) }
123116

124117
/** Gets a result variable of this function. */
125118
ResultVariable getAResultVar() { result.getFunction() = this }
@@ -131,9 +124,7 @@ class FuncDef extends @funcdef, StmtParent, ExprParent {
131124
*/
132125
Parameter getParameter(int i) { result.isParameterOf(this, i) }
133126

134-
/**
135-
* Gets a parameter of this function.
136-
*/
127+
/** Gets a parameter of this function. */
137128
Parameter getAParameter() { result.getFunction() = this }
138129

139130
/**

ql/src/semmle/go/Scopes.qll

+27-9
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,28 @@ class ReceiverVariable extends Parameter {
274274

275275
/** A (named) function result variable. */
276276
class ResultVariable extends DeclaredVariable {
277-
FuncDef fn;
277+
FuncDef f;
278+
int index;
278279

279-
ResultVariable() { fn.getTypeExpr().getAResultDecl().getNameExpr(_) = this.getDeclaration() }
280+
ResultVariable() {
281+
exists(FuncTypeExpr tp | tp = f.getTypeExpr() |
282+
this =
283+
rank[index + 1](DeclaredVariable parm, int j, int k |
284+
parm.getDeclaration() = tp.getResultDecl(j).getNameExpr(k)
285+
|
286+
parm order by j, k
287+
)
288+
)
289+
}
280290

281291
/** Gets the function to which this result variable belongs. */
282-
FuncDef getFunction() { result = fn }
292+
FuncDef getFunction() { result = f }
293+
294+
/** Gets the index of this result among all results of the function. */
295+
int getIndex() { result = index }
296+
297+
/** Holds if this is the `i`th result of function `fd`. */
298+
predicate isResultOf(FuncDef fd, int i) { fd = f and i = index }
283299
}
284300

285301
/**
@@ -396,13 +412,19 @@ class Function extends ValueEntity, @functionobject {
396412
Type getResultType(int i) { result = getType().(SignatureType).getResultType(i) }
397413

398414
/** Gets the body of this function, if any. */
399-
BlockStmt getBody() { none() }
415+
BlockStmt getBody() { result = getFuncDecl().getBody() }
400416

401417
/** Gets the `i`th parameter of this function. */
402-
Parameter getParameter(int i) { none() }
418+
Parameter getParameter(int i) { result.isParameterOf(getFuncDecl(), i) }
403419

404420
/** Gets a parameter of this function. */
405421
Parameter getAParameter() { result = getParameter(_) }
422+
423+
/** Gets the `i`th reslt variable of this function. */
424+
ResultVariable getResult(int i) { result.isResultOf(getFuncDecl(), i) }
425+
426+
/** Gets a result variable of this function. */
427+
ResultVariable getAResult() { result = getResult(_) }
406428
}
407429

408430
/**
@@ -512,10 +534,6 @@ class Method extends Function {
512534
class DeclaredFunction extends Function, DeclaredEntity, @declfunctionobject {
513535
override FuncDecl getFuncDecl() { result.getNameExpr() = this.getDeclaration() }
514536

515-
override BlockStmt getBody() { result = getFuncDecl().getBody() }
516-
517-
override Parameter getParameter(int i) { result = getFuncDecl().getParameter(i) }
518-
519537
override predicate mayHaveSideEffects() {
520538
not exists(getBody())
521539
or

ql/src/semmle/go/dataflow/internal/DataFlowUtil.qll

+18-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,22 @@ abstract class FunctionNode extends Node {
236236
abstract ReceiverNode getReceiver();
237237

238238
/**
239-
* Gets a value returned by the given function via a return statement or an assignment to a result variable.
239+
* Gets a value returned by the given function via a return statement or an assignment to a
240+
* result variable.
240241
*/
241242
abstract ResultNode getAResult();
243+
244+
/**
245+
* Gets the data-flow node corresponding to the `i`th result of this function.
246+
*/
247+
ResultNode getResult(int i) { result = getAResult() and result.getIndex() = i }
248+
249+
/**
250+
* Gets the function entity this node corresponds to.
251+
*
252+
* Note that this predicate has no result for function literals.
253+
*/
254+
Function getFunction() { none() }
242255
}
243256

244257
/** A representation of a function that is declared in the module scope. */
@@ -251,8 +264,7 @@ class GlobalFunctionNode extends FunctionNode, MkGlobalFunctionNode {
251264

252265
override string getName() { result = func.getName() }
253266

254-
/** Gets the function this node corresponds to. */
255-
Function getFunction() { result = func }
267+
override Function getFunction() { result = func }
256268

257269
override ReceiverNode getReceiver() { result = receiverNode(func.(Method).getReceiver()) }
258270

@@ -533,6 +545,9 @@ class ResultNode extends InstructionNode {
533545
or
534546
insn.(IR::ReadResultInstruction).reads(fd.getResultVar(i))
535547
}
548+
549+
/** Gets the index of this result among all results of the function. */
550+
int getIndex() { result = i }
536551
}
537552

538553
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| main.go:7:6:7:7 | f1 | 0 | main.go:7:9:7:9 | x |
2+
| main.go:9:12:9:13 | f2 | 0 | main.go:9:15:9:15 | x |
3+
| main.go:9:12:9:13 | f2 | 1 | main.go:9:18:9:18 | y |
4+
| main.go:9:12:9:13 | f2 | -1 | main.go:9:7:9:7 | t |
5+
| main.go:11:12:11:13 | f3 | 0 | main.go:11:15:11:15 | x |
6+
| main.go:11:12:11:13 | f3 | 1 | main.go:11:22:11:22 | y |
7+
| main.go:11:12:11:13 | f3 | -1 | main.go:11:7:11:7 | _ |
8+
| main.go:13:6:13:7 | f4 | 0 | main.go:13:9:13:9 | x |
9+
| main.go:13:6:13:7 | f4 | 1 | main.go:13:16:13:16 | y |
10+
| main.go:15:6:15:7 | f5 | 0 | main.go:15:9:15:9 | x |
11+
| main.go:17:6:17:7 | f6 | 0 | main.go:17:9:17:9 | x |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import go
2+
3+
from Function f, int i
4+
select f, i, f.getParameter(i)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| main.go:17:6:17:7 | f6 | 0 | main.go:17:17:17:17 | y |
2+
| main.go:19:6:19:7 | f7 | 0 | main.go:19:12:19:12 | x |
3+
| main.go:19:6:19:7 | f7 | 1 | main.go:19:15:19:15 | y |
4+
| main.go:21:6:21:7 | f8 | 0 | main.go:21:12:21:12 | x |
5+
| main.go:21:6:21:7 | f8 | 1 | main.go:21:19:21:19 | _ |
6+
| main.go:23:6:23:7 | f9 | 0 | main.go:23:12:23:12 | _ |
7+
| main.go:23:6:23:7 | f9 | 1 | main.go:23:19:23:19 | y |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import go
2+
3+
from Function f, int i
4+
select f, i, f.getResult(i)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
type t int
4+
5+
func main() {}
6+
7+
func f1(x int) {}
8+
9+
func (t t) f2(x, y int) {}
10+
11+
func (_ t) f3(x int, y string) {}
12+
13+
func f4(x int, y ...string) {}
14+
15+
func f5(x int) int { return 0 }
16+
17+
func f6(x int) (y int) { return 0 }
18+
19+
func f7() (x, y int) { return 0, 0 }
20+
21+
func f8() (x int, _ string) { return 0, "" }
22+
23+
func f9() (_ int, y string) { return 0, "" }

0 commit comments

Comments
 (0)