diff options
author | Jooyung Han <jooyung@google.com> | 2021-01-07 14:12:40 +0900 |
---|---|---|
committer | Jooyung Han <jooyung@google.com> | 2021-01-07 08:16:30 +0000 |
commit | 633eab645d55bcaf9df97b126b6033529311adcb (patch) | |
tree | 8cd3fcec9f4ad7ea8563daab03475ea53a85424c | |
parent | 720253dc96d6d6b48b5490d6f0dbb8e9e8bc69c6 (diff) | |
download | aidl-633eab645d55bcaf9df97b126b6033529311adcb.tar.gz |
Support annotations for 'const'
It was not supported and people use type-level annotations as a
workaround even when the annotations are meant to annotate constants.
const @Annotation... int foo = 0;
Now we can annotate constants like following.
@Annotation... const int foo = 0;
Bug: 174436374
Test: aidl_unittests
Change-Id: I5481de6ac48711765fcd7e1726003267360ba9e6
-rw-r--r-- | aidl_language_y.yy | 15 | ||||
-rw-r--r-- | aidl_unittest.cpp | 16 |
2 files changed, 27 insertions, 4 deletions
diff --git a/aidl_language_y.yy b/aidl_language_y.yy index 8273815d..64d9f437 100644 --- a/aidl_language_y.yy +++ b/aidl_language_y.yy @@ -519,11 +519,18 @@ constant_value_non_empty_list ; constant_decl - : CONST type identifier '=' const_expr ';' { - $2->SetComments($1->GetComments()); - $$ = new AidlConstantDeclaration(loc(@3), $2, $3->GetText(), $5); + : annotation_list CONST type identifier '=' const_expr ';' { + if ($1->size() > 0) { + $3->SetComments($1->begin()->GetComments()); + } else { + $3->SetComments($2->GetComments()); + } + // TODO(b/151102494) do not merge annotations. + $3->Annotate(std::move(*$1)); + $$ = new AidlConstantDeclaration(loc(@4), $3, $4->GetText(), $6); delete $1; - delete $3; + delete $2; + delete $4; } ; diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp index 3880f798..4d77f846 100644 --- a/aidl_unittest.cpp +++ b/aidl_unittest.cpp @@ -1542,6 +1542,22 @@ TEST_P(AidlTest, UnionWithConstants) { EXPECT_EQ("", GetCapturedStderr()); } +TEST_F(AidlTest, ConstantsWithAnnotations) { + io_delegate_.SetFileContents("IFoo.aidl", + "interface IFoo {\n" + " @JavaPassthrough(annotation=\"@Foo\")\n" + " const @JavaPassthrough(annotation=\"@Bar\") int FOO = 0;\n" + "}"); + Options options = Options::From("aidl IFoo.aidl --lang=java -o out"); + CaptureStderr(); + EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_)); + EXPECT_EQ("", GetCapturedStderr()); + string code; + EXPECT_TRUE(io_delegate_.GetWrittenContents("out/IFoo.java", &code)); + EXPECT_THAT(code, HasSubstr("@Foo\n")); + EXPECT_THAT(code, HasSubstr("@Bar\n")); +} + TEST_F(AidlTest, ApiDump) { io_delegate_.SetFileContents( "foo/bar/IFoo.aidl", |