Add automatic default value for primitive type fields
Primitive type fields are auto-initialized with '\0' when not specified.
(This is a non-trivial cherry-pick of
f04d003b4f5fa9f8ee73b6b560c897de45e644cd)
Bug: 206718630
Bug: 206718630
Test: aidl --lang cpp -o out -h out \
tests/android/aidl/tests/StructuredParcelable.aidl
Test: aidl --lang ndk -o out -h out \
tests/android/aidl/tests/StructuredParcelable.aidl
Merged-In: I15cfe2a90c0c9adca1692d9a9c997901b90bbbb6
Change-Id: I15cfe2a90c0c9adca1692d9a9c997901b90bbbb6
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index bbee9b3..b9b2d12 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -1118,6 +1118,9 @@
out << " = " << cppType << "(0)";
}
}
+ } else if (AidlTypenames::IsPrimitiveTypename(variable->GetType().GetName()) &&
+ !variable->GetType().IsArray()) {
+ out << " = {}";
}
out << ";\n";
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index 0efd7eb..d8a29aa 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -886,6 +886,9 @@
out << " = " << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << "(0)";
}
}
+ } else if (AidlTypenames::IsPrimitiveTypename(variable->GetType().GetName()) &&
+ !variable->GetType().IsArray()) {
+ out << " = {}";
}
out << ";\n";
}
diff --git a/tests/android/aidl/tests/StructuredParcelable.aidl b/tests/android/aidl/tests/StructuredParcelable.aidl
index 28046e2..0908782 100644
--- a/tests/android/aidl/tests/StructuredParcelable.aidl
+++ b/tests/android/aidl/tests/StructuredParcelable.aidl
@@ -158,5 +158,16 @@
// String expressions
@utf8InCpp String addString1 = "hello" + " world!";
@utf8InCpp String addString2 = "The quick brown fox jumps " + "over the lazy dog.";
+
+ // primitive types will be initialized with {}.
+ boolean aBoolean;
+ byte aByte;
+ char aChar;
+ int anInt;
+ long aLong;
+ float aFloat;
+ double aDouble;
+ // arrays are not primitive type.
+ int[] anIntArray;
}