Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/call-args-counted-const-member.cpp

raw
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 RefCountable& obj1() const { return m_obj1; }
35
36 private:
37 const Ref<RefCountable> m_obj1;
38 Ref<RefCountable> m_obj2;
39 };
40
41 void Foo::bar() {
42 m_obj1->method();
43 m_obj2->method();
44 // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
45 obj1().method();
46 }
47
48 } // namespace call_args_const_ref_member
49
50 namespace call_args_const_unique_ptr {
51
52 class Foo {
53 public:
54 Foo();
55 void bar();
56
57 RefCountable& ensureObj3() {
58 if (!m_obj3)
59 const_cast<std::unique_ptr<RefCountable>&>(m_obj3) = RefCountable::makeUnique();
60 return *m_obj3;
61 }
62
63 RefCountable& badEnsureObj4() {
64 if (!m_obj4)
65 const_cast<std::unique_ptr<RefCountable>&>(m_obj4) = RefCountable::makeUnique();
66 if (auto* next = m_obj4->next())
67 return *next;
68 return *m_obj4;
69 }
70
71 RefCountable* ensureObj5() {
72 if (!m_obj5)
73 const_cast<std::unique_ptr<RefCountable>&>(m_obj5) = RefCountable::makeUnique();
74 if (m_obj5->next())
75 return nullptr;
76 return m_obj5.get();
77 }
78
79 private:
80 const std::unique_ptr<RefCountable> m_obj1;
81 std::unique_ptr<RefCountable> m_obj2;
82 const std::unique_ptr<RefCountable> m_obj3;
83 const std::unique_ptr<RefCountable> m_obj4;
84 const std::unique_ptr<RefCountable> m_obj5;
85 };
86
87 void Foo::bar() {
88 m_obj1->method();
89 m_obj2->method();
90 // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
91 ensureObj3().method();
92 badEnsureObj4().method();
93 // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
94 ensureObj5()->method();
95 }
96
97 } // namespace call_args_const_unique_ptr
98
99 namespace call_args_const_unique_ref {
100
101 class Foo {
102 public:
103 Foo();
104 void bar();
105 RefCountable& obj1() { return m_obj1; }
106
107 private:
108 const UniqueRef<RefCountable> m_obj1;
109 UniqueRef<RefCountable> m_obj2;
110 };
111
112 void Foo::bar() {
113 m_obj1->method();
114 m_obj2->method();
115 // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
116 obj1().method();
117 }
118
119 } // namespace call_args_const_unique_ref