brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 8da415a Raw
120 lines · cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s2 3#include "mock-types.h"4 5namespace std {6}7 8namespace call_args_const_refptr_member {9 10class Foo {11public:12  Foo();13  void bar();14 15private:16  const RefPtr<RefCountable> m_obj1;17  RefPtr<RefCountable> m_obj2;18};19 20void 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_member27 28namespace call_args_const_ref_member {29 30class Foo {31public:32  Foo();33  void bar();34  RefCountable& obj1() const { return m_obj1; }35 36private:37  const Ref<RefCountable> m_obj1;38  Ref<RefCountable> m_obj2;39};40 41void 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_member49 50namespace call_args_const_unique_ptr {51 52class Foo {53public: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 79private: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 87void 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_ptr98 99namespace call_args_const_unique_ref {100 101class Foo {102public:103  Foo();104  void bar();105  RefCountable& obj1() { return m_obj1; }106 107private:108  const UniqueRef<RefCountable> m_obj1;109  UniqueRef<RefCountable> m_obj2;110};111 112void 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_ref120