From 0063f73f8a4a80df6e271aaadd9fc2204e0c900a Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 24 Jul 2022 23:19:05 +0800 Subject: [PATCH] Fixed __FILE__ and __LINE__ macros for preprocessor. --- src/cc65/compile.c | 4 ++++ src/cc65/macrotab.c | 5 +++++ src/cc65/preproc.c | 18 ++++++++++++++++++ src/cc65/preproc.h | 13 +++++++++++++ src/cc65/scanner.c | 11 +---------- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/cc65/compile.c b/src/cc65/compile.c index 75250da0a..d6069e914 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -396,6 +396,10 @@ void Compile (const char* FileName) DefineNumericMacro ("__EAGERLY_INLINE_FUNCS__", 1); } + /* Placeholders for __FILE__ and __LINE__ macros */ + DefineTextMacro ("__FILE__", ""); + DefineTextMacro ("__LINE__", ""); + /* __TIME__ and __DATE__ macros */ Time = time (0); TM = localtime (&Time); diff --git a/src/cc65/macrotab.c b/src/cc65/macrotab.c index 37b52351f..0e80cd638 100644 --- a/src/cc65/macrotab.c +++ b/src/cc65/macrotab.c @@ -42,6 +42,7 @@ /* cc65 */ #include "error.h" +#include "preproc.h" #include "macrotab.h" @@ -246,6 +247,10 @@ Macro* FindMacro (const char* Name) Macro* M = MacroTab[Hash]; while (M) { if (strcmp (M->Name, Name) == 0) { + /* Check for some special macro names */ + if (Name[0] == '_') { + HandleSpecialMacro (M, Name); + } /* Found it */ return M; } diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index f1328c4d0..6dcbeea70 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -1533,6 +1533,24 @@ static int ParseDirectives (unsigned ModeFlags) +void HandleSpecialMacro (Macro* M, const char* Name) +/* Handle special mandatory macros */ +{ + if (strcmp (Name, "__LINE__") == 0) { + /* Replace __LINE__ with the current line number */ + SB_Printf (&M->Replacement, "%u", GetCurrentLine ()); + } else if (strcmp (Name, "__FILE__") == 0) { + /* Replace __FILE__ with the current filename */ + StrBuf B = AUTO_STRBUF_INITIALIZER; + SB_InitFromString (&B, GetCurrentFile ()); + SB_Clear (&M->Replacement); + Stringize (&B, &M->Replacement); + SB_Done (&B); + } +} + + + /*****************************************************************************/ /* Preprocessing */ /*****************************************************************************/ diff --git a/src/cc65/preproc.h b/src/cc65/preproc.h index f543b05b5..2143dde98 100644 --- a/src/cc65/preproc.h +++ b/src/cc65/preproc.h @@ -38,6 +38,16 @@ +/*****************************************************************************/ +/* Forwards */ +/*****************************************************************************/ + + + +typedef struct Macro Macro; + + + /*****************************************************************************/ /* Data */ /*****************************************************************************/ @@ -80,6 +90,9 @@ void InitPreprocess (void); void DonePreprocess (void); /* Done with preprocessor */ +void HandleSpecialMacro (Macro* M, const char* Name); +/* Handle special mandatory macros */ + /* End of preproc.h */ diff --git a/src/cc65/scanner.c b/src/cc65/scanner.c index ebdcdb33e..09dd8fef8 100644 --- a/src/cc65/scanner.c +++ b/src/cc65/scanner.c @@ -835,16 +835,7 @@ void NextToken (void) /* No reserved word, check for special symbols */ if (token[0] == '_' && token[1] == '_') { /* Special symbols */ - if (strcmp (token+2, "FILE__") == 0) { - NextTok.SVal = AddLiteral (GetCurrentFile()); - NextTok.Tok = TOK_SCONST; - return; - } else if (strcmp (token+2, "LINE__") == 0) { - NextTok.Tok = TOK_ICONST; - NextTok.IVal = GetCurrentLine(); - NextTok.Type = type_int; - return; - } else if (strcmp (token+2, "func__") == 0) { + if (strcmp (token+2, "func__") == 0) { /* __func__ is only defined in functions */ if (CurrentFunc) { NextTok.SVal = AddLiteral (F_GetFuncName (CurrentFunc));