code refactoring

This commit is contained in:
ALEXks
2024-02-20 11:12:00 +03:00
parent 3221934db8
commit 8402f8c7e9
24 changed files with 1623 additions and 1174 deletions

View File

@@ -331,6 +331,14 @@ set(MAIN _src/Sapfor.cpp
set(PREDICTOR _src/Predictor/PredictScheme.cpp
_src/Predictor/PredictScheme.h)
set(PROJ_MAN _src/ProjectManipulation/ParseFiles.cpp
_src/ProjectManipulation/ParseFiles.h
_src/ProjectManipulation/StdCapture.h
_src/ProjectManipulation/PerfAnalyzer.cpp
_src/ProjectManipulation/PerfAnalyzer.h
_src/ProjectManipulation/FileInfo.cpp
_src/ProjectManipulation/FileInfo.h)
set(PARSER ${parser_sources}/cftn.c
${parser_sources}/errors.c
${parser_sources}/gram1.tab.c
@@ -413,7 +421,8 @@ set(SOURCE_EXE
${PPPA}
${ZLIB}
${GR_LAYOUT}
${PR_PARAM})
${PR_PARAM}
${PROJ_MAN})
add_executable(Sapfor_F ${SOURCE_EXE})
source_group (CFGraph FILES ${CFG})
@@ -454,6 +463,7 @@ source_group (SageExtension FILES ${OMEGA})
source_group (Utils FILES ${UTILS})
source_group (VerificationCode FILES ${VERIF})
source_group (ProjectParameters FILES ${PR_PARAM})
source_group (ProjectManipulation FILES ${PROJ_MAN})
source_group (VisualizerCalls FILES ${VS_CALLS})
source_group (VisualizerCalls\\GraphLayout FILES ${GR_LAYOUT})

View File

@@ -0,0 +1,62 @@
#include "../Utils/leak_detector.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "FileInfo.h"
#include "../Utils/utils.h"
using namespace std;
string FileInfo::convertStyle(bool needRewrite)
{
string tmp_text = text;
vector<string> splited;
splitString(tmp_text, '\n', splited);
tmp_text = "";
int z = 0;
for (auto& line : splited)
{
if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*')
line[0] = '!';
bool needContinuation = false;
if (line[0] != '!' && line.size() > 6)
{
if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label
{
line[5] = ' ';
needContinuation = true;// line[5] = '&';
}
int p = 73;
if (style == 1)
p = 133;
if (line.size() > p)
{
while (line[p] != '\0' && line[p] != '\n' && line[p] != '!')
{
line[p] = ' ';
p++;
if (p >= line.size())
break;
}
}
}
if (needContinuation)
tmp_text += "&";
tmp_text += (z != 0 ? "\n" : "") + line;
++z;
}
if (needRewrite)
writeFileFromStr(fileName, tmp_text);
return tmp_text;
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include <string>
#include <set>
struct FileInfo
{
FileInfo()
{
fileName = "";
options = "";
errPath = "";
outPath = "";
outDepPath = "";
text = "";
error = -1;
includesAdded = 0;
style = -1;
lvl = 0;
}
FileInfo(const std::string& _fileName, const std::string& _options, const std::string& _errPath, const std::string& _outPath,
const std::string& _outDepPath, const std::string& _text, int errorInit = -1)
{
fileName = _fileName;
options = _options;
errPath = _errPath;
outPath = _outPath;
outDepPath = _outDepPath;
text = _text;
error = errorInit;
includesAdded = 0;
style = -1;
lvl = 0;
}
int error;
std::string fileName;
std::string options;
std::string errPath;
std::string outPath;
std::string outDepPath;
std::string text;
int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free
int includesAdded;
std::set<std::string> includes;
int lvl;
std::string convertStyle(bool needRewrite = true);
};

View File

@@ -0,0 +1,789 @@
#include "../Utils/leak_detector.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <string>
#include <assert.h>
#include <locale>
#include <algorithm>
#include <thread>
#include <stack>
#include "../Utils/errors.h"
#include "../Utils/SgUtils.h"
#include "../VisualizerCalls/get_information.h"
#include "../VisualizerCalls/SendMessage.h"
#include "ParseFiles.h"
#include "StdCapture.h"
#include "FileInfo.h"
using namespace std;
extern "C" int parse_file(int argc, char* argv[], char* proj_name);
static void findModuleDeclInProject(const string& name, const vector<string>& files, map<string, string>& modDecls)
{
char** filesList = new char* [files.size()];
for (int z = 0; z < files.size(); ++z)
filesList[z] = (char*)files[z].c_str();
SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size());
int numF = tmpProj->numberOfFiles();
set<SgFile*> filesSg;
for (int z = 0; z < numF; ++z)
{
vector<SgStatement*> modules;
SgFile* currF = &tmpProj->file(z);
string fileName = currF->filename();
convertToLower(fileName);
filesSg.insert(currF);
findModulesInFile(currF, modules);
for (auto& elem : modules)
{
if (string(elem->fileName()) == currF->filename())
{
const string name = elem->symbol()->identifier();
auto it = modDecls.find(name);
if (it != modDecls.end() && it->second != currF->filename())
{
__spf_print(1, "found several module declaration of '%s' in files '%s' and '%s'\n", name.c_str(), it->second.c_str(), currF->filename());
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
else
modDecls.insert(it, make_pair(name, currF->filename()));
}
}
}
delete []filesList;
InitializeTable();
}
static void createIncludeOrder(vector<string> &toIncl,
const map<string, string>& moduleDelc,
const map<string, set<string>>& modDirectOrder,
set<string> &done,
const string &curr)
{
if (done.find(curr) == done.end())
{
for (auto& elem : modDirectOrder.find(curr)->second)
createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, elem);
if (done.find(curr) == done.end())
{
toIncl.push_back(moduleDelc.find(curr)->second);
done.insert(curr);
}
}
}
static set<FileInfo*> applyModuleDeclsForFile(FileInfo *forFile, const map<string, FileInfo*> &mapFiles,
const map<string, string>& moduleDelc,
const map<string, set<string>>& mapModuleDeps,
const map<string, set<string>>& modDirectOrder,
vector<string> &optSplited,
bool includeForInline = false)
{
set<FileInfo*> retFilesMod;
auto itF = mapModuleDeps.find(forFile->fileName);
if (itF == mapModuleDeps.end() && !includeForInline)
return retFilesMod;
vector<string> toIncl;
set<string> done;
if (itF != mapModuleDeps.end())
{
for (auto& mod : itF->second)
if (moduleDelc.find(mod) != moduleDelc.end())
createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, mod);
}
//rewrite files to the next iter of parse
set<FileInfo*> allFiles;
bool needToConvertStyle = false;
for (auto& incl : toIncl)
{
if (mapFiles.find(incl) == mapFiles.end())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
allFiles.insert(mapFiles.find(incl)->second);
}
allFiles.insert(forFile);
int style = forFile->style;
for (auto& elem : allFiles)
{
if (style != elem->style)
{
needToConvertStyle = true;
break;
}
}
string mainText = forFile->text;
if (needToConvertStyle)
{
for (auto& elem : allFiles)
{
if (elem->style != 2)
{
retFilesMod.insert(elem);
if (elem != forFile)
elem->convertStyle();
else
mainText = elem->convertStyle(false);
}
}
if (forFile->style != 2)
{
for (auto& opt : optSplited)
{
if (opt == "-FI" || opt == "-extend_source")
opt = "-FR";
}
}
}
string include = "";
int includeCount = 0;
set<string> included;
for (auto& incl : toIncl)
{
if (included.find(incl) == included.end())
{
include += " include '" + incl + "'\n";
includeCount++;
}
included.insert(incl);
}
vector<string> toInclEnds;
string includeLast = "";
if (includeForInline)
{
//find needed modules first
vector<string> filesWithModules;
for (auto& elem : moduleDelc)
filesWithModules.push_back(elem.second);
for (auto& file : filesWithModules)
{
if (file != forFile->fileName && included.find(file) == included.end())
{
toInclEnds.push_back(file);
included.insert(file);
}
}
for (auto& file : mapFiles)
if (file.second != forFile && included.find(file.second->fileName) == included.end())
toInclEnds.push_back(file.second->fileName);
if (toInclEnds.size())
includeLast += "!SPF SHADOW FILES\n";
for (auto& incl : toInclEnds)
includeLast += " include '" + incl + "'\n";
}
if (includeCount)
include = "!SPF NUM FILES " + std::to_string(includeCount) + "\n" + include;
const string data = include + mainText + includeLast;
__spf_print(1, "include to file %s before\n", forFile->fileName.c_str());
__spf_print(1, "%s", include.c_str());
__spf_print(1, "include to file %s after\n", forFile->fileName.c_str());
__spf_print(1, "%s", includeLast.c_str());
writeFileFromStr(forFile->fileName, data);
forFile->includesAdded = included.size();
forFile->includes = included;
retFilesMod.insert(forFile);
return retFilesMod;
}
static void restoreOriginalText(const vector<FileInfo>& listOfProject)
{
for (auto& elem : listOfProject)
writeFileFromStr(elem.fileName, elem.text);
fflush(NULL);
}
static inline void restoreOriginalText(const FileInfo& file)
{
writeFileFromStr(file.fileName, file.text);
}
static void checkRetCode(FileInfo& info, const string& errorMessage)
{
if (info.error != 0)
info.lvl++;
if (errorMessage.find("Warning 308") != string::npos)
if (info.error == 0)
info.error = 1;
}
static vector<string> parseList(vector<FileInfo>& listOfProject,
bool needToInclude, bool needToIncludeForInline,
const map<string, set<string>> &mapModuleDeps,
const map<string, string> &moduleDelc,
const map<string, set<string>> &modDirectOrder, bool isFromConsole = false)
{
map<string, FileInfo*> mapFiles;
for (auto& elem : listOfProject)
mapFiles[elem.fileName] = &elem;
vector<string> errors;
int i = 1;
int N = listOfProject.size();
for (auto& elem : listOfProject)
{
sendMessage_progress(std::to_wstring((int)(((double)(i++) / N) * 100)));
string file = elem.fileName;
string options = elem.options;
vector<string> optSplited = splitAndArgvCreate(options);
char** toParse = new char* [optSplited.size() + 1];
for (int z = 0; z < optSplited.size(); ++z)
{
toParse[z] = new char[optSplited[z].size() + 1];
strcpy(toParse[z], optSplited[z].c_str());
}
toParse[optSplited.size()] = new char[file.size() + 1];
strcpy(toParse[optSplited.size()], file.c_str());
if (options.find("-FI") != string::npos)
elem.style = 0;
else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos)
elem.style = 2;
else if (options.find("-extend_source") != string::npos)
elem.style = 1;
for (int z = 0; z < optSplited.size(); ++z)
{
if (optSplited[z] == "-o")
{
if (z + 1 == optSplited.size())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
elem.outDepPath = optSplited[z + 1];
break;
}
}
FILE* depPath = fopen(elem.outDepPath.c_str(), "r");
if (depPath && !isFromConsole)
{
fclose(depPath);
if (elem.error <= 0)
{
elem.error = 0;
errors.push_back("");
for (int z = 0; z <= optSplited.size(); ++z)
delete toParse[z];
delete[] toParse;
continue;
}
}
#ifdef _WIN32
sendMessage_2lvl(L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> '" + to_wstring(file) + L"'");
#else
sendMessage_2lvl(L" processing file '" + to_wstring(file) + L"'");
#endif
StdCapture stdCapture;
stdCapture.Init();
string errorMessage = "";
try
{
set<FileInfo*> filesModified;
stdCapture.BeginCapture();
if (needToInclude)
filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline);
else if (needToIncludeForInline) // TODO for modules
filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline);
int retCode = parse_file(optSplited.size(), toParse, "dvm.proj");
if (needToInclude || needToIncludeForInline)
{
for (auto &elem : filesModified)
restoreOriginalText(*elem);
fflush(NULL);
}
elem.error = retCode;
stdCapture.EndCapture();
errorMessage = stdCapture.GetCapture();
checkRetCode(elem, errorMessage);
}
catch (int err)
{
stdCapture.EndCapture();
errorMessage = stdCapture.GetCapture();
if (needToInclude || needToIncludeForInline)
restoreOriginalText(listOfProject);
}
catch (...)
{
stdCapture.EndCapture();
errorMessage = stdCapture.GetCapture();
if (needToInclude || needToIncludeForInline)
restoreOriginalText(listOfProject);
}
errors.push_back(errorMessage);
for (int z = 0; z <= optSplited.size(); ++z)
delete toParse[z];
delete[] toParse;
createNeededException();
}
return errors;
}
static string shiftLines(const string &in, const map<string, const FileInfo*> &mapOfFiles, const FileInfo* currF)
{
int byNum = 0;
auto it = in.find("on line ");
if (it != string::npos)
it += strlen("on line ");
int d = 0;
sscanf(in.c_str() + it, "%d", &d);
auto it1 = in.find("of", it + 1);
if (it1 == string::npos)
return in;
it1 += 3;
string fileN = in.substr(it1, in.find(':', it1) - it1);
auto itF = mapOfFiles.find(fileN);
if (itF == mapOfFiles.end())
return in;
if (itF->second != currF)
return in;
byNum = itF->second->includesAdded;
if (byNum == 0)
return in;
if (d - byNum <= 0)
{
//return in;
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
else
d -= byNum;
string newStr = in.substr(0, it) + std::to_string(d) + in.substr(in.find(' ', it + 1));
return newStr;
}
static int dumpErrors(const vector<FileInfo>& listOfProject, const vector<string>& errors)
{
int errorsCount = 0;
map<string, const FileInfo*> mapOfFiles;
for (auto& elem : listOfProject)
mapOfFiles[elem.fileName] = &elem;
int z = 0;
for (auto& file : listOfProject)
{
if (errors[z] == "")
{
FILE* ferr = fopen(file.errPath.c_str(), "w");
if (!ferr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
fclose(ferr);
++z;
continue;
}
FILE* ferr = fopen(file.errPath.c_str(), "w");
FILE* fout = fopen(file.outPath.c_str(), "w");
if (!ferr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (!fout)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
string errS = "", outS = "";
vector<string> splited;
splitString(errors[z], '\n', splited);
for (auto& elem : splited)
{
if (elem.find("Warning 308") != string::npos)
outS += shiftLines(elem, mapOfFiles, &file) + "\n";
else if (elem.find("Error") != string::npos)
{
errS += shiftLines(elem, mapOfFiles, &file) + "\n";
errorsCount++;
}
}
fprintf(fout, "%s", outS.c_str());
fprintf(ferr, "%s", errS.c_str());
fflush(NULL);
fclose(fout);
fclose(ferr);
++z;
}
return errorsCount;
}
static int createMapOfUse(const vector<string>& errors, const vector<FileInfo>& listOfProject, map<string, set<string>> &mapModuleDeps)
{
int changed = 0;
for (int z = 0; z < listOfProject.size(); ++z)
{
if (listOfProject[z].error >= 0)
{
vector<string> splited;
splitString(errors[z], '\n', splited);
for (auto& err : splited)
{
if (err.find("Warning 308") != string::npos && err.find(listOfProject[z].fileName) != string::npos)
{
auto pos = err.find("Unknown module");
if (pos != string::npos)
{
pos += strlen("Unknown module") + 1;
string substr = "";
while (err[pos] != ' ' && pos != err.size())
substr += err[pos++];
mapModuleDeps[listOfProject[z].fileName].insert(substr);
changed++;
}
}
}
}
}
return changed;
}
static map<string, set<string>> createModuleOrder(const map<string, string> &moduleDelc, const map<string, set<string>> &mapModuleDeps)
{
map<string, set<string>> modDirectOrder;
for (auto& elem : moduleDelc)
modDirectOrder[elem.first] = set<string>();
for (auto& elem : moduleDelc)
{
auto itF = mapModuleDeps.find(elem.second);
if (itF != mapModuleDeps.end())
{
for (auto& inFile : itF->second)
{
if (moduleDelc.find(inFile) != moduleDelc.end())
modDirectOrder[elem.first].insert(inFile);
}
}
}
return modDirectOrder;
}
static void printDebug(const map<string, set<string>>& mapModuleDeps, const map<string, set<string>>& modDirectOrder,
const vector<FileInfo>& listOfProject, bool console = false)
{
string toPrint = "MODULE DEPS:\n";
for (auto& elem : mapModuleDeps)
{
toPrint += elem.first + '\n';
for (auto& setEl : elem.second)
toPrint += " " + setEl + '\n';
}
toPrint += "MODULE DIRECT ORDER:\n";
for (auto& elem : modDirectOrder)
{
toPrint += elem.first + '\n';
for (auto& setEl : elem.second)
toPrint += " " + setEl + '\n';
}
toPrint += "FILES LVL:\n";
for (auto& elem : listOfProject)
toPrint += elem.fileName + " " + elem.outDepPath + " lvl = " + std::to_string(elem.lvl) + '\n';
if (console)
printf("%s\n", toPrint.c_str());
__spf_print(1, "%s\n", toPrint.c_str());
}
static int parseFiles(vector<string>& errors, vector<FileInfo>& listOfProject, vector<string>& filesCompilationOrder,
int parseForInlining, bool isFromConsole = false)
{
int rethrow = 0;
int iters = 0;
int changed = 0;
int lastChanged = 0;
const string projName = "tmp";
map<string, set<string>> mapModuleDeps;
map<string, string> moduleDelc;
map<string, set<string>> modDirectOrder;
try
{
do
{
#ifdef _WIN32
sendMessage_1lvl(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " + std::to_wstring((iters + 1)) + L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
#else
sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys");
#endif
errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole);
changed = createMapOfUse(errors, listOfProject, mapModuleDeps);
if (iters != 0)
if (lastChanged <= changed)
break;
createNeededException();
if (changed)
{
vector<string> files;
for (auto& elem : listOfProject)
if (elem.error == 0)
files.push_back(elem.outDepPath);
if (files.size() == 0)
break;
findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc);
modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps);
}
lastChanged = changed;
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
} while (changed);
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
int added = 0;
int iter = 0;
vector<string> files;
while (added != listOfProject.size())
{
for (auto& elem : listOfProject)
{
if (elem.lvl == iter)
{
files.push_back(elem.fileName);
added++;
}
}
++iter;
}
map<string, set<string>> fileDeps;
for (auto& file : files)
{
fileDeps[file] = set<string>();
if (mapModuleDeps.find(file) == mapModuleDeps.end())
continue;
for (auto& dep : mapModuleDeps[file])
{
if (moduleDelc.find(dep) == moduleDelc.end())
continue;
fileDeps[file].insert(moduleDelc[dep]);
}
}
set<string> addedFiles;
added = 0;
while (added != fileDeps.size())
{
for (auto& file : fileDeps)
{
bool depsAdded = true;
for (auto& dep : file.second)
if (addedFiles.find(dep) == addedFiles.end())
depsAdded = false;
if (depsAdded && addedFiles.find(file.first) == addedFiles.end())
{
filesCompilationOrder.push_back(file.first);
addedFiles.insert(file.first);
added++;
}
}
}
__spf_print(1, "files compilation order:\n");
for (auto& file : filesCompilationOrder)
__spf_print(1, " %s\n", file.c_str());
}
catch (int err)
{
rethrow = err;
}
catch (...)
{
rethrow = -1;
}
return rethrow;
}
int parseFiles(const char* proj, vector<string>& filesCompilationOrder, int parseForInlining)
{
FILE* list = fopen(proj, "r");
if (!list)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
vector<string> pathSplit;
if (string(proj).find('\\') != string::npos)
splitString(proj, '\\', pathSplit);
else
splitString(proj, '/', pathSplit);
if (pathSplit.size() < 2)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (pathSplit[pathSplit.size() - 2] != "visualiser_data")
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
string fullPath = "";
for (int z = 0; z < pathSplit.size() - 2; ++z)
fullPath += pathSplit[z] + "/";
if (fullPath == "")
fullPath = "./";
else
{
//change dir
if (chdir(fullPath.c_str()) != 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
vector<FileInfo> listOfProject;
while (!feof(list))
{
char buf[1024];
if (fgets(buf, 1024, list) == NULL)
continue;
string toAdd = buf;
if (toAdd[toAdd.size() - 1] == '\n')
toAdd = toAdd.erase(toAdd.size() - 1);
string fileNameFixed = "";
auto idx = toAdd.find(fullPath);
if (idx != string::npos)
fileNameFixed = toAdd.substr(idx + fullPath.size());
else
fileNameFixed = (toAdd.substr(0, 2) == "./") ? toAdd.substr(2) : toAdd;
const string optPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".opt";
const string errPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".err";
const string outPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".out";
const string fileText = readFileToStr(toAdd);
FILE* opt = fopen(optPath.c_str(), "r");
if (!opt)
{
__spf_print(1, "can not open path %s\n", optPath.c_str());
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
fgets(buf, 1024, opt);
string toAddOpt = buf;
if (toAddOpt[toAddOpt.size() - 1] == '\n')
toAddOpt = toAddOpt.erase(toAddOpt.size() - 1);
fclose(opt);
listOfProject.push_back(FileInfo(fileNameFixed, toAddOpt, errPath, outPath, "", fileText));
}
fclose(list);
vector<string> errors;
int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, parseForInlining);
int errCount = dumpErrors(listOfProject, errors);
if (rethrow != 0)
throw rethrow;
return -errCount;
}
void parseFiles(int argc, char** argv)
{
bool isInline = false;
auto result = splitCommandLineForParse(argv, argc, isInline);
if (result.second.size() == 0)
{
printf("Nothing to parse\n");
exit(0);
}
int code = 0;
vector<string> errors;
vector<FileInfo> listOfProject;
string toAddOpt = "";
for (auto& opt : result.first)
toAddOpt += opt + " ";
for (auto& file : result.second)
{
const string fileText = readFileToStr(file);
listOfProject.push_back(FileInfo(file, toAddOpt + "-o " + file + ".dep", "", "", "", fileText, 0));
}
vector<string> filesCompilationOrder;
int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true);
if (rethrow == 0)
{
for (auto& err : errors)
{
vector<string> splited;
splitString(err, '\n', splited);
for (auto& elem : splited)
{
if (elem.find("Error") != string::npos)
{
printf("%s\n", elem.c_str());
code++;
}
}
}
}
if (code == 0 && rethrow == 0)
{
FILE* proj = fopen("dvm.proj", "w");
if (proj == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
for (auto& file : result.second)
fprintf(proj, "%s.dep\n", file.c_str());
printf("Parsing was completed successfully\n");
}
else
printf("Parsing was completed with errors, throw code %d, errors count %d\n", rethrow, code);
exit(0);
}

View File

@@ -0,0 +1,7 @@
#pragma once
#include <vector>
#include <string>
int parseFiles(const char* proj, std::vector<std::string>& filesCompilationOrder, int parseForInlining);
void parseFiles(int argc, char** argv);

View File

@@ -0,0 +1,38 @@
#include "../Utils/leak_detector.h"
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "../Utils/errors.h"
#include "../Utils/SgUtils.h"
#include "../VisualizerCalls/get_information.h"
#include "../VisualizerCalls/SendMessage.h"
#include "StdCapture.h"
using namespace std;
extern int pppa_analyzer(int argv, char** argc);
int pppaAnalyzer(const char* options)
{
string optionsS(options);
vector<string> splited = splitAndArgvCreate(optionsS);
char** argv = new char* [splited.size()];
for (int z = 0; z < splited.size(); ++z)
argv[z] = (char*)splited[z].c_str();
StdCapture stdCapture;
stdCapture.Init();
string errorMessage = "";
int retCode = pppa_analyzer(splited.size(), argv);
stdCapture.EndCapture();
errorMessage = stdCapture.GetCapture();
delete []argv;
return retCode;
}

View File

@@ -0,0 +1,3 @@
#pragma once
int pppaAnalyzer(const char* options);

View File

@@ -0,0 +1,190 @@
#pragma once
#include <string>
#include <mutex>
#include <fcntl.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#define popen _popen
#define pclose _pclose
#define stat _stat
#define dup _dup
#define dup2 _dup2
#define fileno _fileno
#define close _close
#define pipe _pipe
#define read _read
#define eof _eof
#else
#include <unistd.h>
#endif
#ifndef STD_OUT_FD
#define STD_OUT_FD (fileno(stdout))
#endif
#ifndef STD_ERR_FD
#define STD_ERR_FD (fileno(stderr))
#endif
class StdCapture
{
int m_pipe[2];
int m_oldStdOut;
int m_oldStdErr;
bool m_capturing;
std::mutex m_mutex;
std::string m_captured;
enum PIPES { READ, WRITE };
int secure_dup(int src)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = dup(src);
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
return ret;
}
void secure_pipe(int* pipes)
{
int ret = -1;
bool fd_blocked = false;
do
{
#ifdef _WIN32
ret = pipe(pipes, 1024 * 1024 * 20, O_BINARY); // 20 MB
#else
ret = pipe(pipes) == -1;
fcntl(*pipes, F_SETPIPE_SZ, 1024 * 1024 * 20);
#endif
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
}
void secure_dup2(int src, int dest)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = dup2(src, dest);
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
}
void secure_close(int& fd)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = close(fd);
fd_blocked = (errno == EINTR);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
fd = -1;
}
public:
void Init()
{
// make stdout & stderr streams unbuffered
// so that we don't need to flush the streams
// before capture and after capture
// (fflush can cause a deadlock if the stream is currently being
std::lock_guard<std::mutex> lock(m_mutex);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
void BeginCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_capturing)
return;
secure_pipe(m_pipe);
m_oldStdOut = secure_dup(STD_OUT_FD);
m_oldStdErr = secure_dup(STD_ERR_FD);
secure_dup2(m_pipe[WRITE], STD_OUT_FD);
secure_dup2(m_pipe[WRITE], STD_ERR_FD);
m_capturing = true;
#ifndef _WIN32
secure_close(m_pipe[WRITE]);
#endif
}
bool IsCapturing()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_capturing;
}
void EndCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_capturing)
return;
m_captured.clear();
secure_dup2(m_oldStdOut, STD_OUT_FD);
secure_dup2(m_oldStdErr, STD_ERR_FD);
const int bufSize = 1025;
char buf[bufSize];
int bytesRead = 0;
bool fd_blocked(false);
do
{
bytesRead = 0;
fd_blocked = false;
#ifdef _WIN32
if (!eof(m_pipe[READ]))
bytesRead = read(m_pipe[READ], buf, bufSize - 1);
#else
bytesRead = read(m_pipe[READ], buf, bufSize - 1);
#endif
if (bytesRead > 0)
{
buf[bytesRead] = 0;
m_captured += buf;
}
else if (bytesRead < 0)
{
fd_blocked = (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} while (fd_blocked || bytesRead == (bufSize - 1));
secure_close(m_oldStdOut);
secure_close(m_oldStdErr);
secure_close(m_pipe[READ]);
#ifdef _WIN32
secure_close(m_pipe[WRITE]);
#endif
m_capturing = false;
}
std::string GetCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_captured;
}
};

