6ad4468f007bd84e7ebacee3fe566e575ad7b646
[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" => "RenderTreeTextForm",
159     "QRegExp" => "Regexp",
160     "KWQRegExp" => "Regexp",
161     "KJavaAppletWidget" => "JavaAppletWidget",
162     "KWQKJavaAppletWidget" => "JavaAppletWidget",
163 );
164
165 # rename most files with names ending in Impl
166
167 my %newFile;
168 for my $file (sort @paths) {
169     my $f = $file;
170     $f = "$1$otherRenames{$2}$3" if $f =~ /^(.*\/)(\w+)(\.\w+)$/ && $otherRenames{$2};
171     $f = "$1$2$3" if $f =~ /^(.*?)(\w+)Impl(\.\w+)$/ && !$exceptions{$2};
172     if ($f ne $file) {
173         $newFile{$file} = $f;
174     }
175 }
176
177 for my $file (sort @paths) {
178     if ($newFile{$file}) {
179         my $newFile = $newFile{$file};
180         print "Renaming $file to $newFile\n";
181         system "mv $file $newFile"; # use svn move when "doing the real thing"
182     }
183 }
184
185 # change all file contents
186
187 for my $file (sort @paths) {
188     $file = $newFile{$file} if $newFile{$file};
189     my $contents;
190     {
191         local $/;
192         open FILE, $file or die;
193         $contents = <FILE>;
194         close FILE;
195     }
196     my $newContents = $contents;
197
198     # fix DOM and khtml namespaces
199     $newContents =~ s/\bDOM::/WebCore::/g;
200     $newContents =~ s/\bkhtml::/WebCore::/g;
201     $newContents =~ s/namespace DOM\b/namespace WebCore/g;
202     $newContents =~ s/namespace khtml\b/namespace WebCore/g;
203
204     if ($file !~ /\.(pm|idl)$/) {
205         # fix DOMString
206         $newContents =~ s/\#define DOMStringImpl StringImpl\n//;
207         $newContents =~ s/\bDOMString\b/String/g;
208         $newContents =~ s/\bDOMStringImpl\b/StringImpl/g;
209         $newContents =~ s/\#define String String/#define DOMString String/;
210
211         # fix other identifiers that would collide otherwise
212         for my $from (keys %otherRenames) {
213             my $to = $otherRenames{$from};
214             $newContents =~ s/\b$from\b/$to/g;
215             $newContents =~ s/"$to"/"$from"/g;
216         }
217         $newContents =~ s/\#\#Event_/\#\#Event/g;
218     }
219
220     # remove bogus friend declaration
221     $newContents =~ s/friend class HTMLFormElement;\s*\n\s*(friend class HTMLFormCollection)/$1/;
222
223     # fix binding code generation
224     if ($file =~ /\.pm$/) {
225         $newContents =~ s/interfaceName \. "Impl"/interfaceName/g;
226         $newContents =~ s/{interfaceName}Impl/{interfaceName}/g;
227         $newContents =~ s/{type}Impl/{type}/g;
228         $newContents =~ s/html_documentimpl/HTMLDocument/g;
229     }
230
231     # fix words with Impl suffix
232     $newContents =~ s/\b(\w+)Impl\b/$exceptions{$1} ? "$1Impl" : $1/ge;
233
234     # fix typo that annoys me
235     $newContents =~ s/it.s base class/its base class/;
236
237     if ($newContents ne $contents) {
238         open FILE, ">", $file or die;
239         print FILE $newContents;
240         close FILE;
241     }
242 }