svf-lib 1.0.1291 → 1.0.1293

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,6 +31,7 @@
31
31
  #define GENERICGRAPH_H_
32
32
 
33
33
  #include "SVFIR/SVFValue.h"
34
+ #include "Util/iterator.h"
34
35
 
35
36
  namespace SVF
36
37
  {
@@ -452,6 +453,44 @@ public:
452
453
  namespace llvm
453
454
  {
454
455
 
456
+ // mapped_iter - This is a simple iterator adapter that causes a function to
457
+ // be applied whenever operator* is invoked on the iterator.
458
+
459
+ template <typename ItTy, typename FuncTy,
460
+ typename FuncReturnTy =
461
+ decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))>
462
+ class mapped_iter
463
+ : public iterator_adaptor_base<
464
+ mapped_iter<ItTy, FuncTy>, ItTy,
465
+ typename std::iterator_traits<ItTy>::iterator_category,
466
+ typename std::remove_reference<FuncReturnTy>::type>
467
+ {
468
+ public:
469
+ mapped_iter(ItTy U, FuncTy F)
470
+ : mapped_iter::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
471
+
472
+ ItTy getCurrent()
473
+ {
474
+ return this->I;
475
+ }
476
+
477
+ FuncReturnTy operator*() const
478
+ {
479
+ return F(*this->I);
480
+ }
481
+
482
+ private:
483
+ FuncTy F;
484
+ };
485
+
486
+ // map_iter - Provide a convenient way to create mapped_iters, just like
487
+ // make_pair is useful for creating pairs...
488
+ template <class ItTy, class FuncTy>
489
+ inline mapped_iter<ItTy, FuncTy> map_iter(ItTy I, FuncTy F)
490
+ {
491
+ return mapped_iter<ItTy, FuncTy>(std::move(I), std::move(F));
492
+ }
493
+
455
494
  /*!
456
495
  * GraphTraits for nodes
457
496
  */
@@ -466,7 +505,7 @@ template<class NodeTy,class EdgeTy> struct GraphTraits<SVF::GenericNode<NodeTy,E
466
505
  }
467
506
 
468
507
  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
469
- typedef mapped_iterator<typename SVF::GenericNode<NodeTy,EdgeTy>::iterator, decltype(&edge_dest)> ChildIteratorType;
508
+ typedef mapped_iter<typename SVF::GenericNode<NodeTy,EdgeTy>::iterator, decltype(&edge_dest)> ChildIteratorType;
470
509
 
471
510
  static NodeType* getEntryNode(NodeType* pagN)
472
511
  {
@@ -475,19 +514,19 @@ template<class NodeTy,class EdgeTy> struct GraphTraits<SVF::GenericNode<NodeTy,E
475
514
 
476
515
  static inline ChildIteratorType child_begin(const NodeType* N)
477
516
  {
478
- return map_iterator(N->OutEdgeBegin(), &edge_dest);
517
+ return map_iter(N->OutEdgeBegin(), &edge_dest);
479
518
  }
480
519
  static inline ChildIteratorType child_end(const NodeType* N)
481
520
  {
482
- return map_iterator(N->OutEdgeEnd(), &edge_dest);
521
+ return map_iter(N->OutEdgeEnd(), &edge_dest);
483
522
  }
484
523
  static inline ChildIteratorType direct_child_begin(const NodeType *N)
485
524
  {
486
- return map_iterator(N->directOutEdgeBegin(), &edge_dest);
525
+ return map_iter(N->directOutEdgeBegin(), &edge_dest);
487
526
  }
488
527
  static inline ChildIteratorType direct_child_end(const NodeType *N)
489
528
  {
490
- return map_iterator(N->directOutEdgeEnd(), &edge_dest);
529
+ return map_iter(N->directOutEdgeEnd(), &edge_dest);
491
530
  }
492
531
  };
493
532
 
@@ -506,7 +545,7 @@ struct GraphTraits<Inverse<SVF::GenericNode<NodeTy,EdgeTy>* > >
506
545
  }
507
546
 
508
547
  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
509
- typedef mapped_iterator<typename SVF::GenericNode<NodeTy,EdgeTy>::iterator, decltype(&edge_dest)> ChildIteratorType;
548
+ typedef mapped_iter<typename SVF::GenericNode<NodeTy,EdgeTy>::iterator, decltype(&edge_dest)> ChildIteratorType;
510
549
 
511
550
  static inline NodeType* getEntryNode(Inverse<NodeType* > G)
