/usr/share/makepp/Mpp/Scanner/C.pm is in makepp 2.0.98.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | # $Id: C.pm,v 1.31 2014/01/15 22:47:33 pfeiffer Exp $
=head1 NAME
Mpp::Scanner::C - makepp scanner for C-like files
=head1 DESCRIPTION
Scans a C file for C<#include>'s.
Tags are:
=over 6
=item user
File scanned due to an #include "filename" directive.
=item sys
File scanned due to an #include E<lt>filenameE<gt> directive.
=back
=cut
use strict;
package Mpp::Scanner::C;
use Mpp::Scanner;
our @ISA = qw/Mpp::Scanner/;
use Mpp::File;
use Mpp::Text ();
# The base class uses a scalar to describe a scope; we use arrayrefs.
# These are the indices into the arrayref that describes a scope:
BEGIN {
(*ACT, # Are we outside of a disabled '#if' block
*PREV_ACT, # Has the current or a previous block of the
# current '#if'...'#elif' chain been active?
*EXPR, # The expression that caused the state to be
# unknown, if any.
*UNKNOWN) = @Mpp::Text::N; # any number except 0 or 1
}
our $dont_scan_hook;
sub dont_scan {
#my ($self, $finfo, $absname) = @_;
my( $guard, $dont_scan ) =
Mpp::File::build_info_string $_[1], qw(GUARD DONT_SCAN);
return 1 if defined $guard && exists $_[0]{VARS}{$guard};
if( $dont_scan ) {
$_[0]{VARS}{$guard} = '' if defined $guard; # This is the only thing that would happen if we did scan.
return 1;
}
if( $dont_scan_hook && &$dont_scan_hook( $_[2] )) {
Mpp::log SCAN_C_NOT => $_[1]
if $Mpp::log_level;
return 1;
}
&Mpp::Scanner::dont_scan;
}
sub push_scope {
die "@{$_[1]}" unless $_[1][ACT]==0 || $_[1][PREV_ACT]==1 || $_[1][ACT]==$_[1][PREV_ACT];
my $act = $_[0]{ACTIVE};
if($act == 1) {
$act = $_[1][ACT];
} elsif($_[1][ACT] == 0) {
$act = 0;
}
$_[0]{ACTIVE} = $_[1][ACT] = $act;
$_[1][EXPR] ||= $_[0]{SCOPES}[-1][EXPR] if $act == UNKNOWN;
die "@{$_[1]}" if ($_[1][ACT] == UNKNOWN || $_[1][PREV_ACT] == UNKNOWN) && !defined($_[1][EXPR]);
push(@{$_[0]{SCOPES}}, $_[1]);
}
sub pop_scope {
my $self=$_[0];
my $result = $self->SUPER::pop_scope();
$self->{ACTIVE}=$self->{SCOPES}[-1][ACT];
$result;
}
sub reset {
my $self= shift;
$self->SUPER::reset(@_);
unless(@_ > 0) {
$self->{SCOPES}=[[1, 0]];
}
$self->{ACTIVE}=$self->{SCOPES}[-1][ACT];
}
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->reset;
$self;
}
sub get_bad_expr {
$_[0]{SCOPES}[-1][EXPR];
}
#update scanner state and warning message
sub update_scope {
my ($self, $else, $line, $expr)=@_;
my @state = @{$self->{SCOPES}[-1]};
if($else) { # Continue the current chain
@state = @{$self->pop_scope};
$state[ACT] = $state[PREV_ACT];
$state[ACT] = !$state[ACT] unless $state[ACT] == UNKNOWN;
} else { # Start a new chain
die unless defined($expr);
$state[PREV_ACT] = 0;
}
@state = (0,0) unless $self->{ACTIVE}; # The scope enclosing the chain is off
if( $state[ACT] ) {
my $go = defined($expr) ?
eval_condition($self->expand_defines($expr)) : 1; # '#else' == '#elif 1'
$state[ACT] = $go unless $go==1 && $state[PREV_ACT];
$state[EXPR] = $line."\"$expr\"" if $go == UNKNOWN;
die if $state[PREV_ACT] == 1;
$state[PREV_ACT] = $go unless $go == 0;
}
$self->push_scope(\@state);
}
sub get_macro {
if(/\G(\`*)([a-z_]\w*)/igc) {
return ($1, "", $2);
}
return;
}
sub expand_defines {
my ($self,$expr,$vis) = @_;
$expr =~ s/defined\s*(\(?)\s*(\w+)/$1 . (defined( $self->get_var( $2 )) ? 1 : 0)/eg;
return $self->expand_macros($expr,$vis);
}
#since we already expanded defined and macros we should expect
#arithmetic expression on input and number on output.
#Any remaining words are undefined macros and should be eval to 0.
sub eval_condition {
my $cond = $_[0];
$cond =~ tr/`//d; # TODO: What's this good for?
$cond =~ s/\b([_a-z]\w*)/ 0 /ig;
my $funny;
local $SIG{__WARN__} = sub { $funny = 1 };
$cond = eval $cond;
return UNKNOWN if $@ || $funny;
$cond ? 1 : 0;
}
sub expand_macros {
my ($self, $expr, $visited) = @_;
$visited ||= {};
local $_ = $expr;
$expr = '';
pos = 0;
# TBD: Deal with macros that have parameters.
while(1) {
last if length($_) <= pos($_);
if(/\G(\\.)/sgc) {
$expr .= $1;
} elsif(/\G(\"(?:[^"]*\\\")*[^"]*\")/gc) {
$expr .= $1;
} elsif(/\G'((\\)(?:\d+|.)|.)'/gc) {
$expr .= ord( $2 ? eval "\"$1\"" : $1 );
} elsif(/\G(\d+(?:\.\d+)?)[luf]?/gic) {
$expr .= $1;
} elsif( my( $prefix, $key_prefix, $key ) = $self->get_macro ) {
$expr .= $prefix;
my $x = $self->get_var($key) unless $visited->{$key};
if(defined $x) {
my %v=%$visited;
$v{$key}=1;
# NOTE: pos() isn't preserved by local:
my $pos = pos;
$expr .= $self->expand_defines($x, \%v);
pos = $pos;
} else {
$expr .= $key_prefix . $key;
}
} else {
/\G([^\w\\"`']+)/igc or die "$_\n";
$expr .= $1;
}
}
$expr;
}
sub get_directive {
s/^\s*\#\s*(\w+)\s*// ? $1 : undef;
}
#
# Override this in subclasses for picking up additional directives.
# Return 0 if not interested, or undef to abort.
#
*other_directive = $Mpp::Text::N[0];
sub xscan_file {
my ($self, $cp, $tag, $finfo, $conditional, $fh)=@_;
my $absname = absolute_filename( $finfo );
my ($go, $pending_comment, $continued_comment);
my $guard_scope; # So we can check that #define and #endif match #ifndef.
my $guarded; # True between #define to #endif, 0 afterwards.
my $guard; # The found macro or '' if non empty lines occur outside of #define to #endif.
my $scanworthy; # Is there anything, other than a guard, that warrants a future rescan?
my $line_so_far = '';
LINE: while(<$fh>) {
s/\r*$//; # Ignore Windoze cruft
my $continuation = s/\\\s*$//; # gcc allows spaces after \ with a warning
# Need to handle "//\\\nfoo" correctly.
if($continued_comment) {
undef $continued_comment unless $continuation;
next LINE;
}
# Need to handle /*\n/**/ and /*\n//*/ correctly.
if($pending_comment) {
if(s!^.*?\*/!!) {
undef $pending_comment;
}
else {
next LINE;
}
}
# Get rid of single line comments.
s!/(/.*|\*.*?\*/)!$continued_comment=1 if $continuation && ord $1 == ord '/'; ' '!eg;
# Scan multiline instructions and comments.
if( s!/\*.*! ! and $pending_comment = 1 or $continuation ) {
chomp;
$line_so_far .= $_;
next LINE;
}
else {
$_ = $line_so_far.$_;
$line_so_far = '';
}
$go = $self->{ACTIVE};
if( my $directive = $self->get_directive ) {
s/\s*$//;
my $ret = $self->other_directive( $cp, $finfo, $conditional, $directive, \$scanworthy );
defined $ret or return undef;
if( $ret ) {
warn "$absname:$.: Ignoring trailing cruft \"$_\"\n" if $_;
} elsif( $directive eq 'include' ) {
$_ = $self->expand_macros($_) if $conditional;
my $userinc = s/^\"([^"]*)\"//;
if($userinc || s/^\<([^>]*)\>//) {
warn "$absname:$.: Ignoring trailing cruft \"$_\"\n" if /\S/;
local $_; # Preserve $_ for later
warn "$absname:$.: File $1 included because condition ", $self->get_bad_expr, " cannot be evaluated\n"
if $go == UNKNOWN;
$self->include($cp, $userinc && $tag ne 'sys' ? 'user' : 'sys', $1, $finfo)
or return undef;
} else {
warn "$absname:$.: Include $_ not scanned\n" if /\S/;
}
$scanworthy = 1;
} elsif( $conditional ) {
if( $go && $directive eq 'define' && /^(\w+)\s*(.*)/ ) {
$self->set_var($1, $2);
if( defined $guard_scope && $guard_scope == $self->{SCOPES}[-1] && $guard eq $1 ) {
$guarded = 1; # Looks even more like an include guard.
next LINE;
}
$scanworthy = 1;
} elsif( $go && $directive eq 'undef' && /^\w+$/ ) {
$self->set_var($_, undef);
$scanworthy = 1;
} elsif( (my $no=$directive eq 'ifndef') || $directive eq 'ifdef' and /^\w+$/ ) {
my $def = defined $self->get_var( $_ );
$go = ($no ? !$def : $def) || 0;
$self->push_scope([$go, $go]);
if( $no && !defined $guarded && !defined $guard ) {
$guard_scope = $self->{SCOPES}[-1]; # Might be beginning of an include guard.
$guard = $_;
next LINE;
}
} elsif( $directive eq 'else' ) {
$self->update_scope(1);
warn "$absname:$.: Ignoring trailing cruft \"$_\"\n" if $_;
} elsif( $directive eq 'endif' ) {
$guarded = $guard_scope = 0 # Include guard is ok so far.
if $guarded && $guard_scope == $self->{SCOPES}[-1];
$self->pop_scope();
warn "$absname:$.: Ignoring trailing cruft \"$_\"\n" if $_;
next LINE;
} elsif( $directive eq 'if' ) {
my $maybe_guard = $1
if !defined $guarded && !defined $guard && /^!\s*defined\s*\(?\s*(\w+)\s*\)?$/;
$self->update_scope(undef, "$absname:$.:", $_);
if( $maybe_guard ) {
$guard_scope = $self->{SCOPES}[-1]; # Might be beginning of an include guard.
$guard = $maybe_guard;
next LINE;
}
} elsif( $directive eq 'elif' ) {
$self->update_scope(1, "$absname:$.:", $_);
} elsif( !$scanworthy && !$go && ($directive eq 'define' || $directive eq 'undef') ) {
$scanworthy = 1; # Not doing it this time, but maybe from another command.
}
}
}
$scanworthy ||= (defined $guard && $guard ne ''),
$guard = ''
if defined && !$guarded && (!defined $guard || $guard ne '') && /\S/;
# Non empty line before or after means it is no guard.
}
my @build_info;
@build_info = (GUARD => $guard) # Sane include guard structure for whole file.
if !$guarded && defined $guard && $guard ne '';
push @build_info, DONT_SCAN => 1 if $conditional && !$scanworthy;
# Nothing interesting in file to warrant scanning it again?
Mpp::File::set_build_info_string $finfo, @build_info
if @build_info;
# Don't update_build_infos as this is just optimization info that can be written later.
1;
}
1;
|