annotate directive.c @ 49:8a204d153398

Intercept multiline comments earlier. Leave same-line comments alone. Fixes all of the pathological comment behavior we've noticed so far.
author David A. Holland
date Sun, 31 Mar 2013 01:04:43 -0400
parents 337110e7240a
children 90c6052410ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
1 /*-
76c114899f63 copyrights
David A. Holland
parents: 18
diff changeset
2 * Copyright (c) 2010 The NetBSD Foundation, Inc.
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 <stdbool.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
32 #include <stdlib.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
33 #include <string.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
34
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
35 #include "utils.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
36 #include "mode.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
37 #include "place.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
38 #include "files.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
39 #include "directive.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
40 #include "macro.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
41 #include "eval.h"
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
42 #include "output.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
43
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
44 struct ifstate {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
45 struct ifstate *prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
46 struct place startplace;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
47 bool curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
48 bool evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
49 bool seenelse;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
50 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
51
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
52 static struct ifstate *ifstate;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
53 static bool in_multiline_comment;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
54
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
55 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
56 // common parsing bits
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
57
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
58 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
59 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
60 oneword(const char *what, struct place *p2, char *line)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
61 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
62 size_t pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
63
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
64 pos = strcspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
65 if (line[pos] != '\0') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
66 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
67 complain(p2, "Garbage after %s argument", what);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
68 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
69 line[pos] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
70 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
71 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
72
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
73 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
74 // if handling
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
75
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
76 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
77 struct ifstate *
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
78 ifstate_create(struct ifstate *prev, struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
79 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
80 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
81
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
82 is = domalloc(sizeof(*is));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
83 is->prev = prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
84 if (p != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
85 is->startplace = *p;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
86 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
87 place_setbuiltin(&is->startplace, 1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
88 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
89 is->curtrue = startstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
90 is->evertrue = is->curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
91 is->seenelse = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
92 return is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
93 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
94
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
95 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
96 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
97 ifstate_destroy(struct ifstate *is)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
98 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
99 dofree(is, sizeof(*is));
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
100 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
101
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
102 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
103 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
104 ifstate_push(struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
105 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
106 ifstate = ifstate_create(ifstate, p, startstate);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
107 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
108
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
109 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
110 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
111 ifstate_pop(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
112 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
113 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
114
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
115 is = ifstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
116 ifstate = ifstate->prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
117 ifstate_destroy(is);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
118 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
119
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
120 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
121 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
122 d_if(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
123 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
124 char *expr;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
125 bool val;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
126 struct place p3 = *p2;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
127
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
128 expr = macroexpand(p2, line, len, true);
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
129 val = eval(&p3, expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
130 ifstate_push(p, val);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
131 dostrfree(expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
132 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
133
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
134 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
135 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
136 d_ifdef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
137 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
138 oneword("#ifdef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
139 ifstate_push(p, macro_isdefined(line));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
140 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
141
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
142 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
143 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
144 d_ifndef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
145 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
146 oneword("#ifndef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
147 ifstate_push(p, !macro_isdefined(line));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
148 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
149
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
150 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
151 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
152 d_elif(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
153 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
154 char *expr;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
155 struct place p3 = *p2;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
156
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
157 if (ifstate->seenelse) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
158 complain(p, "#elif after #else");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
159 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
160 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
161
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
162 if (ifstate->evertrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
163 ifstate->curtrue = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
164 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
165 expr = macroexpand(p2, line, len, true);
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
166 ifstate->curtrue = eval(&p3, expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
167 ifstate->evertrue = ifstate->curtrue;
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
168 dostrfree(expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
169 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
170 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
171
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
172 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
173 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
174 d_else(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
175 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
176 if (ifstate->seenelse) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
177 complain(p, "Multiple #else directives in one conditional");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
178 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
179 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
180
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
181 ifstate->curtrue = !ifstate->evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
182 ifstate->evertrue = true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
183 ifstate->seenelse = true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
184 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
185
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
186 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
187 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
188 d_endif(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
189 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
190 if (ifstate->prev == NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
191 complain(p, "Unmatched #endif");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
192 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
193 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
194 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
195 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
196 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
197
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
198 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
199 // macros
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
200
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
201 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
202 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
203 d_define(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
204 {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
205 size_t pos, argpos;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
206 struct place p3, p4;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
207
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
208 /*
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
209 * line may be:
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
210 * macro expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
211 * macro(arg, arg, ...) expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
212 */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
213
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
214 pos = strcspn(line, " \t\f\v(");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
215 if (line[pos] == '(') {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
216 line[pos++] = '\0';
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
217 argpos = pos;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
218 pos = pos + strcspn(line+pos, "()");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
219 if (line[pos] == '(') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
220 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
221 complain(p2, "Left parenthesis in macro parameters");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
222 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
223 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
224 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
225 if (line[pos] != ')') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
226 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
227 complain(p2, "Unclosed macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
228 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
229 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
230 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
231 line[pos++] = '\0';
36
a489cc223483 Don't demand space after the macro argument parenthesis.
David A. Holland
parents: 30
diff changeset
232 #if 0
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
233 if (!strchr(ws, line[pos])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
234 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
235 complain(p2, "Trash after macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
236 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
237 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
238 }
36
a489cc223483 Don't demand space after the macro argument parenthesis.
David A. Holland
parents: 30
diff changeset
239 #endif
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
240 } else if (line[pos] == '\0') {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
241 argpos = 0;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
242 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
243 line[pos++] = '\0';
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
244 argpos = 0;
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 pos += strspn(line+pos, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
248
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
249 p3 = *p2;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
250 p3.column += argpos;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
251
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
252 p4 = *p2;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
253 p4.column += pos;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
254
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
255 if (argpos) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
256 macro_define_params(p2, line, &p3,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
257 line + argpos, &p4,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
258 line + pos);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
259 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
260 macro_define_plain(p2, line, &p4, line + pos);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 16
diff changeset
261 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
262 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
263
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
264 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
265 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
266 d_undef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
267 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
268 oneword("#undef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
269 macro_undef(line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
270 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
271
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
272 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
273 // includes
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
274
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
275 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
276 bool
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
277 tryinclude(struct place *p, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
278 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
279 if (len > 2 && line[0] == '"' && line[len-1] == '"') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
280 line[len-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
281 file_readquote(p, line+1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
282 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
283 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
284 if (len > 2 && line[0] == '<' && line[len-1] == '>') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
285 line[len-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
286 file_readbracket(p, line+1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
287 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
288 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
289 return false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
290 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
291
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
292 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
293 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
294 d_include(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
295 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
296 char *text;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
297
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
298 if (tryinclude(p, line, len)) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
299 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
300 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
301 text = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
302 if (tryinclude(p, text, strlen(text))) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
303 dostrfree(text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
304 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
305 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
306 dostrfree(text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
307 complain(p, "Illegal #include directive");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
308 complain_fail();
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
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
313 d_line(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
314 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
315 /* XXX */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
316 complain(p, "Sorry, no #line yet");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
317 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
318
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
319 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
320 // messages
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
321
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
322 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
323 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
324 d_warning(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
325 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
326 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
327
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
328 msg = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
329 complain(p, "#warning: %s", msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
330 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
331 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
332 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
333 dostrfree(msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
334 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
335
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
336 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
337 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
338 d_error(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
339 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
340 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
341
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
342 msg = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
343 complain(p, "#error: %s", msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
344 complain_fail();
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
345 dostrfree(msg);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
346 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
347
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
348 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
349 // other
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
350
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
351 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
352 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
353 d_pragma(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
354 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
355 complain(p, "#pragma %s", line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
356 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
357 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
358
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
359 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
360 // directive table
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
361
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
362 static const struct {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
363 const char *name;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
364 bool ifskip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
365 void (*func)(struct place *, struct place *, char *line, size_t len);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
366 } directives[] = {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
367 { "define", true, d_define },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
368 { "elif", false, d_elif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
369 { "else", false, d_else },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
370 { "endif", false, d_endif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
371 { "error", true, d_error },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
372 { "if", false, d_if },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
373 { "ifdef", false, d_ifdef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
374 { "ifndef", false, d_ifndef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
375 { "include", true, d_include },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
376 { "line", true, d_line },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
377 { "pragma", true, d_pragma },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
378 { "undef", true, d_undef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
379 { "warning", true, d_warning },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
380 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
381 static const unsigned numdirectives = HOWMANY(directives);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
382
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
383 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
384 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
385 directive_gotdirective(struct place *p, char *line, size_t linelen)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
386 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
387 struct place p2;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
388 size_t len, skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
389 unsigned i;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
390
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
391 p2 = *p;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
392 for (i=0; i<numdirectives; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
393 len = strlen(directives[i].name);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
394 if (!strncmp(line, directives[i].name, len) &&
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
395 strchr(ws, line[len])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
396 if (directives[i].ifskip && !ifstate->curtrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
397 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
398 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
399 skip = len + strspn(line+len, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
400 p2.column += skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
401 line += skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
402 linelen -= skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
403 linelen = notrailingws(line, linelen);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
404 directives[i].func(p, &p2, line, linelen);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
405 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
406 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
407 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
408 skip = strcspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
409 complain(p, "Unknown directive #%.*s", (int)skip, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
410 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
411 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
412
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
413 /*
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
414 * If desired, warn about a nested comment. The comment begins at
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
415 * offset POS from the place P.
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
416 */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
417 static
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
418 void
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
419 warn_nestcomment(const struct place *p, size_t pos)
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
420 {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
421 struct place p2;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
422
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
423 if (warns.nestcomment) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
424 p2 = *p;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
425 p2.column += pos;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
426 complain(p, "Warning: %c%c within comment",
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
427 '/', '*');
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
428 if (mode.werror) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
429 complain_failed();
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
430 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
431 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
432 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
433
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
434 /*
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
435 * Check for comment delimiters in LINE. If a multi-line comment is
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
436 * continuing or ending, set ACOMM to its length. If a multi-line
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
437 * comment is starting, set BCOMM to its length. Set TEXT to the
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
438 * length of text that is not commented out, or that contains comments
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
439 * that both begin and end on this line. ACOMM + TEXT + BCOMM == LEN.
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
440 *
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
441 * Updates in_multiline_comment to the appropriate state for after
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
442 * this line is handled.
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
443 */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
444 static
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
445 size_t
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
446 directive_scancomments(const struct place *p, char *line, size_t len,
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
447 size_t *acomm, size_t *text, size_t *bcomm)
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
448 {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
449 size_t pos;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
450 size_t first_commentend;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
451 size_t last_commentstart;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
452 bool incomment;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
453
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
454 first_commentend = len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
455 last_commentstart = len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
456 incomment = in_multiline_comment;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
457 for (pos = 0; pos+1 < len; pos++) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
458 if (line[pos] == '/' && line[pos+1] == '*') {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
459 if (incomment) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
460 warn_nestcomment(p, pos);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
461 } else {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
462 incomment = true;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
463 last_commentstart = pos;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
464 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
465 } else if (line[pos] == '*' && line[pos+1] == '/') {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
466 if (incomment) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
467 incomment = false;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
468 if (first_commentend == len) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
469 first_commentend = pos;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
470 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
471 last_commentstart = len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
472 } else {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
473 /* stray end-comment; should we care? */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
474 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
475 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
476 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
477
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
478 if (in_multiline_comment && first_commentend < last_commentstart) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
479 /* multiline comment ends */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
480 /* first_commentend points to the star, adjust */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
481 *acomm = first_commentend + 2;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
482 *text = len - *acomm;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
483 } else if (in_multiline_comment) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
484 /* comment did not end, so another one cannot have started */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
485 assert(last_commentstart == len);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
486 *acomm = len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
487 *text = 0;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
488 } else {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
489 *acomm = 0;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
490 *text = len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
491 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
492
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
493 *bcomm = len - last_commentstart;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
494 *text -= *bcomm;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
495
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
496 in_multiline_comment = incomment;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
497 return len;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
498 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
499
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
500 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
501 directive_gotline(struct place *p, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
502 {
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
503 size_t acomm; /* length of comment ending on this line */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
504 size_t text; /* length of non-multi-line-comment text */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
505 size_t bcomm; /* length of comment beginning on this line */
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
506 size_t skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
507
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
508 directive_scancomments(p, line, len, &acomm, &text, &bcomm);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
509
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
510 if (acomm > 0) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
511 if (mode.output_retain_comments && ifstate->curtrue) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
512 /*
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
513 * Do not expand the comment; send it straight
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
514 * to the output. This will cause it to appear
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
515 * first if we're partway through collecting a
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
516 * macro argument. Too bad. This isn't a
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
517 * standard mode anyway.
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
518 */
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
519 output(p, line, acomm);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
520 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
521 p->column += acomm;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
522 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
523
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
524 /* check if we have a directive line */
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
525 skip = strspn(line + acomm, ws);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
526 if (acomm == 0 && line[skip] == '#') {
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
527 skip = skip + 1 + strspn(line + skip + 1, ws);
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
528 assert(skip <= text);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
529 p->column += skip;
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
530 directive_gotdirective(p, line+skip, text-skip);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
531 p->column += text-skip;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
532 } else if (ifstate->curtrue) {
49
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
533 macro_sendline(p, line + acomm, text);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
534 p->column += text;
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
535 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
536
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
537 if (bcomm > 0) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
538 if (mode.output_retain_comments && ifstate->curtrue) {
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
539 output(p, line + acomm + text, bcomm);
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
540 }
8a204d153398 Intercept multiline comments earlier. Leave same-line comments alone.
David A. Holland
parents: 39
diff changeset
541 p->column += bcomm;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
542 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
543 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
544
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
545 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
546 directive_goteof(struct place *p)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
547 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
548 while (ifstate->prev != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
549 complain(p, "Missing #endif");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
550 complain(&ifstate->startplace, "...opened at this point");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
551 complain_failed();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
552 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
553 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
554 macro_sendeof(p);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
555 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
556
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
557 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
558 // module initialization
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
559
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
560 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
561 directive_init(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
562 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
563 ifstate = ifstate_create(NULL, NULL, true);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
564 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
565
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
566 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
567 directive_cleanup(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
568 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
569 assert(ifstate->prev == NULL);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
570 ifstate_destroy(ifstate);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
571 ifstate = NULL;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
572 }