blob: 36d83b60ea18de8d36b3c2fe448d3db7e4a7c2be [file] [log] [blame]
Keun Soo Yimb8edda32016-04-27 17:31:00 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_gen/CodeGenBase.h"
18
19#include <fstream>
20#include <iostream>
21#include <sstream>
22#include <string>
23
24#include "utils/InterfaceSpecUtil.h"
25
26#include "VtsCompilerUtils.h"
27
28using namespace std;
29
30namespace android {
31namespace vts {
32
33CodeGenBase::CodeGenBase(const char* input_vts_file_path,
34 const char* vts_name)
35 : input_vts_file_path_(input_vts_file_path),
36 vts_name_(vts_name) {}
37
38
39CodeGenBase::~CodeGenBase() {}
40
41
42void CodeGenBase::GenerateAll(std::stringstream& cpp_ss,
43 std::stringstream& h_ss,
44 const InterfaceSpecificationMessage& message) {
45 cpp_ss << "#include \"" << string(input_vts_file_path_) << ".h\"" << endl;
46
47 cpp_ss << "#include <iostream>" << endl;
48 cpp_ss << "#include \"vts_datatype.h\"" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -070049 cpp_ss << "#include \"vts_measurement.h\"" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -070050 for (auto const& header : message.header()) {
51 cpp_ss << "#include " << header << endl;
52 }
53 GenerateOpenNameSpaces(cpp_ss);
54
Keun Soo Yim91d634a2016-06-02 11:38:00 -070055 string component_name = GetComponentName(message);
Keun Soo Yimb8edda32016-04-27 17:31:00 -070056
57 string fuzzer_extended_class_name;
Keun Soo Yim78446412016-06-16 14:30:13 -070058 if (message.component_class() == HAL_CONVENTIONAL
Keun Soo Yimdc55fb82016-06-25 10:13:58 -070059 || message.component_class() == HAL_CONVENTIONAL_SUBMODULE
Keun Soo Yim78446412016-06-16 14:30:13 -070060 || message.component_class() == HAL_LEGACY) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -070061 fuzzer_extended_class_name = "FuzzerExtended_" + component_name;
62 }
63
Keun Soo Yim91d634a2016-06-02 11:38:00 -070064 GenerateAllHeader(fuzzer_extended_class_name, h_ss, message);
65 cpp_ss << endl << endl;
Keun Soo Yimdc55fb82016-06-25 10:13:58 -070066 if (message.component_class() == HAL_CONVENTIONAL
67 || message.component_class() == HAL_CONVENTIONAL_SUBMODULE) {
Keun Soo Yima4a6d532016-06-08 09:11:40 -070068 GenerateCppBodyCallbackFunction(cpp_ss, message, fuzzer_extended_class_name);
69 }
Keun Soo Yim91d634a2016-06-02 11:38:00 -070070
71 cpp_ss << endl;
72 GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name);
73
74 std::stringstream ss;
75 // return type
76 ss << "android::vts::FuzzerBase* " << endl;
77 // function name
78 string function_name_prefix = GetFunctionNamePrefix(message);
79 ss << function_name_prefix << "(" << endl;
80 ss << ")";
81
82 GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name);
83
84 GenerateCloseNameSpaces(cpp_ss);
85}
86
87
88void CodeGenBase::GenerateAllHeader(
89 const string& fuzzer_extended_class_name,
90 std::stringstream& h_ss, const InterfaceSpecificationMessage& message) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -070091 h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl;
92 h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl;
93 h_ss << endl;
Keun Soo Yima4a6d532016-06-08 09:11:40 -070094 h_ss << "#include <stdio.h>" << endl;
95 h_ss << "#include <stdarg.h>" << endl;
96 h_ss << "#include <stdlib.h>" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -070097 h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl;
98 h_ss << "#include <utils/Log.h>" << endl;
99 h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl;
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700100 h_ss << "#include \"common/fuzz_tester/FuzzerCallbackBase.h\"" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700101 for (auto const& header : message.header()) {
102 h_ss << "#include " << header << endl;
103 }
104 h_ss << "\n\n" << endl;
105 GenerateOpenNameSpaces(h_ss);
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700106
107 GenerateClassHeader(fuzzer_extended_class_name, h_ss, message);
108
109 string function_name_prefix = GetFunctionNamePrefix(message);
110
111 std::stringstream ss;
112 // return type
113 h_ss << endl;
114 ss << "android::vts::FuzzerBase* " << endl;
115 // function name
116 ss << function_name_prefix << "(" << endl;
117 ss << ")";
118
119 GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str());
120
121 GenerateCloseNameSpaces(h_ss);
122 h_ss << "#endif" << endl;
123}
124
125
126void CodeGenBase::GenerateClassHeader(
127 const string& fuzzer_extended_class_name,
128 std::stringstream& h_ss,
129 const InterfaceSpecificationMessage& message) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700130 h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {"
131 << endl;
Keun Soo Yimd4559882016-05-13 20:03:12 -0700132 h_ss << " public:" << endl;
133 h_ss << " " << fuzzer_extended_class_name << "() : FuzzerBase(";
134
Keun Soo Yim78446412016-06-16 14:30:13 -0700135 if (message.component_class() == HAL_CONVENTIONAL) h_ss << "HAL_CONVENTIONAL";
Keun Soo Yimdc55fb82016-06-25 10:13:58 -0700136 if (message.component_class() == HAL_CONVENTIONAL_SUBMODULE) {
137 h_ss << "HAL_CONVENTIONAL_SUBMODULE";
138 }
Keun Soo Yim78446412016-06-16 14:30:13 -0700139 if (message.component_class() == HAL_LEGACY) h_ss << "HAL_LEGACY";
Keun Soo Yimd4559882016-05-13 20:03:12 -0700140
141 h_ss << ") { }" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700142 h_ss << " protected:" << endl;
Keun Soo Yimf5185852016-06-01 19:37:10 -0700143 h_ss << " bool Fuzz(FunctionSpecificationMessage* func_msg," << endl;
Keun Soo Yim6d8a16b2016-06-30 19:29:02 -0700144 h_ss << " void** result, const string& callback_socket_name);"
145 << endl;
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700146
147 // produce Fuzz method(s) for sub_struct(s).
148 for (auto const& sub_struct : message.sub_struct()) {
149 GenerateFuzzFunctionForSubStruct(h_ss, sub_struct, "_");
150 }
151
Keun Soo Yimdc55fb82016-06-25 10:13:58 -0700152 if (message.component_class() == HAL_CONVENTIONAL_SUBMODULE) {
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700153 string component_name = GetComponentName(message);
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700154 h_ss << " void SetSubModule(" << component_name << "* submodule) {" << endl;
155 h_ss << " submodule_ = submodule;" << endl;
156 h_ss << " }" << endl;
157 h_ss << endl;
158 h_ss << " private:" << endl;
159 h_ss << " " << message.original_data_structure_name() << "* submodule_;"
160 << endl;
161 }
162 h_ss << "};" << endl;
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700163}
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700164
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700165
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700166void CodeGenBase::GenerateFuzzFunctionForSubStruct(
167 std::stringstream& h_ss,
168 const StructSpecificationMessage& message, const string& parent_path) {
169 h_ss << " bool Fuzz_" << parent_path << message.name()
170 << "(FunctionSpecificationMessage* func_msg," << endl;
Keun Soo Yim6d8a16b2016-06-30 19:29:02 -0700171 h_ss << " void** result, const string& callback_socket_name);"
172 << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700173
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700174 for (auto const& sub_struct : message.sub_struct()) {
175 GenerateFuzzFunctionForSubStruct(
176 h_ss, sub_struct, parent_path + message.name() + "_");
177 }
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700178}
179
180
181void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) {
182 ss << "namespace android {" << endl;
183 ss << "namespace vts {" << endl;
184}
185
186
187void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) {
188 ss << "} // namespace vts" << endl;
189 ss << "} // namespace android" << endl;
190}
191
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700192
193void CodeGenBase::GenerateCodeToStartMeasurement(std::stringstream& ss) {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700194 ss << " VtsMeasurement vts_measurement;" << endl;
195 ss << " vts_measurement.Start();" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700196}
197
198
199void CodeGenBase::GenerateCodeToStopMeasurement(std::stringstream& ss) {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700200 ss << " vector<float>* measured = vts_measurement.Stop();" << endl;
201 ss << " cout << \"time \" << (*measured)[0] << endl;" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700202}
203
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700204
205string CodeGenBase::GetComponentName(
206 const InterfaceSpecificationMessage& message) {
207 string component_name = message.original_data_structure_name();
208 while (!component_name.empty()
209 && (std::isspace(component_name.back())
210 || component_name.back() == '*' )) {
211 component_name.pop_back();
212 }
213 const auto pos = component_name.find_last_of(" ");
214 if (pos != std::string::npos) {
215 component_name = component_name.substr(pos + 1);
216 }
217 return component_name;
218}
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700219
220
221void CodeGenBase::GenerateCppBodyCallbackFunction(
222 std::stringstream& /*cpp_ss*/,
223 const InterfaceSpecificationMessage& /*message*/,
224 const string& /*fuzzer_extended_class_name*/) {}
225
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700226} // namespace vts
227} // namespace android