Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | using namespace std; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vts { |
| 32 | |
| 33 | CodeGenBase::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 | |
| 39 | CodeGenBase::~CodeGenBase() {} |
| 40 | |
| 41 | |
| 42 | void 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 Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 49 | cpp_ss << "#include \"vts_measurement.h\"" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 50 | for (auto const& header : message.header()) { |
| 51 | cpp_ss << "#include " << header << endl; |
| 52 | } |
| 53 | GenerateOpenNameSpaces(cpp_ss); |
| 54 | |
| 55 | string component_name = message.original_data_structure_name(); |
| 56 | while (!component_name.empty() |
| 57 | && (std::isspace(component_name.back()) |
| 58 | || component_name.back() == '*' )) { |
| 59 | component_name.pop_back(); |
| 60 | } |
| 61 | const auto pos = component_name.find_last_of(" "); |
| 62 | if (pos != std::string::npos) { |
| 63 | component_name = component_name.substr(pos + 1); |
| 64 | } |
| 65 | |
| 66 | string fuzzer_extended_class_name; |
| 67 | if (message.component_class() == HAL |
Keun Soo Yim | d455988 | 2016-05-13 20:03:12 -0700 | [diff] [blame] | 68 | || message.component_class() == HAL_SUBMODULE |
| 69 | || message.component_class() == LEGACY_HAL) { |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 70 | fuzzer_extended_class_name = "FuzzerExtended_" + component_name; |
| 71 | } |
| 72 | |
| 73 | h_ss << "#ifndef __VTS_SPEC_" << vts_name_ << "__" << endl; |
| 74 | h_ss << "#define __VTS_SPEC_" << vts_name_ << "__" << endl; |
| 75 | h_ss << endl; |
| 76 | h_ss << "#define LOG_TAG \"" << fuzzer_extended_class_name << "\"" << endl; |
| 77 | h_ss << "#include <utils/Log.h>" << endl; |
| 78 | h_ss << "#include \"common/fuzz_tester/FuzzerBase.h\"" << endl; |
| 79 | for (auto const& header : message.header()) { |
| 80 | h_ss << "#include " << header << endl; |
| 81 | } |
| 82 | h_ss << "\n\n" << endl; |
| 83 | GenerateOpenNameSpaces(h_ss); |
| 84 | h_ss << "class " << fuzzer_extended_class_name << " : public FuzzerBase {" |
| 85 | << endl; |
Keun Soo Yim | d455988 | 2016-05-13 20:03:12 -0700 | [diff] [blame] | 86 | h_ss << " public:" << endl; |
| 87 | h_ss << " " << fuzzer_extended_class_name << "() : FuzzerBase("; |
| 88 | |
| 89 | if (message.component_class() == HAL) h_ss << "HAL"; |
| 90 | if (message.component_class() == HAL_SUBMODULE) h_ss << "HAL_SUBMODULE"; |
| 91 | if (message.component_class() == LEGACY_HAL) h_ss << "LEGACY_HAL"; |
| 92 | |
| 93 | h_ss << ") { }" << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 94 | h_ss << " protected:" << endl; |
Keun Soo Yim | 0ae2f74 | 2016-06-01 14:36:01 -0700 | [diff] [blame^] | 95 | h_ss << " bool Fuzz(FunctionSpecificationMessage& func_msg," << endl; |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 96 | h_ss << " void** result);" << endl; |
| 97 | if (message.component_class() == HAL_SUBMODULE) { |
| 98 | h_ss << " void SetSubModule(" << component_name << "* submodule) {" << endl; |
| 99 | h_ss << " submodule_ = submodule;" << endl; |
| 100 | h_ss << " }" << endl; |
| 101 | h_ss << endl; |
| 102 | h_ss << " private:" << endl; |
| 103 | h_ss << " " << message.original_data_structure_name() << "* submodule_;" |
| 104 | << endl; |
| 105 | } |
| 106 | h_ss << "};" << endl; |
| 107 | |
| 108 | string function_name_prefix = GetFunctionNamePrefix(message); |
| 109 | |
| 110 | cpp_ss << endl; |
| 111 | GenerateCppBodyFuzzFunction(cpp_ss, message, fuzzer_extended_class_name); |
| 112 | |
| 113 | std::stringstream ss; |
| 114 | // return type |
| 115 | h_ss << endl; |
| 116 | ss << "android::vts::FuzzerBase* " << endl; |
| 117 | // function name |
| 118 | ss << function_name_prefix << "(" << endl; |
| 119 | ss << ")"; |
| 120 | |
| 121 | GenerateHeaderGlobalFunctionDeclarations(h_ss, ss.str()); |
| 122 | |
| 123 | GenerateCloseNameSpaces(cpp_ss); |
| 124 | cpp_ss << endl << endl; |
| 125 | |
| 126 | GenerateCppBodyGlobalFunctions(cpp_ss, ss.str(), fuzzer_extended_class_name); |
| 127 | |
| 128 | GenerateCloseNameSpaces(h_ss); |
| 129 | h_ss << "#endif" << endl; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void CodeGenBase::GenerateOpenNameSpaces(std::stringstream& ss) { |
| 134 | ss << "namespace android {" << endl; |
| 135 | ss << "namespace vts {" << endl; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void CodeGenBase::GenerateCloseNameSpaces(std::stringstream& ss) { |
| 140 | ss << "} // namespace vts" << endl; |
| 141 | ss << "} // namespace android" << endl; |
| 142 | } |
| 143 | |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 144 | |
| 145 | void CodeGenBase::GenerateCodeToStartMeasurement(std::stringstream& ss) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 146 | ss << " VtsMeasurement vts_measurement;" << endl; |
| 147 | ss << " vts_measurement.Start();" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | void CodeGenBase::GenerateCodeToStopMeasurement(std::stringstream& ss) { |
Keun Soo Yim | 6d94495 | 2016-05-31 16:30:56 -0700 | [diff] [blame] | 152 | ss << " vector<float>* measured = vts_measurement.Stop();" << endl; |
| 153 | ss << " cout << \"time \" << (*measured)[0] << endl;" << endl; |
Keun Soo Yim | ffb07ba | 2016-05-18 16:22:45 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Keun Soo Yim | b8edda3 | 2016-04-27 17:31:00 -0700 | [diff] [blame] | 156 | } // namespace vts |
| 157 | } // namespace android |