fixed memory leak, improved code style

This commit is contained in:
ALEXks
2024-05-13 20:55:58 +03:00
parent 4b0c5f8676
commit c75982269c

View File

@@ -559,31 +559,24 @@ static SgStatement* createOpen(SgExpression* iostat, SgSymbol* files, SgExpressi
return open;
}
static char* mergedChar(const char* l, const char* r)
static string mergedChar(const string& l, const string& r)
{
int lenL = strlen(l);
int lenR = strlen(r);
char* result = new char[lenL + lenR + 2];
strcpy(result, l);
strcat(result, "_");
strcat(result, r);
return result;
return l + "_" + r;
}
static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix) {
char* result = mergedChar(prefix, oldSymb->identifier());
SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result, oldSymb->type(), oldSymb->scope());
static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix)
{
const string result = mergedChar(prefix, oldSymb->identifier());
SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result.c_str(), oldSymb->type(), oldSymb->scope());
return newSymb;
}
static void renameEx(SgExpression* sizeEx, const char* funcName) {
if (sizeEx->variant() == CONST_REF) {
static void renameEx(SgExpression* sizeEx, const char* funcName)
{
if (sizeEx->variant() == CONST_REF)
{
SgSymbol* symbSize = renamedNewSymb(sizeEx->symbol(), funcName);
sizeEx->setSymbol(symbSize);
}
@@ -596,15 +589,19 @@ static void renameEx(SgExpression* sizeEx, const char* funcName) {
static void findSizeEx(SgExpression* sizeEx, const map<string, SgStatement*>& moduleStmts,
const map<string, SgStatement*>& moduleParamStmts, SgStatement* proc_moduleF,
set<string>& addedModuleParams, SgStatement* borderStmt) {
if (sizeEx->variant() == CONST_REF) {
if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1 && addedModuleParams.count(sizeEx->symbol()->identifier()) == 0) {
set<string>& addedModuleParams, SgStatement* borderStmt)
{
if (sizeEx->variant() == CONST_REF)
{
if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1 && addedModuleParams.count(sizeEx->symbol()->identifier()) == 0)
{
auto decl = (SgParameterStmt*)moduleParamStmts.at(sizeEx->symbol()->identifier());
SgStatement* copyParamStmt = moduleParamStmts.at(sizeEx->symbol()->identifier())->copyPtr();
borderStmt->insertStmtBefore(*copyParamStmt, *proc_moduleF);
if (moduleStmts.count(sizeEx->symbol()->identifier())) {
if (moduleStmts.count(sizeEx->symbol()->identifier()))
{
SgStatement* copyStmt = moduleStmts.at(sizeEx->symbol()->identifier())->copyPtr();
copyParamStmt->insertStmtBefore(*copyStmt, *proc_moduleF);
borderStmt = copyStmt;
@@ -613,7 +610,6 @@ static void findSizeEx(SgExpression* sizeEx, const map<string, SgStatement*>& mo
borderStmt = copyParamStmt;
addedModuleParams.insert(sizeEx->symbol()->identifier());
findSizeEx(decl->value(0), moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams, borderStmt);
}
}
@@ -630,11 +626,9 @@ static void processCommonStmt(SgStatement* st, set<string>& commonVariables)
if (st->expr(0))
{
SgExpression* ex = st->expr(0)->lhs();
SgExpression* lhs;
while (ex && ex->lhs())
{
lhs = ex->lhs();
auto lhs = ex->lhs();
commonVariables.insert(lhs->sunparse());
ex = ex->rhs();
@@ -666,9 +660,8 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
continue;
}
char* result = mergedChar(funcName, lhs->symbol()->identifier());
SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result, lhs->symbol()->type(), lhs->symbol()->scope());
const string result = mergedChar(funcName, lhs->symbol()->identifier());
SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result.c_str(), lhs->symbol()->type(), lhs->symbol()->scope());
SgExpression* newExpr = new SgExpression(lhs->variant());
newExpr->setSymbol(symb);
@@ -682,13 +675,11 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
while (sizeEx && sizeEx->lhs())
{
sizeLhs = sizeEx->lhs();
renameEx(sizeLhs, funcName);
sizeEx = sizeEx->rhs();
}
newExpr->setLhs(newArraySizeExpr);
}
@@ -699,9 +690,6 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
moduleStmt->setExpression(1, typeExpr);
moduleStmts[newExpr->symbol()->identifier()] = moduleStmt;
delete[] result;
ex = ex->rhs();
}
}
@@ -710,7 +698,8 @@ static void processParamStmt(SgStatement* st, map<string, SgStatement*>& moduleP
{
auto decl = (SgParameterStmt*)st;
int n = decl->numberOfConstants();
for (int i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i)
{
SgSymbol* constSymb = renamedNewSymb(decl->constant(i), funcName);
SgExpression* constExpr = new SgExpression(CONST_REF, NULL, NULL, constSymb);
@@ -744,8 +733,8 @@ static void processExternStmt(SgStatement* st, set<string>& externVars)
}
static void processVarBlock(SgStatement* func, SgStatement* firstExec, map<string, SgStatement*>& moduleStmts,
map<string, SgStatement*>& moduleParamStmts, set<string>& commonVariables,
set<string>& externVars, FuncInfo* funcFrom, const set<string>& inFuncParam = {})
map<string, SgStatement*>& moduleParamStmts, set<string>& commonVariables,
set<string>& externVars, FuncInfo* funcFrom, const set<string>& inFuncParam = { })
{
const char* funcName = func->symbol()->identifier();
@@ -763,34 +752,37 @@ static void processVarBlock(SgStatement* func, SgStatement* firstExec, map<strin
}
static void insertStmtToModule(const map<string, SgStatement*>& moduleStmts, const map<string, SgStatement*>& moduleParamStmts,
set<string>& addedModuleParams, const set<string>& commonVariables, SgStatement* proc_moduleF,
set<string>& localVarNoParams, const set<string>& externVars, const size_t prefixLen)
set<string>& addedModuleParams, const set<string>& commonVariables, SgStatement* proc_moduleF,
set<string>& localVarNoParams, const set<string>& externVars, const size_t prefixLen)
{
SgStatement* endProcModuleF = proc_moduleF->lastNodeOfStmt();
SgStatement* borderStmt = new SgStatement(VAR_DECL);
proc_moduleF->insertStmtAfter(*borderStmt, *proc_moduleF);
for (const auto& [varName, varStmt] : moduleStmts) {
for (const auto& [varName, varStmt] : moduleStmts)
{
string varNameNoPref = varName;
string::size_type pos{};
varNameNoPref.erase(0, pos + prefixLen + 1);
if (commonVariables.count(varNameNoPref) == 0 && moduleParamStmts.count(varName) == 0
&& externVars.count(varNameNoPref) == 0) {
&& externVars.count(varNameNoPref) == 0)
{
localVarNoParams.insert(varNameNoPref);
endProcModuleF->insertStmtBefore(*varStmt, *proc_moduleF);
if (varStmt->expr(0)->variant() == ARRAY_REF) {
if (varStmt->expr(0)->variant() == ARRAY_REF)
{
SgExpression* arraySizeExpr = varStmt->expr(0)->lhs();
SgExpression* sizeEx = arraySizeExpr;
SgExpression* sizeLhs;
while (sizeEx && sizeEx->lhs()) {
while (sizeEx && sizeEx->lhs())
{
sizeLhs = sizeEx->lhs();
findSizeEx(sizeLhs, moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams, borderStmt);
sizeEx = sizeEx->rhs();
@@ -889,8 +881,8 @@ static SgStatement* createLoadBlock(const vector<SgSymbol*>& loadS, FuncInfo*& f
for (auto localVar : localVarNoParams)
{
const char* varName = localVar.c_str();
char* varNameWithPref = mergedChar(funcName, varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref);
const string varNameWithPref = mergedChar(funcName, varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref.c_str());
SgExpression* exVar = new SgVarRefExp(symbVar);
variablesVec.push_back(exVar);
@@ -956,12 +948,12 @@ static SgSymbol* createLabel(const string namelabel, SgStatement*& proc_moduleF)
}
static SgStatement* createSaveBlock(const vector<SgSymbol*>& loadS, FuncInfo*& funcI, SgExpression* iostat, SgArrayRefExp* journal,
SgExpression& frmt, SgExpression& unit, SgSymbol* files, SgExpression* fileIdx, const int numOfFiles,
const vector<SgSymbol*>& profS, SgExpression& frmtProf, SgExpression& unitNull,
SgStatement* profCallS, SgStatement* profCallE, const set<string>& localVarNoParams,
const map<string, SgStatement*>& moduleStmts, const set<string>& commonVariables, bool createdModuleForIO,
const vector<string>& moduleNames, const int unitNum, const vector<vector<string>>& chainLocalVarNoParams,
const vector<string>& chainLabel)
SgExpression& frmt, SgExpression& unit, SgSymbol* files, SgExpression* fileIdx, const int numOfFiles,
const vector<SgSymbol*>& profS, SgExpression& frmtProf, SgExpression& unitNull,
SgStatement* profCallS, SgStatement* profCallE, const set<string>& localVarNoParams,
const map<string, SgStatement*>& moduleStmts, const set<string>& commonVariables, bool createdModuleForIO,
const vector<string>& moduleNames, const int unitNum, const vector<vector<string>>& chainLocalVarNoParams,
const vector<string>& chainLabel)
{
SgStatement* storeBlock = new SgIfStmt(IF_NODE);
@@ -1022,8 +1014,8 @@ static SgStatement* createSaveBlock(const vector<SgSymbol*>& loadS, FuncInfo*& f
for (auto localVar : localVarNoParams)
{
const char* varName = localVar.c_str();
char* varNameWithPref = mergedChar(funcI->funcName.c_str(), varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref);
const string varNameWithPref = mergedChar(funcI->funcName.c_str(), varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref.c_str());
SgExpression* exVar = new SgVarRefExp(symbVar);
variablesVec.push_back(exVar);
}
@@ -1118,7 +1110,8 @@ static void processAllCalls(SgStatement* firstExec, const string funcToName, con
SgStatement* execStmt = firstExec;
while (execStmt && isSgExecutableStatement(execStmt))
{
if (execStmt->variant() == PROC_STAT && execStmt->symbol()->identifier() == funcToName) {
if (execStmt->variant() == PROC_STAT && execStmt->symbol()->identifier() == funcToName)
{
const int labNum = getNextFreeLabel();
auto nextPULabel = new SgLabel(labNum);
execStmt->setLabel(*nextPULabel);
@@ -1256,7 +1249,6 @@ static void processFunctionCallChain(SgStatement* func, const vector<FuncInfo*>&
}
SgFile::switchToFile(func->fileName());
}