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