hidl-gen: add sanitizedName to emitReaderWriterEmbedded.

* Fix weird variable names in autogenerated code for vec<vec<T>>.

Bug: 31955917

Test: hidl_test
Change-Id: I83363a6d12babae1aba8e118451e1ec80492012c
diff --git a/ArrayType.cpp b/ArrayType.cpp
index 3fb2691..8294806 100644
--- a/ArrayType.cpp
+++ b/ArrayType.cpp
@@ -184,6 +184,7 @@
             out,
             0 /* depth */,
             name,
+            name /* sanitizedName */,
             isReader /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
@@ -197,6 +198,7 @@
         Formatter &out,
         size_t depth,
         const std::string &name,
+        const std::string &sanitizedName,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
@@ -232,6 +234,7 @@
             out,
             depth + 1,
             nameDeref + "data()[" + iteratorName + "]",
+            sanitizedName + "_indexed",
             false /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
diff --git a/ArrayType.h b/ArrayType.h
index 4514c2d..878dc51 100644
--- a/ArrayType.h
+++ b/ArrayType.h
@@ -64,6 +64,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/CompoundType.cpp b/CompoundType.cpp
index 2e2db7e..400cc19 100644
--- a/CompoundType.cpp
+++ b/CompoundType.cpp
@@ -146,6 +146,7 @@
             out,
             0 /* depth */,
             name,
+            name, /* sanitizedName */
             isReader /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
@@ -159,6 +160,7 @@
         Formatter &out,
         size_t /* depth */,
         const std::string &name,
+        const std::string & /*sanitizedName */,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
@@ -626,6 +628,7 @@
                 out,
                 0 /* depth */,
                 field->name(),
+                field->name() /* sanitizedName */,
                 false /* nameIsPointer */,
                 "parcel",
                 !isReader /* parcelObjIsPointer */,
diff --git a/CompoundType.h b/CompoundType.h
index 325b25a..5cd6efa 100644
--- a/CompoundType.h
+++ b/CompoundType.h
@@ -57,6 +57,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/HandleType.cpp b/HandleType.cpp
index f36aaef..210d5ad 100644
--- a/HandleType.cpp
+++ b/HandleType.cpp
@@ -81,6 +81,7 @@
         Formatter &out,
         size_t /* depth */,
         const std::string &name,
+        const std::string &sanitizedName,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
@@ -89,7 +90,7 @@
         const std::string &parentName,
         const std::string &offsetText) const {
     if (isReader) {
-        const std::string ptrName = "_hidl_" + name  + "_ptr";
+        const std::string ptrName = "_hidl_" + sanitizedName  + "_ptr";
 
         out << "const native_handle_t *"
             << ptrName
diff --git a/HandleType.h b/HandleType.h
index 2283229..9bf79b9 100644
--- a/HandleType.h
+++ b/HandleType.h
@@ -44,6 +44,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/PredefinedType.cpp b/PredefinedType.cpp
index 30e0741..f6b744f 100644
--- a/PredefinedType.cpp
+++ b/PredefinedType.cpp
@@ -102,6 +102,7 @@
             out,
             0 /* depth */,
             name,
+            name /* sanitizedName */,
             isReader /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
@@ -115,6 +116,7 @@
         Formatter &out,
         size_t /* depth */,
         const std::string &name,
+        const std::string & /* sanitizedName */,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
diff --git a/PredefinedType.h b/PredefinedType.h
index 8fdd67f..f11e437 100644
--- a/PredefinedType.h
+++ b/PredefinedType.h
@@ -44,6 +44,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/RefType.cpp b/RefType.cpp
index 641f58e..8c949b8 100644
--- a/RefType.cpp
+++ b/RefType.cpp
@@ -171,6 +171,7 @@
             out,
             0 /* depth */,
             name,
+            name /* sanitizedName */,
             true /* nameIsPointer */, // for element type, name is a pointer.
             parcelObj,
             parcelObjIsPointer,
diff --git a/StringType.cpp b/StringType.cpp
index da0d971..35caa8b 100644
--- a/StringType.cpp
+++ b/StringType.cpp
@@ -108,6 +108,7 @@
             out,
             0 /* depth */,
             name,
+            name /* sanitizedName */,
             isReader /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
@@ -121,6 +122,7 @@
         Formatter &out,
         size_t /* depth */,
         const std::string &name,
+        const std::string & /*sanitizedName*/,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
diff --git a/StringType.h b/StringType.h
index 52e44b0..2fb585f 100644
--- a/StringType.h
+++ b/StringType.h
@@ -49,6 +49,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string & /*sanitizedName*/,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/Type.cpp b/Type.cpp
index 5c65a3e..db877d4 100644
--- a/Type.cpp
+++ b/Type.cpp
@@ -154,6 +154,7 @@
         Formatter &,
         size_t,
         const std::string &,
+        const std::string &,
         bool,
         const std::string &,
         bool,
diff --git a/Type.h b/Type.h
index da97f80..0e0f55e 100644
--- a/Type.h
+++ b/Type.h
@@ -110,6 +110,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/VectorType.cpp b/VectorType.cpp
index b7bebe4..2e7d932 100644
--- a/VectorType.cpp
+++ b/VectorType.cpp
@@ -120,6 +120,7 @@
             out,
             0 /* depth */,
             name,
+            name /* sanitizedName */ ,
             isReader /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
@@ -129,20 +130,11 @@
             "0 /* parentOffset */");
 }
 
