Validate @Enforce expression earlier

As suggested by Jooyung in a previous change, validate the expression
when parsing the interface. Add unit tests to confirm that the expected
errors are raised for empty or malformed expressions. With this
implementation, the condition expression is parsed twice (once when
loaded and once when generating the Java code).

Test: out/host/linux-x86/nativetest64/aidl_unittests/aidl_unittests
Bug: 197828948
Change-Id: I8d6008c33b17dc1b0e473c6299cd601f86d9629a
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 6c8f476..10268a8 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -3388,6 +3388,48 @@
   EXPECT_TRUE(compile_aidl(java_options, io_delegate_));
 }
 
+TEST_F(AidlTest, EmptyEnforceAnnotation) {
+  const string expected_stderr = "ERROR: a/IFoo.aidl:3.1-19: Missing 'condition' on @Enforce.\n";
+  io_delegate_.SetFileContents("a/IFoo.aidl", R"(package a;
+    interface IFoo {
+        @Enforce()
+        void Protected();
+    })");
+
+  Options options = Options::From("aidl --lang=java -o out a/IFoo.aidl");
+  CaptureStderr();
+  EXPECT_FALSE(compile_aidl(options, io_delegate_));
+  EXPECT_EQ(expected_stderr, GetCapturedStderr());
+}
+
+TEST_F(AidlTest, EmptyEnforceCondition) {
+  io_delegate_.SetFileContents("a/IFoo.aidl", R"(package a;
+    interface IFoo {
+        @Enforce(condition="")
+        void Protected();
+    })");
+
+  Options options = Options::From("aidl --lang=java -o out a/IFoo.aidl");
+  CaptureStderr();
+  EXPECT_FALSE(compile_aidl(options, io_delegate_));
+  EXPECT_THAT(GetCapturedStderr(),
+              HasSubstr("ERROR: a/IFoo.aidl:3.1-31: Unable to parse @Enforce annotation"));
+}
+
+TEST_F(AidlTest, InvalidEnforceCondition) {
+  io_delegate_.SetFileContents("a/IFoo.aidl", R"(package a;
+    interface IFoo {
+        @Enforce(condition="invalid")
+        void Protected();
+    })");
+
+  Options options = Options::From("aidl --lang=java -o out a/IFoo.aidl");
+  CaptureStderr();
+  EXPECT_FALSE(compile_aidl(options, io_delegate_));
+  EXPECT_THAT(GetCapturedStderr(),
+              HasSubstr("ERROR: a/IFoo.aidl:3.1-38: Unable to parse @Enforce annotation"));
+}
+
 class AidlOutputPathTest : public AidlTest {
  protected:
   void SetUp() override {