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 50 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 55 of file CFLGraphBuilder.h.

◆ Symbol

Definition at line 56 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 37 of file CFLGraphBuilder.cpp.

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

◆ addGNode()

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

add src and dst node from file

Definition at line 82 of file CFLGraphBuilder.cpp.

83{
84 CFLNode* cflNode;
85 if (cflGraph->hasGNode(NodeID)==false)
86 {
87 cflNode = new CFLNode(NodeID);
89 }
90 else
91 {
93 }
94 return cflNode;
95}
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:55

◆ 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 101 of file CFLGraphBuilder.cpp.

102{
103 cflGraph = new CFLGraph(grammar->getStartKind());
104 // buildlabelToKindMap(grammar);
105 for(auto it = graph->begin(); it!= graph->end(); it++)
106 {
107 CFLNode* node = new CFLNode((*it).first);
108 cflGraph->addCFLNode((*it).first, node);
109 }
110 for(auto it = graph->begin(); it!= graph->end(); it++)
111 {
112 N* node = (*it).second;
113 for(E* edge : node->getOutEdges())
114 {
115 CFGrammar::Kind edgeLabel = edge->getEdgeKind();
116 cflGraph->addCFLEdge(cflGraph->getGNode(edge->getSrcID()), cflGraph->getGNode(edge->getDstID()), edgeLabel);
118 {
119 std::string label = grammar->kindToStr(edge);
120 label.append("bar");
121 cflGraph->addCFLEdge(cflGraph->getGNode(edge->getDstID()), cflGraph->getGNode(edge->getSrcID()), grammar->strToKind(label));
122 }
123 }
124 }
125 return cflGraph;
126}
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 128 of file CFLGraphBuilder.cpp.

129{
130 bool isDot = (fileName.rfind(".dot") == fileName.length() - std::string(".dot").length());
131 if (isDot)
132 return buildFromDot(fileName, grammar, direction);
133
134 bool isJson = (fileName.rfind(".json") == fileName.length() - std::string(".json").length());
135 if (isJson)
136 return buildFromJson(fileName, grammar, direction);
137
138 return buildFromText(fileName, grammar, direction);
139}
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 196 of file CFLGraphBuilder.cpp.

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

◆ 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 274 of file CFLGraphBuilder.cpp.

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

◆ 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 142 of file CFLGraphBuilder.cpp.

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

◆ buildlabelToKindMap()

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

build label and kind connect from the grammar

Definition at line 54 of file CFLGraphBuilder.cpp.

55{
56 for(auto pairV : grammar->getTerminals())
57 {
58 if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
59 {
60 labelToKindMap.insert(pairV);
61 }
62 if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
63 {
64 kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
65 }
66 }
67
68 for(auto pairV : grammar->getNonterminals())
69 {
70 if(labelToKindMap.find(pairV.first) == labelToKindMap.end())
71 {
72 labelToKindMap.insert(pairV);
73 }
74 if(kindToLabelMap.find(pairV.second) == kindToLabelMap.end())
75 {
76 kindToLabelMap.insert(make_pair(pairV.second, pairV.first));
77 }
78 }
79}
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 111 of file CFLGraphBuilder.h.

112 {
113 return this->kindToAttrsMap;
114 }

◆ 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 105 of file CFLGraphBuilder.h.

106 {
107 return this->kindToLabelMap;
108 }

◆ 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 99 of file CFLGraphBuilder.h.

100 {
101 return this->labelToKindMap;
102 }

Member Data Documentation

◆ cflGraph

CFLGraph* SVF::CFLGraphBuilder::cflGraph
protected

Definition at line 66 of file CFLGraphBuilder.h.

◆ current

Kind SVF::CFLGraphBuilder::current
protected

Definition at line 65 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 63 of file CFLGraphBuilder.h.

◆ kindToLabelMap

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

Definition at line 60 of file CFLGraphBuilder.h.

◆ labelToKindMap

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

Maps to maintain mapping between labels and kinds.

Definition at line 59 of file CFLGraphBuilder.h.


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