Skip to content

Commit

Permalink
internal/shaderlister: bug fix: a directive in a function must be ign…
Browse files Browse the repository at this point in the history
…ored

Updates #3157
  • Loading branch information
hajimehoshi committed Feb 9, 2025
1 parent 0182f70 commit aaabea0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/shaderlister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"

"golang.org/x/tools/go/ast/inspector"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions internal/shaderlister/shaderlistertest/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit aaabea0

Please sign in to comment.