Change (un)setLinePrefix to push and pop

This allows stacking multiple prefixes together.

Bug: N/A
Test: mma
Test: ./test/run_all_host_tests.sh
Test: ./test/run_all_device_tests.sh
Change-Id: I132c6527ffc4a090ebc4acf3773f3118e50b40ba
diff --git a/DocComment.cpp b/DocComment.cpp
index 2a1c981..02b7ab7 100644
--- a/DocComment.cpp
+++ b/DocComment.cpp
@@ -73,13 +73,13 @@
             break;
     }
 
-    out.setLinePrefix(" *");
+    out.pushLinePrefix(" *");
 
     for (const std::string& line : mLines) {
         out << (line.empty() ? "" : " ") << line << "\n";
     }
 
-    out.unsetLinePrefix();
+    out.popLinePrefix();
     out << " */\n";
 }
 
diff --git a/c2hal/Note.cpp b/c2hal/Note.cpp
index 292bb79..bbb841f 100644
--- a/c2hal/Note.cpp
+++ b/c2hal/Note.cpp
@@ -34,7 +34,7 @@
 }
 
 void Note::generateSource(Formatter &out) const {
-    out.setLinePrefix("//");
+    out.pushLinePrefix("//");
     out << "NOTE:\n";
 
     out.indent();
@@ -45,7 +45,7 @@
     }
     out.unindent();
 
-    out.unsetLinePrefix();
+    out.popLinePrefix();
     out << "\n";
 }
 
diff --git a/generateCppImpl.cpp b/generateCppImpl.cpp
index 278334d..bad6151 100644
--- a/generateCppImpl.cpp
+++ b/generateCppImpl.cpp
@@ -144,7 +144,7 @@
         generateStubImplMethod(out, baseName, method);
     });
 
-    out.setLinePrefix("//");
+    out.pushLinePrefix("//");
     out << iface->definedName() << "* ";
     generateFetchSymbol(out, iface->definedName());
     out << "(const char* /* name */) {\n";
@@ -152,7 +152,7 @@
     out << "return new " << baseName << "();\n";
     out.unindent();
     out << "}\n\n";
-    out.unsetLinePrefix();
+    out.popLinePrefix();
 
     out << "}  // namespace implementation\n";
     enterLeaveNamespace(out, false /* leave */);
