]> andersk Git - splint.git/blobdiff - src/lcllib.c
Fixed problem with assertion checking for negative shifts in
[splint.git] / src / lcllib.c
index a7a51400ef38a7c2011cc9ff454038a309fd5bd7..46b028ed13f67e875b8176f464818990e3b213d6 100644 (file)
@@ -1,6 +1,6 @@
 /*
-** LCLint - annotation-assisted static program checker
-** Copyright (C) 1994-2000 University of Virginia,
+** Splint - annotation-assisted static program checker
+** Copyright (C) 1994-2003 University of Virginia,
 **         Massachusetts Institute of Technology
 **
 ** This program is free software; you can redistribute it and/or modify it
@@ -17,9 +17,9 @@
 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 ** MA 02111-1307, USA.
 **
-** For information on lclint: lclint-request@cs.virginia.edu
-** To report a bug: lclint-bug@cs.virginia.edu
-** For more information: http://lclint.cs.virginia.edu
+** For information on splint: info@splint.org
+** To report a bug: splint-bug@splint.org
+** For more information: http://www.splint.org
 */
 /*
 ** lcllib.c
 **
 */
 
-# include "lclintMacros.nf"
-# include "llbasic.h"
+/*
+ * Herbert 02/17/2002:
+ * - fixed the recognition of Posix headers for OS/2
+ */
 
+# include "splintMacros.nf"
+# include "basic.h"
 # include "osd.h"
+# include "version.h"
 
-# ifndef NOLCL
 # include "gram.h"
-
 # include "lclscan.h"
-# endif
 
-
-# include "herald.h"
 # include "lcllib.h"
 # include "llmain.h"
-# include "portab.h"
 
 /*@-incondefs@*/ /*@-redecl@*/
-extern /*@dependent@*/ FILE *yyin;
+extern /*:open:*/ /*@dependent@*/ FILE *yyin;
 /*@=incondefs@*/ /*@=redecl@*/
 
 /*@constant int NUMLIBS; @*/
-# define NUMLIBS 17
+# define NUMLIBS 25
 
 /*@constant int NUMPOSIXLIBS; @*/
-# define NUMPOSIXLIBS 12
+# define NUMPOSIXLIBS 18
 
 static ob_mstring posixlibs[NUMPOSIXLIBS] = 
 {
-  "sys/stat",
-  "sys/types",
   "dirent",
   "fcntl",
   "grp",
   "pwd",
+  "regex",
+  "sys/stat",
   "sys/times",
+  "sys/types", 
+  "netdb", /* unix */
+  "netinet/in", /* unix */
+  "sys/resource", /* unix */
+  "sys/socket", /* not posix */
+  "sys/syslog", /* not posix */
   "sys/utsname",
   "sys/wait",
   "termios",
@@ -77,25 +82,33 @@ static ob_mstring posixlibs[NUMPOSIXLIBS] =
 static ob_mstring stdlibs[NUMLIBS] =
 {
   "assert", 
+  "complex"
   "ctype",
   "errno",
-  "float", 
+  "fenv",
+  "float",
+  "inttypes",
+  "iso646",
   "limits",
   "locale",
   "math", 
   "setjmp",
   "signal",
   "stdarg",
+  "stdbool",
   "stddef",
+  "stdint",
   "stdio",
   "stdlib",
-  "strings", 
   "string",
+  "strings", /* some systems use this...they shouldn't */
+  "tgmath",
   "time",
-  "wchar"
+  "wchar",
+  "wctype"
 } ;
 
-static bool loadStateFile (FILE * p_f, cstring p_name);
+static bool loadLCDFile (FILE * p_f, cstring p_name);
 
 bool
 lcllib_isSkipHeader (cstring sname)
@@ -103,18 +116,31 @@ lcllib_isSkipHeader (cstring sname)
   int i;
   bool posixlib = FALSE;
   char *libname;
-  char *name = cstring_toCharsSafe (sname);
   char *matchname;
+  cstring xname;
 
   llassert (cstring_isDefined (sname));
-  name = removeExtension (name, ".h");
-
-  libname = strrchr (name, CONNECTCHAR);
+  xname = fileLib_withoutExtension (sname, cstring_makeLiteralTemp (".h"));
+
+  DPRINTF (("Include? %s", sname));
+
+  /*@access cstring@*/
+  llassert (cstring_isDefined (xname));
+# if defined (OS2)
+  {
+    /* Posixlibs use forward slashes, so we use them here, too */
+    cstring_replaceAll (xname, '\\', '/');
+    libname = strrchr (xname, '/');
+    DPRINTF (("libname: %s", libname));
+  }
+# else
+  libname = strrchr (xname, CONNECTCHAR);
+# endif
   matchname = libname;
 
   if (libname == NULL) 
     {
-      libname = name;
+      libname = xname;
     }
   else
     {
@@ -129,25 +155,24 @@ lcllib_isSkipHeader (cstring sname)
       
       voptgenerror 
        (FLG_USEVARARGS,
-        message ("Include file <%s> is inconsistent with "
+        message ("Include file <%s.h> is inconsistent with "
                  "ANSI library (should use <stdarg.h>)",
                  cstring_fromChars (libname)),
         tmp);
       
       fileloc_free (tmp);
-      sfree (name);
+      cstring_free (xname);
       return TRUE;
     }
 
-  if (context_getFlag (FLG_SKIPANSIHEADERS)
+  if (context_getFlag (FLG_SKIPISOHEADERS)
       && context_usingAnsiLibrary ())
     {
-      
       for (i = 0; i < NUMLIBS; i++)
        {
          if (mstring_equal (libname, stdlibs[i]))
            {
-             sfree (name);
+             sfree (xname);
              return TRUE;
            }
        }
@@ -155,11 +180,16 @@ lcllib_isSkipHeader (cstring sname)
 
   for (i = 0; i < NUMPOSIXLIBS; i++)
     {
-      if (strchr (posixlibs[i], CONNECTCHAR) != NULL)
+      if (strchr (posixlibs[i], CONNECTCHAR) != NULL
+# if defined (OS2)
+         || strchr (posixlibs[i], ALTCONNECTCHAR) != NULL
+# endif
+         )
        {
          char *ptr;
          
-         if ((ptr = strstr (name, posixlibs[i])) != NULL) 
+         DPRINTF (("xname: %s, posix: %s", xname, posixlibs[i]));
+         if ((ptr = strstr (xname, posixlibs[i])) != NULL) 
            {
              if (ptr[strlen (posixlibs[i])] == '\0')
                {
@@ -191,8 +221,15 @@ lcllib_isSkipHeader (cstring sname)
        {
          if (context_getFlag (FLG_SKIPPOSIXHEADERS))
            {
-             sfree (name);
-             return TRUE;
+             cstring_free (xname);
+             /*@-nullstate@*/ 
+             return TRUE; 
+             /*@=nullstate@*/
+
+             /* evans 2002-03-02: 
+                  the returned reference is possibly null,
+                  but this should not change the null state of the parameter
+             */
            }
        }
       else
@@ -201,7 +238,7 @@ lcllib_isSkipHeader (cstring sname)
          
          voptgenerror 
            (FLG_WARNPOSIX,
-            message ("Include file <%s> matches the name of a "
+            message ("Include file <%s.h> matches the name of a "
                      "POSIX library, but the POSIX library is "
                      "not being used.  Consider using +posixlib "
                      "or +posixstrictlib to select the POSIX "
@@ -214,38 +251,31 @@ lcllib_isSkipHeader (cstring sname)
        }
     }
 
-  sfree (name);
+  cstring_free (xname);
+  /*@noaccess cstring@*/
+  /*@-nullstate@*/ /* same problem as above */
   return FALSE;
+  /*@=nullstate@*/
 }
 
 static void printDot (void)
 {
-  if (context_getFlag (FLG_SHOWSCAN)) 
-    {
-      (void) fflush (g_msgstream);
-      fprintf (stderr, "."); 
-      (void) fflush (stderr);
-    }
+  displayScanContinue (cstring_makeLiteralTemp ("."));
 }
 
 void
 dumpState (cstring cfname)
 {
   FILE *f;
-  char *fname = cstring_toCharsSafe (cfname);
-  
-  fname = addExtension (fname, DUMP_SUFFIX);
+  cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
   
-  f = fopen (fname, "w");
+  f = fileTable_openWriteFile (context_fileTable (), fname);
 
-  if (context_getFlag (FLG_SHOWSCAN))
-    {
-      fprintf (stderr, "< Dumping to %s ", fname); 
-    }
+  displayScanOpen (message ("Dumping to %s ", fname)); 
   
   if (f == NULL)
     {
-      llgloberror (message ("Cannot open dump file for writing: %s", cfname));
+      lldiagmsg (message ("Cannot open dump file for writing: %s", fname));
     }
   else
     {
@@ -256,51 +286,57 @@ dumpState (cstring cfname)
 
       printDot ();
 
+      /*
+      DPRINTF (("Before prepare dump:"));
+      ctype_printTable ();
+      DPRINTF (("Preparing dump..."));
+      */
+
       usymtab_prepareDump ();
 
       /*
-      ** Be careful, these lines must match loadStateFile checking.
+      ** Be careful, these lines must match loadLCDFile checking.
       */
 
-      fprintf (f, ";;LCLint Dump: %s\n", fname);
-      fprintf (f, ";;%s\n", LCL_VERSION);
+      fprintf (f, "%s %s\n", LIBRARY_MARKER, cstring_toCharsSafe (fname));
+      fprintf (f, ";;Splint %f\n", SPLINT_LIBVERSION);
       fprintf (f, ";;lib:%d\n", (int) context_getLibrary ());
       fprintf (f, ";;ctTable\n");
-
+      
+      DPRINTF (("Dumping types..."));
       printDot ();
-            ctype_dumpTable (f);
+      ctype_dumpTable (f);
       printDot ();
-
+      
+      DPRINTF (("Dumping type sets..."));
       fprintf (f, ";;tistable\n");
       typeIdSet_dumpTable (f);
       printDot ();
-
-            fprintf (f, ";;symTable\n");
+      
+      DPRINTF (("Dumping usymtab..."));
+      fprintf (f, ";;symTable\n");
       usymtab_dump (f);
       printDot ();
 
+      DPRINTF (("Dumping modules..."));
       fprintf (f, ";; Modules access\n");
       context_dumpModuleAccess (f);
       fprintf (f, ";;End\n");
-      check (fclose (f) == 0);
-    }
-
-  if (context_getFlag (FLG_SHOWSCAN))
-    {
-      fprintf (g_msgstream, " >\n");
+      check (fileTable_closeFile (context_fileTable (), f));
     }
 
-  sfree (fname);
+  displayScanClose ();
+  cstring_free (fname);
 }
 
 bool
 loadStandardState ()
 {
-  char *fpath;
+  cstring fpath;
   FILE *stdlib;
   bool result;
-  char *libname = addExtension (context_selectedLibrary (), DUMP_SUFFIX);
-  
+  cstring libname = fileLib_addExtension (context_selectedLibrary (), 
+                                         cstring_makeLiteralTemp (DUMP_SUFFIX));
   
   if (osd_findOnLarchPath (libname, &fpath) != OSD_FILEFOUND)
     {
@@ -308,18 +344,18 @@ loadStandardState ()
                          cstring_makeLiteralTemp 
                          (context_getFlag (FLG_STRICTLIB) ? "strict " 
                           : (context_getFlag (FLG_UNIXLIB) ? "unix " : "")),
-                         cstring_makeLiteralTemp (libname)));
+                         libname));
       lldiagmsg (cstring_makeLiteral ("     Check LARCH_PATH environment variable."));
       result = FALSE;
     }
   else
     {
-      stdlib = fopen (fpath, "r");
+      stdlib = fileTable_openReadFile (context_fileTable (), fpath);
 
       if (stdlib == NULL)
        {
          lldiagmsg (message ("Cannot read standard library: %s",
-                         cstring_fromChars (fpath)));
+                             fpath));
          lldiagmsg (cstring_makeLiteral ("     Check LARCH_PATH environment variable."));
 
          result = FALSE;
@@ -331,12 +367,12 @@ loadStandardState ()
              char *t = mstring_create (MAX_NAME_LENGTH);
              char *ot = t;
 
-             if (fgets (t, MAX_NAME_LENGTH, stdlib) == NULL)
+             if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) == NULL)
                {
                  llfatalerror (cstring_makeLiteral ("Standard library format invalid"));
                }
 
-             if (fgets (t, MAX_NAME_LENGTH, stdlib) != NULL)
+             if ((t = reader_readLine (stdlib, t, MAX_NAME_LENGTH)) != NULL)
                {
                  if (*t == ';' && *(t + 1) == ';') 
                    {
@@ -347,7 +383,7 @@ loadStandardState ()
              if (t == NULL)
                {
                  lldiagmsg (message ("Standard library: %s <cannot read creation information>", 
-                                 cstring_fromChars (fpath)));
+                                     fpath));
                }
              else
                {
@@ -357,37 +393,33 @@ loadStandardState ()
                  if (tt != NULL)
                    *tt = '\0';
 
-                 lldiagmsg (message ("Standard library: %s", cstring_fromChars (fpath)));
-                 lldiagmsg (message ("   (created using %s)", cstring_fromChars (t)));
+                 lldiagmsg (message ("Standard library: %s", fpath));
+                 /* evans 2004-01-13: removed this (it is the libversion which is confusing) */
+                 /*   lldiagmsg (message ("   (created using %s)", cstring_fromChars (t)));                */
                }
 
              sfree (ot);
              
-             check (fclose (stdlib) == 0);
-             stdlib = fopen (fpath, "r");
+             check (fileTable_closeFile (context_fileTable (), stdlib));
+             stdlib = fileTable_openReadFile (context_fileTable (), fpath);
            }
 
          llassert (stdlib != NULL);
 
          fileloc_reallyFree (g_currentloc);
-         g_currentloc = fileloc_createLib (cstring_makeLiteralTemp (libname));
+         g_currentloc = fileloc_createLib (libname);
 
-         if (context_getDebug (FLG_SHOWSCAN))
-           {
-             context_hideShowscan ();
-             result = loadStateFile (stdlib, cstring_fromChars (fpath));
-             context_unhideShowscan ();
-           }
-         else
-           {
-             result = loadStateFile (stdlib, cstring_fromChars (fpath));
-           }
+         DPRINTF (("Loading: %s", fpath));
 
-         check (fclose (stdlib) == 0);
+         displayScanOpen (message ("loading standard library %s ", fpath));
+         result = loadLCDFile (stdlib, fpath);
+         displayScanClose ();
+
+         check (fileTable_closeFile (context_fileTable (), stdlib));
        }
     }
 
