Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
SVF::CFLGraphBuilder Class Reference

#include <CFLGraphBuilder.h>

Inheritance diagram for SVF::CFLGraphBuilder:
SVF::AliasCFLGraphBuilder SVF::VFCFLGraphBuilder

Public Member Functions

template<class N , class E >
CFLGraphbuild (GenericGraph< N, E > *graph, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 
CFLGraphbuild (std::string fileName, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from external file.
 
Map< std::string, Kind > & getLabelToKindMap ()
 Returns a reference to the map that associates string labels with their corresponding Kind.
 
Map< Kind, std::string > & getKindToLabelMap ()
 Returns a reference to the map that associates Kinds with their corresponding string labels.
 
Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > & getKindToAttrsMap ()
 Returns a reference to the map that associates Kinds with their corresponding attributes.
 

Protected Types

typedef CFGrammar::Kind Kind
 
typedef CFGrammar::Symbol Symbol
 

Protected Member Functions

void addAttribute (CFGrammar::Kind kind, CFGrammar::Attribute attribute)
 Method to add an attribute to a specific kind.
 
void buildlabelToKindMap (GrammarBase *grammar)
 build label and kind connect from the grammar
 
CFLNodeaddGNode (u32_t NodeID)
 add src and dst node from file
 
CFLGraphbuildFromText (std::string fileName, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Text file.
 
CFLGraphbuildFromDot (std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Dot file.
 
CFLGraphbuildFromJson (std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Json file.
 

Protected Attributes

Map< std::string, KindlabelToKindMap
 Maps to maintain mapping between labels and kinds.
 
Map< Kind, std::string > kindToLabelMap
 
Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > kindToAttrsMap
 Map to maintain attributes associated with each kind.
 
Kind current
 
CFLGraphcflGraph
 

Detailed Description

CFLGraphBuilder class is responsible for building CFL (Context-Free Language) graphs from text files, dot files, json file or from memory graph.

Definition at line 51 of file CFLGraphBuilder.h.

Member Typedef Documentation

◆ Kind

Define Kind(Not contain attribute) and Symbol(May contain attribute) as types derived from CFGrammar to numerically represent label

Definition at line 56 of file CFLGraphBuilder.h.

◆ Symbol

Definition at line 57 of file CFLGraphBuilder.h.

Member Function Documentation

◆ addAttribute()

void SVF::CFLGraphBuilder::addAttribute ( CFGrammar::Kind  kind,
CFGrammar::Attribute  attribute 
)
protected

Method to add an attribute to a specific kind.

Add attribute to kindToAttribute Map.

Definition at line 36 of file CFLGraphBuilder.cpp.

37{
38 if (kindToAttrsMap.find(kind) == kindToAttrsMap.end())
39 {
41 kindToAttrsMap.insert(make_pair(kind, attrs));
42 }
43 else
44 {
45 if (kindToAttrsMap[kind].find(attribute) == kindToAttrsMap[kind].end())
46 {
47 kindToAttrsMap[kind].insert(attribute);
48 }
49 }
50}
Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > kindToAttrsMap
Map to maintain attributes associated with each kind.
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76

◆ addGNode()

CFLNode * SVF::CFLGraphBuilder::addGNode ( u32_t  NodeID)
protected

add src and dst node from file

Definition at line 81 of file CFLGraphBuilder.cpp.

82{
83 CFLNode* cflNode;
84 if (cflGraph->hasGNode(NodeID)==false)
85 {
86 cflNode = new CFLNode(NodeID);
88 }
89 else
90 {
92 }
93 return cflNode;
94}
virtual void addCFLNode(NodeID id, CFLNode *node)
Definition CFLGraph.cpp:42
bool hasGNode(NodeID id) const
Has a node.
NodeType * getGNode(NodeID id) const
Get a node.
u32_t NodeID
Definition GeneralType.h:76

◆ build() [1/2]

template<class N , class E >
CFLGraph * SVF::CFLGraphBuilder::build ( GenericGraph< N, E > *  graph,
GrammarBase grammar,
BuildDirection  direction = BuildDirection::plain 
)

Method to build a CFL graph by copying nodes and edges from any graph inherited from GenericGraph

Method to build a bidirectional CFL graph by copying nodes and edges from any graph inherited from GenericGraph

Definition at line 100 of file CFLGraphBuilder.cpp.

101{
102 cflGraph = new CFLGraph(grammar->getStartKind());
103 // buildlabelToKindMap(grammar);
104 for(auto it = graph->begin(); it!= graph->end(); it++)
105 {
106 CFLNode* node = new CFLNode((*it).first);
107 cflGraph->addCFLNode((*it).first, node);
108 }
109 for(auto it = graph->begin(); it!= graph->end(); it++)
110 {
111 N* node = (*it).second;
112 for(E* edge : node->getOutEdges())
113 {
114 CFGrammar::Kind edgeLabel = edge->getEdgeKind();
115 cflGraph->addCFLEdge(cflGraph->getGNode(edge->getSrcID()), cflGraph->getGNode(edge->getDstID()), edgeLabel);
117 {
118 std::string label = grammar->kindToStr(edge);
119 label.append("bar");
120 cflGraph->addCFLEdge(cflGraph->getGNode(edge->getDstID()), cflGraph->getGNode(edge->getSrcID()), grammar->strToKind(label));
121 }
122 }
123 }
124 return cflGraph;
125}
virtual const CFLEdge * addCFLEdge(CFLNode *src, CFLNode *dst, CFLEdge::GEdgeFlag label)
Definition CFLGraph.cpp:47

◆ build() [2/2]

CFLGraph * SVF::CFLGraphBuilder::build ( std::string  fileName,
GrammarBase grammar,
BuildDirection  direction = BuildDirection::plain 
)

Method to build a CFL graph from external file.

Definition at line 127 of file CFLGraphBuilder.cpp.

128{
129 bool isDot = (fileName.rfind(".dot") == fileName.length() - std::string(".dot").length());
130 if (isDot)
131 return buildFromDot(fileName, grammar, direction);
132
133 bool isJson = (fileName.rfind(".json") == fileName.length() - std::string(".json").length());
134 if (isJson)
135 return buildFromJson(fileName, grammar, direction);
136
137 return buildFromText(fileName, grammar, direction);
138}
CFLGraph * buildFromDot(std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
Method to build a CFL graph from a Dot file.
CFLGraph * buildFromJson(std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
Method to build a CFL graph from a Json file.
CFLGraph * buildFromText(std::string fileName, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
Method to build a CFL graph from a Text file.

◆ buildFromDot()

CFLGraph * SVF::CFLGraphBuilder::buildFromDot ( std::string  filename,
GrammarBase grammar,
BuildDirection  direction = BuildDirection::plain 
)
protected

Method to build a CFL graph from a Dot file.

Definition at line 195 of file CFLGraphBuilder.cpp.

196{
197 buildlabelToKindMap(grammar);
198 cflGraph = new CFLGraph(grammar->getStartKind());
199 std::string lineString;
200 std::ifstream inputFile(fileName);
201 std::cout << "Building CFL Graph from dot file: " << fileName << "..\n";
202 std::cout << std::boolalpha;
203
204 u32_t lineNum = 0;
205 current = labelToKindMap.size();
206
208 {
209 lineNum += 1;
210
211 // Find "Node" prefixes and "->"
212 size_t srcStart = lineString.find("Node");
213 if (srcStart == std::string::npos) continue;
214
215 size_t srcEnd = lineString.find(" ", srcStart);
216 if (srcEnd == std::string::npos) continue;
217
218 size_t arrowPos = lineString.find("->", srcEnd);
219 if (arrowPos == std::string::npos) continue;
220
221 size_t dstStart = lineString.find("Node", arrowPos);
222 if (dstStart == std::string::npos) continue;
223
224 size_t dstEnd = lineString.find(" ", dstStart);
225 if (dstEnd == std::string::npos) continue;
226
227 size_t labelStart = lineString.find("label=", dstEnd);
228 if (labelStart == std::string::npos) continue;
229
230 labelStart += 6; // Move past "label=" to the start of the label
231 size_t labelEnd = lineString.find_first_of("]", labelStart);
232 if (labelEnd == std::string::npos) continue;
233
234 // Extract the source ID, destination ID, and label
235 std::string srcIDStr = lineString.substr(srcStart + 4, srcEnd - (srcStart + 4));
236 std::string dstIDStr = lineString.substr(dstStart + 4, dstEnd - (dstStart + 4));
237 std::string label = lineString.substr(labelStart, labelEnd - labelStart);
238
239 // Convert source and destination IDs from hexadecimal
240 u32_t srcID = std::stoul(srcIDStr, nullptr, 16);
241 u32_t dstID = std::stoul(dstIDStr, nullptr, 16);
242
243 CFLNode *src = addGNode(srcID);
244 CFLNode *dst = addGNode(dstID);
245
246 if (labelToKindMap.find(label) != labelToKindMap.end())
247 {
249 }
250 else
251 {
252 if (Options::FlexSymMap() == true)
253 {
254 labelToKindMap.insert({label, current++});
256 }
257 else
258 {
259 std::string msg = "In line " + std::to_string(lineNum) +
260 " sym cannot be found in grammar. Please correct the input dot or set --flexsymmap.";
262 std::cout << msg;
263 abort();
264 }
265 }
266 }
267
268 inputFile.close();
269 return cflGraph;
270}
unsigned u32_t
Definition CommandLine.h:18
void buildlabelToKindMap(GrammarBase *grammar)
build label and kind connect from the grammar
Map< std::string, Kind > labelToKindMap
Maps to maintain mapping between labels and kinds.
CFLNode * addGNode(u32_t NodeID)
add src and dst node from file
static const Option< bool > FlexSymMap
Definition Options.h:227
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition SVFUtil.cpp:82

◆ buildFromJson()

CFLGraph * SVF::CFLGraphBuilder::buildFromJson ( std::string  filename,
GrammarBase grammar,
BuildDirection  direction = BuildDirection::plain 
)
protected

Method to build a CFL graph from a Json file.

Definition at line 273 of file CFLGraphBuilder.cpp.

274{
275 cflGraph = new CFLGraph(grammar->getStartKind());
276 return cflGraph;
277}

◆ buildFromText()

CFLGraph * SVF::CFLGraphBuilder::buildFromText ( std::string  fileName,
GrammarBase grammar,
BuildDirection  direction = BuildDirection::plain 
)
protected

Method to build a CFL graph from a Text file.

Definition at line 141 of file CFLGraphBuilder.cpp.

142{
143 buildlabelToKindMap(grammar);
144 cflGraph = new CFLGraph(grammar->getStartKind());
145
146 std::cout << "Building CFL Graph from text file: " << fileName << "..\n";
147 std::string lineString;
148 std::ifstream inputFile(fileName);
149
150 if (!inputFile.is_open())
151 {
152 SVFUtil::errs() << "Error opening " << fileName << std::endl;
153 abort();
154 }
155
156 std::string line;
157 current = labelToKindMap.size();
158 u32_t lineNum = 0 ;
159
160 while (getline(inputFile, line))
161 {
162 std::vector<std::string> vec = SVFUtil::split(line, '\t');
163 if (vec.empty())
164 continue;
165 lineNum += 1;
166 NodeID srcID = std::stoi(vec[0]);
167 NodeID dstID = std::stoi(vec[1]);
168 CFLNode *src = addGNode(srcID);
169 CFLNode *dst = addGNode(dstID);
170 std::string label = vec[2];
171 if (labelToKindMap.find(label) != labelToKindMap.end())
173 else
174 {
175 if(Options::FlexSymMap() == true)
176 {
177 labelToKindMap.insert({label, current++});
179 }
180 else
181 {
182 std::string msg = "In line " + std::to_string(lineNum) +
183 " sym can not find in grammar, please correct the input dot or set --flexsymmap.";
185 std::cout << msg;
186 abort();
187 }
188 }
189 }
190
191 inputFile.close();
192 return cflGraph;
193}
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:58
std::vector< std::string > split(const std::string &s, char separator)
Split into two substrings around the first occurrence of a separator string.
Definition SVFUtil.h:196

◆ buildlabelToKindMap()

void SVF::CFLGraphBuilder::buildlabelToKindMap ( GrammarBase grammar)
protected

build label and kind connect from the grammar

Definition at line 53 of file CFLGraphBuilder.cpp.

54{
55 for(auto pairV : grammar->getTerminals())
56 {
57 if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
58 {
59 labelToKindMap.insert(pairV);
60 }
61 if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
62 {
63 kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
64 }
65 }
66
67 for(auto pairV : grammar->getNonterminals())
68 {
69 if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
70 {
71 labelToKindMap.insert(pairV);
72 }
73 if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
74 {
75 kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
76 }
77 }
78}
Map< Kind, std::string > kindToLabelMap

◆ getKindToAttrsMap()

Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > & SVF::CFLGraphBuilder::getKindToAttrsMap ( )
inline

Returns a reference to the map that associates Kinds with their corresponding attributes.

Definition at line 112 of file CFLGraphBuilder.h.

113 {
114 return this->kindToAttrsMap;
115 }

◆ getKindToLabelMap()

Map< Kind, std::string > & SVF::CFLGraphBuilder::getKindToLabelMap ( )
inline

Returns a reference to the map that associates Kinds with their corresponding string labels.

Definition at line 106 of file CFLGraphBuilder.h.

107 {
108 return this->kindToLabelMap;
109 }

◆ getLabelToKindMap()

Map< std::string, Kind > & SVF::CFLGraphBuilder::getLabelToKindMap ( )
inline

Returns a reference to the map that associates string labels with their corresponding Kind.

Getter methods for accessing class variables

Definition at line 100 of file CFLGraphBuilder.h.

101 {
102 return this->labelToKindMap;
103 }

Member Data Documentation

◆ cflGraph

CFLGraph* SVF::CFLGraphBuilder::cflGraph
protected

Definition at line 67 of file CFLGraphBuilder.h.

◆ current

Kind SVF::CFLGraphBuilder::current
protected

Definition at line 66 of file CFLGraphBuilder.h.

◆ kindToAttrsMap

Map<CFGrammar::Kind, Set<CFGrammar::Attribute> > SVF::CFLGraphBuilder::kindToAttrsMap
protected

Map to maintain attributes associated with each kind.

Definition at line 64 of file CFLGraphBuilder.h.

◆ kindToLabelMap

Map<Kind, std::string> SVF::CFLGraphBuilder::kindToLabelMap
protected

Definition at line 61 of file CFLGraphBuilder.h.

◆ labelToKindMap

Map<std::string, Kind> SVF::CFLGraphBuilder::labelToKindMap
protected

Maps to maintain mapping between labels and kinds.

Definition at line 60 of file CFLGraphBuilder.h.


The documentation for this class was generated from the following files: