-
Notifications
You must be signed in to change notification settings - Fork 47
build: fix ldflags rewrites and prep caching #1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9b76be9
support ldflags rewrites for initialized globals
cpunion e2bb684
build: override vars in alt pkg
cpunion 22a4362
cl: fix global var rewrite in alt pkg
cpunion 1e4616a
build: don't replace ExportFile
cpunion 8ba8ec7
build: write exports into temp files
cpunion b0f5d34
cl: add rewrite coverage test
cpunion 2a52d42
cl: broaden rewrite coverage
cpunion 3a1d869
rewrite: address review feedback
cpunion 4b26ccc
cl: cover rewrite guards
cpunion d17ff25
build: improve error handling and code quality
xgopilot 1ba7d1e
fix: change to isStringPtrType for global string var
xgopilot 034b05c
cl: remove Underlying() call to reject string type aliases in rewrites
xgopilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package dep | ||
|
|
||
| import "fmt" | ||
|
|
||
| var VarName = "dep-default" | ||
| var VarPlain string | ||
|
|
||
| func PrintVar() { | ||
| fmt.Printf("dep.VarName: %s\n", VarName) | ||
| fmt.Printf("dep.VarPlain: %s\n", VarPlain) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "runtime" | ||
|
|
||
| dep "github.com/goplus/llgo/cl/_testgo/rewrite/dep" | ||
| ) | ||
|
|
||
| var VarName = "main-default" | ||
| var VarPlain string | ||
|
|
||
| func printLine(label, value string) { | ||
| fmt.Printf("%s: %s\n", label, value) | ||
| } | ||
|
|
||
| func main() { | ||
| printLine("main.VarName", VarName) | ||
| printLine("main.VarPlain", VarPlain) | ||
| dep.PrintVar() | ||
| printLine("runtime.GOROOT()", runtime.GOROOT()) | ||
| printLine("runtime.Version()", runtime.Version()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| //go:build !llgo | ||
| // +build !llgo | ||
|
|
||
| package cl | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "go/types" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| gpackages "github.com/goplus/gogen/packages" | ||
| llssa "github.com/goplus/llgo/ssa" | ||
| "github.com/goplus/llgo/ssa/ssatest" | ||
| "golang.org/x/tools/go/ssa" | ||
| "golang.org/x/tools/go/ssa/ssautil" | ||
| ) | ||
|
|
||
| func init() { | ||
| llssa.Initialize(llssa.InitAll | llssa.InitNative) | ||
| } | ||
|
|
||
| func compileWithRewrites(t *testing.T, src string, rewrites map[string]string) string { | ||
| t.Helper() | ||
| fset := token.NewFileSet() | ||
| file, err := parser.ParseFile(fset, "rewrite.go", src, 0) | ||
| if err != nil { | ||
| t.Fatalf("parse failed: %v", err) | ||
| } | ||
| importer := gpackages.NewImporter(fset) | ||
| mode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics | ||
| pkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer}, fset, | ||
| types.NewPackage(file.Name.Name, file.Name.Name), []*ast.File{file}, mode) | ||
| if err != nil { | ||
| t.Fatalf("build package failed: %v", err) | ||
| } | ||
| prog := ssatest.NewProgramEx(t, nil, importer) | ||
| prog.TypeSizes(types.SizesFor("gc", runtime.GOARCH)) | ||
| ret, _, err := NewPackageEx(prog, nil, rewrites, pkg, []*ast.File{file}) | ||
| if err != nil { | ||
| t.Fatalf("NewPackageEx failed: %v", err) | ||
| } | ||
| return ret.String() | ||
| } | ||
|
|
||
| func TestRewriteGlobalStrings(t *testing.T) { | ||
| const src = `package rewritepkg | ||
| var VarInit = "original_value" | ||
| var VarPlain string | ||
| func Use() string { return VarInit + VarPlain } | ||
| ` | ||
| ir := compileWithRewrites(t, src, map[string]string{ | ||
| "VarInit": "rewrite_init", | ||
| "VarPlain": "rewrite_plain", | ||
| }) | ||
| if strings.Contains(ir, "original_value") { | ||
| t.Fatalf("original initializer still present:\n%s", ir) | ||
| } | ||
| for _, want := range []string{`c"rewrite_init"`, `c"rewrite_plain"`} { | ||
| if !strings.Contains(ir, want) { | ||
| t.Fatalf("missing %s in IR:\n%s", want, ir) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteSkipsNonConstStores(t *testing.T) { | ||
| const src = `package rewritepkg | ||
| import "strings" | ||
| var VarInit = strings.ToUpper("original_value") | ||
| var VarPlain string | ||
| func Use() string { return VarInit + VarPlain } | ||
| ` | ||
| ir := compileWithRewrites(t, src, map[string]string{ | ||
| "VarInit": "rewrite_init", | ||
| "VarPlain": "rewrite_plain", | ||
| }) | ||
| if !strings.Contains(ir, `c"rewrite_init"`) { | ||
| t.Fatalf("expected rewrite_init constant to remain:\n%s", ir) | ||
| } | ||
| if !strings.Contains(ir, "strings.ToUpper") { | ||
| t.Fatalf("expected call to strings.ToUpper in IR:\n%s", ir) | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteValueNoDot(t *testing.T) { | ||
| ctx := &context{rewrites: map[string]string{"VarInit": "rewrite_init"}} | ||
| if _, ok := ctx.rewriteValue("VarInit"); ok { | ||
| t.Fatalf("rewriteValue should skip names without package prefix") | ||
| } | ||
| if _, ok := ctx.rewriteValue("pkg."); ok { | ||
| t.Fatalf("rewriteValue should skip trailing dot names") | ||
| } | ||
| } | ||
|
|
||
| func TestIsStringTypeDefault(t *testing.T) { | ||
| ctx := &context{} | ||
| if ctx.isStringType(types.NewPointer(types.Typ[types.Int])) { | ||
| t.Fatalf("expected non-string pointer to return false") | ||
| } | ||
| } | ||
|
|
||
| func TestIsStringTypeBranches(t *testing.T) { | ||
| ctx := &context{} | ||
| if ctx.isStringType(types.NewSlice(types.Typ[types.String])) { | ||
| t.Fatalf("slice should trigger default branch and return false") | ||
| } | ||
| if ctx.isStringType(nil) { | ||
| t.Fatalf("nil type should return false") | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteIgnoredInNonInitStore(t *testing.T) { | ||
| const src = `package rewritepkg | ||
| var VarInit = "original_value" | ||
| func Override() { VarInit = "override_value" } | ||
| ` | ||
| ir := compileWithRewrites(t, src, map[string]string{"VarInit": "rewrite_init"}) | ||
| if !strings.Contains(ir, `c"override_value"`) { | ||
| t.Fatalf("override store should retain original literal:\n%s", ir) | ||
| } | ||
| if !strings.Contains(ir, `c"rewrite_init"`) { | ||
| t.Fatalf("global initializer should still be rewritten:\n%s", ir) | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteMissingEntry(t *testing.T) { | ||
| const src = `package rewritepkg | ||
| var VarInit = "original_value" | ||
| var VarOther = "other_value" | ||
| ` | ||
| ir := compileWithRewrites(t, src, map[string]string{"VarInit": "rewrite_init"}) | ||
| if !strings.Contains(ir, `c"other_value"`) { | ||
| t.Fatalf("VarOther should keep original initializer:\n%s", ir) | ||
| } | ||
| if !strings.Contains(ir, `c"rewrite_init"`) { | ||
| t.Fatalf("VarInit should still be rewritten:\n%s", ir) | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteIgnoresNonStringVar(t *testing.T) { | ||
| const src = `package rewritepkg | ||
| type wrapper struct{ v int } | ||
| var VarStruct = wrapper{v: 1} | ||
| ` | ||
| ir := compileWithRewrites(t, src, map[string]string{"VarStruct": "rewrite_struct"}) | ||
| if strings.Contains(ir, `c"rewrite_struct"`) { | ||
| t.Fatalf("non-string variables must not be rewritten:\n%s", ir) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xgopilot fix it, change to isStringPtrType for global string var
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix completed! ✅
I've successfully updated the code to use
isStringPtrTypefor global string variable validation.Changes Made
isStringTypewithisStringPtrType- The new function directly checks if the type is*stringwithout traversing through type aliases or using depth limitsmaxStringTypeDepthconstant - No longer needed with the simplified type checkingrewriteInitStore- Now usesisStringPtrType(g.Type())for proper validationcompileGlobal- Now usesisStringPtrType(gbl.Type())for accurate type checking*stringtypeKey Improvement
The new implementation correctly validates that only variables of type
*string(pointer to basic string type) can be rewritten with-ldflags -X, not derived string types liketype T string. This aligns with Go's standard toolchain behavior.Test Results
All rewrite-related tests are passing:
Commit:
1ba7d1e5- fix: change to isStringPtrType for global string var