Skip to content
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

fix: cgo support #157

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: stable
- uses: golangci/golangci-lint-action@v4
- uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2

Expand Down Expand Up @@ -41,6 +41,9 @@ jobs:
- name: Test code
run: go test -race -v ./...

- name: Run
run: go run ./cmd/wsl/ ./...

coverage:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ linters:
- paralleltest
- prealloc
- rowserrcheck
- tparallel
- testpackage
- varnamelen
- wastedassign
Expand All @@ -68,5 +67,4 @@ issues:
exclude-use-default: true
max-issues-per-linter: 0
max-same-issues: 0

# vim: set sw=2 ts=2 et:
14 changes: 12 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wsl
import (
"flag"
"go/ast"
"go/token"
"strings"

"golang.org/x/tools/go/analysis"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (wa *wslAnalyzer) run(pass *analysis.Pass) (interface{}, error) {
continue
}

filename := pass.Fset.PositionFor(file.Pos(), false).Filename
filename := getFilename(pass.Fset, file)
if !strings.HasSuffix(filename, ".go") {
continue
}
Expand Down Expand Up @@ -127,7 +128,7 @@ type multiStringValue struct {
// Set implements the flag.Value interface and will overwrite the pointer to the
// slice with a new pointer after splitting the flag by comma.
func (m *multiStringValue) Set(value string) error {
s := []string{}
var s []string

for _, v := range strings.Split(value, ",") {
s = append(s, strings.TrimSpace(v))
Expand All @@ -146,3 +147,12 @@ func (m *multiStringValue) String() string {

return strings.Join(*m.slicePtr, ", ")
}

func getFilename(fset *token.FileSet, file *ast.File) string {
filename := fset.PositionFor(file.Pos(), true).Filename
if !strings.HasSuffix(filename, ".go") {
return fset.PositionFor(file.Pos(), false).Filename
}

return filename
}
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module github.com/bombsimon/wsl/v4

go 1.21
go 1.22

require golang.org/x/tools v0.17.0

require (
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
require golang.org/x/mod v0.15.0 // indirect
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
42 changes: 42 additions & 0 deletions testdata/src/default_config/cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package testpkg

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import (
"unsafe"
)

func _() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}

func Advanced() {
var foo = 1
var bar = 2 // want "declarations should never be cuddled"
var biz int // want "declarations should never be cuddled"

x := []string{}
x = append(x, "literal")
notUsed := "just assigning, don't mind me"
x = append(x, "not z..") // want "append only allowed to cuddle with appended value"
useMe := "right away"
alsoNotUsed := ":("
x = append(x, "just noise") // want "append only allowed to cuddle with appended value"
x = append(x, useMe)

_ = foo
_ = bar
_ = biz
_ = notUsed
_ = alsoNotUsed
}
46 changes: 46 additions & 0 deletions testdata/src/default_config/cgo.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testpkg

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import (
"unsafe"
)

func _() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}

func Advanced() {
var foo = 1

var bar = 2 // want "declarations should never be cuddled"

var biz int // want "declarations should never be cuddled"

x := []string{}
x = append(x, "literal")
notUsed := "just assigning, don't mind me"

x = append(x, "not z..") // want "append only allowed to cuddle with appended value"
useMe := "right away"
alsoNotUsed := ":("

x = append(x, "just noise") // want "append only allowed to cuddle with appended value"
x = append(x, useMe)

_ = foo
_ = bar
_ = biz
_ = notUsed
_ = alsoNotUsed
}
41 changes: 41 additions & 0 deletions testdata/src/default_config/line_directive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//line line_directive.tmpl:1
package pkg

import (
"fmt"

"golang.org/x/tools/go/analysis"
)

func _() {
var x int
_ = x
x = 0 // x
}

func main() {
a()
b()
wsl()
}

func a() {
fmt.Println("foo")
}

func b() {
fmt.Println("foo")
}

func c() {
_ = analysis.Analyzer{}
}

// want +1 "block should not start with a whitespace"
func wsl() bool {

return true
}

func notFormatted() {
}
40 changes: 40 additions & 0 deletions testdata/src/default_config/line_directive.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//line line_directive.tmpl:1
package pkg

import (
"fmt"

"golang.org/x/tools/go/analysis"
)

func _() {
var x int
_ = x
x = 0 // x
}

func main() {
a()
b()
wsl()
}

func a() {
fmt.Println("foo")
}

func b() {
fmt.Println("foo")
}

func c() {
_ = analysis.Analyzer{}
}

// want +1 "block should not start with a whitespace"
func wsl() bool {
return true
}

func notFormatted() {
}
1 change: 1 addition & 0 deletions testdata/src/default_config/line_directive.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a template file for some code generator, and is not a valid go source file.
14 changes: 7 additions & 7 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (p *processor) parseBlockStatements(statements []ast.Stmt) {
return false
}

for j := 0; j < n; j++ {
for j := range n {
s1 := statements[i+j]
s2 := statements[i+j+1]

Expand Down Expand Up @@ -1113,8 +1113,8 @@ func (p *processor) findLeadingAndTrailingWhitespaces(ident *ast.Ident, stmt, ne
return
}

blockStartLine = p.fileSet.PositionFor(blockStartPos, false).Line
blockEndLine = p.fileSet.PositionFor(blockEndPos, false).Line
blockStartLine = p.fileSet.Position(blockStartPos).Line
blockEndLine = p.fileSet.Position(blockEndPos).Line

// No whitespace possible if LBrace and RBrace is on the same line.
if blockStartLine == blockEndLine {
Expand Down Expand Up @@ -1362,14 +1362,14 @@ func isExampleFunc(ident *ast.Ident) bool {
}

func (p *processor) nodeStart(node ast.Node) int {
return p.fileSet.PositionFor(node.Pos(), false).Line
return p.fileSet.Position(node.Pos()).Line
}

func (p *processor) nodeEnd(node ast.Node) int {
line := p.fileSet.PositionFor(node.End(), false).Line
line := p.fileSet.Position(node.End()).Line

if isEmptyLabeledStmt(node) {
return p.fileSet.PositionFor(node.Pos(), false).Line
return p.fileSet.Position(node.Pos()).Line
}

return line
Expand Down Expand Up @@ -1408,7 +1408,7 @@ func (p *processor) addErrorRange(reportAt, start, end token.Pos, reason string)
}

func (p *processor) addWarning(w string, pos token.Pos, t interface{}) {
position := p.fileSet.PositionFor(pos, false)
position := p.fileSet.Position(pos)

p.warnings = append(p.warnings,
fmt.Sprintf("%s:%d: %s (%T)", position.Filename, position.Line, w, t),
Expand Down
2 changes: 2 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func TestWIP(t *testing.T) {
}

func TestDefaultConfig(t *testing.T) {
t.Parallel()

testdata := analysistest.TestData()

testCases := []struct {
Expand Down
Loading