]>
git.saurik.com Git - bison.git/blob - src/location.c
2 Copyright (C) 2002, 2005-2010 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 location
const empty_location
= EMPTY_LOCATION_INIT
;
30 /* If BUF is null, add BUFSIZE (which in this case must be less than
31 INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
32 COLUMN. If an overflow occurs, or might occur but is undetectable,
33 return INT_MAX. Assume COLUMN is nonnegative. */
36 add_column_width (int column
, char const *buf
, size_t bufsize
)
39 unsigned int remaining_columns
= INT_MAX
- column
;
43 if (INT_MAX
/ 2 <= bufsize
)
45 width
= mbsnwidth (buf
, bufsize
, 0);
50 return width
<= remaining_columns
? column
+ width
: INT_MAX
;
53 /* Set *LOC and adjust scanner cursor to account for token TOKEN of
57 location_compute (location
*loc
, boundary
*cur
, char const *token
, size_t size
)
60 int column
= cur
->column
;
61 char const *p0
= token
;
62 char const *p
= token
;
63 char const *lim
= token
+ size
;
67 for (p
= token
; p
< lim
; p
++)
71 line
+= line
< INT_MAX
;
77 column
= add_column_width (column
, p0
, p
- p0
);
78 column
= add_column_width (column
, NULL
, 8 - ((column
- 1) & 7));
87 cur
->column
= column
= add_column_width (column
, p0
, p
- p0
);
91 if (line
== INT_MAX
&& loc
->start
.line
!= INT_MAX
)
92 warn_at (*loc
, _("line number overflow"));
93 if (column
== INT_MAX
&& loc
->start
.column
!= INT_MAX
)
94 warn_at (*loc
, _("column number overflow"));
98 /* Output to OUT the location LOC.
99 Warning: it uses quotearg's slot 3. */
101 location_print (FILE *out
, location loc
)
103 int end_col
= 0 != loc
.end
.column
? loc
.end
.column
- 1 : 0;
105 quotearg_n_style (3, escape_quoting_style
, loc
.start
.file
));
106 if (0 <= loc
.start
.line
)
108 fprintf(out
, ":%d", loc
.start
.line
);
109 if (0 <= loc
.start
.column
)
110 fprintf (out
, ".%d", loc
.start
.column
);
112 if (loc
.start
.file
!= loc
.end
.file
)
115 quotearg_n_style (3, escape_quoting_style
, loc
.end
.file
));
116 if (0 <= loc
.end
.line
)
118 fprintf(out
, ":%d", loc
.end
.line
);
120 fprintf (out
, ".%d", end_col
);
123 else if (0 <= loc
.end
.line
)
125 if (loc
.start
.line
< loc
.end
.line
)
127 fprintf (out
, "-%d", loc
.end
.line
);
129 fprintf (out
, ".%d", end_col
);
131 else if (0 <= end_col
&& loc
.start
.column
< end_col
)
132 fprintf (out
, "-%d", end_col
);
137 boundary_set_from_string (boundary
*bound
, char *loc_str
)
139 /* Must search in reverse since the file name field may
140 * contain `.' or `:'. */
141 char *delim
= strrchr (loc_str
, '.');
144 bound
->column
= atoi (delim
+1);
145 delim
= strrchr (loc_str
, ':');
148 bound
->line
= atoi (delim
+1);
149 bound
->file
= uniqstr_new (loc_str
);