Merge pull request #1898 from acqn/PPFix

[cc65] Fixed '\\' + newline
This commit is contained in:
Bob Andrews
2022-11-02 18:09:04 +01:00
committed by GitHub
4 changed files with 49 additions and 10 deletions

View File

@@ -578,15 +578,16 @@ int NextLine (void)
SB_Drop (Line, 1); SB_Drop (Line, 1);
} }
/* If we don't have a line continuation character at the end, /* If we don't have a line continuation character at the end, we
** we're done with this line. Otherwise replace the character ** are done with this line. Otherwise just skip the character and
** by a newline and continue reading. ** continue reading.
*/ */
if (SB_LookAtLast (Line) == '\\') { if (SB_LookAtLast (Line) != '\\') {
Line->Buf[Line->Len-1] = '\n';
} else {
Input->MissingNL = 0; Input->MissingNL = 0;
break; break;
} else {
SB_Drop (Line, 1);
ContinueLine ();
} }
} else if (C != '\0') { /* Ignore embedded NULs */ } else if (C != '\0') { /* Ignore embedded NULs */

View File

@@ -114,7 +114,8 @@ static StrBuf* MLine; /* Buffer for macro expansion in #pragma */
static StrBuf* OLine; /* Buffer for #pragma output */ static StrBuf* OLine; /* Buffer for #pragma output */
/* Newlines to be added to preprocessed text */ /* Newlines to be added to preprocessed text */
static int PendingNewLines; static unsigned PendingNewLines;
static unsigned ContinuedLines;
static int FileChanged; static int FileChanged;
/* Structure used when expanding macros */ /* 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) static void AddPreLine (StrBuf* Str)
/* Add newlines to the string buffer */ /* Add newlines to the string buffer */
{ {
/* No need to prettify the non-exist output */
if (!PreprocessOnly) { if (!PreprocessOnly) {
PendingNewLines = 0; PendingNewLines = 0;
ContinuedLines = 0;
return; return;
} }
/* We'll adjust the line number later if necessary */
PendingNewLines += ContinuedLines;
if (FileChanged || PendingNewLines > 6) { if (FileChanged || PendingNewLines > 6) {
/* Output #line directives as source info */ /* Output #line directives as source info */
StrBuf Comment = AUTO_STRBUF_INITIALIZER; StrBuf Comment = AUTO_STRBUF_INITIALIZER;
if (SB_NotEmpty (Str) && SB_LookAtLast (Str) != '\n') { if (SB_NotEmpty (Str) && SB_LookAtLast (Str) != '\n') {
SB_AppendChar (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); SB_Append (Str, &Comment);
} else { } else {
/* Output new lines */ /* Output new lines */
@@ -846,6 +853,7 @@ static void AddPreLine (StrBuf* Str)
} }
FileChanged = 0; FileChanged = 0;
PendingNewLines = 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 */ /* Read the actual macro arguments */
while (1) { while (1) {
/* Squeeze runs of blanks within an arg */ /* Squeeze runs of blanks within an arg */
int OldPendingNewLines = PendingNewLines; unsigned OldPendingNewLines = PendingNewLines;
int Skipped = SkipWhitespace (MultiLine); int Skipped = SkipWhitespace (MultiLine);
/* Directives can only be found in an argument list that spans /* Directives can only be found in an argument list that spans
** multiple lines. ** multiple lines.
*/ */
if (MultiLine && OldPendingNewLines < PendingNewLines && CurC == '#') { if (MultiLine && OldPendingNewLines < PendingNewLines && CurC == '#') {
int Newlines = 0; unsigned Newlines = 0;
while (OldPendingNewLines < PendingNewLines && CurC == '#') { while (OldPendingNewLines < PendingNewLines && CurC == '#') {
Newlines += PendingNewLines - OldPendingNewLines; 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) void PreprocessBegin (void)
/* Initialize preprocessor with current file */ /* Initialize preprocessor with current file */
{ {

View File

@@ -68,6 +68,9 @@ void Preprocess (void);
void SetPPIfStack (PPIfStack* Stack); void SetPPIfStack (PPIfStack* Stack);
/* Specify which PP #if stack to use */ /* Specify which PP #if stack to use */
void ContinueLine (void);
/* Continue the current line ended with a '\\' */
void PreprocessBegin (void); void PreprocessBegin (void);
/* Initialize preprocessor with current file */ /* Initialize preprocessor with current file */

19
test/val/bug1891.c Normal file
View File

@@ -0,0 +1,19 @@
/* bug #1891 - backslash/newline sequence in string constants is treated wrong */
#include <stdio.h>
#include <string.h>
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;
}