comparison place.c @ 8:97243badae69

split place stuff to its own file
author David A. Holland
date Sun, 19 Dec 2010 19:15:55 -0500
parents
children 800f3a560a3b
comparison
equal deleted inserted replaced
7:b8167949474a 8:97243badae69
1 #include <assert.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "utils.h"
7 #include "place.h"
8
9 static bool overall_failure;
10
11 #define NOWHERE_LINE 0
12 #define BUILTIN_LINE 1
13 #define COMMANDLINE_LINE 2
14
15 static struct place scratchplace;
16 static bool scratchplace_inuse;
17
18 static
19 bool
20 place_isnowhere(const struct place *p)
21 {
22 return p->file == NULL && p->line == NOWHERE_LINE;
23 }
24
25 static
26 bool
27 place_isbuiltin(const struct place *p)
28 {
29 return p->file == NULL && p->line == BUILTIN_LINE;
30 }
31
32 static
33 bool
34 place_iscommandline(const struct place *p)
35 {
36 return p->file == NULL && p->line == COMMANDLINE_LINE;
37 }
38
39 struct place *
40 place_gettemporary(void)
41 {
42 assert(!scratchplace_inuse);
43 scratchplace_inuse = true;
44 return &scratchplace;
45 }
46
47 void
48 place_puttemporary(struct place *p)
49 {
50 assert(scratchplace_inuse);
51 assert(p == &scratchplace);
52 scratchplace_inuse = false;
53 }
54
55 struct place *
56 place_create(void)
57 {
58 struct place *p;
59
60 p = domalloc(sizeof(*p));
61 place_setnowhere(p);
62 return p;
63 }
64
65 struct place *
66 place_clone(const struct place *op)
67 {
68 struct place *p;
69
70 p = domalloc(sizeof(*p));
71 *p = *op;
72 return p;
73 }
74
75 void
76 place_destroy(struct place *p)
77 {
78 free(p);
79 }
80
81 void
82 place_setnowhere(struct place *p)
83 {
84 p->file = NULL;
85 p->line = NOWHERE_LINE;
86 p->column = 0;
87 }
88
89 void
90 place_setbuiltin(struct place *p, unsigned num)
91 {
92 p->file = NULL;
93 p->line = BUILTIN_LINE;
94 p->column = num;
95 }
96
97 void
98 place_setcommandline(struct place *p, unsigned column)
99 {
100 p->file = NULL;
101 p->line = COMMANDLINE_LINE;
102 p->column = column;
103 }
104
105 static
106 void
107 place_print(const struct place *p)
108 {
109 if (place_iscommandline(p)) {
110 fprintf(stderr, "<command-line>:1:%u", p->column);
111 } else if (place_isbuiltin(p)) {
112 fprintf(stderr, "<built-in>:%u:1", p->column);
113 } else {
114 fprintf(stderr, "%s:%u:%u", seenfile_getname(p->file),
115 p->line, p->column);
116 }
117 }
118
119 static
120 void
121 place_printfrom(const struct place *p)
122 {
123 const struct place *from;
124
125 from = seenfile_getincludeplace(p->file);
126 if (!place_isnowhere(from)) {
127 place_printfrom(from);
128 }
129 fprintf(stderr, "In file included from ");
130 place_print(p);
131 fprintf(stderr, ":\n");
132 }
133
134 void
135 complain(const struct place *p, const char *fmt, ...)
136 {
137 va_list ap;
138 const struct place *from;
139
140 from = seenfile_getincludeplace(p->file);
141 if (!place_isnowhere(from)) {
142 place_printfrom(from);
143 }
144 place_print(p);
145 fprintf(stderr, ": ");
146 va_start(ap, fmt);
147 vfprintf(stderr, fmt, ap);
148 va_end(ap);
149 fprintf(stderr, "\n");
150 }
151
152 void
153 complain_fail(void)
154 {
155 overall_failure = true;
156 }
157
158 bool
159 complain_failed(void)
160 {
161 return overall_failure;
162 }
163