mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: consistently use strlit to access constants string values
Passes toolstash-check. Change-Id: Ieaef20b7649787727b69469f93ffc942022bc079 Reviewed-on: https://go-review.googlesource.com/c/go/+/195198 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
142c002ee7
commit
75da700d0a
7 changed files with 19 additions and 19 deletions
|
|
@ -612,7 +612,7 @@ func evconst(n *Node) {
|
||||||
var strs []string
|
var strs []string
|
||||||
i2 := i1
|
i2 := i1
|
||||||
for i2 < len(s) && Isconst(s[i2], CTSTR) {
|
for i2 < len(s) && Isconst(s[i2], CTSTR) {
|
||||||
strs = append(strs, s[i2].Val().U.(string))
|
strs = append(strs, strlit(s[i2]))
|
||||||
i2++
|
i2++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -635,7 +635,7 @@ func evconst(n *Node) {
|
||||||
switch nl.Type.Etype {
|
switch nl.Type.Etype {
|
||||||
case TSTRING:
|
case TSTRING:
|
||||||
if Isconst(nl, CTSTR) {
|
if Isconst(nl, CTSTR) {
|
||||||
setintconst(n, int64(len(nl.Val().U.(string))))
|
setintconst(n, int64(len(strlit(nl))))
|
||||||
}
|
}
|
||||||
case TARRAY:
|
case TARRAY:
|
||||||
if !hascallchan(nl) {
|
if !hascallchan(nl) {
|
||||||
|
|
|
||||||
|
|
@ -762,7 +762,7 @@ func (p *noder) sum(x syntax.Expr) *Node {
|
||||||
n := p.expr(x)
|
n := p.expr(x)
|
||||||
if Isconst(n, CTSTR) && n.Sym == nil {
|
if Isconst(n, CTSTR) && n.Sym == nil {
|
||||||
nstr = n
|
nstr = n
|
||||||
chunks = append(chunks, nstr.Val().U.(string))
|
chunks = append(chunks, strlit(nstr))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := len(adds) - 1; i >= 0; i-- {
|
for i := len(adds) - 1; i >= 0; i-- {
|
||||||
|
|
@ -772,12 +772,12 @@ func (p *noder) sum(x syntax.Expr) *Node {
|
||||||
if Isconst(r, CTSTR) && r.Sym == nil {
|
if Isconst(r, CTSTR) && r.Sym == nil {
|
||||||
if nstr != nil {
|
if nstr != nil {
|
||||||
// Collapse r into nstr instead of adding to n.
|
// Collapse r into nstr instead of adding to n.
|
||||||
chunks = append(chunks, r.Val().U.(string))
|
chunks = append(chunks, strlit(r))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
nstr = r
|
nstr = r
|
||||||
chunks = append(chunks, nstr.Val().U.(string))
|
chunks = append(chunks, strlit(nstr))
|
||||||
} else {
|
} else {
|
||||||
if len(chunks) > 1 {
|
if len(chunks) > 1 {
|
||||||
nstr.SetVal(Val{U: strings.Join(chunks, "")})
|
nstr.SetVal(Val{U: strings.Join(chunks, "")})
|
||||||
|
|
|
||||||
|
|
@ -1017,7 +1017,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
|
||||||
haslit := false
|
haslit := false
|
||||||
for _, n1 := range n.List.Slice() {
|
for _, n1 := range n.List.Slice() {
|
||||||
hasbyte = hasbyte || n1.Op == OBYTES2STR
|
hasbyte = hasbyte || n1.Op == OBYTES2STR
|
||||||
haslit = haslit || n1.Op == OLITERAL && len(n1.Val().U.(string)) != 0
|
haslit = haslit || n1.Op == OLITERAL && len(strlit(n1)) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if haslit && hasbyte {
|
if haslit && hasbyte {
|
||||||
|
|
|
||||||
|
|
@ -211,7 +211,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
|
||||||
|
|
||||||
case OSTR2BYTES:
|
case OSTR2BYTES:
|
||||||
if l.Class() == PEXTERN && r.Left.Op == OLITERAL {
|
if l.Class() == PEXTERN && r.Left.Op == OLITERAL {
|
||||||
sval := r.Left.Val().U.(string)
|
sval := strlit(r.Left)
|
||||||
slicebytes(l, sval, len(sval))
|
slicebytes(l, sval, len(sval))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2336,7 +2336,7 @@ func (s *state) expr(n *Node) *ssa.Value {
|
||||||
// Replace "abc"[1] with 'b'.
|
// Replace "abc"[1] with 'b'.
|
||||||
// Delayed until now because "abc"[1] is not an ideal constant.
|
// Delayed until now because "abc"[1] is not an ideal constant.
|
||||||
// See test/fixedbugs/issue11370.go.
|
// See test/fixedbugs/issue11370.go.
|
||||||
return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(n.Left.Val().U.(string)[n.Right.Int64()])))
|
return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(strlit(n.Left)[n.Right.Int64()])))
|
||||||
}
|
}
|
||||||
a := s.expr(n.Left)
|
a := s.expr(n.Left)
|
||||||
i := s.expr(n.Right)
|
i := s.expr(n.Right)
|
||||||
|
|
|
||||||
|
|
@ -1043,8 +1043,8 @@ func typecheck1(n *Node, top int) (res *Node) {
|
||||||
yyerror("invalid %s index %v (index must be non-negative)", why, n.Right)
|
yyerror("invalid %s index %v (index must be non-negative)", why, n.Right)
|
||||||
} else if t.IsArray() && x >= t.NumElem() {
|
} else if t.IsArray() && x >= t.NumElem() {
|
||||||
yyerror("invalid array index %v (out of bounds for %d-element array)", n.Right, t.NumElem())
|
yyerror("invalid array index %v (out of bounds for %d-element array)", n.Right, t.NumElem())
|
||||||
} else if Isconst(n.Left, CTSTR) && x >= int64(len(n.Left.Val().U.(string))) {
|
} else if Isconst(n.Left, CTSTR) && x >= int64(len(strlit(n.Left))) {
|
||||||
yyerror("invalid string index %v (out of bounds for %d-byte string)", n.Right, len(n.Left.Val().U.(string)))
|
yyerror("invalid string index %v (out of bounds for %d-byte string)", n.Right, len(strlit(n.Left)))
|
||||||
} else if n.Right.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
|
} else if n.Right.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
|
||||||
yyerror("invalid %s index %v (index too large)", why, n.Right)
|
yyerror("invalid %s index %v (index too large)", why, n.Right)
|
||||||
}
|
}
|
||||||
|
|
@ -2148,8 +2148,8 @@ func checksliceindex(l *Node, r *Node, tp *types.Type) bool {
|
||||||
} else if tp != nil && tp.NumElem() >= 0 && r.Int64() > tp.NumElem() {
|
} else if tp != nil && tp.NumElem() >= 0 && r.Int64() > tp.NumElem() {
|
||||||
yyerror("invalid slice index %v (out of bounds for %d-element array)", r, tp.NumElem())
|
yyerror("invalid slice index %v (out of bounds for %d-element array)", r, tp.NumElem())
|
||||||
return false
|
return false
|
||||||
} else if Isconst(l, CTSTR) && r.Int64() > int64(len(l.Val().U.(string))) {
|
} else if Isconst(l, CTSTR) && r.Int64() > int64(len(strlit(l))) {
|
||||||
yyerror("invalid slice index %v (out of bounds for %d-byte string)", r, len(l.Val().U.(string)))
|
yyerror("invalid slice index %v (out of bounds for %d-byte string)", r, len(strlit(l)))
|
||||||
return false
|
return false
|
||||||
} else if r.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
|
} else if r.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
|
||||||
yyerror("invalid slice index %v (index too large)", r)
|
yyerror("invalid slice index %v (index too large)", r)
|
||||||
|
|
@ -3409,7 +3409,7 @@ func stringtoruneslit(n *Node) *Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
var l []*Node
|
var l []*Node
|
||||||
s := n.Left.Val().U.(string)
|
s := strlit(n.Left)
|
||||||
i := 0
|
i := 0
|
||||||
for _, r := range s {
|
for _, r := range s {
|
||||||
l = append(l, nod(OKEY, nodintconst(int64(i)), nodintconst(int64(r))))
|
l = append(l, nod(OKEY, nodintconst(int64(i)), nodintconst(int64(r))))
|
||||||
|
|
|
||||||
|
|
@ -1054,7 +1054,7 @@ opswitch:
|
||||||
yyerror("index out of bounds")
|
yyerror("index out of bounds")
|
||||||
}
|
}
|
||||||
} else if Isconst(n.Left, CTSTR) {
|
} else if Isconst(n.Left, CTSTR) {
|
||||||
n.SetBounded(bounded(r, int64(len(n.Left.Val().U.(string)))))
|
n.SetBounded(bounded(r, int64(len(strlit(n.Left)))))
|
||||||
if Debug['m'] != 0 && n.Bounded() && !Isconst(n.Right, CTINT) {
|
if Debug['m'] != 0 && n.Bounded() && !Isconst(n.Right, CTINT) {
|
||||||
Warn("index bounds check elided")
|
Warn("index bounds check elided")
|
||||||
}
|
}
|
||||||
|
|
@ -1389,7 +1389,7 @@ opswitch:
|
||||||
case OSTR2BYTES:
|
case OSTR2BYTES:
|
||||||
s := n.Left
|
s := n.Left
|
||||||
if Isconst(s, CTSTR) {
|
if Isconst(s, CTSTR) {
|
||||||
sc := s.Val().U.(string)
|
sc := strlit(s)
|
||||||
|
|
||||||
// Allocate a [n]byte of the right size.
|
// Allocate a [n]byte of the right size.
|
||||||
t := types.NewArray(types.Types[TUINT8], int64(len(sc)))
|
t := types.NewArray(types.Types[TUINT8], int64(len(sc)))
|
||||||
|
|
@ -1792,7 +1792,7 @@ func walkprint(nn *Node, init *Nodes) *Node {
|
||||||
for i := 0; i < len(s); {
|
for i := 0; i < len(s); {
|
||||||
var strs []string
|
var strs []string
|
||||||
for i < len(s) && Isconst(s[i], CTSTR) {
|
for i < len(s) && Isconst(s[i], CTSTR) {
|
||||||
strs = append(strs, s[i].Val().U.(string))
|
strs = append(strs, strlit(s[i]))
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
if len(strs) > 0 {
|
if len(strs) > 0 {
|
||||||
|
|
@ -1861,7 +1861,7 @@ func walkprint(nn *Node, init *Nodes) *Node {
|
||||||
case TSTRING:
|
case TSTRING:
|
||||||
cs := ""
|
cs := ""
|
||||||
if Isconst(n, CTSTR) {
|
if Isconst(n, CTSTR) {
|
||||||
cs = n.Val().U.(string)
|
cs = strlit(n)
|
||||||
}
|
}
|
||||||
switch cs {
|
switch cs {
|
||||||
case " ":
|
case " ":
|
||||||
|
|
@ -2510,7 +2510,7 @@ func addstr(n *Node, init *Nodes) *Node {
|
||||||
sz := int64(0)
|
sz := int64(0)
|
||||||
for _, n1 := range n.List.Slice() {
|
for _, n1 := range n.List.Slice() {
|
||||||
if n1.Op == OLITERAL {
|
if n1.Op == OLITERAL {
|
||||||
sz += int64(len(n1.Val().U.(string)))
|
sz += int64(len(strlit(n1)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3350,7 +3350,7 @@ func walkcompareString(n *Node, init *Nodes) *Node {
|
||||||
// Length-only checks are ok, though.
|
// Length-only checks are ok, though.
|
||||||
maxRewriteLen = 0
|
maxRewriteLen = 0
|
||||||
}
|
}
|
||||||
if s := cs.Val().U.(string); len(s) <= maxRewriteLen {
|
if s := strlit(cs); len(s) <= maxRewriteLen {
|
||||||
if len(s) > 0 {
|
if len(s) > 0 {
|
||||||
ncs = safeexpr(ncs, init)
|
ncs = safeexpr(ncs, init)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue