diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 66cbb2a9d..1ee810060 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -2812,11 +2812,30 @@ static void DoInclude (void) InputType IT; StrBuf Filename = AUTO_STRBUF_INITIALIZER; - /* Macro-replace a single line with special support for */ - SB_Clear (MLine); - PreprocessDirective (Line, MLine, MSM_TOK_HEADER); + /* Skip whitespace so the input pointer points to the argument */ + SkipWhitespace (0); - /* Read from the processed line */ + /* We may have three forms of the #include directive: + ** + ** - # include "q-char-sequence" new-line + ** - # include new-line + ** - # include pp-tokens new-line + ** + ** The former two are processed as is while the latter is preprocessed and + ** must then resemble one of the first two forms. + */ + if (CurC == '"' || CurC == '<') { + /* Copy the argument part over to MLine */ + unsigned Start = SB_GetIndex (Line); + unsigned Length = SB_GetLen (Line) - Start; + SB_Slice (MLine, Line, Start, Length); + } else { + /* Macro-replace a single line with special support for */ + SB_Clear (MLine); + PreprocessDirective (Line, MLine, MSM_TOK_HEADER); + } + + /* Read from the copied/preprocessed line */ SB_Reset (MLine); MLine = InitLine (MLine); diff --git a/test/val/bug2458.c b/test/val/bug2458.c new file mode 100644 index 000000000..1494a7946 --- /dev/null +++ b/test/val/bug2458.c @@ -0,0 +1,10 @@ +#define str(arg) #arg +#include str(bug2458.h) /* Ok, macro replacement */ + +#define string foo +#include /* Ok, no macro replacement */ + +int main() +{ + return 0; +} diff --git a/test/val/bug2458.h b/test/val/bug2458.h new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/test/val/bug2458.h @@ -0,0 +1 @@ +