Source file test/fixedbugs/issue22662.go
1 // run 2 3 // Copyright 2018 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // Verify effect of various line directives. 8 // TODO: check columns 9 10 package main 11 12 import ( 13 "fmt" 14 "runtime" 15 "strings" 16 ) 17 18 // Since go.dev/issue/70478, the compiler resolves a relative filename in a 19 // line directive against the directory of the source file, so the expected 20 // path may be a suffix of the actual filename rather than equal to it. 21 // Accept either an exact match or the file as a path-component suffix. 22 // Compiler-emitted paths are always slash-normalized (cmd/internal/objabi.AbsFile). 23 func check(file string, line int) { 24 _, f, l, ok := runtime.Caller(1) 25 if !ok { 26 panic("runtime.Caller(1) failed") 27 } 28 // Prepend exactly one "/" even if file already starts with one, so that 29 // e.g. file="/foo/bar.go" looks for "/foo/bar.go" as the suffix, not "//". 30 want := "/" + strings.TrimPrefix(file, "/") 31 if (f != file && !strings.HasSuffix(f, want)) || l != line { 32 panic(fmt.Sprintf("got %s:%d; want %s:%d (or suffix %s)", f, l, file, line, want)) 33 } 34 } 35 36 func main() { 37 //-style line directives 38 //line :1 39 check("??", 1) // no file specified 40 //line foo.go:1 41 check("foo.go", 1) 42 //line bar.go:10:20 43 check("bar.go", 10) 44 //line :11:22 45 check("bar.go", 11) // no file, but column specified => keep old filename 46 47 /*-style line directives */ 48 /*line :1*/ check("??", 1) // no file specified 49 /*line foo.go:1*/ check("foo.go", 1) 50 /*line bar.go:10:20*/ check("bar.go", 10) 51 /*line :11:22*/ check("bar.go", 11) // no file, but column specified => keep old filename 52 53 /*line :10*/ check("??", 10); /*line foo.go:20*/ check("foo.go", 20); /*line :30:1*/ check("foo.go", 30) 54 check("foo.go", 31) 55 } 56