Fixed __FILE__ and __LINE__ macros for preprocessor.

This commit is contained in:
acqn
2022-07-24 23:19:05 +08:00
parent 8605393953
commit 0063f73f8a
5 changed files with 41 additions and 10 deletions

View File

@@ -396,6 +396,10 @@ void Compile (const char* FileName)
DefineNumericMacro ("__EAGERLY_INLINE_FUNCS__", 1); DefineNumericMacro ("__EAGERLY_INLINE_FUNCS__", 1);
} }
/* Placeholders for __FILE__ and __LINE__ macros */
DefineTextMacro ("__FILE__", "");
DefineTextMacro ("__LINE__", "");
/* __TIME__ and __DATE__ macros */ /* __TIME__ and __DATE__ macros */
Time = time (0); Time = time (0);
TM = localtime (&Time); TM = localtime (&Time);

View File

@@ -42,6 +42,7 @@
/* cc65 */ /* cc65 */
#include "error.h" #include "error.h"
#include "preproc.h"
#include "macrotab.h" #include "macrotab.h"
@@ -246,6 +247,10 @@ Macro* FindMacro (const char* Name)
Macro* M = MacroTab[Hash]; Macro* M = MacroTab[Hash];
while (M) { while (M) {
if (strcmp (M->Name, Name) == 0) { if (strcmp (M->Name, Name) == 0) {
/* Check for some special macro names */
if (Name[0] == '_') {
HandleSpecialMacro (M, Name);
}
/* Found it */ /* Found it */
return M; return M;
} }

View File

@@ -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 */ /* Preprocessing */
/*****************************************************************************/ /*****************************************************************************/

View File

@@ -38,6 +38,16 @@
/*****************************************************************************/
/* Forwards */
/*****************************************************************************/
typedef struct Macro Macro;
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@@ -80,6 +90,9 @@ void InitPreprocess (void);
void DonePreprocess (void); void DonePreprocess (void);
/* Done with preprocessor */ /* Done with preprocessor */
void HandleSpecialMacro (Macro* M, const char* Name);
/* Handle special mandatory macros */
/* End of preproc.h */ /* End of preproc.h */

View File

@@ -835,16 +835,7 @@ void NextToken (void)
/* No reserved word, check for special symbols */ /* No reserved word, check for special symbols */
if (token[0] == '_' && token[1] == '_') { if (token[0] == '_' && token[1] == '_') {
/* Special symbols */ /* Special symbols */
if (strcmp (token+2, "FILE__") == 0) { if (strcmp (token+2, "func__") == 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) {
/* __func__ is only defined in functions */ /* __func__ is only defined in functions */
if (CurrentFunc) { if (CurrentFunc) {
NextTok.SVal = AddLiteral (F_GetFuncName (CurrentFunc)); NextTok.SVal = AddLiteral (F_GetFuncName (CurrentFunc));