aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aidl_unittest.cpp42
-rw-r--r--comments.cpp18
-rw-r--r--comments.h2
-rw-r--r--generate_java.cpp5
-rw-r--r--generate_rust.cpp2
-rw-r--r--tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java2
6 files changed, 62 insertions, 9 deletions
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 7d8b1c80..6d67b7da 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -801,8 +801,7 @@ TEST_P(AidlTest, SupportDeprecated) {
"}",
{
{Options::Language::JAVA, {"out/IFoo.java", "@Deprecated"}},
- // TODO(b/177276893) @deprecated should be in javadoc style comments
- // {Options::Language::JAVA, {"out/IFoo.java", "/** @deprecated use bar() */"}},
+ {Options::Language::JAVA, {"out/IFoo.java", "/** @deprecated use bar() */"}},
{Options::Language::CPP, {"out/IFoo.h", "__attribute__((deprecated(\"use bar()\")))"}},
{Options::Language::NDK,
{"out/aidl/IFoo.h", "__attribute__((deprecated(\"use bar()\")))"}},
@@ -890,7 +889,7 @@ TEST_P(AidlTest, SupportDeprecated) {
{Options::Language::JAVA, {"out/Foo.java", "@Deprecated"}},
{Options::Language::CPP, {"out/Foo.h", "__attribute__((deprecated"}},
{Options::Language::NDK, {"out/aidl/Foo.h", "__attribute__((deprecated"}},
- // TODO(b/174514415) support "deprecated"
+ // TODO(b/177860423) support "deprecated" in Rust enum
// {Options::Language::RUST, {"out/Foo.rs", "#[deprecated"}},
});
}
@@ -3666,6 +3665,43 @@ TEST_P(AidlTest, ErrorInterfaceName) {
GetCapturedStderr());
}
+TEST_F(AidlTest, FormatCommentsForJava) {
+ using android::aidl::FormatCommentsForJava;
+
+ struct TestCase {
+ vector<Comment> comments;
+ string formatted;
+ };
+ vector<TestCase> testcases = {
+ {{}, ""},
+ {{{"// line comments\n"}}, "// line comments\n"},
+ {{{"// @hide \n"}}, "// @hide \n"},
+ // Transform the last block comment as Javadoc.
+ {{{"/*\n"
+ " * Hello, world!\n"
+ " */"}},
+ "/**\n"
+ " * Hello, world!\n"
+ " */"},
+ {{{"/* @hide */"}}, "/** @hide */"},
+ {{{"/**\n"
+ " @param foo ...\n"
+ "*/"}},
+ "/**\n"
+ " @param foo ...\n"
+ "*/"},
+ {{{"/* @hide */"}, {"/* @hide */"}}, "/* @hide *//** @hide */"},
+ {{{"/* @deprecated first */"}, {"/* @deprecated second */"}},
+ "/* @deprecated first *//** @deprecated second */"},
+ {{{"/* @deprecated */"}, {"/** @param foo */"}}, "/* @deprecated *//** @param foo */"},
+ // Line comments are printed as they are
+ {{{"/* @deprecated */"}, {"// line comments\n"}}, "/* @deprecated */// line comments\n"},
+ };
+ for (const auto& [input, formatted] : testcases) {
+ EXPECT_EQ(formatted, FormatCommentsForJava(input));
+ }
+}
+
TEST_F(AidlTest, HideIsNotForArgs) {
io_delegate_.SetFileContents("IFoo.aidl",
"interface IFoo {\n"
diff --git a/comments.cpp b/comments.cpp
index 4b724b2d..afbdc8f8 100644
--- a/comments.cpp
+++ b/comments.cpp
@@ -41,6 +41,7 @@ namespace {
static const std::string_view kLineCommentBegin = "//";
static const std::string_view kBlockCommentBegin = "/*";
static const std::string_view kBlockCommentEnd = "*/";
+static const std::string_view kDocCommentBegin = "/**";
static const std::string kTagDeprecated = "@deprecated";
static const std::regex kTagHideRegex{"@hide\\b"};
@@ -214,5 +215,22 @@ std::optional<Deprecated> FindDeprecated(const Comments& comments) {
return std::nullopt;
}
+// Formats comments for the Java backend.
+// The last/block comment is transformed into javadoc(/** */)
+// and others are used as they are.
+std::string FormatCommentsForJava(const Comments& comments) {
+ std::stringstream out;
+ for (auto it = begin(comments); it != end(comments); it++) {
+ const bool last = next(it) == end(comments);
+ // We only re-format the last/block comment which is not already a doc-style comment.
+ if (last && it->type == Comment::Type::BLOCK && !StartsWith(it->body, kDocCommentBegin)) {
+ out << kDocCommentBegin << ConsumePrefix(it->body, kBlockCommentBegin);
+ } else {
+ out << it->body;
+ }
+ }
+ return out.str();
+}
+
} // namespace aidl
} // namespace android \ No newline at end of file
diff --git a/comments.h b/comments.h
index 6814e60b..8bf7b30f 100644
--- a/comments.h
+++ b/comments.h
@@ -48,5 +48,7 @@ struct Deprecated {
std::optional<Deprecated> FindDeprecated(const Comments& comments);
+std::string FormatCommentsForJava(const Comments& comments);
+
} // namespace aidl
} // namespace android \ No newline at end of file
diff --git a/generate_java.cpp b/generate_java.cpp
index 58409324..17756c91 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -290,10 +290,7 @@ namespace aidl {
namespace java {
std::string GenerateComments(const AidlCommentable& node) {
- std::string comments;
- for (const auto& c : node.GetComments()) {
- comments += c.body;
- }
+ std::string comments = FormatCommentsForJava(node.GetComments());
if (!comments.empty() && comments.back() != '\n') {
comments += '\n';
}
diff --git a/generate_rust.cpp b/generate_rust.cpp
index 7d0e4825..372d30ff 100644
--- a/generate_rust.cpp
+++ b/generate_rust.cpp
@@ -765,7 +765,7 @@ bool GenerateRustEnumDeclaration(const string& filename, const AidlEnumDeclarati
const auto& aidl_backing_type = enum_decl->GetBackingType();
auto backing_type = RustNameOf(aidl_backing_type, typenames, StorageMode::VALUE);
- // TODO(b/174514415) support "deprecated" for enum types
+ // TODO(b/177860423) support "deprecated" for enum types
*code_writer << "#![allow(non_upper_case_globals)]\n";
*code_writer << "use binder::declare_binder_enum;\n";
*code_writer << "declare_binder_enum! { " << enum_decl->GetName() << " : " << backing_type
diff --git a/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java b/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java
index 1877fe48..0f5a3ea0 100644
--- a/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java
+++ b/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java
@@ -2,7 +2,7 @@
* This file is auto-generated. DO NOT MODIFY.
*/
package android.aidl.tests;
-/*
+/**
* Hello, world!
*/
public @interface ByteEnum {