diff options
author | Jeongik Cha <jeongik@google.com> | 2019-04-02 12:08:21 +0900 |
---|---|---|
committer | Jeongik Cha <jeongik@google.com> | 2019-04-08 14:08:50 +0000 |
commit | 71d1f3833613772af89558fbc1c315987c71eefd (patch) | |
tree | 2c35e8cf51ba9f5fbdc862bbd00feb630dbca4fe | |
parent | 081b90a81b3b6d7ccd740fd87faf16b9259f4dab (diff) | |
download | aidl-71d1f3833613772af89558fbc1c315987c71eefd.tar.gz |
Change parameters in log data to array of pairs
As-is: Object type {a:1, b:2, c:3}
To-be: Array type [{name: a, value:1}, {name:b, value:2}, {name:c, value:3}]
Object type doesn't keep insertion order. So change it to array type
Bug: 128833454
Test: m -j
Change-Id: Ic7c36560de03a4f2bbefef3c4a0a790ce4b90869
Merged-In: Ic7c36560de03a4f2bbefef3c4a0a790ce4b90869
(cherry picked from commit 2a2581ea3a58c92a429db85485e74c692dd99de4)
-rw-r--r-- | aidl_to_cpp.cpp | 21 | ||||
-rw-r--r-- | aidl_to_cpp.h | 2 | ||||
-rw-r--r-- | generate_cpp.cpp | 52 |
3 files changed, 48 insertions, 27 deletions
diff --git a/aidl_to_cpp.cpp b/aidl_to_cpp.cpp index 9dba75a8..d5a9f025 100644 --- a/aidl_to_cpp.cpp +++ b/aidl_to_cpp.cpp @@ -105,7 +105,7 @@ const static std::unordered_map<std::string, TypeInfo> kTypeInfoMap = { // missing List, Map, ParcelFileDescriptor, IBinder }; -TypeInfo GetTypeInfo(const AidlTypenames&, const AidlTypeSpecifier& aidl) { +TypeInfo GetTypeInfo(const AidlTypeSpecifier& aidl) { CHECK(aidl.IsResolved()) << aidl.ToString(); const string& aidl_name = aidl.GetName(); @@ -120,21 +120,28 @@ TypeInfo GetTypeInfo(const AidlTypenames&, const AidlTypeSpecifier& aidl) { return info; } +inline bool CanWriteLog(const TypeInfo& t) { + return t.cpp_name != ""; +} + +bool CanWriteLog(const AidlTypeSpecifier& aidl) { + return CanWriteLog(GetTypeInfo(aidl)); +} + void WriteLogFor(const CodeGeneratorContext& c) { - const TypeInfo info = GetTypeInfo(c.types, c.type); - if (info.cpp_name == "") { + const TypeInfo info = GetTypeInfo(c.type); + if (!CanWriteLog(info)) { return; } const string var_object_expr = ((c.isPointer ? "*" : "")) + c.name; if (c.type.IsArray()) { - c.writer << c.log << "[\"" << c.name << "\"] = Json::Value(Json::arrayValue);\n"; - c.writer << "for (const auto& v: " << var_object_expr << ") " << c.log << "[\"" << c.name - << "\"].append("; + c.writer << c.log << " = Json::Value(Json::arrayValue);\n"; + c.writer << "for (const auto& v: " << var_object_expr << ") " << c.log << ".append("; info.toJsonValueExpr(c, "v"); c.writer << ");"; } else { - c.writer << c.log << "[\"" << c.name << "\"] = "; + c.writer << c.log << " = "; info.toJsonValueExpr(c, var_object_expr); c.writer << ";"; } diff --git a/aidl_to_cpp.h b/aidl_to_cpp.h index 8aff3719..ca5a6cd0 100644 --- a/aidl_to_cpp.h +++ b/aidl_to_cpp.h @@ -38,7 +38,7 @@ struct CodeGeneratorContext { // Emits code that writes name and value of an argument or the return value to // the Json object void WriteLogFor(const CodeGeneratorContext& c); - +bool CanWriteLog(const AidlTypeSpecifier& aidl); std::string GetTransactionIdFor(const AidlMethod& method); } // namespace cpp } // namespace aidl diff --git a/generate_cpp.cpp b/generate_cpp.cpp index 544a68f6..ca7d7d69 100644 --- a/generate_cpp.cpp +++ b/generate_cpp.cpp @@ -225,21 +225,38 @@ string BuildHeaderGuard(const AidlDefinedType& defined_type, ClassNames header_t return ret; } +void WriteLogForArguments(CodeWriterPtr& writer, const AidlArgument& a, const TypeNamespace& types, + bool isServer, string logVarName) { + if (!CanWriteLog(a.GetType())) { + return; + } + string logElementVarName = "_log_arg_element"; + (*writer) << "{\n"; + (*writer).Indent(); + (*writer) << "Json::Value " << logElementVarName << "(Json::objectValue);\n"; + string varName = isServer ? BuildVarName(a) : a.GetName(); + (*writer) << logElementVarName << "[\"name\"] = \"" << varName << "\";\n"; + + bool isPointer = a.IsOut() && !isServer; + WriteLogFor({*(writer.get()), types.typenames_, a.GetType(), varName, isPointer, + logElementVarName + "[\"value\"]"}); + (*writer) << logVarName << ".append(" << logElementVarName << ");\n"; + (*writer) << "}\n"; + (*writer).Dedent(); +} + const string GenLogBeforeExecute(const string className, const AidlMethod& method, const TypeNamespace& types, bool isServer) { string code; CodeWriterPtr writer = CodeWriter::ForString(&code); - (*writer) << "Json::Value _log_input_args(Json::objectValue);\n"; + (*writer) << "Json::Value _log_input_args(Json::arrayValue);\n"; (*writer) << "if (" << className << "::logFunc != nullptr) {\n"; (*writer).Indent(); for (const auto& a : method.GetArguments()) { if (a->IsIn()) { - string varName = isServer ? BuildVarName(*a) : a->GetName(); - bool isPointer = a->IsOut() && !isServer; - WriteLogFor( - {*(writer.get()), types.typenames_, a->GetType(), varName, isPointer, "_log_input_args"}); + WriteLogForArguments(writer, *a, types, isServer, "_log_input_args"); } } @@ -277,14 +294,14 @@ const string GenLogAfterExecute(const string className, const AidlInterface& int // interface_name: "foo.bar.IFoo", // method_name: "TestMethod", // (proxy|stub)_address: "0x12345678", - // input_args: { - // arg1: 30, - // arg2: ["apple", "grape"], - // }, - // output_args: { - // arg2: ["mango", "banana"], - // arg3: "10.5", - // }, + // input_args: [ + // {name: "arg1", value: 30,}, + // {name: "arg2", value: ["apple", "grape"],}, + // ], + // output_args: [ + // {name: "arg2", value: ["mango", "banana"],}, + // {name: "arg3", value: "10.5",}, + // ], // _aidl_return: "ok", // binder_status: { // exception_code: -8, @@ -306,7 +323,7 @@ const string GenLogAfterExecute(const string className, const AidlInterface& int (*writer) << "_log_transaction[\"" << (isServer ? "stub_address" : "proxy_address") << "\"] = " << "Json::Value(android::base::StringPrintf(\"0x%%p\", this));\n"; (*writer) << "_log_transaction[\"input_args\"] = _log_input_args;\n"; - (*writer) << "Json::Value _log_output_args(Json::objectValue);\n"; + (*writer) << "Json::Value _log_output_args(Json::arrayValue);\n"; (*writer) << "Json::Value _log_status(Json::objectValue);\n"; (*writer) << "_log_status[\"exception_code\"] = Json::Value(" << statusVarName @@ -321,17 +338,14 @@ const string GenLogAfterExecute(const string className, const AidlInterface& int (*writer) << "_log_transaction[\"binder_status\"] = _log_status;\n"; for (const auto& a : method.GetOutArguments()) { - string varName = isServer ? BuildVarName(*a) : a->GetName(); - bool isPointer = !isServer; - WriteLogFor( - {*(writer.get()), types.typenames_, a->GetType(), varName, isPointer, "_log_output_args"}); + WriteLogForArguments(writer, *a, types, isServer, "_log_output_args"); } (*writer) << "_log_transaction[\"output_args\"] = _log_output_args;\n"; if (method.GetType().GetName() != "void") { WriteLogFor({*(writer.get()), types.typenames_, method.GetType(), returnVarName, !isServer, - "_log_transaction"}); + "_log_transaction[\"" + returnVarName + "\"]"}); } // call the user-provided function with the Json object for the entire |