mirror of
https://github.com/golang/go
synced 2024-11-05 17:06:13 -07:00
go.tools/oracle: change -pos flag syntax from "file pos-pos" to file:pos-pos.
Pro: no shell quotation needed. Con: can't be parsed by (the perpetually useless) Scanf. R=crawshaw, dgryski CC=golang-dev https://golang.org/cl/13441043
This commit is contained in:
parent
a483497cf1
commit
0126405cad
@ -27,11 +27,9 @@ import (
|
|||||||
"code.google.com/p/go.tools/oracle"
|
"code.google.com/p/go.tools/oracle"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO(adonovan): use a format that permits spaces in filenames, and
|
|
||||||
// doesn't require shell quoting.
|
|
||||||
var posFlag = flag.String("pos", "",
|
var posFlag = flag.String("pos", "",
|
||||||
"Filename and offset or extent of a syntax element about which to query, "+
|
"Filename and offset or extent of a syntax element about which to query, "+
|
||||||
"e.g. 'foo.go 123-456', 'bar.go 123'.")
|
"e.g. foo.go:123-456, bar.go:123.")
|
||||||
|
|
||||||
var modeFlag = flag.String("mode", "",
|
var modeFlag = flag.String("mode", "",
|
||||||
"Mode of query to perform: callers, callees, callstack, callgraph, describe.")
|
"Mode of query to perform: callers, callees, callstack, callgraph, describe.")
|
||||||
|
@ -75,11 +75,14 @@ result."
|
|||||||
(if (string-equal "" go-oracle-scope)
|
(if (string-equal "" go-oracle-scope)
|
||||||
(go-oracle-set-scope))
|
(go-oracle-set-scope))
|
||||||
(let* ((filename (file-truename buffer-file-name))
|
(let* ((filename (file-truename buffer-file-name))
|
||||||
(pos (if (use-region-p)
|
(posflag (if (use-region-p)
|
||||||
(format "%s-%s"
|
(format "-pos=%s:%s-%s"
|
||||||
(1- (go--position-bytes (region-beginning)))
|
filename
|
||||||
(1- (go--position-bytes (region-end))))
|
(1- (go--position-bytes (region-beginning)))
|
||||||
(format "%s" (1- (position-bytes (point))))))
|
(1- (go--position-bytes (region-end))))
|
||||||
|
(format "-pos=%s:%s"
|
||||||
|
filename
|
||||||
|
(1- (position-bytes (point))))))
|
||||||
;; This would be simpler if we could just run 'go tool oracle'.
|
;; This would be simpler if we could just run 'go tool oracle'.
|
||||||
(env-vars (go-root-and-paths))
|
(env-vars (go-root-and-paths))
|
||||||
(goroot-env (concat "GOROOT=" (car env-vars)))
|
(goroot-env (concat "GOROOT=" (car env-vars)))
|
||||||
@ -89,7 +92,7 @@ result."
|
|||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(insert "Go Oracle\n")
|
(insert "Go Oracle\n")
|
||||||
(let ((args (append (list go-oracle-command nil t nil
|
(let ((args (append (list go-oracle-command nil t nil
|
||||||
(format "-pos=%s %s" filename pos)
|
posflag
|
||||||
(format "-mode=%s" mode))
|
(format "-mode=%s" mode))
|
||||||
(split-string go-oracle-scope " " t))))
|
(split-string go-oracle-scope " " t))))
|
||||||
;; Log the command to *Messages*, for debugging.
|
;; Log the command to *Messages*, for debugging.
|
||||||
|
@ -29,7 +29,7 @@ set errorformat+=%f:%l.%c-%*[0-9].%*[0-9]:\ %m
|
|||||||
func! s:RunOracle(mode) abort
|
func! s:RunOracle(mode) abort
|
||||||
let s:pos = line2byte(line("."))+col(".")
|
let s:pos = line2byte(line("."))+col(".")
|
||||||
let s:errfile = tempname()
|
let s:errfile = tempname()
|
||||||
let s:cmd = printf("!%s -mode=%s '-pos=%s %d' %s >%s",
|
let s:cmd = printf("!%s -mode=%s -pos=%s:%d %s >%s",
|
||||||
\ s:go_oracle, a:mode, bufname(""), s:pos, s:scope, s:errfile)
|
\ s:go_oracle, a:mode, bufname(""), s:pos, s:scope, s:errfile)
|
||||||
execute s:cmd
|
execute s:cmd
|
||||||
execute "cfile " . s:errfile
|
execute "cfile " . s:errfile
|
||||||
|
@ -21,6 +21,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.google.com/p/go.tools/go/types"
|
"code.google.com/p/go.tools/go/types"
|
||||||
@ -222,25 +224,43 @@ func ptrAnalysis(o *oracle) pointer.CallGraphNode {
|
|||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseQueryPos parses a string of the form "file pos" or file
|
func parseDecimal(s string) int {
|
||||||
// start-end" where pos, start, end are decimal integers, and returns
|
if s, err := strconv.ParseInt(s, 10, 32); err == nil {
|
||||||
// the extent to which it refers.
|
return int(s)
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseQueryPos parses a string of the form "file:pos" or
|
||||||
|
// file:start-end" where pos, start, end are decimal integers, and
|
||||||
|
// returns the extent to which it refers.
|
||||||
//
|
//
|
||||||
func parseQueryPos(fset *token.FileSet, queryPos string) (start, end token.Pos, err error) {
|
func parseQueryPos(fset *token.FileSet, queryPos string) (start, end token.Pos, err error) {
|
||||||
if queryPos == "" {
|
if queryPos == "" {
|
||||||
err = fmt.Errorf("no source position specified (-pos flag)")
|
err = fmt.Errorf("no source position specified (-pos flag)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var filename string
|
|
||||||
var startOffset, endOffset int
|
colon := strings.LastIndex(queryPos, ":")
|
||||||
n, err := fmt.Sscanf(queryPos, "%s %d-%d", &filename, &startOffset, &endOffset)
|
if colon < 0 {
|
||||||
if n != 3 {
|
err = fmt.Errorf("invalid source position -pos=%q", queryPos)
|
||||||
n, err = fmt.Sscanf(queryPos, "%s %d", &filename, &startOffset)
|
return
|
||||||
if n != 2 {
|
}
|
||||||
err = fmt.Errorf("invalid source position -pos=%q", queryPos)
|
filename, offset := queryPos[:colon], queryPos[colon+1:]
|
||||||
return
|
startOffset := -1
|
||||||
}
|
endOffset := -1
|
||||||
|
if hyphen := strings.Index(offset, "-"); hyphen < 0 {
|
||||||
|
// e.g. "foo.go:123"
|
||||||
|
startOffset = parseDecimal(offset)
|
||||||
endOffset = startOffset
|
endOffset = startOffset
|
||||||
|
} else {
|
||||||
|
// e.g. "foo.go:123-456"
|
||||||
|
startOffset = parseDecimal(offset[:hyphen])
|
||||||
|
endOffset = parseDecimal(offset[hyphen+1:])
|
||||||
|
}
|
||||||
|
if startOffset < 0 || endOffset < 0 {
|
||||||
|
err = fmt.Errorf("invalid -pos offset %q", offset)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var file *token.File
|
var file *token.File
|
||||||
|
@ -167,7 +167,7 @@ func doQuery(out io.Writer, q *query) {
|
|||||||
buildContext.GOPATH = "testdata"
|
buildContext.GOPATH = "testdata"
|
||||||
err := oracle.Main([]string{q.filename},
|
err := oracle.Main([]string{q.filename},
|
||||||
q.verb,
|
q.verb,
|
||||||
fmt.Sprintf("%s %d-%d", q.filename, q.start, q.end),
|
fmt.Sprintf("%s:%d-%d", q.filename, q.start, q.end),
|
||||||
/*PTA-log=*/ nil, capture, &buildContext)
|
/*PTA-log=*/ nil, capture, &buildContext)
|
||||||
|
|
||||||
for _, line := range strings.Split(capture.String(), "\n") {
|
for _, line := range strings.Split(capture.String(), "\n") {
|
||||||
|
Loading…
Reference in New Issue
Block a user