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