From fbd0176d6c33c784959b721c7637e543af7aea60 Mon Sep 17 00:00:00 2001 From: Cong Liu Date: Thu, 11 Feb 2016 16:03:27 +0000 Subject: [PATCH] Merge branch 'arcpatch-D16922' llvm-svn: 260532 --- .../clang-tidy/misc/VirtualNearMissCheck.cpp | 14 ++++-- .../clang-tidy/misc/VirtualNearMissCheck.h | 9 ++-- .../clang-tidy/misc-virtual-near-miss.cpp | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.cpp b/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.cpp index ddec9d91b973..043540666d34 100644 --- a/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.cpp @@ -249,11 +249,19 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) { if (EditDistance > 0 && EditDistance <= EditDistanceThreshold) { if (checkOverrideWithoutName(Context, BaseMD, DerivedMD)) { // A "virtual near miss" is found. - diag(DerivedMD->getLocStart(), - "method '%0' has a similar name and the same signature as " - "virtual method '%1'; did you mean to override it?") + auto Range = CharSourceRange::getTokenRange( + SourceRange(DerivedMD->getLocation())); + + bool ApplyFix = !BaseMD->isTemplateInstantiation() && + !DerivedMD->isTemplateInstantiation(); + auto Diag = + diag(DerivedMD->getLocStart(), + "method '%0' has a similar name and the same signature as " + "virtual method '%1'; did you mean to override it?") << DerivedMD->getQualifiedNameAsString() << BaseMD->getQualifiedNameAsString(); + if (ApplyFix) + Diag << FixItHint::CreateReplacement(Range, BaseMD->getName()); } } } diff --git a/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.h b/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.h index f4dad0f9af93..4e108f22ce34 100644 --- a/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.h +++ b/clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.h @@ -12,7 +12,6 @@ #include "../ClangTidy.h" #include -#include namespace clang { namespace tidy { @@ -46,12 +45,12 @@ private: bool isOverriddenByDerivedClass(const CXXMethodDecl *BaseMD, const CXXRecordDecl *DerivedRD); - /// key: the unique ID of a method; - /// value: whether the method is possible to be overridden. + /// Key: the unique ID of a method. + /// Value: whether the method is possible to be overridden. std::map PossibleMap; - /// key: - /// value: whether the base method is overridden by some method in the derived + /// Key: + /// Value: whether the base method is overridden by some method in the derived /// class. std::map, bool> OverriddenMap; diff --git a/clang-tools-extra/test/clang-tidy/misc-virtual-near-miss.cpp b/clang-tools-extra/test/clang-tidy/misc-virtual-near-miss.cpp index c3f2e6261d44..a6bf2221689e 100644 --- a/clang-tools-extra/test/clang-tidy/misc-virtual-near-miss.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-virtual-near-miss.cpp @@ -16,9 +16,11 @@ struct Derived : Base { // overriden by this class. virtual void funk(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Derived::funk' has a similar name and the same signature as virtual method 'Base::func'; did you mean to override it? [misc-virtual-near-miss] + // CHECK-FIXES: virtual void func(); void func2(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Derived::func2' has {{.*}} 'Base::func' + // CHECK-FIXES: void func(); void func22(); // Should not warn. @@ -26,12 +28,46 @@ struct Derived : Base { void fun(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Derived::fun' has {{.*}} 'Base::func' + // CHECK-FIXES: void func(); Derived &operator==(const Base &); // Should not warn: operators are ignored. virtual NoDefinedClass2 *f1(); // Should not crash: non-defined class return type is ignored. }; +template +struct TBase { + virtual void tfunc(T t); +}; + +template +struct TDerived : TBase { + virtual void tfunk(T t); + // Should not apply fix for template. + // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived::tfunk' has {{.*}} 'TBase::tfunc' + // CHECK-MESSAGES: :[[@LINE-3]]:3: warning: method 'TDerived::tfunk' has {{.*}} 'TBase::tfunc' + // CHECK-FIXES: virtual void tfunk(T t); +}; + +TDerived T1; +TDerived T2; + +// Should not fix macro definition +#define MACRO1 void funcM() +// CHECK-FIXES: #define MACRO1 void funcM() +#define MACRO2(m) void m() +// CHECK-FIXES: #define MACRO2(m) void m() + +struct DerivedMacro : Base { + MACRO1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'DerivedMacro::funcM' has {{.*}} 'Base::func' + // CHECK-FIXES: MACRO1; + + MACRO2(func3); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'DerivedMacro::func3' has {{.*}} 'Base::func' + // CHECK-FIXES: MACRO2(func); +}; + typedef Derived derived_type; class Father { @@ -58,32 +94,40 @@ public: virtual void func2(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::func2' has {{.*}} 'Father::func' + // CHECK-FIXES: virtual void func(); int methoe(int x, char **strs); // Should not warn: parameter types don't match. int methoe(int x); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::methoe' has {{.*}} 'Mother::method' + // CHECK-FIXES: int method(int x); void methof(int x, const char **strs); // Should not warn: return types don't match. int methoh(int x, const char **strs); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::methoh' has {{.*}} 'Mother::method' + // CHECK-FIXES: int method(int x, const char **strs); virtual Child *creat(int i); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::creat' has {{.*}} 'Father::create' + // CHECK-FIXES: virtual Child *create(int i); virtual Derived &&generat(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::generat' has {{.*}} 'Father::generate' + // CHECK-FIXES: virtual Derived &&generate(); int decaz(const char str[]); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::decaz' has {{.*}} 'Mother::decay' + // CHECK-FIXES: int decay(const char str[]); operator bool(); derived_type *canonica(derived_type D); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::canonica' has {{.*}} 'Father::canonical' + // CHECK-FIXES: derived_type *canonical(derived_type D); private: void funk(); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::funk' has {{.*}} 'Father::func' + // CHECK-FIXES: void func(); };