annotate directive.c @ 203:3a25180d3a5c

Abort on line numbering or column numbering overflow. Line numbers are limited to values that fit in "unsigned int". Also reject input lines longer than 2^32-1 characters. It seems reasonable to presume that any input that violates these constraints is someone screwing around and not a serious attempt to compile or preprocess anything useful. Done in response to n2129, but without getting into any of the silliness found there.
author David A. Holland
date Tue, 01 Aug 2017 14:51:04 -0400
parents 1d2bad7151f9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
1 /*-
99
60184aa42604 add 2013 to copyrights where it seems warranted
David A. Holland
parents: 90
diff changeset
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
30
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
3 * All rights reserved.
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
4 *
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
6 * by David A. Holland.
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
7 *
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
8 * Redistribution and use in source and binary forms, with or without
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
9 * modification, are permitted provided that the following conditions
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
10 * are met:
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
12 * notice, this list of conditions and the following disclaimer.
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
15 * documentation and/or other materials provided with the distribution.
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
16 *
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
28 */
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
29
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
30 #include <assert.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
31 #include <stdlib.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
32 #include <string.h>
173
6ff17ab68b16 directive.c needs limits.h
David A. Holland
parents: 165
diff changeset
33 #include <limits.h>
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
34 #include <errno.h>
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
35
183
4c3375895c6e Don't use <stdbool.h> unless __STDC__ is large enough.
David A. Holland
parents: 176
diff changeset
36 #include "bool.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
37 #include "utils.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
38 #include "mode.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
39 #include "place.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
40 #include "files.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
41 #include "directive.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
42 #include "macro.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
43 #include "eval.h"
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
44 #include "output.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
45
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
46 struct ifstate {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
47 struct ifstate *prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
48 struct place startplace;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
49 bool curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
50 bool evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
51 bool seenelse;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
52 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
53
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
54 static struct ifstate *ifstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
55
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
56 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
57 // common parsing bits
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
58
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
59 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
60 void
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
61 uncomment(char *buf)
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
62 {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
63 char *s, *t, *u = NULL;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
64 bool incomment = false;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
65 bool inesc = false;
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
66 bool inquote = false;
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
67 char quote = '\0';
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
68
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
69 for (s = t = buf; *s; s++) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
70 if (incomment) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
71 if (s[0] == '*' && s[1] == '/') {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
72 s++;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
73 incomment = false;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
74 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
75 } else {
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
76 if (!inquote && s[0] == '/' && s[1] == '*') {
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
77 incomment = true;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
78 } else {
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
79 if (inesc) {
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
80 inesc = false;
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
81 } else if (s[0] == '\\') {
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
82 inesc = true;
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
83 } else if (!inquote &&
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
84 (s[0] == '"' || s[0] == '\'')) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
85 inquote = true;
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
86 quote = s[0];
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
87 } else if (inquote && s[0] == quote) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 127
diff changeset
88 inquote = false;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
89 }
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 77
diff changeset
90
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
91 if (t != s) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
92 *t = *s;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
93 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
94 if (!strchr(ws, *t)) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
95 u = t;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
96 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
97 t++;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
98 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
99 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
100 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
101 if (u) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
102 /* end string after last non-whitespace char */
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
103 u[1] = '\0';
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
104 } else {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
105 *t = '\0';
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
106 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
107 }
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
108
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
109 static
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
110 void
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
111 oneword(const char *what, struct place *p2, char *line)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
112 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
113 size_t pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
114
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
115 pos = strcspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
116 if (line[pos] != '\0') {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
117 place_addcolumns(p2, pos);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
118 complain(p2, "Garbage after %s argument", what);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
119 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
120 line[pos] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
121 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
122 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
123
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
124 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
125 // if handling
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
126
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
127 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
128 struct ifstate *
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
129 ifstate_create(struct ifstate *prev, struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
130 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
131 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
132
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
133 is = domalloc(sizeof(*is));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
134 is->prev = prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
135 if (p != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
136 is->startplace = *p;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
137 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
138 place_setbuiltin(&is->startplace, 1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
139 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
140 is->curtrue = startstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
141 is->evertrue = is->curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
142 is->seenelse = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
143 return is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
144 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
145
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
146 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
147 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
148 ifstate_destroy(struct ifstate *is)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
149 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
150 dofree(is, sizeof(*is));
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
151 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
152
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
153 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
154 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
155 ifstate_push(struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
156 {
72
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
157 struct ifstate *newstate;
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
158
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
159 newstate = ifstate_create(ifstate, p, startstate);
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
160 if (!ifstate->curtrue) {
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
161 newstate->curtrue = false;
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
162 newstate->evertrue = true;
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
163 }
b1d0f10e8d36 handle nested ifs correctly
David A. Holland
parents: 66
diff changeset
164 ifstate = newstate;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
165 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
166
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
167 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
168 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
169 ifstate_pop(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
170 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
171 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
172
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
173 is = ifstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
174 ifstate = ifstate->prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
175 ifstate_destroy(is);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
176 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
177
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
178 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
179 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
180 d_if(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
181 {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
182 bool doprint;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
183 char *expr;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
184 bool val;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
185 struct place p3 = *p2;
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
186 size_t oldlen;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
187
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
188 doprint = ifstate->curtrue;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
189
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
190 expr = macroexpand(p2, line, strlen(line), true);
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
191
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
192 oldlen = strlen(expr);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
193 uncomment(expr);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
194 /* trim to fit, so the malloc debugging won't complain */
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
195 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
196
127
a0a86380456e fix for #if handling:
David A. Holland
parents: 109
diff changeset
197 if (ifstate->curtrue) {
a0a86380456e fix for #if handling:
David A. Holland
parents: 109
diff changeset
198 val = eval(&p3, expr);
a0a86380456e fix for #if handling:
David A. Holland
parents: 109
diff changeset
199 } else {
a0a86380456e fix for #if handling:
David A. Holland
parents: 109
diff changeset
200 val = 0;
a0a86380456e fix for #if handling:
David A. Holland
parents: 109
diff changeset
201 }
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
202 ifstate_push(&lp->current, val);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
203 dostrfree(expr);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
204
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
205 if (doprint) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
206 debuglog(&lp->current, "#if: %s",
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
207 ifstate->curtrue ? "taken" : "not taken");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
208 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
209 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
210
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
211 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
212 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
213 d_ifdef(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
214 {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
215 bool doprint;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
216
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
217 doprint = ifstate->curtrue;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
218
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
219 uncomment(line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
220 oneword("#ifdef", p2, line);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
221 ifstate_push(&lp->current, macro_isdefined(line));
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
222
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
223 if (doprint) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
224 debuglog(&lp->current, "#ifdef %s: %s",
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
225 line, ifstate->curtrue ? "taken" : "not taken");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
226 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
227 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
228
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
229 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
230 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
231 d_ifndef(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
232 {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
233 bool doprint;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
234
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
235 doprint = ifstate->curtrue;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
236
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
237 uncomment(line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
238 oneword("#ifndef", p2, line);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
239 ifstate_push(&lp->current, !macro_isdefined(line));
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
240
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
241 if (doprint) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
242 debuglog(&lp->current, "#ifndef %s: %s",
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
243 line, ifstate->curtrue ? "taken" : "not taken");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
244 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
245 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
246
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
247 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
248 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
249 d_elif(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
250 {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
251 bool doprint;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
252 char *expr;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
253 struct place p3 = *p2;
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
254 size_t oldlen;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
255
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
256 if (ifstate->seenelse) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
257 complain(&lp->current, "#elif after #else");
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
258 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
259 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
260
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
261 doprint = ifstate->curtrue;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
262
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
263 if (ifstate->evertrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
264 ifstate->curtrue = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
265 } else {
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
266 expr = macroexpand(p2, line, strlen(line), true);
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
267
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
268 oldlen = strlen(expr);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
269 uncomment(expr);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
270 /* trim to fit, so the malloc debugging won't complain */
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
271 expr = dorealloc(expr, oldlen + 1, strlen(expr) + 1);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
272
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
273 ifstate->curtrue = eval(&p3, expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
274 ifstate->evertrue = ifstate->curtrue;
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
275 dostrfree(expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
276 }
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
277
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
278 if (doprint) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
279 debuglog2(&lp->current, &ifstate->startplace, "#elif: %s",
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
280 ifstate->curtrue ? "taken" : "not taken");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
281 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
282 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
283
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
284 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
285 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
286 d_else(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
287 {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
288 bool doprint;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
289
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
290 (void)p2;
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
291 (void)line;
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
292
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
293 if (ifstate->seenelse) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
294 complain(&lp->current,
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
295 "Multiple #else directives in one conditional");
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
296 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
297 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
298
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
299 doprint = ifstate->curtrue;
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
300
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
301 ifstate->curtrue = !ifstate->evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
302 ifstate->evertrue = true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
303 ifstate->seenelse = true;
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
304
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
305 if (doprint) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
306 debuglog2(&lp->current, &ifstate->startplace, "#else: %s",
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
307 ifstate->curtrue ? "taken" : "not taken");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
308 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
309 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
310
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
311 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
312 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
313 d_endif(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
314 {
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
315 (void)p2;
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
316 (void)line;
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
317
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
318 if (ifstate->prev == NULL) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
319 complain(&lp->current, "Unmatched #endif");
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
320 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
321 } else {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
322 debuglog2(&lp->current, &ifstate->startplace, "#endif");
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
323 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
324 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
325 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
326
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
327 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
328 // macros
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
329
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
330 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
331 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
332 d_define(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
333 {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
334 size_t pos, argpos;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
335 struct place p3, p4;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
336
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
337 (void)lp;
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
338
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
339 /*
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
340 * line may be:
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
341 * macro expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
342 * macro(arg, arg, ...) expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
343 */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
344
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
345 pos = strcspn(line, " \t\f\v(");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
346 if (line[pos] == '(') {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
347 line[pos++] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
348 argpos = pos;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
349 pos = pos + strcspn(line+pos, "()");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
350 if (line[pos] == '(') {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
351 place_addcolumns(p2, pos);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
352 complain(p2, "Left parenthesis in macro parameters");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
353 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
354 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
355 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
356 if (line[pos] != ')') {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
357 place_addcolumns(p2, pos);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
358 complain(p2, "Unclosed macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
359 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
360 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
361 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
362 line[pos++] = '\0';
36
a489cc223483 Don't demand space after the macro argument parenthesis.
David A. Holland
parents: 30
diff changeset
363 #if 0
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
364 if (!strchr(ws, line[pos])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
365 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
366 complain(p2, "Trash after macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
367 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
368 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
369 }
36
a489cc223483 Don't demand space after the macro argument parenthesis.
David A. Holland
parents: 30
diff changeset
370 #endif
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
371 } else if (line[pos] == '\0') {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
372 argpos = 0;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
373 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
374 line[pos++] = '\0';
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
375 argpos = 0;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
376 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
377
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
378 pos += strspn(line+pos, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
379
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
380 p3 = *p2;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
381 place_addcolumns(&p3, argpos);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
382
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
383 p4 = *p2;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
384 place_addcolumns(&p4, pos);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
385
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
386 if (argpos) {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
387 debuglog(&lp->current, "Defining %s()", line);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
388 macro_define_params(p2, line, &p3,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
389 line + argpos, &p4,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
390 line + pos);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
391 } else {
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
392 debuglog(&lp->current, "Defining %s", line);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
393 macro_define_plain(p2, line, &p4, line + pos);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
394 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
395 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
396
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
397 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
398 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
399 d_undef(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
400 {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
401 (void)lp;
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
402
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
403 uncomment(line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
404 oneword("#undef", p2, line);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
405 debuglog(&lp->current, "Undef %s", line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
406 macro_undef(line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
407 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
408
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
409 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
410 // includes
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
411
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
412 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
413 bool
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
414 tryinclude(struct place *p, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
415 {
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
416 size_t len;
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
417
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
418 len = strlen(line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
419 if (len > 2 && line[0] == '"' && line[len-1] == '"') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
420 line[len-1] = '\0';
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
421 debuglog(p, "Entering include file \"%s\"", line+1);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
422 file_readquote(p, line+1);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
423 debuglog(p, "Leaving include file \"%s\"", line+1);
62
90c6052410ce Don't truncate the candidate include path strings.
David A. Holland
parents: 49
diff changeset
424 line[len-1] = '"';
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
425 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
426 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
427 if (len > 2 && line[0] == '<' && line[len-1] == '>') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
428 line[len-1] = '\0';
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
429 debuglog(p, "Entering include file <%s>", line+1);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
430 file_readbracket(p, line+1);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 183
diff changeset
431 debuglog(p, "Leaving include file <%s>", line+1);
62
90c6052410ce Don't truncate the candidate include path strings.
David A. Holland
parents: 49
diff changeset
432 line[len-1] = '>';
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
433 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
434 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
435 return false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
436 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
437
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
438 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
439 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
440 d_include(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
441 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
442 char *text;
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
443 size_t oldlen;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
444
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
445 uncomment(line);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
446 if (tryinclude(&lp->current, line)) {
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
447 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
448 }
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
449 text = macroexpand(p2, line, strlen(line), false);
82
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
450
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
451 oldlen = strlen(text);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
452 uncomment(text);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
453 /* trim to fit, so the malloc debugging won't complain */
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
454 text = dorealloc(text, oldlen + 1, strlen(text) + 1);
05a94332f08b In #if/#elif, prune comments *after* macro expansion.
David A. Holland
parents: 81
diff changeset
455
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
456 if (tryinclude(&lp->current, text)) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
457 dostrfree(text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
458 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
459 }
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
460 complain(&lp->current, "Illegal #include directive");
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
461 complain(&lp->current, "Before macro expansion: #include %s", line);
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
462 complain(&lp->current, "After macro expansion: #include %s", text);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
463 dostrfree(text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
464 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
465 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
466
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
467 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
468 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
469 d_line(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
470 {
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
471 char *text;
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
472 size_t oldlen;
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
473 unsigned long val;
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
474 char *moretext;
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
475 size_t moretextlen;
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
476 char *filename;
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
477
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
478 text = macroexpand(p2, line, strlen(line), true);
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
479
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
480 oldlen = strlen(text);
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
481 uncomment(text);
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
482 /* trim to fit, so the malloc debugging won't complain */
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
483 text = dorealloc(text, oldlen + 1, strlen(text) + 1);
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
484
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
485 /*
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
486 * What we should have here: either 1234 "file.c",
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
487 * or just 1234.
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
488 */
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
489
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
490 errno = 0;
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
491 val = strtoul(text, &moretext, 10);
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
492 if (errno) {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
493 complain(&lp->current,
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
494 "Invalid line number in #line directive");
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
495 goto fail;
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
496 }
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
497 #if UINT_MAX < ULONG_MAX
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
498 if (val > UINT_MAX) {
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
499 complain(&lp->current,
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
500 "Line number in #line directive too large");
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
501 goto fail;
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
502 }
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
503 #endif
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
504 moretext += strspn(moretext, ws);
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
505 moretextlen = strlen(moretext);
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
506 place_addcolumns(&lp->current, moretext - text);
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
507
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
508 if (moretextlen > 2 &&
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
509 moretext[0] == '"' && moretext[moretextlen-1] == '"') {
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
510 filename = dostrndup(moretext+1, moretextlen-2);
176
a2f047301c15 Replace Joerg's place_setfile with something that at least sort of works.
David A. Holland
parents: 175
diff changeset
511 place_changefile(&lp->nextline, filename);
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
512 dostrfree(filename);
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
513 }
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
514 else if (moretextlen > 0) {
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
515 complain(&lp->current,
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
516 "Invalid file name in #line directive");
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
517 goto fail;
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
518 }
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
519
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
520 lp->nextline.line = val;
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
521 dostrfree(text);
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
522 return;
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
523
165
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
524 fail:
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
525 complain(&lp->current, "Before macro expansion: #line %s", line);
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
526 complain(&lp->current, "After macro expansion: #line %s", text);
cc6d6f27d6ee Fix Joerg's #line code.
David A. Holland
parents: 164
diff changeset
527 complain_fail();
160
d6e6b3940780 Fully implement #line.
Joerg Sonnenberger <joerg@bec.de>
parents: 128
diff changeset
528 dostrfree(text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
529 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
530
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
531 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
532 // messages
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
533
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
534 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
535 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
536 d_warning(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
537 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
538 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
539
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
540 msg = macroexpand(p2, line, strlen(line), false);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
541 complain(&lp->current, "#warning: %s", msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
542 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
543 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
544 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
545 dostrfree(msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
546 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
547
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
548 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
549 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
550 d_error(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
551 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
552 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
553
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
554 msg = macroexpand(p2, line, strlen(line), false);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
555 complain(&lp->current, "#error: %s", msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
556 complain_fail();
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
557 dostrfree(msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
558 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
559
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
560 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
561 // other
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
562
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
563 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
564 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
565 d_pragma(struct lineplace *lp, struct place *p2, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
566 {
103
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
567 (void)p2;
343af355df1b Pass -Wunused.
David A. Holland
parents: 99
diff changeset
568
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
569 complain(&lp->current, "#pragma %s", line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
570 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
571 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
572
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
573 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
574 // directive table
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
575
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
576 static const struct {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
577 const char *name;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
578 bool ifskip;
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
579 void (*func)(struct lineplace *, struct place *, char *line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
580 } directives[] = {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
581 { "define", true, d_define },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
582 { "elif", false, d_elif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
583 { "else", false, d_else },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
584 { "endif", false, d_endif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
585 { "error", true, d_error },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
586 { "if", false, d_if },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
587 { "ifdef", false, d_ifdef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
588 { "ifndef", false, d_ifndef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
589 { "include", true, d_include },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
590 { "line", true, d_line },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
591 { "pragma", true, d_pragma },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
592 { "undef", true, d_undef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
593 { "warning", true, d_warning },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
594 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
595 static const unsigned numdirectives = HOWMANY(directives);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
596
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
597 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
598 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
599 directive_gotdirective(struct lineplace *lp, char *line)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
600 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
601 struct place p2;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
602 size_t len, skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
603 unsigned i;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
604
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
605 p2 = lp->current;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
606 for (i=0; i<numdirectives; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
607 len = strlen(directives[i].name);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
608 if (!strncmp(line, directives[i].name, len) &&
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
609 strchr(ws, line[len])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
610 if (directives[i].ifskip && !ifstate->curtrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
611 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
612 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
613 skip = len + strspn(line+len, ws);
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
614 place_addcolumns(&p2, skip);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
615 line += skip;
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
616
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
617 len = strlen(line);
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
618 len = notrailingws(line, len);
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
619 if (len < strlen(line)) {
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
620 line[len] = '\0';
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
621 }
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
622 directives[i].func(lp, &p2, line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
623 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
624 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
625 }
83
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
626 /* ugh. allow # by itself, including with a comment after it */
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
627 uncomment(line);
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
628 if (line[0] == '\0') {
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
629 return;
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
630 }
3e505c16b0b0 Accept # by itself, including with a comment after it.
David A. Holland
parents: 82
diff changeset
631
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
632 skip = strcspn(line, ws);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
633 complain(&lp->current, "Unknown directive #%.*s", (int)skip, line);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
634 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
635 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
636
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
637 /*
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
638 * Check for nested comment delimiters in LINE.
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
639 */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
640 static
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
641 size_t
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
642 directive_scancomments(const struct lineplace *lp, char *line, size_t len)
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
643 {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
644 size_t pos;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
645 bool incomment;
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
646 struct place p2;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
647
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
648 p2 = lp->current;
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
649 incomment = 0;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
650 for (pos = 0; pos+1 < len; pos++) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
651 if (line[pos] == '/' && line[pos+1] == '*') {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
652 if (incomment) {
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
653 complain(&p2, "Warning: %c%c within comment",
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
654 '/', '*');
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
655 if (mode.werror) {
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
656 complain_failed();
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
657 }
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
658 } else {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
659 incomment = true;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
660 }
109
4483a14ee101 Make -Wcomment work again
David A. Holland
parents: 103
diff changeset
661 pos++;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
662 } else if (line[pos] == '*' && line[pos+1] == '/') {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
663 if (incomment) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
664 incomment = false;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
665 } else {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
666 /* stray end-comment; should we care? */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
667 }
109
4483a14ee101 Make -Wcomment work again
David A. Holland
parents: 103
diff changeset
668 pos++;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
669 }
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
670 if (line[pos] == '\n') {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
671 place_addlines(&p2, 1);
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
672 p2.column = 0;
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
673 } else {
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
674 place_addcolumns(&p2, 1);
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
675 }
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
676 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
677
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
678 /* multiline comments are supposed to arrive in a single buffer */
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
679 assert(!incomment);
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
680 return len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
681 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
682
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
683 void
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
684 directive_gotline(struct lineplace *lp, char *line, size_t len)
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
685 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
686 size_t skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
687
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
688 if (warns.nestcomment) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
689 directive_scancomments(lp, line, len);
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
690 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
691
66
f8507e5ed84c Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents: 64
diff changeset
692 /* check if we have a directive line (# exactly in column 0) */
175
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 173
diff changeset
693 if (len > 0 && line[0] == '#') {
66
f8507e5ed84c Recognize directive lines only when the # is exactly in column 0.
David A. Holland
parents: 64
diff changeset
694 skip = 1 + strspn(line + 1, ws);
77
123168887da8 Clean out old not-really-working nested comment handling.
David A. Holland
parents: 72
diff changeset
695 assert(skip <= len);
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
696 place_addcolumns(&lp->current, skip);
64
f50b4ea6cbfe Prune single-line comments from (most) directive lines.
David A. Holland
parents: 62
diff changeset
697 assert(line[len] == '\0');
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
698 directive_gotdirective(lp, line+skip /*, length = len-skip */);
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
699 place_addcolumns(&lp->current, len-skip);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
700 } else if (ifstate->curtrue) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 128
diff changeset
701 macro_sendline(&lp->current, line, len);
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
702 place_addcolumns(&lp->current, len);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
703 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
704 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
705
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
706 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
707 directive_goteof(struct place *p)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
708 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
709 while (ifstate->prev != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
710 complain(p, "Missing #endif");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
711 complain(&ifstate->startplace, "...opened at this point");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
712 complain_failed();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
713 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
714 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
715 macro_sendeof(p);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
716 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
717
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
718 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
719 // module initialization
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
720
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
721 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
722 directive_init(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
723 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
724 ifstate = ifstate_create(NULL, NULL, true);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
725 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
726
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
727 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
728 directive_cleanup(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
729 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
730 assert(ifstate->prev == NULL);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
731 ifstate_destroy(ifstate);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
732 ifstate = NULL;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
733 }