Summary: To avoid false positives, we treat `operator[]` in cpp as container read. Moreover, if a container `c` is owned, we make all accesses `c[i]` to be also owned. Reviewed By: sblackshear Differential Revision: D6396574 fbshipit-source-id: 94aabffmaster
parent
74670cb0ba
commit
9e2ecac204
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017 - present Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace constructors {
|
||||||
|
|
||||||
|
struct dynamic {
|
||||||
|
enum T {
|
||||||
|
NULLT,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct ObjectMaker;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <class T>
|
||||||
|
dynamic(T t);
|
||||||
|
static ObjectMaker object();
|
||||||
|
dynamic(dynamic&&) noexcept;
|
||||||
|
dynamic& operator=(dynamic&&) noexcept;
|
||||||
|
dynamic& operator[](dynamic const&) &;
|
||||||
|
dynamic&& operator[](dynamic const&) &&;
|
||||||
|
template <class K, class V>
|
||||||
|
void insert(K&&, V&& val);
|
||||||
|
|
||||||
|
private:
|
||||||
|
T type_;
|
||||||
|
};
|
||||||
|
|
||||||
|
dynamic& dynamic::operator=(dynamic&& o) noexcept {
|
||||||
|
if (&o != this) {
|
||||||
|
if (type_ == o.type_) {
|
||||||
|
} else {
|
||||||
|
type_ = o.type_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dynamic::ObjectMaker {
|
||||||
|
friend struct dynamic;
|
||||||
|
|
||||||
|
explicit ObjectMaker() {}
|
||||||
|
|
||||||
|
ObjectMaker(ObjectMaker&&) = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline dynamic::ObjectMaker dynamic::object() { return ObjectMaker(); }
|
||||||
|
|
||||||
|
struct BSS {
|
||||||
|
dynamic toJson_ok() const noexcept {
|
||||||
|
dynamic ret = dynamic::object();
|
||||||
|
ret["key"] = dynamic::object();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic& toJson_race(dynamic& ret) const noexcept {
|
||||||
|
ret["key"] = dynamic::object();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TSL {
|
||||||
|
std::mutex mutex_;
|
||||||
|
|
||||||
|
void not_locked_ok(dynamic& ret) { BSS().toJson_ok(); }
|
||||||
|
|
||||||
|
void locked_ok(dynamic& ret) {
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
BSS().toJson_ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
void not_locked_race(dynamic& ret) { BSS().toJson_race(ret); }
|
||||||
|
|
||||||
|
void locked_race(dynamic& ret) {
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
BSS().toJson_race(ret);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace constructors
|
Loading…
Reference in new issue