diff options
author | Devin Moore <devinmoore@google.com> | 2022-02-08 19:42:23 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-02-08 19:42:23 +0000 |
commit | 4179eeef1fefb228d47bfab4687bc850f3ed9a35 (patch) | |
tree | 5d90e728aa0b464af0e025c28e5741c29fc90b07 | |
parent | 458417c2bbef397f5164fd45ee3fd8500481564d (diff) | |
parent | 464c01f68ae6166fb7b8f1f4b66250484a94d598 (diff) | |
download | hidl-4179eeef1fefb228d47bfab4687bc850f3ed9a35.tar.gz |
Merge "hidl2aidl: Handle fixed sized arrays" am: aad3f68039 am: 9fd92d23a6 am: 628b53aed8 am: 464c01f68a
Original change: https://android-review.googlesource.com/c/platform/system/tools/hidl/+/1971383
Change-Id: I9d6ad05bf140cf71e128f8c8725e6543c1a03a22
9 files changed, 41 insertions, 12 deletions
diff --git a/hidl2aidl/AidlTranslate.cpp b/hidl2aidl/AidlTranslate.cpp index 7ba907c6..2c23760a 100644 --- a/hidl2aidl/AidlTranslate.cpp +++ b/hidl2aidl/AidlTranslate.cpp @@ -245,7 +245,12 @@ static void containerTranslation(Formatter& out, const FieldWithVersion& field, const std::string inputAccess = "in." + field.fullName + (parent->style() == CompoundType::STYLE_SAFE_UNION ? "()" : ""); if (field.field->type().isArray()) { - elementType = static_cast<const ArrayType*>(field.field->get())->getElementType(); + auto fieldArray = static_cast<const ArrayType*>(field.field->get()); + if (fieldArray->getConstantExpressions()[0]->castSizeT() == 0) { + // Nothing to translate for 0 sized arrays! + return; + } + elementType = fieldArray->getElementType(); javaSizeAccess = inputAccess + ".length"; javaElementAccess = "[i]"; cppSize = "sizeof(" + inputAccess + ")/sizeof(" + inputAccess + "[0])"; @@ -312,10 +317,21 @@ static void containerTranslation(Formatter& out, const FieldWithVersion& field, } else { out << field.field->name(); } - - out << ".push_back(" - << wrapCppSource(inputAccessElement, *elementType, parent->fqName(), backend) - << ");\n"; + // Arrays with explicit size use std::array instead of std::vector + if (field.field->type().isArray() && + static_cast<const ArrayType*>(field.field->get()) + ->getConstantExpressions() + .size() > 0) { + out << "[i] = " + << wrapCppSource(inputAccessElement, *elementType, parent->fqName(), + backend) + << ";\n"; + } else { + out << ".push_back(" + << wrapCppSource(inputAccessElement, *elementType, parent->fqName(), + backend) + << ");\n"; + } }); out << "}\n"; }); diff --git a/hidl2aidl/AidlType.cpp b/hidl2aidl/AidlType.cpp index 7ba9d21b..73e6fa7a 100644 --- a/hidl2aidl/AidlType.cpp +++ b/hidl2aidl/AidlType.cpp @@ -53,7 +53,13 @@ std::string AidlHelper::getAidlType(const Type& type, const FQName& relativeTo, return getAidlType(*vec.getElementType(), relativeTo) + "[]"; } else if (type.isArray()) { const ArrayType& arr = static_cast<const ArrayType&>(type); - return getAidlType(*arr.getElementType(), relativeTo) + "[]"; + auto sizes = arr.getConstantExpressions(); + CHECK(sizes.size() > 0) << "Failed to get array dimensions for " << arr.definedName(); + std::string typeStr = getAidlType(*arr.getElementType(), relativeTo); + for (const auto& size : sizes) { + typeStr += "[" + size->value() + "]"; + } + return typeStr; } else if (type.isNamedType()) { const NamedType& namedType = static_cast<const NamedType&>(type); if (getAidlPackage(relativeTo) == getAidlPackage(namedType.fqName())) { diff --git a/hidl2aidl/test/1.2/types.hal b/hidl2aidl/test/1.2/types.hal index e12c8adc..9de344db 100644 --- a/hidl2aidl/test/1.2/types.hal +++ b/hidl2aidl/test/1.2/types.hal @@ -67,6 +67,8 @@ struct ArrayFoo { uint32_t[12] b; @1.1::Value[12] c; string[2] d; + int8_t[0] e; + int8_t[@1.1::Value:E + 1] f; }; struct VectorFoo { diff --git a/hidl2aidl/test/TranslateJavaTest.java b/hidl2aidl/test/TranslateJavaTest.java index d32262de..32c06f2e 100644 --- a/hidl2aidl/test/TranslateJavaTest.java +++ b/hidl2aidl/test/TranslateJavaTest.java @@ -286,6 +286,7 @@ public class TranslateJavaTest { hidl2aidl.test.V1_2.ArrayFoo source = new hidl2aidl.test.V1_2.ArrayFoo(); source.a[0] = 42; source.a[0] = 42; + assertThat(source.e.length, is(0)); dest = Translate.h2aTranslate(source); assertThat(source.a[0], is(dest.a[0])); } diff --git a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/ArrayFoo.aidl b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/ArrayFoo.aidl index 1dce1a20..467085f0 100644 --- a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/ArrayFoo.aidl +++ b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/ArrayFoo.aidl @@ -20,8 +20,10 @@ package hidl2aidl.test; @VintfStability parcelable ArrayFoo { - byte[] a; - int[] b; - hidl2aidl.test.Value[] c; - String[] d; + byte[12] a; + int[12] b; + hidl2aidl.test.Value[12] c; + String[2] d; + byte[0] e; + byte[28] f; } diff --git a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/SafeUnionBar.aidl b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/SafeUnionBar.aidl index b49a082a..2cba44fc 100644 --- a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/SafeUnionBar.aidl +++ b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/SafeUnionBar.aidl @@ -33,7 +33,7 @@ union SafeUnionBar { char i; byte[] j; hidl2aidl.test.Value[] k; - byte[] l; + byte[2] l; float m; @VintfStability parcelable InnerStructBar { diff --git a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/extension/ArrayFoo.aidl b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/extension/ArrayFoo.aidl index 4fd7104d..9edc5bdb 100644 --- a/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/extension/ArrayFoo.aidl +++ b/hidl2aidl/test/aidl_api/hidl2aidl_test_gen/current/hidl2aidl/test/extension/ArrayFoo.aidl @@ -21,5 +21,5 @@ package hidl2aidl.test.extension; @VintfStability parcelable ArrayFoo { hidl2aidl.test.ArrayFoo base; - byte[] e; + byte[12] e; } diff --git a/hidl2aidl/test/translate_cpp_test.cpp b/hidl2aidl/test/translate_cpp_test.cpp index 6ba4e094..19a5024e 100644 --- a/hidl2aidl/test/translate_cpp_test.cpp +++ b/hidl2aidl/test/translate_cpp_test.cpp @@ -241,6 +241,7 @@ TEST_F(Hidl2aidlTranslateTest, ArrayFoo) { hidl2aidl::test::V1_2::ArrayFoo source; source.a[0] = 42; source.a[1] = 8; + ASSERT_EQ(sizeof(source.e), 0u); ASSERT_TRUE(h2a::translate(source, &dest)); ASSERT_EQ(12u, dest.a.size()); EXPECT_EQ(source.a[0], dest.a[0]); diff --git a/hidl2aidl/test/translate_ndk_test.cpp b/hidl2aidl/test/translate_ndk_test.cpp index 5913a017..9b72135a 100644 --- a/hidl2aidl/test/translate_ndk_test.cpp +++ b/hidl2aidl/test/translate_ndk_test.cpp @@ -241,6 +241,7 @@ TEST_F(Hidl2aidlTranslateTest, ArrayFoo) { hidl2aidl::test::V1_2::ArrayFoo source; source.a[0] = 42; source.a[1] = 8; + ASSERT_EQ(sizeof(source.e), 0u); ASSERT_TRUE(h2a::translate(source, &dest)); ASSERT_EQ(12u, dest.a.size()); EXPECT_EQ(source.a[0], dest.a[0]); |