Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedLocalVarsChecker -verify %s
2
3 #include "mock-types.h"
4 #include "mock-system-header.h"
5
6 void someFunction();
7
8 namespace local_vars_const_refptr_member {
9
10 class Foo {
11 public:
12 Foo();
13 void bar();
14
15 RefCountable& ensureObj3() {
16 if (!m_obj3)
17 const_cast<RefPtr<RefCountable>&>(m_obj3) = RefCountable::create();
18 return *m_obj3;
19 }
20
21 RefCountable& ensureObj4() {
22 if (!m_obj4)
23 const_cast<RefPtr<RefCountable>&>(m_obj4) = RefCountable::create();
24 if (auto* next = m_obj4->next())
25 return *next;
26 return *m_obj4;
27 }
28
29 RefCountable* ensureObj5() {
30 if (!m_obj5)
31 const_cast<RefPtr<RefCountable>&>(m_obj5) = RefCountable::create();
32 if (m_obj5->next())
33 return nullptr;
34 return m_obj5.get();
35 }
36
37 private:
38 const RefPtr<RefCountable> m_obj1;
39 RefPtr<RefCountable> m_obj2;
40 const RefPtr<RefCountable> m_obj3;
41 const RefPtr<RefCountable> m_obj4;
42 const RefPtr<RefCountable> m_obj5;
43 };
44
45 void Foo::bar() {
46 auto* obj1 = m_obj1.get();
47 obj1->method();
48 auto* obj2 = m_obj2.get();
49 // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
50 obj2->method();
51 auto& obj3 = ensureObj3();
52 obj3.method();
53 auto& obj4 = ensureObj4();
54 // expected-warning@-1{{Local variable 'obj4' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
55 obj4.method();
56 auto* obj5 = ensureObj5();
57 }
58
59 } // namespace local_vars_const_refptr_member
60
61 namespace local_vars_const_ref_member {
62
63 class Foo {
64 public:
65 Foo();
66 void bar();
67
68 private:
69 const Ref<RefCountable> m_obj1;
70 Ref<RefCountable> m_obj2;
71 };
72
73 void Foo::bar() {
74 auto& obj1 = m_obj1.get();
75 obj1.method();
76 auto& obj2 = m_obj2.get();
77 // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
78 obj2.method();
79 }
80
81 } // namespace local_vars_const_ref_member
82
83 namespace call_args_const_unique_ptr {
84
85 class Foo {
86 public:
87 Foo();
88 void bar();
89
90 private:
91 const std::unique_ptr<RefCountable> m_obj1;
92 std::unique_ptr<RefCountable> m_obj2;
93 };
94
95 void Foo::bar() {
96 auto* obj1 = m_obj1.get();
97 obj1->method();
98 auto* obj2 = m_obj2.get();
99 // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
100 obj2->method();
101 }
102
103 } // namespace call_args_const_unique_ptr