Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h
Line
Count
Source
1
//===- TypeErasedDataflowAnalysis.h -----------------------------*- 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 type-erased base types and functions for building dataflow
10
//  analyses that run over Control-Flow Graphs (CFGs).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_TYPEERASEDDATAFLOWANALYSIS_H
15
#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_TYPEERASEDDATAFLOWANALYSIS_H
16
17
#include <utility>
18
#include <vector>
19
20
#include "clang/AST/ASTContext.h"
21
#include "clang/AST/Stmt.h"
22
#include "clang/Analysis/CFG.h"
23
#include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
24
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
25
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
26
#include "llvm/ADT/Any.h"
27
#include "llvm/ADT/Optional.h"
28
29
namespace clang {
30
namespace dataflow {
31
32
/// Type-erased lattice element container.
33
///
34
/// Requirements:
35
///
36
///  The type of the object stored in the container must be a bounded
37
///  join-semilattice.
38
struct TypeErasedLattice {
39
  llvm::Any Value;
40
};
41
42
/// Type-erased base class for dataflow analyses built on a single lattice type.
43
class TypeErasedDataflowAnalysis {
44
public:
45
  virtual ~TypeErasedDataflowAnalysis() {}
46
47
  /// Returns the `ASTContext` that is used by the analysis.
48
  virtual ASTContext &getASTContext() = 0;
49
50
  /// Returns a type-erased lattice element that models the initial state of a
51
  /// basic block.
52
  virtual TypeErasedLattice typeErasedInitialElement() = 0;
53
54
  /// Joins two type-erased lattice elements by computing their least upper
55
  /// bound. Places the join result in the left element and returns an effect
56
  /// indicating whether any changes were made to it.
57
  virtual LatticeJoinEffect joinTypeErased(TypeErasedLattice &,
58
                                           const TypeErasedLattice &) = 0;
59
60
  /// Returns true if and only if the two given type-erased lattice elements are
61
  /// equal.
62
  virtual bool isEqualTypeErased(const TypeErasedLattice &,
63
                                 const TypeErasedLattice &) = 0;
64
65
  /// Applies the analysis transfer function for a given statement and
66
  /// type-erased lattice element.
67
  virtual void transferTypeErased(const Stmt *, TypeErasedLattice &,
68
                                  Environment &) = 0;
69
};
70
71
/// Type-erased model of the program at a given program point.
72
struct TypeErasedDataflowAnalysisState {
73
  /// Type-erased model of a program property.
74
  TypeErasedLattice Lattice;
75
76
  /// Model of the state of the program (store and heap).
77
  Environment Env;
78
79
  TypeErasedDataflowAnalysisState(TypeErasedLattice Lattice, Environment Env)
80
134
      : Lattice(std::move(Lattice)), Env(std::move(Env)) {}
81
};
82
83
/// Transfers the state of a basic block by evaluating each of its statements in
84
/// the context of `Analysis` and the states of its predecessors that are
85
/// available in `BlockStates`. `HandleTransferredStmt` (if provided) will be
86
/// applied to each statement in the block, after it is evaluated.
87
///
88
/// Requirements:
89
///
90
///   All predecessors of `Block` except those with loop back edges must have
91
///   already been transferred. States in `BlockStates` that are set to
92
///   `llvm::None` represent basic blocks that are not evaluated yet.
93
TypeErasedDataflowAnalysisState transferBlock(
94
    const ControlFlowContext &CFCtx,
95
    std::vector<llvm::Optional<TypeErasedDataflowAnalysisState>> &BlockStates,
96
    const CFGBlock &Block, const Environment &InitEnv,
97
    TypeErasedDataflowAnalysis &Analysis,
98
    std::function<void(const CFGStmt &,
99
                       const TypeErasedDataflowAnalysisState &)>
100
        HandleTransferredStmt = nullptr);
101
102
/// Performs dataflow analysis and returns a mapping from basic block IDs to
103
/// dataflow analysis states that model the respective basic blocks. Indices
104
/// of the returned vector correspond to basic block IDs.
105
std::vector<llvm::Optional<TypeErasedDataflowAnalysisState>>
106
runTypeErasedDataflowAnalysis(const ControlFlowContext &CFCtx,
107
                              TypeErasedDataflowAnalysis &Analysis,
108
                              const Environment &InitEnv);
109
110
} // namespace dataflow
111
} // namespace clang
112
113
#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_TYPEERASEDDATAFLOWANALYSIS_H