aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h
blob: fb652c1d2eac533103a5028336a1d93a15931997 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===---- CFGMatchSwitch.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//  This file defines the `CFGMatchSwitch` abstraction for building a "switch"
//  statement for control flow graph elements. Each case of the switch is
//  defined by an ASTMatcher which is applied on the AST node contained in the
//  input `CFGElement`.
//
//  Currently, the `CFGMatchSwitch` only handles `CFGElement`s of
//  `Kind::Statement` and `Kind::Initializer`.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_

#include "clang/AST/ASTContext.h"
#include "clang/AST/Stmt.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
#include <functional>
#include <utility>

namespace clang {
namespace dataflow {

template <typename State, typename Result = void>
using CFGMatchSwitch =
    std::function<Result(const CFGElement &, ASTContext &, State &)>;

/// Collects cases of a "match switch": a collection of matchers paired with
/// callbacks, which together define a switch that can be applied to an AST node
/// contained in a CFG element.
template <typename State, typename Result = void> class CFGMatchSwitchBuilder {
public:
  /// Registers an action `A` for `CFGStmt`s that will be triggered by the match
  /// of the pattern `M` against the `Stmt` contained in the input `CFGStmt`.
  ///
  /// Requirements:
  ///
  ///  `NodeT` should be derived from `Stmt`.
  template <typename NodeT>
  CFGMatchSwitchBuilder &&
  CaseOfCFGStmt(MatchSwitchMatcher<Stmt> M,
                MatchSwitchAction<NodeT, State, Result> A) && {
    std::move(StmtBuilder).template CaseOf<NodeT>(M, A);
    return std::move(*this);
  }

  /// Registers an action `A` for `CFGInitializer`s that will be triggered by
  /// the match of the pattern `M` against the `CXXCtorInitializer` contained in
  /// the input `CFGInitializer`.
  ///
  /// Requirements:
  ///
  ///  `NodeT` should be derived from `CXXCtorInitializer`.
  template <typename NodeT>
  CFGMatchSwitchBuilder &&
  CaseOfCFGInit(MatchSwitchMatcher<CXXCtorInitializer> M,
                MatchSwitchAction<NodeT, State, Result> A) && {
    std::move(InitBuilder).template CaseOf<NodeT>(M, A);
    return std::move(*this);
  }

  CFGMatchSwitch<State, Result> Build() && {
    return [StmtMS = std::move(StmtBuilder).Build(),
            InitMS = std::move(InitBuilder).Build()](const CFGElement &Element,
                                                     ASTContext &Context,
                                                     State &S) -> Result {
      switch (Element.getKind()) {
      case CFGElement::Initializer:
        return InitMS(*Element.castAs<CFGInitializer>().getInitializer(),
                      Context, S);
      case CFGElement::Statement:
      case CFGElement::Constructor:
      case CFGElement::CXXRecordTypedCall:
        return StmtMS(*Element.castAs<CFGStmt>().getStmt(), Context, S);
      default:
        // FIXME: Handle other kinds of CFGElement.
        return Result();
      }
    };
  }

private:
  ASTMatchSwitchBuilder<Stmt, State, Result> StmtBuilder;
  ASTMatchSwitchBuilder<CXXCtorInitializer, State, Result> InitBuilder;
};

} // namespace dataflow
} // namespace clang

#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif