    1    | /***************************************
    2    |   $Header: /home/amb/cxref/RCS/var.c 1.13 1999/01/24 16:53:49 amb Exp $
    3    |
    4    |   C Cross Referencing & Documentation tool. Version 1.5.
    5    |
    6    |   Collects the variable definition stuff.
    7    |   ******************/ /******************
    8    |   Written by Andrew M. Bishop
    9    |
    10   |   This file Copyright 1995,96,97,99 Andrew M. Bishop
    11   |   It may be distributed under the GNU Public License, version 2, or
    12   |   any higher version.  See section COPYING of the GNU Public license
    13   |   for conditions under which this file may be redistributed.
    14   |   ***************************************/
    15   |
    16   | /*+ Control the output of debugging information from this file. +*/
    17   | #define DEBUG 0
    18   |
    19   | #include <stdlib.h>
    20   | #include <stdio.h>
    21   | #include <string.h>
    22   |
    23   | #include "memory.h"
    24   | #include "datatype.h"
    25   | #include "parse-yy.h"
    26   | #include "cxref.h"
    27   |
    28   | /*+ The file that is currently being documented. +*/
    29   | extern File CurFile;
    30   |
    31   | /*+ When in a header file make a note of which one for the included variables. +*/
    32   | extern int in_header;
    33   |
    34   | /*+ A list of the variables found at each level of the scope. +*/
    35   | static StringList2 *variable;
    36   |
    37   | /*+ The number of levels of scope depth allocated. +*/
    38   | static int max_scope=0;
    39   |
    40   | /*+ The current scope depth. +*/
    41   | static int cur_scope=-1;
    42   |
    43   |
    44   | static Variable NewVariableType(char *name,char *type);
    45   |
    46   |
    47   | /*++++++++++++++++++++++++++++++++++++++
    48   |   Function that is called when a variable definition is seen.
    49   |
    50   |   char* name The name of the variable.
    51   |
    52   |   char* type The type of the variable.
    53   |
    54   |   int scope The scope of variable that has been seen.
    55   |   ++++++++++++++++++++++++++++++++++++++*/
    56   |
    57   | void SeenVariableDefinition(char* name,char* type,int scope)
    58   | {
    59   |  Variable var;
    60   |  int seen=0;
    61   |
    62   | #if DEBUG
    63   |  printf("#Var.c# Variable definition for '%s'\n",name);
    64   | #endif
    65   |
    66   |  for(var=CurFile->variables;var;var=var->next)
    67   |     if(!strcmp(var->name,name))
    68   |       {
    69   |        var->scope|=scope;
    70   |        seen=1;
    71   |        break;
    72   |        if(!in_header && var->scope&EXTERN_H)
    73   |          {
    74   |           if(var->comment)
    75   |              Free(var->comment);
    76   |           var->comment=MallocString(GetCurrentComment());
    77   |           var->lineno=parse_line;
    78   |          }
    79   |       }
    80   |
    81   |  if(!seen)
    82   |    {
    83   |     var=NewVariableType(name,type);
    84   |
    85   |     var->comment=MallocString(GetCurrentComment());
    86   |     var->scope=scope;
    87   |
    88   |     var->lineno=parse_line;
    89   |
    90   |     if(in_header && !(scope&EXTERN_H))
    91   |        var->incfrom=MallocString(parse_file);
    92   |
    93   |     AddToLinkedList(CurFile->variables,Variable,var);
    94   |    }
    95   | }
    96   |
    97   | /*++++++++++++++++++++++++++++++++++++++
    98   |   Called when a new scope is entered.
    99   |   ++++++++++++++++++++++++++++++++++++++*/
    100  |
    101  | void UpScope(void)
    102  | {
    103  |  cur_scope++;
    104  |
    105  | #if DEBUG
    106  |  printf("#Var.c# Scope ++ (%2d)\n",cur_scope);
    107  | #endif
    108  |
    109  |  if(cur_scope>=max_scope)
    110  |    {
    111  |     if(max_scope==0)
    112  |        variable=Malloc(16*sizeof(StringList2));
    113  |     else
    114  |        variable=Realloc(variable,(max_scope+16)*sizeof(StringList2));
    115  |     max_scope+=16;
    116  |    }
    117  |
    118  |  variable[cur_scope]=NewStringList2();
    119  | }
    120  |
    121  |
    122  | /*++++++++++++++++++++++++++++++++++++++
    123  |   Called when an old scope is exited.
    124  |   ++++++++++++++++++++++++++++++++++++++*/
    125  |
    126  | void DownScope(void)
    127  | {
    128  | #if DEBUG
    129  |  printf("#Var.c# Scope -- (%2d)\n",cur_scope);
    130  | #endif
    131  |
    132  |  DeleteStringList2(variable[cur_scope]);
    133  |
    134  |  cur_scope--;
    135  | }
    136  |
    137  |
    138  | /*++++++++++++++++++++++++++++++++++++++
    139  |   Add a variable to the list of known variables.
    140  |
    141  |   char* name The name of the variable.
    142  |   ++++++++++++++++++++++++++++++++++++++*/
    143  |
    144  | void SeenScopeVariable(char* name)
    145  | {
    146  | #if DEBUG
    147  |  printf("#Var.c# Scope Variable depth %2d '%s'\n",cur_scope,name);
    148  | #endif
    149  |
    150  |  AddToStringList2(variable[cur_scope],name,NULL,0,0);
    151  | }
    152  |
    153  |
    154  | /*++++++++++++++++++++++++++++++++++++++
    155  |   Check through the scope variables to look for the named one.
    156  |
    157  |   int IsAScopeVariable Returns 1 if the name does refer to a variable that is scoped.
    158  |
    159  |   char* name The name of the variable to search for.
    160  |   ++++++++++++++++++++++++++++++++++++++*/
    161  |
    162  | int IsAScopeVariable(char* name)
    163  | {
    164  |  int i,scope;
    165  |
    166  | #if DEBUG
    167  |  printf("#Var.c# Lookup variable '%s'\n",name);
    168  | #endif
    169  |
    170  |  for(scope=cur_scope;scope>=0;scope--)
    171  |     for(i=0;i<variable[scope]->n;i++)
    172  |        if(!strcmp(variable[scope]->s1[i],name))
    173  |           return(1);
    174  |
    175  |  return(0);
    176  | }
    177  |
    178  |
    179  | /*++++++++++++++++++++++++++++++++++++++
    180  |   Tidy up all of the local variables in case of a problem and abnormal parser termination.
    181  |   ++++++++++++++++++++++++++++++++++++++*/
    182  |
    183  | void ResetVariableAnalyser(void)
    184  | {
    185  |  while(cur_scope>=0)
    186  |    {
    187  |     DeleteStringList2(variable[cur_scope]);
    188  |     cur_scope--;
    189  |    }
    190  |
    191  |  if(variable) Free(variable);
    192  |  variable=NULL;
    193  |
    194  |  max_scope=0;
    195  |  cur_scope=-1;
    196  | }
    197  |
    198  |
    199  | /*++++++++++++++++++++++++++++++++++++++
    200  |   Create a new variable type.
    201  |
    202  |   Variable NewVariableType Returns a new Variable type.
    203  |
    204  |   char *name The name of the variable.
    205  |
    206  |   char *type The type of the variable.
    207  |   ++++++++++++++++++++++++++++++++++++++*/
    208  |
    209  | static Variable NewVariableType(char *name,char *type)
    210  | {
    211  |  Variable var=(Variable)Calloc(1,sizeof(struct _Variable)); /* clear unused pointers */
    212  |
    213  |  var->name   =MallocString(name);
    214  |  var->type   =MallocString(type);
    215  |  var->visible=NewStringList2();
    216  |  var->used   =NewStringList2();
    217  |
    218  |  return(var);
    219  | }
    220  |
    221  |
    222  | /*++++++++++++++++++++++++++++++++++++++
    223  |   Delete the specified Variable type.
    224  |
    225  |   Variable var The Variable type to be deleted.
    226  |   ++++++++++++++++++++++++++++++++++++++*/
    227  |
    228  | void DeleteVariableType(Variable var)
    229  | {
    230  |  if(var->comment) Free(var->comment);
    231  |  if(var->name)    Free(var->name);
    232  |  if(var->type)    Free(var->type);
    233  |  if(var->defined) Free(var->defined);
    234  |  if(var->incfrom) Free(var->incfrom);
    235  |  if(var->visible) DeleteStringList2(var->visible);
    236  |  if(var->used)    DeleteStringList2(var->used);
    237  |  Free(var);
    238  | }