union: __assert2() when a tag mismatches
Rather than abort()ing without any message, __assert2() will leave a bit
of hints.
- bad access: get<tag>() with a wrong tag.
- can't reach here: writeToParcel() with an unknown tag. (in practice,
this won't happen though)
Bug: 170681273
Test: aidl_integration_test
Change-Id: I85a872d602720542657ca1cbb1c931a3e6d15749
diff --git a/aidl_to_cpp_common.cpp b/aidl_to_cpp_common.cpp
index 6ac1d30..31b44ba 100644
--- a/aidl_to_cpp_common.cpp
+++ b/aidl_to_cpp_common.cpp
@@ -422,6 +422,7 @@
}
const vector<string> UnionWriter::headers{
+ "cassert", // __assert for logging
"type_traits", // std::is_same_v
"utility", // std::mode/forward for value
"variant", // std::variant for value
@@ -491,13 +492,13 @@
template <Tag _tag>
const auto& get() const {{
- if (getTag() != _tag) {{ abort(); }}
+ if (getTag() != _tag) {{ __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }}
return std::get<_tag>(_value);
}}
template <Tag _tag>
auto& get() {{
- if (getTag() != _tag) {{ abort(); }}
+ if (getTag() != _tag) {{ __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, "bad access: a wrong tag"); }}
return std::get<_tag>(_value);
}}
@@ -575,7 +576,7 @@
out << ";\n";
}
out << "}\n";
- out << "abort();\n";
+ out << "__assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, \"can't reach here\");\n";
}
} // namespace cpp