diff --git a/hidl2aidl/AidlNamedType.cpp b/hidl2aidl/AidlNamedType.cpp
index 397b7d2..184352c 100644
--- a/hidl2aidl/AidlNamedType.cpp
+++ b/hidl2aidl/AidlNamedType.cpp
@@ -24,11 +24,11 @@
 namespace android {
 
 static void emitConversionNotes(Formatter& out, const NamedType& namedType) {
-    out << "// Check the conversion.log file to see the hidl definition.\n";
-
-    AidlHelper::notes() << "This is the HIDL definition of " << namedType.fqName().string() << "\n";
-    namedType.emitHidlDefinition(AidlHelper::notes());
-    AidlHelper::notes() << "\n";
+    out << "// This is the HIDL definition of " << namedType.fqName().string() << "\n";
+    out.pushLinePrefix("// ");
+    namedType.emitHidlDefinition(out);
+    out.popLinePrefix();
+    out << "\n";
 }
 
 static void emitTypeDefAidlDefinition(Formatter& out, const TypeDef& typeDef) {
diff --git a/host_utils/Formatter.cpp b/host_utils/Formatter.cpp
index 24d690c..c801cf7 100644
--- a/host_utils/Formatter.cpp
+++ b/host_utils/Formatter.cpp
@@ -19,6 +19,7 @@
 #include <assert.h>
 
 #include <android-base/logging.h>
+#include <android-base/strings.h>
 #include <string>
 #include <vector>
 
@@ -65,12 +66,12 @@
     return (*this) << "}";
 }
 
-void Formatter::setLinePrefix(const std::string &prefix) {
-    mLinePrefix = prefix;
+void Formatter::pushLinePrefix(const std::string& prefix) {
+    mLinePrefix.push_back(prefix);
 }
 
-void Formatter::unsetLinePrefix() {
-    mLinePrefix = "";
+void Formatter::popLinePrefix() {
+    mLinePrefix.pop_back();
 }
 
 Formatter &Formatter::endl() {
@@ -120,14 +121,16 @@
 Formatter& Formatter::operator<<(const std::string& out) {
     const size_t len = out.length();
     size_t start = 0;
+
+    const std::string& prefix = base::Join(mLinePrefix, "");
     while (start < len) {
         size_t pos = out.find('\n', start);
 
         if (pos == std::string::npos) {
             if (mCurrentPosition == 0) {
                 fprintf(mFile, "%*s", (int)(getIndentation()), "");
-                fprintf(mFile, "%s", mLinePrefix.c_str());
-                mCurrentPosition = getIndentation() + mLinePrefix.size();
+                fprintf(mFile, "%s", prefix.c_str());
+                mCurrentPosition = getIndentation() + prefix.size();
             }
 
             std::string sub = out.substr(start);
@@ -136,10 +139,10 @@
             break;
         }
 
-        if (mCurrentPosition == 0 && (pos > start || !mLinePrefix.empty())) {
+        if (mCurrentPosition == 0 && (pos > start || !prefix.empty())) {
             fprintf(mFile, "%*s", (int)(getIndentation()), "");
-            fprintf(mFile, "%s", mLinePrefix.c_str());
-            mCurrentPosition = getIndentation() + mLinePrefix.size();
+            fprintf(mFile, "%s", prefix.c_str());
+            mCurrentPosition = getIndentation() + prefix.size();
         }
 
         if (pos == start) {
@@ -157,7 +160,12 @@
 }
 
 void Formatter::printBlock(const WrappedOutput::Block& block, size_t lineLength) {
-    size_t lineStart = mCurrentPosition ?: (getIndentation() + mLinePrefix.size());
+    size_t prefixSize = 0;
+    for (const std::string& prefix : mLinePrefix) {
+        prefixSize += prefix.size();
+    }
+
+    size_t lineStart = mCurrentPosition ?: (getIndentation() + prefixSize);
     size_t blockSize = block.computeSize(false);
     if (blockSize + lineStart < lineLength) {
         block.print(*this, false);
@@ -166,7 +174,7 @@
 
     // Everything will not fit on this line. Try to fit it on the next line.
     blockSize = block.computeSize(true);
-    if ((blockSize + getIndentation() + mSpacesPerIndent + mLinePrefix.size()) < lineLength) {
+    if ((blockSize + getIndentation() + mSpacesPerIndent + prefixSize) < lineLength) {
         *this << "\n";
         indent();
 
diff --git a/host_utils/include/hidl-util/Formatter.h b/host_utils/include/hidl-util/Formatter.h
index 3c0e116..0364bd9 100644
--- a/host_utils/include/hidl-util/Formatter.h
+++ b/host_utils/include/hidl-util/Formatter.h
@@ -171,9 +171,10 @@
     // you want to start a // comment block, for example.
     // The prefix will be put before the indentation.
     // Will be effective the next time cursor is at the start of line.
-    void setLinePrefix(const std::string& prefix);
-    // Remove the line prefix.
-    void unsetLinePrefix();
+    // Adding two prefixes will output them in the order they were added
+    void pushLinePrefix(const std::string& prefix);
+    // Remove the last line prefix.
+    void popLinePrefix();
 
     bool isValid() const;
     size_t getIndentation() const;
@@ -187,7 +188,7 @@
     size_t mSpacesPerIndent;
     size_t mCurrentPosition;
 
-    std::string mLinePrefix;
+    std::vector<std::string> mLinePrefix;
 
     void printBlock(const WrappedOutput::Block& block, size_t lineLength);
     void output(const std::string &text) const;