//===---- MatchSwitch.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 `ASTMatchSwitch` abstraction for building a "switch" // statement, where each case of the switch is defined by an AST matcher. The // cases are considered in order, like pattern matching in functional // languages. // // Currently, the design is catered towards simplifying the implementation of // `DataflowAnalysis` transfer functions. Based on experience here, this // library may be generalized and moved to ASTMatchers. // //===----------------------------------------------------------------------===// // // FIXME: Rename to ASTMatchSwitch.h #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_ #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_ #include "clang/AST/ASTContext.h" #include "clang/AST/Stmt.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" #include "llvm/ADT/StringRef.h" #include <functional> #include <string> #include <type_traits> #include <utility> #include <vector> namespace clang { namespace dataflow { /// A common form of state shared between the cases of a transfer function. template <typename LatticeT> struct TransferState { … }; /// A read-only version of TransferState. /// /// FIXME: this type is being used as a general (typed) view type for untyped /// dataflow analysis state, rather than strictly for transfer-function /// purposes. Move it (and rename it) to DataflowAnalysis.h. template <typename LatticeT> struct TransferStateForDiagnostics { … }; MatchSwitchMatcher; MatchSwitchAction; ASTMatchSwitch; /// Collects cases of a "match switch": a collection of matchers paired with /// callbacks, which together define a switch that can be applied to a node /// whose type derives from `BaseT`. This structure can simplify the definition /// of `transfer` functions that rely on pattern-matching. /// /// For example, consider an analysis that handles particular function calls. It /// can define the `ASTMatchSwitch` once, in the constructor of the analysis, /// and then reuse it each time that `transfer` is called, with a fresh state /// value. /// /// \code /// ASTMatchSwitch<Stmt, TransferState<MyLattice> BuildSwitch() { /// return ASTMatchSwitchBuilder<TransferState<MyLattice>>() /// .CaseOf(callExpr(callee(functionDecl(hasName("foo")))), TransferFooCall) /// .CaseOf(callExpr(argumentCountIs(2), /// callee(functionDecl(hasName("bar")))), /// TransferBarCall) /// .Build(); /// } /// \endcode template <typename BaseT, typename State, typename Result = void> class ASTMatchSwitchBuilder { … }; } // namespace dataflow } // namespace clang #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_MATCHSWITCH_H_