1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "gen/thing.h"
6 
7 namespace v8 {
8 
9 class InterfaceOutsideOfBlink {
10  public:
11   virtual void nonBlinkVirtual() = 0;
12 };
13 
14 }  // namespace v8
15 
16 namespace blink {
17 
18 class InsideOfBlink : public v8::InterfaceOutsideOfBlink {
19  public:
20   // This function overrides something outside of blink so don't rename it.
nonBlinkVirtual()21   void nonBlinkVirtual() override {}
22   // This function is in blink so rename it.
blinkVirtual()23   virtual void blinkVirtual() {}
24 };
25 
26 class MyIterator {};
27 using my_iterator = char*;
28 
29 class Task {
30  public:
31   // Already style-compliant methods shouldn't change.
OutputDebugString()32   void OutputDebugString() {}
33 
34   // Tests that the declarations for methods are updated.
35   void doTheWork();
36   // Overload to test using declarations that introduce multiple shadow
37   // declarations.
38   void doTheWork(int);
39   virtual void reallyDoTheWork() = 0;
40 
41   // Note: this is purposely copyable and assignable, to make sure the Clang
42   // tool doesn't try to emit replacements for things that aren't explicitly
43   // written.
44 
45   // Overloaded operators should not be rewritten.
operator ++()46   Task& operator++() {
47     return *this;
48   }
49 
50   // Conversion functions should not be rewritten.
operator int() const51   explicit operator int() const {
52     return 42;
53   }
54 
55   // These are special functions that we don't rename so that range-based
56   // for loops and STL things work.
begin()57   MyIterator begin() {}
end()58   my_iterator end() {}
rbegin()59   my_iterator rbegin() {}
rend()60   MyIterator rend() {}
61   // The trace() method is used by Oilpan, we shouldn't rename it.
trace()62   void trace() {}
63   // These are used by std::unique_lock and std::lock_guard.
lock()64   void lock() {}
unlock()65   void unlock() {}
try_lock()66   void try_lock() {}
67 };
68 
69 class Other {
70   // Static begin/end/trace don't count, and should be renamed.
begin()71   static MyIterator begin() {}
end()72   static my_iterator end() {}
trace()73   static void trace() {}
lock()74   static void lock() {}
75 };
76 
77 class NonIterators {
78   // begin()/end() and friends are renamed if they don't return an iterator.
begin()79   void begin() {}
end()80   int end() { return 0; }
rbegin()81   void rbegin() {}
rend()82   int rend() { return 0; }
83 };
84 
85 // Test that the actual method definition is also updated.
doTheWork()86 void Task::doTheWork() {
87   reallyDoTheWork();
88 }
89 
90 template <typename T>
91 class Testable {
92  public:
93   typedef T Testable::*UnspecifiedBoolType;
94   // This method has a reference to a member in a "member context" and a
95   // "non-member context" to verify both are rewritten.
operator UnspecifiedBoolType()96   operator UnspecifiedBoolType() { return m_ptr ? &Testable::m_ptr : 0; }
97 
98  private:
99   int m_ptr;
100 };
101 
102 namespace subname {
103 
104 class SubnameParent {
subnameMethod()105   virtual void subnameMethod() {}
106 };
107 
108 }  // namespace subname
109 
110 class SubnameChild : public subname::SubnameParent {
111   // This subclasses from blink::subname::SubnameParent and should be renamed.
subnameMethod()112   void subnameMethod() override {}
113 };
114 
115 class GenChild : public blink::GenClass {
116   // This subclasses from the blink namespace but in the gen directory so it
117   // should not be renamed.
genMethod()118   void genMethod() override {}
119 };
120 
121 }  // namespace blink
122 
123 // Test that overrides from outside the Blink namespace are also updated.
124 class BovineTask : public blink::Task {
125  public:
126   using Task::doTheWork;
127   void reallyDoTheWork() override;
128 };
129 
130 class SuperBovineTask : public BovineTask {
131  public:
132   using BovineTask::reallyDoTheWork;
133 };
134 
reallyDoTheWork()135 void BovineTask::reallyDoTheWork() {
136   doTheWork();
137   // Calls via an overridden method should also be updated.
138   reallyDoTheWork();
139 }
140 
141 // Finally, test that method pointers are also updated.
F()142 void F() {
143   void (blink::Task::*p1)() = &blink::Task::doTheWork;
144   void (blink::Task::*p2)() = &BovineTask::doTheWork;
145   void (blink::Task::*p3)() = &blink::Task::reallyDoTheWork;
146   void (BovineTask::*p4)() = &BovineTask::reallyDoTheWork;
147 }
148 
G()149 bool G() {
150   // Use the Testable class to rewrite the method.
151   blink::Testable<int> tt;
152   return tt;
153 }
154 
155 class SubclassOfInsideOfBlink : public blink::InsideOfBlink {
156  public:
157   // This function overrides something outside of blink so don't rename it.
nonBlinkVirtual()158   void nonBlinkVirtual() override {}
159   // This function overrides something in blink so rename it.
blinkVirtual()160   void blinkVirtual() override {}
161 };
162 
163 class TestSubclassInsideOfBlink : public SubclassOfInsideOfBlink {
164  public:
165  public:
166   // This function overrides something outside of blink so don't rename it.
nonBlinkVirtual()167   void nonBlinkVirtual() override {}
168   // This function overrides something in blink so rename it.
blinkVirtual()169   void blinkVirtual() override {}
170 };
171 
172 namespace blink {
173 
174 struct StructInBlink {
175   // Structs in blink should rename their methods to capitals.
functionblink::StructInBlink176   bool function() { return true; }
177 };
178 
179 class BitVector {
180  public:
181   class OutOfLineBits {};
182   enum Foo { Blah };
183   struct Bar {};
184   class Baz {};
185   class FooBar {};
186 
187   template <typename T>
188   class MyRefPtr {};
189 
190   // Naive renaming will break the build, by leaving return type the same
191   // as the method name - to avoid this "Get" prefix needs to be prepended
192   // as suggested in https://crbug.com/582312#c17.
outOfLineBits() const193   const OutOfLineBits* outOfLineBits() const { return nullptr; }
foo()194   Foo foo() { return Blah; }
bar() const195   const Bar& bar() const { return m_bar; }
baz()196   MyRefPtr<Baz> baz() { return MyRefPtr<Baz>(); }
fooBar()197   const MyRefPtr<FooBar>& fooBar() { return foobar_; }
198 
199  private:
200   Bar m_bar;
201   MyRefPtr<FooBar> foobar_;
202 };
203 
204 }  // namespace blink
205 
206 namespace WTF {
207 
208 struct StructInWTF {
209   // Structs in WTF should rename their methods to capitals.
functionWTF::StructInWTF210   bool function() { return true; }
211 };
212 
213 }  // namespace WTF
214 
F2()215 void F2() {
216   blink::StructInBlink b;
217   b.function();
218   WTF::StructInWTF w;
219   w.function();
220 }
221 
222 namespace blink {
223 
224 class ClassDeclaredInsideBlink {
225  public:
226   static void methodDefinedOutsideBlink();
227 };
228 
229 namespace internal {
230 
231 class InternalClass {
232  public:
233   static void method();
234 };
235 
236 }  // namespace internal
237 
238 }  // namespace blink
239 
240 // https://crbug.com/640688 - need to rewrite method name below.
methodDefinedOutsideBlink()241 void blink::ClassDeclaredInsideBlink::methodDefinedOutsideBlink() {}
method()242 void blink::internal::InternalClass::method() {}
243