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 3 // found in the LICENSE file. 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) \ 9 inline thisType* to##thisType(argumentType* argumentName) { \ 10 if (!predicate) \ 11 asm("int 3"); \ 12 return static_cast<thisType*>(argumentName); \ 13 } \ 14 inline long long toInt(argumentType* argumentName) { \ 15 return reinterpret_cast<long long>(argumentName); \ 16 } 17 18 #define LIKELY(x) x 19 20 namespace blink { 21 22 struct Base {}; 23 struct Derived : public Base {}; 24 25 DEFINE_TYPE_CASTS(Derived, Base, object, true); 26 27 void F() { 28 Base* basePtr = new Derived; 29 // 'toDerived' should not be renamed, since the definition lives inside 30 // a macro invocation. 31 Derived* derivedPtr = toDerived(basePtr); 32 long long asInt = toInt(basePtr); 33 // 'derivedPtr' should be renamed: it's a reference to a declaration defined 34 // outside a macro invocation. 35 if (LIKELY(derivedPtr)) { 36 delete derivedPtr; 37 } 38 } 39 40 #define CALL_METHOD_FROM_MACRO() \ 41 void callMethodFromMacro() { method(); } \ 42 void pmethod() override {} 43 44 struct WithMacroP { 45 virtual void pmethod() {} 46 }; 47 48 struct WithMacro : public WithMacroP { 49 void method() {} 50 CALL_METHOD_FROM_MACRO(); 51 }; 52 53 } // namespace blink 54