]> git.sur5r.net Git - cc65/blobdiff - src/cc65/lineinfo.c
Fixed two compiler warnings.
[cc65] / src / cc65 / lineinfo.c
index fbf954134e91c4fe6b1211b5ee0e96d19e69ab41..e99d5201f69f6a6856bf14de495f6083982ffbfe 100644 (file)
 #include <string.h>
 
 /* common */
+#include "chartype.h"
 #include "check.h"
 #include "xmalloc.h"
 
 /* cc65 */
+#include "global.h"
 #include "input.h"
 #include "lineinfo.h"
 
@@ -62,20 +64,49 @@ 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. */
 {
-    /* Calculate the length of the line */
-    unsigned Len = strlen (Line);
+    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 (Len > 0 && IsBlank (*S)) {
+               ++S;
+        --Len;
+    }
 
-    /* Allocate memory */
-    LineInfo* LI = xmalloc (sizeof (LineInfo) + Len);
+    /* Allocate memory for the line info and the input line */
+    LI = xmalloc (sizeof (LineInfo) + Len);
 
     /* Initialize the fields */
     LI->RefCount  = 1;
     LI->InputFile = F;
     LI->LineNum   = LineNum;
-    memcpy (LI->Line, Line, Len+1);
+
+    /* Copy the line, replacing tabs by spaces in the given line since tabs
+     * will give rather arbitrary results when used in the output later, and
+     * if we do it here, we won't need another copy later.
+     */
+    T = LI->Line;
+    while (Len--) {
+               if (*S == '\t') {
+                   *T = ' ';
+               } else {
+                   *T = *S;
+               }
+               ++S;
+               ++T;
+    }
+
+    /* Add the terminator */
+    *T = '\0';
 
     /* Return the new struct */
     return LI;
@@ -125,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 */
@@ -133,6 +164,13 @@ void UpdateLineInfo (struct IFile* F, unsigned LineNum, const char* Line)
        ReleaseLineInfo (CurLineInfo);
     }
 
+    /* If we have intermixed assembly switched off, use an empty line instead
+     * of the supplied one to save some memory.
+     */
+    if (!AddSource) {
+       Line = &EmptyStrBuf;
+    }
+
     /* Create a new line info */
     CurLineInfo = NewLineInfo (F, LineNum, Line);
 }
@@ -143,7 +181,7 @@ const char* GetInputName (const LineInfo* LI)
 /* Return the file name from a line info */
 {
     PRECONDITION (LI != 0);
-    return LI->InputFile->Name;
+    return GetInputFile (LI->InputFile);
 }