| 1 | #!/usr/bin/perl |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | my @del_macos_files; |
|---|
| 5 | my @bad_cpp; |
|---|
| 6 | my @test_main; |
|---|
| 7 | my @makefiles; |
|---|
| 8 | my @autogen_cpp; |
|---|
| 9 | my $cleaned = 1; |
|---|
| 10 | my $dist_archives_exist = 0; |
|---|
| 11 | my @bad_h; |
|---|
| 12 | |
|---|
| 13 | open EVERYTHING,'find . -type d \( -name docs \) -prune -o -type f |' or die "Can't open find for file listing"; |
|---|
| 14 | |
|---|
| 15 | my %exclude_from_memtest_checks = ('PollEmulator.cpp'=>1,'DebugMemLeakFinder.cpp'=>1,'MemLeakFinder.h'=>1,'MemLeakFindOn.h'=>1,'MemLeakFindOff.h'=>1,'Box.h'=>1); |
|---|
| 16 | |
|---|
| 17 | while(<EVERYTHING>) |
|---|
| 18 | { |
|---|
| 19 | chomp; |
|---|
| 20 | next if -d; |
|---|
| 21 | if(m~/autogen_\w+\.(h|cpp)~) |
|---|
| 22 | { |
|---|
| 23 | push @autogen_cpp,$_ |
|---|
| 24 | } |
|---|
| 25 | if(m~/\._[^/]+\Z~ || m~/\.DS_Store\Z~) |
|---|
| 26 | { |
|---|
| 27 | # mac OS files we don't want |
|---|
| 28 | push @del_macos_files,$_ |
|---|
| 29 | } |
|---|
| 30 | elsif(m/\/(\w+\.cpp)/) |
|---|
| 31 | { |
|---|
| 32 | my $leafname = $1; |
|---|
| 33 | # check that Box.h is first include |
|---|
| 34 | open CPP,$_ or die "Can't open $_ for reading"; |
|---|
| 35 | |
|---|
| 36 | my $box_found = 0; |
|---|
| 37 | my $last_was_memteston = 0; |
|---|
| 38 | my $ok = 1; |
|---|
| 39 | |
|---|
| 40 | while(my $l = <CPP>) |
|---|
| 41 | { |
|---|
| 42 | if($l =~ m/#include\s+["<](.+?)[">]/) |
|---|
| 43 | { |
|---|
| 44 | my $inc_name = $1; |
|---|
| 45 | if($inc_name eq 'Box.h') |
|---|
| 46 | { |
|---|
| 47 | $box_found = 1; |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | # Box.h must be first include file in every cpp file |
|---|
| 52 | $ok = 0 unless $box_found; |
|---|
| 53 | } |
|---|
| 54 | # is it the mem test on thing? (ignoring the wire packing .h files) |
|---|
| 55 | if($inc_name ne 'BeginStructPackForWire.h' && $inc_name ne 'EndStructPackForWire.h') |
|---|
| 56 | { |
|---|
| 57 | $last_was_memteston = ($inc_name eq 'MemLeakFindOn.h'); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | if(!exists $exclude_from_memtest_checks{$leafname}) |
|---|
| 62 | { |
|---|
| 63 | $ok = 0 unless $last_was_memteston; |
|---|
| 64 | } |
|---|
| 65 | push @bad_cpp,$_ unless $ok; |
|---|
| 66 | |
|---|
| 67 | close CPP; |
|---|
| 68 | } |
|---|
| 69 | elsif(m/\/(\w+\.h)/) |
|---|
| 70 | { |
|---|
| 71 | my $leafname = $1; |
|---|
| 72 | |
|---|
| 73 | open H,$_ or die "Can't open $_ for reading"; |
|---|
| 74 | |
|---|
| 75 | my $ok = 1; |
|---|
| 76 | my $memteston = 0; |
|---|
| 77 | |
|---|
| 78 | while(my $l = <H>) |
|---|
| 79 | { |
|---|
| 80 | if($l =~ m/#include\s+["<](.+?)[">]/) |
|---|
| 81 | { |
|---|
| 82 | if($1 eq 'MemLeakFindOn.h') |
|---|
| 83 | { |
|---|
| 84 | $memteston = 1; |
|---|
| 85 | } |
|---|
| 86 | elsif($1 eq 'MemLeakFindOff.h') |
|---|
| 87 | { |
|---|
| 88 | $memteston = 0; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | # don't allow #include within mem test on |
|---|
| 93 | $ok = 0 unless !$memteston; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | else |
|---|
| 97 | { |
|---|
| 98 | # strip comments |
|---|
| 99 | my $lsc = $l; |
|---|
| 100 | $lsc =~ s~//.+$~~; |
|---|
| 101 | if($lsc =~ m/\b(new|delete|malloc|free|realloc)\b/) |
|---|
| 102 | { |
|---|
| 103 | # only allow this if memory checking is ON |
|---|
| 104 | $ok = 0 unless $memteston; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | # mem test must be off at the end of this .h file |
|---|
| 109 | $ok = 0 if $memteston; |
|---|
| 110 | |
|---|
| 111 | if($_ !~ /testfiles/ && !exists $exclude_from_memtest_checks{$leafname}) |
|---|
| 112 | { |
|---|
| 113 | push @bad_h,$_ unless $ok; |
|---|
| 114 | } |
|---|
| 115 | close H; |
|---|
| 116 | } |
|---|
| 117 | elsif(m~/Makefile\Z~) |
|---|
| 118 | { |
|---|
| 119 | push @makefiles,$_ |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | if(m~/_(main\.cpp|t|t-gdb)\Z~) |
|---|
| 123 | { |
|---|
| 124 | push @test_main,$_ |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if(m~\./boxbackup~) |
|---|
| 128 | { |
|---|
| 129 | $dist_archives_exist = 1; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | close EVERYTHING; |
|---|
| 134 | |
|---|
| 135 | ask_about_delete(\@del_macos_files, "supurious MacOS X files"); |
|---|
| 136 | ask_about_delete(\@makefiles, "automatically generated Makefiles"); |
|---|
| 137 | ask_about_delete(\@test_main, "automatically generated test files"); |
|---|
| 138 | ask_about_delete(\@autogen_cpp, "automatically generated source files"); |
|---|
| 139 | |
|---|
| 140 | if($#bad_cpp >= 0) |
|---|
| 141 | { |
|---|
| 142 | print "\n"; |
|---|
| 143 | print $_,"\n" for @bad_cpp; |
|---|
| 144 | print "There are some .cpp file where Box.h is not the first included file or MemLeakFindOn.h is not the last .h file included\n"; |
|---|
| 145 | $cleaned = 0; |
|---|
| 146 | } |
|---|
| 147 | if($#bad_h >= 0) |
|---|
| 148 | { |
|---|
| 149 | print "\n"; |
|---|
| 150 | print $_,"\n" for @bad_h; |
|---|
| 151 | print "There are some .h files which use memory functions without memory leak finding on, or leave memory leak finding on at end\n"; |
|---|
| 152 | $cleaned = 0; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | if(-d 'debug') {print "debug directory exists\n"; $cleaned = 0;} |
|---|
| 156 | if(-d 'release') {print "release directory exists\n"; $cleaned = 0;} |
|---|
| 157 | if(-d 'parcels') {print "parcels directory exists\n"; $cleaned = 0;} |
|---|
| 158 | if($dist_archives_exist) {print "boxbackup* files/dirs exist\n"; $cleaned = 0;} |
|---|
| 159 | |
|---|
| 160 | if(!$cleaned) |
|---|
| 161 | { |
|---|
| 162 | print <<__E; |
|---|
| 163 | |
|---|
| 164 | ======================================================== |
|---|
| 165 | NOT CLEANED! |
|---|
| 166 | ======================================================== |
|---|
| 167 | __E |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | sub ask_about_delete |
|---|
| 172 | { |
|---|
| 173 | my ($del_r, $name) = @_; |
|---|
| 174 | return if $#$del_r < 0; |
|---|
| 175 | |
|---|
| 176 | print "\n"; |
|---|
| 177 | for(@$del_r) |
|---|
| 178 | { |
|---|
| 179 | print $_,"\n"; |
|---|
| 180 | } |
|---|
| 181 | print "Delete these ",$#$del_r + 1, " $name? "; |
|---|
| 182 | my $in = <STDIN>; |
|---|
| 183 | chomp $in; |
|---|
| 184 | if($in eq 'yes') |
|---|
| 185 | { |
|---|
| 186 | print "Deleting...\n"; |
|---|
| 187 | unlink $_ for @$del_r |
|---|
| 188 | } |
|---|
| 189 | else |
|---|
| 190 | { |
|---|
| 191 | $cleaned = 0; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|