From 7b326b946a38f58c9b3fdfeee09678bc4bf91292 Mon Sep 17 00:00:00 2001 From: Sam Estep Date: Fri, 24 Jun 2022 13:52:11 +0000 Subject: [PATCH] Revert "[clang][dataflow] Allow MatchSwitch to return a value" This reverts commit 4eecd194b073492a309b87c8f60da6614bba9153. --- .../Analysis/FlowSensitive/MatchSwitch.h | 26 +++++++++---------- .../FlowSensitive/MatchSwitchTest.cpp | 26 ------------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h b/clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h index 77271bda188d5..2aaaf78f9cd0e 100644 --- a/clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h +++ b/clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h @@ -46,8 +46,8 @@ template struct TransferState { /// Matches against `Stmt` and, based on its structure, dispatches to an /// appropriate handler. -template -using MatchSwitch = std::function; +template +using MatchSwitch = std::function; /// Collects cases of a "match switch": a collection of matchers paired with /// callbacks, which together define a switch that can be applied to a @@ -68,7 +68,7 @@ using MatchSwitch = std::function; /// .Build(); /// } /// \endcode -template class MatchSwitchBuilder { +template class MatchSwitchBuilder { public: /// Registers an action that will be triggered by the match of a pattern /// against the input statement. @@ -79,24 +79,24 @@ template class MatchSwitchBuilder { template MatchSwitchBuilder && CaseOf(ast_matchers::internal::Matcher M, - std::function + std::function 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(Stmt), R, S); }); + State &S) { A(cast(Stmt), R, S); }); return std::move(*this); } - MatchSwitch Build() && { + MatchSwitch 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()) { @@ -104,12 +104,12 @@ template class MatchSwitchBuilder { 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 {}; }; } @@ -142,7 +142,7 @@ template class MatchSwitchBuilder { } std::vector Matchers; - std::vector> Actions; }; diff --git a/clang/unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp b/clang/unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp index 98319760fd206..bd2ae0e96c01e 100644 --- a/clang/unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp @@ -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()); - auto &Context = Unit->getASTContext(); - const auto *S = - selectFirst( - "f", - match(functionDecl(isDefinition(), hasName("f")).bind("f"), Context)) - ->getBody(); - - MatchSwitch> Switch = - MatchSwitchBuilder>() - .CaseOf(stmt(), - [](const Stmt *, const MatchFinder::MatchResult &, - const int &State) -> std::vector { - return {1, State, 3}; - }) - .Build(); - std::vector Actual = Switch(*S, Context, 7); - std::vector Expected{1, 7, 3}; - EXPECT_EQ(Actual, Expected); -}