Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "[clang][dataflow] Allow MatchSwitch to return a value"
This reverts commit 4eecd19.
  • Loading branch information
samestep committed Jun 24, 2022
1 parent 4eecd19 commit 7b326b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
26 changes: 13 additions & 13 deletions clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h
Expand Up @@ -46,8 +46,8 @@ template <typename LatticeT> struct TransferState {

/// Matches against `Stmt` and, based on its structure, dispatches to an
/// appropriate handler.
template <typename State, typename Result = void>
using MatchSwitch = std::function<Result(const Stmt &, ASTContext &, State &)>;
template <typename State>
using MatchSwitch = std::function<void(const Stmt &, 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 a
Expand All @@ -68,7 +68,7 @@ using MatchSwitch = std::function<Result(const Stmt &, ASTContext &, State &)>;
/// .Build();
/// }
/// \endcode
template <typename State, typename Result = void> class MatchSwitchBuilder {
template <typename State> class MatchSwitchBuilder {
public:
/// Registers an action that will be triggered by the match of a pattern
/// against the input statement.
Expand All @@ -79,37 +79,37 @@ template <typename State, typename Result = void> class MatchSwitchBuilder {
template <typename Node>
MatchSwitchBuilder &&
CaseOf(ast_matchers::internal::Matcher<Stmt> M,
std::function<Result(const Node *,
const ast_matchers::MatchFinder::MatchResult &,
State &)>
std::function<void(const Node *,
const ast_matchers::MatchFinder::MatchResult &,
State &)>
A) && {
Matchers.push_back(std::move(M));
Actions.push_back(
[A = std::move(A)](const Stmt *Stmt,
const ast_matchers::MatchFinder::MatchResult &R,
State &S) { return A(cast<Node>(Stmt), R, S); });
State &S) { A(cast<Node>(Stmt), R, S); });
return std::move(*this);
}

MatchSwitch<State, Result> Build() && {
MatchSwitch<State> Build() && {
return [Matcher = BuildMatcher(), Actions = std::move(Actions)](
const Stmt &Stmt, ASTContext &Context, State &S) -> Result {
const Stmt &Stmt, ASTContext &Context, State &S) {
auto Results = ast_matchers::matchDynamic(Matcher, Stmt, Context);
if (Results.empty())
return {};
return;
// Look through the map for the first binding of the form "TagN..." use
// that to select the action.
for (const auto &Element : Results[0].getMap()) {
llvm::StringRef ID(Element.first);
size_t Index = 0;
if (ID.consume_front("Tag") && !ID.getAsInteger(10, Index) &&
Index < Actions.size()) {
return Actions[Index](
Actions[Index](
&Stmt,
ast_matchers::MatchFinder::MatchResult(Results[0], &Context), S);
return;
}
}
return {};
};
}

Expand Down Expand Up @@ -142,7 +142,7 @@ template <typename State, typename Result = void> class MatchSwitchBuilder {
}

std::vector<ast_matchers::internal::DynTypedMatcher> Matchers;
std::vector<std::function<Result(
std::vector<std::function<void(
const Stmt *, const ast_matchers::MatchFinder::MatchResult &, State &)>>
Actions;
};
Expand Down
26 changes: 0 additions & 26 deletions clang/unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp
Expand Up @@ -204,29 +204,3 @@ TEST_F(MatchSwitchTest, Neither) {
RunDataflow(Code,
UnorderedElementsAre(Pair("p", Holds(BooleanLattice(false)))));
}

TEST_F(MatchSwitchTest, ReturnNonVoid) {
using namespace ast_matchers;

auto Unit =
tooling::buildASTFromCode("void f() { int x = 42; }", "input.cc",
std::make_shared<PCHContainerOperations>());
auto &Context = Unit->getASTContext();
const auto *S =
selectFirst<FunctionDecl>(
"f",
match(functionDecl(isDefinition(), hasName("f")).bind("f"), Context))
->getBody();

MatchSwitch<const int, std::vector<int>> Switch =
MatchSwitchBuilder<const int, std::vector<int>>()
.CaseOf<Stmt>(stmt(),
[](const Stmt *, const MatchFinder::MatchResult &,
const int &State) -> std::vector<int> {
return {1, State, 3};
})
.Build();
std::vector<int> Actual = Switch(*S, Context, 7);
std::vector<int> Expected{1, 7, 3};
EXPECT_EQ(Actual, Expected);
}

0 comments on commit 7b326b9

Please sign in to comment.