1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-25 17:29:50 +00:00

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
This commit is contained in:
cuz 2005-06-08 11:31:00 +00:00
parent 7579aba638
commit 0da97c6711
3 changed files with 40 additions and 24 deletions

View File

@ -205,6 +205,9 @@ static IFile* FindFile (const char* Name)
void OpenMainFile (const char* Name) void OpenMainFile (const char* Name)
/* Open the main file. Will call Fatal() in case of failures. */ /* Open the main file. Will call Fatal() in case of failures. */
{ {
AFile* MainFile;
/* Setup a new IFile structure for the main file */ /* Setup a new IFile structure for the main file */
IFile* IF = NewIFile (Name); IFile* IF = NewIFile (Name);
@ -216,10 +219,15 @@ void OpenMainFile (const char* Name)
} }
/* Allocate a new AFile structure for the file */ /* Allocate a new AFile structure for the file */
(void) NewAFile (IF, F); MainFile = NewAFile (IF, F);
/* Allocate the input line buffer */ /* Allocate the input line buffer */
Line = NewStrBuf (); 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) void NextChar (void)
/* Skip the current input character and read the next one from the input /* 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. * reached, both are set to NUL, no more lines are read by this function.
*/ */
{ {
@ -478,7 +486,7 @@ int NextLine (void)
InitLine (Line); InitLine (Line);
/* Create line information for this line */ /* Create line information for this line */
UpdateLineInfo (Input->Input, Input->Line, SB_GetConstBuf (Line)); UpdateLineInfo (Input->Input, Input->Line, Line);
/* Done */ /* Done */
return 1; return 1;

View File

@ -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. */ /* Create and return a new line info. Ref count will be 1. */
{ {
unsigned Len; unsigned Len;
LineInfo* LI; LineInfo* LI;
char* T; 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 */ /* Skip leading spaces in Line */
while (IsBlank (*Line)) { while (Len > 0 && IsBlank (*S)) {
++Line; ++S;
--Len;
} }
/* Calculate the length of the line */ /* Allocate memory for the line info and the input line */
Len = strlen (Line);
/* Allocate memory */
LI = xmalloc (sizeof (LineInfo) + Len); LI = xmalloc (sizeof (LineInfo) + Len);
/* Initialize the fields */ /* Initialize the fields */
@ -93,13 +96,13 @@ static LineInfo* NewLineInfo (struct IFile* F, unsigned LineNum, const char* Lin
*/ */
T = LI->Line; T = LI->Line;
while (Len--) { while (Len--) {
if (*Line == '\t') { if (*S == '\t') {
*T = ' '; *T = ' ';
} else { } else {
*T = *Line; *T = *S;
} }
++Line; ++S;
++T; ++T;
} }
/* Add the terminator */ /* 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 */ /* Update the line info - called if a new line is read */
{ {
/* If a current line info exists, release it */ /* 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. * of the supplied one to save some memory.
*/ */
if (!AddSource) { if (!AddSource) {
Line = ""; Line = &EmptyStrBuf;
} }
/* Create a new line info */ /* Create a new line info */

View File

@ -37,6 +37,11 @@
#define LINEINFO_H #define LINEINFO_H
/* common */
#include "strbuf.h"
/*****************************************************************************/ /*****************************************************************************/
/* Forwards */ /* Forwards */
@ -45,7 +50,7 @@
/* Input file structure */ /* Input file structure */
struct IFile; struct IFile;
@ -87,7 +92,7 @@ LineInfo* GetCurLineInfo (void);
* increased, use UseLineInfo for that purpose. * 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 */ /* Update the line info - called if a new line is read */
const char* GetInputName (const LineInfo* LI); const char* GetInputName (const LineInfo* LI);