Still more tweaks to renaming.
[WebKit-https.git] / WebKitTools / Scripts / do-webcore-rename
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1.  Redistributions of source code must retain the above copyright
10 #     notice, this list of conditions and the following disclaimer. 
11 # 2.  Redistributions in binary form must reproduce the above copyright
12 #     notice, this list of conditions and the following disclaimer in the
13 #     documentation and/or other materials provided with the distribution. 
14 # 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 #     its contributors may be used to endorse or promote products derived
16 #     from this software without specific prior written permission. 
17 #
18 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 # Script to do a rename in WebCore. It's sort of a "one time use" script,
30 # but we might change it around for other uses later.
31
32 use strict;
33 use FindBin;
34 use lib $FindBin::Bin;
35 use webkitdirs;
36 use File::Find;
37
38 setConfiguration();
39 chdirWebKit();
40
41 my %words;
42
43 # find all files in WebCore we want to process
44
45 my @paths;
46 find(\&wanted, "WebCore");
47
48 sub wanted
49 {
50     my $file = $_;
51
52     if ($file eq "icu") {
53         $File::Find::prune = 1;
54         return;
55     }
56
57     if ($file =~ /^\../) {
58         $File::Find::prune = 1;
59         return;
60     }
61
62     return if $file =~ /^ChangeLog/;
63     return if -d $file;
64
65     push @paths, $File::Find::name;
66 }
67
68 # find all symbols ending in Impl
69
70 my %exceptions = (
71     "Array" => 1,
72     "AtomicString" => 1,
73     "DeprecatedValueList" => 1,
74     "DeprecatedPtrList" => 1,
75     "QualifiedName" => 1,
76     "String" => 1,
77     "feature" => 1,
78     "name" => 1,
79     "namespaceURI" => 1,
80     "node" => 1,
81     "qualifiedName" => 1,
82     "str" => 1,
83     "version" => 1,
84
85     "Rect" => 1,
86 );
87
88 my %otherRenames = (
89     "MouseEvent" => "PlatformMouseEvent",
90     "WheelEvent" => "PlatformWheelEvent",
91     "XSLTProcessor" => "JSXSLTProcessor",
92     "Document" => "Document_",
93     "Event" => "Event_",
94     "HTMLCollection" => "JSHTMLCollection",
95     "HTMLSelectCollection" => "JSHTMLSelectCollection",
96     "HTMLSelectElement" => "JSHTMLSelectElement",
97     "HTMLElement" => "JSHTMLElement",
98     "HTMLDocument" => "JSHTMLDocument",
99     "MacFrame" => "FrameMac",
100     "createAttrImplIfNeeded" => "createAttrIfNeeded",
101     "dom2_rangeimpl" => "Range",
102     "dom2_viewsimpl" => "AbstractView",
103     "dom_position" => "Position",
104     "html_canvasimpl" => "HTMLCanvasElement",
105     "html_documentimpl" => "HTMLDocument",
106     "jsediting" => "JSEditor",
107     "render_arena" => "RenderArena",
108     "render_box" => "RenderBox",
109     "render_br" => "RenderBR",
110     "render_canvas" => "RenderCanvas",
111     "render_canvasimage" => "RenderCanvasImage",
112     "render_flexbox" => "RenderFlexibleBox",
113     "render_flow" => "RenderFlow",
114     "render_image" => "RenderImage",
115     "render_inline" => "RenderInline",
116     "render_layer" => "RenderLayer",
117     "render_object" => "RenderObject",
118     "render_theme" => "RenderTheme",
119     "render_theme_mac" => "RenderThemeMac",
120     "visible_text" => "TextIterator",
121     "xml_tokenizer" => "Tokenizer",
122     "xmlserializer" => "JSXMLSerializer",
123     "xslt_processorimpl" => "XSLTProcessorImpl",
124     "KWQ_BLOCK_EXCEPTIONS" => "BEGIN_BLOCK_OBJC_EXCEPTIONS",
125     "KWQ_UNBLOCK_EXCEPTIONS" => "END_BLOCK_OBJC_EXCEPTIONS",
126     "KWQReportBlockedException" => "ReportBlockedObjCException",
127     "KWQExceptions" => "BlockExceptions",
128     "KWQAccObject" => "WebCoreAXObject",
129     "KWQAccObjectID" => "WebCoreAXID",
130     "KWQAccObjectCache" => "AccessibilityObjectCache",
131     "accObjectID" => "axObjectID",
132     "setAccObjectID" => "setAXObjectID",
133     "removeAccObjectID" => "removeAXObjectID",
134     "KWQClipboard" => "ClipboardMac",
135     "KWQTextField" => "WebCoreTextField",
136     "KWQTextArea" => "WebCoreTextArea",
137     "KWQTextAreaTextView" => "WebCoreTextView",
138     "KWQValueList" => "DeprecatedValueList",
139     "QValueList" => "DeprecatedValueList",
140     "QString" => "DeprecatedString",
141     "QCString" => "DeprecatedCString",
142     "Array" => "DeprecatedArray",
143     "ByteArray" => "DeprecatedByteArray",
144     "KURL" => "DeprecatedURL",
145     "QStringList" => "DeprecatedStringList",
146     "KWQPtrList" => "DeprecatedPtrList",
147     "QPtrList" => "DeprecatedPtrList",
148     "KWQListImpl" => "DeprecatedPtrListImpl",
149     "KWQValueListImpl" => "DeprecatedValueListImpl",
150     "QPtrQueue" => "DeprecatedPtrQueue",
151     "KWQValueListNodeImpl" => "DeprecatedValueListImplNode",
152     "KWQValueListIteratorImpl" => "DeprecatedValueListImplIterator",
153     "KWQListIteratorImpl" => "DeprecatedPtrListImplIterator",
154     "QPtrListIterator" => "DeprecatedPtrListIterator",
155     "QValueListConstIterator" => "DeprecatedValueListConstIterator",
156     "QValueListIterator" => "DeprecatedValueListIterator",
157     "QValueListNode" => "DeprecatedValueListNode",
158     "KWQRenderTreeDebug" => "RenderTreeAsText",
159     "QRegExp" => "RegularExpression",
160     "KWQRegExp" => "RegularExpression",
161     "KJavaAppletWidget" => "JavaAppletWidget",
162     "KWQKJavaAppletWidget" => "JavaAppletWidget",
163     "uint" => "unsigned",
164 );
165
166 # rename most files with names ending in Impl
167
168 my %newFile;
169 for my $file (sort @paths) {
170     my $f = $file;
171     $f = "$1$otherRenames{$2}$3" if $f =~ /^(.*\/)(\w+)(\.\w+)$/ && $otherRenames{$2};
172     $f = "$1$2$3" if $f =~ /^(.*?)(\w+)Impl(\.\w+)$/ && !$exceptions{$2};
173     if ($f ne $file) {
174         $newFile{$file} = $f;
175     }
176 }
177
178 for my $file (sort @paths) {
179     if ($newFile{$file}) {
180         my $newFile = $newFile{$file};
181         print "Renaming $file to $newFile\n";
182         system "mv $file $newFile"; # use svn move when "doing the real thing"
183     }
184 }
185
186 # change all file contents
187
188 for my $file (sort @paths) {
189     $file = $newFile{$file} if $newFile{$file};
190     my $contents;
191     {
192         local $/;
193         open FILE, $file or die;
194         $contents = <FILE>;
195         close FILE;
196     }
197     my $newContents = $contents;
198
199     # fix namespace names
200     $newContents =~ s/\bDOM::/WebCore::/g;
201     $newContents =~ s/\bKDOM::/WebCore::/g;
202     $newContents =~ s/\bKSVG::/WebCore::/g;
203     $newContents =~ s/\bkhtml::/WebCore::/g;
204     $newContents =~ s/namespace DOM\b/namespace WebCore/g;
205     $newContents =~ s/namespace KDOM\b/namespace WebCore/g;
206     $newContents =~ s/namespace KSVG\b/namespace WebCore/g;
207     $newContents =~ s/namespace khtml\b/namespace WebCore/g;
208
209     if ($file !~ /\.(pm|idl)$/) {
210         # fix DOMString
211         $newContents =~ s/\#define DOMStringImpl StringImpl\n//;
212         $newContents =~ s/\bDOMString\b/String/g;
213         $newContents =~ s/\bDOMStringImpl\b/StringImpl/g;
214         $newContents =~ s/\#define String String/#define DOMString String/;
215
216         # fix other identifiers that would collide otherwise
217         for my $from (keys %otherRenames) {
218             my $to = $otherRenames{$from};
219             $newContents =~ s/\b$from\b/$to/g;
220             $newContents =~ s/"$to"/"$from"/g;
221         }
222         $newContents =~ s/\#\#Event_/\#\#Event/g;
223     }
224
225     # remove some bogus stuff
226     $newContents =~ s/typedef unsigned unsigned;\n//;
227     $newContents =~ s/friend class HTMLFormElement;\s*\n\s*(friend class HTMLFormCollection)/$1/;
228
229     # fix binding code generation
230     if ($file =~ /\.pm$/) {
231         $newContents =~ s/interfaceName \. "Impl"/interfaceName/g;
232         $newContents =~ s/{interfaceName}Impl/{interfaceName}/g;
233         $newContents =~ s/{type}Impl/{type}/g;
234         $newContents =~ s/html_documentimpl/HTMLDocument/g;
235     }
236
237     # fix words with Impl suffix
238     $newContents =~ s/\b(\w+)Impl\b/$exceptions{$1} ? "$1Impl" : $1/ge;
239
240     # fix typo that annoys me
241     $newContents =~ s/it.s base class/its base class/;
242
243     if ($newContents ne $contents) {
244         open FILE, ">", $file or die;
245         print FILE $newContents;
246         close FILE;
247     }
248 }