summaryrefslogtreecommitdiff
path: root/scripts
Unidiff
Diffstat (limited to 'scripts') (more/less context) (ignore whitespace changes)
-rwxr-xr-xscripts/fixincludes34
1 files changed, 17 insertions, 17 deletions
diff --git a/scripts/fixincludes b/scripts/fixincludes
index 95fc129..da8613c 100755
--- a/scripts/fixincludes
+++ b/scripts/fixincludes
@@ -1,587 +1,587 @@
1#!/usr/bin/perl -w 1#!/usr/bin/perl -w
2# tries to reduce the number of includes in KDE source files 2# tries to reduce the number of includes in KDE source files
3# (c) 2001 Dirk Mueller <mueller@kde.org> 3# (c) 2001 Dirk Mueller <mueller@kde.org>
4 4
5use File::Basename; 5use File::Basename;
6use Cwd; 6use Cwd;
7 7
8# declaration of useful subroutines 8# declaration of useful subroutines
9sub find_fixable_sources ($); 9sub find_fixable_sources ($);
10sub find_fixable_headers($); 10sub find_fixable_headers($);
11sub find_removable_includes ($); 11sub find_removable_includes ($);
12sub warn_before_modifying ($); 12sub warn_before_modifying ($);
13sub remove_include ($$$); 13sub remove_include ($$$);
14sub replace_include ($$$); 14sub replace_include ($$$);
15sub fix_duplicates($); 15sub fix_duplicates($);
16sub fix_compat_includes($); 16sub fix_compat_includes($);
17sub fix_unnecessary($); 17sub fix_unnecessary($);
18sub copy_file($$); 18sub copy_file($$);
19sub process_source_file($); 19sub process_source_file($);
20 20
21# some global variables 21# some global variables
22$verbose = 0; # turns on debugging 22$verbose = 0; # turns on debugging
23$modify = 0; # if 1 it should try to fix the files as well 23$modify = 0; # if 1 it should try to fix the files as well
24$experimental = 0; # try&error if an include is obsolete (slow!!) 24$experimental = 0; # try&error if an include is obsolete (slow!!)
25@explicitfiles = (); # filled in if passing files on the command line 25@explicitfiles = (); # filled in if passing files on the command line
26 26
27# statistic variables 27# statistic variables
28$exp_success = 0; 28$exp_success = 0;
29$exp_failure = 0; 29$exp_failure = 0;
30 30
31while (defined ($ARGV[0])) 31while (defined ($ARGV[0]))
32{ 32{
33 $_ = shift; 33 $_ = shift;
34 if (/^--help$|^-h$/) { 34 if (/^--help$|^-h$/) {
35 print "Usage: fixincludes [--verbose | -v] [--experimental | -e ] [--modify | -m ]\n"; 35 print "Usage: fixincludes [--verbose | -v] [--experimental | -e ] [--modify | -m ]\n";
36 exit 0; 36 exit 0;
37 } 37 }
38 elsif (/^--verbose$|^-v$/) { 38 elsif (/^--verbose$|^-v$/) {
39 $verbose = 1; # Oh is there a problem...? 39 $verbose = 1; # Oh is there a problem...?
40 } 40 }
41 elsif (/^--modify$|^-m$/) { 41 elsif (/^--modify$|^-m$/) {
42 $modify = 1; 42 $modify = 1;
43 } 43 }
44 elsif (/^--experimental$|^-e$/) { 44 elsif (/^--experimental$|^-e$/) {
45 $modify = 1; 45 $modify = 1;
46 $experimental = 1; 46 $experimental = 1;
47 } 47 }
48 elsif (!/^-/) { 48 elsif (!/^-/) {
49 push @explicitfiles, $_; 49 push @explicitfiles, $_;
50 } 50 }
51} 51}
52 52
53$cppExt = "(cpp|cc|cxx|C|c\\+\\+)"; 53$cppExt = "(cpp|cc|cxx|C|c\\+\\+)";
54$hExt = "(h|H|hh|hxx|hpp|h\\+\\+)"; 54$hExt = "(h|H|hh|hxx|hpp|h\\+\\+)";
55 55
56# list of compat headers. scroll down ... much of boring stuff here.. 56# list of compat headers. scroll down ... much of boring stuff here..
57%compatmap = ( 57%compatmap = (
58 'qapp.h' => "qapplication.h", 58 'qapp.h' => "qapplication.h",
59 'qarray.h' => "qmemarray.h", 59 #'qarray.h' => "qmemarray.h",
60 'qbitarry.h' => "qbitarray.h", 60 #'qbitarry.h' => "qbitarray.h",
61 'qbttngrp.h' => "qbuttongroup.h", 61 'qbttngrp.h' => "qbuttongroup.h",
62 'qchkbox.h' => "qcheckbox.h", 62 #'qchkbox.h' => "qcheckbox.h",
63 'qclipbrd.h' => "qclipboard.h", 63 'qclipbrd.h' => "qclipboard.h",
64 'qcollect.h' => "qptrcollection.h", 64 #'qcollect.h' => "qptrcollection.h",
65 'qcollection.h' => "qptrcollection.h", 65 #'qcollection.h' => "qptrcollection.h",
66 'qcombo.h' => "qcombobox.h", 66 'qcombo.h' => "qcombobox.h",
67 'qconnect.h' => "qconnection.h", 67 'qconnect.h' => "qconnection.h",
68 'qdatetm.h' => "qdatetime.h", 68 'qdatetm.h' => "qdatetime.h",
69 'qdrawutl.h' => "qdrawutil.h", 69 'qdrawutl.h' => "qdrawutil.h",
70 'qdstream.h' => "qdatastream.h", 70 'qdstream.h' => "qdatastream.h",
71 'qfiledef.h' => "private/qfiledefs_p.h", 71 #'qfiledef.h' => "private/qfiledefs_p.h",
72 'qfiledlg.h' => "qfiledialog.h", 72 'qfiledlg.h' => "qfiledialog.h",
73 'qfileinf.h' => "qfileinfo.h", 73 'qfileinf.h' => "qfileinfo.h",
74 'qfontdta.h' => "qfontdata.h", 74 'qfontdta.h' => "qfontdata.h",
75 'qfontinf.h' => "qfontinfo.h", 75 'qfontinf.h' => "qfontinfo.h",
76 'qfontmet.h' => "qfontmetrics.h", 76 'qfontmet.h' => "qfontmetrics.h",
77 'qgrpbox.h' => "qgroupbox.h", 77 'qgrpbox.h' => "qgroupbox.h",
78 'qintcach.h' => "qintcache.h", 78 'qintcach.h' => "qintcache.h",
79 'qiodev.h' => "qiodevice.h", 79 'qiodev.h' => "qiodevice.h",
80 'qlcdnum.h' => "qlcdnumber.h", 80 'qlcdnum.h' => "qlcdnumber.h",
81 'qlined.h' => "qlineedit.h", 81 'qlined.h' => "qlineedit.h",
82 'qlist.h' => "qptrlist.h", 82 #'qlist.h' => "qptrlist.h",
83 'qmenudta.h' => "qmenudata.h", 83 'qmenudta.h' => "qmenudata.h",
84 'qmetaobj.h' => "qmetaobject.h", 84 'qmetaobj.h' => "qmetaobject.h",
85 'qmlined.h' => "qtmultilineedit.h", 85 'qmlined.h' => "qtmultilineedit.h",
86 'qmsgbox.h' => "qmessagebox.h", 86 'qmsgbox.h' => "qmessagebox.h",
87 'qmultilinedit.h' => "qmultilineedit.h", 87 'qmultilinedit.h' => "qmultilineedit.h",
88 'qobjcoll.h' => "qobjectlist.h>\n\#include <qobjectdict.h", 88 'qobjcoll.h' => "qobjectlist.h>\n\#include <qobjectdict.h",
89 'qobjdefs.h' => "qobjectdefs.h", 89 'qobjdefs.h' => "qobjectdefs.h",
90 'qpaintd.h' => "qpaintdevice.h", 90 'qpaintd.h' => "qpaintdevice.h",
91 'qpaintdc.h' => "qpaintdevicedefs.h", 91 'qpaintdc.h' => "qpaintdevicedefs.h",
92 'qpdevmet.h' => "qpaintdevicemetrics.h", 92 'qpdevmet.h' => "qpaintdevicemetrics.h",
93 'qpmcache.h' => "qpixmapcache.h", 93 'qpmcache.h' => "qpixmapcache.h",
94 'qpntarry.h' => "qpointarray.h", 94 'qpntarry.h' => "qpointarray.h",
95 'qpopmenu.h' => "qpopupmenu.h", 95 'qpopmenu.h' => "qpopupmenu.h",
96 'qprndlg.h' => "qprintdialog.h", 96 'qprndlg.h' => "qprintdialog.h",
97 'qprogbar.h' => "qprogressbar.h", 97 'qprogbar.h' => "qprogressbar.h",
98 'qprogdlg.h' => "qprogressdialog.h", 98 'qprogdlg.h' => "qprogressdialog.h",
99 'qpsprn.h' => "<private/qpsprinter_p.h>", 99 'qpsprn.h' => "<private/qpsprinter_p.h>",
100 'qpushbt.h' => "qpushbutton.h", 100 'qpushbt.h' => "qpushbutton.h",
101 'qqueue.h' => "qptrqueue.h", 101 'qqueue.h' => "qptrqueue.h",
102 'qradiobt.h' => "qradiobutton.h", 102 'qradiobt.h' => "qradiobutton.h",
103 'qrangect.h' => "qrangecontrol.h", 103 'qrangect.h' => "qrangecontrol.h",
104 'qscrbar.h' => "qscrollbar.h", 104 'qscrbar.h' => "qscrollbar.h",
105 'qsocknot.h' => "qsocketnotifier.h", 105 'qsocknot.h' => "qsocketnotifier.h",
106 'qstack.h' => "qptrstack.h", 106# 'qstack.h' => "qptrstack.h",
107 'qtabdlg.h' => "qtabdialog.h", 107 'qtabdlg.h' => "qtabdialog.h",
108 'qtstream.h' => "qtextstream.h", 108 'qtstream.h' => "qtextstream.h",
109 'qvector.h' => "qptrvector.h", 109# 'qvector.h' => "qptrvector.h",
110 'qwidcoll.h' => "qwidgetlist.h\n\#include <qwidgetintdict.h", 110 'qwidcoll.h' => "qwidgetlist.h\n\#include <qwidgetintdict.h",
111 'qwindefs.h' => "qwindowdefs.h", 111 'qwindefs.h' => "qwindowdefs.h",
112 112
113# and now the KDE specific compat includes 113# and now the KDE specific compat includes
114 'kapp.h' => "kapplication.h", 114# 'kapp.h' => "kapplication.h",
115 'kstddirs.h' => "kstandarddirs.h", 115# 'kstddirs.h' => "kstandarddirs.h",
116 'kuniqueapp.h' => "kuniqueapplication.h", 116# 'kuniqueapp.h' => "kuniqueapplication.h",
117 'ktmainwindow.h'=> "kmainwindow.h", 117# 'ktmainwindow.h'=> "kmainwindow.h",
118 'kcolorbtn.h' => "kcolorbutton.h", 118# 'kcolorbtn.h' => "kcolorbutton.h",
119 'kcolordlg.h' => "kcolordialog.h", 119# 'kcolordlg.h' => "kcolordialog.h",
120 'kxmlgui.h' => "kxmlguifactory.h", 120# 'kxmlgui.h' => "kxmlguifactory.h",
121); 121);
122 122
123 123
124# now it starts to get interesting again 124# now it starts to get interesting again
125 125
126# Look for source files in the given directory ($dir, first parameter) 126# Look for source files in the given directory ($dir, first parameter)
127sub find_fixable_sources ($) 127sub find_fixable_sources ($)
128{ 128{
129 # for now I grep the directory (requires srcdir==builddir) 129 # for now I grep the directory (requires srcdir==builddir)
130 # actually it should read the Makefile and 130 # actually it should read the Makefile and
131 # find the _SOURCES / _OBJECTS tags that are put there by 131 # find the _SOURCES / _OBJECTS tags that are put there by
132 # automake and am_edit, but thats an excercise to the reader ;-) 132 # automake and am_edit, but thats an excercise to the reader ;-)
133 133
134 my ( $dir ) = @_; 134 my ( $dir ) = @_;
135 135
136 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n"; 136 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n";
137 my @sources = grep { /^.*\.$cppExt$/o } readdir(DIR); 137 my @sources = grep { /^.*\.$cppExt$/o } readdir(DIR);
138 closedir(DIR); 138 closedir(DIR);
139 139
140 print "found sources: [ " . join(' ', @sources) . " ] in $dir\n" if ($verbose); 140 print "found sources: [ " . join(' ', @sources) . " ] in $dir\n" if ($verbose);
141 141
142 # prefix them with $dir 142 # prefix them with $dir
143 my @retsources = (); 143 my @retsources = ();
144 foreach $source(@sources) { 144 foreach $source(@sources) {
145 push @retsources, "$dir/$source"; 145 push @retsources, "$dir/$source";
146 } 146 }
147 147
148 return @retsources; 148 return @retsources;
149} 149}
150 150
151# Look for header files in the given directory ($dir, first parameter) 151# Look for header files in the given directory ($dir, first parameter)
152sub find_fixable_headers ($) 152sub find_fixable_headers ($)
153{ 153{
154 # for now I grep the directory (requires srcdir==builddir) 154 # for now I grep the directory (requires srcdir==builddir)
155 # actually it should read the Makefile and 155 # actually it should read the Makefile and
156 # find the _HEADERS tags that are put there by 156 # find the _HEADERS tags that are put there by
157 # automake and am_edit, but thats an excercise to the reader ;-) 157 # automake and am_edit, but thats an excercise to the reader ;-)
158 158
159 my ( $dir ) = @_; 159 my ( $dir ) = @_;
160 160
161 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n"; 161 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n";
162 my @headers = grep { /^.*\.$hExt$/o } readdir(DIR); 162 my @headers = grep { /^.*\.$hExt$/o } readdir(DIR);
163 closedir(DIR); 163 closedir(DIR);
164 164
165 print "found headers: [ " . join(' ', @headers) . " ] in $dir\n" if ($verbose); 165 print "found headers: [ " . join(' ', @headers) . " ] in $dir\n" if ($verbose);
166 166
167 # prefix them with $dir 167 # prefix them with $dir
168 my @retheaders = (); 168 my @retheaders = ();
169 foreach $source(@headers) { 169 foreach $source(@headers) {
170 push @retheaders, "$dir/$source"; 170 push @retheaders, "$dir/$source";
171 } 171 }
172 172
173 return @retheaders; 173 return @retheaders;
174} 174}
175 175
176sub find_removable_includes ($) 176sub find_removable_includes ($)
177{ 177{
178 my $srcfile = shift @_; 178 my $srcfile = shift @_;
179 open(SRC, "< $srcfile") || die "find_removable_includes: couldn't open '$srcfile'\n"; 179 open(SRC, "< $srcfile") || die "find_removable_includes: couldn't open '$srcfile'\n";
180 180
181 my @includes = (); 181 my @includes = ();
182 182
183 # we skip all includes that are somehow ifdefed 183 # we skip all includes that are somehow ifdefed
184 184
185 my $cpplevel = 0; 185 my $cpplevel = 0;
186 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 186 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
187 while (<SRC>) { 187 while (<SRC>) {
188 if ($_ =~ m/^\#if/) { 188 if ($_ =~ m/^\#if/) {
189 $cpplevel = $cpplevel + 1; 189 $cpplevel = $cpplevel + 1;
190 next; 190 next;
191 } 191 }
192 if ($_ =~ m/^\#endif/) { 192 if ($_ =~ m/^\#endif/) {
193 $cpplevel = $cpplevel - 1; 193 $cpplevel = $cpplevel - 1;
194 next; 194 next;
195 } 195 }
196 #if ($cpplevel == 0 && $_ =~ m/^\#include\s*[\"<]([qk]\S*\.h)[\">]\S*/) { 196 #if ($cpplevel == 0 && $_ =~ m/^\#include\s*[\"<]([qk]\S*\.h)[\">]\S*/) {
197 if ($cpplevel == 0 && (($_ =~ m/^\#include\s*\"(\S+\.h)\"\S*/) || ($_ =~ m/^\#include\s*\<([qk]\S+.h)\>\S*/))) { 197 if ($cpplevel == 0 && (($_ =~ m/^\#include\s*\"(\S+\.h)\"\S*/) || ($_ =~ m/^\#include\s*\<([qk]\S+.h)\>\S*/))) {
198 push @includes, $1; 198 push @includes, $1;
199 next; 199 next;
200 } 200 }
201 } 201 }
202 close SRC; 202 close SRC;
203 203
204 print "No fixable includes found in $srcfile\n" if ($verbose and not @includes); 204 print "No fixable includes found in $srcfile\n" if ($verbose and not @includes);
205 print "found includes: [ " . join(' ', @includes) . " ]\n" if ($verbose and @includes); 205 print "found includes: [ " . join(' ', @includes) . " ]\n" if ($verbose and @includes);
206 206
207 return @includes; 207 return @includes;
208} 208}
209 209
210# first parameter: srcfile 210# first parameter: srcfile
211# second parameter: include to remove 211# second parameter: include to remove
212# third parameter is the duplicate level: this include is removed $level times 212# third parameter is the duplicate level: this include is removed $level times
213sub remove_include ($$$) 213sub remove_include ($$$)
214{ 214{
215 my $srcfile = shift @_; 215 my $srcfile = shift @_;
216 my $include = quotemeta(shift @_); 216 my $include = quotemeta(shift @_);
217 my $level = shift @_; 217 my $level = shift @_;
218 218
219 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile); 219 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile);
220 open(I, "< $srcfile") or die "remove_include: couldn't open '$srcfile'\n"; 220 open(I, "< $srcfile") or die "remove_include: couldn't open '$srcfile'\n";
221 my @contents = <I>; 221 my @contents = <I>;
222 close(I); 222 close(I);
223 223
224 # ok, CPU time doesn't count so we do it the lazy way 224 # ok, CPU time doesn't count so we do it the lazy way
225 # we should remove the last occurence of the include in the 225 # we should remove the last occurence of the include in the
226 # file because in case its a duplicate removing the first 226 # file because in case its a duplicate removing the first
227 # one could make a difference. 227 # one could make a difference.
228 my @revcontents = reverse @contents; 228 my @revcontents = reverse @contents;
229 @contents = (); 229 @contents = ();
230 230
231 # we skip all inludes that are somehow ifdefed 231 # we skip all inludes that are somehow ifdefed
232 # note the logic is reversed because it operates 232 # note the logic is reversed because it operates
233 # on reversed lines :) 233 # on reversed lines :)
234 my $cpplevel = 0; 234 my $cpplevel = 0;
235 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 235 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
236 foreach $line (@revcontents) { 236 foreach $line (@revcontents) {
237 if ($line =~ m/^\#if/) { 237 if ($line =~ m/^\#if/) {
238 $cpplevel = $cpplevel - 1; 238 $cpplevel = $cpplevel - 1;
239 push @contents, $line; 239 push @contents, $line;
240 next; 240 next;
241 } 241 }
242 242
243 if ($line =~ m/^\#endif/) { 243 if ($line =~ m/^\#endif/) {
244 $cpplevel = $cpplevel + 1; 244 $cpplevel = $cpplevel + 1;
245 push @contents, $line; 245 push @contents, $line;
246 next; 246 next;
247 } 247 }
248 248
249 #if ($cpplevel == 0 && $level > 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*$/) { 249 #if ($cpplevel == 0 && $level > 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*$/) {
250 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) { 250 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) {
251 $level = $level - 1; 251 $level = $level - 1;
252 # skipping the line.. 252 # skipping the line..
253 next; 253 next;
254 } 254 }
255 255
256 push @contents, $line; 256 push @contents, $line;
257 } 257 }
258 258
259 # now we have the fixed contents in @contents, although in wrong order 259 # now we have the fixed contents in @contents, although in wrong order
260 open(O, "> $srcfile") || die "remove_include: couldn't open '$srcfile' for writing\n"; 260 open(O, "> $srcfile") || die "remove_include: couldn't open '$srcfile' for writing\n";
261 print O reverse @contents; 261 print O reverse @contents;
262 close (O); 262 close (O);
263} 263}
264 264
265# first parameter: srcfile 265# first parameter: srcfile
266# second parameter: include to replace 266# second parameter: include to replace
267# third parameter the include file to replace it with 267# third parameter the include file to replace it with
268sub replace_include ($$$) 268sub replace_include ($$$)
269{ 269{
270 my $srcfile = shift @_; 270 my $srcfile = shift @_;
271 my $include = quotemeta(shift @_); 271 my $include = quotemeta(shift @_);
272 my $destinclude = shift @_; 272 my $destinclude = shift @_;
273 273
274 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile); 274 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile);
275 open(I, "< $srcfile") or die "replace_include: couldn't open '$srcfile'\n"; 275 open(I, "< $srcfile") or die "replace_include: couldn't open '$srcfile'\n";
276 my @contents = <I>; 276 my @contents = <I>;
277 close(I); 277 close(I);
278 278
279 # ok, CPU time doesn't count so we do it the lazy way 279 # ok, CPU time doesn't count so we do it the lazy way
280 my @revcontents = reverse @contents; 280 my @revcontents = reverse @contents;
281 @contents = (); 281 @contents = ();
282 282
283 # we skip all inludes that are somehow ifdefed 283 # we skip all inludes that are somehow ifdefed
284 # note the logic is reversed because it operates 284 # note the logic is reversed because it operates
285 # on reversed lines :) 285 # on reversed lines :)
286 my $cpplevel = 0; 286 my $cpplevel = 0;
287 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 287 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
288 foreach $line (@revcontents) { 288 foreach $line (@revcontents) {
289 if ($line =~ m/^\#if/) { 289 if ($line =~ m/^\#if/) {
290 $cpplevel = $cpplevel - 1; 290 $cpplevel = $cpplevel - 1;
291 push @contents, $line; 291 push @contents, $line;
292 next; 292 next;
293 } 293 }
294 294
295 if ($line =~ m/^\#endif/) { 295 if ($line =~ m/^\#endif/) {
296 $cpplevel = $cpplevel + 1; 296 $cpplevel = $cpplevel + 1;
297 push @contents, $line; 297 push @contents, $line;
298 next; 298 next;
299 } 299 }
300 300
301 #if ($cpplevel == 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*/) { 301 #if ($cpplevel == 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*/) {
302 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) { 302 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) {
303 print "HAH! found $include to replace in $srcfile!\n" if($verbose); 303 print "HAH! found $include to replace in $srcfile!\n" if($verbose);
304 $line =~ s/(\#include\s*[\"<])$include([\">]\S*)/$1$destinclude$2/; 304 $line =~ s/(\#include\s*[\"<])$include([\">]\S*)/$1$destinclude$2/;
305 } 305 }
306 306
307 push @contents, $line; 307 push @contents, $line;
308 } 308 }
309 309
310 # now we have the fixed contents in @contents 310 # now we have the fixed contents in @contents
311 open(O, "> $srcfile") || die "replace_include: couldn't open '$srcfile' for writing\n"; 311 open(O, "> $srcfile") || die "replace_include: couldn't open '$srcfile' for writing\n";
312 print O reverse @contents; 312 print O reverse @contents;
313 close (O); 313 close (O);
314} 314}
315 315
316sub fix_duplicates($) 316sub fix_duplicates($)
317{ 317{
318 my $srcfile = shift @_; 318 my $srcfile = shift @_;
319 319
320 my @includes = &find_removable_includes($srcfile); 320 my @includes = &find_removable_includes($srcfile);
321 321
322 my %inclMap = (); 322 my %inclMap = ();
323 323
324 # initialize 324 # initialize
325 foreach $include (@includes) { 325 foreach $include (@includes) {
326 $inclMap{$include} = 0; 326 $inclMap{$include} = 0;
327 } 327 }
328 328
329 # count number of occurences 329 # count number of occurences
330 foreach $include (@includes) { 330 foreach $include (@includes) {
331 $inclMap{$include} = $inclMap{$include} + 1; 331 $inclMap{$include} = $inclMap{$include} + 1;
332 } 332 }
333 333
334 # check for duplicates 334 # check for duplicates
335 foreach $include (keys %inclMap) { 335 foreach $include (keys %inclMap) {
336 next if $inclMap{$include} <= 1; 336 next if $inclMap{$include} <= 1;
337 337
338 print "$srcfile: duplicate level ". $inclMap{$include} .": ". $include ."\n"; 338 print "$srcfile: duplicate level ". $inclMap{$include} .": ". $include ."\n";
339 339
340 &remove_include($srcfile, $include, $inclMap{$include} - 1) if($modify); 340 &remove_include($srcfile, $include, $inclMap{$include} - 1) if($modify);
341 } 341 }
342} 342}
343 343
344sub fix_compat_includes($) 344sub fix_compat_includes($)
345{ 345{
346 my $srcfile = shift @_; 346 my $srcfile = shift @_;
347 347
348 my @includes = &find_removable_includes($srcfile); 348 my @includes = &find_removable_includes($srcfile);
349 349
350 my %inclMap = (); 350 my %inclMap = ();
351 351
352 # initialize 352 # initialize
353 foreach $include (@includes) { 353 foreach $include (@includes) {
354 $inclMap{$include} = 0; 354 $inclMap{$include} = 0;
355 } 355 }
356 356
357 # count number of occurences 357 # count number of occurences
358 foreach $include (@includes) { 358 foreach $include (@includes) {
359 $inclMap{$include} = $inclMap{$include} + 1; 359 $inclMap{$include} = $inclMap{$include} + 1;
360 } 360 }
361 361
362 # check for compat headers 362 # check for compat headers
363 foreach $include (keys %inclMap) { 363 foreach $include (keys %inclMap) {
364 if( defined $compatmap{$include}) { 364 if( defined $compatmap{$include}) {
365 print "$srcfile: compat header: $include, to be replaced by ". $compatmap{$include} ."\n"; 365 print "$srcfile: compat header: $include, to be replaced by ". $compatmap{$include} ."\n";
366 &replace_include($srcfile, $include, $compatmap{$include}) if($modify); 366 &replace_include($srcfile, $include, $compatmap{$include}) if($modify);
367 } 367 }
368 } 368 }
369} 369}
370 370
371# copies a file from src to dest, overwrites destination if exists 371# copies a file from src to dest, overwrites destination if exists
372sub copy_file($$) 372sub copy_file($$)
373{ 373{
374 my $src = shift(@_); 374 my $src = shift(@_);
375 my $dst = shift(@_); 375 my $dst = shift(@_);
376 376
377 open(I, "< $src") or die "copy_file: can't open $src for input\n"; 377 open(I, "< $src") or die "copy_file: can't open $src for input\n";
378 my @fcontents = <I>; 378 my @fcontents = <I>;
379 close(I); 379 close(I);
380 open(O, "> $dst") or die "copy_file: can't open $dst for output\n"; 380 open(O, "> $dst") or die "copy_file: can't open $dst for output\n";
381 print O @fcontents; 381 print O @fcontents;
382 close(O); 382 close(O);
383} 383}
384 384
385# interrupt handler for fix_unnecessary 385# interrupt handler for fix_unnecessary
386sub sighandler_fix_unnecessary() 386sub sighandler_fix_unnecessary()
387{ 387{
388 my($sig) = @_; 388 my($sig) = @_;
389 print "Caught a SIG$sig--shutting down after restoring $srcfile\n"; 389 print "Caught a SIG$sig--shutting down after restoring $srcfile\n";
390 chdir($srcdir); 390 chdir($srcdir);
391 unlink $srcfile || warn "couldn't unlink $srcfile"; 391 unlink $srcfile || warn "couldn't unlink $srcfile";
392 rename $localbackup, $srcfile || warn "couldn't rename $localbackup to $srcfile"; 392 rename $localbackup, $srcfile || warn "couldn't rename $localbackup to $srcfile";
393 exit(1); 393 exit(1);
394} 394}
395 395
396sub fix_unnecessary($) 396sub fix_unnecessary($)
397{ 397{
398 local $srcfile = shift @_; 398 local $srcfile = shift @_;
399 local $srcdir = dirname($srcfile); 399 local $srcdir = dirname($srcfile);
400 400
401 # find canonical path for srcdir 401 # find canonical path for srcdir
402 my $origdir = cwd; 402 my $origdir = cwd;
403 chdir($srcdir); 403 chdir($srcdir);
404 $srcdir = cwd; 404 $srcdir = cwd;
405 print "srcdir=$srcdir\n" if($verbose); 405 print "srcdir=$srcdir\n" if($verbose);
406 406
407 my $builddir = $srcdir; 407 my $builddir = $srcdir;
408 my $makecmd = "make"; 408 my $makecmd = "make";
409 if (defined $ENV{"OBJ_REPLACEMENT"}) 409 if (defined $ENV{"OBJ_REPLACEMENT"})
410 { 410 {
411 # we have to use sed here, because perl can't do s#a#b# 411 # we have to use sed here, because perl can't do s#a#b#
412 $builddir = `echo $srcdir | sed -e \$OBJ_REPLACEMENT`; 412 $builddir = `echo $srcdir | sed -e \$OBJ_REPLACEMENT`;
413 chomp $builddir; 413 chomp $builddir;
414 $makecmd = "makeobj"; 414 $makecmd = "makeobj";
415 } 415 }
416 print "builddir=$builddir\n" if($verbose); 416 print "builddir=$builddir\n" if($verbose);
417 417
418 my $tot = $exp_success + $exp_failure; 418 my $tot = $exp_success + $exp_failure;
419 print "=============== $srcfile (successes: $exp_success; total: $tot)\n"; 419 print "=============== $srcfile (successes: $exp_success; total: $tot)\n";
420 420
421 $srcfile = basename($srcfile); 421 $srcfile = basename($srcfile);
422 422
423 # first figure out some details 423 # first figure out some details
424 my @includes = &find_removable_includes($srcfile); 424 my @includes = &find_removable_includes($srcfile);
425 425
426 my $blanksrc = $srcfile; 426 my $blanksrc = $srcfile;
427 $blanksrc =~ s/(.*)\.[^\.]+/$1/; 427 $blanksrc =~ s/(.*)\.[^\.]+/$1/;
428 428
429 print "Checking for initial compilation: "; 429 print "Checking for initial compilation: ";
430 chdir($builddir); 430 chdir($builddir);
431 my $objextension = "BUG"; 431 my $objextension = "BUG";
432 unlink "$blanksrc.lo"; 432 unlink "$blanksrc.lo";
433 my $output = `$makecmd $blanksrc.lo 2>&1`; 433 my $output = `$makecmd $blanksrc.lo 2>&1`;
434 $objextension = ".lo" if ( 0 == ($? >> 8)); 434 $objextension = ".lo" if ( 0 == ($? >> 8));
435 if($objextension eq "BUG") { 435 if($objextension eq "BUG") {
436 print "failed with .lo... "; 436 print "failed with .lo... ";
437 unlink "$blanksrc.o"; 437 unlink "$blanksrc.o";
438 $output = `$makecmd $blanksrc.o 2>&1`; 438 $output = `$makecmd $blanksrc.o 2>&1`;
439 $objextension = ".o" if ( 0 == ($? >> 8)); 439 $objextension = ".o" if ( 0 == ($? >> 8));
440 } 440 }
441 if($objextension eq "BUG") { 441 if($objextension eq "BUG") {
442 warn "can't figure out right compile command for $srcfile :-(\n??? unused, or didn't compile in the first place?\n"; 442 warn "can't figure out right compile command for $srcfile :-(\n??? unused, or didn't compile in the first place?\n";
443 warn "$output"; 443 warn "$output";
444 chdir($origdir); 444 chdir($origdir);
445 return; 445 return;
446 } 446 }
447 447
448 print "worked with $objextension\n"; 448 print "worked with $objextension\n";
449 449
450 # now try to drop some includes 450 # now try to drop some includes
451 foreach $include (@includes) { 451 foreach $include (@includes) {
452 # kdatastream is special because 452 # kdatastream is special because
453 # it will break the application if removed even 453 # it will break the application if removed even
454 # if it continues to compile 454 # if it continues to compile
455 next if( $include eq "kdatastream.h"); 455 next if( $include eq "kdatastream.h");
456 # I also like to have kdebug.h still in 456 # I also like to have kdebug.h still in
457 # so that it's easy to add kdDebug calls 457 # so that it's easy to add kdDebug calls
458 next if( $include eq "kdebug.h"); 458 next if( $include eq "kdebug.h");
459 # avoid this one as it might cause 459 # avoid this one as it might cause
460 # certain code parts to be disabled from compilation 460 # certain code parts to be disabled from compilation
461 next if( $include eq "qmodules.h"); 461 next if( $include eq "qmodules.h");
462 # check if it is its own header file 462 # check if it is its own header file
463 my $blankhdr = $include; 463 my $blankhdr = $include;
464 $blankhdr =~ s/(.*)\.[^\.]+/$1/; 464 $blankhdr =~ s/(.*)\.[^\.]+/$1/;
465 next if ($blankhdr eq $blanksrc); 465 next if ($blankhdr eq $blanksrc);
466 466
467 chdir($srcdir); 467 chdir($srcdir);
468 468
469 local $localbackup = $srcfile . "#fixincludes"; 469 local $localbackup = $srcfile . "#fixincludes";
470 470
471 # preserve timestamp if possible for CVS 471 # preserve timestamp if possible for CVS
472 unlink $localbackup; 472 unlink $localbackup;
473 rename $srcfile, $localbackup; 473 rename $srcfile, $localbackup;
474 copy_file($localbackup, $srcfile); 474 copy_file($localbackup, $srcfile);
475 475
476 # revert to backup in case of interrupt (Ctrl+C) 476 # revert to backup in case of interrupt (Ctrl+C)
477 $SIG{'INT'} = \&sighandler_fix_unnecessary; 477 $SIG{'INT'} = \&sighandler_fix_unnecessary;
478 478
479 # check if it still compiles 479 # check if it still compiles
480 if($verbose) { 480 if($verbose) {
481 chdir($builddir); 481 chdir($builddir);
482 unlink "$builddir/$blanksrc$objextension"; 482 unlink "$builddir/$blanksrc$objextension";
483 `$makecmd $blanksrc$objextension 2>&1`; 483 `$makecmd $blanksrc$objextension 2>&1`;
484 die "unexpected error $output\nexitcode=" . ($? >> 8) if($? >> 8); 484 die "unexpected error $output\nexitcode=" . ($? >> 8) if($? >> 8);
485 chdir($srcdir); 485 chdir($srcdir);
486 } 486 }
487 487
488 # duplicates have to be nuked here , so it will be dropped maximum once 488 # duplicates have to be nuked here , so it will be dropped maximum once
489 print "trying without $include: "; 489 print "trying without $include: ";
490 &remove_include($srcfile, $include, 1); 490 &remove_include($srcfile, $include, 1);
491 491
492 chdir($builddir); 492 chdir($builddir);
493 493
494 # try if it compiles 494 # try if it compiles
495 unlink "$builddir/$blanksrc$objextension"; 495 unlink "$builddir/$blanksrc$objextension";
496 $output=`$makecmd $blanksrc$objextension 2>&1`; 496 $output=`$makecmd $blanksrc$objextension 2>&1`;
497 my $retcode = ($? >> 8); 497 my $retcode = ($? >> 8);
498 #print "retcode=$retcode\n$output" if ($verbose); 498 #print "retcode=$retcode\n$output" if ($verbose);
499 499
500 chdir($srcdir); 500 chdir($srcdir);
501 if($retcode == 0) { 501 if($retcode == 0) {
502 # wow, it worked, lets continue! 502 # wow, it worked, lets continue!
503 print "SUCCESS!\n"; 503 print "SUCCESS!\n";
504 $SIG{'INT'} = 'DEFAULT'; 504 $SIG{'INT'} = 'DEFAULT';
505 unlink $localbackup; 505 unlink $localbackup;
506 $exp_success = $exp_success + 1; 506 $exp_success = $exp_success + 1;
507 } 507 }
508 else { 508 else {
509 # better luck next time 509 # better luck next time
510 print "failed\n"; 510 print "failed\n";
511 unlink $srcfile; 511 unlink $srcfile;
512 rename $localbackup, $srcfile; 512 rename $localbackup, $srcfile;
513 $SIG{'INT'} = 'DEFAULT'; 513 $SIG{'INT'} = 'DEFAULT';
514 514
515 $exp_failure = $exp_failure + 1; 515 $exp_failure = $exp_failure + 1;
516 } 516 }
517 } 517 }
518 518
519 print "\n"; 519 print "\n";
520 520
521 chdir($origdir); 521 chdir($origdir);
522} 522}
523 523
524sub process_source_file($) 524sub process_source_file($)
525{ 525{
526 local $file = shift @_; 526 local $file = shift @_;
527 print "Checking: $file\n" if($verbose); 527 print "Checking: $file\n" if($verbose);
528 &fix_compat_includes($file); 528 &fix_compat_includes($file);
529 &fix_duplicates($file); 529 &fix_duplicates($file);
530 if ($experimental) { 530 if ($experimental) {
531 &fix_unnecessary($file); 531 &fix_unnecessary($file);
532 } 532 }
533 print "\n" if ($verbose); 533 print "\n" if ($verbose);
534} 534}
535 535
536# here is the main logic 536# here is the main logic
537 537
538# warn about modified files 538# warn about modified files
539if($modify) { 539if($modify) {
540 `cvscheck | grep '^[MmC]'`; 540 `cvscheck | grep '^[MmC]'`;
541 print "WARNING: you have pending local changes.You might commit them by accident!\n" if($? >> 8 == 0); 541 print "WARNING: you have pending local changes.You might commit them by accident!\n" if($? >> 8 == 0);
542} 542}
543 543
544# process files from the command line, if any 544# process files from the command line, if any
545if ( $#explicitfiles >= 0 ) { 545if ( $#explicitfiles >= 0 ) {
546 foreach $file( @explicitfiles ) { 546 foreach $file( @explicitfiles ) {
547 &process_source_file( $file ); 547 &process_source_file( $file );
548 } 548 }
549 exit 0; 549 exit 0;
550} 550}
551 551
552# first generate a list of subdirectories 552# first generate a list of subdirectories
553@dirlist = ("."); 553@dirlist = (".");
554foreach $dir ( @dirlist ) { 554foreach $dir ( @dirlist ) {
555 opendir (DIR, "$dir") || warn "Couldn't read '$dir'"; 555 opendir (DIR, "$dir") || warn "Couldn't read '$dir'";
556 my $subdir = ""; 556 my $subdir = "";
557 while( $subdir = readdir(DIR)) { 557 while( $subdir = readdir(DIR)) {
558 next if ($subdir =~ /^\..*/); 558 next if ($subdir =~ /^\..*/);
559 push @dirlist, "$dir/$subdir" if ( -d "$dir/$subdir"); 559 push @dirlist, "$dir/$subdir" if ( -d "$dir/$subdir");
560 } 560 }
561 closedir(DIR); 561 closedir(DIR);
562} 562}
563 563
564# now iterate over all subdirs 564# now iterate over all subdirs
565foreach $dir(@dirlist) { 565foreach $dir(@dirlist) {
566 566
567 # check if this directory wants not to be fixed 567 # check if this directory wants not to be fixed
568 if(open(M, "$dir/Makefile.am")) { 568 if(open(M, "$dir/Makefile.am")) {
569 my @mcontents = grep /(\-UQT_NO_COMPAT|\-UKDE_NO_COMPAT)/, <M>; 569 my @mcontents = grep /(\-UQT_NO_COMPAT|\-UKDE_NO_COMPAT)/, <M>;
570 close(M); 570 close(M);
571 if ( @mcontents ) { 571 if ( @mcontents ) {
572 print "Skipping directory: $dir\n"; 572 print "Skipping directory: $dir\n";
573 next; 573 next;
574 } 574 }
575 } 575 }
576 576
577 @headers = &find_fixable_headers($dir); 577 @headers = &find_fixable_headers($dir);
578 foreach $file(@headers) { 578 foreach $file(@headers) {
579 print "Checking: $file\n" if($verbose); 579 print "Checking: $file\n" if($verbose);
580 &fix_compat_includes($file); 580 &fix_compat_includes($file);
581 &fix_duplicates($file); 581 &fix_duplicates($file);
582 } 582 }
583 @sources = &find_fixable_sources($dir); 583 @sources = &find_fixable_sources($dir);
584 foreach $file(@sources) { 584 foreach $file(@sources) {
585 &process_source_file($file); 585 &process_source_file($file);
586 } 586 }
587} 587}