]> git.sur5r.net Git - cc65/blobdiff - src/cc65/preproc.c
Fixed a bug
[cc65] / src / cc65 / preproc.c
index c95bbb92cb01e14a2288d5778db5b76ef70f9d0c..8a35d0f27c692e3bb475f098ba65d6a4878d9c77 100644 (file)
@@ -160,8 +160,8 @@ static void keepstr (const char* S)
 
 
 
-static void Comment (void)
-/* Remove a C comment from line. */
+static void OldStyleComment (void)
+/* Remove an old style C comment from line. */
 {
     /* Remember the current line number, so we can output better error
      * messages if the comment is not terminated in the current file.
@@ -176,13 +176,13 @@ static void Comment (void)
     while (CurC != '*' || NextC != '/') {
        if (CurC == '\0') {
            if (NextLine () == 0) {
-               PPError ("End-of-file reached in comment starting at line %u",
-                        StartingLine);
+               PPError ("End-of-file reached in comment starting at line %u",
+                        StartingLine);
                return;
            }
        } else {
            if (CurC == '/' && NextC == '*') {
-               PPWarning ("`/*' found inside a comment");
+               PPWarning ("`/*' found inside a comment");
            }
            NextChar ();
        }
@@ -195,6 +195,24 @@ static void Comment (void)
 
 
 
+static void NewStyleComment (void)
+/* Remove a new style C comment from line. */
+{
+    /* Beware: Because line continuation chars are handled when reading
+     * lines, we may only skip til the end of the source line, which
+     * may not be the same as the end of the input line. The end of the
+     * source line is denoted by a lf (\n) character.
+     */
+    do {
+       NextChar ();
+    } while (CurC != '\n' && CurC != '\0');
+    if (CurC == '\n') {
+       NextChar ();
+    }
+}
+
+
+
 static void SkipBlank (void)
 /* Skip blanks and tabs in the input stream. */
 {
@@ -291,7 +309,21 @@ static void ExpandMacroArgs (Macro* M)
            Replacement = FindMacroArg (M, Ident);
                    if (Replacement) {
                keepch ('\"');
-               keepstr (Replacement);
+                /* We have to escape any characters inside replacement that
+                 * may not be part of a string unescaped.
+                 */
+                while (*Replacement) {
+                    switch (*Replacement) {
+                        case '\"':
+                        case '\\':
+                            keepch ('\\');
+                        /* FALLTHROUGH */
+                        default:
+                            keepch (*Replacement);
+                            break;
+                    }
+                    ++Replacement;
+                }
                keepch ('\"');
            } else {
                keepch ('#');
@@ -314,7 +346,7 @@ static void ExpandMacroArgs (Macro* M)
 static int MacroCall (Macro* M)
 /* Process a function like macro */
 {
-    unsigned   ArgCount;       /* Macro argument count */
+    int         ArgCount;              /* Macro argument count */
     unsigned   ParCount;       /* Number of open parenthesis */
     char       Buf[LINESIZE];  /* Argument buffer */
     const char* ArgStart;
@@ -355,21 +387,21 @@ static int MacroCall (Macro* M)
                if (ArgCount < M->ArgCount) {
                    M->ActualArgs[ArgCount++] = ArgStart;
                        } else if (CurC != ')' || *ArgStart != '\0' || M->ArgCount > 0) {
-                   /* Be sure not to count the single empty argument for a
+                   /* Be sure not to count the single empty argument for a
                     * macro that does not have arguments.
                     */
                    ++ArgCount;
                }
 
-               /* Check for end of macro param list */
-               if (CurC == ')') {
-                   NextChar ();
-                   break;
-               }
+               /* Check for end of macro param list */
+               if (CurC == ')') {
+                   NextChar ();
+                   break;
+               }
 
                        /* Start the next param */
                ArgStart = B;
-               NextChar ();
+               NextChar ();
            } else {
                /* Comma or right paren inside nested parenthesis */
                if (CurC == ')') {
@@ -382,25 +414,31 @@ static int MacroCall (Macro* M)
            /* Squeeze runs of blanks */
            *B++ = ' ';
            SkipBlank ();
+       } else if (CurC == '/' && NextC == '*') {
+           *B++ = ' ';
+           OldStyleComment ();
+       } else if (ANSI == 0 && CurC == '/' && NextC == '/') {
+           *B++ = ' ';
+           NewStyleComment ();
        } else if (CurC == '\0') {
-           /* End of line inside macro argument list - read next line */
+           /* End of line inside macro argument list - read next line */
            if (NextLine () == 0) {
                return 0;
            }
        } else {
-           /* Just copy the character */
+           /* Just copy the character */
            *B++ = CurC;
-           NextChar ();
+           NextChar ();
        }
     }
 
     /* Compare formal argument count with actual */
     if (M->ArgCount != ArgCount) {
-       PPError ("Macro argument count mismatch");
-       /* Be sure to make enough empty arguments available */
-       while (ArgCount < M->ArgCount) {
-           M->ActualArgs [ArgCount++] = "";
-       }
+       PPError ("Macro argument count mismatch");
+       /* Be sure to make enough empty arguments available */
+       while (ArgCount < M->ArgCount) {
+           M->ActualArgs [ArgCount++] = "";
+       }
     }
 
     /* Preprocess the line, replacing macro parameters */
@@ -570,20 +608,10 @@ static int Pass1 (const char* From, char* To)
            mptr = CopyQuotedString (mptr);
        } else if (CurC == '/' && NextC == '*') {
            keepch (' ');
-           Comment ();
+           OldStyleComment ();
        } else if (ANSI == 0 && CurC == '/' && NextC == '/') {
            keepch (' ');
-           /* Beware: Because line continuation chars are handled when reading
-            * lines, we may only skip til the end of the source line, which
-            * may not be the same as the end of the input line. The end of the
-            * source line is denoted by a lf (\n) character.
-            */
-           do {
-               NextChar ();
-           } while (CurC != '\n' && CurC != '\0');
-           if (CurC == '\n') {
-               NextChar ();
-           }
+           NewStyleComment ();
        } else {
            *mptr++ = CurC;
            NextChar ();
@@ -744,7 +772,7 @@ static int DoIf (int Skip)
     NextToken ();
 
     /* Call the expression parser */
-    constexpr (&lval);
+    ConstExpr (&lval);
 
     /* End preprocessing mode */
     Preprocessing = 0;
@@ -906,7 +934,7 @@ void Preprocess (void)
 
                            case PP_ELSE:
                                if (IfIndex >= 0) {
-                           if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
+                           if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
                                if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
                                    Skip = !Skip;
                                }