Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

general.c File Reference

#include "lclintMacros.nf"
#include "basic.h"
#include "portab.h"
#include <stdlib.h>

Go to the source code of this file.

Functions

void sfree (void *x)
void sfreeEventually (void *x)
void* dimalloc (size_t size, char *name, int line)
void* dicalloc (size_t num, size_t size, char *name, int line)
void* direalloc ( void *x, size_t size, char *name, int line)
char* FormatInt (int i)
bool isCext (char *ext)
bool isLCLfile (cstring s)
char* removeExtension ( char *s, const char *suffix)
bool firstWord (char *s, char *w)
char* removePath (char *s)
char* removePathFree ( char *s)
void mstring_markFree (char *s)
char* removeAnyExtension (char *s)
char* addExtension (char *s, const char *suffix)
int getInt (char **s)
char loadChar (char **s)
double getDouble (char **s)
char* getWord (char **s)
bool optCheckChar (char **s, char c)
void docheckChar (char **s, char c, char *file, int line)
char* mstring_spaces (int n)
char* mstring_concat (const char *s1, const char *s2)
char* mstring_concatFree ( char *s1, char *s2)
char* mstring_concatFree1 ( char *s1, const char *s2)
char* mstring_append ( char *s1, char c)
char* mstring_copy (char *s1)
char* mstring_safePrint (char *s)
char* mstring_create (int n)
void fputline (FILE *out, char *s)
int int_log (int x)
long unsigned int longUnsigned_fromInt (int x)
size_t size_fromInt (int x)
int size_toInt (size_t x)
long size_toLong (size_t x)
char char_fromInt (int x)
int longUnsigned_toInt (long unsigned int x)
int long_toInt (long int x)
bool mstring_equalPrefix (const char *c1, const char *c2)
bool mstring_equal ( const char *s1, const char *s2)
char* filenameExtension ( char *s)
char* removePreDirs (char *s)
void checkUngetc (int c, FILE *f)
bool isHeaderFile (cstring fname)


Function Documentation

char * FormatInt ( int i )
 

Definition at line 173 of file general.c.

00174 {
00175   char temp[255]; /* assume the integer has at most 254 digits */
00176   char *outs;
00177 
00178   sprintf (temp, "%i", i);
00179   outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
00180   strcpy (outs, temp);
00181 
00182   return (outs);
00183 }

char * addExtension ( char * s,
const char * suffix )
 

Definition at line 304 of file general.c.

Referenced by dumpState(), loadStandardState(), and loadState().

00305 {
00306   if (strrchr (s, '.') == (char *) 0)
00307     {
00308       /* <<< was mstring_concatFree1 --- bug detected by lclint >>> */
00309       return (mstring_concat (s, suffix));
00310     }
00311   else
00312     {
00313       return mstring_copy (s);
00314     }
00315 }

char char_fromInt ( int x )
 

Definition at line 632 of file general.c.

00633 {
00634   llassert ((x >= (int)'\0') && (x <= (int)'~'));
00635 
00636   return ((char) x);
00637 }

void checkUngetc ( int c,
FILE * f )
 

Definition at line 719 of file general.c.

Referenced by swallowMacro().

00720 {
00721   int res;
00722 
00723   llassert (c != EOF);
00724   res = ungetc (c, f);
00725   llassert (res == c);
00726 }

void * dicalloc ( size_t num,
size_t size,
char * name,
int line )
 

Definition at line 130 of file general.c.

00131 {
00132   void *ret = (void *) calloc (num, size);
00133 
00134   if (ret == NULL)
00135     {
00136       llfatalerrorLoc 
00137         (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
00138                   size_toLongUnsigned (size),
00139                   cstring_fromChars (name), line));
00140     }
00141   
00142   return ret;
00143 }

void * dimalloc ( size_t size,
char * name,
int line )
 

Definition at line 86 of file general.c.

