Commit bb855f98 authored by Rob Pike's avatar Rob Pike

govet: make name-matching for printf etc. case-insensitive.

Update goyacc, cgo to be more canonical in their naming and silence the new warnings.

R=rsc, gri
CC=golang-dev
https://golang.org/cl/4417042
parent 4c60569e
......@@ -30,7 +30,7 @@ func parse(name string, flags uint) *ast.File {
}
os.Exit(2)
}
fatal("parsing %s: %s", name, err)
fatalf("parsing %s: %s", name, err)
}
return ast1
}
......
......@@ -79,7 +79,7 @@ NextLine:
l = strings.TrimSpace(l[4:])
fields := strings.Split(l, ":", 2)
if len(fields) != 2 {
fatal("%s: bad #cgo line: %s", srcfile, line)
fatalf("%s: bad #cgo line: %s", srcfile, line)
}
var k string
......@@ -97,17 +97,17 @@ NextLine:
continue NextLine
}
default:
fatal("%s: bad #cgo option: %s", srcfile, fields[0])
fatalf("%s: bad #cgo option: %s", srcfile, fields[0])
}
if k != "CFLAGS" && k != "LDFLAGS" {
fatal("%s: unsupported #cgo option %s", srcfile, k)
fatalf("%s: unsupported #cgo option %s", srcfile, k)
}
v := strings.TrimSpace(fields[1])
args, err := splitQuoted(v)
if err != nil {
fatal("%s: bad #cgo option %s: %s", srcfile, k, err.String())
fatalf("%s: bad #cgo option %s: %s", srcfile, k, err.String())
}
if oldv, ok := p.CgoFlags[k]; ok {
p.CgoFlags[k] = oldv + " " + v
......@@ -317,7 +317,7 @@ func (p *Package) guessKinds(f *File) []*Name {
b.WriteString("}\n")
stderr := p.gccErrors(b.Bytes())
if stderr == "" {
fatal("gcc produced no output\non input:\n%s", b.Bytes())
fatalf("gcc produced no output\non input:\n%s", b.Bytes())
}
names := make([]*Name, len(toSniff))
......@@ -383,7 +383,7 @@ func (p *Package) guessKinds(f *File) []*Name {
error(token.NoPos, "could not determine kind of name for C.%s", n.Go)
}
if nerrors > 0 {
fatal("unresolved names")
fatalf("unresolved names")
}
return needType
}
......@@ -422,7 +422,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
for {
e, err := r.Next()
if err != nil {
fatal("reading DWARF entry: %s", err)
fatalf("reading DWARF entry: %s", err)
}
if e == nil {
break
......@@ -433,7 +433,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
for {
e, err := r.Next()
if err != nil {
fatal("reading DWARF entry: %s", err)
fatalf("reading DWARF entry: %s", err)
}
if e.Tag == 0 {
break
......@@ -452,27 +452,27 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
name, _ := e.Val(dwarf.AttrName).(string)
typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset)
if name == "" || typOff == 0 {
fatal("malformed DWARF TagVariable entry")
fatalf("malformed DWARF TagVariable entry")
}
if !strings.HasPrefix(name, "__cgo__") {
break
}
typ, err := d.Type(typOff)
if err != nil {
fatal("loading DWARF type: %s", err)
fatalf("loading DWARF type: %s", err)
}
t, ok := typ.(*dwarf.PtrType)
if !ok || t == nil {
fatal("internal error: %s has non-pointer type", name)
fatalf("internal error: %s has non-pointer type", name)
}
i, err := strconv.Atoi(name[7:])
if err != nil {
fatal("malformed __cgo__ name: %s", name)
fatalf("malformed __cgo__ name: %s", name)
}
if enums[i] != 0 {
t, err := d.Type(enums[i])
if err != nil {
fatal("loading DWARF type: %s", err)
fatalf("loading DWARF type: %s", err)
}
types[i] = t
} else {
......@@ -632,14 +632,14 @@ func (p *Package) gccDebug(stdin []byte) *dwarf.Data {
if f, err = elf.Open(gccTmp); err != nil {
if f, err = macho.Open(gccTmp); err != nil {
if f, err = pe.Open(gccTmp); err != nil {
fatal("cannot parse gcc output %s as ELF or Mach-O or PE object", gccTmp)
fatalf("cannot parse gcc output %s as ELF or Mach-O or PE object", gccTmp)
}
}
}
d, err := f.DWARF()
if err != nil {
fatal("cannot load DWARF debug information from %s: %s", gccTmp, err)
fatalf("cannot load DWARF debug information from %s: %s", gccTmp, err)
}
return d
}
......@@ -807,7 +807,7 @@ func (tr *TypeRepr) Set(repr string, fargs ...interface{}) {
func (c *typeConv) Type(dtype dwarf.Type) *Type {
if t, ok := c.m[dtype]; ok {
if t.Go == nil {
fatal("type conversion loop at %s", dtype)
fatalf("type conversion loop at %s", dtype)
}
return t
}
......@@ -830,11 +830,11 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
switch dt := dtype.(type) {
default:
fatal("unexpected type: %s", dtype)
fatalf("unexpected type: %s", dtype)
case *dwarf.AddrType:
if t.Size != c.ptrSize {
fatal("unexpected: %d-byte address type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte address type - %s", t.Size, dtype)
}
t.Go = c.uintptr
t.Align = t.Size
......@@ -860,7 +860,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.CharType:
if t.Size != 1 {
fatal("unexpected: %d-byte char type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte char type - %s", t.Size, dtype)
}
t.Go = c.int8
t.Align = 1
......@@ -880,7 +880,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
}
switch t.Size + int64(signed) {
default:
fatal("unexpected: %d-byte enum type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte enum type - %s", t.Size, dtype)
case 1:
t.Go = c.uint8
case 2:
......@@ -902,7 +902,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.FloatType:
switch t.Size {
default:
fatal("unexpected: %d-byte float type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte float type - %s", t.Size, dtype)
case 4:
t.Go = c.float32
case 8:
......@@ -915,7 +915,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.ComplexType:
switch t.Size {
default:
fatal("unexpected: %d-byte complex type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte complex type - %s", t.Size, dtype)
case 8:
t.Go = c.complex64
case 16:
......@@ -933,11 +933,11 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.IntType:
if dt.BitSize > 0 {
fatal("unexpected: %d-bit int type - %s", dt.BitSize, dtype)
fatalf("unexpected: %d-bit int type - %s", dt.BitSize, dtype)
}
switch t.Size {
default:
fatal("unexpected: %d-byte int type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte int type - %s", t.Size, dtype)
case 1:
t.Go = c.int8
case 2:
......@@ -1022,18 +1022,18 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
case *dwarf.UcharType:
if t.Size != 1 {
fatal("unexpected: %d-byte uchar type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte uchar type - %s", t.Size, dtype)
}
t.Go = c.uint8
t.Align = 1
case *dwarf.UintType:
if dt.BitSize > 0 {
fatal("unexpected: %d-bit uint type - %s", dt.BitSize, dtype)
fatalf("unexpected: %d-bit uint type - %s", dt.BitSize, dtype)
}
switch t.Size {
default:
fatal("unexpected: %d-byte uint type - %s", t.Size, dtype)
fatalf("unexpected: %d-byte uint type - %s", t.Size, dtype)
case 1:
t.Go = c.uint8
case 2:
......@@ -1067,7 +1067,7 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
}
if t.C.Empty() {
fatal("internal error: did not create C name for %s", dtype)
fatalf("internal error: did not create C name for %s", dtype)
}
return t
......@@ -1229,7 +1229,7 @@ func (c *typeConv) Struct(dt *dwarf.StructType) (expr *ast.StructType, csyntax s
off = dt.ByteSize
}
if off != dt.ByteSize {
fatal("struct size calculation error")
fatalf("struct size calculation error")
}
buf.WriteString("}")
csyntax = buf.String()
......
......@@ -177,11 +177,11 @@ func main() {
arch := os.Getenv("GOARCH")
if arch == "" {
fatal("$GOARCH is not set")
fatalf("$GOARCH is not set")
}
ptrSize := ptrSizeMap[arch]
if ptrSize == 0 {
fatal("unknown $GOARCH %q", arch)
fatalf("unknown $GOARCH %q", arch)
}
// Clear locale variables so gcc emits English errors [sic].
......@@ -205,7 +205,7 @@ func main() {
for _, input := range goFiles {
f, err := os.Open(input)
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
io.Copy(h, f)
f.Close()
......
......@@ -105,7 +105,7 @@ func dynimport(obj string) (syms, imports []string) {
if f, err1 = elf.Open(obj); err1 != nil {
if f, err2 = pe.Open(obj); err2 != nil {
if f, err3 = macho.Open(obj); err3 != nil {
fatal("cannot parse %s as ELF (%v) or PE (%v) or Mach-O (%v)", obj, err1, err2, err3)
fatalf("cannot parse %s as ELF (%v) or PE (%v) or Mach-O (%v)", obj, err1, err2, err3)
}
isMacho = true
}
......@@ -114,7 +114,7 @@ func dynimport(obj string) (syms, imports []string) {
var err os.Error
syms, err = f.ImportedSymbols()
if err != nil {
fatal("cannot load dynamic symbols: %v", err)
fatalf("cannot load dynamic symbols: %v", err)
}
if isMacho {
// remove leading _ that OS X insists on
......@@ -127,7 +127,7 @@ func dynimport(obj string) (syms, imports []string) {
imports, err = f.ImportedLibraries()
if err != nil {
fatal("cannot load dynamic imports: %v", err)
fatalf("cannot load dynamic imports: %v", err)
}
return
......
......@@ -18,23 +18,23 @@ import (
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
cmd, err := exec.LookPath(argv[0])
if err != nil {
fatal("exec %s: %s", argv[0], err)
fatalf("exec %s: %s", argv[0], err)
}
r0, w0, err := os.Pipe()
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
r1, w1, err := os.Pipe()
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
r2, w2, err := os.Pipe()
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
p, err := os.StartProcess(cmd, argv, &os.ProcAttr{Files: []*os.File{r0, w1, w2}})
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
defer p.Release()
r0.Close()
......@@ -58,14 +58,14 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
w, err := p.Wait(0)
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
ok = w.Exited() && w.ExitStatus() == 0
return
}
// Die with an error message.
func fatal(msg string, args ...interface{}) {
func fatalf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(2)
}
......@@ -97,7 +97,7 @@ func isName(s string) bool {
func creat(name string) *os.File {
f, err := os.Create(name)
if err != nil {
fatal("%s", err)
fatalf("%s", err)
}
return f
}
......
......@@ -63,6 +63,7 @@ func main() {
}
name = name[:colon]
}
name = strings.ToLower(name)
if name[len(name)-1] == 'f' {
printfList[name] = skip
} else {
......@@ -205,35 +206,38 @@ func (f *File) checkCallExpr(call *ast.CallExpr) {
}
// printfList records the formatted-print functions. The value is the location
// of the format parameter.
// of the format parameter. Names are lower-cased so the lookup is
// case insensitive.
var printfList = map[string]int{
"Errorf": 0,
"Fatalf": 0,
"Fprintf": 1,
"Panicf": 0,
"Printf": 0,
"Sprintf": 0,
"errorf": 0,
"fatalf": 0,
"fprintf": 1,
"panicf": 0,
"printf": 0,
"sprintf": 0,
}
// printList records the unformatted-print functions. The value is the location
// of the first parameter to be printed.
// of the first parameter to be printed. Names are lower-cased so the lookup is
// case insensitive.
var printList = map[string]int{
"Error": 0,
"Fatal": 0,
"Fprint": 1, "Fprintln": 1,
"Panic": 0, "Panicln": 0,
"Print": 0, "Println": 0,
"Sprint": 0, "Sprintln": 0,
"error": 0,
"fatal": 0,
"fprint": 1, "fprintln": 1,
"panic": 0, "panicln": 0,
"print": 0, "println": 0,
"sprint": 0, "sprintln": 0,
}
// checkCall triggers the print-specific checks if the call invokes a print function.
func (f *File) checkCall(call *ast.CallExpr, name string) {
func (f *File) checkCall(call *ast.CallExpr, Name string) {
name := strings.ToLower(Name)
if skip, ok := printfList[name]; ok {
f.checkPrintf(call, name, skip)
f.checkPrintf(call, Name, skip)
return
}
if skip, ok := printList[name]; ok {
f.checkPrint(call, name, skip)
f.checkPrint(call, Name, skip)
return
}
}
......@@ -362,8 +366,14 @@ func BadFunctionUsedInTests() {
fmt.Printf("%s%%%d", "hi", 3) // right # percents
fmt.Printf("%.*d", 3, 3) // right # percents, with a *
fmt.Printf("%.*d", 3, 3, 3) // wrong # percents, with a *
printf("now is the time", "buddy") // no %s
Printf("now is the time", "buddy") // no %s
f := new(File)
f.Warn(0, "%s", "hello", 3) // % in call to added function
f.Warnf(0, "%s", "hello", 3) // wrong # %s in call to added function
}
// printf is used by the test.
func printf(format string, args ...interface{}) {
panic("don't call - testing only")
}
This diff is collapsed.
......@@ -90,7 +90,7 @@ prog:
$2.node = $3;
$2.node.dim[0] = 1;
if f != 0 {
Error("redefinition of %v", $2.name);
Errorf("redefinition of %v", $2.name);
} else
if vflag {
fmt.Printf("%v\t%v\n", $2.name, &$2.node);
......@@ -106,7 +106,7 @@ prog:
}
}
if i >= Ndim {
Error("too many dimensions");
Errorf("too many dimensions");
i = Ndim-1;
}
fund[i] = $2;
......@@ -116,7 +116,7 @@ prog:
$2.node.dim[0] = 1;
$2.node.dim[i] = 1;
if f != 0 {
Error("redefinition of %v", $2.name);
Errorf("redefinition of %v", $2.name);
} else
if vflag {
fmt.Printf("%v\t#\n", $2.name);
......@@ -175,7 +175,7 @@ expr2:
for i=1; i<Ndim; i++ {
if $3.dim[i] != 0 {
Error("exponent has units");
Errorf("exponent has units");
$$ = $1;
break;
}
......@@ -183,7 +183,7 @@ expr2:
if i >= Ndim {
i = int($3.vval);
if float64(i) != $3.vval {
Error("exponent not integral");
Errorf("exponent not integral");
}
xpn(&$$, &$1, i);
}
......@@ -200,7 +200,7 @@ expr0:
VAR
{
if $1.node.dim[0] == 0 {
Error("undefined %v", $1.name);
Errorf("undefined %v", $1.name);
$$ = one;
} else
$$ = $1.node;
......@@ -284,7 +284,7 @@ numb:
}
func (UnitsLex) Error(s string) {
Error("syntax error, last name: %v", sym)
Errorf("syntax error, last name: %v", sym)
}
func main() {
......@@ -391,7 +391,7 @@ func rdigit(c int) bool {
return false
}
func Error(s string, v ...interface{}) {
func Errorf(s string, v ...interface{}) {
fmt.Printf("%v: %v\n\t", lineno, line)
fmt.Printf(s, v...)
fmt.Printf("\n")
......@@ -411,7 +411,7 @@ func add(c, a, b *Node) {
d = a.dim[i]
c.dim[i] = d
if d != b.dim[i] {
Error("add must be like units")
Errorf("add must be like units")
}
}
c.vval = fadd(a.vval, b.vval)
......@@ -425,7 +425,7 @@ func sub(c, a, b *Node) {
d = a.dim[i]
c.dim[i] = d
if d != b.dim[i] {
Error("sub must be like units")
Errorf("sub must be like units")
}
}
c.vval = fadd(a.vval, -b.vval)
......@@ -711,11 +711,11 @@ func fmul(a, b float64) float64 {
}
if l > Maxe {
Error("overflow in multiply")
Errorf("overflow in multiply")
return 1
}
if l < -Maxe {
Error("underflow in multiply")
Errorf("underflow in multiply")
return 0
}
return a * b
......@@ -728,7 +728,7 @@ func fdiv(a, b float64) float64 {
if b <= 0 {
if b == 0 {
Error("division by zero: %v %v", a, b)
Errorf("division by zero: %v %v", a, b)
return 1
}
l = math.Log(-b)
......@@ -746,11 +746,11 @@ func fdiv(a, b float64) float64 {
}
if l < -Maxe {
Error("overflow in divide")
Errorf("overflow in divide")
return 1
}
if l > Maxe {
Error("underflow in divide")
Errorf("underflow in divide")
return 0
}
return a / b
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment