Browse Source

ir: add descriptive error messages to SimpleDCE pass

This commit modifies the SimpleDCE pass to emit errors similar to those emitted by gc when the main function is missing.
pull/1013/head
Jaden Weiss 5 years ago
committed by Ayke
parent
commit
2501602b4f
  1. 5
      compiler/compiler.go
  2. 14
      ir/passes.go

5
compiler/compiler.go

@ -237,7 +237,10 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
c.ir = ir.NewProgram(lprogram, pkgName)
// Run a simple dead code elimination pass.
c.ir.SimpleDCE()
err = c.ir.SimpleDCE()
if err != nil {
return c.mod, nil, []error{err}
}
// Initialize debug information.
if c.Debug() {

14
ir/passes.go

@ -1,6 +1,7 @@
package ir
import (
"errors"
"go/types"
"golang.org/x/tools/go/ssa"
@ -58,7 +59,7 @@ func signature(sig *types.Signature) string {
// Simple pass that removes dead code. This pass makes later analysis passes
// more useful.
func (p *Program) SimpleDCE() {
func (p *Program) SimpleDCE() error {
// Unmark all functions.
for _, f := range p.Functions {
f.flag = false
@ -66,7 +67,14 @@ func (p *Program) SimpleDCE() {
// Initial set of live functions. Include main.main, *.init and runtime.*
// functions.
main := p.mainPkg.Members["main"].(*ssa.Function)
main, ok := p.mainPkg.Members["main"].(*ssa.Function)
if !ok {
if p.mainPkg.Members["main"] == nil {
return errors.New("function main is undeclared in the main package")
} else {
return errors.New("cannot declare main - must be func")
}
}
runtimePkg := p.Program.ImportedPackage("runtime")
mathPkg := p.Program.ImportedPackage("math")
taskPkg := p.Program.ImportedPackage("internal/task")
@ -136,4 +144,6 @@ func (p *Program) SimpleDCE() {
}
}
p.Functions = livefunctions
return nil
}

Loading…
Cancel
Save