|
| 1 | +//===---- MatchSwitch.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 `MatchSwitch` abstraction for building a "switch" |
| 10 | +// statement, where each case of the switch is defined by an AST matcher. The |
| 11 | +// cases are considered in order, like pattern matching in functional |
| 12 | +// languages. |
| 13 | +// |
| 14 | +// Currently, the design is catered towards simplifying the implementation of |
| 15 | +// `DataflowAnalysis` transfer functions. Based on experience here, this |
| 16 | +// library may be generalized and moved to ASTMatchers. |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +#ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_ |
| 21 | +#define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_ |
| 22 | + |
| 23 | +#include "clang/AST/ASTContext.h" |
| 24 | +#include "clang/AST/Stmt.h" |
| 25 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 26 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 27 | +#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" |
| 28 | +#include "llvm/ADT/StringRef.h" |
| 29 | +#include <functional> |
| 30 | +#include <string> |
| 31 | +#include <utility> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +namespace clang { |
| 35 | +namespace dataflow { |
| 36 | + |
| 37 | +/// A common form of state shared between the cases of a transfer function. |
| 38 | +template <typename LatticeT> struct TransferState { |
| 39 | + TransferState(LatticeT &Lattice, Environment &Env) |
| 40 | + : Lattice(Lattice), Env(Env) {} |
| 41 | + |
| 42 | + /// Current lattice element. |
| 43 | + LatticeT &Lattice; |
| 44 | + Environment &Env; |
| 45 | +}; |
| 46 | + |
| 47 | +/// Matches against `Stmt` and, based on its structure, dispatches to an |
| 48 | +/// appropriate handler. |
| 49 | +template <typename State> |
| 50 | +using MatchSwitch = std::function<void(const Stmt &, ASTContext &, State &)>; |
| 51 | + |
| 52 | +/// Collects cases of a "match switch": a collection of matchers paired with |
| 53 | +/// callbacks, which together define a switch that can be applied to a |
| 54 | +/// `Stmt`. This structure can simplify the definition of `transfer` functions |
| 55 | +/// that rely on pattern-matching. |
| 56 | +/// |
| 57 | +/// For example, consider an analysis that handles particular function calls. It |
| 58 | +/// can define the `MatchSwitch` once, in the constructor of the analysis, and |
| 59 | +/// then reuse it each time that `transfer` is called, with a fresh state value. |
| 60 | +/// |
| 61 | +/// \code |
| 62 | +/// MatchSwitch<TransferState<MyLattice> BuildSwitch() { |
| 63 | +/// return MatchSwitchBuilder<TransferState<MyLattice>>() |
| 64 | +/// .CaseOf(callExpr(callee(functionDecl(hasName("foo")))), TransferFooCall) |
| 65 | +/// .CaseOf(callExpr(argumentCountIs(2), |
| 66 | +/// callee(functionDecl(hasName("bar")))), |
| 67 | +/// TransferBarCall) |
| 68 | +/// .Build(); |
| 69 | +/// } |
| 70 | +/// \endcode |
| 71 | +template <typename State> class MatchSwitchBuilder { |
| 72 | +public: |
| 73 | + // An action is triggered by the match of a pattern against the input |
| 74 | + // statement. For generality, actions take both the matched statement and the |
| 75 | + // set of bindings produced by the match. |
| 76 | + using Action = std::function<void( |
| 77 | + const Stmt *, const ast_matchers::MatchFinder::MatchResult &, State &)>; |
| 78 | + |
| 79 | + MatchSwitchBuilder &&CaseOf(ast_matchers::internal::Matcher<Stmt> M, |
| 80 | + Action A) && { |
| 81 | + Matchers.push_back(std::move(M)); |
| 82 | + Actions.push_back(std::move(A)); |
| 83 | + return std::move(*this); |
| 84 | + } |
| 85 | + |
| 86 | + // Convenience function for the common case, where bound nodes are not |
| 87 | + // needed. `Node` should be a subclass of `Stmt`. |
| 88 | + template <typename Node> |
| 89 | + MatchSwitchBuilder &&CaseOf(ast_matchers::internal::Matcher<Stmt> M, |
| 90 | + void (*Action)(const Node *, State &)) && { |
| 91 | + Matchers.push_back(std::move(M)); |
| 92 | + Actions.push_back([Action](const Stmt *Stmt, |
| 93 | + const ast_matchers::MatchFinder::MatchResult &, |
| 94 | + State &S) { Action(cast<Node>(Stmt), S); }); |
| 95 | + return std::move(*this); |
| 96 | + } |
| 97 | + |
| 98 | + MatchSwitch<State> Build() && { |
| 99 | + return [Matcher = BuildMatcher(), Actions = std::move(Actions)]( |
| 100 | + const Stmt &Stmt, ASTContext &Context, State &S) { |
| 101 | + auto Results = ast_matchers::matchDynamic(Matcher, Stmt, Context); |
| 102 | + if (Results.empty()) |
| 103 | + return; |
| 104 | + // Look through the map for the first binding of the form "TagN..." use |
| 105 | + // that to select the action. |
| 106 | + for (const auto &Element : Results[0].getMap()) { |
| 107 | + llvm::StringRef ID(Element.first); |
| 108 | + size_t Index = 0; |
| 109 | + if (ID.consume_front("Tag") && !ID.getAsInteger(10, Index) && |
| 110 | + Index < Actions.size()) { |
| 111 | + Actions[Index]( |
| 112 | + &Stmt, |
| 113 | + ast_matchers::MatchFinder::MatchResult(Results[0], &Context), S); |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | +private: |
| 121 | + ast_matchers::internal::DynTypedMatcher BuildMatcher() { |
| 122 | + using ast_matchers::anything; |
| 123 | + using ast_matchers::stmt; |
| 124 | + using ast_matchers::unless; |
| 125 | + using ast_matchers::internal::DynTypedMatcher; |
| 126 | + if (Matchers.empty()) |
| 127 | + return stmt(unless(anything())); |
| 128 | + for (int I = 0, N = Matchers.size(); I < N; ++I) { |
| 129 | + std::string Tag = ("Tag" + llvm::Twine(I)).str(); |
| 130 | + // Many matchers are not bindable, so ensure that tryBind will work. |
| 131 | + Matchers[I].setAllowBind(true); |
| 132 | + auto M = *Matchers[I].tryBind(Tag); |
| 133 | + // Each anyOf explicitly controls the traversal kind. The anyOf itself is |
| 134 | + // set to `TK_AsIs` to ensure no nodes are skipped, thereby deferring to |
| 135 | + // the kind of the branches. Then, each branch is either left as is, if |
| 136 | + // the kind is already set, or explicitly set to `TK_AsIs`. We choose this |
| 137 | + // setting because it is the default interpretation of matchers. |
| 138 | + Matchers[I] = |
| 139 | + !M.getTraversalKind() ? M.withTraversalKind(TK_AsIs) : std::move(M); |
| 140 | + } |
| 141 | + // The matcher type on the cases ensures that `Expr` kind is compatible with |
| 142 | + // all of the matchers. |
| 143 | + return DynTypedMatcher::constructVariadic( |
| 144 | + DynTypedMatcher::VO_AnyOf, ASTNodeKind::getFromNodeKind<Stmt>(), |
| 145 | + std::move(Matchers)); |
| 146 | + } |
| 147 | + |
| 148 | + std::vector<ast_matchers::internal::DynTypedMatcher> Matchers; |
| 149 | + std::vector<Action> Actions; |
| 150 | +}; |
| 151 | +} // namespace dataflow |
| 152 | +} // namespace clang |
| 153 | +#endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_ |
0 commit comments