Skip to content

Commit e7387bd

Browse files
archanaravindardbenoit17
authored andcommitted
1 parent f47e2d2 commit e7387bd

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

misc/cgo/errors/errors_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func TestReportsTypeErrors(t *testing.T) {
107107
for _, file := range []string{
108108
"err1.go",
109109
"err2.go",
110+
"err5.go",
110111
"issue11097a.go",
111112
"issue11097b.go",
112113
"issue18452.go",

misc/cgo/errors/testdata/err5.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
package main
5+
//line /tmp/_cgo_.go:1
6+
//go:cgo_dynamic_linker "/elf/interp" // ERROR HERE: only allowed in cgo-generated code
7+
func main() {}

src/cmd/compile/internal/gc/noder.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,14 +1612,33 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
16121612
return pragma
16131613
}
16141614

1615+
// trimFilename returns the "trimmed" filename of b, which is the
1616+
// absolute filename after applying -trimpath processing. This
1617+
// filename form is suitable for use in object files and export data.
1618+
//
1619+
// If b's filename has already been trimmed (i.e., because it was read
1620+
// in from an imported package's export data), then the filename is
1621+
// returned unchanged.
1622+
func trimFilename(b *syntax.PosBase) string {
1623+
filename := b.Filename()
1624+
if !b.Trimmed() {
1625+
dir := ""
1626+
if b.IsFileBase() {
1627+
dir = Ctxt.Pathname
1628+
}
1629+
filename = objabi.AbsFile(dir, filename, pathPrefix)
1630+
}
1631+
return filename
1632+
}
1633+
16151634
// isCgoGeneratedFile reports whether pos is in a file
16161635
// generated by cgo, which is to say a file with name
16171636
// beginning with "_cgo_". Such files are allowed to
16181637
// contain cgo directives, and for security reasons
16191638
// (primarily misuse of linker flags), other files are not.
16201639
// See golang.org/issue/23672.
16211640
func isCgoGeneratedFile(pos syntax.Pos) bool {
1622-
return strings.HasPrefix(filepath.Base(filepath.Clean(fileh(pos.Base().Filename()))), "_cgo_")
1641+
return strings.HasPrefix(filepath.Base(trimFilename(pos.Base().Pos().Base())), "_cgo_")
16231642
}
16241643

16251644
// safeArg reports whether arg is a "safe" command-line argument,

src/cmd/compile/internal/syntax/parser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ func (p *parser) updateBase(pos Pos, tline, tcol uint, text string) {
146146
// If we have a column (//line filename:line:col form),
147147
// an empty filename means to use the previous filename.
148148
filename := text[:i-1] // lop off ":line"
149+
trimmed := false
149150
if filename == "" && ok2 {
150151
filename = p.base.Filename()
152+
trimmed = p.base.Trimmed()
151153
}
152154

153-
p.base = NewLineBase(pos, filename, line, col)
155+
p.base = NewLineBase(pos, filename, trimmed, line, col)
156+
154157
}
155158

156159
func commentText(s string) string {

src/cmd/compile/internal/syntax/pos.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,30 @@ type PosBase struct {
9393
pos Pos
9494
filename string
9595
line, col uint32
96+
trimmed bool // whether -trimpath has been applied
9697
}
9798

9899
// NewFileBase returns a new PosBase for the given filename.
99100
// A file PosBase's position is relative to itself, with the
100101
// position being filename:1:1.
101102
func NewFileBase(filename string) *PosBase {
102-
base := &PosBase{MakePos(nil, linebase, colbase), filename, linebase, colbase}
103-
base.pos.base = base
104-
return base
103+
return NewTrimmedFileBase(filename, false)
104+
}
105+
106+
// NewTrimmedFileBase is like NewFileBase, but allows specifying Trimmed.
107+
func NewTrimmedFileBase(filename string, trimmed bool) *PosBase {
108+
base := &PosBase{MakePos(nil, linebase, colbase), filename, linebase, colbase, trimmed}
109+
base.pos.base = base
110+
return base
105111
}
106112

107113
// NewLineBase returns a new PosBase for a line directive "line filename:line:col"
108114
// relative to pos, which is the position of the character immediately following
109115
// the comment containing the line directive. For a directive in a line comment,
110116
// that position is the beginning of the next line (i.e., the newline character
111117
// belongs to the line comment).
112-
func NewLineBase(pos Pos, filename string, line, col uint) *PosBase {
113-
return &PosBase{pos, filename, sat32(line), sat32(col)}
118+
func NewLineBase(pos Pos, filename string, trimmed bool, line, col uint) *PosBase {
119+
return &PosBase{pos, filename, sat32(line), sat32(col), trimmed}
114120
}
115121

116122
func (base *PosBase) IsFileBase() bool {
@@ -148,6 +154,13 @@ func (base *PosBase) Col() uint {
148154
return uint(base.col)
149155
}
150156

157+
func (base *PosBase) Trimmed() bool {
158+
if base == nil {
159+
return false
160+
}
161+
return base.trimmed
162+
}
163+
151164
func sat32(x uint) uint32 {
152165
if x > PosMax {
153166
return PosMax

0 commit comments

Comments
 (0)