xref: /llvm-project/clang/test/Analysis/Checkers/WebKit/local-vars-counted-const-member.cpp (revision 05860f9b384b9b8f8bb01fa8984dbc2833669a27)
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       // expected-warning@-1{{Local variable 'next' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
26       return *next;
27     }
28     return *m_obj4;
29   }
30 
31   RefCountable* ensureObj5() {
32     if (!m_obj5)
33       const_cast<RefPtr<RefCountable>&>(m_obj5) = RefCountable::create();
34     if (m_obj5->next())
35       return nullptr;
36     return m_obj5.get();
37   }
38 
39 private:
40   const RefPtr<RefCountable> m_obj1;
41   RefPtr<RefCountable> m_obj2;
42   const RefPtr<RefCountable> m_obj3;
43   const RefPtr<RefCountable> m_obj4;
44   const RefPtr<RefCountable> m_obj5;
45 };
46 
47 void Foo::bar() {
48   auto* obj1 = m_obj1.get();
49   obj1->method();
50   auto* obj2 = m_obj2.get();
51   // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
52   obj2->method();
53   auto& obj3 = ensureObj3();
54   obj3.method();
55   auto& obj4 = ensureObj4();
56   // expected-warning@-1{{Local variable 'obj4' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
57   obj4.method();
58   auto* obj5 = ensureObj5();
59 }
60 
61 } // namespace local_vars_const_refptr_member
62 
63 namespace local_vars_const_ref_member {
64 
65 class Foo {
66 public:
67   Foo();
68   void bar();
69 
70 private:
71   const Ref<RefCountable> m_obj1;
72   Ref<RefCountable> m_obj2;
73 };
74 
75 void Foo::bar() {
76   auto& obj1 = m_obj1.get();
77   obj1.method();
78   auto& obj2 = m_obj2.get();
79   // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
80   obj2.method();
81 }
82 
83 } // namespace local_vars_const_ref_member
84 
85 namespace call_args_const_unique_ptr {
86 
87 class Foo {
88 public:
89   Foo();
90   void bar();
91 
92 private:
93   const std::unique_ptr<RefCountable> m_obj1;
94   std::unique_ptr<RefCountable> m_obj2;
95 };
96 
97 void Foo::bar() {
98   auto* obj1 = m_obj1.get();
99   obj1->method();
100   auto* obj2 = m_obj2.get();
101   // expected-warning@-1{{Local variable 'obj2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
102   obj2->method();
103 }
104 
105 } // namespace call_args_const_unique_ptr
106