Skip to content

feat: upgrade gogen and use custom go printer output & fix some newline before decl#641

Merged
luoliwoshang merged 21 commits into
xgo-dev:mainfrom
luoliwoshang:fix/issue-635-gowrite-parser-comment
Feb 12, 2026
Merged

feat: upgrade gogen and use custom go printer output & fix some newline before decl#641
luoliwoshang merged 21 commits into
xgo-dev:mainfrom
luoliwoshang:fix/issue-635-gowrite-parser-comment

Conversation

@luoliwoshang
Copy link
Copy Markdown
Contributor

@luoliwoshang luoliwoshang commented Feb 12, 2026

Summary

  • bump github.com/goplus/gogen to v1.20.2
  • add internal/gowrite as the custom Go output path (go/format with a controlled synthetic token.FileSet)
  • switch tool/gengo and cmd/gogensig to use internal/gowrite
  • adjust parser comment handling: keep one multiline comment as one ast.Comment.Text entry in CommentGroup.List
  • update parser/cmp test expectations accordingly
  • create const groups lazily to avoid generating unnecessary empty const ()

Why not use gogen's built-in print path?

This change needs full control of the token.FileSet used during printing.

llcppg often generates AST nodes without full original source positions. To keep output stable on newer go/printer, we assign synthetic anchors and line table entries in a specific FileSet. If the final print path does not use the same controllable FileSet, then even with assigned Pos values, comment/declaration spacing can still collapse or drift.

gogen's default print path does not let this PR choose and manage the exact FileSet lifecycle required by that strategy, so this PR uses a custom writer path.

Parser comment behavior

For Go AST/printer compatibility, a multiline block comment should be represented as a single ast.Comment.Text (for example one /* ... */ text), not split into multiple plain lines.

So in parser output we now keep such multiline comments as one CommentGroup.List item. This keeps go/printer behavior correct and stable.

Future refinement: multiline // runs can be split into multiple Comment.Text entries when/if needed.

Context

Validation

  • go test ./internal/gowrite -count=1
  • go test ./cl/internal/convert -count=1
  • cd _xtool/internal/parser && llgo test .

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @luoliwoshang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where generated Go ASTs, particularly those derived from C outputs, sometimes lacked per-node source positions. This deficiency, exacerbated by an upgrade to gogen/go-printer, led to unstable and incorrect comment formatting, including collapsing or misplacement. The core solution involves introducing a new gowrite package that applies a synthetic declaration anchor pass before formatting, thereby restoring stable and predictable output without extensively altering the existing conversion pipeline. This change ensures that generated Go code maintains proper structure and readability, especially concerning comments.

Highlights

  • New gowrite package introduced: A new gowrite package has been added to ensure stable Go printer output by injecting synthetic declaration anchors, specifically addressing issues with AST nodes missing source positions.
  • Tooling migration to gowrite: The gogensig and gengo tools have been migrated to utilize the new gowrite.WriteFile functionality for improved code generation stability.
  • Comment parsing adjustment: The parser has been modified to retain comment text without automatically appending a trailing newline, aligning with the new formatting behavior.
  • Test expectation files updated: Various expect.json test files have been updated to reflect the changes in comment parsing and formatting.
  • Dependency upgrade: The github.com/goplus/gogen dependency has been upgraded to v1.20.2.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • _xtool/internal/parser/parser.go
    • Removed the automatic addition of a trailing newline character to parsed comment text.
  • _xtool/internal/parser/testdata/comment/expect.json
    • Updated expected JSON output for comments to remove trailing newline characters.
  • _xtool/internal/parser/testdata/forwarddecl1/expect.json
    • Adjusted expected JSON output for comments to remove trailing newline characters.
  • _xtool/internal/parser/testdata/typeof/expect.json
    • Modified expected JSON output for comments to remove trailing newline characters.
  • cmd/gogensig/gogensig.go
    • Imported the new github.com/goplus/llcppg/internal/gowrite package.
    • Switched from pkg.WriteFile to gowrite.WriteFile for writing generated Go files.
  • go.mod
    • Updated the github.com/goplus/gogen dependency from v1.19.7 to v1.20.2.
  • go.sum
    • Updated cryptographic checksums for the github.com/goplus/gogen module.
  • internal/gowrite/gowrite.go
    • Added a new package gowrite containing WriteFile and WriteTo functions.
    • Implemented assignDeclAnchors to inject synthetic token.Pos values for declarations and comments, stabilizing go/format output.
  • internal/gowrite/gowrite_test.go
    • Added unit tests for WriteTo to verify correct formatting of empty and non-empty function bodies.
  • tool/gengo.go
    • Imported the new github.com/goplus/llcppg/internal/gowrite package.
    • Switched from pkg.WriteFile to gowrite.WriteFile for writing generated Go files.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix for unstable Go printer output when AST nodes lack position information. The main change is the new internal/gowrite package, which implements a synthetic-position pass before formatting the Go code. This ensures stable comment placement. The change is adopted in gogensig and gengo tools. Additionally, the comment parser is updated to not force a trailing newline, and test expectations are updated accordingly. The gogen dependency is also bumped. The changes look solid and well-implemented. I have one suggestion for improving idiomatic error handling in the new gowrite package.

Comment thread internal/gowrite/gowrite.go
@fennoai
Copy link
Copy Markdown
Contributor

fennoai Bot commented Feb 12, 2026

Clean approach to stabilizing go/format output for synthetic ASTs. The position-anchor injection technique is well-suited here, and the parser comment fix aligns with go/ast.Comment.Text conventions.

Notable: cl/internal/convert/convert_test.go:248 still uses pkg.WriteFile (with a // todo:reuse same write logic comment), so TestFromTestdata doesn't exercise the new gowrite code path. See inline comments for details.

Comment thread internal/gowrite/gowrite.go Outdated
Comment thread internal/gowrite/gowrite_test.go
Comment thread internal/gowrite/gowrite.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 12, 2026

Codecov Report

❌ Patch coverage is 98.07692% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.30%. Comparing base (d59271a) to head (713b743).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/gowrite/gowrite.go 97.91% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #641      +/-   ##
==========================================
+ Coverage   84.77%   85.30%   +0.52%     
==========================================
  Files          27       28       +1     
  Lines        2318     2415      +97     
==========================================
+ Hits         1965     2060      +95     
- Misses        310      311       +1     
- Partials       43       44       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@luoliwoshang luoliwoshang changed the title fix: stabilize go printer output when AST nodes miss positions chore: upgrade gogen and use custom go printer output Feb 12, 2026
@luoliwoshang luoliwoshang changed the title chore: upgrade gogen and use custom go printer output fix: upgrade gogen and use custom go printer output Feb 12, 2026
@luoliwoshang luoliwoshang changed the title fix: upgrade gogen and use custom go printer output feat: upgrade gogen and use custom go printer output & fix some newline before decl Feb 12, 2026
…ment' into fix/issue-635-gowrite-parser-comment
@luoliwoshang luoliwoshang force-pushed the fix/issue-635-gowrite-parser-comment branch from 025dbbe to 713b743 Compare February 12, 2026 11:30
@luoliwoshang luoliwoshang merged commit 0241693 into xgo-dev:main Feb 12, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants