8
|
1 #include <assert.h>
|
|
2 #include <stdarg.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5
|
|
6 #include "utils.h"
|
10
|
7 #include "array.h"
|
8
|
8 #include "place.h"
|
|
9
|
13
|
10 struct placefile {
|
10
|
11 struct place includedfrom;
|
|
12 char *name;
|
28
|
13 int depth;
|
10
|
14 bool fromsystemdir;
|
|
15 };
|
13
|
16 DECLARRAY(placefile);
|
|
17 DEFARRAY(placefile, );
|
10
|
18
|
13
|
19 static struct placefilearray placefiles;
|
10
|
20 static bool overall_failure;
|
|
21
|
|
22 ////////////////////////////////////////////////////////////
|
|
23 // seenfiles
|
|
24
|
|
25 static
|
13
|
26 struct placefile *
|
|
27 placefile_create(const struct place *from, const char *name,
|
|
28 bool fromsystemdir)
|
10
|
29 {
|
13
|
30 struct placefile *pf;
|
10
|
31
|
13
|
32 pf = domalloc(sizeof(*pf));
|
|
33 pf->includedfrom = *from;
|
|
34 pf->name = dostrdup(name);
|
|
35 pf->fromsystemdir = fromsystemdir;
|
28
|
36 if (from->file != NULL) {
|
|
37 pf->depth = from->file->depth + 1;
|
|
38 } else {
|
|
39 pf->depth = 1;
|
|
40 }
|
13
|
41 return pf;
|
10
|
42 }
|
|
43
|
|
44 static
|
|
45 void
|
13
|
46 placefile_destroy(struct placefile *pf)
|
10
|
47 {
|
13
|
48 free(pf->name);
|
|
49 free(pf);
|
10
|
50 }
|
|
51
|
13
|
52 DESTROYALL_ARRAY(placefile, );
|
10
|
53
|
13
|
54 const struct placefile *
|
|
55 place_addfile(const struct place *place, const char *file, bool issystem)
|
10
|
56 {
|
13
|
57 struct placefile *pf;
|
10
|
58
|
13
|
59 pf = placefile_create(place, file, issystem);
|
|
60 placefilearray_add(&placefiles, pf, NULL);
|
28
|
61 if (pf->depth > 128) {
|
|
62 complain(place, "Maximum include nesting depth exceeded");
|
|
63 die();
|
|
64 }
|
13
|
65 return pf;
|
10
|
66 }
|
|
67
|
|
68 ////////////////////////////////////////////////////////////
|
|
69 // places
|
|
70
|
8
|
71 void
|
|
72 place_setnowhere(struct place *p)
|
|
73 {
|
12
|
74 p->type = P_NOWHERE;
|
8
|
75 p->file = NULL;
|
12
|
76 p->line = 0;
|
8
|
77 p->column = 0;
|
|
78 }
|
|
79
|
|
80 void
|
|
81 place_setbuiltin(struct place *p, unsigned num)
|
|
82 {
|
12
|
83 p->type = P_BUILTIN;
|
8
|
84 p->file = NULL;
|
12
|
85 p->line = num;
|
|
86 p->column = 1;
|
8
|
87 }
|
|
88
|
|
89 void
|
14
|
90 place_setcommandline(struct place *p, unsigned line, unsigned column)
|
8
|
91 {
|
14
|
92 p->type = P_COMMANDLINE;
|
8
|
93 p->file = NULL;
|
14
|
94 p->line = line;
|
8
|
95 p->column = column;
|
|
96 }
|
|
97
|
14
|
98 void
|
|
99 place_setfilestart(struct place *p, const struct placefile *pf)
|
|
100 {
|
28
|
101 p->type = P_FILE;
|
14
|
102 p->file = pf;
|
|
103 p->line = 1;
|
|
104 p->column = 1;
|
|
105 }
|
|
106
|
8
|
107 static
|
12
|
108 const char *
|
|
109 place_getname(const struct place *p)
|
8
|
110 {
|
12
|
111 switch (p->type) {
|
|
112 case P_NOWHERE: return "<nowhere>";
|
|
113 case P_BUILTIN: return "<built-in>";
|
|
114 case P_COMMANDLINE: return "<command-line>";
|
|
115 case P_FILE: return p->file->name;
|
8
|
116 }
|
12
|
117 assert(0);
|
|
118 return NULL;
|
8
|
119 }
|
|
120
|
|
121 static
|
|
122 void
|
|
123 place_printfrom(const struct place *p)
|
|
124 {
|
|
125 const struct place *from;
|
|
126
|
10
|
127 from = &p->file->includedfrom;
|
12
|
128 if (from->type != P_NOWHERE) {
|
8
|
129 place_printfrom(from);
|
12
|
130 fprintf(stderr, "In file included from %s:%u:%u:\n",
|
|
131 place_getname(from), from->line, from->column);
|
8
|
132 }
|
|
133 }
|
|
134
|
10
|
135 ////////////////////////////////////////////////////////////
|
|
136 // complaints
|
|
137
|
8
|
138 void
|
|
139 complain(const struct place *p, const char *fmt, ...)
|
|
140 {
|
|
141 va_list ap;
|
|
142
|
12
|
143 place_printfrom(p);
|
|
144 fprintf(stderr, "%s:%u:%u: ", place_getname(p), p->line, p->column);
|
8
|
145 va_start(ap, fmt);
|
|
146 vfprintf(stderr, fmt, ap);
|
|
147 va_end(ap);
|
|
148 fprintf(stderr, "\n");
|
|
149 }
|
|
150
|
|
151 void
|
|
152 complain_fail(void)
|
|
153 {
|
|
154 overall_failure = true;
|
|
155 }
|
|
156
|
|
157 bool
|
|
158 complain_failed(void)
|
|
159 {
|
|
160 return overall_failure;
|
|
161 }
|
|
162
|
10
|
163 ////////////////////////////////////////////////////////////
|
|
164 // module init and cleanup
|
|
165
|
|
166 void
|
|
167 place_init(void)
|
|
168 {
|
13
|
169 placefilearray_init(&placefiles);
|
10
|
170 }
|
|
171
|
|
172 void
|
|
173 place_cleanup(void)
|
|
174 {
|
13
|
175 placefilearray_destroyall(&placefiles);
|
|
176 placefilearray_cleanup(&placefiles);
|
10
|
177 }
|