test: enable debug testing

This commit is contained in:
Li Jie
2024-11-26 14:39:29 +08:00
parent 954cc0e8bc
commit 43c4a3bfdc
5 changed files with 26 additions and 47 deletions

View File

@@ -21,7 +21,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/llgen" "github.com/goplus/llgo/internal/llgen"
"github.com/goplus/mod" "github.com/goplus/mod"
) )
@@ -38,20 +37,6 @@ func main() {
llgenDir(dir + "/cl/_testdata") llgenDir(dir + "/cl/_testdata")
} }
func isDbgSymEnabled(flagsFile string) bool {
data, err := os.ReadFile(flagsFile)
if err != nil {
return false
}
toks := strings.Split(strings.Join(strings.Split(string(data), "\n"), " "), " ")
for _, tok := range toks {
if tok == "-dbg" {
return true
}
}
return false
}
func llgenDir(dir string) { func llgenDir(dir string) {
fis, err := os.ReadDir(dir) fis, err := os.ReadDir(dir)
check(err) check(err)
@@ -63,8 +48,6 @@ func llgenDir(dir string) {
testDir := dir + "/" + name testDir := dir + "/" + name
fmt.Fprintln(os.Stderr, "llgen", testDir) fmt.Fprintln(os.Stderr, "llgen", testDir)
check(os.Chdir(testDir)) check(os.Chdir(testDir))
dbg := isDbgSymEnabled("flags.txt")
cl.EnableDebugSymbols(dbg)
llgen.SmartDoFile(testDir) llgen.SmartDoFile(testDir)
} }
} }

View File

@@ -107,32 +107,12 @@ func Pkg(t *testing.T, pkgPath, outFile string) {
} }
} }
func isDbgSymEnabled(flagsFile string) bool {
data, err := os.ReadFile(flagsFile)
if err != nil {
return false
}
toks := strings.Split(strings.Join(strings.Split(string(data), "\n"), " "), " ")
for _, tok := range toks {
if tok == "-dbg" {
return true
}
}
return false
}
func testFrom(t *testing.T, pkgDir, sel string) { func testFrom(t *testing.T, pkgDir, sel string) {
if sel != "" && !strings.Contains(pkgDir, sel) { if sel != "" && !strings.Contains(pkgDir, sel) {
return return
} }
log.Println("Parsing", pkgDir) log.Println("Parsing", pkgDir)
out := pkgDir + "/out.ll" out := pkgDir + "/out.ll"
dbg := isDbgSymEnabled(pkgDir + "/flags.txt")
if dbg {
cl.EnableDebugSymbols(true)
defer cl.EnableDebugSymbols(false)
cl.DebugSymbols() // just for coverage
}
b, err := os.ReadFile(out) b, err := os.ReadFile(out)
if err != nil { if err != nil {
t.Fatal("ReadFile failed:", err) t.Fatal("ReadFile failed:", err)

View File

@@ -57,15 +57,10 @@ func SetDebug(dbgFlags dbgFlags) {
debugGoSSA = (dbgFlags & DbgFlagGoSSA) != 0 debugGoSSA = (dbgFlags & DbgFlagGoSSA) != 0
} }
// EnableDebugSymbols enables debug symbols.
func EnableDebugSymbols(b bool) { func EnableDebugSymbols(b bool) {
debugSymbols = b debugSymbols = b
} }
func DebugSymbols() bool {
return debugSymbols
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
type instrOrValue interface { type instrOrValue interface {

View File

@@ -122,7 +122,6 @@ const (
func Do(args []string, conf *Config) ([]Package, error) { func Do(args []string, conf *Config) ([]Package, error) {
flags, patterns, verbose := ParseArgs(args, buildFlags) flags, patterns, verbose := ParseArgs(args, buildFlags)
cl.EnableDebugSymbols(IsDebugEnabled())
flags = append(flags, "-tags", "llgo") flags = append(flags, "-tags", "llgo")
cfg := &packages.Config{ cfg := &packages.Config{
Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile, Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile,
@@ -138,6 +137,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
} }
} }
cl.EnableDebugSymbols(IsDebugEnabled())
llssa.Initialize(llssa.InitAll) llssa.Initialize(llssa.InitAll)
target := &llssa.Target{ target := &llssa.Target{
@@ -187,7 +187,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
}) })
buildMode := ssaBuildMode buildMode := ssaBuildMode
if cl.DebugSymbols() { if IsDebugEnabled() {
buildMode |= ssa.GlobalDebug buildMode |= ssa.GlobalDebug
} }
if !IsOptimizeEnabled() { if !IsOptimizeEnabled() {
@@ -460,7 +460,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs
} }
} }
args = append(args, exargs...) args = append(args, exargs...)
if cl.DebugSymbols() { if IsDebugEnabled() {
args = append(args, "-gdwarf-4") args = append(args, "-gdwarf-4")
} }

View File

@@ -32,6 +32,13 @@ func GenFrom(fileOrPkg string) string {
} }
func genFrom(pkgPath string) (build.Package, error) { func genFrom(pkgPath string) (build.Package, error) {
oldDbg := os.Getenv("LLGO_DEBUG")
dbg := isDbgSymEnabled(filepath.Join(pkgPath, "flags.txt"))
if dbg {
os.Setenv("LLGO_DEBUG", "1")
}
defer os.Setenv("LLGO_DEBUG", oldDbg)
conf := &build.Config{ conf := &build.Config{
Mode: build.ModeGen, Mode: build.ModeGen,
AppExt: build.DefaultAppExt(), AppExt: build.DefaultAppExt(),
@@ -49,8 +56,22 @@ func DoFile(fileOrPkg, outFile string) {
check(err) check(err)
} }
func SmartDoFile(pkgPath ...string) { func isDbgSymEnabled(flagsFile string) bool {
pkg, err := genFrom(pkgPath[0]) data, err := os.ReadFile(flagsFile)
if err != nil {
return false
}
toks := strings.Split(strings.Join(strings.Split(string(data), "\n"), " "), " ")
for _, tok := range toks {
if tok == "-dbg" {
return true
}
}
return false
}
func SmartDoFile(pkgPath string) {
pkg, err := genFrom(pkgPath)
check(err) check(err)
const autgenFile = "llgo_autogen.ll" const autgenFile = "llgo_autogen.ll"