1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
//===- unittests/Analysis/FlowSensitive/MatchSwitchTest.cpp ---------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Stmt.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/StringRef.h"
#include "gtest/gtest.h"
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
using namespace clang;
using namespace dataflow;
using namespace ast_matchers;
namespace {
TEST(MatchSwitchTest, Stmts) {
std::string Code = R"(
void Foo();
void Bar();
void f() {
int X = 1;
Foo();
Bar();
}
)";
auto Unit = tooling::buildASTFromCode(Code);
auto &Ctx = Unit->getASTContext();
llvm::StringRef XStr = "X";
llvm::StringRef FooStr = "Foo";
llvm::StringRef BarStr = "Bar";
auto XMatcher = declStmt(hasSingleDecl(varDecl(hasName(XStr))));
auto FooMatcher = callExpr(callee(functionDecl(hasName(FooStr))));
auto BarMatcher = callExpr(callee(functionDecl(hasName(BarStr))));
ASTMatchSwitch<Stmt, llvm::StringRef> MS =
ASTMatchSwitchBuilder<Stmt, llvm::StringRef>()
.CaseOf<Stmt>(XMatcher,
[&XStr](const Stmt *, const MatchFinder::MatchResult &,
llvm::StringRef &State) { State = XStr; })
.CaseOf<Stmt>(FooMatcher,
[&FooStr](const Stmt *,
const MatchFinder::MatchResult &,
llvm::StringRef &State) { State = FooStr; })
.Build();
llvm::StringRef State;
// State modified from the first case of the switch
const auto *X = selectFirst<Stmt>(XStr, match(XMatcher.bind(XStr), Ctx));
MS(*X, Ctx, State);
EXPECT_EQ(State, XStr);
// State modified from the second case of the switch
const auto *Foo =
selectFirst<Stmt>(FooStr, match(FooMatcher.bind(FooStr), Ctx));
MS(*Foo, Ctx, State);
EXPECT_EQ(State, FooStr);
// State unmodified, no case defined for calling Bar
const auto *Bar =
selectFirst<Stmt>(BarStr, match(BarMatcher.bind(BarStr), Ctx));
MS(*Bar, Ctx, State);
EXPECT_EQ(State, FooStr);
}
TEST(MatchSwitchTest, CtorInitializers) {
std::string Code = R"(
struct f {
int i;
int j;
int z;
f(): i(1), j(1), z(1) {}
};
)";
auto Unit = tooling::buildASTFromCode(Code);
auto &Ctx = Unit->getASTContext();
llvm::StringRef IStr = "i";
llvm::StringRef JStr = "j";
llvm::StringRef ZStr = "z";
auto InitI = cxxCtorInitializer(forField(hasName(IStr)));
auto InitJ = cxxCtorInitializer(forField(hasName(JStr)));
auto InitZ = cxxCtorInitializer(forField(hasName(ZStr)));
ASTMatchSwitch<CXXCtorInitializer, llvm::StringRef> MS =
ASTMatchSwitchBuilder<CXXCtorInitializer, llvm::StringRef>()
.CaseOf<CXXCtorInitializer>(
InitI, [&IStr](const CXXCtorInitializer *,
const MatchFinder::MatchResult &,
llvm::StringRef &State) { State = IStr; })
.CaseOf<CXXCtorInitializer>(
InitJ, [&JStr](const CXXCtorInitializer *,
const MatchFinder::MatchResult &,
llvm::StringRef &State) { State = JStr; })
.Build();
llvm::StringRef State;
// State modified from the first case of the switch
const auto *I =
selectFirst<CXXCtorInitializer>(IStr, match(InitI.bind(IStr), Ctx));
MS(*I, Ctx, State);
EXPECT_EQ(State, IStr);
// State modified from the second case of the switch
const auto *J =
selectFirst<CXXCtorInitializer>(JStr, match(InitJ.bind(JStr), Ctx));
MS(*J, Ctx, State);
EXPECT_EQ(State, JStr);
// State unmodified, no case defined for the initializer of z
const auto *Z =
selectFirst<CXXCtorInitializer>(ZStr, match(InitZ.bind(ZStr), Ctx));
MS(*Z, Ctx, State);
EXPECT_EQ(State, JStr);
}
TEST(MatchSwitchTest, ReturnNonVoid) {
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();
ASTMatchSwitch<Stmt, const int, std::vector<int>> Switch =
ASTMatchSwitchBuilder<Stmt, 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);
}
} // namespace
|