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; return open;
} }
static char* mergedChar(const char* l, const char* r) static string mergedChar(const string& l, const string& r)
{ {
int lenL = strlen(l); return l + "_" + r;
int lenR = strlen(r);
char* result = new char[lenL + lenR + 2];
strcpy(result, l);
strcat(result, "_");
strcat(result, r);
return result;
} }
static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix) { static SgSymbol* renamedNewSymb(SgSymbol* oldSymb, const char* prefix)
char* result = mergedChar(prefix, oldSymb->identifier()); {
const string result = mergedChar(prefix, oldSymb->identifier());
SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result, oldSymb->type(), oldSymb->scope()); SgSymbol* newSymb = new SgSymbol(oldSymb->variant(), result.c_str(), oldSymb->type(), oldSymb->scope());
return newSymb; return newSymb;
} }
static void renameEx(SgExpression* sizeEx, const char* funcName) { static void renameEx(SgExpression* sizeEx, const char* funcName)
if (sizeEx->variant() == CONST_REF) { {
if (sizeEx->variant() == CONST_REF)
{
SgSymbol* symbSize = renamedNewSymb(sizeEx->symbol(), funcName); SgSymbol* symbSize = renamedNewSymb(sizeEx->symbol(), funcName);
sizeEx->setSymbol(symbSize); 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, static void findSizeEx(SgExpression* sizeEx, const map<string, SgStatement*>& moduleStmts,
const map<string, SgStatement*>& moduleParamStmts, SgStatement* proc_moduleF, const map<string, SgStatement*>& moduleParamStmts, SgStatement* proc_moduleF,
set<string>& addedModuleParams, SgStatement* borderStmt) { set<string>& addedModuleParams, SgStatement* borderStmt)
if (sizeEx->variant() == CONST_REF) { {
if (moduleParamStmts.count(sizeEx->symbol()->identifier()) == 1 && addedModuleParams.count(sizeEx->symbol()->identifier()) == 0) { 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()); auto decl = (SgParameterStmt*)moduleParamStmts.at(sizeEx->symbol()->identifier());
SgStatement* copyParamStmt = moduleParamStmts.at(sizeEx->symbol()->identifier())->copyPtr(); SgStatement* copyParamStmt = moduleParamStmts.at(sizeEx->symbol()->identifier())->copyPtr();
borderStmt->insertStmtBefore(*copyParamStmt, *proc_moduleF); 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(); SgStatement* copyStmt = moduleStmts.at(sizeEx->symbol()->identifier())->copyPtr();
copyParamStmt->insertStmtBefore(*copyStmt, *proc_moduleF); copyParamStmt->insertStmtBefore(*copyStmt, *proc_moduleF);
borderStmt = copyStmt; borderStmt = copyStmt;
@@ -613,7 +610,6 @@ static void findSizeEx(SgExpression* sizeEx, const map<string, SgStatement*>& mo
borderStmt = copyParamStmt; borderStmt = copyParamStmt;
addedModuleParams.insert(sizeEx->symbol()->identifier()); addedModuleParams.insert(sizeEx->symbol()->identifier());
findSizeEx(decl->value(0), moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams, borderStmt); 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)) if (st->expr(0))
{ {
SgExpression* ex = st->expr(0)->lhs(); SgExpression* ex = st->expr(0)->lhs();
SgExpression* lhs;
while (ex && ex->lhs()) while (ex && ex->lhs())
{ {
lhs = ex->lhs(); auto lhs = ex->lhs();
commonVariables.insert(lhs->sunparse()); commonVariables.insert(lhs->sunparse());
ex = ex->rhs(); ex = ex->rhs();
@@ -666,9 +660,8 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
continue; continue;
} }
char* result = mergedChar(funcName, lhs->symbol()->identifier()); const string result = mergedChar(funcName, lhs->symbol()->identifier());
SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result.c_str(), lhs->symbol()->type(), lhs->symbol()->scope());
SgSymbol* symb = new SgSymbol(lhs->symbol()->variant(), result, lhs->symbol()->type(), lhs->symbol()->scope());
SgExpression* newExpr = new SgExpression(lhs->variant()); SgExpression* newExpr = new SgExpression(lhs->variant());
newExpr->setSymbol(symb); newExpr->setSymbol(symb);
@@ -682,13 +675,11 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
while (sizeEx && sizeEx->lhs()) while (sizeEx && sizeEx->lhs())
{ {
sizeLhs = sizeEx->lhs(); sizeLhs = sizeEx->lhs();
renameEx(sizeLhs, funcName); renameEx(sizeLhs, funcName);
sizeEx = sizeEx->rhs(); sizeEx = sizeEx->rhs();
} }
newExpr->setLhs(newArraySizeExpr); newExpr->setLhs(newArraySizeExpr);
} }
@@ -699,9 +690,6 @@ static void processVarStmt(SgStatement* st, map<string, SgStatement*>& moduleStm
moduleStmt->setExpression(1, typeExpr); moduleStmt->setExpression(1, typeExpr);
moduleStmts[newExpr->symbol()->identifier()] = moduleStmt; moduleStmts[newExpr->symbol()->identifier()] = moduleStmt;
delete[] result;
ex = ex->rhs(); ex = ex->rhs();
} }
} }
@@ -710,7 +698,8 @@ static void processParamStmt(SgStatement* st, map<string, SgStatement*>& moduleP
{ {
auto decl = (SgParameterStmt*)st; auto decl = (SgParameterStmt*)st;
int n = decl->numberOfConstants(); 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); SgSymbol* constSymb = renamedNewSymb(decl->constant(i), funcName);
SgExpression* constExpr = new SgExpression(CONST_REF, NULL, NULL, constSymb); SgExpression* constExpr = new SgExpression(CONST_REF, NULL, NULL, constSymb);
@@ -745,7 +734,7 @@ static void processExternStmt(SgStatement* st, set<string>& externVars)
static void processVarBlock(SgStatement* func, SgStatement* firstExec, map<string, SgStatement*>& moduleStmts, static void processVarBlock(SgStatement* func, SgStatement* firstExec, map<string, SgStatement*>& moduleStmts,
map<string, SgStatement*>& moduleParamStmts, set<string>& commonVariables, map<string, SgStatement*>& moduleParamStmts, set<string>& commonVariables,
set<string>& externVars, FuncInfo* funcFrom, const set<string>& inFuncParam = {}) set<string>& externVars, FuncInfo* funcFrom, const set<string>& inFuncParam = { })
{ {
const char* funcName = func->symbol()->identifier(); const char* funcName = func->symbol()->identifier();
@@ -771,26 +760,29 @@ static void insertStmtToModule(const map<string, SgStatement*>& moduleStmts, con
SgStatement* borderStmt = new SgStatement(VAR_DECL); SgStatement* borderStmt = new SgStatement(VAR_DECL);
proc_moduleF->insertStmtAfter(*borderStmt, *proc_moduleF); proc_moduleF->insertStmtAfter(*borderStmt, *proc_moduleF);
for (const auto& [varName, varStmt] : moduleStmts) { for (const auto& [varName, varStmt] : moduleStmts)
{
string varNameNoPref = varName; string varNameNoPref = varName;
string::size_type pos{}; string::size_type pos{};
varNameNoPref.erase(0, pos + prefixLen + 1); varNameNoPref.erase(0, pos + prefixLen + 1);
if (commonVariables.count(varNameNoPref) == 0 && moduleParamStmts.count(varName) == 0 if (commonVariables.count(varNameNoPref) == 0 && moduleParamStmts.count(varName) == 0
&& externVars.count(varNameNoPref) == 0) { && externVars.count(varNameNoPref) == 0)
{
localVarNoParams.insert(varNameNoPref); localVarNoParams.insert(varNameNoPref);
endProcModuleF->insertStmtBefore(*varStmt, *proc_moduleF); 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* arraySizeExpr = varStmt->expr(0)->lhs();
SgExpression* sizeEx = arraySizeExpr; SgExpression* sizeEx = arraySizeExpr;
SgExpression* sizeLhs; SgExpression* sizeLhs;
while (sizeEx && sizeEx->lhs()) { while (sizeEx && sizeEx->lhs())
{
sizeLhs = sizeEx->lhs(); sizeLhs = sizeEx->lhs();
findSizeEx(sizeLhs, moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams, borderStmt); findSizeEx(sizeLhs, moduleStmts, moduleParamStmts, proc_moduleF, addedModuleParams, borderStmt);
sizeEx = sizeEx->rhs(); sizeEx = sizeEx->rhs();
@@ -889,8 +881,8 @@ static SgStatement* createLoadBlock(const vector<SgSymbol*>& loadS, FuncInfo*& f
for (auto localVar : localVarNoParams) for (auto localVar : localVarNoParams)
{ {
const char* varName = localVar.c_str(); const char* varName = localVar.c_str();
char* varNameWithPref = mergedChar(funcName, varName); const string varNameWithPref = mergedChar(funcName, varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref); SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref.c_str());
SgExpression* exVar = new SgVarRefExp(symbVar); SgExpression* exVar = new SgVarRefExp(symbVar);
variablesVec.push_back(exVar); variablesVec.push_back(exVar);
@@ -1022,8 +1014,8 @@ static SgStatement* createSaveBlock(const vector<SgSymbol*>& loadS, FuncInfo*& f
for (auto localVar : localVarNoParams) for (auto localVar : localVarNoParams)
{ {
const char* varName = localVar.c_str(); const char* varName = localVar.c_str();
char* varNameWithPref = mergedChar(funcI->funcName.c_str(), varName); const string varNameWithPref = mergedChar(funcI->funcName.c_str(), varName);
SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref); SgSymbol* symbVar = new SgSymbol(VARIABLE_NAME, varNameWithPref.c_str());
SgExpression* exVar = new SgVarRefExp(symbVar); SgExpression* exVar = new SgVarRefExp(symbVar);
variablesVec.push_back(exVar); variablesVec.push_back(exVar);
} }
@@ -1118,7 +1110,8 @@ static void processAllCalls(SgStatement* firstExec, const string funcToName, con
SgStatement* execStmt = firstExec; SgStatement* execStmt = firstExec;
while (execStmt && isSgExecutableStatement(execStmt)) 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(); const int labNum = getNextFreeLabel();
auto nextPULabel = new SgLabel(labNum); auto nextPULabel = new SgLabel(labNum);
execStmt->setLabel(*nextPULabel); execStmt->setLabel(*nextPULabel);
@@ -1256,7 +1249,6 @@ static void processFunctionCallChain(SgStatement* func, const vector<FuncInfo*>&
} }
SgFile::switchToFile(func->fileName()); SgFile::switchToFile(func->fileName());
} }