00087 {
00088   /*
00089   static void *lastaddr = 0;
00090   static int numallocs = 0;
00091   static int numbad = 0;
00092   */
00093 
00094   /* was malloc, use calloc to initialize to zero */
00095   void *ret = (void *) calloc (1, size);
00096 
00097   /*
00098   numallocs++;
00099 
00100   if (ret < lastaddr)
00101     {
00102       numbad++;
00103       fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
00104     }
00105 
00106   lastaddr = ret;
00107   */
00108 
00109   if (ret == NULL)
00110     {
00111       if (size == 0)
00112         {
00113           llbug (message ("Zero allocation at %q.",
00114                           fileloc_unparseRaw (cstring_fromChars (name), line)));
00115         }
00116       else
00117         {
00118           llfatalerrorLoc
00119             (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
00120                       size_toLongUnsigned (size),
00121                       cstring_fromChars (name), line));
00122         }
00123     }
00124       
00125   /*@-null@*/ /* null okay for size = 0 */
00126   return ret; 
00127   /*@=null@*/
00128 }

void * direalloc ( void * x,
size_t size,
char * name,
int line )
 

Definition at line 145 of file general.c.

00147 {
00148   void *ret;
00149 
00150   if (x == NULL)
00151     {                                  
00152       ret = (void *) dmalloc (size);
00153     }
00154   else
00155     {
00156       ret = (void *) realloc (x, size);
00157     }
00158 
00159   if (ret == NULL)
00160     {
00161       llfatalerrorLoc
00162         (message ("Out of memory.  Allocating %w bytes at %s:%d.", 
00163                   size_toLongUnsigned (size),
00164                   cstring_fromChars (name), line));
00165     }
00166   
00167   return ret;
00168 }

void docheckChar ( char ** s,
char c,
char * file,
int line )
 

Definition at line 439 of file general.c.

00440 {
00441   /*@unchecked@*/ static int nbadchars = 0;
00442 
00443   if (**s == c)
00444     {
00445       (*s)++;
00446     }
00447   else
00448     {
00449       nbadchars++;
00450 
00451       if (nbadchars > 5)
00452         {
00453           llfatalbug (cstring_makeLiteral 
00454                       ("checkChar: Too many errors.  Check library is up to date."));
00455         }
00456       else
00457         {
00458           llbug (message ("checkChar: %q: Bad char, expecting %h: %s",
00459                           fileloc_unparseRaw (cstring_fromChars (file), line),
00460                           c,
00461                           cstring_fromChars (*s)));
00462         }
00463     }
00464 }

char * filenameExtension ( char * s )
 

Definition at line 694 of file general.c.

Referenced by context_isSpecialFile(), isHeaderFile(), and isLCLfile().

00695 {
00696   llassert (s != NULL);
00697 
00698   return (strrchr(s, '.'));
00699 }

bool firstWord ( char * s,
char * w )
 

Definition at line 228 of file general.c.

Referenced by o_fctInfo(), processImport(), sort_import(), and symtable_import().

00229 {
00230   llassert (s != NULL);
00231   llassert (w != NULL);
00232   
00233   for (; *w != '\0'; w++, s++)
00234     {
00235       if (*w != *s || *s == '\0')
00236         return FALSE;
00237     }
00238   return TRUE;
00239 }

void fputline ( FILE * out,
char * s )
 

Definition at line 573 of file general.c.

00574 {
00575   if (strlen (s) > 0) 
00576     {
00577       check (fputs (s, out) != EOF);
00578     }
00579 
00580   check (fputc ('\n', out) == (int) '\n');
00581 }

double getDouble ( char ** s )
 

Definition at line 385 of file general.c.

00386 {
00387   char *end = mstring_createEmpty ();
00388   double ret;
00389 
00390   ret = strtod (*s, &end);
00391 
00392   *s = end;
00393   return ret;
00394 }

int getInt ( char ** s )
 

Definition at line 318 of file general.c.

Referenced by GETPRINTF(), ctype_undump(), multiVal_undump(), sRef_undump(), sRef_undumpGlobal(), typeIdSet_undump(), and uentry_undump().

00319 {
00320   bool gotOne = FALSE;
00321   int i = 0;
00322 
00323   while (**s == ' ')
00324     {
00325       (*s)++;
00326     }
00327 
00328   if (**s == '-')
00329     {
00330       (*s)++;
00331       if (**s < '0' || **s > '9')
00332         {
00333           llbug (message ("getInt: bad int: %s", cstring_fromChars (*s))); 
00334         }
00335       else
00336         {
00337           i = -1 * (int) (**s - '0');
00338           gotOne = TRUE;
00339         }
00340 
00341       (*s)++;
00342     }
00343 
00344   while (**s >= '0' && **s <= '9')
00345     {
00346       i *= 10;
00347       i += (int) (**s - '0');
00348       (*s)++;
00349       gotOne = TRUE;
00350     }
00351 
00352   if (!gotOne)
00353     {
00354       llbug (message ("No int to read: %s", cstring_fromChars (*s)));
00355     }
00356 
00357   while (**s == '\n' || **s == ' ' || **s == '\t')
00358     {
00359       (*s)++;
00360     }
00361 
00362   return i;
00363 }

char * getWord ( char ** s )
 

Definition at line 401 of file general.c.

00402 {
00403   char *res;
00404   char *t = *s;
00405   char c;
00406 
00407   while ((c = **s) != '\0' && (c != ' ') && (c != ',') 
00408          && (c != '\n') && (c != '\t') && (c != '#'))
00409     {
00410       (*s)++;
00411     }
00412 
00413   if (*s == t)  
00414     {
00415       return NULL;
00416     }
00417 
00418   **s = '\0';
00419   res = mstring_copy (t);
00420   **s = c;
00421   return res;
00422 }

int int_log ( int x )
 

Definition at line 583 of file general.c.

Referenced by filelocList_unparseUses().

00584 {
00585   int ret = 1;
00586 
00587   while (x > 10)
00588     {
00589       ret++;
00590       x /= 10;
00591     }
00592 
00593   return ret;
00594 }

bool isCext ( char * ext )
 

Definition at line 187 of file general.c.

Referenced by isLCLfile(), and main().

00188 {
00189   return (!mstring_equal (ext, ".lcl") && 
00190           !mstring_equal (ext, ".spc") &&
00191           !mstring_equal (ext, ".ll"));
00192 }

bool isHeaderFile ( cstring fname )
 

Definition at line 728 of file general.c.

00729 {
00730   char *ext = filenameExtension (cstring_toCharsSafe (fname));
00731   
00732   return (mstring_equal (ext, ".h")
00733           || mstring_equal (ext, ".H")
00734           || mstring_equal (ext, ".lh"));
00735 }

bool isLCLfile ( cstring s )
 

Definition at line 195 of file general.c.

00196 {
00197   char *ext;
00198 
00199   ext = filenameExtension (cstring_toCharsSafe (s));
00200 
00201   if (ext != NULL)
00202     {
00203       return (!(isCext (ext)));
00204     }
00205 
00206   return (TRUE);
00207 }

char loadChar ( char ** s )
 

Definition at line 366 of file general.c.

00367 {
00368   char ret;
00369 
00370   while (**s == ' ')
00371     {
00372       (*s)++;
00373     }
00374   
00375   ret = **s;
00376   (*s)++;
00377   return ret;
00378 }

long unsigned int longUnsigned_fromInt ( int x )
 

Definition at line 598 of file general.c.

00599 {
00600   llassert (x >= 0);
00601   
00602   return (long unsigned) x;
00603 }

int longUnsigned_toInt ( long unsigned int x )
 

Definition at line 641 of file general.c.

00642 {
00643   int res = (int) x;
00644 
00645   llassert ((long unsigned) res == x);
00646   return res;
00647 }

int long_toInt ( long int x )
 

Definition at line 650 of file general.c.

Referenced by cppGetToken().

00651 {
00652   int res = (int) x;
00653 
00654   /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
00655   return res;
00656 }

char * mstring_append ( char * s1,
char c )
 

Definition at line 520 of file general.c.

Referenced by LCLScanLine(), and message().

00521 {
00522   size_t l = strlen (s1);
00523   char *s;
00524 
00525   s = (char *) dmalloc (sizeof (*s) * (l + 2));
00526 
00527   strcpy (s, s1);
00528   *(s + l) = c;
00529   *(s + l + 1) = '\0';
00530   sfree (s1); 
00531   return s;
00532 }

char * mstring_concat ( const char * s1,
const char * s2 )
 

Definition at line 487 of file general.c.

Referenced by addExtension(), outputLCSFile(), and processImport().

00488 {
00489   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
00490   strcpy (s, s1);
00491   strcat (s, s2);
00492   return s;
00493 }

char * mstring_concatFree ( char * s1,
char * s2 )
 

Definition at line 496 of file general.c.

Referenced by lslOp_unparse(), and message().

00497 {
00498   /* like mstring_concat but deallocates old strings */
00499   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
00500   strcpy (s, s1);
00501   strcat (s, s2);
00502 
00503   sfree (s1);
00504   sfree (s2);
00505   return s;
00506 }

char * mstring_concatFree1 ( char * s1,
const char * s2 )
 

Definition at line 509 of file general.c.

Referenced by message(), and nd_charp().

00510 {
00511   char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
00512   strcpy (s, s1);
00513   strcat (s, s2);
00514   sfree (s1);
00515 
00516   return s;
00517 }

char * mstring_copy ( char * s1 )
 

Definition at line 535 of file general.c.

Referenced by addExtension(), cppReader_initializeReader(), cstring_copy(), getWord(), main(), nd_charp(), processImport(), removeAnyExtension(), removeExtension(), removePath(), removePathFree(), tsource_fromString(), usymtab_printComplete(), and usymtab_printOut().

00536 {
00537   if (s1 == NULL)
00538     {
00539       return NULL;
00540     }
00541   else
00542     {
00543       char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
00544       strcpy (s, s1);
00545       return s;
00546     }
00547 }

char * mstring_create ( int n )
 

Definition at line 563 of file general.c.

Referenced by context_loadModuleAccess(), cstring_concat(), cstring_copyLength(), loadStandardState(), lsymbol_toCharsSafe(), summarizeErrors(), typeIdSet_loadTable(), and usymtab_load().

00564 {
00565   char *s;
00566 
00567   s = dmalloc (sizeof (*s) * (n + 1));
00568   *s = '\0';
00569   return s;
00570 }

bool mstring_equal ( const char * s1,
const char * s2 )
 

Definition at line 675 of file general.c.

Referenced by LCLScanLine(), context_isSpecialFile(), isCext(), isHeaderFile(), lcllib_isSkipHeader(), main(), outputLCSFile(), and removeExtension().

00676 {
00677   if (s1 == NULL)
00678     {
00679       return (s2 == NULL);
00680     }
00681   else
00682     {
00683       if (s2 == NULL)
00684         {
00685           return FALSE;
00686         }
00687       else
00688         {
00689           return (strcmp(s1, s2) == 0);
00690         }
00691     }
00692 }

bool mstring_equalPrefix ( const char * c1,
const char * c2 )
 

Definition at line 660 of file general.c.

Referenced by BUFLEN(), and cppReader_parseNumber().

00661 {
00662   llassert (c1 != NULL);
00663   llassert (c2 != NULL);
00664 
00665   if (strncmp(c1, c2, strlen(c2)) == 0)
00666     {
00667       return TRUE;
00668     }
00669   else
00670     {
00671       return FALSE;
00672     }
00673 }

void mstring_markFree ( char * s )
 

Definition at line 278 of file general.c.

Referenced by main(), and nd_charp().

00279 {
00280     sfreeEventually (s);
00281 }

char * mstring_safePrint ( char * s )
 

Definition at line 550 of file general.c.

00551 {
00552   if (s == NULL)
00553     {
00554       return ("<undefined>");
00555     }
00556   else
00557     {
00558       return s;
00559     }
00560 }

char * mstring_spaces ( int n )
 

Definition at line 466 of file general.c.

00467 {
00468   int i;
00469   char *ret;
00470   char *ptr;
00471 
00472   llassert (n >= 0);
00473 
00474   ret = (char *) dmalloc (size_fromInt (n + 1));
00475   ptr = ret;
00476 
00477   for (i = 0; i < n; i++)
00478     {
00479       *ptr++ = ' ';
00480     }
00481 
00482   *ptr = '\0';
00483 
00484   return ret;
00485 }

bool optCheckChar ( char ** s,
char c )
 

Definition at line 425 of file general.c.

Referenced by uentry_undump().

00426 {
00427   if (**s == c)
00428     {
00429       (*s)++;
00430       return TRUE;
00431     }
00432   else
00433     {
00434       return FALSE;
00435     }
00436 }

char * removeAnyExtension ( char * s )
 

Definition at line 284 of file general.c.

Referenced by callLSL().

00285 {
00286   char *ret;
00287   char *t = strrchr (s, '.');
00288 
00289   if (t == (char *) 0)
00290     {
00291       return mstring_copy (s);
00292     }
00293 
00294   /*@-mods@*/
00295   *t = '\0';
00296   ret = mstring_copy (s);
00297   *t = '.';
00298   /*@=mods@*/ /* modification is undone */
00299 
00300   return ret;
00301 }

char * removeExtension ( char * s,
const char * suffix )
 

Definition at line 209 of file general.c.

Referenced by context_exitLCLfile(), and lcllib_isSkipHeader().

00210 {
00211   char *t = strrchr (s, '.');
00212   char *s2;
00213 
00214   if (t == (char *) 0 || !mstring_equal (t, suffix))
00215     {
00216       return mstring_copy (s);
00217     }
00218 
00219   /*@-mods@*/ 
00220   *t = '\0';
00221   s2 = mstring_copy (s);
00222   *t = '.';
00223   /*@=mods@*/  /* Modification is undone. */
00224   return s2;
00225 }

char * removePath ( char * s )
 

Definition at line 243 of file general.c.

Referenced by callLSL().

00244 {
00245   char *t = strrchr (s, CONNECTCHAR);
00246 
00247   if (t == NULL) return (mstring_copy (s));
00248   else return (mstring_copy (t + 1));
00249 }

char * removePathFree ( char * s )
 

Definition at line 253 of file general.c.

00254 {
00255   char *t = strrchr (s, CONNECTCHAR);
00256 
00257 # ifdef ALTCONNECTCHAR
00258   {
00259     char *at = strrchr (s, ALTCONNECTCHAR);
00260     if (t == NULL || (at > t)) {
00261       t = at;
00262     }
00263   }
00264 # endif
00265 
00266   if (t == NULL) 
00267     {
00268       return (s);
00269     }
00270   else
00271     {
00272       char *res = mstring_copy (t + 1);
00273       mstring_free (s);
00274       return res;
00275     }
00276 }

char * removePreDirs ( char * s )
 

Definition at line 701 of file general.c.

00702 {
00703   while (*s == '.' && *(s + 1) == CONNECTCHAR) 
00704     {
00705       s += 2;
00706     }
00707 
00708 # if defined(OS2) || defined(MSDOS)
00709   /* remove remainders from double path delimiters... */
00710   while (*s == CONNECTCHAR) 
00711     {
00712       ++s;
00713     }
00714 # endif /* !defined(OS2) && !defined(MSDOS) */
00715 
00716   return s;
00717 }

void sfree ( void * x )
 

Definition at line 49 of file general.c.

Referenced by LCLScanLine(), LCLSynTableCleanup(), LCLTokenTableCleanup(), MMASH(), REST_EXTENSION_LENGTH(), aliasTable_free(), aliasTable_levelUnionSeq(), callLSL(), clauseStack_free(), context_destroyMod(), context_loadModuleAccess(), cppCleanup(), cppReader_checkMacroName(), cppReader_define(), cppReader_deleteMacro(), cppReader_initializeReader(), cppReader_parseExpression(), cstringList_free(), cstringSList_free(), cstring_appendChar(), cstring_free(), ctypeList_free(), declaratorInvNodeList_free(), declaratorInvNode_free(), declaratorNodeList_free(), declaratorNode_free(), dumpState(), enumNameList_free(), enumNameSList_free(), exprNodeList_free(), exprNodeList_freeShallow(), exprNodeSList_free(), exprNode_free(), exprNode_freeShallow(), fcnNodeList_free(), fcnNode_free(), fileTable_addltemp(), fileTable_free(), filelocList_append(), filelocList_free(), filelocStack_free(), fileloc_free(), fileloc_reallyFree(), flagMarkerList_free(), flagMarker_free(), guardSet_free(), hashTable_free(), hashTable_insert(), idDeclList_free(), idDecl_free(), importCTrait(), importNodeList_free(), importNode_free(), initDeclNodeList_free(), initDeclNode_free(), intSet_free(), interfaceNodeList_free(), interfaceNode_free(), lcllib_isSkipHeader(), letDeclNodeList_free(), letDeclNode_free(), lhCleanup(), loadStandardState(), loadState(), lslOpList_free(), lslOpSet_free(), lslOp_free(), lsymbolList_free(), lsymbolSet_free(), lsymbol_destroyMod(), lsymbol_initMod(), lsynTableCleanup(), ltokenList_free(), ltokenTableCleanup(), ltoken_free(), macrocache_free(), main(), makeArrayNode(), mapping_free(), messageLog_free(), mstring_append(), mstring_concatFree(), mstring_concatFree1(), multiVal_free(), nameNode_free(), nd_charp(), outputLCSFile(), pairNodeList_free(), pairNode_free(), paramNodeList_free(), paramNode_free(), parseSignatures(), processImport(), programNodeList_free(), programNode_free(), qtype_free(), qualList_free(), quantifierNodeList_free(), quantifierNode_free(), replaceNodeList_free(), replaceNode_free(), sRefSetList_free(), sRefSet_free(), sRefTable_free(), sRef_free(), sigNodeSet_free(), sigNode_free(), signNode_free(), sortList_free(), sortSetList_free(), sortSet_free(), sort_destroyMod(), specFullName(), specialClauses_free(), stDeclNodeList_free(), stDeclNode_free(), storeRefNodeList_free(), storeRefNode_free(), summarizeErrors(), symtable_free(), termNodeList_free(), termNode_free(), traitRefNodeList_free(), traitRefNode_free(), tsource_free(), tsource_getPath(), typeExpr_free(), typeIdSet_destroyMod(), typeNameNodeList_free(), typeNameNode_free(), uentryList_free(), uentryList_mergeFields(), usymIdSet_free(), usymtab_load(), varDeclarationNodeList_free(), varDeclarationNode_free(), varInfo_free(), varNodeList_free(), and varNode_free().

00050 {
00051   if (x != NULL)
00052     {
00053       /*
00054       fprintf (stderr, "Freeing: %ld\n", x);
00055       if ((unsigned long) x < 136000000 && (unsigned long) x > 135000000) {
00056         fprintf (stderr, "Looks bad!\n");
00057       }
00058       */
00059       free (x);
00060       /* fprintf (stderr, "Done.\n"); */
00061     }
00062 }

void sfreeEventually ( void * x )
 

Definition at line 65 of file general.c.

Referenced by cstring_markOwned(), ltoken_markOwned(), mstring_markFree(), sigNode_markOwned(), uentry_markOwned(), and argdata::use_count().

00066 {
00067   if (x != NULL)
00068     {
00069       ; /* should keep in a table */
00070     }
00071 /*@-mustfree@*/
00072 }

size_t size_fromInt ( int x )
 

Definition at line 605 of file general.c.

Referenced by LSLRootName(), cppReader_checkMacroName(), cppReader_initializeReader(), and cppReader_parseExpression().

00606 {
00607   size_t res = (size_t) x;
00608 
00609   llassert ((int) res == x);
00610   return res;
00611 }

int size_toInt ( size_t x )
 

Definition at line 613 of file general.c.

Referenced by FILE_NAME_MAP_FILE(), REST_EXTENSION_LENGTH(), cppReader_checkMacroName(), cstring_equalCanonicalPrefix(), cstring_length(), cstring_replaceLit(), lclctype_toSort(), lclctype_toSortDebug(), lhCleanup(), nd_charp(), and specFullName().

00614 {
00615   int res = (int) x;
00616 
00617   llassert ((size_t) res == x);
00618   return res;
00619 }

long size_toLong ( size_t x )
 

Definition at line 621 of file general.c.

00622 {
00623   long res = (long) x;
00624 
00625   llassert ((size_t) res == x);
00626   return res;
00627 }


Generated at Fri Nov 3 18:57:59 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000