Guest User

Untitled

a guest
Apr 2nd, 2026
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | Software | 0 0
  1.  
  2. #include <filesystem>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <map>
  6. #include <ranges>
  7. #include <sstream>
  8. #include <string>
  9. #include <string_view>
  10. #include <unordered_map>
  11.  
  12. struct Count {
  13.   size_t count = 0;
  14.   size_t max_count = 0;
  15. };
  16.  
  17. std::unordered_map<std::string, Count> v_use;
  18.  
  19. std::string GetTextFromFile(std::string FilePath) {
  20.   std::ifstream file(FilePath);
  21.   if (!file.is_open()) {
  22.     return "";
  23.   }
  24.  
  25.   std::ostringstream of_stream;
  26.   of_stream << file.rdbuf();
  27.   file.close();
  28.  
  29.   return of_stream.str();
  30. }
  31.  
  32. void SaveTextToFile(std::string &text, std::string FilePath) {
  33.   std::istringstream istream(text);
  34.  
  35.   std::ofstream new_file(FilePath);
  36.   if (!new_file.is_open()) {
  37.     std::cout << "Error with save result into: " << FilePath << std::endl;
  38.     return;
  39.   }
  40.  
  41.   new_file << text;
  42.   new_file.close();
  43. }
  44.  
  45. void CollectVolatileVar(std::string &text) {
  46.   std::size_t v_pos = 0;
  47.   while (true) {
  48.     v_pos = text.find("volatile", v_pos + 1);
  49.     if (v_pos == std::string::npos) {
  50.       return;
  51.     }
  52.  
  53.     auto semicolon_pos = text.find(";", v_pos);
  54.     auto decl = text.substr(v_pos, semicolon_pos - v_pos);
  55.     auto name_pos = decl.rfind(' ') + 1;
  56.     auto name = decl.substr(name_pos, semicolon_pos - name_pos);
  57.  
  58.     v_use.emplace(name, Count{ 0, 0 });
  59.   }
  60. }
  61.  
  62. void InsertIDInFuncall(std::vector<std::string> &func_call_param,
  63.   size_t curr_pos, std::string &text) {
  64.   for (auto &param : func_call_param) {
  65.  
  66.     std::erase_if(
  67.       param, [](char x) { return std::isspace(x) || x == '(' || x == ')'; });
  68.     curr_pos = text.find(param, curr_pos + 1) + param.size();
  69.  
  70.     auto finded = v_use.find(param);
  71.     if (finded == v_use.end()) {
  72.       continue;
  73.     }
  74.  
  75.     auto var_count = &finded->second;
  76.  
  77.     if (var_count->count > 0) {
  78.       text.insert(curr_pos, std::to_string(var_count->count));
  79.     }
  80.  
  81.     var_count->count++;
  82.     if (var_count->count > var_count->max_count)
  83.       var_count->max_count = var_count->count;
  84.   }
  85. }
  86.  
  87. void FixFuncalls(std::string &text) {
  88.   std::size_t curr_pos = 0;
  89.   while (true) {
  90.     auto left_bracket_pos = text.find('(', curr_pos);
  91.     if (left_bracket_pos == std::string::npos) {
  92.       break;
  93.     }
  94.  
  95.     auto right_bracket_pos = text.find(')', left_bracket_pos + 1);
  96.     if (right_bracket_pos == std::string::npos) {
  97.       break;
  98.     }
  99.  
  100.     auto funcall =
  101.       text.substr(left_bracket_pos, right_bracket_pos - left_bracket_pos);
  102.  
  103.     auto func_call_param = std::ranges::to<std::vector<std::string>>(
  104.       std::ranges::split_view(funcall, ','));
  105.  
  106.     InsertIDInFuncall(func_call_param, left_bracket_pos, text);
  107.  
  108.     for (auto &var : v_use) {
  109.       var.second.count = 0;
  110.     }
  111.     curr_pos = right_bracket_pos;
  112.   }
  113. }
  114.  
  115. void AddNewVarDecl(std::string &text)
  116. {
  117.   for (auto &var : v_use) {
  118.     if (var.second.max_count < 2) { continue; }
  119.  
  120.     auto curr_pos = text.find(var.first, 0);
  121.  
  122.     if (curr_pos > text.find("void test", 0)) { continue; } //end decl
  123.  
  124.     auto begin_pos = text.rfind("\n", curr_pos) + 1;
  125.     auto end_pos = text.find(";", curr_pos) + 2 /*EL*/;
  126.  
  127.     auto new_line = text.substr(begin_pos, end_pos - begin_pos);
  128.     auto insert_ID_pos = new_line.find(var.first, 0);
  129.  
  130.     for (size_t i = 1; i < var.second.max_count; i++) {
  131.       auto copy_line = new_line;
  132.       copy_line.insert(insert_ID_pos + var.first.size(), std::to_string(i));
  133.       text.insert(end_pos, copy_line);
  134.       end_pos += copy_line.size();
  135.     }
  136.   }
  137. }
  138.  
  139. int main() {
  140.   std::array<std::string, 16> files{
  141.         "CodeGen/SystemZ/builtins-systemz-i128.c"
  142.       , "CodeGen/SystemZ/builtins-systemz-vector-constrained.c"
  143.       , "CodeGen/SystemZ/builtins-systemz-vector.c"
  144.       , "CodeGen/SystemZ/builtins-systemz-vector2-constrained.c"
  145.       , "CodeGen/SystemZ/builtins-systemz-vector2.c"
  146.       , "CodeGen/SystemZ/builtins-systemz-vector3.c"
  147.       , "CodeGen/SystemZ/builtins-systemz-vector4.c"
  148.       , "CodeGen/SystemZ/builtins-systemz-vector5.c"
  149.       , "CodeGen/SystemZ/builtins-systemz-zvector-constrained.c"
  150.       , "CodeGen/SystemZ/builtins-systemz-zvector-error.c"
  151.       , "CodeGen/SystemZ/builtins-systemz-zvector.c"
  152.       , "CodeGen/SystemZ/builtins-systemz-zvector2-constrained.c"
  153.       , "CodeGen/SystemZ/builtins-systemz-zvector2.c"
  154.       , "CodeGen/SystemZ/builtins-systemz-zvector3.c"
  155.       , "CodeGen/SystemZ/builtins-systemz-zvector4.c"
  156.       , "CodeGen/SystemZ/builtins-systemz-zvector5.c"
  157.   };
  158.  
  159.   std::string base_path = "";
  160.  
  161.   for (auto file_path : files) {
  162.     file_path = base_path + file_path;
  163.     auto text = GetTextFromFile(file_path);
  164.     if (text == "") {
  165.       std::cout << "Error with open: " << file_path << std::endl;
  166.       return 1;
  167.     }
  168.  
  169.     CollectVolatileVar(text);
  170.     FixFuncalls(text);
  171.     AddNewVarDecl(text);
  172.     SaveTextToFile(text, file_path);
  173.     v_use.clear();
  174.   }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment