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