512
551
  {
@@ -515,11 +554,11 @@ struct GraphTraits<Inverse<SVF::GenericNode<NodeTy,EdgeTy>* > >
515
554
 
516
555
  static inline ChildIteratorType child_begin(const NodeType* N)
517
556
  {
518
- return map_iterator(N->InEdgeBegin(), &edge_dest);
557
+ return map_iter(N->InEdgeBegin(), &edge_dest);
519
558
  }
520
559
  static inline ChildIteratorType child_end(const NodeType* N)
521
560
  {
522
- return map_iterator(N->InEdgeEnd(), &edge_dest);
561
+ return map_iter(N->InEdgeEnd(), &edge_dest);
523
562
  }
524
563
 
525
564
  static inline unsigned getNodeID(const NodeType* N)
@@ -549,15 +588,15 @@ template<class NodeTy,class EdgeTy> struct GraphTraits<SVF::GenericGraph<NodeTy,
549
588
  }
550
589
 
551
590
  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
552
- typedef mapped_iterator<typename GenericGraphTy::iterator, decltype(&deref_val)> nodes_iterator;
591
+ typedef mapped_iter<typename GenericGraphTy::iterator, decltype(&deref_val)> nodes_iterator;
553
592
 
554
593
  static nodes_iterator nodes_begin(GenericGraphTy *G)
555
594
  {
556
- return map_iterator(G->begin(), &deref_val);
595
+ return map_iter(G->begin(), &deref_val);
557
596
  }
558
597
  static nodes_iterator nodes_end(GenericGraphTy *G)
559
598
  {
560
- return map_iterator(G->end(), &deref_val);
599
+ return map_iter(G->end(), &deref_val);
561
600
  }
562
601
 
563
602
  static unsigned graphSize(GenericGraphTy* G)
@@ -32,10 +32,8 @@
32
32
  #define INCLUDE_GRAPHS_GRAPHPRINTER_H_
33
33
 
34
34
  #include <system_error>
35
- #include <llvm/Support/ToolOutputFile.h>
36
- #include <llvm/Support/GraphWriter.h> // for graph write
37
- #include <llvm/Support/FileSystem.h> // for file open flag
38
- #include <llvm/ADT/GraphTraits.h>
35
+ #include "Graphs/GraphWriter.h" // for graph write
36
+ #include <fstream>
39
37
 
40
38
  namespace llvm
