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