1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// RUN: %check_clang_tidy %s cppcoreguidelines-slicing %t
class Base {
int i;
void f() {}
virtual void g() {}
};
class DerivedWithMemberVariables : public Base {
void f();
int j;
};
class TwiceDerivedWithNoMemberVariables : public DerivedWithMemberVariables {
void f();
};
class DerivedWithOverride : public Base {
void f();
void g() override {}
};
class TwiceDerivedWithNoOverride : public DerivedWithOverride {
void f();
};
void TakesBaseByValue(Base base);
DerivedWithMemberVariables ReturnsDerived();
void positivesWithMemberVariables() {
DerivedWithMemberVariables b;
Base a{b};
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state [cppcoreguidelines-slicing]
a = b;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state
TakesBaseByValue(b);
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state
TwiceDerivedWithNoMemberVariables c;
a = c;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'TwiceDerivedWithNoMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state
a = ReturnsDerived();
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state
}
void positivesWithOverride() {
DerivedWithOverride b;
Base a{b};
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: slicing object from type 'DerivedWithOverride' to 'Base' discards override 'g'
a = b;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithOverride' to 'Base' discards override 'g'
TakesBaseByValue(b);
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: slicing object from type 'DerivedWithOverride' to 'Base' discards override 'g'
TwiceDerivedWithNoOverride c;
a = c;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithOverride' to 'Base' discards override 'g'
}
void TakesBaseByReference(Base &base);
class DerivedThatAddsVirtualH : public Base {
virtual void h();
};
class DerivedThatOverridesH : public DerivedThatAddsVirtualH {
void h() override;
};
void negatives() {
// OK, simple copying from the same type.
Base a;
TakesBaseByValue(a);
DerivedWithMemberVariables b;
DerivedWithMemberVariables c{b};
b = c;
// OK, derived type does not have extra state.
TwiceDerivedWithNoMemberVariables d;
DerivedWithMemberVariables e{d};
e = d;
// OK, derived does not override any method.
TwiceDerivedWithNoOverride f;
DerivedWithOverride g{f};
g = f;
// OK, no copying.
TakesBaseByReference(d);
TakesBaseByReference(f);
// Derived type overrides methods, but these methods are not in the base type,
// so cannot be called accidentally. Right now this triggers, but we might
// want to allow it.
DerivedThatOverridesH h;
a = h;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedThatOverridesH' to 'Base' discards override 'h'
}
namespace PR31187 {
// Don't warn when calling constructor of base virtual class, from
// initialization list of derived class constructor.
struct BaseA {
virtual ~BaseA() {}
virtual void foo() {}
int i;
};
struct BaseB : virtual BaseA {
virtual void foo() {}
};
struct ClassWithVirtualBases : BaseB {
ClassWithVirtualBases(const BaseB& other) : BaseA(other), BaseB(other) {}
ClassWithVirtualBases(const ClassWithVirtualBases& other) : BaseA(other), BaseB(other) {}
};
}
|