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 | 5 // Identifiers in macros should never be rewritten, as the risk of things |
dcheng
2016/12/22 11:35:04
Let's update this comment =)
Łukasz Anforowicz
2016/12/22 17:59:30
Ooops :-) Done (I've just removed the comment - i
| |
6 // breaking is extremely high. | 6 // breaking is extremely high. |
7 | 7 |
8 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, predicate) \ | 8 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, predicate) \ |
9 inline thisType* to##thisType(argumentType* argumentName) { \ | 9 inline thisType* To##thisType(argumentType* argumentName) { \ |
10 if (!predicate) \ | 10 if (!predicate) \ |
11 asm("int 3"); \ | 11 asm("int 3"); \ |
12 return static_cast<thisType*>(argumentName); \ | 12 return static_cast<thisType*>(argumentName); \ |
13 } \ | 13 } \ |
14 inline long long ToInt(argumentType* argumentName) { \ | 14 inline long long ToInt(argumentType* argumentName) { \ |
15 return reinterpret_cast<long long>(argumentName); \ | 15 return reinterpret_cast<long long>(argumentName); \ |
16 } | 16 } |
17 | 17 |
18 #define LIKELY(x) x | 18 #define LIKELY(x) x |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 struct Base {}; | 22 struct Base {}; |
23 struct Derived : public Base {}; | 23 struct Derived : public Base {}; |
24 | 24 |
25 DEFINE_TYPE_CASTS(Derived, Base, object, true); | 25 DEFINE_TYPE_CASTS(Derived, Base, object, true); |
26 | 26 |
27 void F() { | 27 void F() { |
28 Base* base_ptr = new Derived; | 28 Base* base_ptr = new Derived; |
29 // 'toDerived' should not be renamed, since the definition lives inside | 29 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); | 30 long long as_int = ToInt(base_ptr); |
33 // 'derivedPtr' should be renamed: it's a reference to a declaration defined | 31 // 'derivedPtr' should be renamed: it's a reference to a declaration defined |
34 // outside a macro invocation. | 32 // outside a macro invocation. |
35 if (LIKELY(derived_ptr)) { | 33 if (LIKELY(derived_ptr)) { |
36 delete derived_ptr; | 34 delete derived_ptr; |
37 } | 35 } |
38 } | 36 } |
39 | 37 |
40 #define CALL_METHOD_FROM_MACRO() \ | 38 #define CALL_METHOD_FROM_MACRO() \ |
41 void CallMethodFromMacro() { Method(); } \ | 39 void CallMethodFromMacro() { Method(); } \ |
42 void Pmethod() override {} | 40 void Pmethod() override {} |
43 | 41 |
44 struct WithMacroP { | 42 struct WithMacroP { |
45 virtual void Pmethod() {} | 43 virtual void Pmethod() {} |
46 }; | 44 }; |
47 | 45 |
48 struct WithMacro : public WithMacroP { | 46 struct WithMacro : public WithMacroP { |
49 void Method() {} | 47 void Method() {} |
50 CALL_METHOD_FROM_MACRO(); | 48 CALL_METHOD_FROM_MACRO(); |
51 }; | 49 }; |
52 | 50 |
51 #define DEFINE_WITH_TOKEN_CONCATENATION2(arg1, arg2) \ | |
52 void arg1##arg2() {} | |
53 // We definitely don't want to rewrite |arg1| on the previous line into | |
54 // either |Arg1| or |Frg1| or |Brg1| or |Foo| or |Baz|. | |
55 | |
56 // We might or might not want to rewrite |foo|->|Foo| and |baz|->|Baz| below. | |
57 // The test below just spells out the current behavior of the tool (which one | |
58 // can argue is accidental). | |
59 DEFINE_WITH_TOKEN_CONCATENATION2(foo, Bar1) | |
60 DEFINE_WITH_TOKEN_CONCATENATION2(baz, Bar2) | |
61 | |
62 void TokenConcatenationTest2() { | |
63 // We might or might not want to rewrite |foo|->|Foo| and |baz|->|Baz| below. | |
64 // The test below just spells out the current behavior of the tool (which one | |
65 // can argue is accidental). | |
66 FooBar1(); | |
67 BazBar2(); | |
68 } | |
69 | |
53 } // namespace blink | 70 } // namespace blink |
OLD | NEW |