summaryrefslogtreecommitdiff
path: root/bin/DependencyGenerator/Preprocessor.pm
blob: 89067f7e34437e85916cbbd3d72dd1eebe5e7fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package Preprocessor;

# ************************************************************
# Description   : Preprocesses the supplied file.
# Author        : Chad Elliott
# Create Date   : 2/10/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use FileHandle;

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class)   = shift;
  my($macros)  = shift;
  my($ipaths)  = shift;
  return bless {'macros'  => $macros,
                'ipaths'  => $ipaths,
                'files'   => {},
                'ifound'  => {},
                'recurse' => 0,
               }, $class;
}


sub locateFile {
  my($self) = shift;
  my($file) = shift;

  if (defined $self->{'ifound'}->{$file}) {
    return $self->{'ifound'}->{$file};
  }
  else {
    foreach my $dir ('.', @{$self->{'ipaths'}}) {
      if (-r "$dir/$file") {
        $self->{'ifound'}->{$file} = "$dir/$file";
        return $self->{'ifound'}->{$file};
      }
    }
  }
  return undef;
}


sub getFiles {
  my($self)   = $_[0];
  my(@files)  = ($_[1]);
  my(%ifiles) = ();

  for(my $i = 0; $i <= $#files; ++$i) {
    foreach my $inc (@{$self->{'files'}->{$files[$i]}}) {
      if (!defined $ifiles{$inc}) {
        $ifiles{$inc} = 1;
        push(@files, $inc);
      }
    }
  }
  $self->{'ifiles'} = \%ifiles;
}


sub process {
  my($self)     = shift;
  my($file)     = shift;
  my($noinline) = shift;
  my($noincs)   = shift;
  my($fh)       = new FileHandle();

  if (open($fh, $file)) {
    my($ifcount) = 0;
    my(@zero)    = ();
    my($files)   = $self->{'files'};
    my($recurse) = ++$self->{'recurse'};

    $$files{$file} = [];
    while(<$fh>) {
      ## As an optimization, use a very simple regular expression on the
      ## outside that all of the inner regular expressions have in
      ## common.  That way we go down the path of if elsif only if it is
      ## even possible due to the outside regular expression.
      if (/#/) {
        ## Remove c++ and same line c comments inside this if statement.
        ## This saves about 5% off of processing the ace directory
        ## and we only need to strip comments if we are actually
        ## going to look at the string.
        $_ =~ s/\/\/.*//;
        $_ =~ s/\/\*.*\*\///;

        if (/#\s*endif/) {
          --$ifcount;
          if (defined $zero[0] && $ifcount == $zero[$#zero]) {
            pop(@zero);
          }
        }
        elsif (/#\s*if\s+0/) {
          push(@zero, $ifcount);
          ++$ifcount;
        }
        elsif (/#\s*if/) {
          ++$ifcount;
        }
        elsif (!defined $zero[0] &&
               /#\s*include\s+[<"]([^">]+)[">]/) {
          my($inc) = $self->locateFile($1);
          if (defined $inc) {
            $inc =~ s/\\/\//g;
            if (!$noinline ||
                ($recurse == 1 || $inc !~ /\.i(nl)?$/)) {
              push(@{$$files{$file}}, $inc);
              if (!defined $$files{$inc}) {
                ## Process this file, but do not return the include files
                $self->process($inc, $noinline, 1);
              }
            }
          }
        }
      }
    }
    close($fh);

    --$self->{'recurse'};
  }

  ## This has to be outside the if (open(...
  ## If the last file to be processed isn't accessable then
  ## we still need to return the array reference of includes.
  if (!$noincs) {
    $self->getFiles($file);
    my(@incs) = keys %{$self->{'ifiles'}};
    return \@incs;
  }
}


1;