aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeongik Cha <jeongik@google.com>2021-09-25 01:19:56 +0900
committerJeongik Cha <jeongik@google.com>2021-09-27 01:48:01 +0000
commitf2e752316b0d9d2708bc56d20c1649e704bca030 (patch)
treeb5ae9acae07c5aaf5cc07daf67f49a457484a493
parentee71eb3328a37d83111c527643f89af2b98367a8 (diff)
downloadaidl-f2e752316b0d9d2708bc56d20c1649e704bca030.tar.gz
An enum is initialized as zero if it doesn't have default value
In C++, NDK backend, an enum value hasn't been initialized if it doesn't have default value. In that case, initialize an enum with zero for the backends. Bug: 198346478 Test: check golden output and m Ignore-AOSP-First: security patch Change-Id: I6cb67258cc4aacc41ae80c46ccee10a3cfe723f6 Merged-In: I6cb67258cc4aacc41ae80c46ccee10a3cfe723f6 (cherry picked from commit f39b16442ad832f2921ad0eec298b7c333936dc1)
-rw-r--r--generate_cpp.cpp7
-rw-r--r--generate_ndk.cpp7
2 files changed, 14 insertions, 0 deletions
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index cc256573..bbee9b3a 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -1111,6 +1111,13 @@ std::unique_ptr<Document> BuildParcelHeader(const AidlTypenames& typenames,
if (variable->GetDefaultValue()) {
out << " = " << cppType.c_str() << "(" << variable->ValueString(ConstantValueDecorator)
<< ")";
+ } else if (auto type = typenames.TryGetDefinedType(variable->GetType().GetName()); type) {
+ if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
+ if (!variable->GetType().IsArray()) {
+ // if an enum doesn't have explicit default value, do zero-initialization
+ out << " = " << cppType << "(0)";
+ }
+ }
}
out << ";\n";
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index 274d283f..0efd7eb7 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -879,6 +879,13 @@ void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
if (variable->GetDefaultValue()) {
out << " = " << variable->ValueString(ConstantValueDecorator);
+ } else if (auto type = types.TryGetDefinedType(variable->GetType().GetName()); type) {
+ if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
+ if (!variable->GetType().IsArray()) {
+ // if an enum doesn't have explicit default value, do zero-initialization
+ out << " = " << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << "(0)";
+ }
+ }
}
out << ";\n";
}