• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, predicate) \
6   inline thisType* to##thisType(argumentType* argumentName) {              \
7     if (!predicate)                                                        \
8       asm("int 3");                                                        \
9     return static_cast<thisType*>(argumentName);                           \
10   }                                                                        \
11   inline long long toInt(argumentType* argumentName) {                     \
12     return reinterpret_cast<long long>(argumentName);                      \
13   }
14 
15 #define LIKELY(x) x
16 
17 namespace blink {
18 
19 struct Base {};
20 struct Derived : public Base {};
21 
22 DEFINE_TYPE_CASTS(Derived, Base, object, true);
23 
F()24 void F() {
25   Base* basePtr = new Derived;
26   Derived* derivedPtr = toDerived(basePtr);
27   long long asInt = toInt(basePtr);
28   // 'derivedPtr' should be renamed: it's a reference to a declaration defined
29   // outside a macro invocation.
30   if (LIKELY(derivedPtr)) {
31     delete derivedPtr;
32   }
33 }
34 
35 #define CALL_METHOD_FROM_MACRO()           \
36   void callMethodFromMacro() { method(); } \
37   void pmethod() override {}
38 
39 struct WithMacroP {
pmethodblink::WithMacroP40   virtual void pmethod() {}
41 };
42 
43 struct WithMacro : public WithMacroP {
methodblink::WithMacro44   void method() {}
45   CALL_METHOD_FROM_MACRO();
46 };
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).
DEFINE_WITH_TOKEN_CONCATENATION2(foo,Bar1)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| -> |???|.
FieldsMacro()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 
fieldsMacroTest()85 int fieldsMacroTest() {
86   FieldsMacro fm;
87   fm.setFooBar(789);
88   return fm.fooBar() + fm.barBaz();
89 }
90 
91 }  // namespace blink
92