-// Remove any trailing array indices from the given name, i.e. foo[2][3] => foo
-static std::string StripIndex(const std::string &name) {
-    size_t pos = name.find("[");
-    if (pos == std::string::npos) {
-        return name;
-    }
-
-    return name.substr(0, pos);
-}
-
 void VectorType::emitReaderWriterEmbedded(
         Formatter &out,
         size_t depth,
         const std::string &name,
+        const std::string &sanitizedName,
         bool nameIsPointer,
         const std::string &parcelObj,
         bool parcelObjIsPointer,
@@ -153,7 +145,7 @@
     std::string baseExtra;
     std::string baseType = Type::getCppType(&baseExtra);
 
-    const std::string childName = "_hidl_" + StripIndex(name) + "_child";
+    const std::string childName = "_hidl_" + sanitizedName + "_child";
     out << "size_t " << childName << ";\n\n";
 
     emitReaderWriterEmbeddedForTypeName(
@@ -196,6 +188,7 @@
             depth + 1,
             (nameIsPointer ? "(*" + name + ")" : name)
                 + "[" + iteratorName + "]",
+            sanitizedName + (nameIsPointer ? "_deref" : "") + "_indexed",
             false /* nameIsPointer */,
             parcelObj,
             parcelObjIsPointer,
diff --git a/VectorType.h b/VectorType.h
index 7627509..0e0a07c 100644
--- a/VectorType.h
+++ b/VectorType.h
@@ -47,6 +47,7 @@
             Formatter &out,
             size_t depth,
             const std::string &name,
+            const std::string &sanitizedName,
             bool nameIsPointer,
             const std::string &parcelObj,
             bool parcelObjIsPointer,
diff --git a/test/main.cpp b/test/main.cpp
index 90aa9da..598470b 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -624,6 +624,8 @@
     Return<void> sendVec(
             const hidl_vec<uint8_t> &data, sendVec_cb _hidl_cb) override;
 
+    Return<void> sendVecVec(sendVecVec_cb _hidl_cb) override;
+
     Return<void> thisIsNew() override;
 };
 
@@ -994,6 +996,13 @@
     return Void();
 }
 
+Return<void> Bar::sendVecVec(sendVecVec_cb _hidl_cb) {
+    hidl_vec<hidl_vec<uint8_t>> data;
+    _hidl_cb(data);
+
+    return Void();
+}
+
 Return<void> Bar::thisIsNew() {
     ALOGI("SERVER(Bar) thisIsNew");