gowrite

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gowrite provides a stable output path for generated Go AST files whose nodes often have missing or partial token positions.

Why this package exists:

Generated AST from C/C++ conversion commonly contains token.NoPos on many declaration-related nodes. Older formatting behavior sometimes tolerated this, but newer go/printer behavior is stricter about relative positions. Without explicit anchors, comments and declarations may collapse onto one line, move to unexpected places, or print unstable forms (for example around empty interface types in function signatures).

What gowrite does:

  1. Build a synthetic token.File and assign only the minimal declaration anchors needed for stable formatting.
  2. Run go/format (format.Node) using that synthetic FileSet.

This package does not try to fully reconstruct original source locations. It only supplies enough deterministic positions for correct, readable output.

High-level algorithm:

  • Assign positions in one forward sequence while walking declarations. For comment groups, each comment's Slash gets a new position, and the cursor advances by the comment's line span (not just by comment count). This preserves spacing between multiline comments and the following nodes.

  • Register a synthetic token.File after assignment, then install a monotonic line table with SetLines. Assigned positions align with the final file base via FileSet.Base() from the same FileSet instance.

Why comment line span matters:

Consider:

/*
ExecuteFoo comment
*/
//go:linkname CustomExecuteFoo2 C.ExecuteFoo2
func CustomExecuteFoo2()

If comments are advanced by +1 each, the block comment and //go:linkname may end up on the same logical line. Advancing by real line span prevents this.

Empty interface handling scope:

We intentionally handle only function-related signatures: - ast.FuncDecl.Type - type declarations whose TypeSpec is ast.FuncType

This is enough for cases like ...interface{} in variadic signatures, while avoiding broad, file-wide interface rewrites.

Final line table:

The synthetic token.File uses a simple monotonic line table via SetLines. This gives go/printer consistent line boundaries for all assigned anchors.

Non-goals:

- No semantic AST transformation. - No full-source positional normalization. - No attempt to preserve original file offsets from C/C++ headers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteFile

func WriteFile(pkg *gogen.Package, outFile string, fname ...string) error

WriteFile writes a gogen file using a synthetic-position pass before formatting. This makes declaration comments stable even when source nodes do not carry positions.

func WriteTo

func WriteTo(dst io.Writer, pkg *gogen.Package, fname ...string) error

WriteTo formats a gogen file after injecting minimal declaration anchors.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL