home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 406_01 / atoc / autoagg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-09  |  2.6 KB  |  89 lines

  1. /*=========================================================================
  2.  
  3.     ATOC automatic aggregates module
  4.  
  5. =========================================================================*/
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "atoc.h"
  10.  
  11.  
  12. /**/    /* this version assumes that white space on the left margin means */
  13. /**/    /* you are inside a function. This may be adequate given the other */
  14. /**/    /* parameters we search for. */
  15. /**/    /* Also, it only knows about standard types - typedef'd objects */
  16. /**/    /* are not recognized. */
  17.  
  18.  
  19. /*-------------------------------------------------------------------------
  20. autoaggregates( s ) handles initialized automatic aggregates by making
  21. them static. This function is called after keywords(), so ANSI type
  22. modifiers like const and volatile are removed already, and enums are
  23. already converted to ints.
  24. -------------------------------------------------------------------------*/
  25. autoaggregates( s )
  26. char *s;
  27. {
  28.     char *cp, *c1, *c2, *strchr();
  29.  
  30.     /* if inside a function */
  31.     if ( isspace( s[ 0 ] ) )
  32.     {
  33.         /* find first non-space char */
  34.         for ( cp = s; *cp; ++cp )
  35.             if ( ! isspace( *cp ) )
  36.                 break;
  37.  
  38.         /* see if its a data type */
  39.         if ( *cp && istype( cp ) )
  40.         {
  41.             /* see if its an aggregate */
  42.             c1 = strchr( s, '[' );
  43.             c2 = strchr( s, ']' );
  44.             if ( ( c1 && c2 && c2 > c1 )
  45.               || strncmp( cp, "struct", 6 ) == 0
  46.               || strncmp( cp, "union", 5 ) == 0 )
  47.             {
  48.                 /* see if its initialized */
  49.                 if ( strchr( s, '=' ) )
  50.                 {
  51.                     /* remove "auto" if there */
  52.                     if ( strncmp( cp, "auto", 4 ) == 0 )
  53.                         strcpy( cp, cp + 5 );
  54.                     /* make static */
  55.                     strins( cp, "static " );
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
  61. /*-------------------------------------------------------------------------
  62. istype( s ) returns TRUE if s is pointing at a recognized data type, or
  63. FALSE if not. Note that certain keywords are omitted because in our current
  64. context (looking for initialized automatic aggregates) they would not be
  65. what we are looking for.
  66.  
  67.     Omitted        Reason
  68.     --------    --------------------
  69.     extern        Can't be fixed here
  70.     register    Can't have a register aggregate
  71.     static        Nothing to do - already static!
  72. -------------------------------------------------------------------------*/
  73. PRIVATE int istype( s )
  74. char *s;
  75. {
  76.     static char *types[] = {
  77.         "auto", "char", "double", "int", "float",
  78.         "long", "short", "struct", "union", "unsigned",
  79.         NULL
  80.     };
  81.     int i;
  82.  
  83.     for ( i = 0; types[ i ]; ++i )
  84.         if ( strncmp( s, types[ i ], strlen( types[ i ] ) ) == 0 )
  85.             return( TRUE );
  86.     return( FALSE );
  87. }
  88. /*=======================================================================*/
  89.