diff options
author | Steven Moreland <smoreland@google.com> | 2021-10-12 15:25:11 -0700 |
---|---|---|
committer | Steven Moreland <smoreland@google.com> | 2021-10-13 15:57:49 +0000 |
commit | c29d717c12986564e33c896cccc2560615c83bce (patch) | |
tree | afceb6b56b5e2f021cbf143df8a57fe1ed11f605 | |
parent | c63e8ffa820e2b11c7fd2c4bdcbb0c88cb873366 (diff) | |
download | aidl-c29d717c12986564e33c896cccc2560615c83bce.tar.gz |
Fix Java comment format.
TrimmedLines was copied from hidl-gen to do this, but we weren't using
it in the output.
For simplicity, this converts a single-line block comment to a
multi-line block comment.
Fixes: 202563750
Test: goldens
Change-Id: I2130a32702fb0bc87156363d9dc0e7e7edb57621
-rw-r--r-- | aidl_unittest.cpp | 18 | ||||
-rw-r--r-- | comments.cpp | 27 | ||||
-rw-r--r-- | generate_java.cpp | 6 | ||||
-rw-r--r-- | tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ByteEnum.java | 4 | ||||
-rw-r--r-- | tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ITestService.java | 46 |
5 files changed, 48 insertions, 53 deletions
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp index 78739b44..fd64309f 100644 --- a/aidl_unittest.cpp +++ b/aidl_unittest.cpp @@ -4630,22 +4630,18 @@ TEST_F(AidlTest, FormatCommentsForJava) { {{{"/*\n" " * Hello, world!\n" " */"}}, - "/**\n" - " * Hello, world!\n" - " */"}, - {{{"/* @hide */"}}, "/** @hide */"}, + "/** Hello, world! */\n"}, + {{{"/* @hide */"}}, "/** @hide */\n"}, {{{"/**\n" " @param foo ...\n" "*/"}}, - "/**\n" - " @param foo ...\n" - "*/"}, - {{{"/* @hide */"}, {"/* @hide */"}}, "/* @hide *//** @hide */"}, + "/** @param foo ... */\n"}, + {{{"/* @hide */"}, {"/* @hide */"}}, "/* @hide */\n/** @hide */\n"}, {{{"/* @deprecated first */"}, {"/* @deprecated second */"}}, - "/* @deprecated first *//** @deprecated second */"}, - {{{"/* @deprecated */"}, {"/** @param foo */"}}, "/* @deprecated *//** @param foo */"}, + "/* @deprecated first */\n/** @deprecated second */\n"}, + {{{"/* @deprecated */"}, {"/** @param foo */"}}, "/* @deprecated */\n/** @param foo */\n"}, // Line comments are printed as they are - {{{"/* @deprecated */"}, {"// line comments\n"}}, "/* @deprecated */// line comments\n"}, + {{{"/* @deprecated */"}, {"// line comments\n"}}, "/* @deprecated */\n// line comments\n"}, }; for (const auto& [input, formatted] : testcases) { EXPECT_EQ(formatted, FormatCommentsForJava(input)); diff --git a/comments.cpp b/comments.cpp index afbdc8f8..8feee285 100644 --- a/comments.cpp +++ b/comments.cpp @@ -40,6 +40,7 @@ namespace { static const std::string_view kLineCommentBegin = "//"; static const std::string_view kBlockCommentBegin = "/*"; +static const std::string_view kBlockCommentMid = " *"; static const std::string_view kBlockCommentEnd = "*/"; static const std::string_view kDocCommentBegin = "/**"; static const std::string kTagDeprecated = "@deprecated"; @@ -222,15 +223,31 @@ 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); + auto lines = TrimmedLines(*it); + + if (it->type == Comment::Type::LINE) { + for (const auto& line : lines) { + out << kLineCommentBegin << line; + } } else { - out << it->body; + if (last || StartsWith(it->body, kDocCommentBegin)) { + out << kDocCommentBegin; + } else { + out << kBlockCommentBegin; + } + bool multiline = lines.size() > 1; + + if (multiline) out << "\n"; + for (const auto& line : lines) { + if (multiline) out << kBlockCommentMid; + out << " " << line; + if (multiline) out << "\n"; + } + out << " " << kBlockCommentEnd << "\n"; } } return out.str(); } } // namespace aidl -} // namespace android
\ No newline at end of file +} // namespace android diff --git a/generate_java.cpp b/generate_java.cpp index 6e96b809..52c92d56 100644 --- a/generate_java.cpp +++ b/generate_java.cpp @@ -290,11 +290,7 @@ namespace aidl { namespace java { std::string GenerateComments(const AidlCommentable& node) { - std::string comments = FormatCommentsForJava(node.GetComments()); - if (!comments.empty() && comments.back() != '\n') { - comments += '\n'; - } - return comments; + return FormatCommentsForJava(node.GetComments()); } std::string GenerateAnnotations(const AidlNode& node) { 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 0f5a3ea0..42fe1ed3 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,9 +2,7 @@ * This file is auto-generated. DO NOT MODIFY. */ package android.aidl.tests; -/** - * Hello, world! - */ +/** Hello, world! */ public @interface ByteEnum { // Comment about FOO. public static final byte FOO = 1; diff --git a/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ITestService.java b/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ITestService.java index 490450e0..5d8af5a4 100644 --- a/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ITestService.java +++ b/tests/golden_output/aidl-test-interface-java-source/gen/android/aidl/tests/ITestService.java @@ -2,9 +2,7 @@ * This file is auto-generated. DO NOT MODIFY. */ package android.aidl.tests; -/** - * interface comment - */ +/** interface comment */ public interface ITestService extends android.os.IInterface { /** Default implementation for ITestService. */ @@ -26,9 +24,9 @@ public interface ITestService extends android.os.IInterface return 0; } /** - * @deprecated to make sure we have something in system/tools/aidl which does a compile check - * of deprecated and make sure this is reflected in goldens - */ + * @deprecated to make sure we have something in system/tools/aidl which does a compile check + * of deprecated and make sure this is reflected in goldens + */ @Override public void Deprecated() throws android.os.RemoteException { } @@ -207,9 +205,7 @@ public interface ITestService extends android.os.IInterface { return null; } - /** - * comment before annotation - */ + /** comment before annotation */ @Override public android.aidl.tests.INamedCallback GetCallback(boolean return_null) throws android.os.RemoteException { return null; @@ -999,9 +995,9 @@ public interface ITestService extends android.os.IInterface return _result; } /** - * @deprecated to make sure we have something in system/tools/aidl which does a compile check - * of deprecated and make sure this is reflected in goldens - */ + * @deprecated to make sure we have something in system/tools/aidl which does a compile check + * of deprecated and make sure this is reflected in goldens + */ @Override public void Deprecated() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(asBinder()); @@ -2173,9 +2169,7 @@ public interface ITestService extends android.os.IInterface } return _result; } - /** - * comment before annotation - */ + /** comment before annotation */ @Override public android.aidl.tests.INamedCallback GetCallback(boolean return_null) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(asBinder()); @@ -2466,14 +2460,10 @@ public interface ITestService extends android.os.IInterface } public static final java.lang.String DESCRIPTOR = "android$aidl$tests$ITestService".replace('$', '.'); // Test that constants are accessible - /** - * extra doc comment - */// extra line comment - /* - * extra regular comment - *//** - * const comment - */ + /** extra doc comment */ + // extra line comment + /* extra regular comment */ + /** const comment */ public static final int TEST_CONSTANT = 42; public static final int TEST_CONSTANT2 = -42; public static final int TEST_CONSTANT3 = 42; @@ -2564,9 +2554,9 @@ public interface ITestService extends android.os.IInterface // methods to be added and removed. public int UnimplementedMethod(int arg) throws android.os.RemoteException; /** - * @deprecated to make sure we have something in system/tools/aidl which does a compile check - * of deprecated and make sure this is reflected in goldens - */ + * @deprecated to make sure we have something in system/tools/aidl which does a compile check + * of deprecated and make sure this is reflected in goldens + */ @Deprecated public void Deprecated() throws android.os.RemoteException; public void TestOneway() throws android.os.RemoteException; @@ -2619,9 +2609,7 @@ public interface ITestService extends android.os.IInterface public java.lang.String[] ReverseUtf8CppString(java.lang.String[] input, java.lang.String[] repeated) throws android.os.RemoteException; public java.lang.String[] ReverseNullableUtf8CppString(java.lang.String[] input, java.lang.String[] repeated) throws android.os.RemoteException; public java.util.List<java.lang.String> ReverseUtf8CppStringList(java.util.List<java.lang.String> input, java.util.List<java.lang.String> repeated) throws android.os.RemoteException; - /** - * comment before annotation - */ + /** comment before annotation */ public android.aidl.tests.INamedCallback GetCallback(boolean return_null) throws android.os.RemoteException; // Since this paracelable has clearly defined default values, it would be // inefficient to use an IPC to fill it out in practice. |