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.
Этот коммит содержится в:
Jaden Weiss 2020-03-28 14:35:34 -04:00 коммит произвёл Ayke
родитель 5cc130bb6e
коммит 2501602b4f
2 изменённых файлов: 16 добавлений и 3 удалений

Просмотреть файл

@ -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() {

Просмотреть файл

@ -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
}