internal/shaderlister: bug fix: a directive in a function must be ignored

Updates #3157
This commit is contained in:
Hajime Hoshi 2025-02-09 01:50:06 +09:00
parent 0182f7044d
commit aaabea02cb
2 changed files with 21 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"golang.org/x/tools/go/ast/inspector"
@ -216,8 +217,21 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
visitedPatterns := map[string]struct{}{}
visitedPaths := map[string]struct{}{}
for _, f := range pkg.Syntax {
var funcs []*ast.FuncDecl
for _, decl := range f.Decls {
if f, ok := decl.(*ast.FuncDecl); ok {
funcs = append(funcs, f)
}
}
for _, c := range f.Comments {
for _, l := range c.List {
// Ignore the line if it is in a function declaration.
if slices.ContainsFunc(funcs, func(f *ast.FuncDecl) bool {
return f.Pos() <= l.Pos() && l.Pos() < f.End()
}) {
continue
}
m := reShaderFileDirective.FindString(l.Text)
if m == "" {
continue
@ -293,6 +307,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
// Resolve ebitengine:shadersource directives.
var genDeclStack []*ast.GenDecl
// inspector.Inspector doesn't iterate comments that are not attached to any other nodes.
in := inspector.New(pkg.Syntax)
in.Nodes([]ast.Node{
(*ast.GenDecl)(nil),

View File

@ -75,3 +75,9 @@ const (
//ebitengine:shaderfile *_kage.go *_kage.go *_kage.go
//ebitengine:shaderfile nonexistent.go
func foo() {
// Non top-level files are ignored.
//ebitengine:shaderfile *_notkage.go
}