diff --git a/src/cc65/input.c b/src/cc65/input.c index 8c8007504..f01723783 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -578,15 +578,16 @@ int NextLine (void) SB_Drop (Line, 1); } - /* If we don't have a line continuation character at the end, - ** we're done with this line. Otherwise replace the character - ** by a newline and continue reading. + /* If we don't have a line continuation character at the end, we + ** are done with this line. Otherwise just skip the character and + ** continue reading. */ - if (SB_LookAtLast (Line) == '\\') { - Line->Buf[Line->Len-1] = '\n'; - } else { + if (SB_LookAtLast (Line) != '\\') { Input->MissingNL = 0; break; + } else { + SB_Drop (Line, 1); + ContinueLine (); } } else if (C != '\0') { /* Ignore embedded NULs */ diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 4bde66318..961e20e0d 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -114,7 +114,8 @@ static StrBuf* MLine; /* Buffer for macro expansion in #pragma */ static StrBuf* OLine; /* Buffer for #pragma output */ /* Newlines to be added to preprocessed text */ -static int PendingNewLines; +static unsigned PendingNewLines; +static unsigned ContinuedLines; static int FileChanged; /* Structure used when expanding macros */ @@ -824,18 +825,24 @@ static void CheckForBadIdent (const char* Ident, int Std, const Macro* M) static void AddPreLine (StrBuf* Str) /* Add newlines to the string buffer */ { + /* No need to prettify the non-exist output */ if (!PreprocessOnly) { PendingNewLines = 0; + ContinuedLines = 0; return; } + /* We'll adjust the line number later if necessary */ + PendingNewLines += ContinuedLines; + if (FileChanged || PendingNewLines > 6) { /* Output #line directives as source info */ StrBuf Comment = AUTO_STRBUF_INITIALIZER; if (SB_NotEmpty (Str) && SB_LookAtLast (Str) != '\n') { SB_AppendChar (Str, '\n'); } - SB_Printf (&Comment, "#line %u \"%s\"\n", GetCurrentLine (), GetCurrentFile ()); + SB_Printf (&Comment, "#line %u \"%s\"\n", + GetCurrentLine () - ContinuedLines, GetCurrentFile ()); SB_Append (Str, &Comment); } else { /* Output new lines */ @@ -846,6 +853,7 @@ static void AddPreLine (StrBuf* Str) } FileChanged = 0; PendingNewLines = 0; + ContinuedLines = 0; } @@ -1528,14 +1536,14 @@ static unsigned ReadMacroArgs (unsigned NameIdx, MacroExp* E, const Macro* M, in /* Read the actual macro arguments */ while (1) { /* Squeeze runs of blanks within an arg */ - int OldPendingNewLines = PendingNewLines; + unsigned OldPendingNewLines = PendingNewLines; int Skipped = SkipWhitespace (MultiLine); /* Directives can only be found in an argument list that spans ** multiple lines. */ if (MultiLine && OldPendingNewLines < PendingNewLines && CurC == '#') { - int Newlines = 0; + unsigned Newlines = 0; while (OldPendingNewLines < PendingNewLines && CurC == '#') { Newlines += PendingNewLines - OldPendingNewLines; @@ -3362,6 +3370,14 @@ void SetPPIfStack (PPIfStack* Stack) +void ContinueLine (void) +/* Continue the current line ended with a '\\' */ +{ + ++ContinuedLines; +} + + + void PreprocessBegin (void) /* Initialize preprocessor with current file */ { diff --git a/src/cc65/preproc.h b/src/cc65/preproc.h index 153d0d68a..e2a1b073c 100644 --- a/src/cc65/preproc.h +++ b/src/cc65/preproc.h @@ -68,6 +68,9 @@ void Preprocess (void); void SetPPIfStack (PPIfStack* Stack); /* Specify which PP #if stack to use */ +void ContinueLine (void); +/* Continue the current line ended with a '\\' */ + void PreprocessBegin (void); /* Initialize preprocessor with current file */ diff --git a/test/val/bug1891.c b/test/val/bug1891.c new file mode 100644 index 000000000..0373ba46d --- /dev/null +++ b/test/val/bug1891.c @@ -0,0 +1,19 @@ +/* bug #1891 - backslash/newline sequence in string constants is treated wrong */ + +#include +#include + +const char* a = "hello \ +world"; +const char* b = \ +"hello world"; + +int main(void) +{ + if (strcmp(a, b) != 0) { + printf("a:\n%s\n", a); + printf("b:\n%s\n", b); + return 1; + } + return 0; +}