clang  14.0.0git
DataflowValues.h
Go to the documentation of this file.
1 //===--- DataflowValues.h - Data structure for dataflow values --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 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 a skeleton data structure for encapsulating the dataflow
10 // values for a CFG. Typically this is subclassed to provide methods for
11 // computing these values from a CFG.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_ANALYSES_DATAFLOW_VALUES
16 #define LLVM_CLANG_ANALYSES_DATAFLOW_VALUES
17 
18 #include "clang/Analysis/CFG.h"
20 #include "llvm/ADT/DenseMap.h"
21 
22 namespace clang {
23 
24 //===----------------------------------------------------------------------===//
25 /// Dataflow Directional Tag Classes. These are used for tag dispatching
26 /// within the dataflow solver/transfer functions to determine what direction
27 /// a dataflow analysis flows.
28 //===----------------------------------------------------------------------===//
29 
30 namespace dataflow {
33 } // end namespace dataflow
34 
35 //===----------------------------------------------------------------------===//
36 /// DataflowValues. Container class to store dataflow values for a CFG.
37 //===----------------------------------------------------------------------===//
38 
39 template <typename ValueTypes,
40  typename _AnalysisDirTag = dataflow::forward_analysis_tag >
42 
43  //===--------------------------------------------------------------------===//
44  // Type declarations.
45  //===--------------------------------------------------------------------===//
46 
47 public:
48  typedef typename ValueTypes::ValTy ValTy;
49  typedef typename ValueTypes::AnalysisDataTy AnalysisDataTy;
50  typedef _AnalysisDirTag AnalysisDirTag;
51  typedef llvm::DenseMap<ProgramPoint, ValTy> EdgeDataMapTy;
52  typedef llvm::DenseMap<const CFGBlock*, ValTy> BlockDataMapTy;
53  typedef llvm::DenseMap<const Stmt*, ValTy> StmtDataMapTy;
54 
55  //===--------------------------------------------------------------------===//
56  // Predicates.
57  //===--------------------------------------------------------------------===//
58 
59 public:
60  /// isForwardAnalysis - Returns true if the dataflow values are computed
61  /// from a forward analysis.
63 
64  /// isBackwardAnalysis - Returns true if the dataflow values are computed
65  /// from a backward analysis.
66  bool isBackwardAnalysis() { return !isForwardAnalysis(); }
67 
68 private:
70  bool isForwardAnalysis(dataflow::backward_analysis_tag) { return false; }
71 
72  //===--------------------------------------------------------------------===//
73  // Initialization and accessors methods.
74  //===--------------------------------------------------------------------===//
75 
76 public:
79 
80  /// InitializeValues - Invoked by the solver to initialize state needed for
81  /// dataflow analysis. This method is usually specialized by subclasses.
82  void InitializeValues(const CFG& cfg) {}
83 
84 
85  /// getEdgeData - Retrieves the dataflow values associated with a
86  /// CFG edge.
88  typename EdgeDataMapTy::iterator I = EdgeDataMap.find(E);
89  assert (I != EdgeDataMap.end() && "No data associated with Edge.");
90  return I->second;
91  }
92 
93  const ValTy& getEdgeData(const BlockEdge &E) const {
94  return reinterpret_cast<DataflowValues*>(this)->getEdgeData(E);
95  }
96 
97  /// getBlockData - Retrieves the dataflow values associated with a
98  /// specified CFGBlock. If the dataflow analysis is a forward analysis,
99  /// this data is associated with the END of the block. If the analysis
100  /// is a backwards analysis, it is associated with the ENTRY of the block.
102  typename BlockDataMapTy::iterator I = BlockDataMap.find(B);
103  assert (I != BlockDataMap.end() && "No data associated with block.");
104  return I->second;
105  }
106 
107  const ValTy& getBlockData(const CFGBlock *B) const {
108  return const_cast<DataflowValues*>(this)->getBlockData(B);
109  }
110 
111  /// getStmtData - Retrieves the dataflow values associated with a
112  /// specified Stmt. If the dataflow analysis is a forward analysis,
113  /// this data corresponds to the point immediately before a Stmt.
114  /// If the analysis is a backwards analysis, it is associated with
115  /// the point after a Stmt. This data is only computed for block-level
116  /// expressions, and only when requested when the analysis is executed.
117  ValTy& getStmtData(const Stmt *S) {
118  assert (StmtDataMap && "Dataflow values were not computed for statements.");
119  typename StmtDataMapTy::iterator I = StmtDataMap->find(S);
120  assert (I != StmtDataMap->end() && "No data associated with statement.");
121  return I->second;
122  }
123 
124  const ValTy& getStmtData(const Stmt *S) const {
125  return const_cast<DataflowValues*>(this)->getStmtData(S);
126  }
127 
128  /// getEdgeDataMap - Retrieves the internal map between CFG edges and
129  /// dataflow values. Usually used by a dataflow solver to compute
130  /// values for blocks.
132  const EdgeDataMapTy& getEdgeDataMap() const { return EdgeDataMap; }
133 
134  /// getBlockDataMap - Retrieves the internal map between CFGBlocks and
135  /// dataflow values. If the dataflow analysis operates in the forward
136  /// direction, the values correspond to the dataflow values at the start
137  /// of the block. Otherwise, for a backward analysis, the values correpsond
138  /// to the dataflow values at the end of the block.
140  const BlockDataMapTy& getBlockDataMap() const { return BlockDataMap; }
141 
142  /// getStmtDataMap - Retrieves the internal map between Stmts and
143  /// dataflow values.
145  if (!StmtDataMap) StmtDataMap = new StmtDataMapTy();
146  return *StmtDataMap;
147  }
148 
149  const StmtDataMapTy& getStmtDataMap() const {
150  return const_cast<DataflowValues*>(this)->getStmtDataMap();
151  }
152 
153  /// getAnalysisData - Retrieves the meta data associated with a
154  /// dataflow analysis for analyzing a particular CFG.
155  /// This is typically consumed by transfer function code (via the solver).
156  /// This can also be used by subclasses to interpret the dataflow values.
158  const AnalysisDataTy& getAnalysisData() const { return AnalysisData; }
159 
160  //===--------------------------------------------------------------------===//
161  // Internal data.
162  //===--------------------------------------------------------------------===//
163 
164 protected:
169 };
170 
171 } // end namespace clang
172 #endif // LLVM_CLANG_ANALYSES_DATAFLOW_VALUES
clang::DataflowValues::isForwardAnalysis
bool isForwardAnalysis()
isForwardAnalysis - Returns true if the dataflow values are computed from a forward analysis.
Definition: DataflowValues.h:62
clang::DataflowValues::EdgeDataMapTy
llvm::DenseMap< ProgramPoint, ValTy > EdgeDataMapTy
Definition: DataflowValues.h:51
clang::DataflowValues::AnalysisDataTy
ValueTypes::AnalysisDataTy AnalysisDataTy
Definition: DataflowValues.h:49
clang::dataflow::backward_analysis_tag
Definition: DataflowValues.h:32
clang::DataflowValues::getBlockData
ValTy & getBlockData(const CFGBlock *B)
getBlockData - Retrieves the dataflow values associated with a specified CFGBlock.
Definition: DataflowValues.h:101
clang::DataflowValues::getStmtData
ValTy & getStmtData(const Stmt *S)
getStmtData - Retrieves the dataflow values associated with a specified Stmt.
Definition: DataflowValues.h:117
NULL
#define NULL
Definition: stddef.h:89
clang::DataflowValues::getAnalysisData
AnalysisDataTy & getAnalysisData()
getAnalysisData - Retrieves the meta data associated with a dataflow analysis for analyzing a particu...
Definition: DataflowValues.h:157
clang::DataflowValues::EdgeDataMap
EdgeDataMapTy EdgeDataMap
Definition: DataflowValues.h:165
clang::DataflowValues::InitializeValues
void InitializeValues(const CFG &cfg)
InitializeValues - Invoked by the solver to initialize state needed for dataflow analysis.
Definition: DataflowValues.h:82
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1225
clang::DataflowValues::AnalysisData
AnalysisDataTy AnalysisData
Definition: DataflowValues.h:168
clang::DataflowValues::BlockDataMapTy
llvm::DenseMap< const CFGBlock *, ValTy > BlockDataMapTy
Definition: DataflowValues.h:52
clang::CFGBlock
Represents a single basic block in a source-level CFG.
Definition: CFG.h:576
ProgramPoint.h
clang::DataflowValues::getStmtData
const ValTy & getStmtData(const Stmt *S) const
Definition: DataflowValues.h:124
clang::DataflowValues::getBlockDataMap
const BlockDataMapTy & getBlockDataMap() const
Definition: DataflowValues.h:140
clang::DataflowValues::DataflowValues
DataflowValues()
Definition: DataflowValues.h:77
clang::DataflowValues::getBlockData
const ValTy & getBlockData(const CFGBlock *B) const
Definition: DataflowValues.h:107
clang::DataflowValues::getStmtDataMap
StmtDataMapTy & getStmtDataMap()
getStmtDataMap - Retrieves the internal map between Stmts and dataflow values.
Definition: DataflowValues.h:144
clang::DataflowValues::getEdgeData
const ValTy & getEdgeData(const BlockEdge &E) const
Definition: DataflowValues.h:93
clang::DataflowValues::StmtDataMap
StmtDataMapTy * StmtDataMap
Definition: DataflowValues.h:167
clang::DataflowValues::StmtDataMapTy
llvm::DenseMap< const Stmt *, ValTy > StmtDataMapTy
Definition: DataflowValues.h:53
clang::DataflowValues::ValTy
ValueTypes::ValTy ValTy
Definition: DataflowValues.h:48
clang::DataflowValues::getEdgeDataMap
const EdgeDataMapTy & getEdgeDataMap() const
Definition: DataflowValues.h:132
clang::DataflowValues::isBackwardAnalysis
bool isBackwardAnalysis()
isBackwardAnalysis - Returns true if the dataflow values are computed from a backward analysis.
Definition: DataflowValues.h:66
clang::DataflowValues
DataflowValues. Container class to store dataflow values for a CFG.
Definition: DataflowValues.h:41
clang::DataflowValues::getAnalysisData
const AnalysisDataTy & getAnalysisData() const
Definition: DataflowValues.h:158
clang
Definition: CalledOnceCheck.h:17
clang::DataflowValues::getStmtDataMap
const StmtDataMapTy & getStmtDataMap() const
Definition: DataflowValues.h:149
clang::DataflowValues::AnalysisDirTag
_AnalysisDirTag AnalysisDirTag
Definition: DataflowValues.h:50
CFG.h
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::DataflowValues::getBlockDataMap
BlockDataMapTy & getBlockDataMap()
getBlockDataMap - Retrieves the internal map between CFGBlocks and dataflow values.
Definition: DataflowValues.h:139
clang::BlockEdge
Definition: ProgramPoint.h:503
clang::DataflowValues::BlockDataMap
BlockDataMapTy BlockDataMap
Definition: DataflowValues.h:166
clang::DataflowValues::~DataflowValues
~DataflowValues()
Definition: DataflowValues.h:78
clang::DataflowValues::getEdgeData
ValTy & getEdgeData(const BlockEdge &E)
getEdgeData - Retrieves the dataflow values associated with a CFG edge.
Definition: DataflowValues.h:87
clang::DataflowValues::getEdgeDataMap
EdgeDataMapTy & getEdgeDataMap()
getEdgeDataMap - Retrieves the internal map between CFG edges and dataflow values.
Definition: DataflowValues.h:131
clang::dataflow::forward_analysis_tag
Definition: DataflowValues.h:31