blob: 05c54620c122e0991694662e1b1a894b90d2ad96 [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;
49 for (auto const& header : message.header()) {
50 cpp_ss << "#include " << header << endl;
51 }
52 GenerateOpenNameSpaces(cpp_ss);
53
54 string component_name = message.original_data_structure_name();
55 while (!component_name.empty()
56 && (std::isspace(component_name.back())
57 || component_name.back() == '*' )) {
58 component_name.pop_back();
59 }
60 const auto pos = component_name.find_last_of(" ");
61 if (pos != std::string::npos) {
62 component_name = component_name.substr(pos + 1);
63 }
64
65 string fuzzer_extended_class_name;
66 if (message.component_class() == HAL
Keun Soo Yimd4559882016-05-13 20:03:12 -070067 || message.component_class() == HAL_SUBMODULE
68 || message.component_class() == LEGACY_HAL) {
Keun Soo Yimb8edda32016-04-27 17:31:00 -070069 fuzzer_extended_class_name = "FuzzerExtended_" + component_name;
70 }
71
72 h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl;
73 h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl;
74 h_ss << endl;
75 h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl;
76 h_ss << "#include <utils/Log.h>" << endl;
77 h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl;
78 for (auto const& header : message.header()) {
79 h_ss << "#include " << header << endl;
80 }
81 h_ss << "\n\n" << endl;
82 GenerateOpenNameSpaces(h_ss);
83 h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {"
84 << endl;
Keun Soo Yimd4559882016-05-13 20:03:12 -070085 h_ss << " public:" << endl;
86 h_ss << " " << fuzzer_extended_class_name << "() : FuzzerBase(";
87
88 if (message.component_class() == HAL) h_ss << "HAL";
89 if (message.component_class() == HAL_SUBMODULE) h_ss << "HAL_SUBMODULE";
90 if (message.component_class() == LEGACY_HAL) h_ss << "LEGACY_HAL";
91
92 h_ss << ") { }" << endl;
Keun Soo Yimb8edda32016-04-27 17:31:00 -070093 h_ss << " protected:" << endl;
94 h_ss << " bool Fuzz(const FunctionSpecificationMessage& func_msg," << endl;
95 h_ss << " void** result);" << endl;
96 if (message.component_class() == HAL_SUBMODULE) {
97 h_ss << " void SetSubModule(" << component_name << "* submodule) {" << endl;
98 h_ss << " submodule_ = submodule;" << endl;
99 h_ss << " }" << endl;
100 h_ss << endl;
101 h_ss << " private:" << endl;
102 h_ss << " " << message.original_data_structure_name() << "* submodule_;"
103 << endl;
104 }
105 h_ss << "};" << endl;
106
107 string function_name_prefix = GetFunctionNamePrefix(message);
108
109 cpp_ss << endl;
110 GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name);
111
112 std::stringstream ss;
113 // return type
114 h_ss << endl;
115 ss << "android::vts::FuzzerBase* " << endl;
116 // function name
117 ss << function_name_prefix << "(" << endl;
118 ss << ")";
119
120 GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str());
121
122 GenerateCloseNameSpaces(cpp_ss);
123 cpp_ss << endl << endl;
124
125 GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name);
126
127 GenerateCloseNameSpaces(h_ss);
128 h_ss << "#endif" << endl;
129}
130
131
132void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) {
133 ss << "namespace android {" << endl;
134 ss << "namespace vts {" << endl;
135}
136
137
138void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) {
139 ss << "} // namespace vts" << endl;
140 ss << "} // namespace android" << endl;
141}
142
143} // namespace vts
144} // namespace android