1 /*****************************************************************************/
5 /* cc65 preprocessor */
9 /* (C) 1998-2008, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 */
137 /* Preprocessor keyword to token mapping table */
138 static const struct PPToken {
139 const char* Key; /* Keyword */
140 pptoken_t Tok; /* Token */
142 { "define", PP_DEFINE },
145 { "endif", PP_ENDIF },
146 { "error", PP_ERROR },
148 { "ifdef", PP_IFDEF },
149 { "ifndef", PP_IFNDEF },
150 { "include", PP_INCLUDE },
152 { "pragma", PP_PRAGMA },
153 { "undef", PP_UNDEF },
154 { "warning", PP_WARNING },
157 /* Number of preprocessor tokens */
158 #define PPTOKEN_COUNT (sizeof(PPTokens) / sizeof(PPTokens[0]))
162 static int CmpToken (const void* Key, const void* Elem)
163 /* Compare function for bsearch */
165 return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key);
170 static pptoken_t FindPPToken (const char* Ident)
171 /* Find a preprocessor token and return it. Return PP_ILLEGAL if the identifier
172 * is not a valid preprocessor token.
176 P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken);
177 return P? P->Tok : PP_ILLEGAL;
182 /*****************************************************************************/
183 /* struct MacroExp */
184 /*****************************************************************************/
188 static MacroExp* InitMacroExp (MacroExp* E, Macro* M)
189 /* Initialize a MacroExp structure */
191 InitCollection (&E->ActualArgs);
192 SB_Init (&E->Replacement);
199 static void DoneMacroExp (MacroExp* E)
200 /* Cleanup after use of a MacroExp structure */
204 /* Delete the list with actual arguments */
205 for (I = 0; I < CollCount (&E->ActualArgs); ++I) {
206 FreeStrBuf (CollAtUnchecked (&E->ActualArgs, I));
208 DoneCollection (&E->ActualArgs);
209 SB_Done (&E->Replacement);
214 static void ME_AppendActual (MacroExp* E, StrBuf* Arg)
215 /* Add a copy of Arg to the list of actual macro arguments.
216 * NOTE: This function will clear Arg!
219 /* Create a new string buffer */
220 StrBuf* A = NewStrBuf ();
222 /* Move the contents of Arg to A */
225 /* Add A to the actual arguments */
226 CollAppend (&E->ActualArgs, A);
231 static StrBuf* ME_GetActual (MacroExp* E, unsigned Index)
232 /* Return an actual macro argument with the given index */
234 return CollAt (&E->ActualArgs, Index);
239 static int ME_ArgIsVariadic (const MacroExp* E)
240 /* Return true if the next actual argument we will add is a variadic one */
242 return (E->M->Variadic &&
243 E->M->ArgCount == (int) CollCount (&E->ActualArgs) + 1);
248 /*****************************************************************************/
250 /*****************************************************************************/
254 static void Stringize (StrBuf* Source, StrBuf* Target)
255 /* Stringize the given string: Add double quotes at start and end and preceed
256 * each occurance of " and \ by a backslash.
261 /* Add a starting quote */
262 SB_AppendChar (Target, '\"');
264 /* Replace any characters inside the string may not be part of a string
267 while ((C = SB_Get (Source)) != '\0') {
271 SB_AppendChar (Target, '\\');
274 SB_AppendChar (Target, C);
279 /* Add the closing quote */
280 SB_AppendChar (Target, '\"');
285 static void OldStyleComment (void)
286 /* Remove an old style C comment from line. */
288 /* Remember the current line number, so we can output better error
289 * messages if the comment is not terminated in the current file.
291 unsigned StartingLine = GetCurrentLine();
293 /* Skip the start of comment chars */
297 /* Skip the comment */
298 while (CurC != '*' || NextC != '/') {
300 if (NextLine () == 0) {
301 PPError ("End-of-file reached in comment starting at line %u",
306 if (CurC == '/' && NextC == '*') {
307 PPWarning ("`/*' found inside a comment");
313 /* Skip the end of comment chars */
320 static void NewStyleComment (void)
321 /* Remove a new style C comment from line. */
323 /* Beware: Because line continuation chars are handled when reading
324 * lines, we may only skip til the end of the source line, which
325 * may not be the same as the end of the input line. The end of the
326 * source line is denoted by a lf (\n) character.
330 } while (CurC != '\n' && CurC != '\0');
338 static void SkipWhitespace (void)
339 /* Skip white space in the input stream. */
341 while (IsSpace (CurC)) {
348 static void CopyQuotedString (StrBuf* Target)
349 /* Copy a single or double quoted string from the input to Target. */
351 /* Remember the quote character, copy it to the target buffer and skip it */
353 SB_AppendChar (Target, CurC);
356 /* Copy the characters inside the string */
357 while (CurC != '\0' && CurC != Quote) {
358 /* Keep an escaped char */
360 SB_AppendChar (Target, CurC);
363 /* Copy the character */
364 SB_AppendChar (Target, CurC);
368 /* If we had a terminating quote, copy it */
370 SB_AppendChar (Target, CurC);
377 /*****************************************************************************/
379 /*****************************************************************************/
383 static int MacName (char* Ident)
384 /* Get a macro symbol name into Ident. If we have an error, print a
385 * diagnostic message and clear the line.
388 if (IsSym (Ident) == 0) {
389 PPError ("Identifier expected");
399 static void ReadMacroArgs (MacroExp* E)
400 /* Identify the arguments to a macro call */
402 unsigned Parens; /* Number of open parenthesis */
403 StrBuf Arg = STATIC_STRBUF_INITIALIZER;
405 /* Read the actual macro arguments */
410 /* Nested parenthesis */
411 SB_AppendChar (&Arg, CurC);
415 } else if (IsQuote (CurC)) {
417 /* Quoted string - just copy */
418 CopyQuotedString (&Arg);
420 } else if (CurC == ',' || CurC == ')') {
423 /* Comma or right paren inside nested parenthesis */
427 SB_AppendChar (&Arg, CurC);
429 } else if (CurC == ',' && ME_ArgIsVariadic (E)) {
430 /* It's a comma, but we're inside a variadic macro argument, so
431 * just copy it and proceed.
433 SB_AppendChar (&Arg, CurC);
436 /* End of actual argument. Remove whitespace from the end. */
437 while (IsSpace (SB_LookAtLast (&Arg))) {
441 /* If this is not the single empty argument for a macro with
442 * an empty argument list, remember it.
444 if (CurC != ')' || SB_NotEmpty (&Arg) || E->M->ArgCount > 0) {
445 ME_AppendActual (E, &Arg);
448 /* Check for end of macro param list */
454 /* Start the next param */
458 } else if (IsSpace (CurC)) {
459 /* Squeeze runs of blanks within an arg */
460 if (SB_NotEmpty (&Arg)) {
461 SB_AppendChar (&Arg, ' ');
464 } else if (CurC == '/' && NextC == '*') {
465 if (SB_NotEmpty (&Arg)) {
466 SB_AppendChar (&Arg, ' ');
469 } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') {
470 if (SB_NotEmpty (&Arg)) {
471 SB_AppendChar (&Arg, ' ');
474 } else if (CurC == '\0') {
475 /* End of line inside macro argument list - read next line */
476 if (SB_NotEmpty (&Arg)) {
477 SB_AppendChar (&Arg, ' ');
479 if (NextLine () == 0) {
484 /* Just copy the character */
485 SB_AppendChar (&Arg, CurC);
490 /* Deallocate string buf resources */
496 static void MacroArgSubst (MacroExp* E)
497 /* Argument substitution according to ISO/IEC 9899:1999 (E), 6.10.3.1ff */
506 /* Remember the current input and switch to the macro replacement. */
507 SB_Reset (&E->M->Replacement);
508 OldSource = InitLine (&E->M->Replacement);
510 /* Argument handling loop */
511 while (CurC != '\0') {
513 /* If we have an identifier, check if it's a macro */
516 /* Check if it's a macro argument */
517 if ((ArgIdx = FindMacroArg (E->M, Ident)) >= 0) {
519 /* A macro argument. Get the corresponding actual argument. */
520 Arg = ME_GetActual (E, ArgIdx);
522 /* Copy any following whitespace */
523 HaveSpace = IsSpace (CurC);
528 /* If a ## operator follows, we have to insert the actual
529 * argument as is, otherwise it must be macro replaced.
531 if (CurC == '#' && NextC == '#') {
533 /* ### Add placemarker if necessary */
534 SB_Append (&E->Replacement, Arg);
538 /* Replace the formal argument by a macro replaced copy
542 MacroReplacement (Arg, &E->Replacement);
544 /* If we skipped whitespace before, re-add it now */
546 SB_AppendChar (&E->Replacement, ' ');
553 /* An identifier, keep it */
554 SB_AppendStr (&E->Replacement, Ident);
558 } else if (CurC == '#' && NextC == '#') {
565 /* Since we need to concatenate the token sequences, remove
566 * any whitespace that was added to target, since it must come
569 while (IsSpace (SB_LookAtLast (&E->Replacement))) {
570 SB_Drop (&E->Replacement, 1);
573 /* If the next token is an identifier which is a macro argument,
574 * replace it, otherwise do nothing.
578 /* Check if it's a macro argument */
579 if ((ArgIdx = FindMacroArg (E->M, Ident)) >= 0) {
581 /* Get the corresponding actual argument and add it. */
582 SB_Append (&E->Replacement, ME_GetActual (E, ArgIdx));
586 /* Just an ordinary identifier - add as is */
587 SB_AppendStr (&E->Replacement, Ident);
592 } else if (CurC == '#' && E->M->ArgCount >= 0) {
594 /* A # operator within a macro expansion of a function like
595 * macro. Read the following identifier and check if it's a
600 if (!IsSym (Ident) || (ArgIdx = FindMacroArg (E->M, Ident)) < 0) {
601 PPError ("`#' is not followed by a macro parameter");
603 /* Make a valid string from Replacement */
604 Arg = ME_GetActual (E, ArgIdx);
606 Stringize (Arg, &E->Replacement);
609 } else if (IsQuote (CurC)) {
610 CopyQuotedString (&E->Replacement);
612 SB_AppendChar (&E->Replacement, CurC);
618 /* Remove whitespace from the end of the line */
619 while (IsSpace (SB_LookAtLast (&E->Replacement))) {
620 SB_Drop (&E->Replacement, 1);
624 /* Switch back the input */
625 InitLine (OldSource);
630 static void MacroCall (StrBuf* Target, Macro* M)
631 /* Process a function like macro */
635 /* Eat the left paren */
638 /* Initialize our MacroExp structure */
639 InitMacroExp (&E, M);
641 /* Read the actual macro arguments */
644 /* Compare formal and actual argument count */
645 if (CollCount (&E.ActualArgs) != (unsigned) M->ArgCount) {
647 StrBuf Arg = STATIC_STRBUF_INITIALIZER;
649 /* Argument count mismatch */
650 PPError ("Macro argument count mismatch");
652 /* Be sure to make enough empty arguments available */
653 while (CollCount (&E.ActualArgs) < (unsigned) M->ArgCount) {
654 ME_AppendActual (&E, &Arg);
658 /* Replace macro arguments handling the # and ## operators */
661 /* Do macro replacement on the macro that already has the parameters
665 MacroReplacement (&E.Replacement, Target);
668 /* Free memory allocated for the macro expansion structure */
674 static void ExpandMacro (StrBuf* Target, Macro* M)
675 /* Expand a macro into Target */
677 /* ### printf ("Expanding %s(%u)\n", M->Name, ++V); */
679 /* Check if this is a function like macro */
680 if (M->ArgCount >= 0) {
682 int Whitespace = IsSpace (CurC);
687 /* Function like macro but no parameter list */
688 SB_AppendStr (Target, M->Name);
690 SB_AppendChar (Target, ' ');
693 /* Function like macro */
694 MacroCall (Target, M);
700 InitMacroExp (&E, M);
702 /* Handle # and ## operators for object like macros */
705 /* Do macro replacement on the macro that already has the parameters
709 MacroReplacement (&E.Replacement, Target);
712 /* Free memory allocated for the macro expansion structure */
716 /* ### printf ("Done with %s(%u)\n", M->Name, V--); */
721 static void DefineMacro (void)
722 /* Handle a macro definition. */
729 /* Read the macro name */
731 if (!MacName (Ident)) {
735 /* Remember if we're in C89 mode */
736 C89 = (IS_Get (&Standard) == STD_C89);
738 /* Get an existing macro definition with this name */
739 Existing = FindMacro (Ident);
741 /* Create a new macro definition */
742 M = NewMacro (Ident);
744 /* Check if this is a function like macro */
747 /* Skip the left paren */
750 /* Set the marker that this is a function like macro */
753 /* Read the formal parameter list */
756 /* Skip white space and check for end of parameter list */
762 /* The next token must be either an identifier, or - if not in
763 * C89 mode - the ellipsis.
765 if (!C89 && CurC == '.') {
768 if (CurC != '.' || NextC != '.') {
769 PPError ("`...' expected");
776 /* Remember that the macro is variadic and use __VA_ARGS__ as
779 AddMacroArg (M, "__VA_ARGS__");
783 /* Must be macro argument name */
784 if (MacName (Ident) == 0) {
788 /* __VA_ARGS__ is only allowed in C89 mode */
789 if (!C89 && strcmp (Ident, "__VA_ARGS__") == 0) {
790 PPWarning ("`__VA_ARGS__' can only appear in the expansion "
791 "of a C99 variadic macro");
794 /* Add the macro argument */
795 AddMacroArg (M, Ident);
798 /* If we had an ellipsis, or the next char is not a comma, we've
799 * reached the end of the macro argument list.
802 if (M->Variadic || CurC != ',') {
808 /* Check for a right paren and eat it if we find one */
810 PPError ("`)' expected");
817 /* Skip whitespace before the macro replacement */
820 /* Insert the macro into the macro table and allocate the ActualArgs array */
823 /* Remove whitespace and comments from the line, store the preprocessed
824 * line into the macro replacement buffer.
826 Pass1 (Line, &M->Replacement);
828 /* Remove whitespace from the end of the line */
829 while (IsSpace (SB_LookAtLast (&M->Replacement))) {
830 SB_Drop (&M->Replacement, 1);
833 /* ### printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); */
835 /* If we have an existing macro, check if the redefinition is identical.
836 * Print a diagnostic if not.
838 if (Existing && MacroCmp (M, Existing) != 0) {
839 PPError ("Macro redefinition is not identical");
845 /*****************************************************************************/
847 /*****************************************************************************/
851 static unsigned Pass1 (StrBuf* Source, StrBuf* Target)
852 /* Preprocessor pass 1. Remove whitespace. Handle old and new style comments
853 * and the "defined" operator.
860 /* Switch to the new input source */
861 StrBuf* OldSource = InitLine (Source);
863 /* Loop removing ws and comments */
865 while (CurC != '\0') {
866 if (IsSpace (CurC)) {
867 /* Squeeze runs of blanks */
868 if (!IsSpace (SB_LookAtLast (Target))) {
869 SB_AppendChar (Target, ' ');
872 } else if (IsSym (Ident)) {
873 if (Preprocessing && strcmp (Ident, "defined") == 0) {
874 /* Handle the "defined" operator */
883 SB_AppendChar (Target, IsMacro (Ident)? '1' : '0');
887 PPError ("`)' expected");
893 PPError ("Identifier expected");
894 SB_AppendChar (Target, '0');
898 SB_AppendStr (Target, Ident);
900 } else if (IsQuote (CurC)) {
901 CopyQuotedString (Target);
902 } else if (CurC == '/' && NextC == '*') {
903 if (!IsSpace (SB_LookAtLast (Target))) {
904 SB_AppendChar (Target, ' ');
907 } else if (IS_Get (&Standard) >= STD_C99 && CurC == '/' && NextC == '/') {
908 if (!IsSpace (SB_LookAtLast (Target))) {
909 SB_AppendChar (Target, ' ');
913 SB_AppendChar (Target, CurC);
918 /* Switch back to the old source */
919 InitLine (OldSource);
921 /* Return the number of identifiers found in the line */
927 static void MacroReplacement (StrBuf* Source, StrBuf* Target)
928 /* Perform macro replacement. */
933 /* Remember the current input and switch to Source */
934 StrBuf* OldSource = InitLine (Source);
936 /* Loop substituting macros */
937 while (CurC != '\0') {
938 /* If we have an identifier, check if it's a macro */
940 /* Check if it's a macro */
941 if ((M = FindMacro (Ident)) != 0 && !M->Expanding) {
942 /* It's a macro, expand it */
943 ExpandMacro (Target, M);
945 /* An identifier, keep it */
946 SB_AppendStr (Target, Ident);
948 } else if (IsQuote (CurC)) {
949 CopyQuotedString (Target);
950 } else if (IsSpace (CurC)) {
951 if (!IsSpace (SB_LookAtLast (Target))) {
952 SB_AppendChar (Target, CurC);
956 SB_AppendChar (Target, CurC);
961 /* Switch back the input */
962 InitLine (OldSource);
967 static void PreprocessLine (void)
968 /* Translate one line. */
970 /* Trim whitespace and remove comments. The function returns the number of
971 * identifiers found. If there were any, we will have to check for macros.
974 if (Pass1 (Line, MLine) > 0) {
975 MLine = InitLine (MLine);
978 MacroReplacement (Line, MLine);
981 /* Read from the new line */
983 MLine = InitLine (MLine);
988 static int PushIf (int Skip, int Invert, int Cond)
989 /* Push a new if level onto the if stack */
991 /* Check for an overflow of the if stack */
992 if (IfIndex >= MAX_IFS-1) {
993 PPError ("Too many nested #if clauses");
997 /* Push the #if condition */
1000 IfStack[IfIndex] = IFCOND_SKIP | IFCOND_NEEDTERM;
1003 IfStack[IfIndex] = IFCOND_NONE | IFCOND_NEEDTERM;
1004 return (Invert ^ Cond);
1010 static void DoError (void)
1011 /* Print an error */
1015 PPError ("Invalid #error directive");
1017 PPError ("#error: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
1020 /* Clear the rest of line */
1026 static int DoIf (int Skip)
1027 /* Process #if directive */
1031 /* We're about to abuse the compiler expression parser to evaluate the
1032 * #if expression. Save the current tokens to come back here later.
1033 * NOTE: Yes, this is a hack, but it saves a complete separate expression
1034 * evaluation for the preprocessor.
1036 Token SavedCurTok = CurTok;
1037 Token SavedNextTok = NextTok;
1039 /* Make sure the line infos for the tokens won't get removed */
1040 if (SavedCurTok.LI) {
1041 UseLineInfo (SavedCurTok.LI);
1043 if (SavedNextTok.LI) {
1044 UseLineInfo (SavedNextTok.LI);
1047 /* Switch into special preprocessing mode */
1050 /* Expand macros in this line */
1053 /* Add two semicolons as sentinels to the line, so the following
1054 * expression evaluation will eat these two tokens but nothing from
1055 * the following line.
1057 SB_AppendStr (Line, ";;");
1058 SB_Terminate (Line);
1060 /* Load CurTok and NextTok with tokens from the new input */
1064 /* Call the expression parser */
1065 ConstExpr (hie1, &Expr);
1067 /* End preprocessing mode */
1070 /* Reset the old tokens */
1071 CurTok = SavedCurTok;
1072 NextTok = SavedNextTok;
1074 /* Set the #if condition according to the expression result */
1075 return PushIf (Skip, 1, Expr.IVal != 0);
1080 static int DoIfDef (int skip, int flag)
1081 /* Process #ifdef if flag == 1, or #ifndef if flag == 0. */
1086 if (MacName (Ident) == 0) {
1089 return PushIf (skip, flag, IsMacro(Ident));
1095 static void DoInclude (void)
1096 /* Open an include file. */
1100 StrBuf Filename = STATIC_STRBUF_INITIALIZER;
1103 /* Preprocess the remainder of the line */
1109 /* Get the next char and check for a valid file name terminator. Setup
1110 * the include directory spec (SYS/USR) by looking at the terminator.
1125 PPError ("`\"' or `<' expected");
1130 /* Get a copy of the filename */
1131 while (CurC != '\0' && CurC != RTerm) {
1132 SB_AppendChar (&Filename, CurC);
1135 SB_Terminate (&Filename);
1137 /* Check if we got a terminator */
1138 if (CurC == RTerm) {
1139 /* Open the include file */
1140 OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec);
1141 } else if (CurC == '\0') {
1142 /* No terminator found */
1143 PPError ("#include expects \"FILENAME\" or <FILENAME>");
1147 /* Free the allocated filename data */
1148 SB_Done (&Filename);
1150 /* Clear the remaining line so the next input will come from the new
1158 static void DoPragma (void)
1159 /* Handle a #pragma line by converting the #pragma preprocessor directive into
1160 * the _Pragma() compiler operator.
1163 /* Skip blanks following the #pragma directive */
1166 /* Copy the remainder of the line into MLine removing comments and ws */
1168 Pass1 (Line, MLine);
1170 /* Convert the directive into the operator */
1171 SB_CopyStr (Line, "_Pragma (");
1173 Stringize (MLine, Line);
1174 SB_AppendChar (Line, ')');
1176 /* Initialize reading from line */
1183 static void DoUndef (void)
1184 /* Process the #undef directive */
1189 if (MacName (Ident)) {
1190 UndefineMacro (Ident);
1196 static void DoWarning (void)
1197 /* Print a warning */
1201 PPError ("Invalid #warning directive");
1203 PPWarning ("#warning: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
1206 /* Clear the rest of line */
1212 void Preprocess (void)
1213 /* Preprocess a line */
1218 /* Create the output buffer if we don't already have one */
1220 MLine = NewStrBuf ();
1223 /* Skip white space at the beginning of the line */
1226 /* Check for stuff to skip */
1228 while (CurC == '\0' || CurC == '#' || Skip) {
1230 /* Check for preprocessor lines lines */
1235 /* Ignore the empty preprocessor directive */
1238 if (!IsSym (Directive)) {
1239 PPError ("Preprocessor directive expected");
1242 switch (FindPPToken (Directive)) {
1252 if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
1254 /* Handle as #else/#if combination */
1255 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
1258 IfStack[IfIndex] |= IFCOND_ELSE;
1261 /* #elif doesn't need a terminator */
1262 IfStack[IfIndex] &= ~IFCOND_NEEDTERM;
1264 PPError ("Duplicate #else/#elif");
1267 PPError ("Unexpected #elif");
1273 if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
1274 if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
1277 IfStack[IfIndex] |= IFCOND_ELSE;
1279 PPError ("Duplicate #else");
1282 PPError ("Unexpected `#else'");
1288 /* Remove any clauses on top of stack that do not
1289 * need a terminating #endif.
1291 while (IfIndex >= 0 && (IfStack[IfIndex] & IFCOND_NEEDTERM) == 0) {
1295 /* Stack may not be empty here or something is wrong */
1296 CHECK (IfIndex >= 0);
1298 /* Remove the clause that needs a terminator */
1299 Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0;
1301 PPError ("Unexpected `#endif'");
1316 Skip = DoIfDef (Skip, 1);
1320 Skip = DoIfDef (Skip, 0);
1330 /* Should do something in C99 at least, but we ignore it */
1350 /* #warning is a non standard extension */
1351 if (IS_Get (&Standard) > STD_C99) {
1356 PPError ("Preprocessor directive expected");
1362 PPError ("Preprocessor directive expected");
1368 if (NextLine () == 0) {
1370 PPError ("`#endif' expected");
1380 if (Verbosity > 1 && SB_NotEmpty (Line)) {
1381 printf ("%s(%u): %.*s\n", GetCurrentFile (), GetCurrentLine (),
1382 SB_GetLen (Line), SB_GetConstBuf (Line));