1 // RUN: %check_clang_tidy %s bugprone-compare-pointer-to-member-virtual-function %t
2 
3 struct A {
4   virtual ~A();
5   void f1();
6   void f2();
7   virtual void f3();
8   virtual void f4();
9 
10   void g1(int);
11 };
12 
13 bool Result;
14 
base()15 void base() {
16   Result = (&A::f1 == &A::f2);
17 
18   Result = (&A::f1 == &A::f3);
19   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
20 
21   Result = (&A::f1 != &A::f3);
22   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
23 
24   Result = (&A::f3 == nullptr);
25 
26   Result = (&A::f3 == &A::f4);
27   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
28 
29   void (A::*V1)() = &A::f3;
30   Result = (V1 == &A::f1);
31   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
32   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
33   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
34   Result = (&A::f1 == V1);
35   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
36   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
37   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
38 
39   auto& V2 = V1;
40   Result = (&A::f1 == V2);
41   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
42   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
43   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
44 
45   void (A::*V3)(int) = &A::g1;
46   Result = (V3 == &A::g1);
47   Result = (&A::g1 == V3);
48 }
49 
50 using B = A;
usingRecordName()51 void usingRecordName() {
52   Result = (&B::f1 == &B::f3);
53   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
54 
55   void (B::*V1)() = &B::f1;
56   Result = (V1 == &B::f1);
57   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
58   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
59   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
60 }
61 
62 typedef A C;
typedefRecordName()63 void typedefRecordName() {
64   Result = (&C::f1 == &C::f3);
65   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
66 
67   void (C::*V1)() = &C::f1;
68   Result = (V1 == &C::f1);
69   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
70   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
71   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
72 }
73 
74 struct A1 : public A {
75 };
76 
inheritClass()77 void inheritClass() {
78   Result = (&A1::f1 == &A1::f3);
79   // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
80 
81   void (A1::*V1)() = &A1::f1;
82   Result = (V1 == &A1::f1);
83   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: comparing a pointer to member virtual function with other pointer is unspecified behavior, only compare it with a null-pointer constant for equality.
84   // CHECK-MESSAGES: :7:3: note: potential member virtual function is declared here.
85   // CHECK-MESSAGES: :8:3: note: potential member virtual function is declared here.
86 }
87