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 namespace blink {
6 
7 // Note: do not add any copy or move constructors to this class: doing so will
8 // break test coverage that we don't clobber the class name by trying to emit
9 // replacements for synthesized functions.
10 class C {
11  public:
12   // Make sure initializers are updated to use the new names.
C()13   C()
14       : flag_field_(~0),
15         field_mentioning_http_and_https_(1),
16         should_rename_(0) {}
17 
Method()18   int Method() {
19     // Test that references to fields are updated correctly.
20     return instance_count_ + flag_field_ + field_mentioning_http_and_https_;
21   }
22 
23   // Test that a field without a m_ prefix is correctly renamed.
24   static int instance_count_;
25 
26  protected:
27   // Test that a field with a m_ prefix is correctly renamed.
28   const int flag_field_;
29   // Statics should be named with s_, but make sure s_ and m_ are both correctly
30   // stripped.
31   static int static_count_;
32   static int static_count_with_bad_name_;
33   // Make sure that acronyms don't confuse the underscore inserter.
34   int field_mentioning_http_and_https_;
35   // Already Google style, should not change.
36   int already_google_style_;
37 
38   union {
39     // Anonymous union members should be renamed, as should contructor
40     // initializers of them.
41     char* should_rename_;
42     int* does_rename_;
43   };
44 };
45 
46 struct Derived : public C {
47   using C::flag_field_;
48   using C::field_mentioning_http_and_https_;
49 };
50 
51 int C::instance_count_ = 0;
52 
53 // Structs are like classes.
54 struct S {
55   int integer_field_;
56   int wants_rename;
57   int google_style_already;
58 };
59 
60 // Unions also use struct-style naming.
61 union U {
62   char four_chars[4];
63   short two_shorts[2];
64   int one_hopefully_four_byte_int;
65   int has_prefix_;
66 };
67 
68 // https://crbug.com/640749#c1: Some type traits are inside blink namespace.
69 struct IsGarbageCollectedMixin {
70   static const bool value = true;
71   static const bool safe_to_compare_to_empty_or_deleted = false;
72 };
73 
74 }  // namespace blink
75 
76 namespace not_blink {
77 
78 // These are traits for WTF types that may be defined outside of blink such
79 // as in mojo. But their names are unique so we can globally treat them as
80 // type traits for renaming.
81 struct GloballyKnownTraits {
82   static const bool safe_to_compare_to_empty_or_deleted = false;
83 };
84 
85 }  // namespace not_blink
86 
87 namespace WTF {
88 
TestForTraits()89 void TestForTraits() {
90   bool a = blink::IsGarbageCollectedMixin::safe_to_compare_to_empty_or_deleted;
91   bool b = not_blink::GloballyKnownTraits::safe_to_compare_to_empty_or_deleted;
92 }
93 
94 // We don't want to capitalize fields in type traits
95 // (i.e. the |value| -> |kValue| rename is undesirable below).
96 struct TypeTrait1 {
97   static const bool value = true;
98 };
99 
100 // Some type traits are implemented as classes, not structs
101 // (e.g. WTF::IsGarbageCollectedType or WTF::IsAssignable).
102 // We should not perform a |value| -> |kValue| rename in the type trait below.
103 template <typename T>
104 class TypeTrait2 {
105  public:
106   static const bool value = false;
107 };
108 template <>
109 class TypeTrait2<void> {
110  public:
111   static const bool value = false;
112 };
113 
114 // Some type traits have static methods.  We should not perform
115 // a |value| -> |kValue| rename in the type trait below.
116 template <typename T, typename U>
117 struct IsSubclass {
118  private:
119   typedef char YesType;
120   struct NoType {
121     char padding[8];
122   };
123 
124   static YesType SubclassCheck(U*);
125   static NoType SubclassCheck(...);
126   static T* t_;
127 
128  public:
129   static const bool value = sizeof(SubclassCheck(t_)) == sizeof(YesType);
130 };
131 
132 // Some type traits have deleted instance methods.  We should not perform
133 // a |value| -> |kValue| rename in the type trait below.
134 template <typename U = void>
135 struct IsTraceableInCollection {
136   // Expanded from STATIC_ONLY(IsTraceableInCollection) macro:
137  private:
138   IsTraceableInCollection() = delete;
139   IsTraceableInCollection(const IsTraceableInCollection&) = delete;
140   IsTraceableInCollection& operator=(const IsTraceableInCollection&) = delete;
141   void* operator new(unsigned long) = delete;
142   void* operator new(unsigned long, void*) = delete;
143 
144  public:
145   static const bool value = true;
146 };
147 
148 // Some type traits have a non-boolean value.
149 enum LifetimeManagementType {
150   kRefCountedLifetime,
151   kGarbageCollectedLifetime,
152 };
153 template <typename T>
154 struct LifetimeOf {
155  private:
156   // Okay to rename |isGarbageCollected| to |kIsGarbageCollected|.
157   static const bool kIsGarbageCollected = true;
158 
159  public:
160   // Expecting no rename of |value|.
161   static const LifetimeManagementType value =
162       !kIsGarbageCollected ? kRefCountedLifetime : kGarbageCollectedLifetime;
163 };
164 
165 template <typename T>
166 struct GenericHashTraitsBase {
167   // We don't want to capitalize fields in type traits
168   // (i.e. the |value| -> |kValue| rename is undesirable below).
169   // This problem is prevented by IsCallee heuristic.
170   static const int kWeakHandlingFlag = TypeTrait2<T>::value ? 123 : 456;
171 };
172 
173 template <int Format>
174 struct IntermediateFormat {
175   // Some type traits have int type.  Example below is loosely based on
176   // third_party/WebKit/Source/platform/graphics/gpu/WebGLImageConversion.cpp
177   static const int value = (Format == 123) ? 456 : 789;
178 };
179 
180 };  // namespace WTF
181 
F()182 void F() {
183   // Test that references to a static field are correctly rewritten.
184   blink::C::instance_count_++;
185   // Force instantiation of a copy constructor for blink::C to make sure field
186   // initializers for synthesized functions don't cause weird rewrites.
187   blink::C c;
188   blink::C c2 = c;
189 
190   bool b1 = WTF::TypeTrait1::value;
191   bool b2 = WTF::TypeTrait2<void>::value;
192 }
193