2 * Copyright (c) 2002 - 2020 Tony Finch <dot@dotat.at>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * unifdef - remove ifdef'ed lines
29 * This code was derived from software contributed to Berkeley by Dave Yost.
30 * It was rewritten to support ANSI C by Tony Finch. The original version
31 * of unifdef carried the 4-clause BSD copyright licence. None of its code
32 * remains in this version (though some of the names remain) so it now
33 * carries a more liberal licence.
36 * provide an option which will append the name of the
37 * appropriate symbol after #else's and #endif's
38 * provide an option which will check symbols after
39 * #else's and #endif's to see that they match their
40 * corresponding #ifdef or #ifndef
42 * These require better buffer handling, which would also make
43 * it possible to handle all "dodgy" directives correctly.
48 static const char copyright[] =
50 "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
51 "@(#) $URL: http://dotat.at/prog/unifdef $\n"
54 /* types of input lines: */
56 LT_TRUEI, /* a true #if with ignore flag */
57 LT_FALSEI, /* a false #if with ignore flag */
58 LT_IF, /* an unknown #if */
59 LT_TRUE, /* a true #if */
60 LT_FALSE, /* a false #if */
61 LT_ELIF, /* an unknown #elif */
62 LT_ELTRUE, /* a true #elif */
63 LT_ELFALSE, /* a false #elif */
65 LT_ENDIF, /* #endif */
66 LT_DODGY, /* flag: directive is not on one line */
67 LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
68 LT_PLAIN, /* ordinary line */
69 LT_EOF, /* end of file */
70 LT_ERROR, /* unevaluable #if */
74 static char const * const linetype_name[] = {
75 "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
76 "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
77 "DODGY TRUEI", "DODGY FALSEI",
78 "DODGY IF", "DODGY TRUE", "DODGY FALSE",
79 "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
80 "DODGY ELSE", "DODGY ENDIF",
81 "PLAIN", "EOF", "ERROR"
84 #define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
85 #define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
87 /* state of #if processing */
90 IS_FALSE_PREFIX, /* false #if followed by false #elifs */
91 IS_TRUE_PREFIX, /* first non-false #(el)if is true */
92 IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
93 IS_FALSE_MIDDLE, /* a false #elif after a pass state */
94 IS_TRUE_MIDDLE, /* a true #elif after a pass state */
95 IS_PASS_ELSE, /* an else after a pass state */
96 IS_FALSE_ELSE, /* an else after a true state */
97 IS_TRUE_ELSE, /* an else after only false states */
98 IS_FALSE_TRAILER, /* #elifs after a true are false */
102 static char const * const ifstate_name[] = {
103 "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
104 "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
105 "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
109 /* state of comment parser */
111 NO_COMMENT = false, /* outside a comment */
112 C_COMMENT, /* in a comment like this one */
113 CXX_COMMENT, /* between // and end of line */
114 STARTING_COMMENT, /* just after slash-backslash-newline */
115 FINISHING_COMMENT, /* star-backslash-newline in a C comment */
116 CHAR_LITERAL, /* inside '' */
117 STRING_LITERAL, /* inside "" */
118 RAW_STRING_LITERAL /* inside R"()" */
121 static char const * const comment_name[] = {
122 "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
125 /* state of preprocessor line parser */
127 LS_START, /* only space and comments on this line */
128 LS_HASH, /* only space, comments, and a hash */
129 LS_DIRTY /* this line can't be a preprocessor line */
132 static char const * const linestate_name[] = {
133 "START", "HASH", "DIRTY"
137 * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
139 #define MAXDEPTH 64 /* maximum #if nesting */
140 #define MAXLINE 4096 /* maximum length of line */
143 * Sometimes when editing a keyword the replacement text is longer, so
144 * we leave some space at the end of the tline buffer to accommodate this.
149 * C17/18 allow 63 characters per macro name, but up to 127 arbitrarily large
153 RB_ENTRY(macro) entry;
156 bool ignore; /* -iDsym or -iUsym */
160 macro_cmp(struct macro *a, struct macro *b)
162 return (strcmp(a->name, b->name));
165 static RB_HEAD(MACROMAP, macro) macro_tree = RB_INITIALIZER(¯o_tree);
166 RB_GENERATE_STATIC(MACROMAP, macro, entry, macro_cmp);
172 static bool compblank; /* -B: compress blank lines */
173 static bool lnblank; /* -b: blank deleted lines */
174 static bool complement; /* -c: do the complement */
175 static bool debugging; /* -d: debugging reports */
176 static bool inplace; /* -m: modify in place */
177 static bool iocccok; /* -e: fewer IOCCC errors */
178 static bool strictlogic; /* -K: keep ambiguous #ifs */
179 static bool killconsts; /* -k: eval constant #ifs */
180 static bool lnnum; /* -n: add #line directives */
181 static bool symlist; /* -s: output symbol list */
182 static bool symdepth; /* -S: output symbol depth */
183 static bool text; /* -t: this is a text file */
185 static FILE *input; /* input file pointer */
186 static const char *filename; /* input file name */
187 static int linenum; /* current line number */
188 static const char *linefile; /* file name for #line */
189 static FILE *output; /* output file pointer */
190 static const char *ofilename; /* output file name */
191 static const char *backext; /* backup extension */
192 static char *tempname; /* avoid splatting input */
194 static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
195 static char *keyword; /* used for editing #elif's */
198 * When processing a file, the output's newline style will match the
199 * input's, and unifdef correctly handles CRLF or LF endings whatever
200 * the platform's native style. The stdio streams are opened in binary
201 * mode to accommodate platforms whose native newline style is CRLF.
202 * When the output isn't a processed input file (when it is error /
203 * debug / diagnostic messages) then unifdef uses native line endings.
206 static const char *newline; /* input file format */
207 static const char newline_unix[] = "\n";
208 static const char newline_crlf[] = "\r\n";
210 static Comment_state incomment; /* comment parser state */
211 static Line_state linestate; /* #if line parser state */
212 static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
213 static bool ignoring[MAXDEPTH]; /* ignore comments state */
214 static int stifline[MAXDEPTH]; /* start of current #if */
215 static int depth; /* current #if nesting */
216 static int delcount; /* count of deleted lines */
217 static unsigned blankcount; /* count of blank lines */
218 static unsigned blankmax; /* maximum recent blankcount */
219 static bool constexpr; /* constant #if expression */
220 static bool zerosyms; /* to format symdepth output */
221 static bool firstsym; /* ditto */
223 static int exitmode; /* exit status mode */
224 static int exitstat; /* program exit status */
225 static bool altered; /* was this file modified? */
227 static void addsym1(bool, bool, char *);
228 static void addsym2(bool, const char *, const char *);
229 static char *astrcat(const char *, const char *);
230 static void cleantemp(void);
231 static void closeio(void);
232 static void debug(const char *, ...);
233 static void debugsym(const char *, const struct macro *);
234 static bool defundef(void);
235 static void defundefile(const char *);
236 static void done(void);
237 static void error(const char *);
238 static struct macro *findsym(const char **);
239 static void flushline(bool);
240 static void hashline(void);
241 static void help(void);
242 static Linetype ifeval(const char **);
243 static void ignoreoff(void);
244 static void ignoreon(void);
245 static void indirectsym(void);
246 static void keywordedit(const char *);
247 static const char *matchsym(const char *, const char *);
248 static void nest(void);
249 static Linetype parseline(void);
250 static void process(void);
251 static void processinout(const char *, const char *);
252 static const char *skipargs(const char *);
253 static const char *skipcomment(const char *);
254 static const char *skiphash(void);
255 static const char *skipline(const char *);
256 static const char *skipsym(const char *);
257 static void state(Ifstate);
258 static void unnest(void);
259 static void usage(void);
260 static void version(void);
261 static const char *xstrdup(const char *, const char *);
263 #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
269 main(int argc, char *argv[])
273 while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
275 case 'i': /* treat stuff controlled by these symbols as text */
277 * For strict backwards-compatibility the U or D
278 * should be immediately after the -i but it doesn't
279 * matter much if we relax that requirement.
283 addsym1(true, true, optarg);
285 addsym1(true, false, optarg);
289 case 'D': /* define a symbol */
290 addsym1(false, true, optarg);
292 case 'U': /* undef a symbol */
293 addsym1(false, false, optarg);
295 case 'I': /* no-op for compatibility with cpp */
297 case 'b': /* blank deleted lines instead of omitting them */
298 case 'l': /* backwards compatibility */
301 case 'B': /* compress blank lines around removed section */
304 case 'c': /* treat -D as -U and vice versa */
310 case 'e': /* fewer errors from dodgy lines */
313 case 'f': /* definitions file */
319 case 'K': /* keep ambiguous #ifs */
322 case 'k': /* process constant #ifs */
325 case 'm': /* modify in place */
328 case 'M': /* modify in place and keep backup */
330 if (strlen(optarg) > 0)
333 case 'n': /* add #line directive after deleted lines */
336 case 'o': /* output to a file */
339 case 's': /* only output list of symbols that control #ifs */
342 case 'S': /* list symbols with their nesting depth */
343 symlist = symdepth = true;
345 case 't': /* don't parse C comments */
352 exitmode = atoi(optarg);
353 if(exitmode < 0 || exitmode > 2)
361 if (compblank && lnblank)
362 errx(2, "-B and -b are mutually exclusive");
363 if (symlist && (ofilename != NULL || inplace || argc > 1))
364 errx(2, "-s only works with one input file");
365 if (argc > 1 && ofilename != NULL)
366 errx(2, "-o cannot be used with multiple input files");
367 if (argc > 1 && !inplace)
368 errx(2, "multiple input files require -m or -M");
369 if (argc == 0 && inplace)
370 errx(2, "-m requires an input file");
373 if (argc == 1 && !inplace && ofilename == NULL)
378 if (ofilename != NULL)
379 processinout(*argv, ofilename);
380 else while (argc-- > 0) {
381 processinout(*argv, *argv);
385 case(0): exit(exitstat);
386 case(1): exit(!exitstat);
388 default: abort(); /* bug */
396 processinout(const char *ifn, const char *ofn)
400 if (ifn == NULL || strcmp(ifn, "-") == 0) {
401 filename = "[stdin]";
403 input = fbinmode(stdin);
407 input = fopen(ifn, "rb");
409 err(2, "can't open %s", ifn);
411 if (strcmp(ofn, "-") == 0) {
412 output = fbinmode(stdout);
416 if (stat(ofn, &st) < 0) {
417 output = fopen(ofn, "wb");
419 err(2, "can't create %s", ofn);
424 tempname = astrcat(ofn, ".XXXXXX");
425 output = mktempmode(tempname, st.st_mode);
427 err(2, "can't create %s", tempname);
431 if (backext != NULL) {
432 char *backname = astrcat(ofn, backext);
433 if (rename(ofn, backname) < 0)
434 err(2, "can't rename \"%s\" to \"%s\"", ofn, backname);
437 /* leave file unmodified if unifdef made no changes */
438 if (!altered && backext == NULL) {
439 if (remove(tempname) < 0)
440 warn("can't remove \"%s\"", tempname);
441 } else if (replace(tempname, ofn) < 0)
442 err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn);
448 * For cleaning up if there is an error.
453 if (tempname != NULL)
458 * Self-identification functions.
464 const char *c = copyright;
479 "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
480 " [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
495 " -Dsym=val define preprocessor symbol with given value\n"
496 " -Dsym define preprocessor symbol with value 1\n"
497 " -Usym preprocessor symbol is undefined\n"
498 " -iDsym=val \\ ignore C strings and comments\n"
499 " -iDsym ) in sections controlled by these\n"
500 " -iUsym / preprocessor symbols\n"
501 " -fpath file containing #define and #undef directives\n"
502 " -b blank lines instead of deleting them\n"
503 " -B compress blank lines around deleted section\n"
504 " -c complement (invert) keep vs. delete\n"
505 " -d debugging mode\n"
506 " -e ignore multiline preprocessor directives\n"
508 " -Ipath extra include file path (ignored)\n"
509 " -K disable && and || short-circuiting\n"
510 " -k process constant #if expressions\n"
511 " -Mext modify in place and keep backups\n"
512 " -m modify input files in place\n"
513 " -n add #line directives to output\n"
514 " -opath output file name\n"
515 " -S list #if control symbols with nesting\n"
516 " -s list #if control symbols\n"
517 " -t ignore C strings and comments\n"
518 " -V print version\n"
519 " -x{012} exit status mode\n"
525 * A state transition function alters the global #if processing state
526 * in a particular way. The table below is indexed by the current
527 * processing state and the type of the current line.
529 * Nesting is handled by keeping a stack of states; some transition
530 * functions increase or decrease the depth. They also maintain the
531 * ignore state on a stack. In some complicated cases they have to
532 * alter the preprocessor directive, as follows.
534 * When we have processed a group that starts off with a known-false
535 * #if/#elif sequence (which has therefore been deleted) followed by a
536 * #elif that we don't understand and therefore must keep, we edit the
537 * latter into a #if to keep the nesting correct. We use memcpy() to
538 * overwrite the 4 byte token "elif" with "if " without a '\0' byte.
540 * When we find a true #elif in a group, the following block will
541 * always be kept and the rest of the sequence after the next #elif or
542 * #else will be discarded. We edit the #elif into a #else and the
543 * following directive to #endif since this has the desired behaviour.
545 * "Dodgy" directives are split across multiple lines, the most common
546 * example being a multi-line comment hanging off the right of the
547 * directive. We can handle them correctly only if there is no change
548 * from printing to dropping (or vice versa) caused by that directive.
549 * If the directive is the first of a group we have a choice between
550 * failing with an error, or passing it through unchanged instead of
551 * evaluating it. The latter is not the default to avoid questions from
552 * users about unifdef unexpectedly leaving behind preprocessor directives.
554 typedef void state_fn(void);
556 /* report an error */
557 static void Eelif (void) { error("Inappropriate #elif"); }
558 static void Eelse (void) { error("Inappropriate #else"); }
559 static void Eendif(void) { error("Inappropriate #endif"); }
560 static void Eeof (void) { error("Premature EOF"); }
561 static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
562 /* plain line handling */
563 static void print (void) { flushline(true); }
564 static void drop (void) { flushline(false); }
565 /* output lacks group's start line */
566 static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
567 static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
568 static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
569 /* print/pass this block */
570 static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
571 static void Pelse (void) { print(); state(IS_PASS_ELSE); }
572 static void Pendif(void) { print(); unnest(); }
573 /* discard this block */
574 static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
575 static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
576 static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
577 static void Dendif(void) { drop(); unnest(); }
578 /* first line of group */
579 static void Fdrop (void) { nest(); Dfalse(); }
580 static void Fpass (void) { nest(); Pelif(); }
581 static void Ftrue (void) { nest(); Strue(); }
582 static void Ffalse(void) { nest(); Sfalse(); }
583 /* variable pedantry for obfuscated lines */
584 static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
585 static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
586 static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
587 /* ignore comments in this block */
588 static void Idrop (void) { Fdrop(); ignoreon(); }
589 static void Itrue (void) { Ftrue(); ignoreon(); }
590 static void Ifalse(void) { Ffalse(); ignoreon(); }
591 /* modify this line */
592 static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
593 static void Mtrue (void) { keywordedit("else"); state(IS_TRUE_MIDDLE); }
594 static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
595 static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
597 static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
599 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
600 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
601 print, done, abort },
602 /* IS_FALSE_PREFIX */
603 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
604 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
607 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
608 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
609 print, Eeof, abort },
611 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
612 Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
613 print, Eeof, abort },
614 /* IS_FALSE_MIDDLE */
615 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
616 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
619 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
620 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
621 print, Eeof, abort },
623 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
624 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
625 print, Eeof, abort },
627 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
628 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
631 { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
632 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
633 print, Eeof, abort },
634 /* IS_FALSE_TRAILER */
635 { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
636 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
638 /*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
639 TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
644 * State machine utility functions
651 ignoring[depth] = ignoring[depth-1];
656 ignoring[depth] = true;
659 keywordedit(const char *replacement)
661 snprintf(keyword, tline + sizeof(tline) - keyword,
662 "%s%s", replacement, newline);
669 if (depth > MAXDEPTH-1)
671 if (depth == MAXDEPTH-1)
672 error("Too many levels of nesting");
674 stifline[depth] = linenum;
690 * The last state transition function. When this is called,
691 * lineval == LT_EOF, so the process() loop will terminate.
697 error("EOF in comment");
702 * Write a line to the output or not, according to command line options.
703 * If writing fails, closeio() will print the error and exit.
710 if (keep ^ complement) {
711 bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
712 if (blankline && compblank && blankcount != blankmax) {
716 if (lnnum && delcount > 0)
718 if (fputs(tline, output) == EOF)
721 blankmax = blankcount = blankline ? blankcount + 1 : 0;
724 if (lnblank && fputs(newline, output) == EOF)
730 if (debugging && fflush(output) == EOF)
735 * Format of #line directives depends on whether we know the input filename.
742 if (linefile == NULL)
743 e = fprintf(output, "#line %d%s", linenum, newline);
745 e = fprintf(output, "#line %d \"%s\"%s",
746 linenum, linefile, newline);
752 * Flush the output and handle errors.
757 /* Tidy up after findsym(). */
758 if (symdepth && !zerosyms)
760 if (output != NULL && (ferror(output) || fclose(output) == EOF))
761 err(2, "%s: can't write to output", filename);
766 * The driver for the state machine.
771 Linetype lineval = LT_PLAIN;
772 /* When compressing blank lines, act as if the file
773 is preceded by a large number of blank lines. */
774 blankmax = blankcount = 1000;
779 while (lineval != LT_EOF) {
780 lineval = parseline();
781 trans_table[ifstate[depth]][lineval]();
782 debug("process line %d %s -> %s depth %d",
783 linenum, linetype_name[lineval],
784 ifstate_name[ifstate[depth]], depth);
790 * Parse a line and determine its type. We keep the preprocessor line
791 * parser state between calls in the global variable linestate, with
792 * help from skipcomment().
798 struct macro *cursym;
800 Comment_state wascomment;
802 wascomment = incomment;
806 if (newline == NULL) {
807 if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
808 newline = newline_crlf;
810 newline = newline_unix;
816 keyword = tline + (cp - tline);
817 if ((cp = matchsym("ifdef", keyword)) != NULL ||
818 (cp = matchsym("ifndef", keyword)) != NULL) {
819 cp = skipcomment(cp);
820 if ((cursym = findsym(&cp)) == NULL)
823 retval = (keyword[2] == 'n')
824 ? LT_FALSE : LT_TRUE;
825 if (cursym->value == NULL)
826 retval = (retval == LT_TRUE)
827 ? LT_FALSE : LT_TRUE;
829 retval = (retval == LT_TRUE)
830 ? LT_TRUEI : LT_FALSEI;
832 } else if ((cp = matchsym("if", keyword)) != NULL)
833 retval = ifeval(&cp);
834 else if ((cp = matchsym("elif", keyword)) != NULL)
835 retval = linetype_if2elif(ifeval(&cp));
836 else if ((cp = matchsym("else", keyword)) != NULL)
838 else if ((cp = matchsym("endif", keyword)) != NULL)
841 cp = skipsym(keyword);
842 /* no way can we deal with a continuation inside a keyword */
843 if (strncmp(cp, "\\\r\n", 3) == 0 ||
844 strncmp(cp, "\\\n", 2) == 0)
850 cp = skipcomment(cp);
853 if (retval == LT_TRUE || retval == LT_FALSE ||
854 retval == LT_TRUEI || retval == LT_FALSEI)
856 if (retval == LT_ELTRUE || retval == LT_ELFALSE)
859 /* the following can happen if the last line of the file lacks a
860 newline or if there is too much whitespace in a directive */
861 if (linestate == LS_HASH) {
862 long len = cp - tline;
863 if (fgets(tline + len, MAXLINE - len, input) == NULL) {
865 err(2, "can't read %s", filename);
866 debug("parser insert newline at EOF", linenum);
867 strcpy(tline + len, newline);
868 cp += strlen(newline);
869 linestate = LS_START;
871 debug("parser concatenate dangling whitespace");
873 cp = skipcomment(cp);
876 if (retval != LT_PLAIN && (wascomment || linestate != LS_START)) {
877 retval = linetype_2dodgy(retval);
878 linestate = LS_DIRTY;
881 debug("parser line %d state %s comment %s line", linenum,
882 comment_name[incomment], linestate_name[linestate]);
887 * These are the binary operators that are supported by the expression
890 static Linetype op_strict(long *p, long v, Linetype at, Linetype bt) {
891 if(at == LT_IF || bt == LT_IF) return (LT_IF);
892 return (*p = v, v ? LT_TRUE : LT_FALSE);
894 static Linetype op_lt(long *p, Linetype at, long a, Linetype bt, long b) {
895 return op_strict(p, a < b, at, bt);
897 static Linetype op_gt(long *p, Linetype at, long a, Linetype bt, long b) {
898 return op_strict(p, a > b, at, bt);
900 static Linetype op_le(long *p, Linetype at, long a, Linetype bt, long b) {
901 return op_strict(p, a <= b, at, bt);
903 static Linetype op_ge(long *p, Linetype at, long a, Linetype bt, long b) {
904 return op_strict(p, a >= b, at, bt);
906 static Linetype op_eq(long *p, Linetype at, long a, Linetype bt, long b) {
907 return op_strict(p, a == b, at, bt);
909 static Linetype op_ne(long *p, Linetype at, long a, Linetype bt, long b) {
910 return op_strict(p, a != b, at, bt);
912 static Linetype op_or(long *p, Linetype at, long a, Linetype bt, long b) {
913 if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
914 return (*p = 1, LT_TRUE);
915 return op_strict(p, a || b, at, bt);
917 static Linetype op_and(long *p, Linetype at, long a, Linetype bt, long b) {
918 if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
919 return (*p = 0, LT_FALSE);
920 return op_strict(p, a && b, at, bt);
922 static Linetype op_blsh(long *p, Linetype at, long a, Linetype bt, long b) {
923 return op_strict(p, a << b, at, bt);
925 static Linetype op_brsh(long *p, Linetype at, long a, Linetype bt, long b) {
926 return op_strict(p, a >> b, at, bt);
928 static Linetype op_add(long *p, Linetype at, long a, Linetype bt, long b) {
929 return op_strict(p, a + b, at, bt);
931 static Linetype op_sub(long *p, Linetype at, long a, Linetype bt, long b) {
932 return op_strict(p, a - b, at, bt);
934 static Linetype op_mul(long *p, Linetype at, long a, Linetype bt, long b) {
935 return op_strict(p, a * b, at, bt);
937 static Linetype op_div(long *p, Linetype at, long a, Linetype bt, long b) {
939 debug("eval division by zero");
942 return op_strict(p, a / b, at, bt);
944 static Linetype op_mod(long *p, Linetype at, long a, Linetype bt, long b) {
945 return op_strict(p, a % b, at, bt);
947 static Linetype op_bor(long *p, Linetype at, long a, Linetype bt, long b) {
948 return op_strict(p, a | b, at, bt);
950 static Linetype op_bxor(long *p, Linetype at, long a, Linetype bt, long b) {
951 return op_strict(p, a ^ b, at, bt);
953 static Linetype op_band(long *p, Linetype at, long a, Linetype bt, long b) {
954 return op_strict(p, a & b, at, bt);
958 * An evaluation function takes three arguments, as follows: (1) a pointer to
959 * an element of the precedence table which lists the operators at the current
960 * level of precedence; (2) a pointer to an integer which will receive the
961 * value of the expression; and (3) a pointer to a char* that points to the
962 * expression to be evaluated and that is updated to the end of the expression
963 * when evaluation is complete. The function returns LT_FALSE if the value of
964 * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
965 * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
969 typedef Linetype eval_fn(const struct ops *, long *, const char **);
971 static eval_fn eval_table, eval_unary;
974 * The precedence table. Expressions involving binary operators are evaluated
975 * in a table-driven way by eval_table. When it evaluates a subexpression it
976 * calls the inner function with its first argument pointing to the next
977 * element of the table. Innermost expressions have special non-table-driven
980 * The stop characters help with lexical analysis: an operator is not
981 * recognized if it is followed by one of the stop characters because
982 * that would make it a different operator.
986 Linetype (*fn)(long *, Linetype, long, Linetype, long);
993 static const struct ops eval_ops[] = {
994 { eval_table, { { "||", op_or, NULL } } },
995 { eval_table, { { "&&", op_and, NULL } } },
996 { eval_table, { { "|", op_bor, "|" } } },
997 { eval_table, { { "^", op_bxor, NULL } } },
998 { eval_table, { { "&", op_band, "&" } } },
999 { eval_table, { { "==", op_eq, NULL },
1000 { "!=", op_ne, NULL } } },
1001 { eval_table, { { "<=", op_le, NULL },
1002 { ">=", op_ge, NULL },
1003 { "<", op_lt, "<=" },
1004 { ">", op_gt, ">=" } } },
1005 { eval_table, { { "<<", op_blsh, NULL },
1006 { ">>", op_brsh, NULL } } },
1007 { eval_table, { { "+", op_add, NULL },
1008 { "-", op_sub, NULL } } },
1009 { eval_unary, { { "*", op_mul, NULL },
1010 { "/", op_div, NULL },
1011 { "%", op_mod, NULL } } },
1014 /* Current operator precedence level */
1015 static long prec(const struct ops *ops)
1017 return (ops - eval_ops);
1021 * Function for evaluating the innermost parts of expressions,
1022 * viz. !expr (expr) number defined(symbol) symbol
1023 * We reset the constexpr flag in the last two cases.
1026 eval_unary(const struct ops *ops, long *valp, const char **cpp)
1034 cp = skipcomment(*cpp);
1036 debug("eval%d !", prec(ops));
1038 lt = eval_unary(ops, valp, &cp);
1043 lt = *valp ? LT_TRUE : LT_FALSE;
1045 } else if (*cp == '~') {
1046 debug("eval%d ~", prec(ops));
1048 lt = eval_unary(ops, valp, &cp);
1053 lt = *valp ? LT_TRUE : LT_FALSE;
1055 } else if (*cp == '-') {
1056 debug("eval%d -", prec(ops));
1058 lt = eval_unary(ops, valp, &cp);
1063 lt = *valp ? LT_TRUE : LT_FALSE;
1065 } else if (*cp == '(') {
1067 debug("eval%d (", prec(ops));
1068 lt = eval_table(eval_ops, valp, &cp);
1071 cp = skipcomment(cp);
1074 } else if (isdigit((unsigned char)*cp)) {
1075 debug("eval%d number", prec(ops));
1076 *valp = strtol(cp, &ep, 0);
1079 lt = *valp ? LT_TRUE : LT_FALSE;
1081 } else if (matchsym("defined", cp) != NULL) {
1082 cp = skipcomment(cp+7);
1084 cp = skipcomment(cp+1);
1090 cp = skipcomment(cp);
1091 if (defparen && *cp++ != ')') {
1092 debug("eval%d defined missing ')'", prec(ops));
1096 debug("eval%d defined unknown", prec(ops));
1099 debug("eval%d defined %s", prec(ops), sym->name);
1100 *valp = (sym->value != NULL);
1101 lt = *valp ? LT_TRUE : LT_FALSE;
1104 } else if (!endsym(*cp)) {
1105 debug("eval%d symbol", prec(ops));
1110 } else if (sym->value == NULL) {
1114 *valp = strtol(sym->value, &ep, 0);
1115 if (*ep != '\0' || ep == sym->value)
1117 lt = *valp ? LT_TRUE : LT_FALSE;
1122 debug("eval%d bad expr", prec(ops));
1127 debug("eval%d = %d", prec(ops), *valp);
1132 * Table-driven evaluation of binary operators.
1135 eval_table(const struct ops *ops, long *valp, const char **cpp)
1137 const struct op *op;
1142 debug("eval%d", prec(ops));
1144 lt = ops->inner(ops+1, valp, &cp);
1148 cp = skipcomment(cp);
1149 for (op = ops->op; op->str != NULL; op++) {
1150 if (strncmp(cp, op->str, strlen(op->str)) == 0) {
1151 /* assume only one-char operators have stop chars */
1152 if (op->stop != NULL && cp[1] != '\0' &&
1153 strchr(op->stop, cp[1]) != NULL)
1159 if (op->str == NULL)
1161 cp += strlen(op->str);
1162 debug("eval%d %s", prec(ops), op->str);
1163 rt = ops->inner(ops+1, &val, &cp);
1166 lt = op->fn(valp, lt, *valp, rt, val);
1170 debug("eval%d = %d", prec(ops), *valp);
1171 debug("eval%d lt = %s", prec(ops), linetype_name[lt]);
1176 * Evaluate the expression on a #if or #elif line. If we can work out
1177 * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
1178 * return just a generic LT_IF.
1181 ifeval(const char **cpp)
1186 debug("eval %s", *cpp);
1187 constexpr = killconsts ? false : true;
1188 ret = eval_table(eval_ops, &val, cpp);
1189 debug("eval = %d", val);
1190 return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
1194 * Read a line and examine its initial part to determine if it is a
1195 * preprocessor directive. Returns NULL on EOF, or a pointer to a
1196 * preprocessor directive name, or a pointer to the zero byte at the
1205 if (fgets(tline, MAXLINE, input) == NULL) {
1207 err(2, "can't read %s", filename);
1211 cp = skipcomment(tline);
1212 if (linestate == LS_START && *cp == '#') {
1213 linestate = LS_HASH;
1214 return (skipcomment(cp + 1));
1215 } else if (*cp == '\0') {
1218 return (skipline(cp));
1223 * Mark a line dirty and consume the rest of it, keeping track of the
1227 skipline(const char *cp)
1231 linestate = LS_DIRTY;
1232 while (*cp != '\0') {
1233 cp = skipcomment(pcp = cp);
1241 * Skip over comments, strings, and character literals and stop at the
1242 * next character position that is not whitespace. Between calls we keep
1243 * the comment state in the global variable incomment, and we also adjust
1244 * the global variable linestate when we see a newline.
1245 * XXX: doesn't cope with the buffer splitting inside a state transition.
1248 skipcomment(const char *cp)
1250 if (text || ignoring[depth]) {
1251 for (; isspace((unsigned char)*cp); cp++)
1253 linestate = LS_START;
1257 /* don't reset to LS_START after a line continuation */
1258 if (strncmp(cp, "\\\r\n", 3) == 0)
1260 else if (strncmp(cp, "\\\n", 2) == 0)
1262 else switch (incomment) {
1264 if (strncmp(cp, "/\\\r\n", 4) == 0) {
1265 incomment = STARTING_COMMENT;
1267 } else if (strncmp(cp, "/\\\n", 3) == 0) {
1268 incomment = STARTING_COMMENT;
1270 } else if (strncmp(cp, "/*", 2) == 0) {
1271 incomment = C_COMMENT;
1273 } else if (strncmp(cp, "//", 2) == 0) {
1274 incomment = CXX_COMMENT;
1276 } else if (strncmp(cp, "\'", 1) == 0) {
1277 incomment = CHAR_LITERAL;
1278 linestate = LS_DIRTY;
1280 } else if (strncmp(cp, "\"", 1) == 0) {
1281 incomment = STRING_LITERAL;
1282 linestate = LS_DIRTY;
1284 } else if (strncmp(cp, "R\"(", 3) == 0) {
1285 incomment = RAW_STRING_LITERAL;
1286 linestate = LS_DIRTY;
1288 } else if (strncmp(cp, "\n", 1) == 0) {
1289 linestate = LS_START;
1291 } else if (strchr(" \r\t", *cp) != NULL) {
1297 if (strncmp(cp, "\n", 1) == 0) {
1298 incomment = NO_COMMENT;
1299 linestate = LS_START;
1304 case STRING_LITERAL:
1305 if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
1306 (incomment == STRING_LITERAL && cp[0] == '\"')) {
1307 incomment = NO_COMMENT;
1309 } else if (cp[0] == '\\') {
1314 } else if (strncmp(cp, "\n", 1) == 0) {
1315 if (incomment == CHAR_LITERAL)
1316 error("Unterminated char literal");
1318 error("Unterminated string literal");
1322 case RAW_STRING_LITERAL:
1323 if (strncmp(cp, ")\"", 2) == 0) {
1324 incomment = NO_COMMENT;
1330 if (strncmp(cp, "*\\\r\n", 4) == 0) {
1331 incomment = FINISHING_COMMENT;
1333 } else if (strncmp(cp, "*\\\n", 3) == 0) {
1334 incomment = FINISHING_COMMENT;
1336 } else if (strncmp(cp, "*/", 2) == 0) {
1337 incomment = NO_COMMENT;
1342 case STARTING_COMMENT:
1344 incomment = C_COMMENT;
1346 } else if (*cp == '/') {
1347 incomment = CXX_COMMENT;
1350 incomment = NO_COMMENT;
1351 linestate = LS_DIRTY;
1354 case FINISHING_COMMENT:
1356 incomment = NO_COMMENT;
1359 incomment = C_COMMENT;
1368 * Skip macro arguments.
1371 skipargs(const char *cp)
1373 const char *ocp = cp;
1375 cp = skipcomment(cp);
1383 cp = skipcomment(cp+1);
1384 } while (level != 0 && *cp != '\0');
1388 /* Rewind and re-detect the syntax error later. */
1393 * Skip over an identifier.
1396 skipsym(const char *cp)
1398 while (!endsym(*cp))
1404 * Skip whitespace and take a copy of any following identifier.
1407 getsym(const char **cpp)
1409 const char *cp = *cpp, *sym;
1411 cp = skipcomment(cp);
1412 cp = skipsym(sym = cp);
1416 return (xstrdup(sym, cp));
1420 * Check that s (a symbol) matches the start of t, and that the
1421 * following character in t is not a symbol character. Returns a
1422 * pointer to the following character in t if there is a match,
1426 matchsym(const char *s, const char *t)
1428 while (*s != '\0' && *t != '\0')
1433 if (*s == '\0' && endsym(*t))
1440 * Look for the symbol in the symbol table. If it is found, we return
1441 * the symbol table index, else we return -1.
1443 static struct macro *
1444 findsym(const char **strp)
1448 struct macro key, *res;
1451 *strp = skipsym(str);
1455 if (symdepth && firstsym)
1456 printf("%s%3d", zerosyms ? "" : "\n", depth);
1457 firstsym = zerosyms = false;
1459 symdepth ? " " : "",
1460 (int)(*strp-str), str,
1461 symdepth ? "" : "\n");
1462 /* we don't care about the value of the symbol */
1467 * 'str' just points into the current mid-parse input and is not
1468 * nul-terminated. We know the length of the symbol, *strp - str, but
1469 * need to provide a nul-terminated lookup key for RB_FIND's comparison
1470 * function. Create one here.
1472 strkey = malloc(*strp - str + 1);
1473 memcpy(strkey, str, *strp - str);
1474 strkey[*strp - str] = 0;
1477 res = RB_FIND(MACROMAP, ¯o_tree, &key);
1479 debugsym("findsym", res);
1486 * Resolve indirect symbol values to their final definitions.
1493 struct macro *sym, *ind;
1497 RB_FOREACH(sym, MACROMAP, ¯o_tree) {
1498 if (sym->value == NULL)
1502 if (ind == NULL || ind == sym ||
1504 ind->value == NULL ||
1505 ind->value == sym->value)
1507 debugsym("indir...", sym);
1508 sym->value = ind->value;
1509 debugsym("...ectsym", sym);
1516 * Add a symbol to the symbol table, specified with the format sym=val
1519 addsym1(bool ignorethis, bool definethis, char *symval)
1521 const char *sym, *val;
1525 if (definethis && *val == '=') {
1526 symval[val - sym] = '\0';
1528 } else if (*val == '\0') {
1529 val = definethis ? "1" : NULL;
1533 addsym2(ignorethis, sym, val);
1537 * Add a symbol to the symbol table.
1540 addsym2(bool ignorethis, const char *symname, const char *val)
1542 const char *cp = symname;
1543 struct macro *sym, *r;
1547 sym = calloc(1, sizeof(*sym));
1548 sym->ignore = ignorethis;
1549 sym->name = symname;
1551 r = RB_INSERT(MACROMAP, ¯o_tree, sym);
1553 debugsym("addsym", sym);
1555 sym->ignore = ignorethis;
1557 debugsym("updsym", sym);
1562 debugsym(const char *why, const struct macro *sym)
1564 debug("%s %s%c%s", why, sym->name,
1565 sym->value ? '=' : ' ',
1566 sym->value ? sym->value : "undef");
1570 * Add symbols to the symbol table from a file containing
1571 * #define and #undef preprocessor directives.
1574 defundefile(const char *fn)
1577 input = fopen(fn, "rb");
1579 err(2, "can't open %s", fn);
1584 err(2, "can't read %s", filename);
1588 error("EOF in comment");
1592 * Read and process one #define or #undef directive
1597 const char *cp, *kw, *sym, *val, *end;
1604 /* strip trailing whitespace, and do a fairly rough check to
1605 avoid unsupported multi-line preprocessor directives */
1606 end = cp + strlen(cp);
1607 while (end > tline && strchr(" \t\n\r", end[-1]) != NULL)
1609 if (end > tline && end[-1] == '\\')
1613 if ((cp = matchsym("define", kw)) != NULL) {
1616 error("Missing macro name in #define");
1620 cp = skipcomment(cp);
1621 val = (cp < end) ? xstrdup(cp, end) : "";
1624 addsym2(false, sym, val);
1625 } else if ((cp = matchsym("undef", kw)) != NULL) {
1628 error("Missing macro name in #undef");
1629 cp = skipcomment(cp);
1631 addsym2(false, sym, NULL);
1633 error("Unrecognized preprocessor directive");
1637 debug("parser line %d state %s comment %s line", linenum,
1638 comment_name[incomment], linestate_name[linestate]);
1643 * Concatenate two strings into new memory, checking for failure.
1646 astrcat(const char *s1, const char *s2)
1652 len = snprintf(NULL, 0, "%s%s", s1, s2);
1655 size = (size_t)len + 1;
1656 s = (char *)malloc(size);
1659 snprintf(s, size, "%s%s", s1, s2);
1664 * Duplicate a segment of a string, checking for failure.
1667 xstrdup(const char *start, const char *end)
1672 if (end < start) abort(); /* bug */
1673 n = (size_t)(end - start) + 1;
1674 s = (char *)malloc(n);
1677 snprintf(s, n, "%s", start);
1685 debug(const char *msg, ...)
1697 error(const char *msg)
1700 warnx("%s: %d: %s", filename, linenum, msg);
1702 warnx("%s: %d: %s (#if line %d depth %d)",
1703 filename, linenum, msg, stifline[depth], depth);
1705 errx(2, "Output may be truncated");