]>
git.saurik.com Git - bison.git/blob - src/location.c
3 Copyright (C) 2002, 2005-2013 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 location
const empty_location
= EMPTY_LOCATION_INIT
;
31 /* If BUF is null, add BUFSIZE (which in this case must be less than
32 INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
33 COLUMN. If an overflow occurs, or might occur but is undetectable,
34 return INT_MAX. Assume COLUMN is nonnegative. */
37 add_column_width (int column
, char const *buf
, size_t bufsize
)
40 unsigned int remaining_columns
= INT_MAX
- column
;
44 if (INT_MAX
/ 2 <= bufsize
)
46 width
= mbsnwidth (buf
, bufsize
, 0);
51 return width
<= remaining_columns
? column
+ width
: INT_MAX
;
54 /* Set *LOC and adjust scanner cursor to account for token TOKEN of
58 location_compute (location
*loc
, boundary
*cur
, char const *token
, size_t size
)
61 int column
= cur
->column
;
62 char const *p0
= token
;
63 char const *p
= token
;
64 char const *lim
= token
+ size
;
68 for (p
= token
; p
< lim
; p
++)
72 line
+= line
< INT_MAX
;
78 column
= add_column_width (column
, p0
, p
- p0
);
79 column
= add_column_width (column
, NULL
, 8 - ((column
- 1) & 7));
88 cur
->column
= column
= add_column_width (column
, p0
, p
- p0
);
92 if (line
== INT_MAX
&& loc
->start
.line
!= INT_MAX
)
93 complain (loc
, Wother
, _("line number overflow"));
94 if (column
== INT_MAX
&& loc
->start
.column
!= INT_MAX
)
95 complain (loc
, Wother
, _("column number overflow"));
99 /* Output to OUT the location LOC.
100 Warning: it uses quotearg's slot 3. */
102 location_print (location loc
, FILE *out
)
105 int end_col
= 0 != loc
.end
.column
? loc
.end
.column
- 1 : 0;
106 res
+= fprintf (out
, "%s",
107 quotearg_n_style (3, escape_quoting_style
, loc
.start
.file
));
108 if (0 <= loc
.start
.line
)
110 res
+= fprintf (out
, ":%d", loc
.start
.line
);
111 if (0 <= loc
.start
.column
)
112 res
+= fprintf (out
, ".%d", loc
.start
.column
);
114 if (loc
.start
.file
!= loc
.end
.file
)
116 res
+= fprintf (out
, "-%s",
117 quotearg_n_style (3, escape_quoting_style
,
119 if (0 <= loc
.end
.line
)
121 res
+= fprintf (out
, ":%d", loc
.end
.line
);
123 res
+= fprintf (out
, ".%d", end_col
);
126 else if (0 <= loc
.end
.line
)
128 if (loc
.start
.line
< loc
.end
.line
)
130 res
+= fprintf (out
, "-%d", loc
.end
.line
);
132 res
+= fprintf (out
, ".%d", end_col
);
134 else if (0 <= end_col
&& loc
.start
.column
< end_col
)
135 res
+= fprintf (out
, "-%d", end_col
);
142 /* Persistant data used by location_caret to avoid reopening and rereading the
143 same file all over for each error. */
151 static struct caret_info caret_info
= { NULL
, 1, 0 };
156 if (caret_info
.source
)
157 fclose (caret_info
.source
);
158 caret_info
.source
= NULL
;
160 caret_info
.offset
= 0;
164 location_caret (location loc
, FILE *out
)
166 /* FIXME: find a way to support multifile locations, and only open once each
167 file. That would make the procedure future-proof. */
168 if (! (caret_info
.source
169 || (caret_info
.source
= fopen (loc
.start
.file
, "r")))
170 || loc
.start
.column
== -1 || loc
.start
.line
== -1)
173 /* If the line we want to quote is seekable (the same line as the previous
174 location), just seek it. If it was a previous line, we lost track of it,
175 so return to the start of file. */
176 if (caret_info
.line
<= loc
.start
.line
)
177 fseek (caret_info
.source
, caret_info
.offset
, SEEK_SET
);
181 caret_info
.offset
= 0;
182 fseek (caret_info
.source
, caret_info
.offset
, SEEK_SET
);
185 /* Advance to the line's position, keeping track of the offset. */
186 while (caret_info
.line
< loc
.start
.line
)
187 caret_info
.line
+= getc (caret_info
.source
) == '\n';
188 caret_info
.offset
= ftell (caret_info
.source
);
190 /* Read the actual line. Don't update the offset, so that we keep a pointer
191 to the start of the line. */
193 char c
= getc (caret_info
.source
);
196 /* Quote the file, indent by a single column. */
200 while ((c
= getc (caret_info
.source
)) != EOF
&& c
!= '\n');
204 /* The caret of a multiline location ends with the first line. */
205 size_t len
= loc
.start
.line
!= loc
.end
.line
206 ? ftell (caret_info
.source
) - caret_info
.offset
210 /* Print the carets (at least one), with the same indent as above.*/
211 fprintf (out
, " %*s", loc
.start
.column
- 1, "");
212 for (i
= loc
.start
.column
; i
== loc
.start
.column
|| i
< len
; ++i
)
221 boundary_set_from_string (boundary
*bound
, char *loc_str
)
223 /* Must search in reverse since the file name field may
224 * contain `.' or `:'. */
225 char *delim
= strrchr (loc_str
, '.');
228 bound
->column
= atoi (delim
+1);
229 delim
= strrchr (loc_str
, ':');
232 bound
->line
= atoi (delim
+1);
233 bound
->file
= uniqstr_new (loc_str
);