add -Wenum-zero warning.
This checks if enum types have 0 as its first enumerator.
For now, enum-type fields are initialized as 0 by default constructor.
To prevent invalid-valued enum-type fields, we recommend to set the
first enumerator as zero for enum types.
We could fix how default constructors initialize enum-type fields. But
this might break legacy codebase.
Bug: 168028537
Bug: 175841271
Test: aidl_unittests
Change-Id: Icb1d2949bb9fa2dd30e7620feffac1100f48114c
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index b829f6b..413be99 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -4332,6 +4332,16 @@
GetCapturedStderr());
}
+TEST_P(AidlTest, ErrorEnumZero) {
+ io_delegate_.SetFileContents("p/Foo.aidl", "enum Foo { FOO = 1 }");
+ auto options = Options::From("aidl --lang " + Options::LanguageToString(GetLanguage()) +
+ " -Weverything -Werror -o out -h out p/Foo.aidl");
+ CaptureStderr();
+ EXPECT_EQ(1, aidl::compile_aidl(options, io_delegate_));
+ EXPECT_EQ("ERROR: p/Foo.aidl:1.11-15: The first enumerator 'FOO' should be 0, but it is 1.\n",
+ GetCapturedStderr());
+}
+
struct TypeParam {
string kind;
string literal;