-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasefuncName.cpp
More file actions
113 lines (100 loc) · 3.57 KB
/
Copy pathbasefuncName.cpp
File metadata and controls
113 lines (100 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <unordered_map>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
struct FunctionData
{
std::string fileName;
std::vector<std::string> callees;
};
void parseFortranFiles(const std::string &directory, std::unordered_map<std::string, FunctionData> &callGraph, bool includeNf90)
{
std::regex callRegex(R"(\bCALL\s+(\w+))"); // Detects function calls
std::regex defRegex(R"(^\s*(program|subroutine|function|module)\s+(\w+))", std::regex::icase); // Detects definitions
std::regex nf90Regex(R"(\bnf90_\w+)"); // Detects nf90_ functions
std::string currentFunction;
for (const auto &entry : fs::recursive_directory_iterator(directory))
{
if (entry.path().extension() == ".f90" || entry.path().extension() == ".F")
{
std::ifstream file(entry.path());
if (!file.is_open())
{
std::cerr << "Error opening file: " << entry.path() << std::endl;
continue;
}
std::string line;
while (std::getline(file, line))
{
// Ignore lines that start with '!'
if (line.empty() || line[0] == '!')
{
continue;
}
std::smatch match;
if (std::regex_search(line, match, defRegex))
{
currentFunction = match[2];
callGraph[currentFunction].fileName = entry.path().filename().string(); // Store file name
}
else if (std::regex_search(line, match, callRegex))
{
if (!currentFunction.empty())
{
callGraph[currentFunction].callees.push_back(match[1]);
}
}
else if (includeNf90 && std::regex_search(line, match, nf90Regex))
{
if (!currentFunction.empty())
{
callGraph[currentFunction].callees.push_back(match[0]);
}
}
}
}
}
}
void outputCallGraphText(const std::unordered_map<std::string, FunctionData> &callGraph)
{
std::ofstream outFile("build_graph/base_call_graph.txt");
if (!outFile.is_open())
{
std::cerr << "Error opening output file" << std::endl;
return;
}
for (const auto &[caller, data] : callGraph)
{
outFile << caller << " (" << data.fileName << ") -> ";
bool first = true;
for (const auto &callee : data.callees)
{
if (!first)
{
outFile << ", ";
}
outFile << callee;
first = false;
}
outFile << std::endl << std::endl;
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <directory> [--include-nf90]" << std::endl;
return 1;
}
std::string directory = argv[1];
bool includeNf90 = (argc > 2 && std::string(argv[2]) == "--include-nf90");
std::unordered_map<std::string, FunctionData> callGraph;
parseFortranFiles(directory, callGraph, includeNf90);
outputCallGraphText(callGraph); // Output text format
std::cout << "dependency graph generated in base_call_graph.txt" << std::endl;
return 0;
}