go/loader supports cgo by processing CgoFiles with go tool cgo and parsing the resulting files, similar to how go build works. Unfortunately, go tool cgo does not preserve comments, stripping our directives.
One way around this is to parse the original file, extract the linter directives and transplant them to the generated file. While we do rely on CommentMaps and the association of comments to nodes, linter directives are still line-based, and we extract the line number as soon as possible. Combined with //line directives in the cgo generated file, we can transplant our directives.
It would be nice to do this in our own loader, but in the meantime we can reparse the files we know are part of CgoFiles.
go/loader supports cgo by processing CgoFiles with
go tool cgo
and parsing the resulting files, similar to howgo build
works. Unfortunately,go tool cgo
does not preserve comments, stripping our directives.One way around this is to parse the original file, extract the linter directives and transplant them to the generated file. While we do rely on CommentMaps and the association of comments to nodes, linter directives are still line-based, and we extract the line number as soon as possible. Combined with
//line
directives in the cgo generated file, we can transplant our directives.It would be nice to do this in our own loader, but in the meantime we can reparse the files we know are part of CgoFiles.