Skip to content

Commit fbd0176

Browse files
committedFeb 11, 2016
Merge branch 'arcpatch-D16922'
llvm-svn: 260532
1 parent 227740d commit fbd0176

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed
 

‎clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,19 @@ void VirtualNearMissCheck::check(const MatchFinder::MatchResult &Result) {
249249
if (EditDistance > 0 && EditDistance <= EditDistanceThreshold) {
250250
if (checkOverrideWithoutName(Context, BaseMD, DerivedMD)) {
251251
// A "virtual near miss" is found.
252-
diag(DerivedMD->getLocStart(),
253-
"method '%0' has a similar name and the same signature as "
254-
"virtual method '%1'; did you mean to override it?")
252+
auto Range = CharSourceRange::getTokenRange(
253+
SourceRange(DerivedMD->getLocation()));
254+
255+
bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
256+
!DerivedMD->isTemplateInstantiation();
257+
auto Diag =
258+
diag(DerivedMD->getLocStart(),
259+
"method '%0' has a similar name and the same signature as "
260+
"virtual method '%1'; did you mean to override it?")
255261
<< DerivedMD->getQualifiedNameAsString()
256262
<< BaseMD->getQualifiedNameAsString();
263+
if (ApplyFix)
264+
Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
257265
}
258266
}
259267
}

‎clang-tools-extra/clang-tidy/misc/VirtualNearMissCheck.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "../ClangTidy.h"
1414
#include <map>
15-
#include <string>
1615

1716
namespace clang {
1817
namespace tidy {
@@ -46,12 +45,12 @@ class VirtualNearMissCheck : public ClangTidyCheck {
4645
bool isOverriddenByDerivedClass(const CXXMethodDecl *BaseMD,
4746
const CXXRecordDecl *DerivedRD);
4847

49-
/// key: the unique ID of a method;
50-
/// value: whether the method is possible to be overridden.
48+
/// Key: the unique ID of a method.
49+
/// Value: whether the method is possible to be overridden.
5150
std::map<const CXXMethodDecl *, bool> PossibleMap;
5251

53-
/// key: <unique ID of base method, name of derived class>
54-
/// value: whether the base method is overridden by some method in the derived
52+
/// Key: <unique ID of base method, name of derived class>
53+
/// Value: whether the base method is overridden by some method in the derived
5554
/// class.
5655
std::map<std::pair<const CXXMethodDecl *, const CXXRecordDecl *>, bool>
5756
OverriddenMap;

‎clang-tools-extra/test/clang-tidy/misc-virtual-near-miss.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,58 @@ struct Derived : Base {
1616
// overriden by this class.
1717
virtual void funk();
1818
// 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]
19+
// CHECK-FIXES: virtual void func();
1920

2021
void func2();
2122
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Derived::func2' has {{.*}} 'Base::func'
23+
// CHECK-FIXES: void func();
2224

2325
void func22(); // Should not warn.
2426

2527
void gunk(); // Should not warn: gunk is override.
2628

2729
void fun();
2830
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Derived::fun' has {{.*}} 'Base::func'
31+
// CHECK-FIXES: void func();
2932

3033
Derived &operator==(const Base &); // Should not warn: operators are ignored.
3134

3235
virtual NoDefinedClass2 *f1(); // Should not crash: non-defined class return type is ignored.
3336
};
3437

38+
template <typename T>
39+
struct TBase {
40+
virtual void tfunc(T t);
41+
};
42+
43+
template <typename T>
44+
struct TDerived : TBase<T> {
45+
virtual void tfunk(T t);
46+
// Should not apply fix for template.
47+
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: method 'TDerived<double>::tfunk' has {{.*}} 'TBase<double>::tfunc'
48+
// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: method 'TDerived<int>::tfunk' has {{.*}} 'TBase<int>::tfunc'
49+
// CHECK-FIXES: virtual void tfunk(T t);
50+
};
51+
52+
TDerived<int> T1;
53+
TDerived<double> T2;
54+
55+
// Should not fix macro definition
56+
#define MACRO1 void funcM()
57+
// CHECK-FIXES: #define MACRO1 void funcM()
58+
#define MACRO2(m) void m()
59+
// CHECK-FIXES: #define MACRO2(m) void m()
60+
61+
struct DerivedMacro : Base {
62+
MACRO1;
63+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'DerivedMacro::funcM' has {{.*}} 'Base::func'
64+
// CHECK-FIXES: MACRO1;
65+
66+
MACRO2(func3);
67+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'DerivedMacro::func3' has {{.*}} 'Base::func'
68+
// CHECK-FIXES: MACRO2(func);
69+
};
70+
3571
typedef Derived derived_type;
3672

3773
class Father {
@@ -58,32 +94,40 @@ class Child : private Father, private Mother {
5894

5995
virtual void func2();
6096
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::func2' has {{.*}} 'Father::func'
97+
// CHECK-FIXES: virtual void func();
6198

6299
int methoe(int x, char **strs); // Should not warn: parameter types don't match.
63100

64101
int methoe(int x);
65102
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::methoe' has {{.*}} 'Mother::method'
103+
// CHECK-FIXES: int method(int x);
66104

67105
void methof(int x, const char **strs); // Should not warn: return types don't match.
68106

69107
int methoh(int x, const char **strs);
70108
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::methoh' has {{.*}} 'Mother::method'
109+
// CHECK-FIXES: int method(int x, const char **strs);
71110

72111
virtual Child *creat(int i);
73112
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::creat' has {{.*}} 'Father::create'
113+
// CHECK-FIXES: virtual Child *create(int i);
74114

75115
virtual Derived &&generat();
76116
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::generat' has {{.*}} 'Father::generate'
117+
// CHECK-FIXES: virtual Derived &&generate();
77118

78119
int decaz(const char str[]);
79120
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::decaz' has {{.*}} 'Mother::decay'
121+
// CHECK-FIXES: int decay(const char str[]);
80122

81123
operator bool();
82124

83125
derived_type *canonica(derived_type D);
84126
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::canonica' has {{.*}} 'Father::canonical'
127+
// CHECK-FIXES: derived_type *canonical(derived_type D);
85128

86129
private:
87130
void funk();
88131
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: method 'Child::funk' has {{.*}} 'Father::func'
132+
// CHECK-FIXES: void func();
89133
};

0 commit comments

Comments
 (0)
Please sign in to comment.