Snap for 5611628 from 8ca5e902a392a92963f86b9e9e3e3db32e626852 to sdk-release

Change-Id: I45c9f89f95ccb9e478b8a772f360819f10761881
diff --git a/Android.bp b/Android.bp
index 692903e..908af10 100644
--- a/Android.bp
+++ b/Android.bp
@@ -124,6 +124,7 @@
     ],
     export_shared_lib_headers: [
         "libbase",
+        "libhidl-gen-hash",
         "libhidl-gen-utils",
     ],
     export_include_dirs: ["."], // for tests
diff --git a/Coordinator.cpp b/Coordinator.cpp
index 007d9b2..0c897a1 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -24,6 +24,7 @@
 
 #include <android-base/logging.h>
 #include <hidl-hash/Hash.h>
+#include <hidl-util/Formatter.h>
 #include <hidl-util/StringHelper.h>
 #include <iostream>
 
@@ -943,10 +944,22 @@
     return true;
 }
 
+void Coordinator::emitOptionsUsageString(Formatter& out) {
+    out << "[-p <root path>] (-r <interface root>)+ [-R] [-v] [-d <depfile>]";
+}
+
+void Coordinator::emitOptionsDetailString(Formatter& out) {
+    out << "-p <root path>: Android build root, defaults to $ANDROID_BUILD_TOP or pwd.\n"
+        << "-R: Do not add default package roots if not specified in -r.\n"
+        << "-r <package:path root>: E.g., android.hardware:hardware/interfaces.\n"
+        << "-v: verbose output.\n"
+        << "-d <depfile>: location of depfile to write to.\n";
+}
+
 void Coordinator::parseOptions(int argc, char** argv, const std::string& options,
                                const HandleArg& handleArg) {
     // reset global state for getopt
-    optind = 0;
+    optind = 1;
 
     bool suppressDefaultPackagePaths = false;
 
diff --git a/Coordinator.h b/Coordinator.h
index 8f5d874..6559100 100644
--- a/Coordinator.h
+++ b/Coordinator.h
@@ -141,6 +141,9 @@
     void parseOptions(int argc, char** argv, const std::string& options,
                       const HandleArg& handleArg);
 
+    static void emitOptionsUsageString(Formatter& out);
+    static void emitOptionsDetailString(Formatter& out);
+
   private:
     static bool MakeParentHierarchy(const std::string &path);
 
diff --git a/Interface.cpp b/Interface.cpp
index fef3a3d..55df082 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -959,22 +959,6 @@
         << "\"\n";
 }
 
-bool Interface::hasOnewayMethods() const {
-    for (auto const &method : methods()) {
-        if (method->isOneway()) {
-            return true;
-        }
-    }
-
-    const Interface* superClass = superType();
-
-    if (superClass != nullptr) {
-        return superClass->hasOnewayMethods();
-    }
-
-    return false;
-}
-
 bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
     if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
         return false;
diff --git a/Interface.h b/Interface.h
index aa3d18f..43e1fd8 100644
--- a/Interface.h
+++ b/Interface.h
@@ -127,8 +127,6 @@
     void emitVtsAttributeDeclaration(Formatter& out) const;
     void emitVtsMethodDeclaration(Formatter& out, bool isInherited) const;
 
-    bool hasOnewayMethods() const;
-
     bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
 
     bool isNeverStrongReference() const override;
diff --git a/Method.cpp b/Method.cpp
index 482bae5..dad0ddf 100644
--- a/Method.cpp
+++ b/Method.cpp
@@ -239,6 +239,30 @@
     emitJavaArgResultSignature(out, results());
 }
 
