1 // Copyright 2014 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 // Tests for chromium style checks for virtual/override/final specifiers on 6 // virtual methods. 7 8 // Note: This is not actual windows.h but the stub file in system/windows.h 9 #include <windows.h> 10 11 // Purposely use macros to test that the FixIt hints don't try to remove the 12 // macro body. 13 #define OVERRIDE override 14 #define FINAL final 15 16 // Base class can only use virtual. 17 class Base { 18 public: ~Base()19 virtual ~Base() {} 20 virtual void F() = 0; 21 }; 22 23 // Derived classes correctly use only override or final specifier. 24 class CorrectOverride : public Base { 25 public: ~CorrectOverride()26 ~CorrectOverride() OVERRIDE {} F()27 void F() OVERRIDE {} 28 }; 29 30 class CorrectFinal : public CorrectOverride { 31 public: ~CorrectFinal()32 ~CorrectFinal() FINAL {} F()33 void F() FINAL {} 34 }; 35 36 // No override on an overridden method should trigger a diagnostic. 37 class MissingOverride : public Base { 38 public: ~MissingOverride()39 ~MissingOverride() {} F()40 void F() {} 41 }; 42 43 // Redundant specifiers should trigger a diagnostic. 44 class VirtualAndOverride : public Base { 45 public: ~VirtualAndOverride()46 virtual ~VirtualAndOverride() OVERRIDE {} F()47 virtual void F() OVERRIDE {} 48 }; 49 50 class VirtualAndFinal : public Base { 51 public: ~VirtualAndFinal()52 virtual ~VirtualAndFinal() FINAL {} F()53 virtual void F() FINAL {} 54 }; 55 56 class VirtualAndOverrideFinal : public Base { 57 public: ~VirtualAndOverrideFinal()58 virtual ~VirtualAndOverrideFinal() OVERRIDE FINAL {} F()59 virtual void F() OVERRIDE FINAL {} 60 }; 61 62 class OverrideAndFinal : public Base { 63 public: ~OverrideAndFinal()64 ~OverrideAndFinal() OVERRIDE FINAL {} F()65 void F() OVERRIDE FINAL {} 66 }; 67 68 // Also warn on pure functions. 69 class CorrectPureVirtual : public Base { 70 virtual void F() = 0; 71 }; 72 73 class Pure : public Base { 74 void F() = 0; 75 }; 76 77 class PureOverride : public Base { 78 void F() override = 0; 79 }; 80 81 class PureVirtualOverride : public Base { 82 virtual void F() override = 0; 83 }; 84 85 // Test that the redundant virtual warning is suppressed when the virtual 86 // keyword comes from a macro in a system header. 87 class COMIsAwesome : public Base { 88 STDMETHOD(F)() override = 0; 89 }; 90 91 // Some tests that overrides in the testing namespace 92 // don't trigger warnings, except for testing::Test. 93 namespace testing { 94 95 class Test { 96 public: 97 virtual ~Test(); 98 virtual void SetUp(); 99 }; 100 101 class NotTest { 102 public: 103 virtual ~NotTest(); 104 virtual void SetUp(); 105 }; 106 107 } // namespace 108 109 class MyTest : public testing::Test { 110 public: 111 virtual ~MyTest(); 112 virtual void SetUp() override; 113 }; 114 115 class MyNotTest : public testing::NotTest { 116 public: 117 virtual ~MyNotTest(); 118 virtual void SetUp() override; 119 }; 120 121 class MacroBase { 122 public: 123 virtual void AddRef() = 0; Virtual()124 virtual void Virtual() {} 125 }; 126 127 class Sub : public MacroBase { 128 // Shouldn't warn. 129 END_COM_MAP() 130 SYSTEM_REDUNDANT1; 131 SYSTEM_REDUNDANT2; 132 }; 133