3 # Copyright (C) 2008 Apple Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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.
30 # This script attempts to find instances of a problem where the signatures
31 # of virtual methods fail to match because one is defined 'const', and another
32 # is not. For example:
33 # virtual void Base::doStuff() const;
34 # virtual void Derived::doStuff();
36 # The lack of 'const' on the derived class gives it a different signature, and
37 # it will therefore not be called when doStuff() is called on a derived object
38 # via a base class pointer.
40 # Limitations of this script:
41 # * It only works on things in the WebCore namespace
42 # * Not all templatized methods may be found correctly
43 # * It doesn't know anything about inheritance, or if methods are actually virtual
44 # * It has lots of false positives (should add a whitelist for known-good signatures,
45 # and specific methods)
48 # Added by Simon Fraser <simon.fraser@apple.com>
50 # Run the script like this:
51 # WebKitTools/Scripts/detect-mismatched-virtual-const WebKitBuild/Debug/WebCore.framework/WebCore
53 # Output consists of a series of warnings like this:
55 # Both const and non-const versions of bgColor():
56 # HTMLDocument::bgColor()
57 # HTMLBodyElement::bgColor() const
58 # HTMLTableElement::bgColor() const
59 # HTMLTableRowElement::bgColor() const
60 # HTMLTableCellElement::bgColor() const
64 no warnings qw /syntax/;
69 print "Looking for unmatched const methods in $file\n";
71 if (!open NM, "(nm '$file' | c++filt | sed 's/^/STDOUT:/') 2>&1 |") {
72 die "Could not open $file\n";
85 my $nestedAngleBrackets;
86 $nestedAngleBrackets = qr /
90 (??{ $nestedAngleBrackets })
97 (??{ $nestedAngleBrackets })?
98 (??{ $nestedParens }))
101 my %signature_map = ();
106 if ($line =~ m/ [tT] WebCore::(.+)$/) {
109 if ($method =~ /$bal/) {
111 my $const = $2 eq " const";
113 my $class = substr($method, 0, length($method) - length($signature) - ($const ? 6 : 0));
115 # print "line: $line\nclass: $class\nmethod: $method\nsignature: $signature\nconst: $const\n\n";
123 push @{$signature_map{$signature}}, \%method_info;
125 print "unmatched line $method\n\n"
132 for $sig (keys %signature_map) {
135 my @entries = @{$signature_map{$sig}};
136 # print "$#entries\n";
139 my $num_not_const = 0;
141 for $i (0 .. $#entries) {
142 my $entry = @entries[$i];
144 my $class = $entry->{'class'};
145 my $const = $entry->{'const'};
154 if ($#entries > 1 && $num_const > 0 && $num_not_const > 0) {
155 print "Both const and non-const versions of $sig:\n";
157 for $i (0 .. $#entries) {
158 my $entry = @entries[$i];
159 my $method = $entry->{'method'};