]> git.sur5r.net Git - cc65/blobdiff - src/cc65/input.c
Fixed multi line macro bug
[cc65] / src / cc65 / input.c
index d4b278f1d00778df25565b7c43d0e9b36c90b012..6db7df5f8e508b5661c5838f86797edd13d2f94f 100644 (file)
@@ -36,6 +36,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 /* common */
 #include "check.h"
 
 /* cc65 */
 #include "asmcode.h"
+#include "codegen.h"
 #include "error.h"
-#include "global.h"
 #include "incpath.h"
+#include "lineinfo.h"
 #include "input.h"
 
 
@@ -69,20 +72,12 @@ char NextC = '\0';
 /* Maximum count of nested includes */
 #define MAX_INC_NESTING        16
 
-/* Struct that describes an input file */
-typedef struct IFile IFile;
-struct IFile {
-    unsigned   Index;          /* File index                           */
-    unsigned   Usage;          /* Usage counter                        */
-    char               Name[1];        /* Name of file (dynamically allocated) */
-};
-
 /* Struct that describes an active input file */
 typedef struct AFile AFile;
 struct AFile {
     unsigned   Line;           /* Line number for this file            */
     FILE*      F;              /* Input file stream                    */
-    const char*        Name;           /* Points to corresponding IFile name   */
+    IFile*      Input;          /* Points to corresponding IFile        */
 };
 
 /* List of all input files */
@@ -94,7 +89,7 @@ static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
 
 
 /*****************************************************************************/
-/*                              struct IFile                                */
+/*                              struct IFile                                */
 /*****************************************************************************/
 
 
@@ -106,11 +101,13 @@ static IFile* NewIFile (const char* Name)
     unsigned Len = strlen (Name);
 
     /* Allocate a IFile structure */
-    IFile* IF = xmalloc (sizeof (IFile) + Len);
+    IFile* IF = (IFile*) xmalloc (sizeof (IFile) + Len);
 
     /* Initialize the fields */
     IF->Index = CollCount (&IFiles) + 1;
     IF->Usage = 0;
+    IF->Size  = 0;
+    IF->MTime = 0;
     memcpy (IF->Name, Name, Len+1);
 
     /* Insert the new structure into the IFile collection */
