hidl: keep track of comment format

Mainly, to cleanup hidl2aidl conversion log:
/**
 * // Note that while the previous type had a version suffix, this type does not. This is because the
 */
/**
 * // versions are already present in the namespace and thus don't need to also be embedded in the
 */
/**
 * // name of the type.
 */

=>

 // Note that while the previous type had a version suffix, this type does not. This is because the
 // versions are already present in the namespace and thus don't need to also be embedded in the
 // name of the type.

Bug: N/A
Test: hidl2aidl, hidl_format_test
Change-Id: I1a52b12e0e060137ec7b8aa866c469b6991e7c51
diff --git a/DocComment.cpp b/DocComment.cpp
index 02b7ab7..daa16ea 100644
--- a/DocComment.cpp
+++ b/DocComment.cpp
@@ -16,6 +16,7 @@
 
 #include "DocComment.h"
 
+#include <android-base/logging.h>
 #include <android-base/strings.h>
 #include <hidl-util/StringHelper.h>
 
@@ -26,7 +27,8 @@
 
 namespace android {
 
-DocComment::DocComment(const std::string& comment, const Location& location) : mLocation(location) {
+DocComment::DocComment(const std::string& comment, const Location& location, CommentType type)
+    : mType(type), mLocation(location) {
     std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
 
     bool foundFirstLine = false;
@@ -64,23 +66,36 @@
 }
 
 void DocComment::emit(Formatter& out, CommentType type) const {
-    switch (type) {
-        case CommentType::DOC_MULTILINE:
-            out << "/**\n";
-            break;
-        case CommentType::MULTILINE:
-            out << "/*\n";
-            break;
-    }
+    CommentType useType = type;
+    if (useType == CommentType::UNSPECIFIED) useType = mType;
+    if (useType == CommentType::UNSPECIFIED) useType = CommentType::DOC_MULTILINE;
 
-    out.pushLinePrefix(" *");
+    bool isMultiline = useType != CommentType::SINGLELINE;
+
+    // singleline comments include '//' as part of line text
+    if (isMultiline) {
+        switch (useType) {
+            case CommentType::DOC_MULTILINE:
+                out << "/**\n";
+                break;
+            case CommentType::MULTILINE:
+                out << "/*\n";
+                break;
+            default:
+                LOG(FATAL) << "bad type: " << static_cast<int>(useType);
+        }
+
+        out.pushLinePrefix(" *");
+    }
 
     for (const std::string& line : mLines) {
-        out << (line.empty() ? "" : " ") << line << "\n";
+        out << (line.empty() && isMultiline ? "" : " ") << line << "\n";
     }
 
-    out.popLinePrefix();
-    out << " */\n";
+    if (isMultiline) {
+        out.popLinePrefix();
+        out << " */\n";
+    }
 }
 
 }  // namespace android