1# Copyright (c) 2024 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'erb' 15 16abckit_scripts = File.dirname(__FILE__) 17abckit_root = File.expand_path("../", abckit_scripts) 18abckit_test = File.join(abckit_root, "/tests/mock/tests/") 19 20 21def getReturnTypeString(arr) 22 str = "" 23 arr[1].each do |el| 24 str += el + " " 25 end 26 return str.slice(0, str.length - 1) 27end 28 29def getReturnType(arr) 30 return arr[1] 31end 32 33def getName(arr) 34 return arr[0][1] 35end 36 37def getClass(arr) 38 return arr[0][0] 39end 40 41def getFuncArgs(arr) 42 return arr[2] 43end 44 45def fixCppApiName(foo) 46 if getClass(foo) == "DynamicIsa" 47 if getName(foo)[0..5] == "Create" 48 return ("Icreate" + getName(foo).slice(6, getName(foo).length)) 49 end 50 if getName(foo)[0..2] == "Get" 51 return ("Iget" + getName(foo).slice(3, getName(foo).length)) 52 end 53 if getName(foo)[0..2] == "Set" 54 return ("Iset" + getName(foo).slice(3, getName(foo).length)) 55 end 56 end 57 if getClass(foo) == "BasicBlock" 58 return ("BB" + getName(foo)) 59 end 60 if getClass(foo) == "Graph" 61 return ("G" + getName(foo)) 62 end 63 if getClass(foo) == "Value" 64 return ("Value" + getName(foo)) 65 end 66 if getClass(foo) == "Literal" 67 return ("Literal" + getName(foo)) 68 end 69 return getName(foo) 70end 71 72def argIsVararg(arg) 73 if arg[arg.length - 1].include?("...") || arg[arg.length - 2].include?("...") 74 return true 75 end 76 return false 77end 78 79def generateArgsFromHelpers(args) 80 default_types = Hash.new 81 default_types = { 82 default_types = { 83 "double" => "DEFAULT_DOUBLE", 84 "int" => "DEFAULT_I32", 85 "int32_t" => "DEFAULT_I32", 86 "uint8_t" => "DEFAULT_U8", 87 "uint16_t" => "DEFAULT_U16", 88 "uint32_t" => "DEFAULT_U32", 89 "uint64_t" => "DEFAULT_U64", 90 "bool" => "DEFAULT_BOOL", 91 "std::function<void(BasicBlock)>&" => "DEFAULT_CB", 92 "std::function<bool(core::Module)>&" => "DEFAULT_CB", 93 "std::string" => "DEFAULT_CONST_CHAR", 94 "std::string&" => "DEFAULT_CONST_CHAR", 95 "std::string_view" => "DEFAULT_CONST_CHAR", 96 "std::string_view&" => "DEFAULT_CONST_CHAR", 97 "instructions&&" => "abckit\:\:mock\:\:helpers\:\:GetMockInstruction(f)", 98 "AbckitIsaApiDynamicConditionCode" => "DEFAULT_ENUM_ISA_DYNAMIC_CONDITION_TYPE" 99 } 100 } 101 str = "" 102 args.each do |arg| 103 str += "" 104 if default_types.include?(arg[arg.length - 2]) 105 str += default_types[arg[arg.length - 2]].to_s + ", " 106 elsif argIsVararg(arg) 107 str += "abckit\:\:mock\:\:helpers\:\:GetMockInstruction(f)" 108 str += ", " 109 else 110 str += "abckit\:\:mock\:\:helpers\:\:GetMock" + arg[arg.length - 2].delete("&").delete("*").gsub("core\:\:", "Core") + "(f)" + ", " 111 end 112 end 113 return str.length == 0 ? str : str.slice(0, str.rindex(',')); 114end 115 116def parseArgs(arg_str) 117 args_arr = [] 118 arr = arg_str.split(',') 119 arr.each do |el| 120 arg = el.split(" ") 121 if arg[arg.length - 1][0..1] == '&&' 122 arg[arg.length - 2] += '&&' 123 arg[arg.length - 1].delete!('&') 124 end 125 if arg[arg.length - 1][0] == '&' 126 arg[arg.length - 2] += '&' 127 arg[arg.length - 1].delete!('&') 128 end 129 if arg[arg.length - 1][0] == '*' 130 arg[arg.length - 2] += '*' 131 arg[arg.length - 1].delete!('*') 132 end 133 args_arr << arg 134 end 135 return args_arr 136end 137 138def parseFunction(str, cls) 139 arr = [] 140 name = str.slice(0, str.index('(')); 141 ret_type = name.slice(0, name.rindex(' ')) 142 name = name.slice(name.rindex(' '), name.length) 143 name.delete! " " 144 145 if name[0..1] == '&&' 146 ret_type += '&&' 147 name = name.slice(2, name.length) 148 end 149 150 if name[0] == '&' 151 ret_type += '&' 152 name = name.slice(1, name.length) 153 end 154 155 if name[0] == '*' 156 ret_type += '*' 157 name = name.slice(1, name.length) 158 end 159 160 ret_type_arr = ret_type.split(" ") 161 if ret_type.include? "template" 162 s = "" 163 i = 0 164 while ret_type_arr[0] do 165 s += ret_type_arr[0] + " " 166 ret_type_arr.delete_at(0) 167 if s.include? ">" 168 s = s.slice(0, s.length - 1) 169 break 170 end 171 end 172 ret_type_arr[0] = s 173 end 174 175 arr << [cls, name] 176 arr << ret_type_arr 177 arr << parseArgs(str.slice(str.index('(') + 1, str.rindex(')') - str.index('(') - 1)) 178 179 return arr 180end 181 182def parseFile(filePath, cls) 183 file = File.open(filePath) 184 file_data = file.readlines.map(&:chomp) 185 while file_data[0] != "public:" do 186 file_data.delete_at(0) 187 end 188 file_data.delete_at(0) 189 file_data.delete_if {|x| x.include? "*/" or x.include? "* "or x.include? "//" or x.include? "/**" or x.empty? } 190 file_data.delete_if {|x| x.include?("default")} 191 file_data.delete_if {|x| x.include?("operator")} 192 if(file_data.include?("protected:")) 193 file_data = file_data.slice(0, file_data.index("protected:")) 194 elsif (file_data.include?("private:")) 195 file_data = file_data.slice(0, file_data.index("private:")) 196 elsif (file_data.include?("};")) 197 file_data = file_data.slice(0, file_data.index("};")) 198 end 199 200 size = file_data.size 201 i = 0 202 while i < (size - 1) do 203 if !file_data[i].include?(";") 204 if file_data[i + 1].include? "{" 205 file_data[i] += ";" 206 while !file_data[i + 1].include? "}" do 207 file_data.delete_at(i + 1) 208 size = size - 1 209 end 210 file_data.delete_at(i + 1) 211 size = size - 1 212 next 213 end 214 if file_data[i].include? "{" 215 file_data.delete("{") 216 file_data[i] += ";" 217 while !file_data[i + 1].include? "}" do 218 file_data.delete_at(i + 1) 219 size = size - 1 220 end 221 file_data.delete_at(i + 1) 222 size = size - 1 223 next 224 end 225 file_data[i] = file_data[i] + file_data[i + 1] 226 file_data.delete_at(i + 1) 227 size = size - 1 228 next 229 end 230 i = i + 1 231 end 232 puts file_data 233 234 parsed_functions = [] 235 i = 0 236 while i < file_data.size do 237 parsed_functions << parseFunction(file_data[i], cls) 238 i = i + 1 239 end 240 parsed_functions.delete_if {|x| getReturnType(x).empty?} 241 parsed_functions.delete_if {|x| getName(x) == getClass(x)} 242 243 parsed_functions.each do |foo| 244 puts foo.inspect 245 end 246 247 return parsed_functions 248end 249 250parsed_functions = [] 251parsed_functions += parseFile("../include/cpp/headers/core/annotation_interface.h", "Annotation") 252 253cpp_tests_erb = File.join(abckit_test, "cpp_api_test.cpp.erb") 254testfile_fullpath = File.join(abckit_test, "cpp_api_gtests_gen.cpp") 255res = ERB.new(File.read(cpp_tests_erb), nil, "%").result(binding) 256File.write(testfile_fullpath, res)