]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: fix printf test for unsafe Pointer
authorRob Pike <r@golang.org>
Tue, 26 Feb 2013 18:36:13 +0000 (10:36 -0800)
committerRob Pike <r@golang.org>
Tue, 26 Feb 2013 18:36:13 +0000 (10:36 -0800)
And fix test. Pointer to unsafe.Pointer tests nothing important...
Also identify the incorrect type: go/types.Type is a Stringer.

Also fix a couple of incorrect format verbs found by new printf checker,
now that we can run it on more files.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7385051

src/cmd/vet/print.go
src/cmd/vet/test_print.go
src/pkg/sync/atomic/atomic_test.go

index ad3d39c8fcf70ecacdd9fe840f9eaaef285fbe38..5b012027101a5c8c84fd42d098b46d91fa79ef7a 100644 (file)
@@ -276,6 +276,9 @@ func (f *File) checkPrintfArg(call *ast.CallExpr, verb rune, flags []byte, argNu
                                        return
                                }
                        }
+                       if f.pkg == nil { // Nothing more to do.
+                               return
+                       }
                        // Verb is good. If nargs>1, we have something like %.*s and all but the final
                        // arg must be integer.
                        for i := 0; i < nargs-1; i++ {
@@ -285,8 +288,13 @@ func (f *File) checkPrintfArg(call *ast.CallExpr, verb rune, flags []byte, argNu
                        }
                        for _, v := range printVerbs {
                                if v.verb == verb {
-                                       if !f.matchArgType(v.typ, call.Args[argNum+nargs-1]) {
-                                               f.Badf(call.Pos(), "arg for printf verb %%%c of wrong type", verb)
+                                       arg := call.Args[argNum+nargs-1]
+                                       if !f.matchArgType(v.typ, arg) {
+                                               typeString := ""
+                                               if typ := f.pkg.types[arg]; typ != nil {
+                                                       typeString = typ.String()
+                                               }
+                                               f.Badf(call.Pos(), "arg for printf verb %%%c of wrong type: %s", verb, typeString)
                                        }
                                        break
                                }
@@ -298,9 +306,6 @@ func (f *File) checkPrintfArg(call *ast.CallExpr, verb rune, flags []byte, argNu
 }
 
 func (f *File) matchArgType(t printfArgType, arg ast.Expr) bool {
-       if f.pkg == nil {
-               return true // Don't know; assume OK.
-       }
        // TODO: for now, we can only test builtin types and untyped constants.
        typ := f.pkg.types[arg]
        if typ == nil {
@@ -322,7 +327,7 @@ func (f *File) matchArgType(t printfArgType, arg ast.Expr) bool {
        case types.String:
                return t&argString != 0
        case types.UnsafePointer:
-               return t&argPointer != 0
+               return t&(argPointer|argInt) != 0
        case types.UntypedBool:
                return t&argBool != 0
        case types.UntypedComplex:
index 5a19e07a555c8c414c3254e3399d9662abf51d9d..bd06f25963e51d559251fdfca290592e0b8d7995 100644 (file)
@@ -14,8 +14,8 @@ import (
 )
 
 func UnsafePointerPrintfTest() {
-       var up *unsafe.Pointer
-       fmt.Printf("%p", up)
+       var up unsafe.Pointer
+       fmt.Printf("%p, %x %X", up, up, up)
 }
 
 // Error methods that do not satisfy the Error interface and should be checked.
index 25be63b5a37156f9342603a7a22b0023a4e15e6a..3e105561c4efcc679c861a0fc536194693140c5b 100644 (file)
@@ -1119,7 +1119,7 @@ func TestStoreLoadRelAcq32(t *testing.T) {
                                        d1 := X.data1
                                        d2 := X.data2
                                        if d1 != i || d2 != float32(i) {
-                                               t.Fatalf("incorrect data: %d/%d (%d)", d1, d2, i)
+                                               t.Fatalf("incorrect data: %d/%g (%d)", d1, d2, i)
                                        }
                                }
                        }
@@ -1167,7 +1167,7 @@ func TestStoreLoadRelAcq64(t *testing.T) {
                                        d1 := X.data1
                                        d2 := X.data2
                                        if d1 != i || d2 != float64(i) {
-                                               t.Fatalf("incorrect data: %d/%d (%d)", d1, d2, i)
+                                               t.Fatalf("incorrect data: %d/%g (%d)", d1, d2, i)
                                        }
                                }
                        }