]> git.sur5r.net Git - cc65/blob - src/cc65/preproc.c
Redoing the pragma stuff
[cc65] / src / cc65 / preproc.c
1
2 /* C pre-processor functions */
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <errno.h>
8
9 /* common */
10 #include "chartype.h"
11 #include "check.h"
12 #include "inline.h"
13 #include "print.h"
14 #include "xmalloc.h"
15
16 /* cc65 */
17 #include "codegen.h"
18 #include "error.h"
19 #include "expr.h"
20 #include "global.h"
21 #include "ident.h"
22 #include "incpath.h"
23 #include "input.h"
24 #include "lineinfo.h"
25 #include "macrotab.h"
26 #include "scanner.h"
27 #include "util.h"
28 #include "preproc.h"
29
30
31
32 /*****************************************************************************/
33 /*                                 Forwards                                  */
34 /*****************************************************************************/
35
36
37
38 static int Pass1 (const char* From, char* To);
39 /* Preprocessor pass 1. Remove whitespace and comments. */
40
41
42
43 /*****************************************************************************/
44 /*                                   Data                                    */
45 /*****************************************************************************/
46
47
48
49 /* Set when the preprocessor calls expr() recursively */
50 unsigned char Preprocessing = 0;
51
52 /* Management data for #if */
53 #define MAX_IFS         64
54 #define IFCOND_NONE     0x00U
55 #define IFCOND_SKIP     0x01U
56 #define IFCOND_ELSE     0x02U
57 #define IFCOND_NEEDTERM 0x04U
58 static unsigned char IfStack[MAX_IFS];
59 static int           IfIndex = -1;
60
61 /* Buffer for macro expansion */
62 static char mlinebuf [LINESIZE];
63 static char* mline = mlinebuf;
64 static char* mptr;
65
66
67
68 /*****************************************************************************/
69 /*                   Low level preprocessor token handling                   */
70 /*****************************************************************************/
71
72
73
74 /* Types of preprocessor tokens */
75 typedef enum {
76     PP_DEFINE,
77     PP_ELIF,
78     PP_ELSE,
79     PP_ENDIF,
80     PP_ERROR,
81     PP_IF,
82     PP_IFDEF,
83     PP_IFNDEF,
84     PP_INCLUDE,
85     PP_LINE,
86     PP_PRAGMA,
87     PP_UNDEF,
88     PP_ILLEGAL
89 } pptoken_t;
90
91
92
93 /* Preprocessor keyword to token mapping table */
94 static const struct PPToken {
95     const char* Key;            /* Keyword */
96     pptoken_t   Tok;            /* Token */
97 } PPTokens[] = {
98     {   "define",       PP_DEFINE       },
99     {   "elif",         PP_ELIF         },
100     {   "else",         PP_ELSE         },
101     {   "endif",        PP_ENDIF        },
102     {   "error",        PP_ERROR        },
103     {   "if",           PP_IF           },
104     {   "ifdef",        PP_IFDEF        },
105     {   "ifndef",       PP_IFNDEF       },
106     {   "include",      PP_INCLUDE      },
107     {   "line",         PP_LINE         },
108     {   "pragma",       PP_PRAGMA       },
109     {   "undef",        PP_UNDEF        },
110 };
111
112 /* Number of preprocessor tokens */
113 #define PPTOKEN_COUNT   (sizeof(PPTokens) / sizeof(PPTokens[0]))
114
115
116
117 static int CmpToken (const void* Key, const void* Elem)
118 /* Compare function for bsearch */
119 {
120     return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key);
121 }
122
123
124
125 static pptoken_t FindPPToken (const char* Ident)
126 /* Find a preprocessor token and return ut. Return PP_ILLEGAL if the identifier
127  * is not a valid preprocessor token.
128  */
129 {
130     struct PPToken* P;
131     P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken);
132     return P? P->Tok : PP_ILLEGAL;
133 }
134
135
136
137 /*****************************************************************************/
138 /*                                   Code                                    */
139 /*****************************************************************************/
140
141
142
143 #ifdef HAVE_INLINE
144 INLINE void KeepChar (char c)
145 /* Put character c into translation buffer. */
146 {
147     *mptr++ = c;
148 }
149 #else
150 #define KeepChar(c)     *mptr++ = (c)
151 #endif
152
153
154
155 static void KeepStr (const char* S)
156 /* Put string str into translation buffer. */
157 {
158     unsigned Len = strlen (S);
159     memcpy (mptr, S, Len);
160     mptr += Len;
161 }
162
163
164
165 static void Stringize (const char* S)
166 /* Stringize the given string: Add double quotes at start and end and preceed
167  * each occurance of " and \ by a backslash.
168  */
169 {
170     KeepChar ('\"');
171     /* Replace any characters inside the string may not be part of a string
172      * unescaped.
173      */
174     while (*S) {
175         switch (*S) {
176             case '\"':
177             case '\\':
178                 KeepChar ('\\');
179             /* FALLTHROUGH */
180             default:
181                 KeepChar (*S);
182                 break;
183         }
184         ++S;
185     }
186     KeepChar ('\"');
187 }
188
189
190
191 static void SwapLineBuffers (void)
192 /* Swap both line buffers */
193 {
194     /* Swap mline and line */
195     char* p = line;
196     line = mline;
197     mline = p;
198 }
199
200
201
202 static void OldStyleComment (void)
203 /* Remove an old style C comment from line. */
204 {
205     /* Remember the current line number, so we can output better error
206      * messages if the comment is not terminated in the current file.
207      */
208     unsigned StartingLine = GetCurrentLine();
209
210     /* Skip the start of comment chars */
211     NextChar ();
212     NextChar ();
213
214     /* Skip the comment */
215     while (CurC != '*' || NextC != '/') {
216         if (CurC == '\0') {
217             if (NextLine () == 0) {
218                 PPError ("End-of-file reached in comment starting at line %u",
219                          StartingLine);
220                 return;
221             }
222         } else {
223             if (CurC == '/' && NextC == '*') {
224                 PPWarning ("`/*' found inside a comment");
225             }
226             NextChar ();
227         }
228     }
229
230     /* Skip the end of comment chars */
231     NextChar ();
232     NextChar ();
233 }
234
235
236
237 static void NewStyleComment (void)
238 /* Remove a new style C comment from line. */
239 {
240     /* Beware: Because line continuation chars are handled when reading
241      * lines, we may only skip til the end of the source line, which
242      * may not be the same as the end of the input line. The end of the
243      * source line is denoted by a lf (\n) character.
244      */
245     do {
246         NextChar ();
247     } while (CurC != '\n' && CurC != '\0');
248     if (CurC == '\n') {
249         NextChar ();
250     }
251 }
252
253
254
255 static void SkipBlank (void)
256 /* Skip blanks and tabs in the input stream. */
257 {
258     while (IsBlank (CurC)) {
259         NextChar ();
260     }
261 }
262
263
264
265 static char* CopyQuotedString (char* Target)
266 /* Copy a single or double quoted string from the input to Target. Return the
267  * new target pointer. Target will not be terminated after the copy.
268  */
269 {
270     /* Remember the quote character, copy it to the target buffer and skip it */
271     char Quote = CurC;
272     *Target++  = CurC;
273     NextChar ();
274
275     /* Copy the characters inside the string */
276     while (CurC != '\0' && CurC != Quote) {
277         /* Keep an escaped char */
278         if (CurC == '\\') {
279             *Target++ = CurC;
280             NextChar ();
281         }
282         /* Copy the character */
283         *Target++ = CurC;
284         NextChar ();
285     }
286
287     /* If we had a terminating quote, copy it */
288     if (CurC != '\0') {
289         *Target++ = CurC;
290         NextChar ();
291     }
292
293     /* Return the new target pointer */
294     return Target;
295 }
296
297
298
299 /*****************************************************************************/
300 /*                                Macro stuff                                */
301 /*****************************************************************************/
302
303
304
305 static int MacName (char* Ident)
306 /* Get a macro symbol name into Ident.  If we have an error, print a
307  * diagnostic message and clear the line.
308  */
309 {
310     if (IsSym (Ident) == 0) {
311         PPError ("Identifier expected");
312         ClearLine ();
313         return 0;
314     } else {
315         return 1;
316     }
317 }
318
319
320
321 static void ExpandMacroArgs (Macro* M)
322 /* Expand the arguments of a macro */
323 {
324     ident       Ident;
325     const char* Replacement;
326     const char* SavePtr;
327
328     /* Save the current line pointer and setup the new ones */
329     SavePtr = lptr;
330     InitLine (M->Replacement);
331
332     /* Copy the macro replacement checking for parameters to replace */
333     while (CurC != '\0') {
334         /* If the next token is an identifier, check for a macro arg */
335         if (IsIdent (CurC)) {
336             SymName (Ident);
337             Replacement = FindMacroArg (M, Ident);
338             if (Replacement) {
339                 /* Macro arg, keep the replacement */
340                 KeepStr (Replacement);
341             } else {
342                 /* No macro argument, keep the original identifier */
343                 KeepStr (Ident);
344             }
345         } else if (CurC == '#' && IsIdent (NextC)) {
346             NextChar ();
347             SymName (Ident);
348             Replacement = FindMacroArg (M, Ident);
349             if (Replacement) {
350                 /* Make a valid string from Replacement */
351                 Stringize (Replacement);
352             } else {
353                 /* No replacement - keep the input */
354                 KeepChar ('#');
355                 KeepStr (Ident);
356             }
357         } else if (IsQuote (CurC)) {
358             mptr = CopyQuotedString (mptr);
359         } else {
360             KeepChar (CurC);
361             NextChar ();
362         }
363     }
364
365     /* Reset the line pointer */
366     InitLine (SavePtr);
367 }
368
369
370
371 static int MacroCall (Macro* M)
372 /* Process a function like macro */
373 {
374     int         ArgCount;       /* Macro argument count */
375     unsigned    ParCount;       /* Number of open parenthesis */
376     char        Buf[LINESIZE];  /* Argument buffer */
377     const char* ArgStart;
378     char*       B;
379
380     /* Expect an argument list */
381     SkipBlank ();
382     if (CurC != '(') {
383         PPError ("Illegal macro call");
384         return 0;
385     }
386
387     /* Eat the left paren */
388     NextChar ();
389
390     /* Read the actual macro arguments and store pointers to these arguments
391      * into the array of actual arguments in the macro definition.
392      */
393     ArgCount = 0;
394     ParCount = 0;
395     ArgStart = Buf;
396     B        = Buf;
397     while (1) {
398         if (CurC == '(') {
399             /* Nested parenthesis */
400             *B++ = CurC;
401             NextChar ();
402             ++ParCount;
403         } else if (IsQuote (CurC)) {
404             B = CopyQuotedString (B);
405         } else if (CurC == ',' || CurC == ')') {
406             if (ParCount == 0) {
407                 /* End of actual argument */
408                 *B++ = '\0';
409                 while (IsBlank(*ArgStart)) {
410                     ++ArgStart;
411                 }
412                 if (ArgCount < M->ArgCount) {
413                     M->ActualArgs[ArgCount++] = ArgStart;
414                 } else if (CurC != ')' || *ArgStart != '\0' || M->ArgCount > 0) {
415                     /* Be sure not to count the single empty argument for a
416                      * macro that does not have arguments.
417                      */
418                     ++ArgCount;
419                 }
420
421                 /* Check for end of macro param list */
422                 if (CurC == ')') {
423                     NextChar ();
424                     break;
425                 }
426
427                 /* Start the next param */
428                 ArgStart = B;
429                 NextChar ();
430             } else {
431                 /* Comma or right paren inside nested parenthesis */
432                 if (CurC == ')') {
433                     --ParCount;
434                 }
435                 *B++ = CurC;
436                 NextChar ();
437             }
438         } else if (IsBlank (CurC)) {
439             /* Squeeze runs of blanks */
440             *B++ = ' ';
441             SkipBlank ();
442         } else if (CurC == '/' && NextC == '*') {
443             *B++ = ' ';
444             OldStyleComment ();
445         } else if (ANSI == 0 && CurC == '/' && NextC == '/') {
446             *B++ = ' ';
447             NewStyleComment ();
448         } else if (CurC == '\0') {
449             /* End of line inside macro argument list - read next line */
450             if (NextLine () == 0) {
451                 return 0;
452             }
453         } else {
454             /* Just copy the character */
455             *B++ = CurC;
456             NextChar ();
457         }
458     }
459
460     /* Compare formal argument count with actual */
461     if (M->ArgCount != ArgCount) {
462         PPError ("Macro argument count mismatch");
463         /* Be sure to make enough empty arguments available */
464         while (ArgCount < M->ArgCount) {
465             M->ActualArgs [ArgCount++] = "";
466         }
467     }
468
469     /* Preprocess the line, replacing macro parameters */
470     ExpandMacroArgs (M);
471
472     /* Done */
473     return 1;
474 }
475
476
477
478 static void ExpandMacro (Macro* M)
479 /* Expand a macro */
480 {
481     /* Check if this is a function like macro */
482     if (M->ArgCount >= 0) {
483         /* Function like macro */
484         if (MacroCall (M) == 0) {
485             ClearLine ();
486         }
487     } else {
488         /* Just copy the replacement text */
489         KeepStr (M->Replacement);
490     }
491 }
492
493
494
495 static void DefineMacro (void)
496 /* Handle a macro definition. */
497 {
498     char*       saveptr;
499     ident       Ident;
500     char        Buf[LINESIZE];
501     Macro*      M;
502     Macro*      Existing;
503
504     /* Read the macro name */
505     SkipBlank ();
506     if (!MacName (Ident)) {
507         return;
508     }
509
510     /* Get an existing macro definition with this name */
511     Existing = FindMacro (Ident);
512
513     /* Create a new macro definition */
514     M = NewMacro (Ident);
515
516     /* Check if this is a function like macro */
517     if (CurC == '(') {
518
519         /* Skip the left paren */
520         NextChar ();
521
522         /* Set the marker that this is a function like macro */
523         M->ArgCount = 0;
524
525         /* Read the formal parameter list */
526         while (1) {
527             SkipBlank ();
528             if (CurC == ')')
529                 break;
530             if (MacName (Ident) == 0) {
531                 return;
532             }
533             AddMacroArg (M, Ident);
534             SkipBlank ();
535             if (CurC != ',')
536                 break;
537             NextChar ();
538         }
539
540         /* Check for a right paren and eat it if we find one */
541         if (CurC != ')') {
542             PPError ("`)' expected");
543             ClearLine ();
544             return;
545         }
546         NextChar ();
547     }
548
549     /* Insert the macro into the macro table and allocate the ActualArgs array */
550     InsertMacro (M);
551
552     /* Remove whitespace and comments from the line, store the preprocessed
553      * line into Buf.
554      */
555     SkipBlank ();
556     saveptr = mptr;
557     Pass1 (lptr, Buf);
558     mptr = saveptr;
559
560     /* Create a copy of the replacement */
561     M->Replacement = xstrdup (Buf);
562
563     /* If we have an existing macro, check if the redefinition is identical.
564      * Print a diagnostic if not.
565      */
566     if (Existing) {
567         if (MacroCmp (M, Existing) != 0) {
568             PPError ("Macro redefinition is not identical");
569         }
570     }
571 }
572
573
574
575 /*****************************************************************************/
576 /*                               Preprocessing                               */
577 /*****************************************************************************/
578
579
580
581 static int Pass1 (const char* From, char* To)
582 /* Preprocessor pass 1. Remove whitespace and comments. */
583 {
584     int         done;
585     ident       Ident;
586     int         HaveParen;
587
588     /* Initialize reading from "From" */
589     InitLine (From);
590
591     /* Target is "To" */
592     mptr = To;
593
594     /* Loop removing ws and comments */
595     done = 1;
596     while (CurC != '\0') {
597         if (IsBlank (CurC)) {
598             KeepChar (' ');
599             SkipBlank ();
600         } else if (IsIdent (CurC)) {
601             SymName (Ident);
602             if (Preprocessing && strcmp(Ident, "defined") == 0) {
603                 /* Handle the "defined" operator */
604                 SkipBlank();
605                 HaveParen = 0;
606                 if (CurC == '(') {
607                     HaveParen = 1;
608                     NextChar ();
609                     SkipBlank();
610                 }
611                 if (!IsIdent (CurC)) {
612                     PPError ("Identifier expected");
613                     KeepChar ('0');
614                 } else {
615                     SymName (Ident);
616                     KeepChar (IsMacro (Ident)? '1' : '0');
617                     if (HaveParen) {
618                         SkipBlank();
619                         if (CurC != ')') {
620                             PPError ("`)' expected");
621                         } else {
622                             NextChar ();
623                         }
624                     }
625                 }
626             } else {
627                 if (MaybeMacro (Ident[0])) {
628                     done = 0;
629                 }
630                 KeepStr (Ident);
631             }
632         } else if (IsQuote (CurC)) {
633             mptr = CopyQuotedString (mptr);
634         } else if (CurC == '/' && NextC == '*') {
635             KeepChar (' ');
636             OldStyleComment ();
637         } else if (ANSI == 0 && CurC == '/' && NextC == '/') {
638             KeepChar (' ');
639             NewStyleComment ();
640         } else {
641             KeepChar (CurC);
642             NextChar ();
643         }
644     }
645     KeepChar ('\0');
646     return done;
647 }
648
649
650
651 static int Pass2 (const char* From, char* To)
652 /* Preprocessor pass 2.  Perform macro substitution. */
653 {
654     int         no_chg;
655     ident       Ident;
656     Macro*      M;
657
658     /* Initialize reading from "From" */
659     InitLine (From);
660
661     /* Target is "To" */
662     mptr = To;
663
664     /* Loop substituting macros */
665     no_chg = 1;
666     while (CurC != '\0') {
667         /* If we have an identifier, check if it's a macro */
668         if (IsIdent (CurC)) {
669             SymName (Ident);
670             M = FindMacro (Ident);
671             if (M) {
672                 ExpandMacro (M);
673                 no_chg = 0;
674             } else {
675                 KeepStr (Ident);
676             }
677         } else if (IsQuote (CurC)) {
678             mptr = CopyQuotedString (mptr);
679         } else {
680             KeepChar (CurC);
681             NextChar ();
682         }
683     }
684     return no_chg;
685 }
686
687
688
689 static void PreprocessLine (void)
690 /* Translate one line. */
691 {
692     unsigned I;
693
694     /* Trim whitespace and remove comments. The function returns false if no
695      * identifiers were found that may be macros. If this is the case, no
696      * macro substitution is performed.
697      */
698     int Done = Pass1 (line, mline);
699
700     /* Repeatedly expand macros in the line */
701     for (I = 0; I < 5; ++I) {
702         /* Swap mline and line */
703         SwapLineBuffers ();
704         if (Done) {
705             break;
706         }
707         /* Perform macro expansion */
708         Done = Pass2 (line, mline);
709         KeepChar ('\0');
710     }
711
712     /* Reinitialize line parsing */
713     InitLine (line);
714 }
715
716
717
718 static void DoUndef (void)
719 /* Process the #undef directive */
720 {
721     ident Ident;
722
723     SkipBlank ();
724     if (MacName (Ident)) {
725         UndefineMacro (Ident);
726     }
727 }
728
729
730
731 static int PushIf (int Skip, int Invert, int Cond)
732 /* Push a new if level onto the if stack */
733 {
734     /* Check for an overflow of the if stack */
735     if (IfIndex >= MAX_IFS-1) {
736         PPError ("Too many nested #if clauses");
737         return 1;
738     }
739
740     /* Push the #if condition */
741     ++IfIndex;
742     if (Skip) {
743         IfStack[IfIndex] = IFCOND_SKIP | IFCOND_NEEDTERM;
744         return 1;
745     } else {
746         IfStack[IfIndex] = IFCOND_NONE | IFCOND_NEEDTERM;
747         return (Invert ^ Cond);
748     }
749 }
750
751
752
753 static int DoIf (int Skip)
754 /* Process #if directive */
755 {
756     ExprDesc lval;
757     char* S;
758
759     /* We're about to abuse the compiler expression parser to evaluate the
760      * #if expression. Save the current tokens to come back here later.
761      * NOTE: Yes, this is a hack, but it saves a complete separate expression
762      * evaluation for the preprocessor.
763      */
764     Token sv1 = CurTok;
765     Token sv2 = NextTok;
766
767     /* Make sure the line infos for the tokens won't get removed */
768     if (sv1.LI) {
769         UseLineInfo (sv1.LI);
770     }
771     if (sv2.LI) {
772         UseLineInfo (sv2.LI);
773     }
774
775     /* Remove the #if from the line and add two semicolons as sentinels */
776     SkipBlank ();
777     S = line;
778     while (CurC != '\0') {
779         *S++ = CurC;
780         NextChar ();
781     }
782     *S++ = ';';
783     *S++ = ';';
784     *S   = '\0';
785
786     /* Start over parsing from line */
787     InitLine (line);
788
789     /* Switch into special preprocessing mode */
790     Preprocessing = 1;
791
792     /* Expand macros in this line */
793     PreprocessLine ();
794
795     /* Prime the token pump (remove old tokens from the stream) */
796     NextToken ();
797     NextToken ();
798
799     /* Call the expression parser */
800     ConstExpr (&lval);
801
802     /* End preprocessing mode */
803     Preprocessing = 0;
804
805     /* Reset the old tokens */
806     CurTok  = sv1;
807     NextTok = sv2;
808
809     /* Set the #if condition according to the expression result */
810     return PushIf (Skip, 1, lval.ConstVal != 0);
811 }
812
813
814
815 static int DoIfDef (int skip, int flag)
816 /* Process #ifdef if flag == 1, or #ifndef if flag == 0. */
817 {
818     ident Ident;
819
820     SkipBlank ();
821     if (MacName (Ident) == 0) {
822         return 0;
823     } else {
824         return PushIf (skip, flag, IsMacro(Ident));
825     }
826 }
827
828
829
830 static void DoInclude (void)
831 /* Open an include file. */
832 {
833     char        RTerm;
834     unsigned    DirSpec;
835
836
837     /* Skip blanks */
838     SkipBlank ();
839
840     /* Get the next char and check for a valid file name terminator. Setup
841      * the include directory spec (SYS/USR) by looking at the terminator.
842      */
843     switch (CurC) {
844
845         case '\"':
846             RTerm   = '\"';
847             DirSpec = INC_USER;
848             break;
849
850         case '<':
851             RTerm   = '>';
852             DirSpec = INC_SYS;
853             break;
854
855         default:
856             PPError ("`\"' or `<' expected");
857             goto Done;
858     }
859     NextChar ();
860
861     /* Copy the filename into mline. Since mline has the same size as the
862      * input line, we don't need to check for an overflow here.
863      */
864     mptr = mline;
865     while (CurC != '\0' && CurC != RTerm) {
866         KeepChar (CurC);
867         NextChar ();
868     }
869     *mptr = '\0';
870
871     /* Check if we got a terminator */
872     if (CurC != RTerm) {
873         /* No terminator found */
874         PPError ("Missing terminator or file name too long");
875         goto Done;
876     }
877
878     /* Open the include file */
879     OpenIncludeFile (mline, DirSpec);
880
881 Done:
882     /* Clear the remaining line so the next input will come from the new
883      * file (if open)
884      */
885     ClearLine ();
886 }
887
888
889
890 static void DoError (void)
891 /* Print an error */
892 {
893     SkipBlank ();
894     if (CurC == '\0') {
895         PPError ("Invalid #error directive");
896     } else {
897         PPError ("#error: %s", lptr);
898     }
899
900     /* Clear the rest of line */
901     ClearLine ();
902 }
903
904
905
906 static void DoPragma (void)
907 /* Handle a #pragma line by converting the #pragma preprocessor directive into
908  * the _Pragma() compiler operator.
909  */
910 {
911     /* Skip blanks following the #pragma directive */
912     SkipBlank ();
913
914     /* Copy the remainder of the line into mline removing comments and ws */
915     Pass1 (lptr, mline);
916
917     /* Convert the directive into the operator */
918     mptr = line;
919     KeepStr ("_Pragma (");
920     Stringize (mline);
921     KeepChar (')');
922     *mptr = '\0';
923
924     /* Initialize reading from line */
925     InitLine (line);
926 }
927
928
929
930 void Preprocess (void)
931 /* Preprocess a line */
932 {
933     int         Skip;
934     ident       Directive;
935
936     /* Skip white space at the beginning of the line */
937     SkipBlank ();
938
939     /* Check for stuff to skip */
940     Skip = 0;
941     while (CurC == '\0' || CurC == '#' || Skip) {
942
943         /* Check for preprocessor lines lines */
944         if (CurC == '#') {
945             NextChar ();
946             SkipBlank ();
947             if (CurC == '\0') {
948                 /* Ignore the empty preprocessor directive */
949                 continue;
950             }
951             if (!IsSym (Directive)) {
952                 PPError ("Preprocessor directive expected");
953                 ClearLine ();
954             } else {
955                 switch (FindPPToken (Directive)) {
956
957                     case PP_DEFINE:
958                         if (!Skip) {
959                             DefineMacro ();
960                         }
961                         break;
962
963                     case PP_ELIF:
964                         if (IfIndex >= 0) {
965                             if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
966
967                                 /* Handle as #else/#if combination */
968                                 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
969                                     Skip = !Skip;
970                                 }
971                                 IfStack[IfIndex] |= IFCOND_ELSE;
972                                 Skip = DoIf (Skip);
973
974                                 /* #elif doesn't need a terminator */
975                                 IfStack[IfIndex] &= ~IFCOND_NEEDTERM;
976                             } else {
977                                 PPError ("Duplicate #else/#elif");
978                             }
979                         } else {
980                             PPError ("Unexpected #elif");
981                         }
982                         break;
983
984                     case PP_ELSE:
985                         if (IfIndex >= 0) {
986                             if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
987                                 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
988                                     Skip = !Skip;
989                                 }
990                                 IfStack[IfIndex] |= IFCOND_ELSE;
991                             } else {
992                                 PPError ("Duplicate #else");
993                             }
994                         } else {
995                             PPError ("Unexpected `#else'");
996                         }
997                         break;
998
999                     case PP_ENDIF:
1000                         if (IfIndex >= 0) {
1001                             /* Remove any clauses on top of stack that do not
1002                              * need a terminating #endif.
1003                              */
1004                             while (IfIndex >= 0 && (IfStack[IfIndex] & IFCOND_NEEDTERM) == 0) {
1005                                 --IfIndex;
1006                             }
1007
1008                             /* Stack may not be empty here or something is wrong */
1009                             CHECK (IfIndex >= 0);
1010
1011                             /* Remove the clause that needs a terminator */
1012                             Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0;
1013                         } else {
1014                             PPError ("Unexpected `#endif'");
1015                         }
1016                         break;
1017
1018                     case PP_ERROR:
1019                         if (!Skip) {
1020                             DoError ();
1021                         }
1022                         break;
1023
1024                     case PP_IF:
1025                         Skip = DoIf (Skip);
1026                         break;
1027
1028                     case PP_IFDEF:
1029                         Skip = DoIfDef (Skip, 1);
1030                         break;
1031
1032                     case PP_IFNDEF:
1033                         Skip = DoIfDef (Skip, 0);
1034                         break;
1035
1036                     case PP_INCLUDE:
1037                         if (!Skip) {
1038                             DoInclude ();
1039                         }
1040                         break;
1041
1042                     case PP_LINE:
1043                         /* Not allowed in strict ANSI mode */
1044                         if (!Skip && ANSI) {
1045                             PPError ("Preprocessor directive expected");
1046                             ClearLine ();
1047                         }
1048                         break;
1049
1050                     case PP_PRAGMA:
1051                         if (!Skip) {
1052                             DoPragma ();
1053                             goto Done;
1054                         }
1055                         break;
1056
1057                     case PP_UNDEF:
1058                         if (!Skip) {
1059                             DoUndef ();
1060                         }
1061                         break;
1062
1063                     default:
1064                         PPError ("Preprocessor directive expected");
1065                         ClearLine ();
1066                 }
1067             }
1068
1069         }
1070         if (NextLine () == 0) {
1071             if (IfIndex >= 0) {
1072                 PPError ("`#endif' expected");
1073             }
1074             return;
1075         }
1076         SkipBlank ();
1077     }
1078
1079     PreprocessLine ();
1080
1081 Done:
1082     Print (stdout, 2, "line: %s\n", line);
1083 }
1084