xref: /llvm-project/clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp (revision 05860f9b384b9b8f8bb01fa8984dbc2833669a27)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2 
3 #include "mock-types.h"
4 
5 namespace std {
6 }
7 
8 namespace call_args_const_refptr_member {
9 
10 class Foo {
11 public:
12   Foo();
13   void bar();
14 
15 private:
16   const RefPtr<RefCountable> m_obj1;
17   RefPtr<RefCountable> m_obj2;
18 };
19 
20 void Foo::bar() {
21   m_obj1->method();
22   m_obj2->method();
23   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
24 }
25 
26 } // namespace call_args_const_refptr_member
27 
28 namespace call_args_const_ref_member {
29 
30 class Foo {
31 public:
32   Foo();
33   void bar();
34 
35 private:
36   const Ref<RefCountable> m_obj1;
37   Ref<RefCountable> m_obj2;
38 };
39 
40 void Foo::bar() {
41   m_obj1->method();
42   m_obj2->method();
43   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
44 }
45 
46 } // namespace call_args_const_ref_member
47 
48 namespace call_args_const_unique_ptr {
49 
50 class Foo {
51 public:
52   Foo();
53   void bar();
54 
55   RefCountable& ensureObj3() {
56     if (!m_obj3)
57       const_cast<std::unique_ptr<RefCountable>&>(m_obj3) = RefCountable::makeUnique();
58     return *m_obj3;
59   }
60 
61   RefCountable& badEnsureObj4() {
62     if (!m_obj4)
63       const_cast<std::unique_ptr<RefCountable>&>(m_obj4) = RefCountable::makeUnique();
64     if (auto* next = m_obj4->next())
65       return *next;
66     return *m_obj4;
67   }
68 
69   RefCountable* ensureObj5() {
70     if (!m_obj5)
71       const_cast<std::unique_ptr<RefCountable>&>(m_obj5) = RefCountable::makeUnique();
72     if (m_obj5->next())
73       return nullptr;
74     return m_obj5.get();
75   }
76 
77 private:
78   const std::unique_ptr<RefCountable> m_obj1;
79   std::unique_ptr<RefCountable> m_obj2;
80   const std::unique_ptr<RefCountable> m_obj3;
81   const std::unique_ptr<RefCountable> m_obj4;
82   const std::unique_ptr<RefCountable> m_obj5;
83 };
84 
85 void Foo::bar() {
86   m_obj1->method();
87   m_obj2->method();
88   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
89   ensureObj3().method();
90   badEnsureObj4().method();
91   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
92   ensureObj5()->method();
93 }
94 
95 } // namespace call_args_const_unique_ptr
96 
97 namespace call_args_const_unique_ref {
98 
99 class Foo {
100 public:
101   Foo();
102   void bar();
103 
104 private:
105   const UniqueRef<RefCountable> m_obj1;
106   UniqueRef<RefCountable> m_obj2;
107 };
108 
109 void Foo::bar() {
110   m_obj1->method();
111   m_obj2->method();
112   // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
113 }
114 
115 } // namespace call_args_const_unique_ref
116