[llvm] 8c3adce - [JSON] Handle uint64_t type
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 15 02:19:10 PDT 2021
Author: djtodoro
Date: 2021-10-15T11:18:22+02:00
New Revision: 8c3adce81dc36306ba30cda0cdf458cfcf7d076c
URL: https://github.com/llvm/llvm-project/commit/8c3adce81dc36306ba30cda0cdf458cfcf7d076c
DIFF: https://github.com/llvm/llvm-project/commit/8c3adce81dc36306ba30cda0cdf458cfcf7d076c.diff
LOG: [JSON] Handle uint64_t type
There was no handling of uint64_t in the LLVM JSON library.
This patch adds support for that. The motivation is
the https://reviews.llvm.org/D109217.
Differential Revision: https://reviews.llvm.org/D109347
Added:
Modified:
llvm/include/llvm/Support/JSON.h
llvm/lib/Support/JSON.cpp
llvm/unittests/Support/JSONTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h
index c753cee60ec12..469f50be40e03 100644
--- a/llvm/include/llvm/Support/JSON.h
+++ b/llvm/include/llvm/Support/JSON.h
@@ -234,7 +234,7 @@ inline bool operator!=(const Array &L, const Array &R) { return !(L == R); }
/// Each Value is one of the JSON kinds:
/// null (nullptr_t)
/// boolean (bool)
-/// number (double or int64)
+/// number (double, int64 or uint64)
/// string (StringRef)
/// array (json::Array)
/// object (json::Object)
@@ -342,9 +342,20 @@ class Value {
Value(T B) : Type(T_Boolean) {
create<bool>(B);
}
- // Integers (except boolean). Must be non-narrowing convertible to int64_t.
+
+ // Unsigned 64-bit long integers.
+ template <typename T,
+ typename = std::enable_if_t<std::is_same<T, uint64_t>::value>,
+ bool = false, bool = false>
+ Value(T V) : Type(T_UINT64) {
+ create<uint64_t>(uint64_t{V});
+ }
+
+ // Integers (except boolean and uint64_t).
+ // Must be non-narrowing convertible to int64_t.
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>,
- typename = std::enable_if_t<!std::is_same<T, bool>::value>>
+ typename = std::enable_if_t<!std::is_same<T, bool>::value>,
+ typename = std::enable_if_t<!std::is_same<T, uint64_t>::value>>
Value(T I) : Type(T_Integer) {
create<int64_t>(int64_t{I});
}
@@ -382,6 +393,7 @@ class Value {
return Boolean;
case T_Double:
case T_Integer:
+ case T_UINT64:
return Number;
case T_String:
case T_StringRef:
@@ -410,6 +422,8 @@ class Value {
return as<double>();
if (LLVM_LIKELY(Type == T_Integer))
return as<int64_t>();
+ if (LLVM_LIKELY(Type == T_UINT64))
+ return as<uint64_t>();
return llvm::None;
}
// Succeeds if the Value is a Number, and exactly representable as int64_t.
@@ -425,6 +439,16 @@ class Value {
}
return llvm::None;
}
+ llvm::Optional<uint64_t> getAsUINT64() const {
+ if (Type == T_UINT64)
+ return as<uint64_t>();
+ else if (Type == T_Integer) {
+ int64_t N = as<int64_t>();
+ if (N >= 0)
+ return as<uint64_t>();
+ }
+ return llvm::None;
+ }
llvm::Optional<llvm::StringRef> getAsString() const {
if (Type == T_String)
return llvm::StringRef(as<std::string>());
@@ -467,11 +491,12 @@ class Value {
friend class OStream;
- enum ValueType : char {
+ enum ValueType : char16_t {
T_Null,
T_Boolean,
T_Double,
T_Integer,
+ T_UINT64,
T_StringRef,
T_String,
T_Object,
@@ -479,8 +504,9 @@ class Value {
};
// All members mutable, see moveFrom().
mutable ValueType Type;
- mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, llvm::StringRef,
- std::string, json::Array, json::Object>
+ mutable llvm::AlignedCharArrayUnion<bool, double, int64_t, uint64_t,
+ llvm::StringRef, std::string, json::Array,
+ json::Object>
Union;
friend bool operator==(const Value &, const Value &);
};
@@ -683,6 +709,14 @@ inline bool fromJSON(const Value &E, bool &Out, Path P) {
P.report("expected boolean");
return false;
}
+inline bool fromJSON(const Value &E, uint64_t &Out, Path P) {
+ if (auto S = E.getAsUINT64()) {
+ Out = *S;
+ return true;
+ }
+ P.report("expected uint64_t");
+ return false;
+}
inline bool fromJSON(const Value &E, std::nullptr_t &Out, Path P) {
if (auto S = E.getAsNull()) {
Out = *S;
diff --git a/llvm/lib/Support/JSON.cpp b/llvm/lib/Support/JSON.cpp
index dbfd673553f48..17b36ed51850d 100644
--- a/llvm/lib/Support/JSON.cpp
+++ b/llvm/lib/Support/JSON.cpp
@@ -109,6 +109,7 @@ void Value::copyFrom(const Value &M) {
case T_Boolean:
case T_Double:
case T_Integer:
+ case T_UINT64:
memcpy(&Union, &M.Union, sizeof(Union));
break;
case T_StringRef:
@@ -133,6 +134,7 @@ void Value::moveFrom(const Value &&M) {
case T_Boolean:
case T_Double:
case T_Integer:
+ case T_UINT64:
memcpy(&Union, &M.Union, sizeof(Union));
break;
case T_StringRef:
@@ -159,6 +161,7 @@ void Value::destroy() {
case T_Boolean:
case T_Double:
case T_Integer:
+ case T_UINT64:
break;
case T_StringRef:
as<StringRef>().~StringRef();
@@ -750,6 +753,8 @@ void llvm::json::OStream::value(const Value &V) {
valueBegin();
if (V.Type == Value::T_Integer)
OS << *V.getAsInteger();
+ else if (V.Type == Value::T_UINT64)
+ OS << *V.getAsUINT64();
else
OS << format("%.*g", std::numeric_limits<double>::max_digits10,
*V.getAsNumber());
diff --git a/llvm/unittests/Support/JSONTest.cpp b/llvm/unittests/Support/JSONTest.cpp
index ed9a72d36b064..f28c99819d053 100644
--- a/llvm/unittests/Support/JSONTest.cpp
+++ b/llvm/unittests/Support/JSONTest.cpp
@@ -356,6 +356,27 @@ TEST(JSONTest, Integers) {
}
}
+// Verify uint64_t type.
+TEST(JSONTest, U64Integers) {
+ Value Val = uint64_t{3100100100};
+ uint64_t Var = 3100100100;
+ EXPECT_EQ(Val, Var);
+
+ // Test the parse() part.
+ const char *Str = "4611686018427387905";
+ llvm::Expected<Value> Doc = parse(Str);
+
+ EXPECT_TRUE(!!Doc);
+ EXPECT_EQ(Doc->getAsInteger(), int64_t{4611686018427387905});
+ EXPECT_EQ(Doc->getAsUINT64(), uint64_t{4611686018427387905});
+
+ const char *Str2 = "-78278238238328222";
+ llvm::Expected<Value> Doc2 = parse(Str2);
+ EXPECT_TRUE(!!Doc2);
+ EXPECT_EQ(Doc2->getAsInteger(), int64_t{-78278238238328222});
+ EXPECT_EQ(Doc2->getAsUINT64(), llvm::None);
+}
+
// Sample struct with typical JSON-mapping rules.
struct CustomStruct {
CustomStruct() : B(false) {}
More information about the llvm-commits
mailing list