From 0da97c67115a80ac14c2ae90a5cda18b81c224d7 Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 8 Jun 2005 11:31:00 +0000 Subject: [PATCH] Fixed a bug: Compiling an empty source file led to an internal error. Changed the lineinfo module to take dynamically allocated string buffers instead of char*. git-svn-id: svn://svn.cc65.org/cc65/trunk@3523 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/input.c | 14 +++++++++++--- src/cc65/lineinfo.c | 41 ++++++++++++++++++++++------------------- src/cc65/lineinfo.h | 9 +++++++-- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/cc65/input.c b/src/cc65/input.c index 6a26869f2..d093f4103 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -205,6 +205,9 @@ static IFile* FindFile (const char* Name) void OpenMainFile (const char* Name) /* Open the main file. Will call Fatal() in case of failures. */ { + AFile* MainFile; + + /* Setup a new IFile structure for the main file */ IFile* IF = NewIFile (Name); @@ -216,10 +219,15 @@ void OpenMainFile (const char* Name) } /* Allocate a new AFile structure for the file */ - (void) NewAFile (IF, F); + MainFile = NewAFile (IF, F); /* Allocate the input line buffer */ Line = NewStrBuf (); + + /* Update the line infos, so we have a valid line info even at start of + * the main file before the first line is read. + */ + UpdateLineInfo (MainFile->Input, MainFile->Line, Line); } @@ -340,7 +348,7 @@ static void GetInputChar (void) void NextChar (void) /* Skip the current input character and read the next one from the input - * stream. CurC and NextC are valid after the call. If end of line is + * stream. CurC and NextC are valid after the call. If end of line is * reached, both are set to NUL, no more lines are read by this function. */ { @@ -478,7 +486,7 @@ int NextLine (void) InitLine (Line); /* Create line information for this line */ - UpdateLineInfo (Input->Input, Input->Line, SB_GetConstBuf (Line)); + UpdateLineInfo (Input->Input, Input->Line, Line); /* Done */ return 1; diff --git a/src/cc65/lineinfo.c b/src/cc65/lineinfo.c index 342eeaced..cdb409551 100644 --- a/src/cc65/lineinfo.c +++ b/src/cc65/lineinfo.c @@ -64,22 +64,25 @@ static LineInfo* CurLineInfo = 0; -static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Line) +static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const StrBuf* Line) /* Create and return a new line info. Ref count will be 1. */ { - unsigned Len; - LineInfo* LI; - char* T; + unsigned Len; + LineInfo* LI; + const char* S; + char* T; + + /* Get the length of the line and a pointer to the line buffer */ + Len = SB_GetLen (Line); + S = SB_GetConstBuf (Line); /* Skip leading spaces in Line */ - while (IsBlank (*Line)) { - ++Line; + while (Len > 0 && IsBlank (*S)) { + ++S; + --Len; } - /* Calculate the length of the line */ - Len = strlen (Line); - - /* Allocate memory */ + /* Allocate memory for the line info and the input line */ LI = xmalloc (sizeof (LineInfo) + Len); /* Initialize the fields */ @@ -93,13 +96,13 @@ static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Lin */ T = LI->Line; while (Len--) { - if (*Line == '\t') { - *T = ' '; - } else { - *T = *Line; - } - ++Line; - ++T; + if (*S == '\t') { + *T = ' '; + } else { + *T = *S; + } + ++S; + ++T; } /* Add the terminator */ @@ -153,7 +156,7 @@ LineInfo* GetCurLineInfo (void) -void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line) +void UpdateLineInfo (struct IFile* F, unsigned LineNum, const StrBuf* Line) /* Update the line info - called if a new line is read */ { /* If a current line info exists, release it */ @@ -165,7 +168,7 @@ void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line) * of the supplied one to save some memory. */ if (!AddSource) { - Line = ""; + Line = &EmptyStrBuf; } /* Create a new line info */ diff --git a/src/cc65/lineinfo.h b/src/cc65/lineinfo.h index 3a19ca0ae..6d0f2d05c 100644 --- a/src/cc65/lineinfo.h +++ b/src/cc65/lineinfo.h @@ -37,6 +37,11 @@ #define LINEINFO_H + +/* common */ +#include "strbuf.h" + + /*****************************************************************************/ /* Forwards */ @@ -45,7 +50,7 @@ /* Input file structure */ -struct IFile; +struct IFile; @@ -87,7 +92,7 @@ LineInfo* GetCurLineInfo (void); * increased, use UseLineInfo for that purpose. */ -void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line); +void UpdateLineInfo (struct IFile* F, unsigned LineNum, const StrBuf* Line); /* Update the line info - called if a new line is read */ const char* GetInputName (const LineInfo* LI);