cmd/internal/obj: fix Link.Diag printf errors

go1.26's vet printf checker can associate the printf-wrapper
property with local vars and struct fields if they are assigned
from a printf-like func literal (CL 706635). This leads to better
detection of mistakes.

Change-Id: I604be1e200aa1aba75e09d4f36ab68c1dba3b8a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/710195
Auto-Submit: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Alan Donovan 2025-10-08 12:19:14 -04:00
parent e1ca1de123
commit d4830c6130
7 changed files with 15 additions and 8 deletions

View file

@ -579,7 +579,7 @@ func span5(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
} }
if int64(pc) > p.Pc { if int64(pc) > p.Pc {
ctxt.Diag("PC padding invalid: want %#d, has %#d: %v", p.Pc, pc, p) ctxt.Diag("PC padding invalid: want %d, has %d: %v", p.Pc, pc, p)
} }
for int64(pc) != p.Pc { for int64(pc) != p.Pc {
// emit 0xe1a00000 (MOVW R0, R0) // emit 0xe1a00000 (MOVW R0, R0)

View file

@ -4354,7 +4354,7 @@ func (c *ctxt7) asmout(p *obj.Prog, out []uint32) (count int) {
// remove the NOTUSETMP flag in optab. // remove the NOTUSETMP flag in optab.
op := c.opirr(p, p.As) op := c.opirr(p, p.As)
if op&Sbit != 0 { if op&Sbit != 0 {
c.ctxt.Diag("can not break addition/subtraction when S bit is set", p) c.ctxt.Diag("can not break addition/subtraction when S bit is set (%v)", p)
} }
rt, r := p.To.Reg, p.Reg rt, r := p.To.Reg, p.Reg
if r == obj.REG_NONE { if r == obj.REG_NONE {

View file

@ -1216,6 +1216,13 @@ type Link struct {
Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch
} }
// Assert to vet's printf checker that Link.DiagFunc is a printf-like.
func _(ctxt *Link) {
ctxt.DiagFunc = func(format string, args ...any) {
_ = fmt.Sprintf(format, args...)
}
}
func (ctxt *Link) Diag(format string, args ...interface{}) { func (ctxt *Link) Diag(format string, args ...interface{}) {
ctxt.Errors++ ctxt.Errors++
ctxt.DiagFunc(format, args...) ctxt.DiagFunc(format, args...)

View file

@ -2057,7 +2057,7 @@ func (c *ctxt0) asmout(p *obj.Prog, o *Optab, out []uint32) {
switch o.type_ { switch o.type_ {
default: default:
c.ctxt.Diag("unknown type %d %v", o.type_) c.ctxt.Diag("unknown type %d", o.type_)
prasm(p) prasm(p)
case 0: // pseudo ops case 0: // pseudo ops
@ -4438,7 +4438,7 @@ func (c *ctxt0) specialFpMovInst(a obj.As, fclass int, tclass int) uint32 {
} }
} }
c.ctxt.Diag("bad class combination: %s %s,%s\n", a, fclass, tclass) c.ctxt.Diag("bad class combination: %s %d,%s\n", a, fclass, tclass)
return 0 return 0
} }

View file

@ -1172,7 +1172,7 @@ func (c *ctxt0) asmout(p *obj.Prog, o *Optab, out []uint32) {
} }
switch o.type_ { switch o.type_ {
default: default:
c.ctxt.Diag("unknown type %d %v", o.type_) c.ctxt.Diag("unknown type %d", o.type_)
prasm(p) prasm(p)
case 0: /* pseudo ops */ case 0: /* pseudo ops */

View file

@ -63,12 +63,12 @@ func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc) {
switch p.To.Sym.Name { switch p.To.Sym.Name {
case "go_args_stackmap": case "go_args_stackmap":
if p.From.Type != TYPE_CONST || p.From.Offset != abi.FUNCDATA_ArgsPointerMaps { if p.From.Type != TYPE_CONST || p.From.Offset != abi.FUNCDATA_ArgsPointerMaps {
ctxt.Diag("%s: FUNCDATA use of go_args_stackmap(SB) without FUNCDATA_ArgsPointerMaps", p.Pos) ctxt.Diag("%v: FUNCDATA use of go_args_stackmap(SB) without FUNCDATA_ArgsPointerMaps", p)
} }
p.To.Sym = ctxt.LookupDerived(curtext, curtext.Name+".args_stackmap") p.To.Sym = ctxt.LookupDerived(curtext, curtext.Name+".args_stackmap")
case "no_pointers_stackmap": case "no_pointers_stackmap":
if p.From.Type != TYPE_CONST || p.From.Offset != abi.FUNCDATA_LocalsPointerMaps { if p.From.Type != TYPE_CONST || p.From.Offset != abi.FUNCDATA_LocalsPointerMaps {
ctxt.Diag("%s: FUNCDATA use of no_pointers_stackmap(SB) without FUNCDATA_LocalsPointerMaps", p.Pos) ctxt.Diag("%v: FUNCDATA use of no_pointers_stackmap(SB) without FUNCDATA_LocalsPointerMaps", p)
} }
// funcdata for functions with no local variables in frame. // funcdata for functions with no local variables in frame.
// Define two zero-length bitmaps, because the same index is used // Define two zero-length bitmaps, because the same index is used

View file

@ -3026,7 +3026,7 @@ func instructionsForOpImmediate(p *obj.Prog, as obj.As, rs int16) []*instruction
low, high, err := Split32BitImmediate(ins.imm) low, high, err := Split32BitImmediate(ins.imm)
if err != nil { if err != nil {
p.Ctxt.Diag("%v: constant %d too large", p, ins.imm, err) p.Ctxt.Diag("%v: constant %d too large: %v", p, ins.imm, err)
return nil return nil
} }
if high == 0 { if high == 0 {