Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "rs_bindings_from_cc/ir.h" |
| 6 | |
Googler | 4dda73e | 2021-12-03 09:13:42 +0000 | [diff] [blame] | 7 | #include <iomanip> |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "rs_bindings_from_cc/bazel_types.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 11 | #include "testing/base/public/gunit.h" |
Marcel Hlopko | 7aa38a7 | 2021-11-11 07:39:51 +0000 | [diff] [blame] | 12 | #include "third_party/absl/hash/hash_testing.h" |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 13 | #include "third_party/json/src/json.hpp" |
| 14 | |
| 15 | namespace rs_bindings_from_cc { |
| 16 | |
| 17 | namespace { |
| 18 | |
Googler | 4dda73e | 2021-12-03 09:13:42 +0000 | [diff] [blame] | 19 | MATCHER_P(EqualsJson, expected_json, "") { |
| 20 | if (arg == expected_json) { |
| 21 | return true; |
| 22 | } |
| 23 | *result_listener << "Diff:\n" |
| 24 | << std::setw(2) << nlohmann::json::diff(arg, expected_json); |
| 25 | return false; |
| 26 | } |
| 27 | |
Googler | ea1aff8 | 2021-09-16 08:40:21 +0000 | [diff] [blame] | 28 | TEST(IrTest, TypeToJson) { |
Devin Jeanpierre | 5c87a72 | 2021-09-16 10:35:58 +0000 | [diff] [blame] | 29 | nlohmann::json expected = nlohmann::json::parse(R"j({ |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 30 | "rs_type": { |
| 31 | "name": "CompoundRs", |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 32 | "lifetime_args": [], |
| 33 | "type_args": [{"name": "i32", "lifetime_args": [], "type_args": []}] |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 34 | }, |
| 35 | "cc_type": { |
| 36 | "name": "CompoundCc", |
| 37 | "is_const": false, |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 38 | "type_args": [ |
| 39 | {"is_const": false, "name": "int", "type_args": []} |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 40 | ] |
| 41 | } |
Devin Jeanpierre | 5c87a72 | 2021-09-16 10:35:58 +0000 | [diff] [blame] | 42 | })j"); |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 43 | auto type = MappedType{ |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 44 | .rs_type = RsType{.name = "CompoundRs", .type_args = {RsType{"i32"}}}, |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 45 | .cc_type = CcType{.name = "CompoundCc", |
| 46 | .is_const = false, |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 47 | .type_args = {CcType{"int"}}}}; |
Googler | 4dda73e | 2021-12-03 09:13:42 +0000 | [diff] [blame] | 48 | EXPECT_THAT(type.ToJson(), EqualsJson(expected)); |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(IrTest, TypeWithDeclIdToJson) { |
| 52 | nlohmann::json expected = nlohmann::json::parse(R"j({ |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 53 | "rs_type": {"lifetime_args": [], "type_args": [], "decl_id": 42}, |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 54 | "cc_type": { |
| 55 | "is_const": false, |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 56 | "type_args": [], |
Marcel Hlopko | 9c150da | 2021-11-12 10:30:03 +0000 | [diff] [blame] | 57 | "decl_id": 43 |
| 58 | } |
| 59 | })j"); |
| 60 | auto type = MappedType{.rs_type = {RsType{"Status", DeclId(42)}}, |
| 61 | .cc_type = {CcType{"Result", DeclId(43)}}}; |
Googler | 4dda73e | 2021-12-03 09:13:42 +0000 | [diff] [blame] | 62 | EXPECT_THAT(type.ToJson(), EqualsJson(expected)); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Googler | ea1aff8 | 2021-09-16 08:40:21 +0000 | [diff] [blame] | 65 | TEST(IrTest, IR) { |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 66 | nlohmann::json expected = nlohmann::json::parse( |
| 67 | R"j({ |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 68 | "current_target": "//foo:bar", |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 69 | "used_headers": [{ "name": "foo/bar.h" }], |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 70 | "items": [ |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 71 | { "Record": { |
| 72 | "identifier": { "identifier": "SomeStruct" }, |
Marcel Hlopko | 264b9ad | 2021-12-02 21:06:44 +0000 | [diff] [blame] | 73 | "id": 42, |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 74 | "owning_target": "//foo:bar", |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 75 | "fields": [ |
| 76 | { |
| 77 | "identifier": { "identifier": "public_int" }, |
| 78 | "type": { |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 79 | "rs_type": { "name": "i32", "lifetime_args": [], "type_args": [] }, |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 80 | "cc_type": { |
| 81 | "is_const": false, |
| 82 | "name": "int", |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 83 | "type_args": [] |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 84 | } |
| 85 | }, |
| 86 | "access": "Public", |
| 87 | "offset": 0 |
| 88 | }, |
| 89 | { |
| 90 | "identifier": { "identifier": "protected_int" }, |
| 91 | "type": { |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 92 | "rs_type": { "name": "i32", "lifetime_args": [], "type_args": [] }, |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 93 | "cc_type": { |
| 94 | "is_const": false, |
| 95 | "name": "int", |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 96 | "type_args": [] |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 97 | } |
| 98 | }, |
| 99 | "access": "Protected", |
| 100 | "offset": 32 |
| 101 | }, |
| 102 | { |
| 103 | "identifier": { "identifier": "private_int" }, |
| 104 | "type": { |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 105 | "rs_type": { "name": "i32", "lifetime_args": [], "type_args": [] }, |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 106 | "cc_type": { |
| 107 | "is_const": false, |
| 108 | "name": "int", |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 109 | "type_args": [] |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 110 | } |
| 111 | }, |
| 112 | "access": "Private", |
| 113 | "offset": 64 |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 114 | } |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 115 | ], |
Googler | 64e4edb | 2021-12-03 12:17:38 +0000 | [diff] [blame] | 116 | "lifetime_params": [], |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 117 | "size": 12, |
| 118 | "alignment": 4, |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 119 | "copy_constructor": { |
Devin Jeanpierre | 7b62e95 | 2021-12-08 21:43:30 +0000 | [diff] [blame] | 120 | "definition": "NontrivialUserDefined", |
Devin Jeanpierre | 0793127 | 2021-10-05 11:40:13 +0000 | [diff] [blame] | 121 | "access": "Private" |
| 122 | }, |
| 123 | "move_constructor": { |
| 124 | "definition": "Deleted", |
| 125 | "access": "Protected" |
| 126 | }, |
Devin Jeanpierre | e78b2fb | 2021-10-05 11:40:33 +0000 | [diff] [blame] | 127 | "destructor": { |
| 128 | "definition": "Trivial", |
| 129 | "access": "Public" |
| 130 | }, |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 131 | "is_trivial_abi": true, |
| 132 | "is_final": true |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 133 | }} |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 134 | ] |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 135 | })j"); |
Googler | 6a63857 | 2021-10-07 07:34:13 +0000 | [diff] [blame] | 136 | IR ir = {.used_headers = {HeaderName("foo/bar.h")}, |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 137 | .current_target = Label(std::string("//foo:bar")), |
Googler | 6a63857 | 2021-10-07 07:34:13 +0000 | [diff] [blame] | 138 | .items = {Record{.identifier = Identifier("SomeStruct"), |
Marcel Hlopko | 264b9ad | 2021-12-02 21:06:44 +0000 | [diff] [blame] | 139 | .id = DeclId(42), |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 140 | .owning_target = Label(std::string("//foo:bar")), |
Googler | 6a63857 | 2021-10-07 07:34:13 +0000 | [diff] [blame] | 141 | .fields = |
| 142 | { |
| 143 | Field{ |
| 144 | .identifier = Identifier("public_int"), |
| 145 | .type = |
| 146 | MappedType::Simple("i32", "int"), |
| 147 | .access = kPublic, |
| 148 | .offset = 0}, |
| 149 | Field{.identifier = |
| 150 | Identifier("protected_int"), |
Marcel Hlopko | 80441c1 | 2021-11-12 10:43:18 +0000 | [diff] [blame] | 151 | .type = MappedType::Simple("i32", |
| 152 | "int"), |
Googler | 6a63857 | 2021-10-07 07:34:13 +0000 | [diff] [blame] | 153 | .access = kProtected, |
| 154 | .offset = 32}, |
| 155 | Field{ |
| 156 | .identifier = Identifier("private_int"), |
| 157 | .type = MappedType::Simple("i32", |
| 158 | "int"), |
| 159 | .access = kPrivate, |
| 160 | .offset = 64}, |
| 161 | }, |
| 162 | .size = 12, |
| 163 | .alignment = 4, |
| 164 | .copy_constructor = |
| 165 | SpecialMemberFunc{ |
| 166 | .definition = SpecialMemberFunc:: |
Devin Jeanpierre | 7b62e95 | 2021-12-08 21:43:30 +0000 | [diff] [blame] | 167 | Definition::kNontrivialUserDefined, |
Googler | 6a63857 | 2021-10-07 07:34:13 +0000 | [diff] [blame] | 168 | .access = kPrivate}, |
| 169 | .move_constructor = |
| 170 | SpecialMemberFunc{ |
| 171 | .definition = |
| 172 | SpecialMemberFunc::Definition::kDeleted, |
| 173 | .access = kProtected}, |
| 174 | .destructor = |
| 175 | SpecialMemberFunc{ |
| 176 | .definition = |
| 177 | SpecialMemberFunc::Definition::kTrivial, |
| 178 | .access = kPublic}, |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 179 | .is_trivial_abi = true, |
| 180 | .is_final = true}}}; |
Googler | 4dda73e | 2021-12-03 09:13:42 +0000 | [diff] [blame] | 181 | EXPECT_THAT(ir.ToJson(), EqualsJson(expected)); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Marcel Hlopko | 7aa38a7 | 2021-11-11 07:39:51 +0000 | [diff] [blame] | 184 | TEST(HeaderName, Hash) { |
| 185 | EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({ |
| 186 | HeaderName("foo.h"), |
| 187 | HeaderName("bar.h"), |
| 188 | })); |
| 189 | } |
| 190 | |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 191 | } // namespace |
| 192 | } // namespace rs_bindings_from_cc |