1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:14:39 -07:00

internal/lsp: use TypecheckCgo when possible

When on 1.15+, enable TypecheckCgo. This improves cgo support
significantly, but we'll still have trouble with newly-referenced C
identifiers and changes to the magic comment.

Updates golang/go#35721.

Change-Id: I44dc95ce2d91d552e1e66e3722dc4230ab59fedd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229779
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-04-22 13:45:24 -04:00
parent 866d71a317
commit 0951661448
8 changed files with 67 additions and 14 deletions

View File

@ -9,8 +9,10 @@ import (
"fmt"
"go/ast"
"go/token"
"go/types"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
@ -118,6 +120,10 @@ func (s *snapshot) Config(ctx context.Context) *packages.Config {
},
Tests: true,
}
// We want to type check cgo code if go/types supports it.
if reflect.ValueOf(&types.Config{}).Elem().FieldByName("UsesCgo").IsValid() {
cfg.Mode |= packages.TypecheckCgo
}
packagesinternal.SetGoCmdRunner(cfg, s.view.gocmdRunner)
return cfg

View File

@ -10,9 +10,10 @@ void myprint(char* s) {
*/
import "C"
import "fmt"
import "unsafe"
import (
"fmt"
"unsafe"
)
func Example() { //@mark(funccgoexample, "Example"),item(funccgoexample, "Example", "func()", "func")
fmt.Println()
@ -22,5 +23,5 @@ func Example() { //@mark(funccgoexample, "Example"),item(funccgoexample, "Exampl
}
func _() {
Example()
Example() //@godef("ample", funccgoexample),complete("ample", funccgoexample)
}

View File

@ -0,0 +1,30 @@
-- funccgoexample-definition --
cgo/declarecgo.go:18:6-13: defined here as ```go
func Example()
```
[`cgo.Example` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/cgo#Example)
-- funccgoexample-definition-json --
{
"span": {
"uri": "file://cgo/declarecgo.go",
"start": {
"line": 18,
"column": 6,
"offset": 151
},
"end": {
"line": 18,
"column": 13,
"offset": 158
}
},
"description": "```go\nfunc Example()\n```\n\n[`cgo.Example` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/cgo#Example)"
}
-- funccgoexample-hover --
[`cgo.Example` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/cgo#Example)
```go
func Example()
```

View File

@ -3,4 +3,4 @@
package cgo
// Set a dummy marker to keep the test framework happy. The tests should be skipped.
var _ = "Example" //@mark(funccgoexample, "Example")
var _ = "Example" //@mark(funccgoexample, "Example"),godef("ample", funccgoexample),complete("ample", funccgoexample)

View File

@ -1,5 +1,5 @@
-- funccgoexample-definition --
cgo/declarecgo.go:17:6-13: defined here as ```go
cgo/declarecgo.go:18:6-13: defined here as ```go
func cgo.Example()
```
@ -9,14 +9,14 @@ func cgo.Example()
"span": {
"uri": "file://cgo/declarecgo.go",
"start": {
"line": 17,
"line": 18,
"column": 6,
"offset": 153
"offset": 151
},
"end": {
"line": 17,
"line": 18,
"column": 13,
"offset": 160
"offset": 158
}
},
"description": "```go\nfunc cgo.Example()\n```\n\n[`cgo.Example` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/cgo#Example)"

View File

@ -1,6 +1,6 @@
-- summary --
CodeLensCount = 4
CompletionsCount = 240
CompletionsCount = 241
CompletionSnippetCount = 76
UnimportedCompletionsCount = 6
DeepCompletionsCount = 5
@ -12,7 +12,7 @@ FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
SuggestedFixCount = 6
DefinitionsCount = 50
DefinitionsCount = 51
TypeDefinitionsCount = 2
HighlightsCount = 52
ReferencesCount = 11

View File

@ -230,7 +230,10 @@ func DefaultOptions() source.Options {
return o
}
var haveCgo = false
var (
haveCgo = false
go115 = false
)
// Load creates the folder structure required when testing with modules.
// The directory structure of a test needs to look like the example below:
@ -453,6 +456,9 @@ func Run(t *testing.T, tests Tests, data *Data) {
if (!haveCgo || runtime.GOOS == "android") && strings.Contains(t.Name(), "cgo") {
t.Skip("test requires cgo, not supported")
}
if !go115 && strings.Contains(t.Name(), "declarecgo") {
t.Skip("test requires Go 1.15")
}
test(t, src, e, data.CompletionItems)
})
}
@ -611,6 +617,9 @@ func Run(t *testing.T, tests Tests, data *Data) {
if (!haveCgo || runtime.GOOS == "android") && strings.Contains(t.Name(), "cgo") {
t.Skip("test requires cgo, not supported")
}
if !go115 && strings.Contains(t.Name(), "declarecgo") {
t.Skip("test requires Go 1.15")
}
tests.Definition(t, spn, d)
})
}
@ -809,7 +818,7 @@ func checkData(t *testing.T, data *Data) {
}))
got := buf.String()
if want != got {
t.Errorf("test summary does not match, want\n%s\ngot:\n%s", want, got)
t.Errorf("test summary does not match: %v", Diff(want, got))
}
}

View File

@ -0,0 +1,7 @@
// +build go1.15
package tests
func init() {
go115 = true
}