fixed dead code

This commit is contained in:
ALEXks
2024-04-14 21:30:09 +03:00
parent f7406cf729
commit d8bd2ec43a
5 changed files with 116 additions and 17 deletions

View File

@@ -351,6 +351,28 @@ public:
{ }
};
static bool hasCalls(SgExpression* ex)
{
if (ex)
{
if (ex->variant() == FUNC_CALL)
return true;
return hasCalls(ex->lhs()) || hasCalls(ex->rhs());
}
return false;
}
//TODO: add check for side effects for function calls
static bool hasCalls(SgStatement* stat)
{
if (stat->variant() == FOR_NODE)
{
auto forSt = isSgForStmt(stat);
return hasCalls(forSt->start()) || hasCalls(forSt->end()) || hasCalls(forSt->step());
}
else
return hasCalls(stat->expr(0)) || hasCalls(stat->expr(1)) || hasCalls(stat->expr(2));
}
int removeDeadCode(SgStatement* func,
const map<string, vector<FuncInfo*>>& allFuncs,
@@ -493,7 +515,20 @@ int removeDeadCode(SgStatement* func,
for (auto& rem : remove)
{
__spf_print(PRINT_USELESS_STATEMENTS, "[Useless statement on line %d and file %s]\n", rem->lineNumber(), rem->fileName());
rem->deleteStmt();
auto cp = rem->controlParent();
if (cp->variant() == LOGIF_NODE)
{
if (hasCalls(cp))
{
((SgLogIfStmt*)cp)->convertLogicIf();
rem->deleteStmt();
}
else
cp->deleteStmt();
}
else
rem->deleteStmt();
}
countOfTransform += remove.size();
@@ -507,25 +542,33 @@ int removeDeadCode(SgStatement* func,
if ((var == FOR_NODE || var == WHILE_NODE || var == SWITCH_NODE) &&
st->lexNext()->variant() == CONTROL_END)
{
remove.push_back(st);
if (!hasCalls(st))
remove.push_back(st);
}
else if (var == IF_NODE)
{
SgStatement* ifS = st;
while (ifS->lexNext()->variant() == ELSEIF_NODE)
if (!hasCalls(st))
{
bool hasCalls_ = false;
SgStatement* ifS = st;
while (ifS->lexNext()->variant() == ELSEIF_NODE)
{
hasCalls_ |= hasCalls(ifS->lexNext());
ifS = ifS->lexNext();
}
SgStatement* lastNode = ifS->lastNodeOfStmt();
ifS = ifS->lexNext();
SgStatement* lastNode = ifS->lastNodeOfStmt();
ifS = ifS->lexNext();
while (ifS->variant() == CONTROL_END && ifS != lastNode)
ifS = ifS->lexNext();
while (ifS->variant() == CONTROL_END && ifS != lastNode)
ifS = ifS->lexNext();
if (ifS == lastNode)
remove.push_back(st);
if (ifS == lastNode && !hasCalls_)
remove.push_back(st);
}
}
//TODO: SWITCH and other block statements
//TODO: other block statements
if (var == CONTAINS_STMT)
break;

View File

@@ -1,3 +1,3 @@
#pragma once
#define VERSION_SPF "2318"
#define VERSION_SPF "2319"