added new pass

This commit is contained in:
ALEXks
2024-06-10 09:16:15 +03:00
parent 0a8a8f5d96
commit c261697970
9 changed files with 91 additions and 25 deletions

View File

@@ -329,7 +329,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo
if (curr_regime == INSERT_INCLUDES && filesToInclude.find(file_name) != filesToInclude.end()) if (curr_regime == INSERT_INCLUDES && filesToInclude.find(file_name) != filesToInclude.end())
{ {
unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile,
moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, true); moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, false, true);
auto itI = filesToInclude.find(file_name); auto itI = filesToInclude.find(file_name);
for (auto& incl : itI->second) for (auto& incl : itI->second)
if (allIncludeFiles.find(incl) != allIncludeFiles.end()) if (allIncludeFiles.find(incl) != allIncludeFiles.end())
@@ -338,7 +338,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo
else else
{ {
unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile,
moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString); moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, (curr_regime == RENAME_INLCUDES), false);
// copy includes that have not changed // copy includes that have not changed
if (folderName != NULL) if (folderName != NULL)
@@ -362,7 +362,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo
} }
allIncludeFiles = allIncludeFilesFiltr; allIncludeFiles = allIncludeFilesFiltr;
copyIncludes(allIncludeFiles, commentsToInclude, newCopyDeclToIncl, folderName, keepSpfDirs, out_free_form == 1, removeDvmDirs); copyIncludes(allIncludeFiles, commentsToInclude, newCopyDeclToIncl, folderName, keepSpfDirs, out_free_form == 1, (curr_regime == RENAME_INLCUDES), removeDvmDirs);
} }
} }
@@ -1345,7 +1345,7 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne
// copy includes that have not changed // copy includes that have not changed
if (folderName != NULL) if (folderName != NULL)
copyIncludes(allIncludeFiles, commentsToInclude, newCopyDeclToIncl, folderName, out_free_form == 1, keepSpfDirs); copyIncludes(allIncludeFiles, commentsToInclude, newCopyDeclToIncl, folderName, out_free_form == 1, keepSpfDirs, false);
} }
else if (curr_regime == EXTRACT_SHADOW_DIRS) else if (curr_regime == EXTRACT_SHADOW_DIRS)
commentsToInclude.clear(); commentsToInclude.clear();
@@ -2336,6 +2336,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam
case REMOVE_COMMENTS: case REMOVE_COMMENTS:
case INSERT_NO_DISTR_FLAGS_FROM_GUI: case INSERT_NO_DISTR_FLAGS_FROM_GUI:
case PRIVATE_REMOVING: case PRIVATE_REMOVING:
case RENAME_INLCUDES:
runAnalysis(*project, curr_regime, true, "", folderName); runAnalysis(*project, curr_regime, true, "", folderName);
break; break;
case INLINE_PROCEDURES: case INLINE_PROCEDURES:

View File

@@ -178,6 +178,7 @@ enum passes {
INSERT_NO_DISTR_FLAGS_FROM_GUI, INSERT_NO_DISTR_FLAGS_FROM_GUI,
SET_IMPLICIT_NONE, SET_IMPLICIT_NONE,
RENAME_INLCUDES,
TEST_PASS, TEST_PASS,
EMPTY_PASS EMPTY_PASS
@@ -359,6 +360,7 @@ static void setPassValues()
passNames[GET_MIN_MAX_BLOCK_DIST] = "GET_MIN_MAX_BLOCK_DIST"; passNames[GET_MIN_MAX_BLOCK_DIST] = "GET_MIN_MAX_BLOCK_DIST";
passNames[CONVERT_TO_C] = "CONVERT_TO_C"; passNames[CONVERT_TO_C] = "CONVERT_TO_C";
passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE"; passNames[SET_IMPLICIT_NONE] = "SET_IMPLICIT_NONE";
passNames[RENAME_INLCUDES] = "RENAME_INLCUDES";
passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI"; passNames[INSERT_NO_DISTR_FLAGS_FROM_GUI] = "INSERT_NO_DISTR_FLAGS_FROM_GUI";
passNames[TEST_PASS] = "TEST_PASS"; passNames[TEST_PASS] = "TEST_PASS";

View File

@@ -30,6 +30,17 @@ static void FillCommonTypes(map<char, SgType*>& types)
types[letter.first] = new SgType(T_FLOAT); types[letter.first] = new SgType(T_FLOAT);
} }
static bool isByUse(SgSymbol* s, SgStatement* checkScope)
{
auto scope = s->scope();
auto isByUse_s = IS_BY_USE(s);
if (!isByUse_s && scope == checkScope)
return false;
else
return true;
}
static void FindAllVars(SgExpression* expr, set<SgSymbol*>& allVars, set<SgSymbol*>& allVarsConst, SgStatement* scope) static void FindAllVars(SgExpression* expr, set<SgSymbol*>& allVars, set<SgSymbol*>& allVarsConst, SgStatement* scope)
{ {
if (expr == NULL) if (expr == NULL)
@@ -51,7 +62,11 @@ static void FindAllVars(SgExpression* expr, set<SgSymbol*>& allVars, set<SgSymbo
} }
} }
else if (var == CONST_REF) else if (var == CONST_REF)
allVarsConst.insert(expr->symbol()); {
auto s = expr->symbol();
if (!isByUse(s, scope))
allVarsConst.insert(s);
}
FindAllVars(expr->lhs(), allVars, allVarsConst, scope); FindAllVars(expr->lhs(), allVars, allVarsConst, scope);
FindAllVars(expr->rhs(), allVars, allVarsConst, scope); FindAllVars(expr->rhs(), allVars, allVarsConst, scope);
@@ -98,7 +113,8 @@ static void AddLettersToMap(SgExpression* expr, SgType* type, map<char, SgType*>
} }
static vector<SgSymbol*> getVars(const set<string>& functionSymbs, set<SgSymbol*>& toRename, static vector<SgSymbol*> getVars(const set<string>& functionSymbs, set<SgSymbol*>& toRename,
const set<SgSymbol*>& allVars, const map<char, SgType*>& types) const set<SgSymbol*>& allVars, const map<char, SgType*>& types,
SgStatement* scope)
{ {
vector<SgSymbol*> varsWithoutDecl; vector<SgSymbol*> varsWithoutDecl;
map<string, SgSymbol*> vars; map<string, SgSymbol*> vars;
@@ -115,7 +131,7 @@ static vector<SgSymbol*> getVars(const set<string>& functionSymbs, set<SgSymbol*
continue; continue;
vector<SgStatement*> allDecls; vector<SgStatement*> allDecls;
declaratedInStmt(var, &allDecls, false); declaratedInStmt(var, &allDecls, false, (var->variant() == FUNCTION_NAME) ? scope : NULL);
bool hasTypeDecls = false; bool hasTypeDecls = false;
for (auto& decl : allDecls) for (auto& decl : allDecls)
@@ -246,11 +262,8 @@ static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, const map
FindAllVars(st->expr(i), allVars, allVarsConst, function); FindAllVars(st->expr(i), allVars, allVarsConst, function);
if (st->variant() == FOR_NODE) if (st->variant() == FOR_NODE)
{ if (!isByUse(isSgForStmt(st)->doName(), function))
auto s = isSgForStmt(st)->doName(); allVars.insert(isSgForStmt(st)->doName());
if (!IS_BY_USE(s) && s->scope() == function)
allVars.insert(s);
}
} }
//add parameters //add parameters
@@ -264,8 +277,8 @@ static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, const map
} }
} }
varsWithoutDecl = getVars(functionSymbs, toRename, allVars, types); varsWithoutDecl = getVars(functionSymbs, toRename, allVars, types, function);
varsWithoutDeclConst = getVars(functionSymbs, toRename, allVarsConst, types); varsWithoutDeclConst = getVars(functionSymbs, toRename, allVarsConst, types, NULL);
if (!hasImplicitNone) if (!hasImplicitNone)
{ {

View File

@@ -276,6 +276,9 @@ static set<int> hideUnnecessary(SgFile* file, const string& fileN, const set<str
set<int> changedVars; set<int> changedVars;
for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) for (SgStatement* st = file->firstStatement(); st; st = st->lexNext())
{ {
if (st->variant() == GLOBAL)
continue;
if (dontReplaceIncludes == false) if (dontReplaceIncludes == false)
{ {
if (st->fileName() != fileN || st->getUnparseIgnore()) if (st->fileName() != fileN || st->getUnparseIgnore())
@@ -311,7 +314,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch
set<string> &allIncludeFiles, bool outFree, set<string> &allIncludeFiles, bool outFree,
const map<string, set<string>>& moduleUsesByFile, const map<string, string>& moduleDelcs, const map<string, set<string>>& moduleUsesByFile, const map<string, string>& moduleDelcs,
const map<SgStatement*, vector<SgStatement*>>& exctactedModuleStats, const map<SgStatement*, vector<SgStatement*>>& exctactedModuleStats,
bool toString, bool dontReplaceIncludes) bool toString, bool renameIncludes, bool dontReplaceIncludes)
{ {
removeSpecialCommentsFromProject(file); removeSpecialCommentsFromProject(file);
@@ -572,7 +575,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch
string inlcude = ""; string inlcude = "";
for (auto& inclByPos : toInsertIncludeComment) for (auto& inclByPos : toInsertIncludeComment)
for (auto& incl : inclByPos.second) for (auto& incl : inclByPos.second)
inlcude += incl; inlcude += (renameIncludes ? renameInclude(incl) : incl);
if (st->comments()) if (st->comments())
st->setComments((inlcude + st->comments()).c_str()); st->setComments((inlcude + st->comments()).c_str());
@@ -658,7 +661,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch
strUnparse = string(file->firstStatement()->unparse()); strUnparse = string(file->firstStatement()->unparse());
else else
{ {
auto tmp = string(file->firstStatement()->unparse()); const string tmp = file->firstStatement()->unparse();
if (tmp.size() > 0) if (tmp.size() > 0)
{ {
#ifdef _WIN32 #ifdef _WIN32
@@ -1015,12 +1018,15 @@ static bool findSymbol(SgExpression *declLst, const string &toFind)
} }
extern map<string, vector<Messages>> SPF_messages; extern map<string, vector<Messages>> SPF_messages;
SgStatement* declaratedInStmt(SgSymbol *toFind, vector<SgStatement*> *allDecls, bool printInternal) SgStatement* declaratedInStmt(SgSymbol *toFind, vector<SgStatement*> *allDecls, bool printInternal, SgStatement* scope)
{ {
//need to call this function for MODULE symbols! //need to call this function for MODULE symbols!
toFind = OriginalSymbol(toFind); toFind = OriginalSymbol(toFind);
vector<SgStatement*> inDecl; vector<SgStatement*> inDecl;
SgStatement *start = toFind->scope(); if (toFind->variant() == FUNCTION_NAME && scope == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
SgStatement *start = scope ? scope : toFind->scope();
if (start) if (start)
{ {

View File

@@ -6,12 +6,12 @@
#include "../GraphCall/graph_calls.h" #include "../GraphCall/graph_calls.h"
#include "../DynamicAnalysis/gcov_info.h" #include "../DynamicAnalysis/gcov_info.h"
SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector<SgStatement*> *allDecls = NULL, bool printInternal = true); SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector<SgStatement*> *allDecls = NULL, bool printInternal = true, SgStatement* scope = NULL);
#include "DefUseList.h" #include "DefUseList.h"
#include "CommonBlock.h" #include "CommonBlock.h"
std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set<std::string> &allIncludeFiles, bool outFree, const std::map<std::string, std::set<std::string>> &moduleUsesByFile, const std::map<std::string, std::string>& moduleDelcs, const std::map<SgStatement*, std::vector<SgStatement*>>& exctactedModuleStats, bool toString, bool dontReplaceIncls = false); std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set<std::string> &allIncludeFiles, bool outFree, const std::map<std::string, std::set<std::string>> &moduleUsesByFile, const std::map<std::string, std::string>& moduleDelcs, const std::map<SgStatement*, std::vector<SgStatement*>>& exctactedModuleStats, bool toString, bool renameIncludes, bool dontReplaceIncls);
SgSymbol* findSymbolOrCreate(SgFile *file, const std::string toFind, SgType *type = NULL, SgStatement *scope = NULL); SgSymbol* findSymbolOrCreate(SgFile *file, const std::string toFind, SgType *type = NULL, SgStatement *scope = NULL);
void recExpressionPrint(SgExpression *exp); void recExpressionPrint(SgExpression *exp);
void removeSubstrFromStr(std::string &str, const std::string &del); void removeSubstrFromStr(std::string &str, const std::string &del);

View File

@@ -520,16 +520,49 @@ bool isDVM_comment(const string& bufStr)
return dvmStart; return dvmStart;
} }
static string renameExtension(const string& inc)
{
string ret = inc;
if (ret.find(".") != string::npos)
ret = OnlyName(ret.c_str());
ret += ".h";
return ret;
}
string renameInclude(const string& inc)
{
auto posStart = inc.find("'");
auto posEnd = inc.find("'", posStart + 1);
if (posStart == string::npos || posEnd == string::npos)
{
posStart = inc.find("\"");
posEnd = inc.find("\"", posStart + 1);
if (posStart == string::npos || posEnd == string::npos) {
__spf_print(1, "incorrect include string <%s>\n", inc.c_str());
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
}
}
string substr = renameExtension(inc.substr(posStart + 1, posEnd - posStart - 1));
string ret = inc;
ret.replace(posStart + 1, posEnd - posStart - 1, substr);
return ret;
}
void copyIncludes(const set<string> &allIncludeFiles, const map<string, map<int, set<string>>> &commentsToInclude, void copyIncludes(const set<string> &allIncludeFiles, const map<string, map<int, set<string>>> &commentsToInclude,
const map<string, map<int, set<string>>>& newCopyDeclToIncl, const map<string, map<int, set<string>>>& newCopyDeclToIncl,
const char *folderName, bool keepSpfDirs, bool isFreeStyle, int removeDvmDirs) const char *folderName, bool keepSpfDirs, bool isFreeStyle, bool isRename,
int removeDvmDirs)
{ {
for (auto &include : allIncludeFiles) for (auto &include : allIncludeFiles)
{ {
if (commentsToInclude.find(include) != commentsToInclude.end()) if (commentsToInclude.find(include) != commentsToInclude.end())
continue; continue;
string newCurrFile = string(folderName) + "/" + include; string newCurrFile = string(folderName) + "/" + (isRename ? renameExtension(include) : include);
FILE *tryToOpen = fopen(newCurrFile.c_str(), "r"); FILE *tryToOpen = fopen(newCurrFile.c_str(), "r");
if (tryToOpen == NULL) if (tryToOpen == NULL)

View File

@@ -33,7 +33,8 @@ const std::string& getGlobalBuffer();
std::wstring to_wstring(const std::string); std::wstring to_wstring(const std::string);
void convertBuffers(short*& resultM, int*& resultSizeM, short*& result, int*& resultSize); void convertBuffers(short*& resultM, int*& resultSizeM, short*& result, int*& resultSize);
void clearGlobalMessagesBuffer(); void clearGlobalMessagesBuffer();
void copyIncludes(const std::set<std::string> &allIncludeFiles, const std::map<std::string, std::map<int, std::set<std::string>>> &commentsToInclude, const std::map<std::string, std::map<int, std::set<std::string>>>& newCopyDeclToIncl, const char *folderName, bool keepSpfDirs, bool isFreeStyle, int removeDvmDirs = 0); std::string renameInclude(const std::string& inc);
void copyIncludes(const std::set<std::string> &allIncludeFiles, const std::map<std::string, std::map<int, std::set<std::string>>> &commentsToInclude, const std::map<std::string, std::map<int, std::set<std::string>>>& newCopyDeclToIncl, const char *folderName, bool keepSpfDirs, bool isFreeStyle, bool isRename, int removeDvmDirs = 0);
std::string splitDirective(const std::string &in); std::string splitDirective(const std::string &in);
std::string splitDirectiveFull(const std::string &in_); std::string splitDirectiveFull(const std::string &in_);

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2346" #define VERSION_SPF "2350"

View File

@@ -1920,6 +1920,14 @@ int SPF_InsertImplicitNone(void*& context, int winHandler, short* options, short
return simpleTransformPass(SET_IMPLICIT_NONE, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize); return simpleTransformPass(SET_IMPLICIT_NONE, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize);
} }
int SPF_RenameIncludes(void*& context, int winHandler, short* options, short* projName, short* folderName, short*& output,
int*& outputSize, short*& outputMessage, int*& outputMessageSize)
{
MessageManager::clearCache();
MessageManager::setWinHandler(winHandler);
return simpleTransformPass(RENAME_INLCUDES, options, projName, folderName, output, outputSize, outputMessage, outputMessageSize);
}
static inline void convertBackSlash(char *str, int strL) static inline void convertBackSlash(char *str, int strL)
{ {
for (int z = 0; z < strL; ++z) for (int z = 0; z < strL; ++z)
@@ -2636,6 +2644,8 @@ const wstring Sapfor_RunTransformation(const char* transformName_c, const char*
retCode = SPF_RemoveDeadCode(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); retCode = SPF_RemoveDeadCode(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize);
else if (whichRun == "SPF_InsertImplicitNone") else if (whichRun == "SPF_InsertImplicitNone")
retCode = SPF_InsertImplicitNone(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize); retCode = SPF_InsertImplicitNone(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize);
else if (whichRun == "SPF_RenameIncludes")
retCode = SPF_RenameIncludes(context, winHandler, optSh, projSh, fold, output, outputSize, outputMessage, outputMessageSize);
else else
{ {
if (showDebug) if (showDebug)