Skip to content

Commit

Permalink
Fix #139: don't fail govet on cgo
Browse files Browse the repository at this point in the history
  • Loading branch information
jirfag committed Jun 30, 2018
1 parent 6dd4f07 commit 14bc8dc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
31 changes: 31 additions & 0 deletions pkg/lint/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package lint
import (
"context"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"time"

"github.com/golangci/golangci-lint/pkg/goutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result/processors"

"github.com/golangci/go-tools/ssa"
"github.com/golangci/go-tools/ssa/ssautil"
Expand Down Expand Up @@ -258,6 +261,30 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
}
}

func removeFakePkgFiles(info *loader.PackageInfo, fset *token.FileSet) {
newFiles := make([]*ast.File, 0, len(info.Files))
for _, f := range info.Files {
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
newFiles = append(newFiles, f)
}
}
info.Files = newFiles
}

func removeFakePackages(prog *loader.Program) {
if prog.Created != nil {
for _, info := range prog.Created {
removeFakePkgFiles(info, prog.Fset)
}
}

if prog.Imported != nil {
for _, info := range prog.Imported {
removeFakePkgFiles(info, prog.Fset)
}
}
}

//nolint:gocyclo
func LoadContext(ctx context.Context, linters []linter.Config, cfg *config.Config,
log logutils.Log) (*linter.Context, error) {
Expand Down Expand Up @@ -298,6 +325,10 @@ func LoadContext(ctx context.Context, linters []linter.Config, cfg *config.Confi
ssaProg = buildSSAProgram(ctx, prog, log)
}

if prog != nil {
removeFakePackages(prog)
}

astLog := log.Child("astcache")
var astCache *astcache.Cache
if prog != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/result/processors/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package processors

import (
"fmt"
"strings"
"path/filepath"

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

func IsCgoFilename(f string) bool {
return f == "C" || strings.HasSuffix(f, "/C")
return filepath.Base(f) == "C"
}
16 changes: 16 additions & 0 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ func TestTestsAreLintedByDefault(t *testing.T) {
assert.Equal(t, exitcodes.Success, exitCode, out)
}

func TestCgoOk(t *testing.T) {
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "cgo"))
checkNoIssuesRun(t, out, exitCode)
}

func TestUnsafeOk(t *testing.T) {
out, exitCode := runGolangciLint(t, "--enable-all", filepath.Join(testdataDir, "unsafe"))
checkNoIssuesRun(t, out, exitCode)
}

func TestDeadcodeNoFalsePositivesInMainPkg(t *testing.T) {
out, exitCode := runGolangciLint(t, "--no-config", "--disable-all", "-Edeadcode",
filepath.Join(testdataDir, "deadcode_main_pkg"))
checkNoIssuesRun(t, out, exitCode)
}

func TestConfigFileIsDetected(t *testing.T) {
checkGotConfig := func(out string, exitCode int) {
assert.Equal(t, exitcodes.Success, exitCode, out)
Expand Down
19 changes: 19 additions & 0 deletions test/testdata/cgo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cgoexample

/*
#include <stdio.h>
#include <stdlib.h>
void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import "unsafe"

func Example() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}
14 changes: 14 additions & 0 deletions test/testdata/unsafe/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg

import (
"log"
"unsafe"
)

func F() {
x := 123 // of type int
p := unsafe.Pointer(&x)
pp := &p // of type *unsafe.Pointer
p = unsafe.Pointer(pp)
log.Print(p)
}

0 comments on commit 14bc8dc

Please sign in to comment.