+void Method::emitJavaSignature(Formatter& out) const {
+    const bool returnsValue = !results().empty();
+    const bool needsCallback = results().size() > 1;
+
+    if (returnsValue && !needsCallback) {
+        out << results()[0]->type().getJavaType();
+    } else {
+        out << "void";
+    }
+
+    out << " " << name() << "(";
+    emitJavaArgSignature(out);
+
+    if (needsCallback) {
+        if (!args().empty()) {
+            out << ", ";
+        }
+
+        out << name() << "Callback _hidl_cb";
+    }
+
+    out << ")";
+}
+
 void Method::dumpAnnotations(Formatter &out) const {
     if (mAnnotations->size() == 0) {
         return;
diff --git a/Method.h b/Method.h
index 22dd08f..41a9d6b 100644
--- a/Method.h
+++ b/Method.h
@@ -101,6 +101,7 @@
 
     void emitJavaArgSignature(Formatter &out) const;
     void emitJavaResultSignature(Formatter &out) const;
+    void emitJavaSignature(Formatter& out) const;
 
     const NamedReference<Type>* canElideCallback() const;
 
diff --git a/generateCpp.cpp b/generateCpp.cpp
index 4caf824..c6de972 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -1723,8 +1723,6 @@
 
     const std::string klassName = iface->getPassthroughName();
 
-    bool supportOneway = iface->hasOnewayMethods();
-
     const std::string guard = makeHeaderGuard(klassName);
 
     out << "#ifndef " << guard << "\n";
@@ -1742,9 +1740,7 @@
     out << "\n";
 
     out << "#include <hidl/HidlPassthroughSupport.h>\n";
-    if (supportOneway) {
-        out << "#include <hidl/TaskRunner.h>\n";
-    }
+    out << "#include <hidl/TaskRunner.h>\n";
 
     enterLeaveNamespace(out, true /* enter */);
     out << "\n";
@@ -1774,14 +1770,12 @@
     out.indent();
     out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
 
-    if (supportOneway) {
-        out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
+    out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
 
-        out << "\n";
+    out << "\n";
 
-        out << "::android::hardware::Return<void> addOnewayTask("
-               "std::function<void(void)>);\n\n";
-    }
+    out << "::android::hardware::Return<void> addOnewayTask("
+           "std::function<void(void)>);\n\n";
 
     out.unindent();
 
@@ -1853,48 +1847,33 @@
 
     const std::string klassName = iface->getPassthroughName();
 
-    out << klassName
-        << "::"
-        << klassName
-        << "(const ::android::sp<"
-        << iface->fullName()
-        << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
-        << mPackage.string()
-        << "\", \""
-        << iface->localName()
-        << "\"), mImpl(impl) {";
-    if (iface->hasOnewayMethods()) {
-        out << "\n";
-        out.indent([&] {
-            out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
-        });
-    }
+    out << klassName << "::" << klassName << "(const ::android::sp<" << iface->fullName()
+        << "> impl) : ::android::hardware::details::HidlInstrumentor(\"" << mPackage.string()
+        << "\", \"" << iface->localName() << "\"), mImpl(impl) {\n";
+
+    out.indent([&] { out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n"; });
+
     out << "}\n\n";
 
-    if (iface->hasOnewayMethods()) {
-        out << "::android::hardware::Return<void> "
-            << klassName
-            << "::addOnewayTask(std::function<void(void)> fun) {\n";
-        out.indent();
-        out << "if (!mOnewayQueue.push(fun)) {\n";
-        out.indent();
-        out << "return ::android::hardware::Status::fromExceptionCode(\n";
-        out.indent();
-        out.indent();
-        out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
-            << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
-        out.unindent();
-        out.unindent();
-        out.unindent();
-        out << "}\n";
+    out << "::android::hardware::Return<void> " << klassName
+        << "::addOnewayTask(std::function<void(void)> fun) {\n";
+    out.indent();
+    out << "if (!mOnewayQueue.push(fun)) {\n";
+    out.indent();
+    out << "return ::android::hardware::Status::fromExceptionCode(\n";
+    out.indent();
+    out.indent();
+    out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
+        << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
+    out.unindent();
+    out.unindent();
+    out.unindent();
+    out << "}\n";
 
-        out << "return ::android::hardware::Status();\n";
+    out << "return ::android::hardware::Status();\n";
 
-        out.unindent();
-        out << "}\n\n";
-
-
-    }
+    out.unindent();
+    out << "}\n\n";
 }
 
 void AST::generateCppAtraceCall(Formatter &out,
diff --git a/generateJava.cpp b/generateJava.cpp
index 1fe0634..577caf9 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -238,7 +238,6 @@
     iface->emitJavaTypeDeclarations(out, false /* atTopLevel */);
 
     for (const auto &method : iface->methods()) {
-        const bool returnsValue = !method->results().empty();
         const bool needsCallback = method->results().size() > 1;
 
         if (needsCallback) {
@@ -257,27 +256,9 @@
 
         method->emitDocComment(out);
 
-        if (returnsValue && !needsCallback) {
-            out << method->results()[0]->type().getJavaType();
-        } else {
-            out << "void";
-        }
+        method->emitJavaSignature(out);
 
-        out << " "
-            << method->name()
-            << "(";
-        method->emitJavaArgSignature(out);
-
-        if (needsCallback) {
-            if (!method->args().empty()) {
-                out << ", ";
-            }
-
-            out << method->name()
-                << "Callback _hidl_cb";
-        }
-
-        out << ")\n";
+        out << "\n";
         out.indent();
         out << "throws android.os.RemoteException;\n";
         out.unindent();
@@ -341,27 +322,9 @@
         const bool needsCallback = method->results().size() > 1;
 
         out << "@Override\npublic ";
-        if (returnsValue && !needsCallback) {
-            out << method->results()[0]->type().getJavaType();
-        } else {
-            out << "void";
-        }
+        method->emitJavaSignature(out);
 
-        out << " "
-            << method->name()
-            << "(";
-        method->emitJavaArgSignature(out);
-
-        if (needsCallback) {
-            if (!method->args().empty()) {
-                out << ", ";
-            }
-
-            out << method->name()
-                << "Callback _hidl_cb";
-        }
-
-        out << ")\n";
+        out << "\n";
         out.indent();
         out.indent();
         out << "throws android.os.RemoteException {\n";
diff --git a/generateJavaImpl.cpp b/generateJavaImpl.cpp
index 91b359e..163a250 100644
--- a/generateJavaImpl.cpp
+++ b/generateJavaImpl.cpp
@@ -53,28 +53,10 @@
                 prevInterface = superInterface;
             }
 
-            const bool returnsValue = !method->results().empty();
-            const bool needsCallback = method->results().size() > 1;
-
             out << "@Override\npublic ";
-            if (returnsValue && !needsCallback) {
-                out << method->results()[0]->type().getJavaType();
-            } else {
-                out << "void";
-            }
+            method->emitJavaSignature(out);
 
-            out << " " << method->name() << "(";
-            method->emitJavaArgSignature(out);
-
-            if (needsCallback) {
-                if (!method->args().empty()) {
-                    out << ", ";
-                }
-
-                out << method->name() << "Callback _hidl_cb";
-            }
-
-            out << ")\n";
+            out << "\n";
             out.indent([&] {
                 out.indent();
                 out << "throws android.os.RemoteException {\n";
@@ -83,11 +65,13 @@
                 out << "// TODO: Implement\n";
 
                 // Return the appropriate value
+                const bool returnsValue = !method->results().empty();
                 if (returnsValue) {
                     for (const auto& arg : method->results()) {
                         arg->type().emitJavaFieldInitializer(out, arg->name());
                     }
 
+                    const bool needsCallback = method->results().size() > 1;
                     if (needsCallback) {
                         out << "_hidl_cb.onValues(";
 
diff --git a/main.cpp b/main.cpp
index b57072d..ba5d8ac 100644
--- a/main.cpp
+++ b/main.cpp
@@ -29,6 +29,7 @@
 #include <unistd.h>
 #include <iostream>
 #include <set>
+#include <sstream>
 #include <string>
 #include <vector>
 
@@ -1232,27 +1233,36 @@
 };
 // clang-format on
 
-static void usage(const char *me) {
-    fprintf(stderr,
-            "usage: %s [-p <root path>] -o <output path> -L <language> [-O <owner>] (-r <interface "
-            "root>)+ [-R] [-v] [-d <depfile>] FQNAME...\n\n",
-            me);
+static void usage(const char* me) {
+    Formatter out(stderr);
 
-    fprintf(stderr,
-            "Process FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)?, to create output.\n\n");
+    out << "Usage: " << me << " -o <output path> -L <language> [-O <owner>] ";
+    Coordinator::emitOptionsUsageString(out);
+    out << " FQNAME...\n\n";
 
-    fprintf(stderr, "         -h: Prints this menu.\n");
-    fprintf(stderr, "         -L <language>: The following options are available:\n");
-    for (auto& e : kFormats) {
-        fprintf(stderr, "            %-16s: %s\n", e.name().c_str(), e.description().c_str());
-    }
-    fprintf(stderr, "         -O <owner>: The owner of the module for -Landroidbp(-impl)?.\n");
-    fprintf(stderr, "         -o <output path>: Location to output files.\n");
-    fprintf(stderr, "         -p <root path>: Android build root, defaults to $ANDROID_BUILD_TOP or pwd.\n");
-    fprintf(stderr, "         -R: Do not add default package roots if not specified in -r.\n");
-    fprintf(stderr, "         -r <package:path root>: E.g., android.hardware:hardware/interfaces.\n");
-    fprintf(stderr, "         -v: verbose output.\n");
-    fprintf(stderr, "         -d <depfile>: location of depfile to write to.\n");
+    out << "Process FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)?, to create output.\n\n";
+
+    out.indent();
+    out.indent();
+
+    out << "-h: Prints this menu.\n";
+    out << "-L <language>: The following options are available:\n";
+    out.indent([&] {
+        for (auto& e : kFormats) {
+            std::stringstream sstream;
+            sstream.fill(' ');
+            sstream.width(16);
+            sstream << std::left << e.name();
+
+            out << sstream.str() << ": " << e.description() << "\n";
+        }
+    });
+    out << "-O <owner>: The owner of the module for -Landroidbp(-impl)?.\n";
+    out << "-o <output path>: Location to output files.\n";
+    Coordinator::emitOptionsDetailString(out);
+
+    out.unindent();
+    out.unindent();
 }
 
 // hidl is intentionally leaky. Turn off LeakSanitizer by default.