Simplify zero-init code after unifying result-zero-init

This commit is contained in:
Owen Mansel-Chan
2026-07-11 00:01:14 +01:00
parent 992a0b5c17
commit 6bb4aefa4e
2 changed files with 10 additions and 20 deletions

View File

@@ -578,7 +578,6 @@ module GoCfg {
n = fd.getBody() and
exists(fd.getBody()) and
exists(fd.getResultVar(i)) and
exists(fd.getResultVar(i).(Go::ResultVariable).getFunction().getBody()) and
tag = "zero-init:" + i.toString()
)
or

View File

@@ -809,6 +809,9 @@ module IR {
override Instruction getRhs() { result = this }
/** Gets the variable (a local or a named result) being zero-initialized. */
ValueEntity getVariable() { result = v }
override Type getResultType() { result = v.getType() }
override ControlFlow::Root getRoot() { result.isRootOf(v.getDeclaration()) }
@@ -1112,14 +1115,12 @@ module IR {
lhs = fd.getParameter(idx).getDeclaration()
)
or
exists(FuncDef fd, int idx |
write.isAdditional(fd.getBody(), "zero-init:" + idx.toString()) and
lhs = fd.getResultVar(idx).getDeclaration()
)
or
exists(ValueSpec spec, int idx |
write.isAdditional(spec, "zero-init:" + idx.toString()) and
lhs = spec.getNameExpr(idx)
// The `zero-init` node writes the variable it initializes: a named
// function result or a local declared without an initializer.
exists(int idx, AstNode an | write.isAdditional(an, "zero-init:" + idx.toString()) |
an = any(FuncDef fd | lhs = fd.getResultVar(idx).getDeclaration()).getBody()
or
an = any(ValueSpec spec | lhs = spec.getNameExpr(idx))
)
or
// A tuple-destructuring `extract` node writes its component directly (see
@@ -1392,17 +1393,7 @@ module IR {
* Gets the instruction corresponding to the implicit initialization of `v`
* to its zero value.
*/
EvalImplicitInitInstruction implicitInitInstruction(ValueEntity v) {
exists(ValueSpec spec, int i |
spec.getNameExpr(i) = v.getDeclaration() and
result.isAdditional(spec, "zero-init:" + i.toString())
)
or
exists(FuncDef fd, int i |
fd.getResultVar(i) = v and
result.isAdditional(fd.getBody(), "zero-init:" + i.toString())
)
}
EvalImplicitInitInstruction implicitInitInstruction(ValueEntity v) { result.getVariable() = v }
/**
* Gets the instruction corresponding to the extraction of the `idx`th element