Source file src/cmd/compile/internal/noder/dump.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package noder
     6  
     7  import (
     8  	"sync"
     9  
    10  	"cmd/compile/internal/base"
    11  	"cmd/compile/internal/ir"
    12  	"cmd/compile/internal/syntax"
    13  	"cmd/compile/internal/types2"
    14  )
    15  
    16  // MatchASTDump returns true if the fn matches the value
    17  // of the astdump debug flag.
    18  func MatchASTDump(fn *syntax.FuncDecl) bool {
    19  	if len(base.Debug.AstDump) == 0 {
    20  		return false
    21  	}
    22  	if fn.Name == nil {
    23  		return false
    24  	}
    25  	return matchForDump(fn, base.Ctxt.Pkgpath)
    26  }
    27  
    28  // matchForDump is marked noinline to ensure that the exported
    29  // function MatchAstDump IS inlineable and is also small, because
    30  // common case is AstDump is not set.
    31  //
    32  //go:noinline
    33  func matchForDump(fn *syntax.FuncDecl, pkgPath string) bool {
    34  	return ir.MatchPkgFn(pkgPath, fn.Name.Value, base.Debug.AstDump)
    35  }
    36  
    37  func escapedFileName(fn *syntax.FuncDecl, suffix string) string {
    38  	return ir.EscapedFileName(base.Ctxt.Pkgpath+"."+fn.Name.Value, suffix)
    39  }
    40  
    41  var mu sync.Mutex
    42  var htmlWriters = make(map[*syntax.FuncDecl]*HTMLWriter)
    43  var orderedFuncs = []*syntax.FuncDecl{}
    44  
    45  // DumpNodeHTML dumps the node n to the HTML writer for fn.
    46  func DumpNodeHTML(pkg *types2.Package, file *syntax.File, info *types2.Info, fn *syntax.FuncDecl, why string, n syntax.Node) {
    47  	mu.Lock()
    48  	defer mu.Unlock()
    49  	w, ok := htmlWriters[fn]
    50  	if !ok {
    51  		name := escapedFileName(fn, ".syntax.html")
    52  		w = NewHTMLWriter(pkg, file, info, name, fn, "")
    53  		htmlWriters[fn] = w
    54  		orderedFuncs = append(orderedFuncs, fn)
    55  	}
    56  	w.WritePhase(why, why)
    57  }
    58  
    59  // CloseHTMLWriters closes the HTML writer for fn, if one exists.
    60  func CloseHTMLWriters() {
    61  	mu.Lock()
    62  	defer mu.Unlock()
    63  	for _, fn := range orderedFuncs {
    64  		if w, ok := htmlWriters[fn]; ok {
    65  			w.Close("Writing html syntax output for %s to %s\n", w.pkgFuncName(), w.Path())
    66  			delete(htmlWriters, fn)
    67  		}
    68  	}
    69  	orderedFuncs = nil
    70  }
    71  

View as plain text