Skip to content

Commit f590e2d

Browse files
committed
Merge remote-tracking branch 'origin/38-rlp-backend' into staging
2 parents ac995f5 + 8c828f3 commit f590e2d

14 files changed

Lines changed: 860 additions & 0 deletions

src/consumer_rlp.hpp

Lines changed: 491 additions & 0 deletions
Large diffs are not rendered by default.

src/h5_attr_translator.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ inline bool is_h5_attr_name(llvm::StringRef name) {
4040
|| name == "serialize_full";
4141
}
4242

43+
inline bool is_rlp_attr_name(llvm::StringRef name) {
44+
return name == "name" || name == "ignore" || name == "doc"
45+
|| name == "alias" || name == "required" || name == "timestamp";
46+
}
47+
4348
inline bool is_json_attr_name(llvm::StringRef name) {
4449
return name == "name" || name == "ignore" || name == "doc"
4550
|| name == "alias" || name == "version" || name == "name_all"
@@ -176,6 +181,10 @@ inline std::string rewrite_one_attr(llvm::StringRef spec) {
176181
llvm::StringRef leading = spec.substr(0, start);
177182
llvm::StringRef body = spec.substr(start);
178183

184+
bool is_h5 = body.starts_with("h5::");
185+
bool is_rlp = body.starts_with("rlp::");
186+
if (!is_h5 && !is_rlp) return spec.str();
187+
llvm::StringRef ns = is_h5 ? "h5::" : "rlp::";
179188
bool is_h5 = body.starts_with("h5::");
180189
bool is_json = body.starts_with("json::");
181190
if (!is_h5 && !is_json) return spec.str();
@@ -205,6 +214,8 @@ inline std::string rewrite_one_attr(llvm::StringRef spec) {
205214
if (i == 0) return spec.str();
206215
llvm::StringRef name = body.substr(0, i);
207216
if (is_h5 && !is_h5_attr_name(name)) return spec.str();
217+
if (is_rlp && !is_rlp_attr_name(name)) return spec.str();
218+
if (is_h5 && !is_h5_attr_name(name)) return spec.str();
208219
if (is_json && !is_json_attr_name(name)) return spec.str();
209220
if (is_h5 && !is_h5_attr_name(name)) return spec.str();
210221
if (is_msgpack && !is_msgpack_attr_name(name)) return spec.str();
@@ -283,6 +294,7 @@ inline std::string rewrite(llvm::StringRef src) {
283294
continue;
284295
}
285296
llvm::StringRef block = src.substr(i + 2, close - i - 2);
297+
if (block.contains("h5::") || block.contains("rlp::")) {
286298
if (block.contains("h5::") || block.contains("json::")) {
287299
if (block.contains("h5::") || block.contains("msgpack::")) {
288300
if (block.contains("h5::") || block.contains("cbor::")) {
@@ -320,6 +332,7 @@ inline void install_virtual_files(clang::tooling::ClangTool& Tool,
320332
for (const auto& p : paths) {
321333
std::string content = read_file(p);
322334
if (content.empty()) continue;
335+
if (content.find("h5::") == std::string::npos && content.find("rlp::") == std::string::npos) continue;
323336
if (content.find("h5::") == std::string::npos && content.find("json::") == std::string::npos) continue;
324337
if (content.find("h5::") == std::string::npos && content.find("msgpack::") == std::string::npos) continue;
325338
if (content.find("h5::") == std::string::npos && content.find("cbor::") == std::string::npos) continue;

src/h5cpp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "producer_h5.hpp"
1919
#include "consumer.hpp"
20+
#include "consumer_rlp.hpp"
2021
#include "consumer_bson.hpp"
2122
#include "consumer_avro.hpp"
2223
#include "h5_attr_translator.hpp"
@@ -161,11 +162,16 @@ int main(int argc, const char **argv) {
161162
Finder.addMatcher(h5templateMatcher, &callback);
162163
rc = Tool.run(clang::tooling::newFrontendActionFactory(&Finder).get());
163164
break;
165+
case OutputFormat::rlp: {
166+
RlpTemplateCallback callback(work_path);
167+
Finder.addMatcher(h5templateMatcher, &callback);
168+
rc = Tool.run(clang::tooling::newFrontendActionFactory(&Finder).get());
164169
}
165170
case OutputFormat::rlp:
166171
llvm::errs() << "h5cpp-compiler: --format rlp not yet implemented\n";
167172
rc = 1;
168173
break;
174+
}
169175
}
170176
}
171177

tests/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ foreach(fixture IN LISTS FIXTURES)
7777
add_h5cpp_fixture_test(${fixture})
7878
endforeach()
7979

80+
set(RLP_FIXTURES
81+
rlp_primitives
82+
rlp_arrays
83+
rlp_nested
84+
rlp_strings
85+
rlp_timestamp
86+
)
87+
88+
foreach(fixture IN LISTS RLP_FIXTURES)
89+
add_h5cpp_backend_fixture_test(${fixture} rlp)
90+
endforeach()
91+
8092
set(JSON_FIXTURES
8193
json_primitives
8294
json_arrays

tests/fixtures/rlp_arrays.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "h5cpp_stub.hpp"
2+
3+
namespace sn::typecheck {
4+
struct [[rlp::doc("Array type test")]] Record {
5+
std::vector<int> ints;
6+
std::vector<double> doubles;
7+
std::vector<std::string> strings;
8+
};
9+
}
10+
11+
void use() {
12+
sn::typecheck::Record r;
13+
h5::write(0, "p/arrays", r);
14+
}

tests/fixtures/rlp_nested.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "h5cpp_stub.hpp"
2+
3+
namespace sn::typecheck {
4+
struct Inner {
5+
int x;
6+
double y;
7+
};
8+
9+
struct [[rlp::doc("Nested type test")]] Record {
10+
Inner inner;
11+
std::vector<Inner> inners;
12+
};
13+
}
14+
15+
void use() {
16+
sn::typecheck::Record r;
17+
h5::write(0, "p/nested", r);
18+
}

tests/fixtures/rlp_primitives.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "h5cpp_stub.hpp"
2+
3+
namespace sn::typecheck {
4+
struct [[rlp::alias("Primitives"), rlp::doc("Primitive type test")]] Record {
5+
[[rlp::required]]
6+
char _char;
7+
unsigned char _uchar;
8+
short _short;
9+
unsigned short _ushort;
10+
int _int;
11+
unsigned int _uint;
12+
long _long;
13+
unsigned long _ulong;
14+
long long _llong;
15+
unsigned long long _ullong;
16+
float _float;
17+
double _double;
18+
bool _bool;
19+
};
20+
}
21+
22+
void use() {
23+
sn::typecheck::Record r;
24+
h5::write(0, "p/primitives", r);
25+
}

tests/fixtures/rlp_strings.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <optional>
2+
#include "h5cpp_stub.hpp"
3+
4+
namespace sn::typecheck {
5+
struct [[rlp::doc("String and optional type test")]] Record {
6+
[[rlp::name("display_name")]]
7+
std::string label;
8+
std::optional<int> maybe_count;
9+
std::optional<std::string> maybe_label;
10+
[[rlp::required]]
11+
std::string id;
12+
};
13+
}
14+
15+
void use() {
16+
sn::typecheck::Record r;
17+
h5::write(0, "p/strings", r);
18+
}

tests/fixtures/rlp_timestamp.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <chrono>
2+
#include "h5cpp_stub.hpp"
3+
4+
namespace sn::typecheck {
5+
struct [[rlp::doc("Timestamp type test")]] Record {
6+
[[rlp::timestamp]]
7+
std::chrono::system_clock::time_point created_at;
8+
std::chrono::system_clock::time_point updated_at;
9+
std::string name;
10+
};
11+
}
12+
13+
void use() {
14+
sn::typecheck::Record r;
15+
h5::write(0, "p/timestamp", r);
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
/* Generated by h5cpp-compiler RLP backend */
4+
5+
#include <cstddef>
6+
#include <cstdint>
7+
8+
namespace rlp::meta {
9+
10+
enum class rlp_type_t : std::uint8_t {
11+
bytes, list, optional, timestamp
12+
};
13+
14+
struct field_desc {
15+
const char* json_name;
16+
rlp_type_t type;
17+
std::size_t offset;
18+
bool required;
19+
const char* doc;
20+
const field_desc* item;
21+
const field_desc* key;
22+
const field_desc* value;
23+
};
24+
25+
template<typename T>
26+
struct descriptor {
27+
static constexpr char alias[] = "";
28+
static constexpr field_desc fields[] = {};
29+
static constexpr std::size_t field_count = 0;
30+
};
31+
32+
} // namespace rlp::meta
33+
34+
// descriptor for sn::typecheck::Record
35+
template<>
36+
struct descriptor<sn::typecheck::Record> {
37+
static constexpr field_desc item_1 { nullptr, rlp_type_t::bytes, 0, false, nullptr, nullptr, nullptr, nullptr };
38+
static constexpr field_desc item_2 { nullptr, rlp_type_t::bytes, 0, false, nullptr, nullptr, nullptr, nullptr };
39+
static constexpr field_desc item_3 { nullptr, rlp_type_t::bytes, 0, false, nullptr, nullptr, nullptr, nullptr };
40+
static constexpr field_desc fields[] = {
41+
{ "ints", rlp_type_t::list, offsetof(sn::typecheck::Record, ints), false, nullptr, &item_1, nullptr, nullptr },
42+
{ "doubles", rlp_type_t::list, offsetof(sn::typecheck::Record, doubles), false, nullptr, &item_2, nullptr, nullptr },
43+
{ "strings", rlp_type_t::list, offsetof(sn::typecheck::Record, strings), false, nullptr, &item_3, nullptr, nullptr }
44+
};
45+
static constexpr std::size_t field_count = 3;
46+
};
47+

0 commit comments

Comments
 (0)