Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <filesystem>
- #include <fstream>
- #include <iostream>
- #include <map>
- #include <ranges>
- #include <sstream>
- #include <string>
- #include <string_view>
- #include <unordered_map>
- struct Count {
- size_t count = 0;
- size_t max_count = 0;
- };
- std::unordered_map<std::string, Count> v_use;
- std::string GetTextFromFile(std::string FilePath) {
- std::ifstream file(FilePath);
- if (!file.is_open()) {
- return "";
- }
- std::ostringstream of_stream;
- of_stream << file.rdbuf();
- file.close();
- return of_stream.str();
- }
- void SaveTextToFile(std::string &text, std::string FilePath) {
- std::istringstream istream(text);
- std::ofstream new_file(FilePath);
- if (!new_file.is_open()) {
- std::cout << "Error with save result into: " << FilePath << std::endl;
- return;
- }
- new_file << text;
- new_file.close();
- }
- void CollectVolatileVar(std::string &text) {
- std::size_t v_pos = 0;
- while (true) {
- v_pos = text.find("volatile", v_pos + 1);
- if (v_pos == std::string::npos) {
- return;
- }
- auto semicolon_pos = text.find(";", v_pos);
- auto decl = text.substr(v_pos, semicolon_pos - v_pos);
- auto name_pos = decl.rfind(' ') + 1;
- auto name = decl.substr(name_pos, semicolon_pos - name_pos);
- v_use.emplace(name, Count{ 0, 0 });
- }
- }
- void InsertIDInFuncall(std::vector<std::string> &func_call_param,
- size_t curr_pos, std::string &text) {
- for (auto ¶m : func_call_param) {
- std::erase_if(
- param, [](char x) { return std::isspace(x) || x == '(' || x == ')'; });
- curr_pos = text.find(param, curr_pos + 1) + param.size();
- auto finded = v_use.find(param);
- if (finded == v_use.end()) {
- continue;
- }
- auto var_count = &finded->second;
- if (var_count->count > 0) {
- text.insert(curr_pos, std::to_string(var_count->count));
- }
- var_count->count++;
- if (var_count->count > var_count->max_count)
- var_count->max_count = var_count->count;
- }
- }
- void FixFuncalls(std::string &text) {
- std::size_t curr_pos = 0;
- while (true) {
- auto left_bracket_pos = text.find('(', curr_pos);
- if (left_bracket_pos == std::string::npos) {
- break;
- }
- auto right_bracket_pos = text.find(')', left_bracket_pos + 1);
- if (right_bracket_pos == std::string::npos) {
- break;
- }
- auto funcall =
- text.substr(left_bracket_pos, right_bracket_pos - left_bracket_pos);
- auto func_call_param = std::ranges::to<std::vector<std::string>>(
- std::ranges::split_view(funcall, ','));
- InsertIDInFuncall(func_call_param, left_bracket_pos, text);
- for (auto &var : v_use) {
- var.second.count = 0;
- }
- curr_pos = right_bracket_pos;
- }
- }
- void AddNewVarDecl(std::string &text)
- {
- for (auto &var : v_use) {
- if (var.second.max_count < 2) { continue; }
- auto curr_pos = text.find(var.first, 0);
- if (curr_pos > text.find("void test", 0)) { continue; } //end decl
- auto begin_pos = text.rfind("\n", curr_pos) + 1;
- auto end_pos = text.find(";", curr_pos) + 2 /*EL*/;
- auto new_line = text.substr(begin_pos, end_pos - begin_pos);
- auto insert_ID_pos = new_line.find(var.first, 0);
- for (size_t i = 1; i < var.second.max_count; i++) {
- auto copy_line = new_line;
- copy_line.insert(insert_ID_pos + var.first.size(), std::to_string(i));
- text.insert(end_pos, copy_line);
- end_pos += copy_line.size();
- }
- }
- }
- int main() {
- std::array<std::string, 16> files{
- "CodeGen/SystemZ/builtins-systemz-i128.c"
- , "CodeGen/SystemZ/builtins-systemz-vector-constrained.c"
- , "CodeGen/SystemZ/builtins-systemz-vector.c"
- , "CodeGen/SystemZ/builtins-systemz-vector2-constrained.c"
- , "CodeGen/SystemZ/builtins-systemz-vector2.c"
- , "CodeGen/SystemZ/builtins-systemz-vector3.c"
- , "CodeGen/SystemZ/builtins-systemz-vector4.c"
- , "CodeGen/SystemZ/builtins-systemz-vector5.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector-constrained.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector-error.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector2-constrained.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector2.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector3.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector4.c"
- , "CodeGen/SystemZ/builtins-systemz-zvector5.c"
- };
- std::string base_path = "";
- for (auto file_path : files) {
- file_path = base_path + file_path;
- auto text = GetTextFromFile(file_path);
- if (text == "") {
- std::cout << "Error with open: " << file_path << std::endl;
- return 1;
- }
- CollectVolatileVar(text);
- FixFuncalls(text);
- AddNewVarDecl(text);
- SaveTextToFile(text, file_path);
- v_use.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment