OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Identifiers in macros should never be rewritten, as the risk of things | |
6 // breaking is extremely high. | |
7 | |
8 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, predicate) \ | 5 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, predicate) \ |
9 inline thisType* to##thisType(argumentType* argumentName) { \ | 6 inline thisType* To##thisType(argumentType* argumentName) { \ |
10 if (!predicate) \ | 7 if (!predicate) \ |
11 asm("int 3"); \ | 8 asm("int 3"); \ |
12 return static_cast<thisType*>(argumentName); \ | 9 return static_cast<thisType*>(argumentName); \ |
13 } \ | 10 } \ |
14 inline long long ToInt(argumentType* argumentName) { \ | 11 inline long long ToInt(argumentType* argumentName) { \ |
15 return reinterpret_cast<long long>(argumentName); \ | 12 return reinterpret_cast<long long>(argumentName); \ |
16 } | 13 } |
17 | 14 |
18 #define LIKELY(x) x | 15 #define LIKELY(x) x |
19 | 16 |
20 namespace blink { | 17 namespace blink { |
21 | 18 |
22 struct Base {}; | 19 struct Base {}; |
23 struct Derived : public Base {}; | 20 struct Derived : public Base {}; |
24 | 21 |
25 DEFINE_TYPE_CASTS(Derived, Base, object, true); | 22 DEFINE_TYPE_CASTS(Derived, Base, object, true); |
26 | 23 |
27 void F() { | 24 void F() { |
28 Base* base_ptr = new Derived; | 25 Base* base_ptr = new Derived; |
29 // 'toDerived' should not be renamed, since the definition lives inside | 26 Derived* derived_ptr = ToDerived(base_ptr); |
30 // a macro invocation. | |
31 Derived* derived_ptr = toDerived(base_ptr); | |
32 long long as_int = ToInt(base_ptr); | 27 long long as_int = ToInt(base_ptr); |
33 // 'derivedPtr' should be renamed: it's a reference to a declaration defined | 28 // 'derivedPtr' should be renamed: it's a reference to a declaration defined |
34 // outside a macro invocation. | 29 // outside a macro invocation. |
35 if (LIKELY(derived_ptr)) { | 30 if (LIKELY(derived_ptr)) { |
36 delete derived_ptr; | 31 delete derived_ptr; |
37 } | 32 } |
38 } | 33 } |
39 | 34 |
40 #define CALL_METHOD_FROM_MACRO() \ | 35 #define CALL_METHOD_FROM_MACRO() \ |
41 void CallMethodFromMacro() { Method(); } \ | 36 void CallMethodFromMacro() { Method(); } \ |
42 void Pmethod() override {} | 37 void Pmethod() override {} |
43 | 38 |
44 struct WithMacroP { | 39 struct WithMacroP { |
45 virtual void Pmethod() {} | 40 virtual void Pmethod() {} |
46 }; | 41 }; |
47 | 42 |
48 struct WithMacro : public WithMacroP { | 43 struct WithMacro : public WithMacroP { |
49 void Method() {} | 44 void Method() {} |
50 CALL_METHOD_FROM_MACRO(); | 45 CALL_METHOD_FROM_MACRO(); |
51 }; | 46 }; |
52 | 47 |
| 48 #define DEFINE_WITH_TOKEN_CONCATENATION2(arg1, arg2) \ |
| 49 void arg1##arg2() {} |
| 50 // We definitely don't want to rewrite |arg1| on the previous line into |
| 51 // either |Arg1| or |Frg1| or |Brg1| or |Foo| or |Baz|. |
| 52 |
| 53 // We might or might not want to rewrite |foo|->|Foo| and |baz|->|Baz| below. |
| 54 // The test below just spells out the current behavior of the tool (which one |
| 55 // can argue is accidental). |
| 56 DEFINE_WITH_TOKEN_CONCATENATION2(foo, Bar1) |
| 57 DEFINE_WITH_TOKEN_CONCATENATION2(baz, Bar2) |
| 58 |
| 59 void TokenConcatenationTest2() { |
| 60 // We might or might not want to rewrite |foo|->|Foo| and |baz|->|Baz| below. |
| 61 // The test below just spells out the current behavior of the tool (which one |
| 62 // can argue is accidental). |
| 63 fooBar1(); |
| 64 bazBar2(); |
| 65 } |
| 66 |
| 67 class FieldsMacro { |
| 68 public: |
| 69 // We shouldn't rewrite |m_fooBar| -> |foo_bar_|, because we cannot rewrite |
| 70 // |m_##name| -> |???|. |
| 71 FieldsMacro() : m_fooBar(123), m_barBaz(456) {} |
| 72 |
| 73 #define DECLARE_FIELD(name, Name) \ |
| 74 private: \ |
| 75 int m_##name; \ |
| 76 \ |
| 77 public: \ |
| 78 int name() { return m_##name; } \ |
| 79 void Set##Name(int value) { m_##name = value; } |
| 80 |
| 81 DECLARE_FIELD(FooBar, FooBar) |
| 82 DECLARE_FIELD(BarBaz, BarBaz) |
| 83 }; |
| 84 |
| 85 int FieldsMacroTest() { |
| 86 FieldsMacro fm; |
| 87 fm.SetFooBar(789); |
| 88 return fm.FooBar() + fm.BarBaz(); |
| 89 } |
| 90 |
53 } // namespace blink | 91 } // namespace blink |
OLD | NEW |