Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 8667b64

Browse files
author
Max Schaefer
committed
Make result variables aware of their index.
1 parent 88c740b commit 8667b64

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

ql/src/semmle/go/Decls.qll

Lines changed: 1 addition & 8 deletions
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 }

ql/src/semmle/go/Scopes.qll

Lines changed: 19 additions & 3 deletions
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
/**

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,15 @@ 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 }
242248
}
243249

244250
/** A representation of a function that is declared in the module scope. */
@@ -533,6 +539,9 @@ class ResultNode extends InstructionNode {
533539
or
534540
insn.(IR::ReadResultInstruction).reads(fd.getResultVar(i))
535541
}
542+
543+
/** Gets the index of this result among all results of the function. */
544+
int getIndex() { result = i }
536545
}
537546

538547
/**

0 commit comments

Comments
 (0)