Home | History | Annotate | Download | only in tests
      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, the_object, true);
     23 
     24 void F() {
     25   Base* base_ptr = new Derived;
     26   Derived* derived_ptr = ToDerived(base_ptr);
     27   long long as_int = ToInt(base_ptr);
     28   // 'derivedPtr' should be renamed: it's a reference to a declaration defined
     29   // outside a macro invocation.
     30   if (LIKELY(derived_ptr)) {
     31     delete derived_ptr;
     32   }
     33 }
     34 
     35 #define CALL_METHOD_FROM_MACRO()           \
     36   void CallMethodFromMacro() { Method(); } \
     37   void Pmethod() override {}
     38 
     39 struct WithMacroP {
     40   virtual void Pmethod() {}
     41 };
     42 
     43 struct WithMacro : public WithMacroP {
     44   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).
     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 name) { m_##name = name; }
     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 
     91 }  // namespace blink
     92