From dae0afef458070267afb5cf04cbccfea8f643a11 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Wed, 16 Apr 2025 14:58:46 +0300 Subject: [PATCH] moved SPF_GetIncludeDependencies to json --- src/Sapfor.cpp | 25 +++++++---- src/SapforData.h | 4 +- src/Utils/version.h | 2 +- src/VisualizerCalls/get_information.cpp | 55 ++++++++++++++----------- 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/Sapfor.cpp b/src/Sapfor.cpp index 85bbfa1..b7e80b8 100644 --- a/src/Sapfor.cpp +++ b/src/Sapfor.cpp @@ -331,7 +331,7 @@ static string unparseProjectIfNeed(SgFile* file, const int curr_regime, const bo unparseToBuf = removeIncludeStatsAndUnparse(file, file_name, fout_name.c_str(), allIncludeFiles, out_free_form == 1, moduleUsesByFile, moduleDecls, getObjectForFileFromMap(file_name, exctactedModuleStats), toString, false, true); auto itI = filesToInclude.find(file_name); - for (auto& incl : itI->second) + for (auto& [_, incl] : itI->second) if (allIncludeFiles.find(incl) != allIncludeFiles.end()) allIncludeFiles.erase(incl); } @@ -827,20 +827,27 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne { auto fileIt = includeDependencies.find(file_name); if (fileIt == includeDependencies.end()) - fileIt = includeDependencies.insert(fileIt, make_pair(file_name, set())); + fileIt = includeDependencies.insert(fileIt, make_pair(file_name, vector>())); set modFiles; for (auto& elem : moduleDecls) modFiles.insert(elem.second); - for (SgStatement *first = file->firstStatement(); first; first = first->lexNext()) + SgStatement* lastFromFile = NULL; + for (SgStatement *st = file->firstStatement(); st; st = st->lexNext()) { - if (strcmp(file_name, first->fileName())) + if (st->variant() == MODULE_STMT && modFiles.find(st->fileName()) != modFiles.end()) + st = st->lastNodeOfStmt(); + else { - if (first->variant() == MODULE_STMT && modFiles.find(first->fileName()) != modFiles.end()) - first = first->lastNodeOfStmt(); + if (strcmp(file_name, st->fileName())) + { + if (lastFromFile == NULL) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + fileIt->second.push_back(make_pair(lastFromFile->lineNumber(), st->fileName())); + } else - fileIt->second.insert(first->fileName()); + lastFromFile = st; } } @@ -1596,9 +1603,9 @@ static bool runAnalysis(SgProject &project, const int curr_regime, const bool ne set includedToThisFile; if (itDep != includeDependencies.end()) { - for (auto &inclDep : itDep->second) + for (auto& [_, incl] : itDep->second) { - auto comm = commentsToInclude.find(inclDep); + auto comm = commentsToInclude.find(incl); if (comm != commentsToInclude.end()) for (auto &allComm : comm->second) includedToThisFile.insert(allComm.second.begin(), allComm.second.end()); diff --git a/src/SapforData.h b/src/SapforData.h index 97fc31e..8a46da9 100644 --- a/src/SapforData.h +++ b/src/SapforData.h @@ -85,7 +85,7 @@ std::map>> commentsToInclude; // //for INSERT_INCLUDES -std::map> filesToInclude; // file -> includes +std::map>> filesToInclude; // file -> includes [nearest line, include] // //for PASSES DEPENDENSIES @@ -96,7 +96,7 @@ std::set passesIgnoreStateDone; //for files info std::map lineInfo; // file -> lines count std::map, std::set>> dirsInfo; // file -> dirs count -std::map> includeDependencies; // file -> includes +std::map>> includeDependencies; // file -> includes [nearest line, include] std::vector filesCompilationOrder; // order of files for unite to one file std::map>> exctactedModuleStats; // file -> hided excluded modules // diff --git a/src/Utils/version.h b/src/Utils/version.h index c6878ca..1be73b4 100644 --- a/src/Utils/version.h +++ b/src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2406" +#define VERSION_SPF "2408" diff --git a/src/VisualizerCalls/get_information.cpp b/src/VisualizerCalls/get_information.cpp index 102b86f..f4f74f9 100644 --- a/src/VisualizerCalls/get_information.cpp +++ b/src/VisualizerCalls/get_information.cpp @@ -1308,13 +1308,12 @@ int SPF_GetIntrinsics(void*& context, short *&result) return (int)resVal.size() + 1; } -extern map> includeDependencies; +extern map>> includeDependencies; int SPF_GetIncludeDependencies(void*& context, int winHandler, short *options, short *projName, short *&result, short*& output, int*& outputSize, short*& outputMessage, int*& outputMessageSize) { MessageManager::clearCache(); MessageManager::setWinHandler(winHandler); - string resVal = ""; setOptions(options); int retSize = 0; @@ -1322,23 +1321,28 @@ int SPF_GetIncludeDependencies(void*& context, int winHandler, short *options, s { runPassesForVisualizer(projName, { BUILD_INCLUDE_DEPENDENCIES }); - int i = 0; - for (auto &deps : includeDependencies) + json inc_array = json::array(); + for (const auto& deps : includeDependencies) { - if (i != 0) - resVal += "@"; - resVal += deps.first + "@"; - int k = 0; - for (auto &incl : deps.second) + json includes; + includes["file"] = deps.first; + + json array = json::array(); + for (const auto& [line, incl] : deps.second) { - if (k != 0) - resVal += "|"; - resVal += incl; - ++k; + json elem; + elem["line"] = line; + elem["dependencyFileName"] = incl; + array.push_back(elem); } - ++i; + includes["includes"] = array; + inc_array.push_back(includes); } + json allIncludes; + allIncludes["allIncludes"] = inc_array; + string resVal = allIncludes.dump(); + copyStringToShort(result, resVal); retSize = (int)resVal.size() + 1; } @@ -2171,36 +2175,41 @@ int SPF_InlineProcedures(void*& context, int winHandler, short* options, short* } -extern map> filesToInclude; -int SPF_InsertIncludesPass(void*& context, int winHandler, short *options, short *projName, short *folderName, char *filesToInclude, +extern map>> filesToInclude; +int SPF_InsertIncludesPass(void*& context, int winHandler, short *options, short *projName, short *folderName, char *visFilesToInclude, short *&output, int *&outputSize, short *&outputMessage, int *&outputMessageSize) { MessageManager::clearCache(); MessageManager::setWinHandler(winHandler); - if (filesToInclude == NULL) + if (visFilesToInclude == NULL) return -2; vector splited; //printf("%s\n", conv); - splitString(filesToInclude, '|', splited); + splitString(visFilesToInclude, '|', splited); if (splited.size() == 0) return -3; - ::filesToInclude.clear(); + filesToInclude.clear(); for (int i = 0; i < splited.size(); ++i) { string file = splited[i]; int num = 0; if (sscanf(splited[i + 1].c_str(), "%d", &num) == -1) - return -3; + return -4; __spf_print(1, "file = %s:\n", file.c_str()); - for (int k = i + 2; k < i + 2 + num; ++k) + for (int k = i + 2; k < i + 2 + 2 * num; k += 2) { - ::filesToInclude[file].insert(splited[k]); - __spf_print(1, " include = %s\n", splited[k].c_str()); + int line = 0; + if (sscanf(splited[k].c_str(), "%d", &line) == -1) + return -5; + + auto pair = make_pair(line, splited[k + 1]); + filesToInclude[file].push_back(pair); + __spf_print(1, " include = [%d %s]\n", pair.first, pair.second.c_str()); } i += 1 + num; }