Static Value-Flow Analysis
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. More...
 
Map< std::string, Kind > & getLabelToKindMap ()
 Returns a reference to the map that associates string labels with their corresponding Kind. More...
 
Map< Kind, std::string > & getKindToLabelMap ()
 Returns a reference to the map that associates Kinds with their corresponding string labels. More...
 
Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > & getKindToAttrsMap ()
 Returns a reference to the map that associates Kinds with their corresponding attributes. More...
 

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. More...
 
void buildlabelToKindMap (GrammarBase *grammar)
 build label and kind connect from the grammar More...
 
CFLNodeaddGNode (u32_t NodeID)
 add src and dst node from file More...
 
CFLGraphbuildFromText (std::string fileName, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Text file. More...
 
CFLGraphbuildFromDot (std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Dot file. More...
 
CFLGraphbuildFromJson (std::string filename, GrammarBase *grammar, BuildDirection direction=BuildDirection::plain)
 Method to build a CFL graph from a Json file. More...
 

Protected Attributes

Map< std::string, KindlabelToKindMap
 Maps to maintain mapping between labels and kinds. More...
 
Map< Kind, std::stringkindToLabelMap
 
Map< CFGrammar::Kind, Set< CFGrammar::Attribute > > kindToAttrsMap
 Map to maintain attributes associated with each kind. More...
 
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  {
41  Set<CFGrammar::Attribute> attrs{attribute};
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.

◆ 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);
88  cflGraph->addCFLNode(NodeID, cflNode);
89  }
90  else
91  {
92  cflNode = cflGraph->getGNode(NodeID);
93  }
94  return cflNode;
95 }
virtual void addCFLNode(NodeID id, CFLNode *node)
Definition: CFLGraph.cpp:42
NodeType * getGNode(NodeID id) const
Get a node.
Definition: GenericGraph.h:653
bool hasGNode(NodeID id) const
Has a node.
Definition: GenericGraph.h:661
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);
117  if (direction == BuildDirection::bidirection)
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 }
const char *const string
Definition: cJSON.h:172
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::regex reg("Node(\\w+)\\s*->\\s*Node(\\w+)\\s*\\[.*label=(.*)\\]");
204  std::cout << std::boolalpha;
205  u32_t lineNum = 0 ;
206  current = labelToKindMap.size();
207 
208  while (getline(inputFile, lineString))
209  {
210  lineNum += 1;
211  std::smatch matches;
212  if (std::regex_search(lineString, matches, reg))
213  {
214  u32_t srcID = std::stoul(matches.str(1), nullptr, 16);
215  u32_t dstID = std::stoul(matches.str(2), nullptr, 16);
216  std::string label = matches.str(3);
217  CFLNode *src = addGNode(srcID);
218  CFLNode *dst = addGNode(dstID);
219  if (labelToKindMap.find(label) != labelToKindMap.end())
220  cflGraph->addCFLEdge(src, dst, labelToKindMap[label]);
221  else
222  {
223  if(Options::FlexSymMap() == true)
224  {
225  labelToKindMap.insert({label, current++});
226  cflGraph->addCFLEdge(src, dst, labelToKindMap[label]);
227  }
228  else
229  {
230  std::string msg = "In line " + std::to_string(lineNum) +
231  " sym can not find in grammar, please correct the input dot or set --flexsymmap.";
232  SVFUtil::errMsg(msg);
233  std::cout << msg;
234  abort();
235  }
236  }
237  }
238  }
239  inputFile.close();
240  return cflGraph;
241 }
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:76

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

245 {
246  cflGraph = new CFLGraph(grammar->getStartKind());
247  return cflGraph;
248 }

◆ 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())
173  cflGraph->addCFLEdge(src, dst, labelToKindMap[label]);
174  else
175  {
176  if(Options::FlexSymMap() == true)
177  {
178  labelToKindMap.insert({label, current++});
179  cflGraph->addCFLEdge(src, dst, labelToKindMap[label]);
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.";
185  SVFUtil::errMsg(msg);
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: