]> git.saurik.com Git - bison.git/blob - src/location.c
* tests/testsuite.at (AT_CHECK): When checking if $1 starts with
[bison.git] / src / location.c
1 /* Locations for Bison
2 Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison 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 2, or (at your option)
9 any later version.
10
11 Bison 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.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <mbswidth.h>
25 #include <quotearg.h>
26
27 #include "complain.h"
28 #include "location.h"
29
30 location const empty_location;
31
32 /* If BUF is null, add BUFSIZE (which in this case must be less than
33 INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
34 COLUMN. If an overflow occurs, or might occur but is undetectable,
35 return INT_MAX. Assume COLUMN is nonnegative. */
36
37 static inline int
38 add_column_width (int column, char const *buf, size_t bufsize)
39 {
40 size_t width;
41 unsigned int remaining_columns = INT_MAX - column;
42
43 if (buf)
44 {
45 if (INT_MAX / 2 <= bufsize)
46 return INT_MAX;
47 width = mbsnwidth (buf, bufsize, 0);
48 }
49 else
50 width = bufsize;
51
52 return width <= remaining_columns ? column + width : INT_MAX;
53 }
54
55 /* Set *LOC and adjust scanner cursor to account for token TOKEN of
56 size SIZE. */
57
58 void
59 location_compute (location *loc, boundary *cur, char const *token, size_t size)
60 {
61 int line = cur->line;
62 int column = cur->column;
63 char const *p0 = token;
64 char const *p = token;
65 char const *lim = token + size;
66
67 loc->start = *cur;
68
69 for (p = token; p < lim; p++)
70 switch (*p)
71 {
72 case '\n':
73 line += line < INT_MAX;
74 column = 1;
75 p0 = p + 1;
76 break;
77
78 case '\t':
79 column = add_column_width (column, p0, p - p0);
80 column = add_column_width (column, NULL, 8 - ((column - 1) & 7));
81 p0 = p + 1;
82 break;
83
84 default:
85 break;
86 }
87
88 cur->line = line;
89 cur->column = column = add_column_width (column, p0, p - p0);
90
91 loc->end = *cur;
92
93 if (line == INT_MAX && loc->start.line != INT_MAX)
94 warn_at (*loc, _("line number overflow"));
95 if (column == INT_MAX && loc->start.column != INT_MAX)
96 warn_at (*loc, _("column number overflow"));
97 }
98
99
100 /* Output to OUT the location LOC.
101 Warning: it uses quotearg's slot 3. */
102 void
103 location_print (FILE *out, location loc)
104 {
105 int end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
106 fprintf (out, "%s:%d.%d",
107 quotearg_n_style (3, escape_quoting_style, loc.start.file),
108 loc.start.line, loc.start.column);
109
110 if (loc.start.file != loc.end.file)
111 fprintf (out, "-%s:%d.%d",
112 quotearg_n_style (3, escape_quoting_style, loc.end.file),
113 loc.end.line, end_col);
114 else if (loc.start.line < loc.end.line)
115 fprintf (out, "-%d.%d", loc.end.line, end_col);
116 else if (loc.start.column < end_col)
117 fprintf (out, "-%d", end_col);
118 }