1 /*****************************************************************************/
5 /* cc65 preprocessor */
9 /* (C) 1998-2005, Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 /* Set when the preprocessor calls expr() recursively */
71 unsigned char Preprocessing = 0;
73 /* Management data for #if */
75 #define IFCOND_NONE 0x00U
76 #define IFCOND_SKIP 0x01U
77 #define IFCOND_ELSE 0x02U
78 #define IFCOND_NEEDTERM 0x04U
79 static unsigned char IfStack[MAX_IFS];
80 static int IfIndex = -1;
82 /* Buffer for macro expansion */
85 /* Structure used when expanding macros */
86 typedef struct MacroExp MacroExp;
88 Collection ActualArgs; /* Actual arguments */
89 StrBuf Replacement; /* Replacement with arguments substituted */
90 Macro* M; /* The macro we're handling */
95 /*****************************************************************************/
97 /*****************************************************************************/
101 static unsigned Pass1 (StrBuf* Source, StrBuf* Target);
102 /* Preprocessor pass 1. Remove whitespace. Handle old and new style comments
103 * and the "defined" operator.
106 static void MacroReplacement (StrBuf* Source, StrBuf* Target);
107 /* Perform macro replacement. */
111 /*****************************************************************************/
112 /* Low level preprocessor token handling */
113 /*****************************************************************************/
117 /* Types of preprocessor tokens */
136 /* Preprocessor keyword to token mapping table */
137 static const struct PPToken {
138 const char* Key; /* Keyword */
139 pptoken_t Tok; /* Token */
141 { "define", PP_DEFINE },
144 { "endif", PP_ENDIF },
145 { "error", PP_ERROR },
147 { "ifdef", PP_IFDEF },
148 { "ifndef", PP_IFNDEF },
149 { "include", PP_INCLUDE },
151 { "pragma", PP_PRAGMA },
152 { "undef", PP_UNDEF },
155 /* Number of preprocessor tokens */
156 #define PPTOKEN_COUNT (sizeof(PPTokens) / sizeof(PPTokens[0]))
160 static int CmpToken (const void* Key, const void* Elem)
161 /* Compare function for bsearch */
163 return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key);
168 static pptoken_t FindPPToken (const char* Ident)
169 /* Find a preprocessor token and return it. Return PP_ILLEGAL if the identifier
170 * is not a valid preprocessor token.
174 P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken);
175 return P? P->Tok : PP_ILLEGAL;
180 /*****************************************************************************/
181 /* struct MacroExp */
182 /*****************************************************************************/
186 static MacroExp* InitMacroExp (MacroExp* E, Macro* M)
187 /* Initialize a MacroExp structure */
189 InitCollection (&E->ActualArgs);
190 InitStrBuf (&E->Replacement);
197 static void DoneMacroExp (MacroExp* E)
198 /* Cleanup after use of a MacroExp structure */
202 /* Delete the list with actual arguments */
203 for (I = 0; I < CollCount (&E->ActualArgs); ++I) {
204 FreeStrBuf (CollAtUnchecked (&E->ActualArgs, I));
206 DoneCollection (&E->ActualArgs);
207 DoneStrBuf (&E->Replacement);
212 static void ME_AppendActual (MacroExp* E, StrBuf* Arg)
213 /* Add a copy of Arg to the list of actual macro arguments.
214 * NOTE: This function will clear Arg!
217 /* Create a new string buffer */
218 StrBuf* A = NewStrBuf ();
220 /* Move the contents of Arg to A */
223 /* Add A to the actual arguments */
224 CollAppend (&E->ActualArgs, A);
229 static StrBuf* ME_GetActual (MacroExp* E, unsigned Index)
230 /* Return an actual macro argument with the given index */
232 return CollAt (&E->ActualArgs, Index);
237 static int ME_ArgIsVariadic (const MacroExp* E)
238 /* Return true if the next actual argument we will add is a variadic one */
240 return (E->M->Variadic &&
241 E->M->ArgCount == (int) CollCount (&E->ActualArgs) + 1);
246 /*****************************************************************************/
248 /*****************************************************************************/
252 static void Stringize (StrBuf* Source, StrBuf* Target)
253 /* Stringize the given string: Add double quotes at start and end and preceed
254 * each occurance of " and \ by a backslash.
259 /* Add a starting quote */
260 SB_AppendChar (Target, '\"');
262 /* Replace any characters inside the string may not be part of a string
265 while ((C = SB_Get (Source)) != '\0') {
269 SB_AppendChar (Target, '\\');
272 SB_AppendChar (Target, C);
277 /* Add the closing quote */
278 SB_AppendChar (Target, '\"');
283 static void OldStyleComment (void)
284 /* Remove an old style C comment from line. */
286 /* Remember the current line number, so we can output better error
287 * messages if the comment is not terminated in the current file.
289 unsigned StartingLine = GetCurrentLine();
291 /* Skip the start of comment chars */
295 /* Skip the comment */
296 while (CurC != '*' || NextC != '/') {
298 if (NextLine () == 0) {
299 PPError ("End-of-file reached in comment starting at line %u",
304 if (CurC == '/' && NextC == '*') {
305 PPWarning ("`/*' found inside a comment");
311 /* Skip the end of comment chars */
318 static void NewStyleComment (void)
319 /* Remove a new style C comment from line. */
321 /* Beware: Because line continuation chars are handled when reading
322 * lines, we may only skip til the end of the source line, which
323 * may not be the same as the end of the input line. The end of the
324 * source line is denoted by a lf (\n) character.
328 } while (CurC != '\n' && CurC != '\0');
336 static void SkipWhitespace (void)
337 /* Skip white space in the input stream. */
339 while (IsSpace (CurC)) {
346 static void CopyQuotedString (StrBuf* Target)
347 /* Copy a single or double quoted string from the input to Target. */
349 /* Remember the quote character, copy it to the target buffer and skip it */
351 SB_AppendChar (Target, CurC);
354 /* Copy the characters inside the string */
355 while (CurC != '\0' && CurC != Quote) {
356 /* Keep an escaped char */
358 SB_AppendChar (Target, CurC);
361 /* Copy the character */
362 SB_AppendChar (Target, CurC);
366 /* If we had a terminating quote, copy it */
368 SB_AppendChar (Target, CurC);
375 /*****************************************************************************/
377 /*****************************************************************************/
381 static int MacName (char* Ident)
382 /* Get a macro symbol name into Ident. If we have an error, print a
383 * diagnostic message and clear the line.
386 if (IsSym (Ident) == 0) {
387 PPError ("Identifier expected");
397 static void ReadMacroArgs (MacroExp* E)
398 /* Identify the arguments to a macro call */
400 unsigned Parens; /* Number of open parenthesis */
401 StrBuf Arg = STATIC_STRBUF_INITIALIZER;
403 /* Read the actual macro arguments */
408 /* Nested parenthesis */
409 SB_AppendChar (&Arg, CurC);
413 } else if (IsQuote (CurC)) {
415 /* Quoted string - just copy */
416 CopyQuotedString (&Arg);
418 } else if (CurC == ',' || CurC == ')') {
421 /* Comma or right paren inside nested parenthesis */
425 SB_AppendChar (&Arg, CurC);
427 } else if (CurC == ',' && ME_ArgIsVariadic (E)) {
428 /* It's a comma, but we're inside a variadic macro argument, so
429 * just copy it and proceed.
431 SB_AppendChar (&Arg, CurC);
434 /* End of actual argument. Remove whitespace from the end. */
435 while (IsSpace (SB_LookAtLast (&Arg))) {
439 /* If this is not the single empty argument for a macro with
440 * an empty argument list, remember it.
442 if (CurC != ')' || SB_NotEmpty (&Arg) || E->M->ArgCount > 0) {
443 ME_AppendActual (E, &Arg);
446 /* Check for end of macro param list */
452 /* Start the next param */
456 } else if (IsSpace (CurC)) {
457 /* Squeeze runs of blanks within an arg */
458 if (SB_NotEmpty (&Arg)) {
459 SB_AppendChar (&Arg, ' ');
462 } else if (CurC == '/' && NextC == '*') {
463 if (SB_NotEmpty (&Arg)) {
464 SB_AppendChar (&Arg, ' ');
467 } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') {
468 if (SB_NotEmpty (&Arg)) {
469 SB_AppendChar (&Arg, ' ');
472 } else if (CurC == '\0') {
473 /* End of line inside macro argument list - read next line */
474 if (SB_NotEmpty (&Arg)) {
475 SB_AppendChar (&Arg, ' ');
477 if (NextLine () == 0) {
482 /* Just copy the character */
483 SB_AppendChar (&Arg, CurC);
488 /* Deallocate string buf resources */
494 static void MacroArgSubst (MacroExp* E)
495 /* Argument substitution according to ISO/IEC 9899:1999 (E), 6.10.3.1ff */
504 /* Remember the current input and switch to the macro replacement. */
505 SB_Reset (&E->M->Replacement);
506 OldSource = InitLine (&E->M->Replacement);
508 /* Argument handling loop */
509 while (CurC != '\0') {
511 /* If we have an identifier, check if it's a macro */
514 /* Check if it's a macro argument */
515 if ((ArgIdx = FindMacroArg (E->M, Ident)) >= 0) {
517 /* A macro argument. Get the corresponding actual argument. */
518 Arg = ME_GetActual (E, ArgIdx);
520 /* Copy any following whitespace */
521 HaveSpace = IsSpace (CurC);
526 /* If a ## operator follows, we have to insert the actual
527 * argument as is, otherwise it must be macro replaced.
529 if (CurC == '#' && NextC == '#') {
531 /* ### Add placemarker if necessary */
532 SB_Append (&E->Replacement, Arg);
536 /* Replace the formal argument by a macro replaced copy
540 MacroReplacement (Arg, &E->Replacement);
542 /* If we skipped whitespace before, re-add it now */
544 SB_AppendChar (&E->Replacement, ' ');
551 /* An identifier, keep it */
552 SB_AppendStr (&E->Replacement, Ident);
556 } else if (CurC == '#' && NextC == '#') {
563 /* Since we need to concatenate the token sequences, remove
564 * any whitespace that was added to target, since it must come
567 while (IsSpace (SB_LookAtLast (&E->Replacement))) {
568 SB_Drop (&E->Replacement, 1);
571 /* If the next token is an identifier which is a macro argument,
572 * replace it, otherwise do nothing.
576 /* Check if it's a macro argument */
577 if ((ArgIdx = FindMacroArg (E->M, Ident)) >= 0) {
579 /* Get the corresponding actual argument and add it. */
580 SB_Append (&E->Replacement, ME_GetActual (E, ArgIdx));
584 /* Just an ordinary identifier - add as is */
585 SB_AppendStr (&E->Replacement, Ident);
590 } else if (CurC == '#' && E->M->ArgCount >= 0) {
592 /* A # operator within a macro expansion of a function like
593 * macro. Read the following identifier and check if it's a
598 if (!IsSym (Ident) || (ArgIdx = FindMacroArg (E->M, Ident)) < 0) {
599 PPError ("`#' is not followed by a macro parameter");
601 /* Make a valid string from Replacement */
602 Arg = ME_GetActual (E, ArgIdx);
604 Stringize (Arg, &E->Replacement);
607 } else if (IsQuote (CurC)) {
608 CopyQuotedString (&E->Replacement);
610 SB_AppendChar (&E->Replacement, CurC);
616 /* Remove whitespace from the end of the line */
617 while (IsSpace (SB_LookAtLast (&E->Replacement))) {
618 SB_Drop (&E->Replacement, 1);
622 /* Switch back the input */
623 InitLine (OldSource);
628 static void MacroCall (StrBuf* Target, Macro* M)
629 /* Process a function like macro */
633 /* Eat the left paren */
636 /* Initialize our MacroExp structure */
637 InitMacroExp (&E, M);
639 /* Read the actual macro arguments */
642 /* Compare formal and actual argument count */
643 if (CollCount (&E.ActualArgs) != (unsigned) M->ArgCount) {
645 StrBuf Arg = STATIC_STRBUF_INITIALIZER;
647 /* Argument count mismatch */
648 PPError ("Macro argument count mismatch");
650 /* Be sure to make enough empty arguments available */
651 while (CollCount (&E.ActualArgs) < (unsigned) M->ArgCount) {
652 ME_AppendActual (&E, &Arg);
656 /* Replace macro arguments handling the # and ## operators */
659 /* Do macro replacement on the macro that already has the parameters
663 MacroReplacement (&E.Replacement, Target);
666 /* Free memory allocated for the macro expansion structure */
672 static void ExpandMacro (StrBuf* Target, Macro* M)
673 /* Expand a macro into Target */
675 /* ### printf ("Expanding %s(%u)\n", M->Name, ++V); */
677 /* Check if this is a function like macro */
678 if (M->ArgCount >= 0) {
680 int Whitespace = IsSpace (CurC);
685 /* Function like macro but no parameter list */
686 SB_AppendStr (Target, M->Name);
688 SB_AppendChar (Target, ' ');
691 /* Function like macro */
692 MacroCall (Target, M);
698 InitMacroExp (&E, M);
700 /* Handle # and ## operators for object like macros */
703 /* Do macro replacement on the macro that already has the parameters
707 MacroReplacement (&E.Replacement, Target);
710 /* Free memory allocated for the macro expansion structure */
714 /* ### printf ("Done with %s(%u)\n", M->Name, V--); */
719 static void DefineMacro (void)
720 /* Handle a macro definition. */
727 /* Read the macro name */
729 if (!MacName (Ident)) {
733 /* Remember if we're in C89 mode */
734 C89 = (IS_Get (&Standard) == STD_C89);
736 /* Get an existing macro definition with this name */
737 Existing = FindMacro (Ident);
739 /* Create a new macro definition */
740 M = NewMacro (Ident);
742 /* Check if this is a function like macro */
745 /* Skip the left paren */
748 /* Set the marker that this is a function like macro */
751 /* Read the formal parameter list */
754 /* Skip white space and check for end of parameter list */
760 /* The next token must be either an identifier, or - if not in
761 * C89 mode - the ellipsis.
763 if (!C89 && CurC == '.') {
766 if (CurC != '.' || NextC != '.') {
767 PPError ("`...' expected");
774 /* Remember that the macro is variadic and use __VA_ARGS__ as
777 AddMacroArg (M, "__VA_ARGS__");
781 /* Must be macro argument name */
782 if (MacName (Ident) == 0) {
786 /* __VA_ARGS__ is only allowed in C89 mode */
787 if (!C89 && strcmp (Ident, "__VA_ARGS__") == 0) {
788 PPWarning ("`__VA_ARGS__' can only appear in the expansion "
789 "of a C99 variadic macro");
792 /* Add the macro argument */
793 AddMacroArg (M, Ident);
796 /* If we had an ellipsis, or the next char is not a comma, we've
797 * reached the end of the macro argument list.
800 if (M->Variadic || CurC != ',') {
806 /* Check for a right paren and eat it if we find one */
808 PPError ("`)' expected");
815 /* Skip whitespace before the macro replacement */
818 /* Insert the macro into the macro table and allocate the ActualArgs array */
821 /* Remove whitespace and comments from the line, store the preprocessed
822 * line into the macro replacement buffer.
824 Pass1 (Line, &M->Replacement);
826 /* Remove whitespace from the end of the line */
827 while (IsSpace (SB_LookAtLast (&M->Replacement))) {
828 SB_Drop (&M->Replacement, 1);
831 /* ### printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); */
833 /* If we have an existing macro, check if the redefinition is identical.
834 * Print a diagnostic if not.
836 if (Existing && MacroCmp (M, Existing) != 0) {
837 PPError ("Macro redefinition is not identical");
843 /*****************************************************************************/
845 /*****************************************************************************/
849 static unsigned Pass1 (StrBuf* Source, StrBuf* Target)
850 /* Preprocessor pass 1. Remove whitespace. Handle old and new style comments
851 * and the "defined" operator.
858 /* Switch to the new input source */
859 StrBuf* OldSource = InitLine (Source);
861 /* Loop removing ws and comments */
863 while (CurC != '\0') {
864 if (IsSpace (CurC)) {
865 /* Squeeze runs of blanks */
866 if (!IsSpace (SB_LookAtLast (Target))) {
867 SB_AppendChar (Target, ' ');
870 } else if (IsSym (Ident)) {
871 if (Preprocessing && strcmp (Ident, "defined") == 0) {
872 /* Handle the "defined" operator */
881 SB_AppendChar (Target, IsMacro (Ident)? '1' : '0');
885 PPError ("`)' expected");
891 PPError ("Identifier expected");
892 SB_AppendChar (Target, '0');
896 SB_AppendStr (Target, Ident);
898 } else if (IsQuote (CurC)) {
899 CopyQuotedString (Target);
900 } else if (CurC == '/' && NextC == '*') {
901 if (!IsSpace (SB_LookAtLast (Target))) {
902 SB_AppendChar (Target, ' ');
905 } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') {
906 if (!IsSpace (SB_LookAtLast (Target))) {
907 SB_AppendChar (Target, ' ');
911 SB_AppendChar (Target, CurC);
916 /* Switch back to the old source */
917 InitLine (OldSource);
919 /* Return the number of identifiers found in the line */
925 static void MacroReplacement (StrBuf* Source, StrBuf* Target)
926 /* Perform macro replacement. */
931 /* Remember the current input and switch to Source */
932 StrBuf* OldSource = InitLine (Source);
934 /* Loop substituting macros */
935 while (CurC != '\0') {
936 /* If we have an identifier, check if it's a macro */
938 /* Check if it's a macro */
939 if ((M = FindMacro (Ident)) != 0 && !M->Expanding) {
940 /* It's a macro, expand it */
941 ExpandMacro (Target, M);
943 /* An identifier, keep it */
944 SB_AppendStr (Target, Ident);
946 } else if (IsQuote (CurC)) {
947 CopyQuotedString (Target);
948 } else if (IsSpace (CurC)) {
949 if (!IsSpace (SB_LookAtLast (Target))) {
950 SB_AppendChar (Target, CurC);
954 SB_AppendChar (Target, CurC);
959 /* Switch back the input */
960 InitLine (OldSource);
965 static void PreprocessLine (void)
966 /* Translate one line. */
968 /* Trim whitespace and remove comments. The function returns the number of
969 * identifiers found. If there were any, we will have to check for macros.
972 if (Pass1 (Line, MLine) > 0) {
973 MLine = InitLine (MLine);
976 MacroReplacement (Line, MLine);
979 /* Read from the new line */
981 MLine = InitLine (MLine);
986 static void DoUndef (void)
987 /* Process the #undef directive */
992 if (MacName (Ident)) {
993 UndefineMacro (Ident);
999 static int PushIf (int Skip, int Invert, int Cond)
1000 /* Push a new if level onto the if stack */
1002 /* Check for an overflow of the if stack */
1003 if (IfIndex >= MAX_IFS-1) {
1004 PPError ("Too many nested #if clauses");
1008 /* Push the #if condition */
1011 IfStack[IfIndex] = IFCOND_SKIP | IFCOND_NEEDTERM;
1014 IfStack[IfIndex] = IFCOND_NONE | IFCOND_NEEDTERM;
1015 return (Invert ^ Cond);
1021 static int DoIf (int Skip)
1022 /* Process #if directive */
1026 /* We're about to abuse the compiler expression parser to evaluate the
1027 * #if expression. Save the current tokens to come back here later.
1028 * NOTE: Yes, this is a hack, but it saves a complete separate expression
1029 * evaluation for the preprocessor.
1031 Token SavedCurTok = CurTok;
1032 Token SavedNextTok = NextTok;
1034 /* Make sure the line infos for the tokens won't get removed */
1035 if (SavedCurTok.LI) {
1036 UseLineInfo (SavedCurTok.LI);
1038 if (SavedNextTok.LI) {
1039 UseLineInfo (SavedNextTok.LI);
1043 /* Remove the #if from the line */
1046 while (CurC != '\0') {
1052 /* Start over parsing from line */
1056 /* Switch into special preprocessing mode */
1059 /* Expand macros in this line */
1062 /* Add two semicolons as sentinels to the line, so the following
1063 * expression evaluation will eat these two tokens but nothing from
1064 * the following line.
1066 SB_AppendStr (Line, ";;");
1068 /* Load CurTok and NextTok with tokens from the new input */
1072 /* Call the expression parser */
1073 ConstExpr (hie1, &Expr);
1075 /* End preprocessing mode */
1078 /* Reset the old tokens */
1079 CurTok = SavedCurTok;
1080 NextTok = SavedNextTok;
1082 /* Set the #if condition according to the expression result */
1083 return PushIf (Skip, 1, Expr.IVal != 0);
1088 static int DoIfDef (int skip, int flag)
1089 /* Process #ifdef if flag == 1, or #ifndef if flag == 0. */
1094 if (MacName (Ident) == 0) {
1097 return PushIf (skip, flag, IsMacro(Ident));
1103 static void DoInclude (void)
1104 /* Open an include file. */
1108 StrBuf Filename = STATIC_STRBUF_INITIALIZER;
1114 /* Get the next char and check for a valid file name terminator. Setup
1115 * the include directory spec (SYS/USR) by looking at the terminator.
1130 PPError ("`\"' or `<' expected");
1135 /* Get a copy of the filename */
1136 while (CurC != '\0' && CurC != RTerm) {
1137 SB_AppendChar (&Filename, CurC);
1140 SB_Terminate (&Filename);
1142 /* Check if we got a terminator */
1143 if (CurC != RTerm) {
1144 /* No terminator found */
1145 PPError ("Missing terminator or file name too long");
1149 /* Open the include file */
1150 OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec);
1153 /* Free the allocated filename data */
1154 DoneStrBuf (&Filename);
1156 /* Clear the remaining line so the next input will come from the new
1164 static void DoError (void)
1165 /* Print an error */
1169 PPError ("Invalid #error directive");
1171 PPError ("#error: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
1174 /* Clear the rest of line */
1180 static void DoPragma (void)
1181 /* Handle a #pragma line by converting the #pragma preprocessor directive into
1182 * the _Pragma() compiler operator.
1185 /* Skip blanks following the #pragma directive */
1188 /* Copy the remainder of the line into MLine removing comments and ws */
1190 Pass1 (Line, MLine);
1192 /* Convert the directive into the operator */
1193 SB_CopyStr (Line, "_Pragma (");
1195 Stringize (MLine, Line);
1196 SB_AppendChar (Line, ')');
1198 /* Initialize reading from line */
1205 void Preprocess (void)
1206 /* Preprocess a line */
1211 /* Create the output buffer if we don't already have one */
1213 MLine = NewStrBuf ();
1216 /* Skip white space at the beginning of the line */
1219 /* Check for stuff to skip */
1221 while (CurC == '\0' || CurC == '#' || Skip) {
1223 /* Check for preprocessor lines lines */
1228 /* Ignore the empty preprocessor directive */
1231 if (!IsSym (Directive)) {
1232 PPError ("Preprocessor directive expected");
1235 switch (FindPPToken (Directive)) {
1245 if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
1247 /* Handle as #else/#if combination */
1248 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
1251 IfStack[IfIndex] |= IFCOND_ELSE;
1254 /* #elif doesn't need a terminator */
1255 IfStack[IfIndex] &= ~IFCOND_NEEDTERM;
1257 PPError ("Duplicate #else/#elif");
1260 PPError ("Unexpected #elif");
1266 if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
1267 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
1270 IfStack[IfIndex] |= IFCOND_ELSE;
1272 PPError ("Duplicate #else");
1275 PPError ("Unexpected `#else'");
1281 /* Remove any clauses on top of stack that do not
1282 * need a terminating #endif.
1284 while (IfIndex >= 0 && (IfStack[IfIndex] & IFCOND_NEEDTERM) == 0) {
1288 /* Stack may not be empty here or something is wrong */
1289 CHECK (IfIndex >= 0);
1291 /* Remove the clause that needs a terminator */
1292 Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0;
1294 PPError ("Unexpected `#endif'");
1309 Skip = DoIfDef (Skip, 1);
1313 Skip = DoIfDef (Skip, 0);
1323 /* Should do something in C99 at least, but we ignore it */
1343 PPError ("Preprocessor directive expected");
1349 if (NextLine () == 0) {
1351 PPError ("`#endif' expected");
1361 if (Verbosity > 1 && SB_NotEmpty (Line)) {
1362 printf ("%s(%u): %.*s\n", GetCurrentFile (), GetCurrentLine (),
1363 SB_GetLen (Line), SB_GetConstBuf (Line));