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 void
|
|
65 place_setnowhere(struct place *p)
|
|
66 {
|
12
|
67 p->type = P_NOWHERE;
|
8
|
68 p->file = NULL;
|
12
|
69 p->line = 0;
|
8
|
70 p->column = 0;
|
|
71 }
|
|
72
|
|
73 void
|
|
74 place_setbuiltin(struct place *p, unsigned num)
|
|
75 {
|
12
|
76 p->type = P_BUILTIN;
|
8
|
77 p->file = NULL;
|
12
|
78 p->line = num;
|
|
79 p->column = 1;
|
8
|
80 }
|
|
81
|
|
82 void
|
|
83 place_setcommandline(struct place *p, unsigned column)
|
|
84 {
|
|
85 p->file = NULL;
|
|
86 p->line = COMMANDLINE_LINE;
|
|
87 p->column = column;
|
|
88 }
|
|
89
|
|
90 static
|
12
|
91 const char *
|
|
92 place_getname(const struct place *p)
|
8
|
93 {
|
12
|
94 switch (p->type) {
|
|
95 case P_NOWHERE: return "<nowhere>";
|
|
96 case P_BUILTIN: return "<built-in>";
|
|
97 case P_COMMANDLINE: return "<command-line>";
|
|
98 case P_FILE: return p->file->name;
|
8
|
99 }
|
12
|
100 assert(0);
|
|
101 return NULL;
|
8
|
102 }
|
|
103
|
|
104 static
|
|
105 void
|
|
106 place_printfrom(const struct place *p)
|
|
107 {
|
|
108 const struct place *from;
|
|
109
|
10
|
110 from = &p->file->includedfrom;
|
12
|
111 if (from->type != P_NOWHERE) {
|
8
|
112 place_printfrom(from);
|
12
|
113 fprintf(stderr, "In file included from %s:%u:%u:\n",
|
|
114 place_getname(from), from->line, from->column);
|
8
|
115 }
|
|
116 }
|
|
117
|
10
|
118 ////////////////////////////////////////////////////////////
|
|
119 // complaints
|
|
120
|
8
|
121 void
|
|
122 complain(const struct place *p, const char *fmt, ...)
|
|
123 {
|
|
124 va_list ap;
|
|
125
|
12
|
126 place_printfrom(p);
|
|
127 fprintf(stderr, "%s:%u:%u: ", place_getname(p), p->line, p->column);
|
8
|
128 va_start(ap, fmt);
|
|
129 vfprintf(stderr, fmt, ap);
|
|
130 va_end(ap);
|
|
131 fprintf(stderr, "\n");
|
|
132 }
|
|
133
|
|
134 void
|
|
135 complain_fail(void)
|
|
136 {
|
|
137 overall_failure = true;
|
|
138 }
|
|
139
|
|
140 bool
|
|
141 complain_failed(void)
|
|
142 {
|
|
143 return overall_failure;
|
|
144 }
|
|
145
|
10
|
146 ////////////////////////////////////////////////////////////
|
|
147 // module init and cleanup
|
|
148
|
|
149 void
|
|
150 place_init(void)
|
|
151 {
|
|
152 seenfilearray_init(&seenfiles);
|
|
153 }
|
|
154
|
|
155 void
|
|
156 place_cleanup(void)
|
|
157 {
|
|
158 seenfilearray_destroyall(&seenfiles);
|
|
159 seenfilearray_cleanup(&seenfiles);
|
|
160 }
|