]> git.saurik.com Git - bison.git/blame - src/location.c
remove useless include.
[bison.git] / src / location.c
CommitLineData
8efe435c 1/* Locations for Bison
1462fcee 2 Copyright (C) 2002, 2005-2010 Free Software Foundation, Inc.
8efe435c
AD
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
f16b0819 6 This program is free software: you can redistribute it and/or modify
8efe435c 7 it under the terms of the GNU General Public License as published by
f16b0819
PE
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
8efe435c 10
f16b0819 11 This program is distributed in the hope that it will be useful,
8efe435c
AD
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
f16b0819 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8efe435c 18
2cec9080 19#include <config.h>
1a715ef2 20#include "system.h"
81ebdef9 21
0c8e079f 22#include <mbswidth.h>
b17a1fc5 23#include <quotearg.h>
8efe435c 24
0c8e079f 25#include "complain.h"
81ebdef9
PE
26#include "location.h"
27
28e52c0d 28location const empty_location = EMPTY_LOCATION_INIT;
b17a1fc5 29
e9071366
AD
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. */
34
35static inline int
36add_column_width (int column, char const *buf, size_t bufsize)
37{
38 size_t width;
39 unsigned int remaining_columns = INT_MAX - column;
40
41 if (buf)
42 {
43 if (INT_MAX / 2 <= bufsize)
44 return INT_MAX;
45 width = mbsnwidth (buf, bufsize, 0);
46 }
47 else
48 width = bufsize;
49
50 return width <= remaining_columns ? column + width : INT_MAX;
51}
52
53/* Set *LOC and adjust scanner cursor to account for token TOKEN of
54 size SIZE. */
55
56void
57location_compute (location *loc, boundary *cur, char const *token, size_t size)
58{
59 int line = cur->line;
60 int column = cur->column;
61 char const *p0 = token;
62 char const *p = token;
63 char const *lim = token + size;
64
65 loc->start = *cur;
66
67 for (p = token; p < lim; p++)
68 switch (*p)
69 {
70 case '\n':
71 line += line < INT_MAX;
72 column = 1;
73 p0 = p + 1;
74 break;
75
76 case '\t':
77 column = add_column_width (column, p0, p - p0);
78 column = add_column_width (column, NULL, 8 - ((column - 1) & 7));
79 p0 = p + 1;
80 break;
81
82 default:
83 break;
84 }
85
86 cur->line = line;
87 cur->column = column = add_column_width (column, p0, p - p0);
88
89 loc->end = *cur;
90
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"));
95}
96
97
b17a1fc5
PE
98/* Output to OUT the location LOC.
99 Warning: it uses quotearg's slot 3. */
348f5608 100unsigned
81ebdef9 101location_print (FILE *out, location loc)
b17a1fc5 102{
348f5608 103 unsigned res = 0;
580c075d 104 int end_col = 0 != loc.end.column ? loc.end.column - 1 : 0;
348f5608
AR
105 res += fprintf (out, "%s",
106 quotearg_n_style (3, escape_quoting_style, loc.start.file));
580c075d
JD
107 if (0 <= loc.start.line)
108 {
348f5608 109 res += fprintf(out, ":%d", loc.start.line);
580c075d 110 if (0 <= loc.start.column)
348f5608 111 res += fprintf (out, ".%d", loc.start.column);
580c075d 112 }
b17a1fc5 113 if (loc.start.file != loc.end.file)
580c075d 114 {
348f5608
AR
115 res += fprintf (out, "-%s",
116 quotearg_n_style (3, escape_quoting_style,
117 loc.end.file));
580c075d
JD
118 if (0 <= loc.end.line)
119 {
348f5608 120 res += fprintf(out, ":%d", loc.end.line);
580c075d 121 if (0 <= end_col)
348f5608 122 res += fprintf (out, ".%d", end_col);
580c075d
JD
123 }
124 }
125 else if (0 <= loc.end.line)
126 {
127 if (loc.start.line < loc.end.line)
128 {
348f5608 129 res += fprintf (out, "-%d", loc.end.line);
580c075d 130 if (0 <= end_col)
348f5608 131 res += fprintf (out, ".%d", end_col);
580c075d
JD
132 }
133 else if (0 <= end_col && loc.start.column < end_col)
348f5608 134 res += fprintf (out, "-%d", end_col);
580c075d 135 }
348f5608
AR
136
137 return res;
b17a1fc5 138}
3fc65ead
JD
139
140void
141boundary_set_from_string (boundary *bound, char *loc_str)
142{
143 /* Must search in reverse since the file name field may
144 * contain `.' or `:'. */
145 char *delim = strrchr (loc_str, '.');
146 aver (delim);
147 *delim = '\0';
148 bound->column = atoi (delim+1);
149 delim = strrchr (loc_str, ':');
150 aver (delim);
151 *delim = '\0';
152 bound->line = atoi (delim+1);
153 bound->file = uniqstr_new (loc_str);
154}