clang  16.0.0git
CFGMatchSwitch.h
Go to the documentation of this file.
1 //===---- CFGMatchSwitch.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 the `CFGMatchSwitch` abstraction for building a "switch"
10 // statement for control flow graph elements. Each case of the switch is
11 // defined by an ASTMatcher which is applied on the AST node contained in the
12 // input `CFGElement`.
13 //
14 // Currently, the `CFGMatchSwitch` only handles `CFGElement`s of
15 // `Kind::Statement` and `Kind::Initializer`.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
20 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
21 
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Stmt.h"
24 #include "clang/Analysis/CFG.h"
26 #include <functional>
27 #include <utility>
28 
29 namespace clang {
30 namespace dataflow {
31 
32 template <typename State, typename Result = void>
33 using CFGMatchSwitch =
34  std::function<Result(const CFGElement &, ASTContext &, State &)>;
35 
36 /// Collects cases of a "match switch": a collection of matchers paired with
37 /// callbacks, which together define a switch that can be applied to an AST node
38 /// contained in a CFG element.
39 template <typename State, typename Result = void> class CFGMatchSwitchBuilder {
40 public:
41  /// Registers an action `A` for `CFGStmt`s that will be triggered by the match
42  /// of the pattern `M` against the `Stmt` contained in the input `CFGStmt`.
43  ///
44  /// Requirements:
45  ///
46  /// `NodeT` should be derived from `Stmt`.
47  template <typename NodeT>
51  std::move(StmtBuilder).template CaseOf<NodeT>(M, A);
52  return std::move(*this);
53  }
54 
55  /// Registers an action `A` for `CFGInitializer`s that will be triggered by
56  /// the match of the pattern `M` against the `CXXCtorInitializer` contained in
57  /// the input `CFGInitializer`.
58  ///
59  /// Requirements:
60  ///
61  /// `NodeT` should be derived from `CXXCtorInitializer`.
62  template <typename NodeT>
66  std::move(InitBuilder).template CaseOf<NodeT>(M, A);
67  return std::move(*this);
68  }
69 
71  return [StmtMS = std::move(StmtBuilder).Build(),
72  InitMS = std::move(InitBuilder).Build()](const CFGElement &Element,
73  ASTContext &Context,
74  State &S) -> Result {
75  switch (Element.getKind()) {
77  return InitMS(*Element.castAs<CFGInitializer>().getInitializer(),
78  Context, S);
82  return StmtMS(*Element.castAs<CFGStmt>().getStmt(), Context, S);
83  default:
84  // FIXME: Handle other kinds of CFGElement.
85  return Result();
86  }
87  };
88  }
89 
90 private:
93 };
94 
95 } // namespace dataflow
96 } // namespace clang
97 
98 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
clang::dataflow::MatchSwitchMatcher
ast_matchers::internal::Matcher< T > MatchSwitchMatcher
Definition: MatchSwitch.h:52
clang::dataflow::CFGMatchSwitchBuilder
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
Definition: CFGMatchSwitch.h:39
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::CFGInitializer::getInitializer
CXXCtorInitializer * getInitializer() const
Definition: CFG.h:232
clang::CFGElement::CXXRecordTypedCall
@ CXXRecordTypedCall
Definition: CFG.h:68
clang::CFGElement::Initializer
@ Initializer
Definition: CFG.h:59
ASTContext.h
clang::dataflow::CFGMatchSwitchBuilder::Build
CFGMatchSwitch< State, Result > Build() &&
Definition: CFGMatchSwitch.h:70
clang::dataflow::ASTMatchSwitchBuilder
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
Definition: MatchSwitch.h:88
MatchSwitch.h
clang::CFGElement::Statement
@ Statement
Definition: CFG.h:66
clang::CFGStmt
Definition: CFG.h:132
clang::CFGInitializer
Represents C++ base or member initializer from constructor's initialization list.
Definition: CFG.h:227
clang::CFGStmt::getStmt
const Stmt * getStmt() const
Definition: CFG.h:138
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1147
clang::CFGElement
Represents a top-level expression in a basic block.
Definition: CFG.h:55
clang::dataflow::CFGMatchSwitchBuilder::CaseOfCFGInit
CFGMatchSwitchBuilder && CaseOfCFGInit(MatchSwitchMatcher< CXXCtorInitializer > M, MatchSwitchAction< NodeT, State, Result > A) &&
Registers an action A for CFGInitializers that will be triggered by the match of the pattern M agains...
Definition: CFGMatchSwitch.h:64
clang
Definition: CalledOnceCheck.h:17
CFG.h
clang::dataflow::CFGMatchSwitch
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
Definition: CFGMatchSwitch.h:34
clang::dataflow::CFGMatchSwitchBuilder::CaseOfCFGStmt
CFGMatchSwitchBuilder && CaseOfCFGStmt(MatchSwitchMatcher< Stmt > M, MatchSwitchAction< NodeT, State, Result > A) &&
Registers an action A for CFGStmts that will be triggered by the match of the pattern M against the S...
Definition: CFGMatchSwitch.h:49
Stmt.h
clang::dataflow::MatchSwitchAction
std::function< Result(const T *, const ast_matchers::MatchFinder::MatchResult &, State &)> MatchSwitchAction
Definition: MatchSwitch.h:56
clang::CFGElement::Constructor
@ Constructor
Definition: CFG.h:67