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
|
|
10 #define NOWHERE_LINE 0
|
|
11 #define BUILTIN_LINE 1
|
|
12 #define COMMANDLINE_LINE 2
|
|
13
|
10
|
14 struct seenfile {
|
|
15 struct place includedfrom;
|
|
16 char *name;
|
|
17 bool fromsystemdir;
|
|
18 };
|
|
19 DECLARRAY(seenfile);
|
|
20 DEFARRAY(seenfile, );
|
|
21
|
11
|
22 static struct seenfilearray seenfiles;
|
10
|
23 static bool overall_failure;
|
|
24
|
|
25 ////////////////////////////////////////////////////////////
|
|
26 // seenfiles
|
|
27
|
|
28 static
|
|
29 struct seenfile *
|
|
30 seenfile_create(const struct place *from, char *name, bool fromsystemdir)
|
|
31 {
|
|
32 struct seenfile *sf;
|
|
33
|
|
34 sf = domalloc(sizeof(*sf));
|
|
35 sf->includedfrom = *from;
|
|
36 sf->name = name;
|
|
37 sf->fromsystemdir = fromsystemdir;
|
|
38 return sf;
|
|
39 }
|
|
40
|
|
41 static
|
|
42 void
|
|
43 seenfile_destroy(struct seenfile *sf)
|
|
44 {
|
|
45 free(sf->name);
|
|
46 free(sf);
|
|
47 }
|
|
48
|
|
49 DESTROYALL_ARRAY(seenfile, );
|
|
50
|
|
51 struct seenfile *
|
|
52 place_seen_file(const struct place *place, char *file, bool issystem)
|
|
53 {
|
|
54 struct seenfile *sf;
|
|
55
|
|
56 sf = seenfile_create(place, file, issystem);
|
|
57 seenfilearray_add(&seenfiles, sf, NULL);
|
|
58 return sf;
|
|
59 }
|
|
60
|
|
61 ////////////////////////////////////////////////////////////
|
|
62 // places
|
|
63
|
8
|
64 static
|
|
65 bool
|
|
66 place_isnowhere(const struct place *p)
|
|
67 {
|
|
68 return p->file == NULL && p->line == NOWHERE_LINE;
|
|
69 }
|
|
70
|
|
71 static
|
|
72 bool
|
|
73 place_isbuiltin(const struct place *p)
|
|
74 {
|
|
75 return p->file == NULL && p->line == BUILTIN_LINE;
|
|
76 }
|
|
77
|
|
78 static
|
|
79 bool
|
|
80 place_iscommandline(const struct place *p)
|
|
81 {
|
|
82 return p->file == NULL && p->line == COMMANDLINE_LINE;
|
|
83 }
|
|
84
|
|
85 void
|
|
86 place_setnowhere(struct place *p)
|
|
87 {
|
|
88 p->file = NULL;
|
|
89 p->line = NOWHERE_LINE;
|
|
90 p->column = 0;
|
|
91 }
|
|
92
|
|
93 void
|
|
94 place_setbuiltin(struct place *p, unsigned num)
|
|
95 {
|
|
96 p->file = NULL;
|
|
97 p->line = BUILTIN_LINE;
|
|
98 p->column = num;
|
|
99 }
|
|
100
|
|
101 void
|
|
102 place_setcommandline(struct place *p, unsigned column)
|
|
103 {
|
|
104 p->file = NULL;
|
|
105 p->line = COMMANDLINE_LINE;
|
|
106 p->column = column;
|
|
107 }
|
|
108
|
|
109 static
|
|
110 void
|
|
111 place_print(const struct place *p)
|
|
112 {
|
|
113 if (place_iscommandline(p)) {
|
|
114 fprintf(stderr, "<command-line>:1:%u", p->column);
|
|
115 } else if (place_isbuiltin(p)) {
|
|
116 fprintf(stderr, "<built-in>:%u:1", p->column);
|
|
117 } else {
|
10
|
118 fprintf(stderr, "%s:%u:%u", p->file->name,
|
8
|
119 p->line, p->column);
|
|
120 }
|
|
121 }
|
|
122
|
|
123 static
|
|
124 void
|
|
125 place_printfrom(const struct place *p)
|
|
126 {
|
|
127 const struct place *from;
|
|
128
|
10
|
129 from = &p->file->includedfrom;
|
8
|
130 if (!place_isnowhere(from)) {
|
|
131 place_printfrom(from);
|
|
132 }
|
|
133 fprintf(stderr, "In file included from ");
|
|
134 place_print(p);
|
|
135 fprintf(stderr, ":\n");
|
|
136 }
|
|
137
|
10
|
138 ////////////////////////////////////////////////////////////
|
|
139 // complaints
|
|
140
|
8
|
141 void
|
|
142 complain(const struct place *p, const char *fmt, ...)
|
|
143 {
|
|
144 va_list ap;
|
|
145 const struct place *from;
|
|
146
|
10
|
147 from = &p->file->includedfrom;
|
8
|
148 if (!place_isnowhere(from)) {
|
|
149 place_printfrom(from);
|
|
150 }
|
|
151 place_print(p);
|
|
152 fprintf(stderr, ": ");
|
|
153 va_start(ap, fmt);
|
|
154 vfprintf(stderr, fmt, ap);
|
|
155 va_end(ap);
|
|
156 fprintf(stderr, "\n");
|
|
157 }
|
|
158
|
|
159 void
|
|
160 complain_fail(void)
|
|
161 {
|
|
162 overall_failure = true;
|
|
163 }
|
|
164
|
|
165 bool
|
|
166 complain_failed(void)
|
|
167 {
|
|
168 return overall_failure;
|
|
169 }
|
|
170
|
10
|
171 ////////////////////////////////////////////////////////////
|
|
172 // module init and cleanup
|
|
173
|
|
174 void
|
|
175 place_init(void)
|
|
176 {
|
|
177 seenfilearray_init(&seenfiles);
|
|
178 }
|
|
179
|
|
180 void
|
|
181 place_cleanup(void)
|
|
182 {
|
|
183 seenfilearray_destroyall(&seenfiles);
|
|
184 seenfilearray_cleanup(&seenfiles);
|
|
185 }
|