From 3b59a8ca6f53ef93da0d8292096892f80180f40a Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 20 Jan 2011 20:54:30 +0000 Subject: [PATCH] Mark tokens with the file position from where they're read. Restore this position for tokens read from a token list. This means that line info does now show the actual point of definition. This is an improvement but needs to be refined. git-svn-id: svn://svn.cc65.org/cc65/trunk@4911 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/dbginfo.c | 4 ++-- src/ca65/lineinfo.c | 24 ++++++++++++------------ src/ca65/lineinfo.h | 10 +++++----- src/ca65/macro.c | 14 +++++++------- src/ca65/token.c | 15 +++++++++++++++ src/ca65/token.h | 5 +++++ src/ca65/toklist.c | 40 ++++++++++++++++------------------------ src/ca65/toklist.h | 13 +++++-------- 8 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/ca65/dbginfo.c b/src/ca65/dbginfo.c index 5466a00b0..fc80539b4 100644 --- a/src/ca65/dbginfo.c +++ b/src/ca65/dbginfo.c @@ -133,7 +133,7 @@ void DbgInfoLine (void) } /* Remember the line info */ - GenLineInfo (Index, LineNum); + GenLineInfo (Index, LineNum, 0); } @@ -147,4 +147,4 @@ void DbgInfoSym (void) - + diff --git a/src/ca65/lineinfo.c b/src/ca65/lineinfo.c index 6f81beaaf..2e674b719 100644 --- a/src/ca65/lineinfo.c +++ b/src/ca65/lineinfo.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@cc65.org */ +/* (C) 2001-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* 70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -74,7 +74,7 @@ LineInfo* CurLineInfo = 0; -static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum) +static LineInfo* NewLineInfo (unsigned File, unsigned long Line, unsigned Col) /* Create and return a new line info. Usage will be zero. */ { /* Allocate memory */ @@ -83,9 +83,9 @@ static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum) /* Initialize the fields */ LI->Usage = 0; LI->Index = 0; /* Currently invalid */ - LI->Pos.Line = LineNum; - LI->Pos.Col = 0; - LI->Pos.Name = FileIndex; + LI->Pos.Line = Line; + LI->Pos.Col = Col; + LI->Pos.Name = File; /* Insert this structure into the collection */ CollAppend (&LineInfoColl, LI); @@ -112,11 +112,11 @@ LineInfo* UseLineInfo (LineInfo* LI) -void GenLineInfo (unsigned FileIndex, unsigned long LineNum) +void GenLineInfo (unsigned FileIndex, unsigned long LineNum, unsigned ColNum) /* Generate a new line info */ { /* Create a new line info and make it current */ - CurLineInfo = NewLineInfo (FileIndex, LineNum); + CurLineInfo = NewLineInfo (FileIndex, LineNum, ColNum); } @@ -129,7 +129,7 @@ void ClearLineInfo (void) -static int CmpLineInfo (void* Data attribute ((unused)), +static int CmpLineInfo (void* Data attribute ((unused)), const void* LI1_, const void* LI2_) /* Compare function for the sort */ { @@ -162,7 +162,7 @@ static int CmpLineInfo (void* Data attribute ((unused)), } } - + void MakeLineInfoIndex (void) /* Sort the line infos and drop all unreferenced ones */ diff --git a/src/ca65/lineinfo.h b/src/ca65/lineinfo.h index 1414fe364..0d1ca495d 100644 --- a/src/ca65/lineinfo.h +++ b/src/ca65/lineinfo.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2001 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@cc65.org */ +/* (C) 2001-2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* 70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -93,7 +93,7 @@ LineInfo* UseLineInfo (LineInfo* LI); * function will gracefully accept NULL pointers and do nothing in this case. */ -void GenLineInfo (unsigned FileIndex, unsigned long LineNum); +void GenLineInfo (unsigned FileIndex, unsigned long LineNum, unsigned ColNum); /* Generate a new line info */ void ClearLineInfo (void); diff --git a/src/ca65/macro.c b/src/ca65/macro.c index 2d2dedeb6..bca119d99 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -331,7 +331,7 @@ void MacDef (unsigned Style) /* Parse a macro definition */ { Macro* M; - TokNode* T; + TokNode* N; int HaveParams; /* We expect a macro name here */ @@ -489,7 +489,7 @@ void MacDef (unsigned Style) } /* Create a token node for the current token */ - T = NewTokNode (); + N = NewTokNode (); /* If the token is an ident, check if it is a local parameter */ if (CurTok.Tok == TOK_IDENT) { @@ -498,8 +498,8 @@ void MacDef (unsigned Style) while (I) { if (SB_Compare (&I->Id, &CurTok.SVal) == 0) { /* Local param name, replace it */ - T->Tok = TOK_MACPARAM; - T->IVal = Count; + N->T.Tok = TOK_MACPARAM; + N->T.IVal = Count; break; } ++Count; @@ -510,11 +510,11 @@ void MacDef (unsigned Style) /* Insert the new token in the list */ if (M->TokCount == 0) { /* First token */ - M->TokRoot = M->TokLast = T; + M->TokRoot = M->TokLast = N; } else { /* We have already tokens */ - M->TokLast->Next = T; - M->TokLast = T; + M->TokLast->Next = N; + M->TokLast = N; } ++M->TokCount; diff --git a/src/ca65/token.c b/src/ca65/token.c index b389b8511..5fb3d2fc8 100644 --- a/src/ca65/token.c +++ b/src/ca65/token.c @@ -60,3 +60,18 @@ int TokHasIVal (token_t Tok) +void CopyToken (Token* Dst, const Token* Src) +/* Copy a token from Src to Dst. The current value of Dst.SVal is free'd, + * so Dst must be initialized. + */ +{ + /* Copy the fields */ + Dst->Tok = Src->Tok; + Dst->WS = Src->WS; + Dst->IVal = Src->IVal; + SB_Copy (&Dst->SVal, &Src->SVal); + Dst->Pos = Src->Pos; +} + + + diff --git a/src/ca65/token.h b/src/ca65/token.h index ea4da599b..cbff651d8 100644 --- a/src/ca65/token.h +++ b/src/ca65/token.h @@ -303,6 +303,11 @@ INLINE int TokIsSep (enum token_t T) # define TokIsSep(T) ((T) == TOK_SEP || (T) == TOK_EOF) #endif +void CopyToken (Token* Dst, const Token* Src); +/* Copy a token. The current value of Dst.SVal is free'd, so Dst must be + * initialized. + */ + /* End of token.h */ diff --git a/src/ca65/toklist.c b/src/ca65/toklist.c index d5ec5ee6c..12e125e67 100644 --- a/src/ca65/toklist.c +++ b/src/ca65/toklist.c @@ -57,62 +57,55 @@ TokNode* NewTokNode (void) /* Create and return a token node with the current token value */ { - TokNode* T; /* Allocate memory */ - T = xmalloc (sizeof (TokNode)); + TokNode* N = xmalloc (sizeof (TokNode)); /* Initialize the token contents */ - T->Next = 0; - T->Tok = CurTok.Tok; - T->WS = CurTok.WS; - T->IVal = CurTok.IVal; - SB_Init (&T->SVal); - SB_Copy (&T->SVal, &CurTok.SVal); + N->Next = 0; + SB_Init (&N->T.SVal); + CopyToken (&N->T, &CurTok); /* Return the node */ - return T; + return N; } -void FreeTokNode (TokNode* T) +void FreeTokNode (TokNode* N) /* Free the given token node */ { - SB_Done (&T->SVal); - xfree (T); + SB_Done (&N->T.SVal); + xfree (N); } -void TokSet (TokNode* T) +void TokSet (TokNode* N) /* Set the scanner token from the given token node */ { /* Set the values */ - CurTok.Tok = T->Tok; - CurTok.WS = T->WS; - CurTok.IVal = T->IVal; - SB_Copy (&CurTok.SVal, &T->SVal); + CopyToken (&CurTok, &N->T); SB_Terminate (&CurTok.SVal); } -enum TC TokCmp (const TokNode* T) +enum TC TokCmp (const TokNode* N) /* Compare the token given as parameter against the current token */ { - if (T->Tok != CurTok.Tok) { + if (N->T.Tok != CurTok.Tok) { /* Different token */ return tcDifferent; } /* If the token has string attribute, check it */ - if (TokHasSVal (T->Tok)) { - if (SB_Compare (&CurTok.SVal, &T->SVal) != 0) { + if (TokHasSVal (N->T.Tok)) { + if (SB_Compare (&CurTok.SVal, &N->T.SVal) != 0) { return tcSameToken; } - } else if (TokHasIVal (T->Tok)) { - if (T->IVal != CurTok.IVal) { + } else if (TokHasIVal (N->T.Tok)) { + if (N->T.IVal != CurTok.IVal) { return tcSameToken; } } @@ -278,4 +271,3 @@ void PushTokList (TokList* List, const char* Desc) - diff --git a/src/ca65/toklist.h b/src/ca65/toklist.h index 08e64d0d1..b31df38af 100644 --- a/src/ca65/toklist.h +++ b/src/ca65/toklist.h @@ -49,17 +49,14 @@ /*****************************************************************************/ /* Data */ /*****************************************************************************/ - + /* Struct holding a token */ typedef struct TokNode TokNode; struct TokNode { TokNode* Next; /* For single linked list */ - token_t Tok; /* Token value */ - int WS; /* Whitespace before token? */ - long IVal; /* Integer token attribute */ - StrBuf SVal; /* String attribute, dyn. allocated */ + Token T; /* Token value */ }; /* Struct holding a token list */ @@ -95,13 +92,13 @@ enum TC { TokNode* NewTokNode (void); /* Create and return a token node with the current token value */ -void FreeTokNode (TokNode* T); +void FreeTokNode (TokNode* N); /* Free the given token node */ -void TokSet (TokNode* T); +void TokSet (TokNode* N); /* Set the scanner token from the given token node */ -enum TC TokCmp (const TokNode* T); +enum TC TokCmp (const TokNode* N); /* Compare the token given as parameter against the current token */ void InitTokList (TokList* T); -- 2.39.5