blob: b4050f0e4b9242d54c00d622d24598ea47e16324 [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;
58 if (message.component_class() == HAL
Keun Soo Yimd4559882016-05-13 20:03:12 -070059 || message.component_class() == HAL_SUBMODULE
60 || message.component_class() == LEGACY_HAL) {
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;
66
67 cpp_ss << endl;
68 GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name);
69
70 std::stringstream ss;
71 // return type
72 ss << "android::vts::FuzzerBase* " << endl;
73 // function name
74 string function_name_prefix = GetFunctionNamePrefix(message);
75 ss << function_name_prefix << "(" << endl;
76 ss << ")";
77
78 GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name);
79
80 GenerateCloseNameSpaces(cpp_ss);
81}
82
83
84void CodeGenBase::GenerateAllHeader(
85 const string& fuzzer_extended_class_name,
86 std::stringstream& h_ss, const InterfaceSpecificationMessage& message) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -070087 h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl;
88 h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl;
89 h_ss << endl;
90 h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl;
91 h_ss << "#include <utils/Log.h>" << endl;
92 h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl;
93 for (auto const& header : message.header()) {
94 h_ss << "#include " << header << endl;
95 }
96 h_ss << "\n\n" << endl;
97 GenerateOpenNameSpaces(h_ss);
Keun Soo Yim91d634a2016-06-02 11:38:00 -070098
99 GenerateClassHeader(fuzzer_extended_class_name, h_ss, message);
100
101 string function_name_prefix = GetFunctionNamePrefix(message);
102
103 std::stringstream ss;
104 // return type
105 h_ss << endl;
106 ss << "android::vts::FuzzerBase* " << endl;
107 // function name
108 ss << function_name_prefix << "(" << endl;
109 ss << ")";
110
111 GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str());
112
113 GenerateCloseNameSpaces(h_ss);
114 h_ss << "#endif" << endl;
115}
116
117
118void CodeGenBase::GenerateClassHeader(
119 const string& fuzzer_extended_class_name,
120 std::stringstream& h_ss,
121 const InterfaceSpecificationMessage& message) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700122 h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {"
123 << endl;
Keun Soo Yimd4559882016-05-13 20:03:12 -0700124 h_ss << " public:" << endl;
125 h_ss << " " << fuzzer_extended_class_name << "() : FuzzerBase(";
126
127 if (message.component_class() == HAL) h_ss << "HAL";
128 if (message.component_class() == HAL_SUBMODULE) h_ss << "HAL_SUBMODULE";
129 if (message.component_class() == LEGACY_HAL) h_ss << "LEGACY_HAL";
130
131 h_ss << ") { }" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700132 h_ss << " protected:" << endl;
Keun Soo Yimf5185852016-06-01 19:37:10 -0700133 h_ss << " bool Fuzz(FunctionSpecificationMessage* func_msg," << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700134 h_ss << " void** result);" << endl;
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700135
136 // produce Fuzz method(s) for sub_struct(s).
137 for (auto const& sub_struct : message.sub_struct()) {
138 GenerateFuzzFunctionForSubStruct(h_ss, sub_struct, "_");
139 }
140
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700141 if (message.component_class() == HAL_SUBMODULE) {
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700142 string component_name = GetComponentName(message);
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700143 h_ss << " void SetSubModule(" << component_name << "* submodule) {" << endl;
144 h_ss << " submodule_ = submodule;" << endl;
145 h_ss << " }" << endl;
146 h_ss << endl;
147 h_ss << " private:" << endl;
148 h_ss << " " << message.original_data_structure_name() << "* submodule_;"
149 << endl;
150 }
151 h_ss << "};" << endl;
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700152}
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700153
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700154
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700155void CodeGenBase::GenerateFuzzFunctionForSubStruct(
156 std::stringstream& h_ss,
157 const StructSpecificationMessage& message, const string& parent_path) {
158 h_ss << " bool Fuzz_" << parent_path << message.name()
159 << "(FunctionSpecificationMessage* func_msg," << endl;
160 h_ss << " void** result);" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700161
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700162 for (auto const& sub_struct : message.sub_struct()) {
163 GenerateFuzzFunctionForSubStruct(
164 h_ss, sub_struct, parent_path + message.name() + "_");
165 }
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700166}
167
168
169void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) {
170 ss << "namespace android {" << endl;
171 ss << "namespace vts {" << endl;
172}
173
174
175void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) {
176 ss << "} // namespace vts" << endl;
177 ss << "} // namespace android" << endl;
178}
179
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700180
181void CodeGenBase::GenerateCodeToStartMeasurement(std::stringstream& ss) {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700182 ss << " VtsMeasurement vts_measurement;" << endl;
183 ss << " vts_measurement.Start();" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700184}
185
186
187void CodeGenBase::GenerateCodeToStopMeasurement(std::stringstream& ss) {
Keun Soo Yim6d944952016-05-31 16:30:56 -0700188 ss << " vector<float>* measured = vts_measurement.Stop();" << endl;
189 ss << " cout << \"time \" << (*measured)[0] << endl;" << endl;
Keun Soo Yimffb07ba2016-05-18 16:22:45 -0700190}
191
Keun Soo Yim91d634a2016-06-02 11:38:00 -0700192
193string CodeGenBase::GetComponentName(
194 const InterfaceSpecificationMessage& message) {
195 string component_name = message.original_data_structure_name();
196 while (!component_name.empty()
197 && (std::isspace(component_name.back())
198 || component_name.back() == '*' )) {
199 component_name.pop_back();
200 }
201 const auto pos = component_name.find_last_of(" ");
202 if (pos != std::string::npos) {
203 component_name = component_name.substr(pos + 1);
204 }
205 return component_name;
206}
Keun Soo Yimb8edda32016-04-27 17:31:00 -0700207} // namespace vts
208} // namespace android