Source file test/fixedbugs/issue18149.go

     1  // run
     2  
     3  // Copyright 2016 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 that //line directives with filenames
     8  // containing ':' (Windows) are correctly parsed.
     9  // (For a related issue, see test/fixedbugs/bug305.go)
    10  
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"runtime"
    16  	"strings"
    17  )
    18  
    19  // Since go.dev/issue/70478, the compiler resolves a relative filename in a
    20  // line directive against the directory of the source file, so the expected
    21  // path may be a suffix of the actual filename rather than equal to it.
    22  // Accept either an exact match or the file as a path-component suffix.
    23  // Compiler-emitted paths are always slash-normalized (cmd/internal/objabi.AbsFile).
    24  func check(file string, line int) {
    25  	_, f, l, ok := runtime.Caller(1)
    26  	if !ok {
    27  		panic("runtime.Caller(1) failed")
    28  	}
    29  	// Prepend exactly one "/" even if file already starts with one, so that
    30  	// e.g. file="/foo/bar.go" looks for "/foo/bar.go" as the suffix, not "//".
    31  	want := "/" + strings.TrimPrefix(file, "/")
    32  	if (f != file && !strings.HasSuffix(f, want)) || l != line {
    33  		panic(fmt.Sprintf("got %s:%d; want %s:%d (or suffix %s)", f, l, file, line, want))
    34  	}
    35  }
    36  
    37  func main() {
    38  //line /foo/bar.go:123
    39  	check("/foo/bar.go", 123)
    40  //line c:/foo/bar.go:987
    41  	check("c:/foo/bar.go", 987)
    42  }
    43  

View as plain text