Skip to content

Commit 14bc8dc

Browse files
committed
Fix #139: don't fail govet on cgo
1 parent 6dd4f07 commit 14bc8dc

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

Diff for: pkg/lint/load.go

+31
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package lint
33
import (
44
"context"
55
"fmt"
6+
"go/ast"
67
"go/build"
78
"go/parser"
9+
"go/token"
810
"os"
911
"path/filepath"
1012
"strings"
1113
"time"
1214

1315
"github.com/golangci/golangci-lint/pkg/goutils"
1416
"github.com/golangci/golangci-lint/pkg/logutils"
17+
"github.com/golangci/golangci-lint/pkg/result/processors"
1518

1619
"github.com/golangci/go-tools/ssa"
1720
"github.com/golangci/go-tools/ssa/ssautil"
@@ -258,6 +261,30 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
258261
}
259262
}
260263

264+
func removeFakePkgFiles(info *loader.PackageInfo, fset *token.FileSet) {
265+
newFiles := make([]*ast.File, 0, len(info.Files))
266+
for _, f := range info.Files {
267+
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
268+
newFiles = append(newFiles, f)
269+
}
270+
}
271+
info.Files = newFiles
272+
}
273+
274+
func removeFakePackages(prog *loader.Program) {
275+
if prog.Created != nil {
276+
for _, info := range prog.Created {
277+
removeFakePkgFiles(info, prog.Fset)
278+
}
279+
}
280+
281+
if prog.Imported != nil {
282+
for _, info := range prog.Imported {
283+
removeFakePkgFiles(info, prog.Fset)
284+
}
285+
}
286+
}
287+
261288
//nolint:gocyclo
262289
func LoadContext(ctx context.Context, linters []linter.Config, cfg *config.Config,
263290
log logutils.Log) (*linter.Context, error) {
@@ -298,6 +325,10 @@ func LoadContext(ctx context.Context, linters []linter.Config, cfg *config.Confi
298325
ssaProg = buildSSAProgram(ctx, prog, log)
299326
}
300327

328+
if prog != nil {
329+
removeFakePackages(prog)
330+
}
331+
301332
astLog := log.Child("astcache")
302333
var astCache *astcache.Cache
303334
if prog != nil {

Diff for: pkg/result/processors/utils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package processors
22

33
import (
44
"fmt"
5-
"strings"
5+
"path/filepath"
66

77
"github.com/golangci/golangci-lint/pkg/result"
88
)
@@ -47,5 +47,5 @@ func transformIssues(issues []result.Issue, transform func(i *result.Issue) *res
4747
}
4848

4949
func IsCgoFilename(f string) bool {
50-
return f == "C" || strings.HasSuffix(f, "/C")
50+
return filepath.Base(f) == "C"
5151
}

Diff for: test/run_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ func TestTestsAreLintedByDefault(t *testing.T) {
131131
assert.Equal(t, exitcodes.Success, exitCode, out)
132132
}
133133

134+
func TestCgoOk(t *testing.T) {
135+
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "cgo"))
136+
checkNoIssuesRun(t, out, exitCode)
137+
}
138+
139+
func TestUnsafeOk(t *testing.T) {
140+
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "unsafe"))
141+
checkNoIssuesRun(t, out, exitCode)
142+
}
143+
144+
func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
145+
out, exitCode := runGolangciLint(t, "--no-config", "--disable-all", "-Edeadcode",
146+
filepath.Join(testdataDir, "deadcode_main_pkg"))
147+
checkNoIssuesRun(t, out, exitCode)
148+
}
149+
134150
func TestConfigFileIsDetected(t *testing.T) {
135151
checkGotConfig := func(out string, exitCode int) {
136152
assert.Equal(t, exitcodes.Success, exitCode, out)

Diff for: test/testdata/cgo/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cgoexample
2+
3+
/*
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
void myprint(char* s) {
8+
printf("%d\n", s);
9+
}
10+
*/
11+
import "C"
12+
13+
import "unsafe"
14+
15+
func Example() {
16+
cs := C.CString("Hello from stdio\n")
17+
C.myprint(cs)
18+
C.free(unsafe.Pointer(cs))
19+
}

Diff for: test/testdata/unsafe/pkg.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pkg
2+
3+
import (
4+
"log"
5+
"unsafe"
6+
)
7+
8+
func F() {
9+
x := 123 // of type int
10+
p := unsafe.Pointer(&x)
11+
pp := &p // of type *unsafe.Pointer
12+
p = unsafe.Pointer(pp)
13+
log.Print(p)
14+
}

0 commit comments

Comments
 (0)