File: call-args-wtf-containers.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (264 lines) | stat: -rw-r--r-- 7,733 bytes parent folder | download | duplicates (7)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s

#include "mock-types.h"

namespace WTF {

  constexpr unsigned long notFound = static_cast<unsigned long>(-1);

  class String;
  class StringImpl;

  class StringView {
  public:
    StringView(const String&);
  private:
    RefPtr<StringImpl> m_impl;
  };

  class StringImpl {
  public:
    void ref() const { ++m_refCount; }
    void deref() const {
      if (!--m_refCount)
        delete this;
    }

    static constexpr unsigned s_flagIs8Bit = 1u << 0;
    bool is8Bit() const { return m_hashAndFlags & s_flagIs8Bit; }
    const char* characters8() const { return m_char8; }
    const short* characters16() const { return m_char16; }
    unsigned length() const { return m_length; }
    Ref<StringImpl> substring(unsigned position, unsigned length) const;

    unsigned long find(char) const;
    unsigned long find(StringView) const;
    unsigned long contains(StringView) const;
    unsigned long findIgnoringASCIICase(StringView) const;

    bool startsWith(StringView) const;
    bool startsWithIgnoringASCIICase(StringView) const;
    bool endsWith(StringView) const;
    bool endsWithIgnoringASCIICase(StringView) const;

  private:
    mutable unsigned m_refCount { 0 };
    unsigned m_length { 0 };
    union {
      const char* m_char8;
      const short* m_char16;
    };
    unsigned m_hashAndFlags { 0 };
  };

  class String {
  public:
    String() = default;
    String(StringImpl& impl) : m_impl(&impl) { }
    String(StringImpl* impl) : m_impl(impl) { }
    String(Ref<StringImpl>&& impl) : m_impl(impl.get()) { }
    StringImpl* impl() { return m_impl.get(); }
    unsigned length() const { return m_impl ? m_impl->length() : 0; }
    const char* characters8() const { return m_impl ? m_impl->characters8() : nullptr; }
    const short* characters16() const { return m_impl ? m_impl->characters16() : nullptr; }

    bool is8Bit() const { return !m_impl || m_impl->is8Bit(); }

    unsigned long find(char character) const { return m_impl ? m_impl->find(character) : notFound; }
    unsigned long find(StringView str) const { return m_impl ? m_impl->find(str) : notFound; }
    unsigned long findIgnoringASCIICase(StringView) const;

    bool contains(char character) const { return find(character) != notFound; }
    bool contains(StringView) const;
    bool containsIgnoringASCIICase(StringView) const;

    bool startsWith(StringView) const;
    bool startsWithIgnoringASCIICase(StringView) const;
    bool endsWith(StringView) const;
    bool endsWithIgnoringASCIICase(StringView) const;

    String substring(unsigned position, unsigned length) const
    {
      if (!m_impl)
        return { };
      if (!position && length >= m_impl->length())
        return *this;
      return m_impl->substring(position, length);
    }

  private:
    RefPtr<StringImpl> m_impl;
  };

  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::StringView;
using WTF::StringImpl;
using WTF::String;
using WTF::HashSet;
using WTF::HashMap;
using WTF::WeakHashSet;
using WTF::Vector;

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

RefCounted* object();
StringImpl* strImpl();
String* str();
StringView strView();

void test() {
  strImpl()->is8Bit();
  strImpl()->characters8();
  strImpl()->characters16();
  strImpl()->length();
  strImpl()->substring(2, 4);
  strImpl()->find(strView());
  strImpl()->contains(strView());
  strImpl()->findIgnoringASCIICase(strView());
  strImpl()->startsWith(strView());
  strImpl()->startsWithIgnoringASCIICase(strView());
  strImpl()->endsWith(strView());
  strImpl()->endsWithIgnoringASCIICase(strView());

  str()->is8Bit();
  str()->characters8();
  str()->characters16();
  str()->length();
  str()->substring(2, 4);
  str()->find(strView());
  str()->contains(strView());
  str()->findIgnoringASCIICase(strView());
  str()->startsWith(strView());
  str()->startsWithIgnoringASCIICase(strView());
  str()->endsWith(strView());
  str()->endsWithIgnoringASCIICase(strView());

  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; });
}