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
This commit is contained in:
uz
2011-01-20 20:54:30 +00:00
parent ddb7296b6c
commit 3b59a8ca6f
8 changed files with 67 additions and 58 deletions

View File

@@ -133,7 +133,7 @@ void DbgInfoLine (void)
} }
/* Remember the line info */ /* Remember the line info */
GenLineInfo (Index, LineNum); GenLineInfo (Index, LineNum, 0);
} }
@@ -147,4 +147,4 @@ void DbgInfoSym (void)

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2001 Ullrich von Bassewitz */ /* (C) 2001-2011, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* 70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* 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. */ /* Create and return a new line info. Usage will be zero. */
{ {
/* Allocate memory */ /* Allocate memory */
@@ -83,9 +83,9 @@ static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum)
/* Initialize the fields */ /* Initialize the fields */
LI->Usage = 0; LI->Usage = 0;
LI->Index = 0; /* Currently invalid */ LI->Index = 0; /* Currently invalid */
LI->Pos.Line = LineNum; LI->Pos.Line = Line;
LI->Pos.Col = 0; LI->Pos.Col = Col;
LI->Pos.Name = FileIndex; LI->Pos.Name = File;
/* Insert this structure into the collection */ /* Insert this structure into the collection */
CollAppend (&LineInfoColl, LI); 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 */ /* Generate a new line info */
{ {
/* Create a new line info and make it current */ /* 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_) const void* LI1_, const void* LI2_)
/* Compare function for the sort */ /* Compare function for the sort */
{ {
@@ -162,7 +162,7 @@ static int CmpLineInfo (void* Data attribute ((unused)),
} }
} }
void MakeLineInfoIndex (void) void MakeLineInfoIndex (void)
/* Sort the line infos and drop all unreferenced ones */ /* Sort the line infos and drop all unreferenced ones */

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 2001 Ullrich von Bassewitz */ /* (C) 2001-2011, Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Roemerstrasse 52 */
/* D-70597 Stuttgart */ /* 70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* 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. * 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 */ /* Generate a new line info */
void ClearLineInfo (void); void ClearLineInfo (void);

View File

@@ -331,7 +331,7 @@ void MacDef (unsigned Style)
/* Parse a macro definition */ /* Parse a macro definition */
{ {
Macro* M; Macro* M;
TokNode* T; TokNode* N;
int HaveParams; int HaveParams;
/* We expect a macro name here */ /* We expect a macro name here */
@@ -489,7 +489,7 @@ void MacDef (unsigned Style)
} }
/* Create a token node for the current token */ /* 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 the token is an ident, check if it is a local parameter */
if (CurTok.Tok == TOK_IDENT) { if (CurTok.Tok == TOK_IDENT) {
@@ -498,8 +498,8 @@ void MacDef (unsigned Style)
while (I) { while (I) {
if (SB_Compare (&I->Id, &CurTok.SVal) == 0) { if (SB_Compare (&I->Id, &CurTok.SVal) == 0) {
/* Local param name, replace it */ /* Local param name, replace it */
T->Tok = TOK_MACPARAM; N->T.Tok = TOK_MACPARAM;
T->IVal = Count; N->T.IVal = Count;
break; break;
} }
++Count; ++Count;
@@ -510,11 +510,11 @@ void MacDef (unsigned Style)
/* Insert the new token in the list */ /* Insert the new token in the list */
if (M->TokCount == 0) { if (M->TokCount == 0) {
/* First token */ /* First token */
M->TokRoot = M->TokLast = T; M->TokRoot = M->TokLast = N;
} else { } else {
/* We have already tokens */ /* We have already tokens */
M->TokLast->Next = T; M->TokLast->Next = N;
M->TokLast = T; M->TokLast = N;
} }
++M->TokCount; ++M->TokCount;

View File

@@ -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;
}

View File

@@ -303,6 +303,11 @@ INLINE int TokIsSep (enum token_t T)
# define TokIsSep(T) ((T) == TOK_SEP || (T) == TOK_EOF) # define TokIsSep(T) ((T) == TOK_SEP || (T) == TOK_EOF)
#endif #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 */ /* End of token.h */

View File

@@ -57,62 +57,55 @@
TokNode* NewTokNode (void) TokNode* NewTokNode (void)
/* Create and return a token node with the current token value */ /* Create and return a token node with the current token value */
{ {
TokNode* T;
/* Allocate memory */ /* Allocate memory */
T = xmalloc (sizeof (TokNode)); TokNode* N = xmalloc (sizeof (TokNode));
/* Initialize the token contents */ /* Initialize the token contents */
T->Next = 0; N->Next = 0;
T->Tok = CurTok.Tok; SB_Init (&N->T.SVal);
T->WS = CurTok.WS; CopyToken (&N->T, &CurTok);
T->IVal = CurTok.IVal;
SB_Init (&T->SVal);
SB_Copy (&T->SVal, &CurTok.SVal);
/* Return the node */ /* Return the node */
return T; return N;
} }
void FreeTokNode (TokNode* T) void FreeTokNode (TokNode* N)
/* Free the given token node */ /* Free the given token node */
{ {
SB_Done (&T->SVal); SB_Done (&N->T.SVal);
xfree (T); xfree (N);
} }
void TokSet (TokNode* T) void TokSet (TokNode* N)
/* Set the scanner token from the given token node */ /* Set the scanner token from the given token node */
{ {
/* Set the values */ /* Set the values */
CurTok.Tok = T->Tok; CopyToken (&CurTok, &N->T);
CurTok.WS = T->WS;
CurTok.IVal = T->IVal;
SB_Copy (&CurTok.SVal, &T->SVal);
SB_Terminate (&CurTok.SVal); 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 */ /* Compare the token given as parameter against the current token */
{ {
if (T->Tok != CurTok.Tok) { if (N->T.Tok != CurTok.Tok) {
/* Different token */ /* Different token */
return tcDifferent; return tcDifferent;
} }
/* If the token has string attribute, check it */ /* If the token has string attribute, check it */
if (TokHasSVal (T->Tok)) { if (TokHasSVal (N->T.Tok)) {
if (SB_Compare (&CurTok.SVal, &T->SVal) != 0) { if (SB_Compare (&CurTok.SVal, &N->T.SVal) != 0) {
return tcSameToken; return tcSameToken;
} }
} else if (TokHasIVal (T->Tok)) { } else if (TokHasIVal (N->T.Tok)) {
if (T->IVal != CurTok.IVal) { if (N->T.IVal != CurTok.IVal) {
return tcSameToken; return tcSameToken;
} }
} }
@@ -278,4 +271,3 @@ void PushTokList (TokList* List, const char* Desc)

View File

@@ -49,17 +49,14 @@
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
/* Struct holding a token */ /* Struct holding a token */
typedef struct TokNode TokNode; typedef struct TokNode TokNode;
struct TokNode { struct TokNode {
TokNode* Next; /* For single linked list */ TokNode* Next; /* For single linked list */
token_t Tok; /* Token value */ Token T; /* Token value */
int WS; /* Whitespace before token? */
long IVal; /* Integer token attribute */
StrBuf SVal; /* String attribute, dyn. allocated */
}; };
/* Struct holding a token list */ /* Struct holding a token list */
@@ -95,13 +92,13 @@ enum TC {
TokNode* NewTokNode (void); TokNode* NewTokNode (void);
/* Create and return a token node with the current token value */ /* Create and return a token node with the current token value */
void FreeTokNode (TokNode* T); void FreeTokNode (TokNode* N);
/* Free the given token node */ /* Free the given token node */
void TokSet (TokNode* T); void TokSet (TokNode* N);
/* Set the scanner token from the given token node */ /* 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 */ /* Compare the token given as parameter against the current token */
void InitTokList (TokList* T); void InitTokList (TokList* T);