-  sfree (libname);
+  cstring_free (libname);
   return result;
 }
 
@@ -395,51 +427,52 @@ loadStandardState ()
 # define BUFLEN 128
 
 static bool
-loadStateFile (FILE *f, cstring name)
+loadLCDFile (FILE *f, cstring name)
 {
   char buf[BUFLEN];
   
   /*
-  ** Check version.  Should be >= LCL_MIN_VERSION
+  ** Check version.  Should be >= SPLINT_LIBVERSION
   */
 
-  if ((fgets (buf, BUFLEN, f) == NULL)
-      || !mstring_equalPrefix (buf, ";;LCLint Dump:"))
+  if (reader_readLine (f, buf, BUFLEN) == NULL
+      || !mstring_equalPrefix (buf, LIBRARY_MARKER))
     {
-      loadllmsg (message ("Load library %s is not in LCLint library format.  Attempting "
-                     "to continue without library.", name));
+      loadllmsg (message ("Load library %s is not in Splint library format.  Attempting "
+                         "to continue without library.", name));
       return FALSE;
     }
   
-  if (fgets (buf, BUFLEN, f) != NULL)
+  if (reader_readLine (f, buf, BUFLEN) != NULL)
     {
       if (!mstring_equalPrefix (buf, ";;"))
        {
-         loadllmsg (message ("Load library %s is not in LCLint library format.  Attempting "
-                         "to continue without library.", name));
+         loadllmsg (message ("Load library %s is not in Splint library format.  Attempting "
+                             "to continue without library.", name));
          return FALSE;
        }
       else if (mstring_equalPrefix (buf, ";;ctTable"))
        {
-         loadllmsg (message ("Load library %s is in obsolete LCLint library format.  Attempting "
-                         "to continue anyway, but results may be incorrect.  Rebuild "
-                         "the library with this version of lclint.", 
-                         name));
+         loadllmsg (message ("Load library %s is in obsolete Splint library format.  Attempting "
+                             "to continue anyway, but results may be incorrect.  Rebuild "
+                             "the library with this version of splint.", 
+                             name));
        }
       else 
        {
          float version = 0.0;
 
-         if (sscanf (buf, ";;LCLint %f", &version) != 1)
+         if (sscanf (buf, ";;Splint %f", &version) != 1
+             && (sscanf (buf, ";;LCLint %f", &version) != 1))
            {
-             loadllmsg (message ("Load library %s is not in LCLint library format (missing version "
-                             "number).  Attempting "
-                             "to continue without library.", name));
+             loadllmsg (message ("Load library %s is not in Splint library format (missing version "
+                                 "number).  Attempting "
+                                 "to continue without library.", name));
              return FALSE;
            }
          else
            {
-             if ((LCL_MIN_VERSION - version) >= FLT_EPSILON)
+             if ((SPLINT_LIBVERSION - version) >= FLT_EPSILON)
                {
                  cstring vname;
                  char *nl = strchr (buf, '\n');
@@ -448,17 +481,20 @@ loadStateFile (FILE *f, cstring name)
 
                  vname = cstring_fromChars (buf + 9);
 
-                 loadllmsg (message ("Load library %s is in obsolete LCLint library "
-                                     "format (version %s).  Attempting "
+                 loadllmsg (message ("Load library %s is in obsolete Splint library "
+                                     "format (version %f (%s), expecting version %f).  Attempting "
                                      "to continue anyway, but results may be incorrect.  Rebuild "
-                                     "the library with this version of lclint.", 
-                                     name, vname));
+                                     "the library with this version of splint.", 
+                                     name, 
+                                     version,
+                                     vname, 
+                                     SPLINT_LIBVERSION));
                }
              else
                {
-                 if ((fgets (buf, BUFLEN, f) == NULL))
+                 if (reader_readLine (f, buf, BUFLEN) == NULL)
                    {
-                     loadllmsg (message ("Load library %s is not in LCLint library "
+                     loadllmsg (message ("Load library %s is not in Splint library "
                                          "format (missing library code). Attempting "
                                          "to continue without library.", name));
                      return FALSE;
@@ -469,7 +505,7 @@ loadStateFile (FILE *f, cstring name)
                      
                      if (sscanf (buf, ";;lib:%d", &lib) != 1)
                        {
-                         loadllmsg (message ("Load library %s is not in LCLint library "
+                         loadllmsg (message ("Load library %s is not in Splint library "
                                              "format (missing library code). Attempting "
                                              "to continue without library.", name));
                          return FALSE;
@@ -487,9 +523,10 @@ loadStateFile (FILE *f, cstring name)
                            }
                          else
                            {
-                             loadllmsg (message ("Load library %s has invalid library code.  "
+                             loadllmsg (message ("Load library %s has invalid library code (%s).  "
                                                  "Attempting to continue without library.",
-                                                 name));
+                                                 name,
+                                                 flagcode_unparse (code)));
                              
                              return FALSE;
                            }
@@ -501,7 +538,7 @@ loadStateFile (FILE *f, cstring name)
     }
   else
     {
-      loadllmsg (message ("Load library %s is not in LCLint library format (missing lines).  "
+      loadllmsg (message ("Load library %s is not in Splint library format (missing lines).  "
                          "Attempting to continue without library.", name));
       return FALSE;
     }
@@ -529,26 +566,22 @@ void
 loadState (cstring cfname)
 {
   FILE *f;
-  char *fname = cstring_toCharsSafe (cfname);
-  cstring ofname = cstring_copy (cfname);
+  cstring fname = fileLib_addExtension (cfname, cstring_makeLiteralTemp (DUMP_SUFFIX));
 
-  fname = addExtension (fname, DUMP_SUFFIX);
-
-  f = fopen (fname, "r");
+  f = fileTable_openReadFile (context_fileTable (), fname);
 
   if (f == NULL)
     {
-      if (context_getDebug (FLG_SHOWSCAN))
-       fprintf (g_msgstream, " >\n");
-
-      llfatalerror (message ("Cannot open dump file for loading: %s", cfname));
+      displayScanClose ();
+      llfatalerror (message ("Cannot open dump file for loading: %s", 
+                            fname));
     }
   else
     {
       fileloc_reallyFree (g_currentloc);
-      g_currentloc = fileloc_createLib (ofname);
+      g_currentloc = fileloc_createLib (cfname);
 
-      if (!loadStateFile (f, ofname)) 
+      if (!loadLCDFile (f, cfname)) 
        {
          if (!loadStandardState ()) 
            {
@@ -556,10 +589,10 @@ loadState (cstring cfname)
            }
        }
       
-      check (fclose (f) == 0);
+      check (fileTable_closeFile (context_fileTable (), f));
     }
 
-  cstring_free (ofname);
-  sfree (fname);
+  /* usymtab_printAll (); */
+  cstring_free (fname);
 }
 
This page took 0.103905 seconds and 4 git commands to generate.