File: call-args-wtf-containers.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (146 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s

#include "mock-types.h"

namespace WTF {

  template <typename T>
  class HashSet {
  public:
    template <typename U> T* find(U&) const;
    template <typename U> bool contains(U&) const;
    unsigned size() { return m_size; }
    template <typename U> void add(U&) const;
    template <typename U> void remove(U&) const;

  private:
    T* m_table { nullptr };
    unsigned m_size { 0 };
  };

  template <typename T, typename S>
  class HashMap {
  public:
    struct Item {
      T key;
      S value;
    };

    template <typename U> Item* find(U&) const;
    template <typename U> bool contains(U&) const;
    template <typename U> S* get(U&) const;
    template <typename U> S* inlineGet(U&) const;
    template <typename U> void add(U&) const;
    template <typename U> void remove(U&) const;

  private:
    Item* m_table { nullptr };
  };

  template <typename T>
  class WeakHashSet {
  public:
    template <typename U> T* find(U&) const;
    template <typename U> bool contains(U&) const;
    template <typename U> void add(U&) const;
    template <typename U> void remove(U&) const;
  };

  template <typename T>
  class Vector {
  public:
    unsigned size() { return m_size; }
    T& at(unsigned i) { return m_buffer[i]; }
    T& operator[](unsigned i) { return m_buffer[i]; }
    template <typename U> unsigned find(U&);
    template <typename U> unsigned reverseFind(U&);
    template <typename U> bool contains(U&);
    template <typename MatchFunction> unsigned findIf(const MatchFunction& match)
    {
      for (unsigned i = 0; i < m_size; ++i) {
        if (match(at(i)))
          return i;
      }
      return static_cast<unsigned>(-1);
    }
    template <typename MatchFunction> unsigned reverseFindIf(const MatchFunction& match)
    {
      for (unsigned i = 0; i < m_size; ++i) {
        if (match(at(m_size - i)))
          return i;
      }
      return static_cast<unsigned>(-1);
    }
    template <typename MatchFunction> bool containsIf(const MatchFunction& match)
    {
      for (unsigned i = 0; i < m_size; ++i) {
        if (match(at(m_size - i)))
          return true;
      }
      return false;
    }
    template <typename U> void append(U&) const;
    template <typename U> void remove(U&) const;

  private:
    T* m_buffer { nullptr };
    unsigned m_size { 0 };
  };

}

using WTF::HashSet;
using WTF::HashMap;
using WTF::WeakHashSet;
using WTF::Vector;

class RefCounted {
public:
  void ref() const;
  void deref() const;
};

RefCounted* object();

void test() {
  HashSet<RefPtr<RefCounted>> set;
  set.find(*object());
  set.contains(*object());
  set.add(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}
  set.remove(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}

  HashMap<Ref<RefCounted>, unsigned> map;
  map.find(*object());
  map.contains(*object());
  map.inlineGet(*object());
  map.add(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}
  map.remove(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}

  WeakHashSet<Ref<RefCounted>> weakSet;
  weakSet.find(*object());
  weakSet.contains(*object());
  weakSet.add(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}
  weakSet.remove(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}

  Vector<Ref<RefCounted>> vector;
  vector.at(0);
  vector[0];
  vector.find(*object());
  vector.reverseFind(*object());
  vector.contains(*object());
  vector.append(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}
  vector.remove(*object());
  // expected-warning@-1{{Call argument is uncounted and unsafe}}

  auto* obj = object();
  vector.findIf([&](Ref<RefCounted> key) { return key.ptr() == obj; });
  vector.reverseFindIf([&](Ref<RefCounted> key) { return key.ptr() == obj; });
  vector.containsIf([&](Ref<RefCounted> key) { return key.ptr() == obj; });
}