mirror of
https://github.com/golang/go.git
synced 2025-05-06 08:03:03 +00:00
go.tools.cmd/cover: define a block to end at the closing brace rather than last statement
Makes for prettier output in many cases. R=golang-dev, adg CC=golang-dev https://golang.org/cl/10360049
This commit is contained in:
parent
d338982a64
commit
33ae2b030f
@ -121,18 +121,18 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
|
|||||||
case *ast.CaseClause: // switch
|
case *ast.CaseClause: // switch
|
||||||
for _, n := range n.List {
|
for _, n := range n.List {
|
||||||
clause := n.(*ast.CaseClause)
|
clause := n.(*ast.CaseClause)
|
||||||
clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body)
|
clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body, false)
|
||||||
}
|
}
|
||||||
return f
|
return f
|
||||||
case *ast.CommClause: // select
|
case *ast.CommClause: // select
|
||||||
for _, n := range n.List {
|
for _, n := range n.List {
|
||||||
clause := n.(*ast.CommClause)
|
clause := n.(*ast.CommClause)
|
||||||
clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body)
|
clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body, false)
|
||||||
}
|
}
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
n.List = f.addCounters(n.Pos(), n.End(), n.List)
|
n.List = f.addCounters(n.Lbrace, n.Rbrace+1, n.List, true) // +1 to step past closing brace.
|
||||||
case *ast.SelectStmt:
|
case *ast.SelectStmt:
|
||||||
// Don't annotate an empty select - creates a syntax error.
|
// Don't annotate an empty select - creates a syntax error.
|
||||||
if n.Body == nil || len(n.Body.List) == 0 {
|
if n.Body == nil || len(n.Body.List) == 0 {
|
||||||
@ -151,7 +151,7 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
|
|||||||
func unquote(s string) string {
|
func unquote(s string) string {
|
||||||
t, err := strconv.Unquote(s)
|
t, err := strconv.Unquote(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("cover: improperly quoted string %q\n", s)
|
log.Fatalf("cover: improperly quoted string %q\n", s)
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
@ -325,11 +325,11 @@ func (f *File) newCounter(start, end token.Pos, numStmt int) ast.Stmt {
|
|||||||
// counters will be added before S1 and before S3. The block containing S2
|
// counters will be added before S1 and before S3. The block containing S2
|
||||||
// will be visited in a separate call.
|
// will be visited in a separate call.
|
||||||
// TODO: Nested simple blocks get unecessary (but correct) counters
|
// TODO: Nested simple blocks get unecessary (but correct) counters
|
||||||
func (f *File) addCounters(pos, end token.Pos, list []ast.Stmt) []ast.Stmt {
|
func (f *File) addCounters(pos, blockEnd token.Pos, list []ast.Stmt, extendToClosingBrace bool) []ast.Stmt {
|
||||||
// Special case: make sure we add a counter to an empty block. Can't do this below
|
// Special case: make sure we add a counter to an empty block. Can't do this below
|
||||||
// or we will add a counter to an empty statement list after, say, a return statement.
|
// or we will add a counter to an empty statement list after, say, a return statement.
|
||||||
if len(list) == 0 {
|
if len(list) == 0 {
|
||||||
return []ast.Stmt{f.newCounter(pos, end, 0)}
|
return []ast.Stmt{f.newCounter(pos, blockEnd, 0)}
|
||||||
}
|
}
|
||||||
// We have a block (statement list), but it may have several basic blocks due to the
|
// We have a block (statement list), but it may have several basic blocks due to the
|
||||||
// appearance of statements that affect the flow of control.
|
// appearance of statements that affect the flow of control.
|
||||||
@ -338,14 +338,18 @@ func (f *File) addCounters(pos, end token.Pos, list []ast.Stmt) []ast.Stmt {
|
|||||||
// Find first statement that affects flow of control (break, continue, if, etc.).
|
// Find first statement that affects flow of control (break, continue, if, etc.).
|
||||||
// It will be the last statement of this basic block.
|
// It will be the last statement of this basic block.
|
||||||
var last int
|
var last int
|
||||||
end = pos
|
end := blockEnd
|
||||||
for last = 0; last < len(list); last++ {
|
for last = 0; last < len(list); last++ {
|
||||||
end = f.statementBoundary(list[last])
|
end = f.statementBoundary(list[last])
|
||||||
if f.endsBasicSourceBlock(list[last]) {
|
if f.endsBasicSourceBlock(list[last]) {
|
||||||
|
extendToClosingBrace = false // Block is broken up now.
|
||||||
last++
|
last++
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if extendToClosingBrace {
|
||||||
|
end = blockEnd
|
||||||
|
}
|
||||||
if pos != end { // Can have no source to cover if e.g. blocks abut.
|
if pos != end { // Can have no source to cover if e.g. blocks abut.
|
||||||
newList = append(newList, f.newCounter(pos, end, last))
|
newList = append(newList, f.newCounter(pos, end, last))
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func htmlOutput(profile, outfile string) error {
|
|||||||
|
|
||||||
if outfile == "" {
|
if outfile == "" {
|
||||||
if !startBrowser("file://" + out.Name()) {
|
if !startBrowser("file://" + out.Name()) {
|
||||||
fmt.Fprintln(os.Stderr, "HTML output written to %v", out.Name())
|
fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user