summaryrefslogtreecommitdiff
path: root/cleanupforcvs.pl
blob: 270ea525dbe7ad3cadf5b17a52d5566031c220b9 (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
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
#!/usr/bin/perl
use strict;

my @del_macos_files;
my @bad_cpp;
my @test_main;
my @makefiles;
my @autogen_cpp;
my $cleaned = 1;
my $dist_archives_exist = 0;
my @bad_h;

open EVERYTHING,'find . |' or die "Can't open find for file listing";

my %exclude_from_memtest_checks = ('PollEmulator.cpp'=>1,'DebugMemLeakFinder.cpp'=>1,'MemLeakFinder.h'=>1,'MemLeakFindOn.h'=>1,'MemLeakFindOff.h'=>1,'Box.h'=>1);

while(<EVERYTHING>)
{
	chomp;
	next if -d;
	if(m~/autogen_\w+\.(h|cpp)~)	
	{
		push @autogen_cpp,$_
	}
	if(m~/\._[^/]+\Z~ || m~/\.DS_Store\Z~)
	{
		# mac OS files we don't want
		push @del_macos_files,$_
	}
	elsif(m/\/(\w+\.cpp)/)
	{
		my $leafname = $1;
		# check that Box.h is first include
		open CPP,$_ or die "Can't open $_ for reading";
		
		my $box_found = 0;
		my $last_was_memteston = 0;
		my $ok = 1;
		
		while(my $l = <CPP>)
		{
			if($l =~ m/#include\s+["<](.+?)[">]/)
			{
				my $inc_name = $1;
				if($inc_name eq 'Box.h')
				{
					$box_found = 1;
				}
				else
				{
					# Box.h must be first include file in every cpp file
					$ok = 0 unless $box_found;
				}
				# is it the mem test on thing? (ignoring the wire packing .h files)
				if($inc_name ne 'BeginStructPackForWire.h' && $inc_name ne 'EndStructPackForWire.h')
				{
					$last_was_memteston = ($inc_name eq 'MemLeakFindOn.h');
				}
			}
		}
		if(!exists $exclude_from_memtest_checks{$leafname})
		{
			$ok = 0 unless $last_was_memteston;
		}
		push @bad_cpp,$_ unless $ok;
				
		close CPP;
	}
	elsif(m/\/(\w+\.h)/)
	{
		my $leafname = $1;

		open H,$_ or die "Can't open $_ for reading";
		
		my $ok = 1;
		my $memteston = 0;
		
		while(my $l = <H>)
		{
			if($l =~ m/#include\s+["<](.+?)[">]/)
			{
				if($1 eq 'MemLeakFindOn.h')
				{
					$memteston = 1;
				}
				elsif($1 eq 'MemLeakFindOff.h')
				{
					$memteston = 0;
				}
				else
				{
					# don't allow #include within mem test on
					$ok = 0 unless !$memteston;
				}
			}
			else
			{
				# strip comments
				my $lsc = $l;
				$lsc =~ s~//.+$~~;
				if($lsc =~ m/\b(new|delete|malloc|free|realloc)\b/)
				{
					# only allow this if memory checking is ON
					$ok = 0 unless $memteston;
				}
			}
		}
		# mem test must be off at the end of this .h file
		$ok = 0 if $memteston;
		
		if($_ !~ /testfiles/ && !exists $exclude_from_memtest_checks{$leafname})
		{
			push @bad_h,$_ unless $ok;
		}
		close H;
	}
	elsif(m~/Makefile\Z~)
	{
		push @makefiles,$_
	}
	
	if(m~/_(main\.cpp|t|t-gdb)\Z~)
	{
		push @test_main,$_
	}
	
	if(m~\./boxbackup~)
	{
		$dist_archives_exist = 1;
	}
}

close EVERYTHING;

ask_about_delete(\@del_macos_files, "supurious MacOS X files");
ask_about_delete(\@makefiles, "automatically generated Makefiles");
ask_about_delete(\@test_main, "automatically generated test files");
ask_about_delete(\@autogen_cpp, "automatically generated source files");

if($#bad_cpp >= 0)
{
	print "\n";
	print $_,"\n" for @bad_cpp;
	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";
	$cleaned = 0;
}
if($#bad_h >= 0)
{
	print "\n";
	print $_,"\n" for @bad_h;
	print "There are some .h files which use memory functions without memory leak finding on, or leave memory leak finding on at end\n";
	$cleaned = 0;
}

if(-d 'debug') {print "debug directory exists\n"; $cleaned = 0;}
if(-d 'release') {print "release directory exists\n"; $cleaned = 0;}
if(-d 'parcels') {print "parcels directory exists\n"; $cleaned = 0;}
if($dist_archives_exist) {print "boxbackup* files/dirs exist\n"; $cleaned = 0;}

if(!$cleaned)
{
	print <<__E;

========================================================
   NOT CLEANED!
========================================================
__E
}


sub ask_about_delete
{
	my ($del_r, $name) = @_;
	return if $#$del_r < 0;
		
	print "\n";
	for(@$del_r)
	{
		print $_,"\n";
	}
	print "Delete these ",$#$del_r + 1, " $name?";
	my $in = <STDIN>;
	chomp $in;
	if($in eq 'yes')
	{
		print "Deleting...\n";
		unlink $_ for @$del_r
	}
	else
	{
		$cleaned = 0;
	}
}