OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef VIRTUAL_METHODS_H_ | 5 #ifndef VIRTUAL_METHODS_H_ |
6 #define VIRTUAL_METHODS_H_ | 6 #define VIRTUAL_METHODS_H_ |
7 | 7 |
8 // Note: This is not actual windows.h but the stub file in system/windows.h | 8 // Note: This is not actual windows.h but the stub file in system/windows.h |
9 #include <windows.h> | 9 #include <windows.h> |
10 | 10 |
11 #define CR_BEGIN_MSG_MAP_EX(theClass) virtual int f() { return 4; } | 11 struct NonTrivial { |
12 #define BEGIN_SAFE_MSG_MAP_EX(theClass) virtual int g() { return 4; } | 12 NonTrivial(); |
13 | 13 ~NonTrivial(); |
14 // Should warn about virtual method usage. | |
15 class VirtualMethodsInHeaders { | |
16 public: | |
17 // Don't complain about these. | |
18 virtual void MethodIsAbstract() = 0; | |
19 virtual void MethodHasNoArguments(); | |
20 virtual void MethodHasEmptyDefaultImpl() {} | |
21 | |
22 // But complain about this: | |
23 virtual bool ComplainAboutThis() { return true; } | |
24 | |
25 SYSTEM_INLINE_VIRTUAL | |
26 CR_BEGIN_MSG_MAP_EX(Sub) | |
27 BEGIN_SAFE_MSG_MAP_EX(Sub) | |
28 }; | 14 }; |
29 | 15 |
30 // Complain on missing 'virtual' keyword in overrides. | 16 // Should warn about inline constructors with too many inline virtual methods. |
31 class WarnOnMissingVirtual : public VirtualMethodsInHeaders { | 17 class InlineVirtualMethodsInHeaders { |
32 public: | 18 public: |
33 void MethodHasNoArguments() override; | 19 // Complain about the inline/missing constructor. |
20 virtual void Method1() {} | |
21 virtual bool Method2() { return true; } | |
22 virtual void Method3() {} | |
23 virtual bool Method4() { return true; } | |
24 virtual void Method5() {} | |
34 }; | 25 }; |
35 | 26 |
36 // Don't complain about things in a 'testing' namespace. | 27 // Should warn about inline constructors with long inline virtual methods. |
37 namespace testing { | 28 class LongInlineVirtualMethods { |
38 struct TestStruct {}; | 29 public: |
39 } // namespace testing | 30 // Complain about the inline/missing constructor. |
31 virtual int LongMethod() { | |
32 int x = 1; | |
33 int y = x * x; | |
34 int w = 73; | |
35 int u = 1; | |
36 for (int z = 7; z < 8; ++z) { | |
37 if (z > x) { | |
38 return x + 1; | |
39 } else { | |
40 return z - 1; | |
41 } | |
42 } | |
43 return 1; | |
44 } | |
45 }; | |
40 | 46 |
41 class VirtualMethodsInHeadersTesting : public VirtualMethodsInHeaders { | 47 // Should not warn if enough virtual methods are out-of-line. |
dcheng
2016/03/24 17:37:25
Nit: I find this comment a bit confusing because t
| |
48 class OutoflineVirtualMethodsInHeaders { | |
42 public: | 49 public: |
43 // Don't complain about no virtual testing methods. | 50 // Complain about the inline/missing constructor. |
44 void MethodHasNoArguments(); | 51 virtual void Method1(); |
52 virtual bool Method2() { return true; } | |
53 virtual void Method3() {} | |
54 virtual bool Method4() { return true; } | |
55 virtual void Method5() {} | |
56 }; | |
57 | |
58 class InlineVirtualMethodsInAbstractClass { | |
59 public: | |
60 // Accept an inline constructor for a base class. | |
61 virtual void Abstract() = 0; | |
62 virtual void Method1() {} | |
63 virtual bool Method2() { return true; } | |
64 virtual void Method3() {} | |
65 virtual bool Method4() { return true; } | |
66 virtual void Method5() {} | |
67 }; | |
68 | |
69 class InlineVirtualMethodsWithProtectedConstructor { | |
70 protected: | |
71 // Accept an inline constructor for a base class. | |
72 InlineVirtualMethodsWithProtectedConstructor() {} | |
73 | |
74 public: | |
75 virtual void Method1() {} | |
76 virtual bool Method2() { return true; } | |
77 virtual void Method3() {} | |
78 virtual bool Method4() { return true; } | |
79 virtual void Method5() {} | |
80 }; | |
81 | |
82 // Complain for leaf classes even if they inherit their inline virtual methods. | |
83 class InlineVirtualMethodsInLeafClass | |
84 : public InlineVirtualMethodsInAbstractClass { | |
85 public: | |
86 void Abstract() override; | |
87 }; | |
88 | |
89 class DtorInlineVirtualMethodsInHeaders { | |
90 public: | |
91 DtorInlineVirtualMethodsInHeaders(); | |
92 // Complain about the inline destructor. | |
dcheng
2016/03/24 17:37:25
Nit: non-virtual inline
| |
93 ~DtorInlineVirtualMethodsInHeaders() {} | |
94 | |
95 virtual void Method1() {} | |
96 virtual bool Method2() { return true; } | |
97 virtual void Method3() {} | |
98 virtual bool Method4() { return true; } | |
99 virtual void Method5() {} | |
45 | 100 |
46 private: | 101 private: |
47 testing::TestStruct tester_; | 102 NonTrivial non_trivial; |
103 }; | |
104 | |
105 class VirtualDtorInlineVirtualMethodsInHeaders { | |
106 public: | |
107 VirtualDtorInlineVirtualMethodsInHeaders(); | |
108 // Don't complain about the inline virtual destructor. | |
109 virtual ~VirtualDtorInlineVirtualMethodsInHeaders() {} | |
110 | |
111 virtual void Method1() {} | |
112 virtual bool Method2() { return true; } | |
113 virtual void Method3() {} | |
114 virtual bool Method4() { return true; } | |
115 virtual void Method5() {} | |
116 | |
117 private: | |
118 NonTrivial non_trivial; | |
119 }; | |
120 | |
121 // Check for a bug in which the inline destructor for the most-derived class is | |
122 // re-counted for each base class whose destructor it overrides. | |
123 struct Base1 { | |
124 virtual ~Base1(); | |
125 virtual void Abstract() = 0; | |
126 }; | |
127 struct Base2 : Base1 {}; | |
128 struct Base3 : Base2 {}; | |
129 struct Base4 : Base3 {}; | |
130 struct Base5 : Base4 {}; | |
131 struct Base6 : Base5 {}; | |
132 struct Base7 : Base6 {}; | |
133 struct Base8 : Base7 {}; | |
134 struct Base9 : Base8 {}; | |
135 struct Derived : Base9 { | |
136 ~Derived() override {} | |
137 void Abstract() override; | |
48 }; | 138 }; |
49 | 139 |
50 #endif // VIRTUAL_METHODS_H_ | 140 #endif // VIRTUAL_METHODS_H_ |
OLD | NEW |