Skip to content

Commit 6bb4aef

Browse files
committed
Simplify zero-init code after unifying result-zero-init
1 parent 992a0b5 commit 6bb4aef

2 files changed

Lines changed: 10 additions & 20 deletions

File tree

go/ql/lib/semmle/go/controlflow/ControlFlowGraphShared.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ module GoCfg {
578578
n = fd.getBody() and
579579
exists(fd.getBody()) and
580580
exists(fd.getResultVar(i)) and
581-
exists(fd.getResultVar(i).(Go::ResultVariable).getFunction().getBody()) and
582581
tag = "zero-init:" + i.toString()
583582
)
584583
or

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ module IR {
809809

810810
override Instruction getRhs() { result = this }
811811

812+
/** Gets the variable (a local or a named result) being zero-initialized. */
813+
ValueEntity getVariable() { result = v }
814+
812815
override Type getResultType() { result = v.getType() }
813816

814817
override ControlFlow::Root getRoot() { result.isRootOf(v.getDeclaration()) }
@@ -1112,14 +1115,12 @@ module IR {
11121115
lhs = fd.getParameter(idx).getDeclaration()
11131116
)
11141117
or
1115-
exists(FuncDef fd, int idx |
1116-
write.isAdditional(fd.getBody(), "zero-init:" + idx.toString()) and
1117-
lhs = fd.getResultVar(idx).getDeclaration()
1118-
)
1119-
or
1120-
exists(ValueSpec spec, int idx |
1121-
write.isAdditional(spec, "zero-init:" + idx.toString()) and
1122-
lhs = spec.getNameExpr(idx)
1118+
// The `zero-init` node writes the variable it initializes: a named
1119+
// function result or a local declared without an initializer.
1120+
exists(int idx, AstNode an | write.isAdditional(an, "zero-init:" + idx.toString()) |
1121+
an = any(FuncDef fd | lhs = fd.getResultVar(idx).getDeclaration()).getBody()
1122+
or
1123+
an = any(ValueSpec spec | lhs = spec.getNameExpr(idx))
11231124
)
11241125
or
11251126
// A tuple-destructuring `extract` node writes its component directly (see
@@ -1392,17 +1393,7 @@ module IR {
13921393
* Gets the instruction corresponding to the implicit initialization of `v`
13931394
* to its zero value.
13941395
*/
1395-
EvalImplicitInitInstruction implicitInitInstruction(ValueEntity v) {
1396-
exists(ValueSpec spec, int i |
1397-
spec.getNameExpr(i) = v.getDeclaration() and
1398-
result.isAdditional(spec, "zero-init:" + i.toString())
1399-
)
1400-
or
1401-
exists(FuncDef fd, int i |
1402-
fd.getResultVar(i) = v and
1403-
result.isAdditional(fd.getBody(), "zero-init:" + i.toString())
1404-
)
1405-
}
1396+
EvalImplicitInitInstruction implicitInitInstruction(ValueEntity v) { result.getVariable() = v }
14061397

14071398
/**
14081399
* Gets the instruction corresponding to the extraction of the `idx`th element

0 commit comments

Comments
 (0)