30
|
1 /*-
|
|
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
3 * All rights reserved.
|
|
4 *
|
|
5 * This code is derived from software contributed to The NetBSD Foundation
|
|
6 * by David A. Holland.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions
|
|
10 * are met:
|
|
11 * 1. Redistributions of source code must retain the above copyright
|
|
12 * notice, this list of conditions and the following disclaimer.
|
|
13 * 2. Redistributions in binary form must reproduce the above copyright
|
|
14 * notice, this list of conditions and the following disclaimer in the
|
|
15 * documentation and/or other materials provided with the distribution.
|
|
16 *
|
|
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27 * POSSIBILITY OF SUCH DAMAGE.
|
|
28 */
|
|
29
|
8
|
30 #include <assert.h>
|
|
31 #include <stdarg.h>
|
|
32 #include <stdio.h>
|
|
33 #include <stdlib.h>
|
|
34
|
|
35 #include "utils.h"
|
10
|
36 #include "array.h"
|
8
|
37 #include "place.h"
|
|
38
|
13
|
39 struct placefile {
|
10
|
40 struct place includedfrom;
|
|
41 char *name;
|
28
|
42 int depth;
|
10
|
43 bool fromsystemdir;
|
|
44 };
|
13
|
45 DECLARRAY(placefile);
|
|
46 DEFARRAY(placefile, );
|
10
|
47
|
13
|
48 static struct placefilearray placefiles;
|
10
|
49 static bool overall_failure;
|
|
50
|
|
51 ////////////////////////////////////////////////////////////
|
|
52 // seenfiles
|
|
53
|
|
54 static
|
13
|
55 struct placefile *
|
|
56 placefile_create(const struct place *from, const char *name,
|
|
57 bool fromsystemdir)
|
10
|
58 {
|
13
|
59 struct placefile *pf;
|
10
|
60
|
13
|
61 pf = domalloc(sizeof(*pf));
|
|
62 pf->includedfrom = *from;
|
|
63 pf->name = dostrdup(name);
|
|
64 pf->fromsystemdir = fromsystemdir;
|
28
|
65 if (from->file != NULL) {
|
|
66 pf->depth = from->file->depth + 1;
|
|
67 } else {
|
|
68 pf->depth = 1;
|
|
69 }
|
13
|
70 return pf;
|
10
|
71 }
|
|
72
|
|
73 static
|
|
74 void
|
13
|
75 placefile_destroy(struct placefile *pf)
|
10
|
76 {
|
13
|
77 free(pf->name);
|
|
78 free(pf);
|
10
|
79 }
|
|
80
|
13
|
81 DESTROYALL_ARRAY(placefile, );
|
10
|
82
|
13
|
83 const struct placefile *
|
|
84 place_addfile(const struct place *place, const char *file, bool issystem)
|
10
|
85 {
|
13
|
86 struct placefile *pf;
|
10
|
87
|
13
|
88 pf = placefile_create(place, file, issystem);
|
|
89 placefilearray_add(&placefiles, pf, NULL);
|
28
|
90 if (pf->depth > 128) {
|
|
91 complain(place, "Maximum include nesting depth exceeded");
|
|
92 die();
|
|
93 }
|
13
|
94 return pf;
|
10
|
95 }
|
|
96
|
|
97 ////////////////////////////////////////////////////////////
|
|
98 // places
|
|
99
|
8
|
100 void
|
|
101 place_setnowhere(struct place *p)
|
|
102 {
|
12
|
103 p->type = P_NOWHERE;
|
8
|
104 p->file = NULL;
|
12
|
105 p->line = 0;
|
8
|
106 p->column = 0;
|
|
107 }
|
|
108
|
|
109 void
|
|
110 place_setbuiltin(struct place *p, unsigned num)
|
|
111 {
|
12
|
112 p->type = P_BUILTIN;
|
8
|
113 p->file = NULL;
|
12
|
114 p->line = num;
|
|
115 p->column = 1;
|
8
|
116 }
|
|
117
|
|
118 void
|
14
|
119 place_setcommandline(struct place *p, unsigned line, unsigned column)
|
8
|
120 {
|
14
|
121 p->type = P_COMMANDLINE;
|
8
|
122 p->file = NULL;
|
14
|
123 p->line = line;
|
8
|
124 p->column = column;
|
|
125 }
|
|
126
|
14
|
127 void
|
|
128 place_setfilestart(struct place *p, const struct placefile *pf)
|
|
129 {
|
28
|
130 p->type = P_FILE;
|
14
|
131 p->file = pf;
|
|
132 p->line = 1;
|
|
133 p->column = 1;
|
|
134 }
|
|
135
|
8
|
136 static
|
12
|
137 const char *
|
|
138 place_getname(const struct place *p)
|
8
|
139 {
|
12
|
140 switch (p->type) {
|
|
141 case P_NOWHERE: return "<nowhere>";
|
|
142 case P_BUILTIN: return "<built-in>";
|
|
143 case P_COMMANDLINE: return "<command-line>";
|
|
144 case P_FILE: return p->file->name;
|
8
|
145 }
|
12
|
146 assert(0);
|
|
147 return NULL;
|
8
|
148 }
|
|
149
|
|
150 static
|
|
151 void
|
|
152 place_printfrom(const struct place *p)
|
|
153 {
|
|
154 const struct place *from;
|
|
155
|
10
|
156 from = &p->file->includedfrom;
|
12
|
157 if (from->type != P_NOWHERE) {
|
8
|
158 place_printfrom(from);
|
12
|
159 fprintf(stderr, "In file included from %s:%u:%u:\n",
|
|
160 place_getname(from), from->line, from->column);
|
8
|
161 }
|
|
162 }
|
|
163
|
10
|
164 ////////////////////////////////////////////////////////////
|
|
165 // complaints
|
|
166
|
8
|
167 void
|
|
168 complain(const struct place *p, const char *fmt, ...)
|
|
169 {
|
|
170 va_list ap;
|
|
171
|
12
|
172 place_printfrom(p);
|
|
173 fprintf(stderr, "%s:%u:%u: ", place_getname(p), p->line, p->column);
|
8
|
174 va_start(ap, fmt);
|
|
175 vfprintf(stderr, fmt, ap);
|
|
176 va_end(ap);
|
|
177 fprintf(stderr, "\n");
|
|
178 }
|
|
179
|
|
180 void
|
|
181 complain_fail(void)
|
|
182 {
|
|
183 overall_failure = true;
|
|
184 }
|
|
185
|
|
186 bool
|
|
187 complain_failed(void)
|
|
188 {
|
|
189 return overall_failure;
|
|
190 }
|
|
191
|
10
|
192 ////////////////////////////////////////////////////////////
|
|
193 // module init and cleanup
|
|
194
|
|
195 void
|
|
196 place_init(void)
|
|
197 {
|
13
|
198 placefilearray_init(&placefiles);
|
10
|
199 }
|
|
200
|
|
201 void
|
|
202 place_cleanup(void)
|
|
203 {
|
13
|
204 placefilearray_destroyall(&placefiles);
|
|
205 placefilearray_cleanup(&placefiles);
|
10
|
206 }
|