-
Notifications
You must be signed in to change notification settings - Fork 9
/
loc.c
237 lines (209 loc) · 5.98 KB
/
loc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2011 Leiden University. All rights reserved.
* Copyright 2014 Ecole Normale Superieure. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include <string.h>
#include "loc.h"
/* A pet_loc object represents a region of the input file.
* The "start" and "end" fields contain the offsets in the input file
* of the region, where end points to the first character after the region.
* "line" is the line number of a line inside the region.
* "indentation" is (a reasonable guess at) the indentation of the region.
*
* A special pet_loc_dummy instance is used to indicate that
* no offset information is available (yet).
*/
struct pet_loc {
int ref;
isl_ctx *ctx;
unsigned start;
unsigned end;
int line;
char *indent;
};
/* A special pet_loc object that is used to indicate that
* no region information is available yet.
*
* This special pet_loc object cannot be changed.
* In particular, it is not allowed to call pet_loc_cow on this object.
*/
pet_loc pet_loc_dummy = {
.ref = -1,
.ctx = NULL,
.start = 0,
.end = 0,
.line = -1,
.indent = ""
};
/* Allocate a pet_loc with the given start, end, line number and indentation.
*/
__isl_give pet_loc *pet_loc_alloc(isl_ctx *ctx,
unsigned start, unsigned end, int line, __isl_take char *indent)
{
pet_loc *loc;
if (!indent)
return NULL;
loc = isl_alloc_type(ctx, struct pet_loc);
if (!loc)
goto error;
loc->ctx = ctx;
isl_ctx_ref(ctx);
loc->ref = 1;
loc->start = start;
loc->end = end;
loc->line = line;
loc->indent = indent;
return loc;
error:
free(indent);
return NULL;
}
/* Return a pet_loc that is equal to "loc" and that has only one reference.
*
* It is not allowed to call pet_loc_cow on pet_loc_dummy.
* We cannot raise an error in this case because pet_loc_dummy does
* not have a reference to a valid isl_ctx.
*/
__isl_give pet_loc *pet_loc_cow(__isl_take pet_loc *loc)
{
if (loc == &pet_loc_dummy)
return NULL;
if (!loc)
return NULL;
if (loc->ref == 1)
return loc;
loc->ref--;
return pet_loc_alloc(loc->ctx, loc->start, loc->end, loc->line,
strdup(loc->indent));
}
/* Return an extra reference to "loc".
*
* The special pet_loc_dummy object is not reference counted.
*/
__isl_give pet_loc *pet_loc_copy(__isl_keep pet_loc *loc)
{
if (loc == &pet_loc_dummy)
return loc;
if (!loc)
return NULL;
loc->ref++;
return loc;
}
/* Free a reference to "loc" and return NULL.
*
* The special pet_loc_dummy object is not reference counted.
*/
__isl_null pet_loc *pet_loc_free(__isl_take pet_loc *loc)
{
if (loc == &pet_loc_dummy)
return NULL;
if (!loc)
return NULL;
if (--loc->ref > 0)
return NULL;
free(loc->indent);
isl_ctx_deref(loc->ctx);
free(loc);
return NULL;
}
/* Return the offset in the input file of the start of "loc".
*/
unsigned pet_loc_get_start(__isl_keep pet_loc *loc)
{
return loc ? loc->start : 0;
}
/* Return the offset in the input file of the character after "loc".
*/
unsigned pet_loc_get_end(__isl_keep pet_loc *loc)
{
return loc ? loc->end : 0;
}
/* Return the line number of a line within the "loc" region.
*/
int pet_loc_get_line(__isl_keep pet_loc *loc)
{
return loc ? loc->line : -1;
}
/* Return the indentation of the "loc" region.
*/
__isl_keep const char *pet_loc_get_indent(__isl_keep pet_loc *loc)
{
return loc ? loc->indent : NULL;
}
/* Update loc->start and loc->end to include the region from "start"
* to "end".
*
* Since we may be modifying "loc", it should be different from
* pet_loc_dummy.
*/
__isl_give pet_loc *pet_loc_update_start_end(__isl_take pet_loc *loc,
unsigned start, unsigned end)
{
loc = pet_loc_cow(loc);
if (!loc)
return NULL;
if (start < loc->start)
loc->start = start;
if (end > loc->end)
loc->end = end;
return loc;
}
/* Update loc->start and loc->end to include the region of "loc2".
*
* "loc" may be pet_loc_dummy, in which case we return a copy of "loc2".
* Similarly, if "loc2" is pet_loc_dummy, then we leave "loc" untouched.
*/
__isl_give pet_loc *pet_loc_update_start_end_from_loc(__isl_take pet_loc *loc,
__isl_keep pet_loc *loc2)
{
if (!loc2)
return pet_loc_free(loc);
if (loc == &pet_loc_dummy)
return pet_loc_copy(loc2);
if (loc2 == &pet_loc_dummy)
return loc;
return pet_loc_update_start_end(loc, loc2->start, loc2->end);
}
/* Replace the indentation of "loc" by "indent".
*/
__isl_give pet_loc *pet_loc_set_indent(__isl_take pet_loc *loc,
__isl_take char *indent)
{
if (!loc || !indent)
goto error;
free(loc->indent);
loc->indent = indent;
return loc;
error:
free(indent);
return pet_loc_free(loc);
}