View File

@@ -33,6 +33,8 @@
#include "Utils/errors.h"
#include "Utils/SgUtils.h"
#include "ProjectManipulation/ParseFiles.h"
#include "LoopAnalyzer/loop_analyzer.h"
#include "LoopAnalyzer/loop_analyzer_nodist.h"
@@ -2564,11 +2566,8 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
}
}
extern "C" int parse_file(int argc, char* argv[], char* proj_name);
extern int pppa_analyzer(int argv, char** argc);
bool runAsClient = false;
int main(int argc, char **argv)
{
int leakMemDump = 0;
@@ -2730,62 +2729,7 @@ int main(int argc, char **argv)
else if (string(curr_arg) == "-autoArray")
parallizeFreeLoops = 1;
else if (string(curr_arg) == "-parse")
{
bool isInline = false;
auto result = splitCommandLineForParse(argv + (i + 1), argc - (i + 1), isInline);
if (result.second.size() == 0)
{
printf("Nothing to parse\n");
exit(0);
}
int code = 0;
vector<string> errors;
vector<FileInfo> listOfProject;
string toAddOpt = "";
for (auto& opt : result.first)
toAddOpt += opt + " ";
for (auto& file : result.second)
{
const string fileText = readFileToStr(file);
listOfProject.push_back(FileInfo(file, toAddOpt + "-o " + file + ".dep", "", "", "", fileText, 0));
}
int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, isInline, true);
if (rethrow == 0)
{
for (auto& err : errors)
{
vector<string> splited;
splitString(err, '\n', splited);
for (auto& elem : splited)
{
if (elem.find("Error") != string::npos)
{
printf("%s\n", elem.c_str());
code++;
}
}
}
}
if (code == 0 && rethrow == 0)
{
FILE* proj = fopen("dvm.proj", "w");
if (proj == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
for (auto& file : result.second)
fprintf(proj, "%s.dep\n", file.c_str());
printf("Parsing was completed successfully\n");
}
else
printf("Parsing was completed with errors, throw code %d, errors count %d\n", rethrow, code);
exit(0);
}
parseFiles(argc - (i + 1), argv + (i + 1));
else if (string(curr_arg) == "-mpi")
mpiProgram = 1;
else if (string(curr_arg) == "-pppa")

View File

@@ -47,6 +47,7 @@ bool fullDepGraph = false;
bool noLogo = false;
bool withTemplateInfo = false;
bool inlcudeAllFiles = false; // for pass INSERT_INLCUDES
bool runAsClient = false; // run console project as client for Visualizer
uint64_t currentAvailMemory = 0;
int QUALITY; // quality of conflicts search in graph

View File

@@ -23,6 +23,7 @@
#include <mutex>
#include <thread>
#include <stack>
#ifdef _WIN32
#include <direct.h>
#else
@@ -58,30 +59,6 @@ using std::make_tuple;
using std::wstring;
using std::stack;
#ifdef _MSC_VER
#include <io.h>
#define popen _popen
#define pclose _pclose
#define stat _stat
#define dup _dup
#define dup2 _dup2
#define fileno _fileno
#define close _close
#define pipe _pipe
#define read _read
#define eof _eof
#else
#include <unistd.h>
#endif
#ifndef STD_OUT_FD
#define STD_OUT_FD (fileno(stdout))
#endif
#ifndef STD_ERR_FD
#define STD_ERR_FD (fileno(stderr))
#endif
static bool ifIntervalExists(const vector<pair<int, int>> &intervals, const pair<int, int> &toFind, pair<int, int>& inInterval)
{
bool retVal = false;
@@ -2768,903 +2745,6 @@ vector<SgStatement*> makeDeclaration(const vector<SgSymbol*>& symbolsToDeclare,
return allDecls;
}
static void findModuleDeclInProject(const string &name, const vector<string> &files, map<string, string> &modDecls)
{
char** filesList = new char* [files.size()];
for (int z = 0; z < files.size(); ++z)
filesList[z] = (char*)files[z].c_str();
SgProject* tmpProj = new SgProject(name.c_str(), filesList, files.size());
int numF = tmpProj->numberOfFiles();
set<SgFile*> filesSg;
for (int z = 0; z < numF; ++z)
{
vector<SgStatement*> modules;
SgFile* currF = &tmpProj->file(z);
string fileName = currF->filename();
convertToLower(fileName);
filesSg.insert(currF);
findModulesInFile(currF, modules);
for (auto& elem : modules)
{
if (string(elem->fileName()) == currF->filename())
{
const string name = elem->symbol()->identifier();
auto it = modDecls.find(name);
if (it != modDecls.end() && it->second != currF->filename())
{
__spf_print(1, "found several module declaration of '%s' in files '%s' and '%s'\n", name.c_str(), it->second.c_str(), currF->filename());
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
else
modDecls.insert(it, make_pair(name, currF->filename()));
}
}
}
delete[]filesList;
InitializeTable();
}
class StdCapture
{
public:
static void Init()
{
// make stdout & stderr streams unbuffered
// so that we don't need to flush the streams
// before capture and after capture
// (fflush can cause a deadlock if the stream is currently being
std::lock_guard<std::mutex> lock(m_mutex);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
static void BeginCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_capturing)
return;
secure_pipe(m_pipe);
m_oldStdOut = secure_dup(STD_OUT_FD);
m_oldStdErr = secure_dup(STD_ERR_FD);
secure_dup2(m_pipe[WRITE], STD_OUT_FD);
secure_dup2(m_pipe[WRITE], STD_ERR_FD);
m_capturing = true;
#ifndef _MSC_VER
secure_close(m_pipe[WRITE]);
#endif
}
static bool IsCapturing()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_capturing;
}
static void EndCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_capturing)
return;
m_captured.clear();
secure_dup2(m_oldStdOut, STD_OUT_FD);
secure_dup2(m_oldStdErr, STD_ERR_FD);
const int bufSize = 1025;
char buf[bufSize];
int bytesRead = 0;
bool fd_blocked(false);
do
{
bytesRead = 0;
fd_blocked = false;
#ifdef _MSC_VER
if (!eof(m_pipe[READ]))
bytesRead = read(m_pipe[READ], buf, bufSize - 1);
#else
bytesRead = read(m_pipe[READ], buf, bufSize - 1);
#endif
if (bytesRead > 0)
{
buf[bytesRead] = 0;
m_captured += buf;
}
else if (bytesRead < 0)
{
fd_blocked = (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} while (fd_blocked || bytesRead == (bufSize - 1));
secure_close(m_oldStdOut);
secure_close(m_oldStdErr);
secure_close(m_pipe[READ]);
#ifdef _MSC_VER
secure_close(m_pipe[WRITE]);
#endif
m_capturing = false;
}
static std::string GetCapture()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_captured;
}
private:
enum PIPES { READ, WRITE };
static int secure_dup(int src)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = dup(src);
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
return ret;
}
static void secure_pipe(int* pipes)
{
int ret = -1;
bool fd_blocked = false;
do
{
#ifdef _MSC_VER
ret = pipe(pipes, 1024 * 1024 * 20, O_BINARY); // 20 MB
#else
ret = pipe(pipes) == -1;
fcntl(*pipes, F_SETPIPE_SZ, 1024 * 1024 * 20);
#endif
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
}
static void secure_dup2(int src, int dest)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = dup2(src, dest);
fd_blocked = (errno == EINTR || errno == EBUSY);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
}
static void secure_close(int& fd)
{
int ret = -1;
bool fd_blocked = false;
do
{
ret = close(fd);
fd_blocked = (errno == EINTR);
if (fd_blocked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while (ret < 0);
fd = -1;
}
static int m_pipe[2];
static int m_oldStdOut;
static int m_oldStdErr;
static bool m_capturing;
static std::mutex m_mutex;
static std::string m_captured;
};
// actually define vars.
int StdCapture::m_pipe[2];
int StdCapture::m_oldStdOut;
int StdCapture::m_oldStdErr;
bool StdCapture::m_capturing;
std::mutex StdCapture::m_mutex;
std::string StdCapture::m_captured;
static vector<string> splitAndArgvCreate(const string& options)
{
vector<string> optSplited;
optSplited.push_back("");
splitString(options, ' ', optSplited, true);
vector<string> optSplited1;
for (auto& elem : optSplited)
if (elem != "")
optSplited1.push_back(elem);
optSplited1.insert(optSplited1.begin(), "");
for (int z = 0; z < optSplited1.size(); ++z)
{
//printf("%s\n", optSplited1[z].c_str());
if (optSplited1[z][0] == '"' && optSplited1[z][optSplited1[z].size() - 1] == '"')
optSplited1[z] = optSplited1[z].substr(1, optSplited1[z].size() - 2);
}
return optSplited1;
}
static void createIncludeOrder(vector<string> &toIncl,
const map<string, string>& moduleDelc,
const map<string, set<string>>& modDirectOrder,
set<string> &done,
const string &curr)
{
if (done.find(curr) == done.end())
{
for (auto& elem : modDirectOrder.find(curr)->second)
createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, elem);
if (done.find(curr) == done.end())
{
toIncl.push_back(moduleDelc.find(curr)->second);
done.insert(curr);
}
}
}
static set<FileInfo*> applyModuleDeclsForFile(FileInfo *forFile, const map<string, FileInfo*> &mapFiles,
const map<string, string>& moduleDelc,
const map<string, set<string>>& mapModuleDeps,
const map<string, set<string>>& modDirectOrder,
vector<string> &optSplited,
bool includeForInline = false)
{
set<FileInfo*> retFilesMod;
auto itF = mapModuleDeps.find(forFile->fileName);
if (itF == mapModuleDeps.end() && !includeForInline)
return retFilesMod;
vector<string> toIncl;
set<string> done;
if (itF != mapModuleDeps.end())
{
for (auto& mod : itF->second)
if (moduleDelc.find(mod) != moduleDelc.end())
createIncludeOrder(toIncl, moduleDelc, modDirectOrder, done, mod);
}
//rewrite files to the next iter of parse
set<FileInfo*> allFiles;
bool needToConvertStyle = false;
for (auto& incl : toIncl)
{
if (mapFiles.find(incl) == mapFiles.end())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
allFiles.insert(mapFiles.find(incl)->second);
}
allFiles.insert(forFile);
int style = forFile->style;
for (auto& elem : allFiles)
{
if (style != elem->style)
{
needToConvertStyle = true;
break;
}
}
string mainText = forFile->text;
if (needToConvertStyle)
{
for (auto& elem : allFiles)
{
if (elem->style != 2)
{
retFilesMod.insert(elem);
if (elem != forFile)
auto tmp = convertStyle(elem);
else
mainText = convertStyle(elem, false);
}
}
if (forFile->style != 2)
{
for (auto& opt : optSplited)
{
if (opt == "-FI" || opt == "-extend_source")
opt = "-FR";
}
}
}
string include = "";
int includeCount = 0;
set<string> included;
for (auto& incl : toIncl)
{
if (included.find(incl) == included.end())
{
include += " include '" + incl + "'\n";
includeCount++;
}
included.insert(incl);
}
vector<string> toInclEnds;
string includeLast = "";
if (includeForInline)
{
//find needed modules first
vector<string> filesWithModules;
for (auto& elem : moduleDelc)
filesWithModules.push_back(elem.second);
for (auto& file : filesWithModules)
{
if (file != forFile->fileName && included.find(file) == included.end())
{
toInclEnds.push_back(file);
included.insert(file);
}
}
for (auto& file : mapFiles)
if (file.second != forFile && included.find(file.second->fileName) == included.end())
toInclEnds.push_back(file.second->fileName);
if (toInclEnds.size())
includeLast += "!SPF SHADOW FILES\n";
for (auto& incl : toInclEnds)
includeLast += " include '" + incl + "'\n";
}
if (includeCount)
include = "!SPF NUM FILES " + std::to_string(includeCount) + "\n" + include;
const string data = include + mainText + includeLast;
__spf_print(1, "include to file %s before\n", forFile->fileName.c_str());
__spf_print(1, "%s", include.c_str());
__spf_print(1, "include to file %s after\n", forFile->fileName.c_str());
__spf_print(1, "%s", includeLast.c_str());
writeFileFromStr(forFile->fileName, data);
forFile->includesAdded = included.size();
forFile->includes = included;
retFilesMod.insert(forFile);
return retFilesMod;
}
static void restoreOriginalText(const vector<FileInfo>& listOfProject)
{
for (auto& elem : listOfProject)
writeFileFromStr(elem.fileName, elem.text);
fflush(NULL);
}
static inline void restoreOriginalText(const FileInfo& file)
{
writeFileFromStr(file.fileName, file.text);
}
static void checkRetCode(FileInfo& info, const string& errorMessage)
{
if (info.error != 0)
info.lvl++;
if (errorMessage.find("Warning 308") != string::npos)
if (info.error == 0)
info.error = 1;
}
extern "C" int parse_file(int argc, char* argv[], char* proj_name);
static vector<string> parseList(vector<FileInfo>& listOfProject,
bool needToInclude, bool needToIncludeForInline,
const map<string, set<string>> &mapModuleDeps,
const map<string, string> &moduleDelc,
const map<string, set<string>> &modDirectOrder, bool isFromConsole = false)
{
map<string, FileInfo*> mapFiles;
for (auto& elem : listOfProject)
mapFiles[elem.fileName] = &elem;
vector<string> errors;
int i = 1;
int N = listOfProject.size();
for (auto& elem : listOfProject)
{
sendMessage_progress(std::to_wstring((int)(((double)(i++) / N) * 100)));
string file = elem.fileName;
string options = elem.options;
vector<string> optSplited = splitAndArgvCreate(options);
char** toParse = new char* [optSplited.size() + 1];
for (int z = 0; z < optSplited.size(); ++z)
{
toParse[z] = new char[optSplited[z].size() + 1];
strcpy(toParse[z], optSplited[z].c_str());
}
toParse[optSplited.size()] = new char[file.size() + 1];
strcpy(toParse[optSplited.size()], file.c_str());
if (options.find("-FI") != string::npos)
elem.style = 0;
else if (options.find("-FR") != string::npos || options.find("-f90") != string::npos)
elem.style = 2;
else if (options.find("-extend_source") != string::npos)
elem.style = 1;
for (int z = 0; z < optSplited.size(); ++z)
{
if (optSplited[z] == "-o")
{
if (z + 1 == optSplited.size())
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
elem.outDepPath = optSplited[z + 1];
break;
}
}
FILE* depPath = fopen(elem.outDepPath.c_str(), "r");
if (depPath && !isFromConsole)
{
fclose(depPath);
if (elem.error <= 0)
{
elem.error = 0;
errors.push_back("");
for (int z = 0; z <= optSplited.size(); ++z)
delete toParse[z];
delete[] toParse;
continue;
}
}
#ifdef _WIN32
sendMessage_2lvl(L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> '" + to_wstring(file) + L"'");
#else
sendMessage_2lvl(L" processing file '" + to_wstring(file) + L"'");
#endif
StdCapture::Init();
string errorMessage = "";
try
{
set<FileInfo*> filesModified;
StdCapture::BeginCapture();
if (needToInclude)
filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline);
else if (needToIncludeForInline) // TODO for modules
filesModified = applyModuleDeclsForFile(&elem, mapFiles, moduleDelc, mapModuleDeps, modDirectOrder, optSplited, needToIncludeForInline);
int retCode = parse_file(optSplited.size(), toParse, "dvm.proj");
if (needToInclude || needToIncludeForInline)
{
for (auto &elem : filesModified)
restoreOriginalText(*elem);
fflush(NULL);
}
elem.error = retCode;
StdCapture::EndCapture();
errorMessage = StdCapture::GetCapture();
checkRetCode(elem, errorMessage);
}
catch (int err)
{
StdCapture::EndCapture();
errorMessage = StdCapture::GetCapture();
if (needToInclude || needToIncludeForInline)
restoreOriginalText(listOfProject);
}
catch (...)
{
StdCapture::EndCapture();
errorMessage = StdCapture::GetCapture();
if (needToInclude || needToIncludeForInline)
restoreOriginalText(listOfProject);
}
errors.push_back(errorMessage);
for (int z = 0; z <= optSplited.size(); ++z)
delete toParse[z];
delete[] toParse;
createNeededException();
}
return errors;
}
static string shiftLines(const string &in, const map<string, const FileInfo*> &mapOfFiles, const FileInfo* currF)
{
int byNum = 0;
auto it = in.find("on line ");
if (it != string::npos)
it += strlen("on line ");
int d = 0;
sscanf(in.c_str() + it, "%d", &d);
auto it1 = in.find("of", it + 1);
if (it1 == string::npos)
return in;
it1 += 3;
string fileN = in.substr(it1, in.find(':', it1) - it1);
auto itF = mapOfFiles.find(fileN);
if (itF == mapOfFiles.end())
return in;
if (itF->second != currF)
return in;
byNum = itF->second->includesAdded;
if (byNum == 0)
return in;
if (d - byNum <= 0)
{
//return in;
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
else
d -= byNum;
string newStr = in.substr(0, it) + std::to_string(d) + in.substr(in.find(' ', it + 1));
return newStr;
}
static int dumpErrors(const vector<FileInfo>& listOfProject, const vector<string>& errors)
{
int errorsCount = 0;
map<string, const FileInfo*> mapOfFiles;
for (auto& elem : listOfProject)
mapOfFiles[elem.fileName] = &elem;
int z = 0;
for (auto& file : listOfProject)
{
if (errors[z] == "")
{
FILE* ferr = fopen(file.errPath.c_str(), "w");
if (!ferr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
fclose(ferr);
++z;
continue;
}
FILE* ferr = fopen(file.errPath.c_str(), "w");
FILE* fout = fopen(file.outPath.c_str(), "w");
if (!ferr)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (!fout)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
string errS = "", outS = "";
vector<string> splited;
splitString(errors[z], '\n', splited);
for (auto& elem : splited)
{
if (elem.find("Warning 308") != string::npos)
outS += shiftLines(elem, mapOfFiles, &file) + "\n";
else if (elem.find("Error") != string::npos)
{
errS += shiftLines(elem, mapOfFiles, &file) + "\n";
errorsCount++;
}
}
fprintf(fout, "%s", outS.c_str());
fprintf(ferr, "%s", errS.c_str());
fflush(NULL);
fclose(fout);
fclose(ferr);
++z;
}
return errorsCount;
}
static int createMapOfUse(const vector<string>& errors, const vector<FileInfo>& listOfProject, map<string, set<string>> &mapModuleDeps)
{
int changed = 0;
for (int z = 0; z < listOfProject.size(); ++z)
{
if (listOfProject[z].error >= 0)
{
vector<string> splited;
splitString(errors[z], '\n', splited);
for (auto& err : splited)
{
if (err.find("Warning 308") != string::npos && err.find(listOfProject[z].fileName) != string::npos)
{
auto pos = err.find("Unknown module");
if (pos != string::npos)
{
pos += strlen("Unknown module") + 1;
string substr = "";
while (err[pos] != ' ' && pos != err.size())
substr += err[pos++];
mapModuleDeps[listOfProject[z].fileName].insert(substr);
changed++;
}
}
}
}
}
return changed;
}
static map<string, set<string>> createModuleOrder(const map<string, string> &moduleDelc, const map<string, set<string>> &mapModuleDeps)
{
map<string, set<string>> modDirectOrder;
for (auto& elem : moduleDelc)
modDirectOrder[elem.first] = set<string>();
for (auto& elem : moduleDelc)
{
auto itF = mapModuleDeps.find(elem.second);
if (itF != mapModuleDeps.end())
{
for (auto& inFile : itF->second)
{
if (moduleDelc.find(inFile) != moduleDelc.end())
modDirectOrder[elem.first].insert(inFile);
}
}
}
return modDirectOrder;
}
static void printDebug(const map<string, set<string>>& mapModuleDeps, const map<string, set<string>>& modDirectOrder,
const vector<FileInfo>& listOfProject, bool console = false)
{
string toPrint = "MODULE DEPS:\n";
for (auto& elem : mapModuleDeps)
{
toPrint += elem.first + '\n';
for (auto& setEl : elem.second)
toPrint += " " + setEl + '\n';
}
toPrint += "MODULE DIRECT ORDER:\n";
for (auto& elem : modDirectOrder)
{
toPrint += elem.first + '\n';
for (auto& setEl : elem.second)
toPrint += " " + setEl + '\n';
}
toPrint += "FILES LVL:\n";
for (auto& elem : listOfProject)
toPrint += elem.fileName + " " + elem.outDepPath + " lvl = " + std::to_string(elem.lvl) + '\n';
if (console)
printf("%s\n", toPrint.c_str());
__spf_print(1, "%s\n", toPrint.c_str());
}
int parseFiles(vector<string>& errors, vector<FileInfo>& listOfProject, vector<string>& filesCompilationOrder, int parseForInlining, bool isFromConsole)
{
int rethrow = 0;
int iters = 0;
int changed = 0;
int lastChanged = 0;
const string projName = "tmp";
map<string, set<string>> mapModuleDeps;
map<string, string> moduleDelc;
map<string, set<string>> modDirectOrder;
try
{
do
{
#ifdef _WIN32
sendMessage_1lvl(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " + std::to_wstring((iters + 1)) + L" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
#else
sendMessage_1lvl(L"running " + std::to_wstring((iters + 1)) + L" iteration of syntax analisys");
#endif
errors = parseList(listOfProject, iters != 0, (parseForInlining != 0), mapModuleDeps, moduleDelc, modDirectOrder, isFromConsole);
changed = createMapOfUse(errors, listOfProject, mapModuleDeps);
if (iters != 0)
if (lastChanged <= changed)
break;
createNeededException();
if (changed)
{
vector<string> files;
for (auto& elem : listOfProject)
if (elem.error == 0)
files.push_back(elem.outDepPath);
if (files.size() == 0)
break;
findModuleDeclInProject(projName + std::to_string(iters++), files, moduleDelc);
modDirectOrder = createModuleOrder(moduleDelc, mapModuleDeps);
}
lastChanged = changed;
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
} while (changed);
//printDebug(mapModuleDeps, modDirectOrder, listOfProject);
int added = 0;
int iter = 0;
vector<string> files;
while (added != listOfProject.size())
{
for (auto& elem : listOfProject)
{
if (elem.lvl == iter)
{
files.push_back(elem.fileName);
added++;
}
}
++iter;
}
map<string, set<string>> fileDeps;
for (auto& file : files)
{
fileDeps[file] = set<string>();
if (mapModuleDeps.find(file) == mapModuleDeps.end())
continue;
for (auto& dep : mapModuleDeps[file])
{
if (moduleDelc.find(dep) == moduleDelc.end())
continue;
fileDeps[file].insert(moduleDelc[dep]);
}
}
set<string> addedFiles;
added = 0;
while (added != fileDeps.size())
{
for (auto& file : fileDeps)
{
bool depsAdded = true;
for (auto& dep : file.second)
if (addedFiles.find(dep) == addedFiles.end())
depsAdded = false;
if (depsAdded && addedFiles.find(file.first) == addedFiles.end())
{
filesCompilationOrder.push_back(file.first);
addedFiles.insert(file.first);
added++;
}
}
}
__spf_print(1, "files compilation order:\n");
for (auto& file : filesCompilationOrder)
__spf_print(1, " %s\n", file.c_str());
}
catch (int err)
{
rethrow = err;
}
catch (...)
{
rethrow = -1;
}
return rethrow;
}
int parseFiles(const char* proj, vector<string>& filesCompilationOrder, int parseForInlining)
{
FILE* list = fopen(proj, "r");
if (!list)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
vector<string> pathSplit;
if (string(proj).find('\\') != string::npos)
splitString(proj, '\\', pathSplit);
else
splitString(proj, '/', pathSplit);
if (pathSplit.size() < 2)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
if (pathSplit[pathSplit.size() - 2] != "visualiser_data")
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
string fullPath = "";
for (int z = 0; z < pathSplit.size() - 2; ++z)
fullPath += pathSplit[z] + "/";
if (fullPath == "")
fullPath = "./";
else
{
//change dir
if (chdir(fullPath.c_str()) != 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
vector<FileInfo> listOfProject;
while (!feof(list))
{
char buf[1024];
if (fgets(buf, 1024, list) == NULL)
continue;
string toAdd = buf;
if (toAdd[toAdd.size() - 1] == '\n')
toAdd = toAdd.erase(toAdd.size() - 1);
string fileNameFixed = "";
auto idx = toAdd.find(fullPath);
if (idx != string::npos)
fileNameFixed = toAdd.substr(idx + fullPath.size());
else
fileNameFixed = (toAdd.substr(0, 2) == "./") ? toAdd.substr(2) : toAdd;
const string optPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".opt";
const string errPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".err";
const string outPath = fullPath + "visualiser_data/options/" + fileNameFixed + ".out";
const string fileText = readFileToStr(toAdd);
FILE* opt = fopen(optPath.c_str(), "r");
if (!opt)
{
__spf_print(1, "can not open path %s\n", optPath.c_str());
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
fgets(buf, 1024, opt);
string toAddOpt = buf;
if (toAddOpt[toAddOpt.size() - 1] == '\n')
toAddOpt = toAddOpt.erase(toAddOpt.size() - 1);
fclose(opt);
listOfProject.push_back(FileInfo(fileNameFixed, toAddOpt, errPath, outPath, "", fileText));
}
fclose(list);
vector<string> errors;
int rethrow = parseFiles(errors, listOfProject, filesCompilationOrder, parseForInlining);
int errCount = dumpErrors(listOfProject, errors);
if (rethrow != 0)
throw rethrow;
return -errCount;
}
extern int pppa_analyzer(int argv, char** argc);
int pppaAnalyzer(const char* options)
{
string optionsS(options);
vector<string> splited = splitAndArgvCreate(optionsS);
char** argv = new char* [splited.size()];
for (int z = 0; z < splited.size(); ++z)
argv[z] = (char*)splited[z].c_str();
StdCapture::Init();
string errorMessage = "";
int retCode = pppa_analyzer(splited.size(), argv);
StdCapture::EndCapture();
errorMessage = StdCapture::GetCapture();
delete[]argv;
return retCode;
}
int getNextFreeLabel()
{
PTR_LABEL lab;

View File

@@ -74,11 +74,6 @@ std::string unparseProjectToString(SgFile* file, const int curr_regime);
SgStatement* makeDeclaration(SgStatement* curr, const std::vector<SgSymbol*>& s, std::vector<SgExpression*>* inits = NULL);
std::vector<SgStatement*> makeDeclaration(const std::vector<SgSymbol*>& symbolsToDeclare, SgStatement* where, std::vector<SgExpression*>* inits = NULL);
int parseFiles(const char* proj, std::vector<std::string>& filesCompilationOrder, int parseForInlining);
int parseFiles(std::vector<std::string>& errors, std::vector<FileInfo>& listOfProject, std::vector<std::string>& filesCompilationOrder, int parseForInlining, bool isFromConsole = false);
int pppaAnalyzer(const char* options);
int getNextFreeLabel();
void fillUsedModulesInFunction(SgStatement *st, std::vector<SgStatement*> &useStats);
@@ -111,4 +106,4 @@ void removeSpecialCommentsFromProject(SgFile* file);
void getMaxMinBlockDistribution(SgFile* file, std::pair<int, int>& min_max);
void addPrivatesToArraysFromGUI(SgFile* file, const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays, const std::map<std::string, int>& distrStateFromGUI);
void addPrivatesToArraysFromGUI(SgFile* file, const std::map<std::tuple<int, std::string, std::string>, std::pair<DIST::Array*, DIST::ArrayAccessInfo*>>& declaredArrays, const std::map<std::string, int>& distrStateFromGUI);

View File

@@ -150,7 +150,7 @@ void printHelp(const char **passNames, const int lastPass)
printf(" -keepDVM keep DVM directives\n");
printf(" -allVars get all parallel versions\n");
printf(" -var N get specific parallel version, N=1,2,..\n");
printf(" -parse run parser with next option\n");
printf(" -parse run parser with next option (-inl option allow to parse project for -inlineH/I regime)\n");
printf(" -inlineH <funcName> run hierarchical inlining for all functions called from 'funcName'\n");
printf(" -inlineI <funcName> <lineNum> <fileName> run incremental inlining for function 'funcName' on 'lineNum' of 'fileName'\n");
printf(" -passInfo print passes information\n");
@@ -1537,56 +1537,6 @@ wstring fixedLongFormat(const wchar_t* old_)
return ret;
}
string convertStyle(const FileInfo* file, bool needRewrite)
{
string text = file->text;
vector<string> splited;
splitString(text, '\n', splited);
text = "";
int z = 0;
for (auto& line : splited)
{
if (line[0] == 'c' || line[0] == 'C' || line[0] == 'd' || line[0] == 'D' || line[0] == '*')
line[0] = '!';
bool needContinuation = false;
if (line[0] != '!' && line.size() > 6)
{
if (line[5] != ' ' && !(line[5] > '0' && line[5] < '9')) // not label
{
line[5] = ' ';
needContinuation = true;// line[5] = '&';
}
int p = 73;
if (file->style == 1)
p = 133;
if (line.size() > p)
{
while (line[p] != '\0' && line[p] != '\n' && line[p] != '!')
{
line[p] = ' ';
p++;
if (p >= line.size())
break;
}
}
}
if (needContinuation)
text += "&";
text += (z != 0 ? "\n" : "") + line;
++z;
}
if (needRewrite)
writeFileFromStr(file->fileName, text);
return text;
}
const set<string> getExcludedModules() { return { "omp_lib", "ifport" }; };
map<string, DIST::Array*> sortArraysByName(const set<DIST::Array*>& toSort)
@@ -1599,4 +1549,25 @@ map<string, DIST::Array*> sortArraysByName(const set<DIST::Array*>& toSort)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
return sorted;
}
}
vector<string> splitAndArgvCreate(const string& options)
{
vector<string> optSplited;
optSplited.push_back("");
splitString(options, ' ', optSplited, true);
vector<string> optSplited1;
for (auto& elem : optSplited)
if (elem != "")
optSplited1.push_back(elem);
optSplited1.insert(optSplited1.begin(), "");
for (int z = 0; z < optSplited1.size(); ++z)
{
//printf("%s\n", optSplited1[z].c_str());
if (optSplited1[z][0] == '"' && optSplited1[z][optSplited1[z].size() - 1] == '"')
optSplited1[z] = optSplited1[z].substr(1, optSplited1[z].size() - 2);
}
return optSplited1;
}

View File

@@ -87,50 +87,6 @@ std::pair<std::vector<std::string>, std::vector<std::string>> splitCommandLineFo
std::string getClearName(const std::string& in);
std::wstring fixedLongFormat(const wchar_t* old);
struct FileInfo
{
FileInfo()
{
fileName = "";
options = "";
errPath = "";
outPath = "";
outDepPath = "";
text = "";
error = -1;
includesAdded = 0;
style = -1;
lvl = 0;
}
FileInfo(const std::string& _fileName, const std::string& _options, const std::string& _errPath, const std::string& _outPath,
const std::string& _outDepPath, const std::string& _text, int errorInit = -1)
{
fileName = _fileName;
options = _options;
errPath = _errPath;
outPath = _outPath;
outDepPath = _outDepPath;
text = _text;
error = errorInit;
includesAdded = 0;
style = -1;
lvl = 0;
}
int error;
std::string fileName;
std::string options;
std::string errPath;
std::string outPath;
std::string outDepPath;
std::string text;
int style; // -1 unk, 0 fixed, 1 fixed ext, 2 free
int includesAdded;
std::set<std::string> includes;
int lvl;
};
std::string convertStyle(const FileInfo* file, bool needRewrite = true);
std::map<std::string, DIST::Array*> sortArraysByName(const std::set<DIST::Array*>& toSort);
bool createDirectory(const std::string& name);
bool createDirectory(const std::string& name);
std::vector<std::string> splitAndArgvCreate(const std::string& options);

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2275"
#define VERSION_SPF "2276"

View File

@@ -40,6 +40,8 @@
#include "../Distribution/CreateDistributionDirs.h"
#include "../LoopAnalyzer/loop_analyzer.h"
#include "../DirectiveProcessing/insert_directive.h"
#include "../ProjectManipulation/PerfAnalyzer.h"
#include "BuildGraph.h"
#ifdef _WIN32