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. 13 C() 14 : flag_field_(~0), 15 field_mentioning_http_and_https_(1), 16 should_rename_(0) {} 17 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 }; 72 73 } // namespace blink 74 75 namespace WTF { 76 77 // We don't want to capitalize fields in type traits 78 // (i.e. the |value| -> |kValue| rename is undesirable below). 79 struct TypeTrait1 { 80 static const bool value = true; 81 }; 82 83 // Some type traits are implemented as classes, not structs 84 // (e.g. WTF::IsGarbageCollectedType or WTF::IsAssignable). 85 // We should not perform a |value| -> |kValue| rename in the type trait below. 86 template <typename T> 87 class TypeTrait2 { 88 public: 89 static const bool value = false; 90 }; 91 template <> 92 class TypeTrait2<void> { 93 public: 94 static const bool value = false; 95 }; 96 97 // Some type traits have static methods. We should not perform 98 // a |value| -> |kValue| rename in the type trait below. 99 template <typename T, typename U> 100 struct IsSubclass { 101 private: 102 typedef char YesType; 103 struct NoType { 104 char padding[8]; 105 }; 106 107 static YesType SubclassCheck(U*); 108 static NoType SubclassCheck(...); 109 static T* t_; 110 111 public: 112 static const bool value = sizeof(SubclassCheck(t_)) == sizeof(YesType); 113 }; 114 115 // Some type traits have deleted instance methods. We should not perform 116 // a |value| -> |kValue| rename in the type trait below. 117 template <typename U = void> 118 struct IsTraceableInCollection { 119 // Expanded from STATIC_ONLY(IsTraceableInCollection) macro: 120 private: 121 IsTraceableInCollection() = delete; 122 IsTraceableInCollection(const IsTraceableInCollection&) = delete; 123 IsTraceableInCollection& operator=(const IsTraceableInCollection&) = delete; 124 void* operator new(unsigned long) = delete; 125 void* operator new(unsigned long, void*) = delete; 126 127 public: 128 static const bool value = true; 129 }; 130 131 // Some type traits have a non-boolean value. 132 enum LifetimeManagementType { 133 kRefCountedLifetime, 134 kGarbageCollectedLifetime, 135 }; 136 template <typename T> 137 struct LifetimeOf { 138 private: 139 // Okay to rename |isGarbageCollected| to |kIsGarbageCollected|. 140 static const bool kIsGarbageCollected = true; 141 142 public: 143 // Expecting no rename of |value|. 144 static const LifetimeManagementType value = 145 !kIsGarbageCollected ? kRefCountedLifetime : kGarbageCollectedLifetime; 146 }; 147 148 template <typename T> 149 struct GenericHashTraitsBase { 150 // We don't want to capitalize fields in type traits 151 // (i.e. the |value| -> |kValue| rename is undesirable below). 152 // This problem is prevented by IsCallee heuristic. 153 static const int kWeakHandlingFlag = TypeTrait2<T>::value ? 123 : 456; 154 }; 155 156 template <int Format> 157 struct IntermediateFormat { 158 // Some type traits have int type. Example below is loosely based on 159 // third_party/WebKit/Source/platform/graphics/gpu/WebGLImageConversion.cpp 160 static const int value = (Format == 123) ? 456 : 789; 161 }; 162 163 }; // namespace WTF 164 165 void F() { 166 // Test that references to a static field are correctly rewritten. 167 blink::C::instance_count_++; 168 // Force instantiation of a copy constructor for blink::C to make sure field 169 // initializers for synthesized functions don't cause weird rewrites. 170 blink::C c; 171 blink::C c2 = c; 172 173 bool b1 = WTF::TypeTrait1::value; 174 bool b2 = WTF::TypeTrait2<void>::value; 175 } 176