]> git.sur5r.net Git - cc65/blobdiff - src/cc65/preproc.c
Fixed problem with last change. Wide string constants were not handled
[cc65] / src / cc65 / preproc.c
index f04a9642ae7670a637e58e02e4eb924b68d9c7fd..1b13018bc3a623e5cf4fde505edb38df5f4f8f53 100644 (file)
@@ -6,8 +6,8 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2005, Ullrich von Bassewitz                                      */
-/*                Römerstraße 52                                             */
+/* (C) 1998-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
 /*                                                                           */
@@ -128,7 +128,8 @@ typedef enum {
     PP_INCLUDE,
     PP_LINE,
     PP_PRAGMA,
-    PP_UNDEF
+    PP_UNDEF,
+    PP_WARNING,
 } pptoken_t;
 
 
@@ -150,6 +151,7 @@ static const struct PPToken {
     {   "line",                PP_LINE         },
     {          "pragma",       PP_PRAGMA       },
     {          "undef",        PP_UNDEF        },
+    {          "warning",      PP_WARNING      },
 };
 
 /* Number of preprocessor tokens */
@@ -187,7 +189,7 @@ static MacroExp* InitMacroExp (MacroExp* E, Macro* M)
 /* Initialize a MacroExp structure */
 {
     InitCollection (&E->ActualArgs);
-    InitStrBuf (&E->Replacement);
+    SB_Init (&E->Replacement);
     E->M = M;
     return E;
 }
@@ -204,7 +206,7 @@ static void DoneMacroExp (MacroExp* E)
         FreeStrBuf (CollAtUnchecked (&E->ActualArgs, I));
     }
     DoneCollection (&E->ActualArgs);
-    DoneStrBuf (&E->Replacement);
+    SB_Done (&E->Replacement);
 }
 
 
@@ -444,7 +446,7 @@ static void ReadMacroArgs (MacroExp* E)
                }
 
                /* Check for end of macro param list */
-               if (CurC == ')') {
+               if (CurC == ')') {
                    NextChar ();
                    break;
                }
@@ -486,7 +488,7 @@ static void ReadMacroArgs (MacroExp* E)
     }
 
     /* Deallocate string buf resources */
-    DoneStrBuf (&Arg);
+    SB_Done (&Arg);
 }
 
 
@@ -502,6 +504,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);
 
@@ -621,6 +624,7 @@ static void MacroArgSubst (MacroExp* E)
 
     /* Switch back the input */
     InitLine (OldSource);
+    SB_SetIndex (&E->M->Replacement, OldIndex);
 }
 
 
@@ -672,7 +676,10 @@ 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) {
@@ -711,7 +718,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
 }
 
 
@@ -827,8 +836,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.
@@ -983,19 +993,6 @@ static void PreprocessLine (void)
 
 
 
-static void DoUndef (void)
-/* Process the #undef directive */
-{
-    ident Ident;
-
-    SkipWhitespace ();
-    if (MacName (Ident)) {
-       UndefineMacro (Ident);
-    }
-}
-
-
-
 static int PushIf (int Skip, int Invert, int Cond)
 /* Push a new if level onto the if stack */
 {
@@ -1018,6 +1015,22 @@ static int PushIf (int Skip, int Invert, int Cond)
 
 
 
+static void DoError (void)
+/* Print an error */
+{
+    SkipWhitespace ();
+    if (CurC == '\0') {
+       PPError ("Invalid #error directive");
+    } else {
+        PPError ("#error: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
+    }
+
+    /* Clear the rest of line */
+    ClearLine ();
+}
+
+
+
 static int DoIf (int Skip)
 /* Process #if directive */
 {
@@ -1039,20 +1052,6 @@ static int DoIf (int Skip)
        UseLineInfo (SavedNextTok.LI);
     }
 
-#if 0
-    /* Remove the #if from the line */
-    SkipWhitespace ();
-    S = line;
-    while (CurC != '\0') {
-       *S++ = CurC;
-       NextChar ();
-    }
-    *S = '\0';
-
-    /* Start over parsing from line */
-    InitLine (line);
-#endif
-
     /* Switch into special preprocessing mode */
     Preprocessing = 1;
 
@@ -1064,6 +1063,7 @@ static int DoIf (int Skip)
      * the following line.
      */
     SB_AppendStr (Line, ";;");
+    SB_Terminate (Line);
 
     /* Load CurTok and NextTok with tokens from the new input */
     NextToken ();
@@ -1108,6 +1108,9 @@ static void DoInclude (void)
     StrBuf      Filename = STATIC_STRBUF_INITIALIZER;
 
 
+    /* Preprocess the remainder of the line */
+    PreprocessLine ();
+
     /* Skip blanks */
     SkipWhitespace ();
 
@@ -1140,18 +1143,17 @@ static void DoInclude (void)
     SB_Terminate (&Filename);
 
     /* Check if we got a terminator */
-    if (CurC != RTerm) {
+    if (CurC == RTerm) {
+        /* Open the include file */
+        OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec);
+    } else if (CurC == '\0') {
                /* No terminator found */
-               PPError ("Missing terminator or file name too long");
-               goto Done;
+               PPError ("#include expects \"FILENAME\" or <FILENAME>");
     }
 
-    /* Open the include file */
-    OpenIncludeFile (SB_GetConstBuf (&Filename), DirSpec);
-
 Done:
     /* Free the allocated filename data */
-    DoneStrBuf (&Filename);
+    SB_Done (&Filename);
 
     /* Clear the remaining line so the next input will come from the new
      * file (if open)
@@ -1161,22 +1163,6 @@ Done:
 
 
 
-static void DoError (void)
-/* Print an error */
-{
-    SkipWhitespace ();
-    if (CurC == '\0') {
-       PPError ("Invalid #error directive");
-    } else {
-        PPError ("#error: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
-    }
-
-    /* Clear the rest of line */
-    ClearLine ();
-}
-
-
-
 static void DoPragma (void)
 /* Handle a #pragma line by converting the #pragma preprocessor directive into
  * the _Pragma() compiler operator.
@@ -1202,6 +1188,35 @@ static void DoPragma (void)
 
 
 
+static void DoUndef (void)
+/* Process the #undef directive */
+{
+    ident Ident;
+
+    SkipWhitespace ();
+    if (MacName (Ident)) {
+       UndefineMacro (Ident);
+    }
+}
+
+
+
+static void DoWarning (void)
+/* Print a warning */
+{
+    SkipWhitespace ();
+    if (CurC == '\0') {
+       PPError ("Invalid #warning directive");
+    } else {
+        PPWarning ("#warning: %s", SB_GetConstBuf (Line) + SB_GetIndex (Line));
+    }
+
+    /* Clear the rest of line */
+    ClearLine ();
+}
+
+
+
 void Preprocess (void)
 /* Preprocess a line */
 {
@@ -1339,6 +1354,18 @@ void Preprocess (void)
                        }
                        break;
 
+                    case PP_WARNING:
+                        /* #warning is a non standard extension */
+                        if (IS_Get (&Standard) > STD_C99) {
+                            if (!Skip) {
+                                DoWarning ();
+                            }
+                        } else {
+                            PPError ("Preprocessor directive expected");
+                            ClearLine ();
+                        }
+                        break;
+
                    default:
                        PPError ("Preprocessor directive expected");
                        ClearLine ();
@@ -1360,7 +1387,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));
     }
 }