]> git.sur5r.net Git - cc65/blobdiff - src/cc65/preproc.c
Removed unneeded include files.
[cc65] / src / cc65 / preproc.c
index dda22ffd2544ad48afda6b9d2d82e5f0a63b54f1..8edeacdd79bb20ab8761b88f402feb3445358986 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2008, Ullrich von Bassewitz                                      */
+/* (C) 1998-2010, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -335,12 +335,31 @@ static void NewStyleComment (void)
 
 
 
-static void SkipWhitespace (void)
-/* Skip white space in the input stream. */
+static int SkipWhitespace (int SkipLines)
+/* Skip white space in the input stream. Do also skip newlines if SkipLines
+ * is true. Return zero if nothing was skipped, otherwise return a
+ * value != zero.
+ */
 {
-    while (IsSpace (CurC)) {
-       NextChar ();
+    int Skipped = 0;
+    while (1) {
+        if (IsSpace (CurC)) {
+            NextChar ();
+            Skipped = 1;
+        } else if (CurC == '\0' && SkipLines) {
+            /* End of line, read next */
+            if (NextLine () != 0) {
+                Skipped = 1;
+            } else {
+                /* End of input */
+                break;
+            }
+        } else {
+            /* No more white space */
+            break;
+        }
     }
+    return Skipped;
 }
 
 
@@ -446,7 +465,7 @@ static void ReadMacroArgs (MacroExp* E)
                }
 
                /* Check for end of macro param list */
-               if (CurC == ')') {
+               if (CurC == ')') {
                    NextChar ();
                    break;
                }
@@ -455,12 +474,11 @@ static void ReadMacroArgs (MacroExp* E)
                NextChar ();
                 SB_Clear (&Arg);
            }
