-
Notifications
You must be signed in to change notification settings - Fork 1k
tools: add alloc2cover tool to convert heap allocation report to same… #5129
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
Open
deadprogram
wants to merge
1
commit into
dev
Choose a base branch
from
print-allocs-out
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,146 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| func main() { | ||
| if len(os.Args) < 3 { | ||
| fmt.Fprintln(os.Stderr, "Usage: alloc2cover <input-file> <source-root> [module-path]") | ||
| fmt.Fprintln(os.Stderr, " Reads TinyGo -print-allocs output and converts to cover.out format") | ||
| fmt.Fprintln(os.Stderr, " source-root: path to the root of the source files") | ||
| fmt.Fprintln(os.Stderr, " module-path: optional Go module path prefix (e.g., github.com/user/repo)") | ||
| fmt.Fprintln(os.Stderr, " Heap allocations are marked as uncovered (red in coverage reports)") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| inputFile := os.Args[1] | ||
| sourceRoot := os.Args[2] | ||
| modulePath := "" | ||
| if len(os.Args) > 3 { | ||
| modulePath = os.Args[3] | ||
| } | ||
|
|
||
| file, err := os.Open(inputFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| // Pattern to match allocation lines | ||
| // /path/to/file.go:line:column: object allocated on the heap: reason | ||
| pattern := regexp.MustCompile(`^(.+\.go):(\d+):\d+: object allocated on the heap:`) | ||
|
|
||
| type allocation struct { | ||
| absPath string | ||
| modPath string | ||
| line int | ||
| } | ||
|
|
||
| allocations := make(map[string]allocation) | ||
| fileLineLengths := make(map[string][]int) // cache of line lengths per file | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| matches := pattern.FindStringSubmatch(line) | ||
| if matches == nil { | ||
| continue | ||
| } | ||
|
|
||
| absPath := matches[1] | ||
| lineNum, _ := strconv.Atoi(matches[2]) | ||
|
|
||
| // Convert absolute path to module path if provided | ||
| modPath := absPath | ||
| if modulePath != "" { | ||
| idx := strings.Index(absPath, modulePath) | ||
| if idx >= 0 { | ||
| modPath = absPath[idx:] | ||
| } | ||
| } | ||
|
|
||
| alloc := allocation{ | ||
| absPath: absPath, | ||
| modPath: modPath, | ||
| line: lineNum, | ||
| } | ||
|
|
||
| key := fmt.Sprintf("%s:%d", modPath, lineNum) | ||
| allocations[key] = alloc | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Helper function to get line lengths for a file | ||
| getLineLengths := func(absPath, modPath string) []int { | ||
| if lengths, ok := fileLineLengths[absPath]; ok { | ||
| return lengths | ||
| } | ||
|
|
||
| // Try to find the file | ||
| var filePath string | ||
| if _, err := os.Stat(absPath); err == nil { | ||
| filePath = absPath | ||
| } else if sourceRoot != "" { | ||
| // Try relative to source root using module path | ||
| filePath = filepath.Join(sourceRoot, modPath) | ||
| } | ||
|
|
||
| if filePath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| f, err := os.Open(filePath) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var lengths []int | ||
| scanner := bufio.NewScanner(f) | ||
| for scanner.Scan() { | ||
| lengths = append(lengths, len(scanner.Text())) | ||
| } | ||
| fileLineLengths[absPath] = lengths | ||
| return lengths | ||
| } | ||
|
|
||
| // Output in cover.out format | ||
| fmt.Println("mode: set") | ||
|
|
||
| // Sort keys for consistent output | ||
| keys := make([]string, 0, len(allocations)) | ||
| for k := range allocations { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
|
|
||
| for _, key := range keys { | ||
| a := allocations[key] | ||
|
|
||
| endCol := 1 // default if we can't read the file | ||
|
|
||
| // Try to get actual line length for end column | ||
| lengths := getLineLengths(a.absPath, a.modPath) | ||
| if lengths != nil && a.line <= len(lengths) { | ||
| endCol = lengths[a.line-1] + 1 // +1 for end of line | ||
| } | ||
|
|
||
| // Format: file:line.column,line.column statements count | ||
| // start at column 1, end at actual line length | ||
| // count = 0 marks as uncovered (red in coverage reports) | ||
| fmt.Printf("%s:%d.1,%d.%d 1 0\n", a.modPath, a.line, a.line, endCol) | ||
| } | ||
| } | ||
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.
can we do this using Go's standard library flag package for automatic documentation generation and straightforward way of adding more options in the future?
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.
and checking inputs: