Repository navigation
feat(tool/merge): merge per-platform packages - #1173
Conversation
There was a problem hiding this comment.
Review summary
The new tool/merge package is well organized and well tested. I found two correctness bugs:
- A
var A, B = pair()declaration is written twice in the shared output, which doesn't compile. - The
z_check rejectsz_*.gofiles that llcppg itself generates from_-prefixed headers.
There are also some smaller hardening, edge-case and license-header items. Details are inline. Performance looks fine: there is some repeated hashing and comparing per type spec in replaced and collectTypes, but nothing blocking.
| blockDoc := declBlockDoc(shared.decl) | ||
| if i > 0 && groups[i-1].declIndex == group.declIndex { | ||
| gen := list[len(list)-1].(*ast.GenDecl) | ||
| gen.Specs = append(gen.Specs, shared.spec) |
There was a problem hiding this comment.
Bug: a spec that declares several names is written twice. collectValues makes one group per name. For var A, B = pair() (which the comment at collect.go:55-56 says is supported), A and B both become shared groups, and chooseRef gives them the same declRef. After sorting they sit next to each other with the same declIndex, so this line appends the same spec a second time:
var (
A, B = pair()
A, B = pair()
)That fails to compile (A redeclared). Skip a group whose declRef equals the previous one, or build shared value groups per spec instead of per name. A multi-name case in _testmerge would catch this.
| for _, pkg := range pkgs { | ||
| platform := pkg.platform() | ||
| for name, file := range pkg.Files { | ||
| if strings.HasPrefix(name, "z_") { |
There was a problem hiding this comment.
This check rejects files that llcppg itself generates. goFileOf (tool/gen.go:302-304) turns a header that starts with _ into a z_*.go file. For example, Darwin's _types.h becomes z_types.go. Merge would fail on that input with reserved file name. Only names that can actually collide with what build writes should be rejected, i.e. z_<goos>_<goarch>.go for the platforms being merged.
| constraints := make(map[string]string) // file name => build constraint | ||
| for platform, pkg := range p.files.pkgs { | ||
| if list := p.buildPlatform(platform); len(list) != 0 { | ||
| name := "z_" + pkg.Tags[0] + "_" + pkg.Tags[1] + ".go" |
There was a problem hiding this comment.
Two smaller points:
- Shared output keeps the source file name. Go reads some file names as build constraints: a header
foo_linux.hproducesfoo_linux.go, andx_test.hproducesx_test.go. A shared declaration written into such a file is silently dropped on the other platforms, or left out of the package entirely for_test.go. Rename or reject these names. - Tags go straight into the file name and the
//go:buildline. Only non-empty tags are checked. Tags{"linux_x","amd64"}and{"linux","x_amd64"}both map to the same file name, and the second overwrites the first indecls. A tag containing spaces,||,/or a newline would corrupt the constraint or the path. Validating tags as identifiers insortPackages(e.g.^[a-z0-9]+$) covers all of this.
|
|
||
| func (p *mergeIndex) collectValue(name string, decl *ast.GenDecl, ref declRef) { | ||
| refs := p.values[name] | ||
| if len(refs) != 0 { |
There was a problem hiding this comment.
Minor: constants that rely on implicit repetition can be wrongly treated as equal. Specs are compared on their own syntax. In const (A = 1 << iota; B), the spec for B has no type and no value, so it matches a bare B on another platform even when the line above it differs. B then becomes shared and is printed alone as const B, which doesn't compile. llcppg may always write explicit values, but it would be safer to never share a ValueSpec with empty Values (or to compare it together with the initializer it inherits).
| @@ -0,0 +1,468 @@ | |||
| // Copyright 2014 The Go Authors. All rights reserved. | |||
| // Use of this source code is governed by a BSD-style | |||
| // license that can be found in tool/merge/internal/astutil/LICENSE. | |||
There was a problem hiding this comment.
The license file this header points to doesn't exist. This header (and type_map_test.go) refers to tool/merge/internal/astutil/LICENSE, and internal/astutil/equal.go refers to "the LICENSE file", but this PR adds no such file. The repository root LICENSE is Apache-2.0. Add the Go BSD LICENSE file there, or embed the text the way filter.go does.
Also, setHasher, theHasher and makeHasher are unused upstream leftovers. Their "do not use astHasher" comment is now misleading, because mergeIndex relies on astHasher.identName. Consider removing them.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1173 +/- ##
==========================================
+ Coverage 91.35% 92.30% +0.94%
==========================================
Files 27 39 +12
Lines 3587 4456 +869
==========================================
+ Hits 3277 4113 +836
- Misses 310 343 +33
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Group const and var declarations by spec. Add golden coverage for input validation, types and methods, import references, and comments.
Add AST-based merging for llcppg's per-platform output in
tool/merge. Related to #1129.merge.Mergeandmerge.PlatformPackage.llgo test -count=1 ./tool ./tool/merge/....Callers can consume bindings for all input platforms through one Go package.