-       } else if (IsSpace (CurC)) {
+       } else if (SkipWhitespace (1)) {
            /* Squeeze runs of blanks within an arg */
             if (SB_NotEmpty (&Arg)) {
                 SB_AppendChar (&Arg, ' ');
             }
-           SkipWhitespace ();
        } else if (CurC == '/' && NextC == '*') {
             if (SB_NotEmpty (&Arg)) {
                 SB_AppendChar (&Arg, ' ');
@@ -472,14 +490,11 @@ static void ReadMacroArgs (MacroExp* E)
             }
            NewStyleComment ();
        } else if (CurC == '\0') {
-           /* End of line inside macro argument list - read next line */
-            if (SB_NotEmpty (&Arg)) {
-                SB_AppendChar (&Arg, ' ');
-            }
-           if (NextLine () == 0) {
-                ClearLine ();
-               break;
-           }
+           /* End of input inside macro argument list */
+            PPError ("Unterminated argument list invoking macro `%s'", E->M->Name);
+
+            ClearLine ();
+            break;
        } else {
            /* Just copy the character */
                    SB_AppendChar (&Arg, CurC);
@@ -504,6 +519,7 @@ static void MacroArgSubst (MacroExp* E)
 
 
     /* Remember the current input and switch to the macro replacement. */
+    int OldIndex = SB_GetIndex (&E->M->Replacement);
     SB_Reset (&E->M->Replacement);
     OldSource = InitLine (&E->M->Replacement);
 
@@ -520,10 +536,7 @@ static void MacroArgSubst (MacroExp* E)
                 Arg = ME_GetActual (E, ArgIdx);
 
                 /* Copy any following whitespace */
-                HaveSpace = IsSpace (CurC);
-                if (HaveSpace) {
-                    SkipWhitespace ();
-                }
+                HaveSpace = SkipWhitespace (0);
 
                 /* If a ## operator follows, we have to insert the actual
                  * argument as is, otherwise it must be macro replaced.
@@ -560,7 +573,7 @@ static void MacroArgSubst (MacroExp* E)
             /* ## operator. */
             NextChar ();
             NextChar ();
-            SkipWhitespace ();
+            SkipWhitespace (0);
 
             /* Since we need to concatenate the token sequences, remove
              * any whitespace that was added to target, since it must come
@@ -596,7 +609,7 @@ static void MacroArgSubst (MacroExp* E)
              * macro parameter.
              */
             NextChar ();
-            SkipWhitespace ();
+            SkipWhitespace (0);
             if (!IsSym (Ident) || (ArgIdx = FindMacroArg (E->M, Ident)) < 0) {
                 PPError ("`#' is not followed by a macro parameter");
             } else {
@@ -623,6 +636,7 @@ static void MacroArgSubst (MacroExp* E)
 
     /* Switch back the input */
     InitLine (OldSource);
+    SB_SetIndex (&E->M->Replacement, OldIndex);
 }
 
 
@@ -674,15 +688,15 @@ static void MacroCall (StrBuf* Target, Macro* M)
 static void ExpandMacro (StrBuf* Target, Macro* M)
 /* Expand a macro into Target */
 {
-    /* ### printf ("Expanding %s(%u)\n", M->Name, ++V); */
+#if 0
+    static unsigned V = 0;
+    printf ("Expanding %s(%u)\n", M->Name, ++V);
+#endif
 
     /* Check if this is a function like macro */
     if (M->ArgCount >= 0) {
 
-        int Whitespace = IsSpace (CurC);
-        if (Whitespace) {
-            SkipWhitespace ();
-        }
+        int Whitespace = SkipWhitespace (1);
         if (CurC != '(') {
             /* Function like macro but no parameter list */
             SB_AppendStr (Target, M->Name);
@@ -713,7 +727,9 @@ static void ExpandMacro (StrBuf* Target, Macro* M)
         DoneMacroExp (&E);
 
     }
-    /* ### printf ("Done with %s(%u)\n", M->Name, V--); */
+#if 0
+    printf ("Done with %s(%u)\n", M->Name, V--);
+#endif
 }
 
 
@@ -727,7 +743,7 @@ static void DefineMacro (void)
     int         C89;
 
     /* Read the macro name */
-    SkipWhitespace ();
+    SkipWhitespace (0);
     if (!MacName (Ident)) {
        return;
     }
@@ -754,7 +770,7 @@ static void DefineMacro (void)
        while (1) {
 
             /* Skip white space and check for end of parameter list */
-           SkipWhitespace ();
+           SkipWhitespace (0);
            if (CurC == ')') {
                break;
             }
@@ -798,7 +814,7 @@ static void DefineMacro (void)
             /* If we had an ellipsis, or the next char is not a comma, we've
              * reached the end of the macro argument list.
              */
-           SkipWhitespace ();
+           SkipWhitespace (0);
                    if (M->Variadic || CurC != ',') {
                break;
             }
@@ -815,7 +831,7 @@ static void DefineMacro (void)
     }
 
     /* Skip whitespace before the macro replacement */
-    SkipWhitespace ();
+    SkipWhitespace (0);
 
     /* Insert the macro into the macro table and allocate the ActualArgs array */
     InsertMacro (M);
@@ -829,8 +845,9 @@ static void DefineMacro (void)
     while (IsSpace (SB_LookAtLast (&M->Replacement))) {
         SB_Drop (&M->Replacement, 1);
     }
-
-    /* ### printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); */
+#if 0
+    printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement));
+#endif
 
     /* If we have an existing macro, check if the redefinition is identical.
      * Print a diagnostic if not.
@@ -863,26 +880,25 @@ static unsigned Pass1 (StrBuf* Source, StrBuf* Target)
     /* Loop removing ws and comments */
     IdentCount = 0;
     while (CurC != '\0') {
-               if (IsSpace (CurC)) {
+               if (SkipWhitespace (0)) {
             /* Squeeze runs of blanks */
             if (!IsSpace (SB_LookAtLast (Target))) {
                 SB_AppendChar (Target, ' ');
             }
-           SkipWhitespace ();
                } else if (IsSym (Ident)) {
            if (Preprocessing && strcmp (Ident, "defined") == 0) {
                /* Handle the "defined" operator */
-               SkipWhitespace ();
+               SkipWhitespace (0);
                HaveParen = 0;
                if (CurC == '(') {
                    HaveParen = 1;
                    NextChar ();
-                   SkipWhitespace ();
+                   SkipWhitespace (0);
                }
                if (IsSym (Ident)) {
                    SB_AppendChar (Target, IsMacro (Ident)? '1' : '0');
                    if (HaveParen) {
-                       SkipWhitespace ();
+                       SkipWhitespace (0);
                        if (CurC != ')') {
                            PPError ("`)' expected");
                        } else {
@@ -1010,7 +1026,7 @@ static int PushIf (int Skip, int Invert, int Cond)
 static void DoError (void)
 /* Print an error */
 {
-    SkipWhitespace ();
+    SkipWhitespace (0);
     if (CurC == '\0') {
        PPError ("Invalid #error directive");
     } else {
@@ -1082,7 +1098,7 @@ static int DoIfDef (int skip, int flag)
 {
     ident Ident;
 
-    SkipWhitespace ();
+    SkipWhitespace (0);
     if (MacName (Ident) == 0) {
                return 0;
     } else {
@@ -1096,7 +1112,7 @@ static void DoInclude (void)
 /* Open an include file. */
 {
     char       RTerm;
-    unsigned   DirSpec;
+    InputType   IT;
     StrBuf      Filename = STATIC_STRBUF_INITIALIZER;
 
 
@@ -1104,7 +1120,7 @@ static void DoInclude (void)
     PreprocessLine ();
 
     /* Skip blanks */
-    SkipWhitespace ();
+    SkipWhitespace (0);
 
     /* Get the next char and check for a valid file name terminator. Setup
      * the include directory spec (SYS/USR) by looking at the terminator.
@@ -1113,12 +1129,12 @@ static void DoInclude (void)
 
                case '\"':
                    RTerm   = '\"';
-                   DirSpec = INC_USER;
+                   IT = IT_USRINC;
                    break;
 
                case '<':
                    RTerm   = '>';
-                   DirSpec = INC_SYS;
+                   IT = IT_SYSINC;
                    break;
 
                default:
@@ -1137,7 +1153,7 @@ static void DoInclude (void)
     /* Check if we got a terminator */
     if (CurC == RTerm) {
         /* Open the include file */
-        OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec);
+        OpenIncludeFile (SB_GetConstBuf (&Filename), IT);
     } else if (CurC == '\0') {
                /* No terminator found */
                PPError ("#include expects \"FILENAME\" or <FILENAME>");
@@ -1161,7 +1177,7 @@ static void DoPragma (void)
  */
 {
     /* Skip blanks following the #pragma directive */
-    SkipWhitespace ();
+    SkipWhitespace (0);
 
     /* Copy the remainder of the line into MLine removing comments and ws */
     SB_Clear (MLine);
@@ -1185,7 +1201,7 @@ static void DoUndef (void)
 {
     ident Ident;
 
-    SkipWhitespace ();
+    SkipWhitespace (0);
     if (MacName (Ident)) {
        UndefineMacro (Ident);
     }
@@ -1196,7 +1212,7 @@ static void DoUndef (void)
 static void DoWarning (void)
 /* Print a warning */
 {
-    SkipWhitespace ();
+    SkipWhitespace (0);
     if (CurC == '\0') {
        PPError ("Invalid #warning directive");
     } else {
@@ -1221,7 +1237,7 @@ void Preprocess (void)
     }
 
     /* Skip white space at the beginning of the line */
-    SkipWhitespace ();
+    SkipWhitespace (0);
 
     /* Check for stuff to skip */
     Skip = 0;
@@ -1230,7 +1246,7 @@ void Preprocess (void)
                /* Check for preprocessor lines lines */
                if (CurC == '#') {
                    NextChar ();
-                   SkipWhitespace ();
+                   SkipWhitespace (0);
                    if (CurC == '\0') {
                        /* Ignore the empty preprocessor directive */
                        continue;
@@ -1353,13 +1369,17 @@ void Preprocess (void)
                                 DoWarning ();
                             }
                         } else {
-                            PPError ("Preprocessor directive expected");
+                            if (!Skip) {
+                                PPError ("Preprocessor directive expected");
+                            }
                             ClearLine ();
                         }
                         break;
 
                    default:
-                       PPError ("Preprocessor directive expected");
+                        if (!Skip) {
+                           PPError ("Preprocessor directive expected");
+                        }
                        ClearLine ();
                }
            }
@@ -1371,7 +1391,7 @@ void Preprocess (void)
            }
            return;
        }
-       SkipWhitespace ();
+       SkipWhitespace (0);
     }
 
     PreprocessLine ();
@@ -1379,7 +1399,7 @@ void Preprocess (void)
 Done:
     if (Verbosity > 1 && SB_NotEmpty (Line)) {
         printf ("%s(%u): %.*s\n", GetCurrentFile (), GetCurrentLine (),
-                SB_GetLen (Line), SB_GetConstBuf (Line));
+                (int) SB_GetLen (Line), SB_GetConstBuf (Line));
     }
 }