@@ -75,10 +75,11 @@ type printer struct {
75
75
// white space). If there's a difference and SourcePos is set in
76
76
// ConfigMode, //line directives are used in the output to restore
77
77
// original source positions for a reader.
78
- pos token.Position // current position in AST (source) space
79
- out token.Position // current position in output space
80
- last token.Position // value of pos after calling writeString
81
- linePtr * int // if set, record out.Line for the next token in *linePtr
78
+ pos token.Position // current position in AST (source) space
79
+ out token.Position // current position in output space
80
+ last token.Position // value of pos after calling writeString
81
+ linePtr * int // if set, record out.Line for the next token in *linePtr
82
+ sourcePosErr error // if non-nil, the first error emitting a //line directive
82
83
83
84
// The list of all source comments, in order of appearance.
84
85
comments []* ast.CommentGroup // may be nil
@@ -196,6 +197,13 @@ func (p *printer) lineFor(pos token.Pos) int {
196
197
// writeLineDirective writes a //line directive if necessary.
197
198
func (p * printer ) writeLineDirective (pos token.Position ) {
198
199
if pos .IsValid () && (p .out .Line != pos .Line || p .out .Filename != pos .Filename ) {
200
+ if strings .ContainsAny (pos .Filename , "\r \n " ) {
201
+ if p .sourcePosErr == nil {
202
+ p .sourcePosErr = fmt .Errorf ("go/printer: source filename contains unexpected newline character: %q" , pos .Filename )
203
+ }
204
+ return
205
+ }
206
+
199
207
p .output = append (p .output , tabwriter .Escape ) // protect '\n' in //line from tabwriter interpretation
200
208
p .output = append (p .output , fmt .Sprintf ("//line %s:%d\n " , pos .Filename , pos .Line )... )
201
209
p .output = append (p .output , tabwriter .Escape )
@@ -1169,7 +1177,7 @@ func (p *printer) printNode(node any) error {
1169
1177
goto unsupported
1170
1178
}
1171
1179
1172
- return nil
1180
+ return p . sourcePosErr
1173
1181
1174
1182
unsupported:
1175
1183
return fmt .Errorf ("go/printer: unsupported node type %T" , node )
0 commit comments