clang 20.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
23#include "clang/AST/Stmt.h"
24#include "clang/Analysis/CFG.h"
26#include <functional>
27#include <utility>
28
29namespace clang {
30namespace dataflow {
31
32template <typename State, typename Result = void>
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.
39template <typename State, typename Result = void> class CFGMatchSwitchBuilder {
40public:
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
90private:
93};
94
95} // namespace dataflow
96} // namespace clang
97
98#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_CFGMATCHSWITCH_H_
Defines the clang::ASTContext interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
Represents a top-level expression in a basic block.
Definition: CFG.h:55
@ CXXRecordTypedCall
Definition: CFG.h:68
Represents C++ base or member initializer from constructor's initialization list.
Definition: CFG.h:227
CXXCtorInitializer * getInitializer() const
Definition: CFG.h:232
const Stmt * getStmt() const
Definition: CFG.h:138
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
Definition: MatchSwitch.h:96
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
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...
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...
CFGMatchSwitch< State, Result > Build() &&
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
std::function< Result(const T *, const ast_matchers::MatchFinder::MatchResult &, State &)> MatchSwitchAction
Definition: MatchSwitch.h:69
ast_matchers::internal::Matcher< T > MatchSwitchMatcher
Definition: MatchSwitch.h:65
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.