diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp index 7759c53..fa3c72f 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.cpp @@ -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()) { 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); for (auto& incl : itI->second) if (allIncludeFiles.find(incl) != allIncludeFiles.end()) @@ -338,7 +338,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo else { 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 if (folderName != NULL) @@ -362,7 +362,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo } 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 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) commentsToInclude.clear(); @@ -2336,6 +2336,7 @@ void runPass(const int curr_regime, const char *proj_name, const char *folderNam case REMOVE_COMMENTS: case INSERT_NO_DISTR_FLAGS_FROM_GUI: case PRIVATE_REMOVING: + case RENAME_INLCUDES: runAnalysis(*project, curr_regime, true, "", folderName); break; case INLINE_PROCEDURES: diff --git a/sapfor/experts/Sapfor_2017/_src/Sapfor.h b/sapfor/experts/Sapfor_2017/_src/Sapfor.h index 1337941..70e3f7c 100644 --- a/sapfor/experts/Sapfor_2017/_src/Sapfor.h +++ b/sapfor/experts/Sapfor_2017/_src/Sapfor.h @@ -178,6 +178,7 @@ enum passes { INSERT_NO_DISTR_FLAGS_FROM_GUI, SET_IMPLICIT_NONE, + RENAME_INLCUDES, TEST_PASS, EMPTY_PASS @@ -359,6 +360,7 @@ static void setPassValues() passNames[GET_MIN_MAX_BLOCK_DIST] = "GET_MIN_MAX_BLOCK_DIST"; passNames[CONVERT_TO_C] = "CONVERT_TO_C"; 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[TEST_PASS] = "TEST_PASS"; diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp index 826afbd..84300a0 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp @@ -30,6 +30,17 @@ static void FillCommonTypes(map& types) 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& allVars, set& allVarsConst, SgStatement* scope) { if (expr == NULL) @@ -51,7 +62,11 @@ static void FindAllVars(SgExpression* expr, set& allVars, setsymbol()); + { + auto s = expr->symbol(); + if (!isByUse(s, scope)) + allVarsConst.insert(s); + } FindAllVars(expr->lhs(), allVars, allVarsConst, scope); FindAllVars(expr->rhs(), allVars, allVarsConst, scope); @@ -98,7 +113,8 @@ static void AddLettersToMap(SgExpression* expr, SgType* type, map } static vector getVars(const set& functionSymbs, set& toRename, - const set& allVars, const map& types) + const set& allVars, const map& types, + SgStatement* scope) { vector varsWithoutDecl; map vars; @@ -115,7 +131,7 @@ static vector getVars(const set& functionSymbs, set allDecls; - declaratedInStmt(var, &allDecls, false); + declaratedInStmt(var, &allDecls, false, (var->variant() == FUNCTION_NAME) ? scope : NULL); bool hasTypeDecls = false; for (auto& decl : allDecls) @@ -246,11 +262,8 @@ static map FunctionImplicitCheck(SgStatement* function, const map FindAllVars(st->expr(i), allVars, allVarsConst, function); if (st->variant() == FOR_NODE) - { - auto s = isSgForStmt(st)->doName(); - if (!IS_BY_USE(s) && s->scope() == function) - allVars.insert(s); - } + if (!isByUse(isSgForStmt(st)->doName(), function)) + allVars.insert(isSgForStmt(st)->doName()); } //add parameters @@ -264,8 +277,8 @@ static map FunctionImplicitCheck(SgStatement* function, const map } } - varsWithoutDecl = getVars(functionSymbs, toRename, allVars, types); - varsWithoutDeclConst = getVars(functionSymbs, toRename, allVarsConst, types); + varsWithoutDecl = getVars(functionSymbs, toRename, allVars, types, function); + varsWithoutDeclConst = getVars(functionSymbs, toRename, allVarsConst, types, NULL); if (!hasImplicitNone) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp index 02c2c11..befe3e6 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.cpp @@ -276,6 +276,9 @@ static set hideUnnecessary(SgFile* file, const string& fileN, const set changedVars; for (SgStatement* st = file->firstStatement(); st; st = st->lexNext()) { + if (st->variant() == GLOBAL) + continue; + if (dontReplaceIncludes == false) { if (st->fileName() != fileN || st->getUnparseIgnore()) @@ -311,7 +314,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch set &allIncludeFiles, bool outFree, const map>& moduleUsesByFile, const map& moduleDelcs, const map>& exctactedModuleStats, - bool toString, bool dontReplaceIncludes) + bool toString, bool renameIncludes, bool dontReplaceIncludes) { removeSpecialCommentsFromProject(file); @@ -572,7 +575,7 @@ string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const ch string inlcude = ""; for (auto& inclByPos : toInsertIncludeComment) for (auto& incl : inclByPos.second) - inlcude += incl; + inlcude += (renameIncludes ? renameInclude(incl) : incl); if (st->comments()) 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()); else { - auto tmp = string(file->firstStatement()->unparse()); + const string tmp = file->firstStatement()->unparse(); if (tmp.size() > 0) { #ifdef _WIN32 @@ -1015,12 +1018,15 @@ static bool findSymbol(SgExpression *declLst, const string &toFind) } extern map> SPF_messages; -SgStatement* declaratedInStmt(SgSymbol *toFind, vector *allDecls, bool printInternal) +SgStatement* declaratedInStmt(SgSymbol *toFind, vector *allDecls, bool printInternal, SgStatement* scope) { //need to call this function for MODULE symbols! toFind = OriginalSymbol(toFind); vector 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) { diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h index 8b0257e..85f8f45 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/SgUtils.h @@ -6,12 +6,12 @@ #include "../GraphCall/graph_calls.h" #include "../DynamicAnalysis/gcov_info.h" -SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector *allDecls = NULL, bool printInternal = true); +SgStatement* declaratedInStmt(SgSymbol *toFind, std::vector *allDecls = NULL, bool printInternal = true, SgStatement* scope = NULL); #include "DefUseList.h" #include "CommonBlock.h" -std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set &allIncludeFiles, bool outFree, const std::map> &moduleUsesByFile, const std::map& moduleDelcs, const std::map>& exctactedModuleStats, bool toString, bool dontReplaceIncls = false); +std::string removeIncludeStatsAndUnparse(SgFile *file, const char *fileName, const char *fout, std::set &allIncludeFiles, bool outFree, const std::map> &moduleUsesByFile, const std::map& moduleDelcs, const std::map>& exctactedModuleStats, bool toString, bool renameIncludes, bool dontReplaceIncls); SgSymbol* findSymbolOrCreate(SgFile *file, const std::string toFind, SgType *type = NULL, SgStatement *scope = NULL); void recExpressionPrint(SgExpression *exp); void removeSubstrFromStr(std::string &str, const std::string &del); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp index 9bc31d2..4b60f13 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.cpp @@ -520,16 +520,49 @@ bool isDVM_comment(const string& bufStr) 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 &allIncludeFiles, const map>> &commentsToInclude, const map>>& 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) { if (commentsToInclude.find(include) != commentsToInclude.end()) continue; - string newCurrFile = string(folderName) + "/" + include; + string newCurrFile = string(folderName) + "/" + (isRename ? renameExtension(include) : include); FILE *tryToOpen = fopen(newCurrFile.c_str(), "r"); if (tryToOpen == NULL) diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/utils.h b/sapfor/experts/Sapfor_2017/_src/Utils/utils.h index d46a9db..8db0be7 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/utils.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/utils.h @@ -33,7 +33,8 @@ const std::string& getGlobalBuffer(); std::wstring to_wstring(const std::string); void convertBuffers(short*& resultM, int*& resultSizeM, short*& result, int*& resultSize); void clearGlobalMessagesBuffer(); -void copyIncludes(const std::set &allIncludeFiles, const std::map>> &commentsToInclude, const std::map>>& newCopyDeclToIncl, const char *folderName, bool keepSpfDirs, bool isFreeStyle, int removeDvmDirs = 0); +std::string renameInclude(const std::string& inc); +void copyIncludes(const std::set &allIncludeFiles, const std::map>> &commentsToInclude, const std::map>>& newCopyDeclToIncl, const char *folderName, bool keepSpfDirs, bool isFreeStyle, bool isRename, int removeDvmDirs = 0); std::string splitDirective(const std::string &in); std::string splitDirectiveFull(const std::string &in_); diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 3ef0133..6044142 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2346" +#define VERSION_SPF "2350" diff --git a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp index bc2d60f..958f673 100644 --- a/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp +++ b/sapfor/experts/Sapfor_2017/_src/VisualizerCalls/get_information.cpp @@ -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); } +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) { 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); else if (whichRun == "SPF_InsertImplicitNone") 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 { if (showDebug)