@@ -132,15 +129,31 @@ static AFile* NewAFile (IFile* IF, FILE* F)
 /* Create and return a new AFile */
 {
     /* Allocate a AFile structure */
-    AFile* AF = xmalloc (sizeof (AFile));
+    AFile* AF = (AFile*) xmalloc (sizeof (AFile));
 
     /* Initialize the fields */
     AF->Line  = 0;
     AF->F     = F;
-    AF->Name  = IF->Name;
+    AF->Input = IF;
+
+    /* Increment the usage counter of the corresponding IFile. If this
+     * is the first use, set the file data and output debug info if
+     * requested.
+     */
+    if (IF->Usage++ == 0) {
 
-    /* Increment the usage counter of the corresponding IFile */
-    ++IF->Usage;
+       /* Get file size and modification time */
+       struct stat Buf;
+       if (fstat (fileno (F), &Buf) != 0) {
+           /* Error */
+           Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
+       }
+               IF->Size  = (unsigned long) Buf.st_size;
+       IF->MTime = (unsigned long) Buf.st_mtime;
+
+       /* Set the debug data */
+       g_fileinfo (IF->Name, IF->Size, IF->MTime);
+    }
 
     /* Insert the new structure into the AFile collection */
     CollAppend (&AFiles, AF);
@@ -174,7 +187,7 @@ static IFile* FindFile (const char* Name)
     unsigned I;
     for (I = 0; I < CollCount (&IFiles); ++I) {
        /* Get the file struct */
-       IFile* IF = CollAt (&IFiles, I);
+       IFile* IF = (IFile*) CollAt (&IFiles, I);
        /* Check the name */
        if (strcmp (Name, IF->Name) == 0) {
            /* Found, return the struct */
@@ -198,7 +211,7 @@ void OpenMainFile (const char* Name)
     FILE* F = fopen (Name, "r");
     if (F == 0) {
                /* Cannot open */
-               Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
+               Fatal ("Cannot open input file `%s': %s", Name, strerror (errno));
     }
 
     /* Allocate a new AFile structure for the file */
@@ -216,14 +229,14 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
 
     /* Check for the maximum include nesting */
     if (CollCount (&AFiles) > MAX_INC_NESTING) {
-       PPError (ERR_INCLUDE_NESTING);
+       PPError ("Include nesting too deep");
        return;
     }
 
     /* Search for the file */
     N = FindInclude (Name, DirSpec);
     if (N == 0) {
-       PPError (ERR_INCLUDE_NOT_FOUND, Name);
+       PPError ("Include file `%s' not found", Name);
        return;
     }
 
@@ -242,7 +255,7 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
     F = fopen (IF->Name, "r");
     if (F == 0) {
        /* Error opening the file */
-       PPError (ERR_INCLUDE_OPEN_FAILURE, IF->Name, strerror (errno));
+       PPError ("Cannot open include file `%s': %s", IF->Name, strerror (errno));
        return;
     }
 
@@ -266,7 +279,7 @@ static void CloseIncludeFile (void)
     PRECONDITION (AFileCount > 0);
 
     /* Get the current active input file */
-    Input = CollLast (&AFiles);
+    Input = (AFile*) CollLast (&AFiles);
 
     /* Close the current input file (we're just reading so no error check) */
     fclose (Input->F);
@@ -342,7 +355,7 @@ int NextLine (void)
     if (CollCount (&AFiles) == 0) {
        return 0;
     }
-    Input = CollLast (&AFiles);
+    Input = (AFile*) CollLast (&AFiles);
 
     /* Read lines until we get one with real contents */
     Len = 0;
@@ -363,7 +376,7 @@ int NextLine (void)
            if (CollCount (&AFiles) == 0) {
                return 0;
            }
-           Input = CollLast (&AFiles);
+           Input = (AFile*) CollLast (&AFiles);
 
                }
 
@@ -379,13 +392,6 @@ int NextLine (void)
        }
        line [Len] = '\0';
 
-       /* Output the source line in the generated assembler file
-        * if requested.
-        */
-       if (AddSource && line[Start] != '\0') {
-           AddCodeLine ("; %s", line+Start);
-       }
-
        /* Check if we have a line continuation character at the end. If not,
         * we're done.
         */
@@ -399,6 +405,9 @@ int NextLine (void)
     /* Got a line. Initialize the current and next characters. */
     InitLine (line);
 
+    /* Create line information for this line */
+    UpdateLineInfo (Input->Input, Input->Line, line);
+
     /* Done */
     return 1;
 }
@@ -410,13 +419,13 @@ const char* GetCurrentFile (void)
 {
     unsigned AFileCount = CollCount (&AFiles);
     if (AFileCount > 0) {
-       const AFile* AF = CollAt (&AFiles, AFileCount-1);
-       return AF->Name;
+       const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
+       return AF->Input->Name;
     } else {
        /* No open file. Use the main file if we have one. */
        unsigned IFileCount = CollCount (&IFiles);
        if (IFileCount > 0) {
-           const IFile* IF = CollAt (&IFiles, 0);
+           const IFile* IF = (const IFile*) CollAt (&IFiles, 0);
            return IF->Name;
        } else {
            return "(outside file scope)";
@@ -431,7 +440,7 @@ unsigned GetCurrentLine (void)
 {
     unsigned AFileCount = CollCount (&AFiles);
     if (AFileCount > 0) {
-       const AFile* AF = CollAt (&AFiles, AFileCount-1);
+       const AFile* AF = (const AFile*) CollAt (&AFiles, AFileCount-1);
        return AF->Line;
     } else {
        /* No open file */
@@ -455,7 +464,7 @@ void WriteDependencies (FILE* F, const char* OutputFile)
     /* Loop over all files */
     for (I = 0; I < IFileCount; ++I) {
        /* Get the next input file */
-       const IFile* IF = CollAt (&IFiles, I);
+       const IFile* IF = (const IFile*) CollAt (&IFiles, I);
        /* If this is not the first file, add a space */
        const char* Format = (I == 0)? "%s" : " %s";
        /* Print the dependency */