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++() { return *this; }
47 
48   // Conversion functions should not be rewritten.
operator int() const49   explicit operator int() const { return 42; }
50 
51   // These are special functions that we don't rename so that range-based
52   // for loops and STL things work.
begin()53   MyIterator begin() { return {}; }
end()54   my_iterator end() { return {}; }
rbegin()55   my_iterator rbegin() { return {}; }
rend()56   MyIterator rend() { return {}; }
57   // The trace() method is used by Oilpan, but we plan to tweak the Oilpan's
58   // clang plugin, so that it recognizes the new method name.
Trace()59   void Trace() {}
60   // These are used by std::unique_lock and std::lock_guard.
lock()61   void lock() {}
unlock()62   void unlock() {}
try_lock()63   void try_lock() {}
64 };
65 
66 class Other {
67   // Static begin/end/trace don't count, and should be renamed.
Begin()68   static MyIterator Begin() { return {}; }
End()69   static my_iterator End() { return {}; }
Trace()70   static void Trace() {}
Lock()71   static void Lock() {}
72 };
73 
74 // Test that the actual method definition is also updated.
DoTheWork()75 void Task::DoTheWork() {
76   ReallyDoTheWork();
77 }
78 
79 template <typename T>
80 class Testable {
81  public:
82   typedef T Testable::*UnspecifiedBoolType;
83   // This method has a reference to a member in a "member context" and a
84   // "non-member context" to verify both are rewritten.
operator UnspecifiedBoolType()85   operator UnspecifiedBoolType() { return ptr_ ? &Testable::ptr_ : 0; }
86 
87  private:
88   int ptr_;
89 };
90 
91 namespace subname {
92 
93 class SubnameParent {
SubnameMethod()94   virtual void SubnameMethod() {}
95 };
96 
97 }  // namespace subname
98 
99 class SubnameChild : public subname::SubnameParent {
100   // This subclasses from blink::subname::SubnameParent and should be renamed.
SubnameMethod()101   void SubnameMethod() override {}
102 };
103 
104 class GenChild : public blink::GenClass {
105   // This subclasses from the blink namespace but in the gen directory so it
106   // should not be renamed.
genMethod()107   void genMethod() override {}
108 };
109 
110 }  // namespace blink
111 
112 // Test that overrides from outside the Blink namespace are also updated.
113 class BovineTask : public blink::Task {
114  public:
115   using Task::DoTheWork;
116   void ReallyDoTheWork() override;
117 };
118 
119 class SuperBovineTask : public BovineTask {
120  public:
121   using BovineTask::ReallyDoTheWork;
122 };
123 
ReallyDoTheWork()124 void BovineTask::ReallyDoTheWork() {
125   DoTheWork();
126   // Calls via an overridden method should also be updated.
127   ReallyDoTheWork();
128 }
129 
130 // Finally, test that method pointers are also updated.
F()131 void F() {
132   void (blink::Task::*p1)() = &blink::Task::DoTheWork;
133   void (blink::Task::*p2)() = &BovineTask::DoTheWork;
134   void (blink::Task::*p3)() = &blink::Task::ReallyDoTheWork;
135   void (BovineTask::*p4)() = &BovineTask::ReallyDoTheWork;
136 }
137 
G()138 bool G() {
139   // Use the Testable class to rewrite the method.
140   blink::Testable<int> tt;
141   return tt;
142 }
143 
144 class SubclassOfInsideOfBlink : public blink::InsideOfBlink {
145  public:
146   // This function overrides something outside of blink so don't rename it.
nonBlinkVirtual()147   void nonBlinkVirtual() override {}
148   // This function overrides something in blink so rename it.
BlinkVirtual()149   void BlinkVirtual() override {}
150 };
151 
152 class TestSubclassInsideOfBlink : public SubclassOfInsideOfBlink {
153  public:
154  public:
155   // This function overrides something outside of blink so don't rename it.
nonBlinkVirtual()156   void nonBlinkVirtual() override {}
157   // This function overrides something in blink so rename it.
BlinkVirtual()158   void BlinkVirtual() override {}
159 };
160 
161 namespace blink {
162 
163 struct StructInBlink {
164   // Structs in blink should rename their methods to capitals.
Functionblink::StructInBlink165   bool Function() { return true; }
166 };
167 
168 class BitVector {
169  public:
170   class OutOfLineBits {};
171   enum Foo { kBlah };
172   struct Bar {};
173   class Baz {};
174   class FooBar {};
175 
176   // Should be renamed to GetReadyState, because of
177   // ShouldPrefixFunctionName heuristic.
GetReadyState()178   int GetReadyState() { return 123; }
179 
180   template <typename T>
181   class MyRefPtr {};
182 
183   // Naive renaming will break the build, by leaving return type the same
184   // as the method name - to avoid this "Get" prefix needs to be prepended
185   // as suggested in https://crbug.com/582312#c17.
GetOutOfLineBits() const186   const OutOfLineBits* GetOutOfLineBits() const { return nullptr; }
GetFoo()187   Foo GetFoo() { return kBlah; }
GetBar() const188   const Bar& GetBar() const { return bar_; }
GetBaz()189   MyRefPtr<Baz> GetBaz() { return MyRefPtr<Baz>(); }
GetFooBar()190   const MyRefPtr<FooBar>& GetFooBar() { return foobar_; }
191 
192  private:
193   Bar bar_;
194   MyRefPtr<FooBar> foobar_;
195 };
196 
197 namespace get_prefix_vs_inheritance {
198 
199 // Regression test for https://crbug.com/673031:
200 // 1. |frame| accessor/method should be renamed in the same way for
201 //    WebFrameImplBase and WebLocalFrameImpl.
202 // 2. Need to rename |frame| to |GetFrame| (not to |Frame|) to avoid
203 //    a conflict with the Frame type.
204 
205 class FrameFoo {};
206 class LocalFrame : public FrameFoo {};
207 
208 class WebFrameImplBase {
209  public:
210   // Using |frameFoo| to test inheritance, and NOT just the presence on the
211   // ShouldPrefixFunctionName list.
212   virtual FrameFoo* GetFrameFoo() const = 0;
213 };
214 
215 class WebLocalFrameImpl : public WebFrameImplBase {
216  public:
GetFrameFoo() const217   LocalFrame* GetFrameFoo() const override { return nullptr; }
218 };
219 
220 // This is also a regression test for https://crbug.com/673031.  We should NOT
221 // rewrite in a non-virtual case, because walking the inheritance chain of the
222 // return type depends too much on unrelated context (i.e. walking the
223 // inheritance chain might not be possible if the return type is
224 // forward-declared).
225 class LayoutObjectFoo {};
226 class LayoutBoxModelObject : public LayoutObjectFoo {};
227 class PaintLayerStackingNode {
228  public:
229   // |layoutObjectFoo| should NOT be renamed to |GetLayoutObjectFoo| (just to
230   // |LayoutObjectFoo|) - see the big comment above.  We use layoutObject*Foo*
231   // to test inheritance-related behavior and avoid testing whether method name
232   // is covered via ShouldPrefixFunctionName.
LayoutObjectFoo()233   LayoutBoxModelObject* LayoutObjectFoo() { return nullptr; }
234 };
235 
236 }  // namespace get_prefix_vs_inheritance
237 
238 namespace blacklisting_of_method_and_function_names {
239 
240 class Foo {
241   // Expecting |swap| method to be renamed to |Swap| - we blacklist renaming of
242   // |swap| *function*, because it needs to have the same casing as std::swap,
243   // so that ADL can kick-in and pull it from another namespace depending on the
244   // bargument.  We have a choice to rename or not rename |swap| *methods* - we
245   // chose to rename to be consistent (i.e. we rename |clear| -> |Clear|) and
246   // because Google C++ Styke Guide uses "Swap" in examples.
Swap()247   void Swap() {}
Swap(Foo & x,Foo & y)248   static void Swap(Foo& x, Foo& y) {}
249 
250   // We don't rename |begin|, so that <algorithms> and other templates that
251   // expect |begin|, |end|, etc. continue to work.  This is only necessary
252   // for instance methods - renaming static methods and funcitons is okay.
begin()253   void begin() {}
Begin(int x)254   static void Begin(int x) {}
255 
256   // https://crbug.com672902: std-like names should not be rewritten.
emplace_back(int x)257   void emplace_back(int x) {}
insert(int x)258   void insert(int x) {}
push_back(int x)259   void push_back(int x) {}
back()260   int* back() { return nullptr; }
front()261   int* front() { return nullptr; }
erase()262   void erase() {}
empty()263   bool empty() { return true; }
264 };
265 
Begin(int x)266 void Begin(int x) {}
swap(Foo & x,Foo & y)267 void swap(Foo& x, Foo& y) {}
268 
269 }  // blacklisting_of_method_and_function_names
270 
271 }  // namespace blink
272 
273 namespace WTF {
274 
275 struct StructInWTF {
276   // Structs in WTF should rename their methods to capitals.
FunctionWTF::StructInWTF277   bool Function() { return true; }
278 };
279 
280 }  // namespace WTF
281 
F2()282 void F2() {
283   blink::StructInBlink b;
284   b.Function();
285   WTF::StructInWTF w;
286   w.Function();
287 }
288 
289 namespace blink {
290 
291 class ClassDeclaredInsideBlink {
292  public:
293   static void MethodDefinedOutsideBlink();
294 };
295 
296 namespace internal {
297 
298 class InternalClass {
299  public:
300   static void Method();
301 };
302 
303 }  // namespace internal
304 
305 }  // namespace blink
306 
307 // https://crbug.com/640688 - need to rewrite method name below.
MethodDefinedOutsideBlink()308 void blink::ClassDeclaredInsideBlink::MethodDefinedOutsideBlink() {}
Method()309 void blink::internal::InternalClass::Method() {}
310