41
39
  {
@@ -60,24 +58,18 @@ public:
60
58
  {
61
59
  // Filename of the output dot file
62
60
  std::string Filename = GraphName + ".dot";
63
- O << "Writing '" << Filename << "'...";
64
- std::error_code ErrInfo;
65
- llvm::ToolOutputFile F(Filename.c_str(), ErrInfo, llvm::sys::fs::OF_None);
66
-
67
- if (!ErrInfo)
61
+ std::ofstream outFile(Filename);
62
+ if (outFile.fail())
68
63
  {
69
- // dump the ValueFlowGraph here
70
- WriteGraph(F.os(), GT, simple);
71
- F.os().close();
72
- if (!F.os().has_error())
73
- {
74
- O << "\n";
75
- F.keep();
76
- return;
77
- }
64
+ O << " error opening file for writing!\n";
65
+ outFile.close();
66
+ return;
78
67
  }
79
- O << " error opening file for writing!\n";
80
- F.os().clear_error();
68
+
69
+ O << "Writing '" << Filename << "'...";
70
+
71
+ WriteGraph(outFile, GT, simple);
72
+ outFile.close();
81
73
  }
82
74
 
83
75
  /*!
@@ -0,0 +1,150 @@
1
+ //===- llvm/ADT/GraphTraits.h - Graph traits template -----------*- C++ -*-===//
2
+ //
3
+ // From the LLVM Project with some modifications, under the Apache License v2.0
4
+ // with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+ //===----------------------------------------------------------------------===//
8
+ //
9
+ // This file defines the little GraphTraits<X> template class that should be
10
+ // specialized by classes that want to be iteratable by generic graph iterators.
11
+ //
12
+ // This file also defines the marker class Inverse that is used to iterate over
13
+ // graphs in a graph defined, inverse ordering...
14
+ //
15
+ //===----------------------------------------------------------------------===//
16
+
17
+ #ifndef LLVM_ADT_GRAPHTRAITS_H
18
+ #define LLVM_ADT_GRAPHTRAITS_H
19
+
20
+ #include "Util/iterator_range.h"
21
+
22
+ namespace llvm
23
+ {
24
+
25
+ // GraphTraits - This class should be specialized by different graph types...
26
+ // which is why the default version is empty.
27
+ //
28
+ // This template evolved from supporting `BasicBlock` to also later supporting
29
+ // more complex types (e.g. CFG and DomTree).
30
+ //
31
+ // GraphTraits can be used to create a view over a graph interpreting it
32
+ // differently without requiring a copy of the original graph. This could
33
+ // be achieved by carrying more data in NodeRef. See LoopBodyTraits for one
34
+ // example.
35
+ template<class GraphType>
36
+ struct GraphTraits
37
+ {
38
+ // Elements to provide:
39
+
40
+ // typedef NodeRef - Type of Node token in the graph, which should
41
+ // be cheap to copy.
42
+ // typedef ChildIteratorType - Type used to iterate over children in graph,
43
+ // dereference to a NodeRef.
44
+
45
+ // static NodeRef getEntryNode(const GraphType &)
46
+ // Return the entry node of the graph
47
+
48
+ // static ChildIteratorType child_begin(NodeRef)
49
+ // static ChildIteratorType child_end (NodeRef)
50
+ // Return iterators that point to the beginning and ending of the child
51
+ // node list for the specified node.
52
+
53
+ // typedef ...iterator nodes_iterator; - dereference to a NodeRef
54
+ // static nodes_iterator nodes_begin(GraphType *G)
55
+ // static nodes_iterator nodes_end (GraphType *G)
56
+ // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
57
+
58
+ // typedef EdgeRef - Type of Edge token in the graph, which should
59
+ // be cheap to copy.
60
+ // typedef ChildEdgeIteratorType - Type used to iterate over children edges in
61
+ // graph, dereference to a EdgeRef.
62
+
63
+ // static ChildEdgeIteratorType child_edge_begin(NodeRef)
64
+ // static ChildEdgeIteratorType child_edge_end(NodeRef)
65
+ // Return iterators that point to the beginning and ending of the
66
+ // edge list for the given callgraph node.
67
+ //
68
+ // static NodeRef edge_dest(EdgeRef)
69
+ // Return the destination node of an edge.
70
+
71
+ // static unsigned size (GraphType *G)
72
+ // Return total number of nodes in the graph
73
+
74
+ // If anyone tries to use this class without having an appropriate
75
+ // specialization, make an error. If you get this error, it's because you
76
+ // need to include the appropriate specialization of GraphTraits<> for your
77
+ // graph, or you need to define it for a new graph type. Either that or
78
+ // your argument to XXX_begin(...) is unknown or needs to have the proper .h
79
+ // file #include'd.
80
+ using NodeRef = typename GraphType::UnknownGraphTypeError;
81
+ };
82
+
83
+ // Inverse - This class is used as a little marker class to tell the graph
84
+ // iterator to iterate over the graph in a graph defined "Inverse" ordering.
85
+ // Not all graphs define an inverse ordering, and if they do, it depends on
86
+ // the graph exactly what that is. Here's an example of usage with the
87
+ // df_iterator:
88
+ //
89
+ // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
90
+ // for (; I != E; ++I) { ... }
91
+ //
92
+ // Which is equivalent to:
93
+ // df_iterator<Inverse<Method*>> I = idf_begin(M), E = idf_end(M);
94
+ // for (; I != E; ++I) { ... }
95
+ //
96
+ template <class GraphType>
97
+ struct Inverse
98
+ {
99
+ const GraphType &Graph;
100
+
101
+ inline Inverse(const GraphType &G) : Graph(G) {}
102
+ };
103
+
104
+ // Provide a partial specialization of GraphTraits so that the inverse of an
105
+ // inverse falls back to the original graph.
106
+ template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {};
107
+
108
+ // Provide iterator ranges for the graph traits nodes and children
109
+ template <class GraphType>
110
+ iterator_range<typename GraphTraits<GraphType>::nodes_iterator>
111
+ nodes(const GraphType &G)
112
+ {
113
+ return make_range(GraphTraits<GraphType>::nodes_begin(G),
114
+ GraphTraits<GraphType>::nodes_end(G));
115
+ }
116
+ template <class GraphType>
117
+ iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator>
118
+ inverse_nodes(const GraphType &G)
119
+ {
120
+ return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G),
121
+ GraphTraits<Inverse<GraphType>>::nodes_end(G));
122
+ }
123
+
124
+ template <class GraphType>
125
+ iterator_range<typename GraphTraits<GraphType>::ChildIteratorType>
126
+ children(const typename GraphTraits<GraphType>::NodeRef &G)
127
+ {
128
+ return make_range(GraphTraits<GraphType>::child_begin(G),
129
+ GraphTraits<GraphType>::child_end(G));
130
+ }
131
+
132
+ template <class GraphType>
133
+ iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType>
134
+ inverse_children(const typename GraphTraits<GraphType>::NodeRef &G)
135
+ {
136
+ return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G),
137
+ GraphTraits<Inverse<GraphType>>::child_end(G));
138
+ }
139
+
140
+ template <class GraphType>
141
+ iterator_range<typename GraphTraits<GraphType>::ChildEdgeIteratorType>
142
+ children_edges(const typename GraphTraits<GraphType>::NodeRef &G)
143
+ {
144
+ return make_range(GraphTraits<GraphType>::child_edge_begin(G),
145
+ GraphTraits<GraphType>::child_edge_end(G));
146
+ }
147
+
148
+ } // end namespace llvm
149
+
150
+ #endif // LLVM_ADT_GRAPHTRAITS_H