[clang] 14757d5 - [clang][dataflow] Refactor `TypeErasedDataflowAnalysisTest` - replace usage of the deprecated overload of `checkDataflow`.
Wei Yi Tee via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 1 12:18:18 PDT 2022
Author: Wei Yi Tee
Date: 2022-09-01T19:17:37Z
New Revision: 14757d5b845bdba0ea101b4cff2efc07261c898b
URL: https://github.com/llvm/llvm-project/commit/14757d5b845bdba0ea101b4cff2efc07261c898b
DIFF: https://github.com/llvm/llvm-project/commit/14757d5b845bdba0ea101b4cff2efc07261c898b.diff
LOG: [clang][dataflow] Refactor `TypeErasedDataflowAnalysisTest` - replace usage of the deprecated overload of `checkDataflow`.
Reviewed By: gribozavr2, sgatev
Differential Revision: https://reviews.llvm.org/D132756
Added:
Modified:
clang/unittests/Analysis/FlowSensitive/TestingSupport.h
clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
Removed:
################################################################################
diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
index e3b68ca2976d5..7ea51d83e25ed 100644
--- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
+++ b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
@@ -57,6 +57,21 @@ std::ostream &operator<<(std::ostream &OS,
namespace test {
+/// Returns the environment at the program point marked with `Annotation` from
+/// the mapping of annotated program points to analysis state.
+///
+/// Requirements:
+///
+/// `Annotation` must be present as a key in `AnnotationStates`.
+template <typename LatticeT>
+const Environment &getEnvironmentAtAnnotation(
+ const llvm::StringMap<DataflowAnalysisState<LatticeT>> &AnnotationStates,
+ llvm::StringRef Annotation) {
+ auto It = AnnotationStates.find(Annotation);
+ assert(It != AnnotationStates.end());
+ return It->getValue().Env;
+}
+
/// Contains data structures required and produced by a dataflow analysis run.
struct AnalysisOutputs {
/// Input code that is analyzed. Points within the code may be marked with
diff --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
index 0a87a7f02fa53..98847557206d3 100644
--- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -24,8 +24,10 @@
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
+#include "llvm/Testing/ADT/StringMapEntry.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
@@ -42,12 +44,10 @@ using namespace clang;
using namespace dataflow;
using namespace test;
using namespace ast_matchers;
-using ::testing::_;
-using ::testing::ElementsAre;
+using llvm::IsStringMapEntry;
+using ::testing::DescribeMatcher;
using ::testing::IsEmpty;
-using ::testing::IsNull;
using ::testing::NotNull;
-using ::testing::Pair;
using ::testing::Test;
using ::testing::UnorderedElementsAre;
@@ -129,7 +129,8 @@ TEST(DataflowAnalysisTest, NonConvergingAnalysis) {
}
struct FunctionCallLattice {
- llvm::SmallSet<std::string, 8> CalledFunctions;
+ using FunctionSet = llvm::SmallSet<std::string, 8>;
+ FunctionSet CalledFunctions;
bool operator==(const FunctionCallLattice &Other) const {
return CalledFunctions == Other.CalledFunctions;
@@ -195,16 +196,20 @@ class NoreturnDestructorTest : public Test {
ASSERT_THAT_ERROR(
test::checkDataflow<FunctionCallAnalysis>(
- Code, "target",
- [](ASTContext &C, Environment &) {
- return FunctionCallAnalysis(C);
- },
+ AnalysisInputs<FunctionCallAnalysis>(
+ Code, ast_matchers::hasName("target"),
+ [](ASTContext &C, Environment &) {
+ return FunctionCallAnalysis(C);
+ })
+ .withASTBuildArgs({"-fsyntax-only", "-std=c++17"})
+ .withASTBuildVirtualMappedFiles(std::move(FilesContents)),
+ /*VerifyResults=*/
[&Expectations](
- llvm::ArrayRef<std::pair<
- std::string, DataflowAnalysisState<FunctionCallLattice>>>
- Results,
- ASTContext &) { EXPECT_THAT(Results, Expectations); },
- {"-fsyntax-only", "-std=c++17"}, FilesContents),
+ const llvm::StringMap<
+ DataflowAnalysisState<FunctionCallLattice>> &Results,
+ const AnalysisOutputs &) {
+ EXPECT_THAT(Results, Expectations);
+ }),
llvm::Succeeded());
}
};
@@ -212,12 +217,16 @@ class NoreturnDestructorTest : public Test {
MATCHER_P(HoldsFunctionCallLattice, m,
((negation ? "doesn't hold" : "holds") +
llvm::StringRef(" a lattice element that ") +
- ::testing::DescribeMatcher<FunctionCallLattice>(m, negation))
+ DescribeMatcher<FunctionCallLattice>(m))
.str()) {
return ExplainMatchResult(m, arg.Lattice, result_listener);
}
-MATCHER_P(HasCalledFunctions, m, "") {
+MATCHER_P(HasCalledFunctions, m,
+ ((negation ? "doesn't hold" : "holds") +
+ llvm::StringRef(" a set of called functions that ") +
+ DescribeMatcher<FunctionCallLattice::FunctionSet>(m))
+ .str()) {
return ExplainMatchResult(m, arg.CalledFunctions, result_listener);
}
@@ -231,9 +240,9 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorBothBranchesReturn) {
// [[p]]
}
)";
- runDataflow(Code, UnorderedElementsAre(
- Pair("p", HoldsFunctionCallLattice(HasCalledFunctions(
- UnorderedElementsAre("foo", "bar"))))));
+ runDataflow(Code, UnorderedElementsAre(IsStringMapEntry(
+ "p", HoldsFunctionCallLattice(HasCalledFunctions(
+ UnorderedElementsAre("foo", "bar"))))));
}
TEST_F(NoreturnDestructorTest, ConditionalOperatorLeftBranchReturns) {
@@ -246,9 +255,9 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorLeftBranchReturns) {
// [[p]]
}
)";
- runDataflow(Code, UnorderedElementsAre(
- Pair("p", HoldsFunctionCallLattice(HasCalledFunctions(
- UnorderedElementsAre("foo"))))));
+ runDataflow(Code, UnorderedElementsAre(IsStringMapEntry(
+ "p", HoldsFunctionCallLattice(HasCalledFunctions(
+ UnorderedElementsAre("foo"))))));
}
TEST_F(NoreturnDestructorTest, ConditionalOperatorRightBranchReturns) {
@@ -261,9 +270,9 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorRightBranchReturns) {
// [[p]]
}
)";
- runDataflow(Code, UnorderedElementsAre(
- Pair("p", HoldsFunctionCallLattice(HasCalledFunctions(
- UnorderedElementsAre("foo"))))));
+ runDataflow(Code, UnorderedElementsAre(IsStringMapEntry(
+ "p", HoldsFunctionCallLattice(HasCalledFunctions(
+ UnorderedElementsAre("foo"))))));
}
TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchesDoNotReturn) {
@@ -290,9 +299,9 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchReturns) {
// [[p]]
}
)";
- runDataflow(Code, UnorderedElementsAre(
- Pair("p", HoldsFunctionCallLattice(HasCalledFunctions(
- UnorderedElementsAre("baz", "foo"))))));
+ runDataflow(Code, UnorderedElementsAre(IsStringMapEntry(
+ "p", HoldsFunctionCallLattice(HasCalledFunctions(
+ UnorderedElementsAre("baz", "foo"))))));
// FIXME: Called functions at point `p` should contain only "foo".
}
@@ -386,16 +395,17 @@ class JoinFlowConditionsTest : public Test {
void runDataflow(llvm::StringRef Code, Matcher Match) {
ASSERT_THAT_ERROR(
test::checkDataflow<SpecialBoolAnalysis>(
- Code, "target",
- [](ASTContext &Context, Environment &Env) {
- return SpecialBoolAnalysis(Context);
- },
- [&Match](
- llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) { Match(Results, ASTCtx); },
- {"-fsyntax-only", "-std=c++17"}),
+ AnalysisInputs<SpecialBoolAnalysis>(
+ Code, ast_matchers::hasName("target"),
+ [](ASTContext &Context, Environment &Env) {
+ return SpecialBoolAnalysis(Context);
+ })
+ .withASTBuildArgs({"-fsyntax-only", "-std=c++17"}),
+ /*VerifyResults=*/[&Match](const llvm::StringMap<
+ DataflowAnalysisState<NoopLattice>>
+ &Results,
+ const AnalysisOutputs
+ &AO) { Match(Results, AO.ASTCtx); }),
llvm::Succeeded());
}
};
@@ -422,16 +432,15 @@ TEST_F(JoinFlowConditionsTest, JoinDistinctButProvablyEquivalentValues) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _),
- Pair("p3", _), Pair("p4", _)));
- const Environment &Env1 = Results[0].second.Env;
- const Environment &Env2 = Results[1].second.Env;
- const Environment &Env3 = Results[2].second.Env;
- const Environment &Env4 = Results[3].second.Env;
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(),
+ UnorderedElementsAre("p1", "p2", "p3", "p4"));
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3");
+ const Environment &Env4 = getEnvironmentAtAnnotation(Results, "p4");
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
ASSERT_THAT(FooDecl, NotNull());
@@ -537,20 +546,22 @@ class WideningTest : public Test {
};
)"));
ASSERT_THAT_ERROR(
- test::checkDataflow<OptionalIntAnalysis>(
- Code, "target",
- [this](ASTContext &Context, Environment &Env) {
- assert(HasValueTop == nullptr);
- HasValueTop =
- &Env.takeOwnership(std::make_unique<AtomicBoolValue>());
- return OptionalIntAnalysis(Context, *HasValueTop);
- },
- [&Match](
- llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) { Match(Results, ASTCtx); },
- {"-fsyntax-only", "-std=c++17"}, FilesContents),
+ checkDataflow<OptionalIntAnalysis>(
+ AnalysisInputs<OptionalIntAnalysis>(
+ Code, ast_matchers::hasName("target"),
+ [this](ASTContext &Context, Environment &Env) {
+ assert(HasValueTop == nullptr);
+ HasValueTop =
+ &Env.takeOwnership(std::make_unique<AtomicBoolValue>());
+ return OptionalIntAnalysis(Context, *HasValueTop);
+ })
+ .withASTBuildArgs({"-fsyntax-only", "-std=c++17"})
+ .withASTBuildVirtualMappedFiles(std::move(FilesContents)),
+ /*VerifyResults=*/[&Match](const llvm::StringMap<
+ DataflowAnalysisState<NoopLattice>>
+ &Results,
+ const AnalysisOutputs
+ &AO) { Match(Results, AO.ASTCtx); }),
llvm::Succeeded());
}
@@ -574,15 +585,12 @@ TEST_F(WideningTest, JoinDistinctValuesWithDistinctProperties) {
)";
runDataflow(
Code,
- [this](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
+ [this](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
- ASSERT_THAT(Results,
- ElementsAre(Pair("p1", _), Pair("p2", _), Pair("p3", _)));
- const Environment &Env1 = Results[0].second.Env;
- const Environment &Env2 = Results[1].second.Env;
- const Environment &Env3 = Results[2].second.Env;
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2", "p3"));
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3");
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
ASSERT_THAT(FooDecl, NotNull());
@@ -617,34 +625,33 @@ TEST_F(WideningTest, JoinDistinctValuesWithSameProperties) {
/*[[p4]]*/
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _),
- Pair("p3", _), Pair("p4", _)));
- const Environment &Env1 = Results[0].second.Env;
- const Environment &Env2 = Results[1].second.Env;
- const Environment &Env3 = Results[2].second.Env;
- const Environment &Env4 = Results[3].second.Env;
-
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- auto GetFooValue = [FooDecl](const Environment &Env) {
- return Env.getValue(*FooDecl, SkipPast::None);
- };
-
- EXPECT_EQ(GetFooValue(Env1)->getProperty("has_value"),
- &Env1.getBoolLiteralValue(false));
- EXPECT_EQ(GetFooValue(Env2)->getProperty("has_value"),
- &Env2.getBoolLiteralValue(true));
- EXPECT_EQ(GetFooValue(Env3)->getProperty("has_value"),
- &Env3.getBoolLiteralValue(true));
- EXPECT_EQ(GetFooValue(Env4)->getProperty("has_value"),
- &Env4.getBoolLiteralValue(true));
- });
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(),
+ UnorderedElementsAre("p1", "p2", "p3", "p4"));
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3");
+ const Environment &Env4 = getEnvironmentAtAnnotation(Results, "p4");
+
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ auto GetFooValue = [FooDecl](const Environment &Env) {
+ return Env.getValue(*FooDecl, SkipPast::None);
+ };
+
+ EXPECT_EQ(GetFooValue(Env1)->getProperty("has_value"),
+ &Env1.getBoolLiteralValue(false));
+ EXPECT_EQ(GetFooValue(Env2)->getProperty("has_value"),
+ &Env2.getBoolLiteralValue(true));
+ EXPECT_EQ(GetFooValue(Env3)->getProperty("has_value"),
+ &Env3.getBoolLiteralValue(true));
+ EXPECT_EQ(GetFooValue(Env4)->getProperty("has_value"),
+ &Env4.getBoolLiteralValue(true));
+ });
}
TEST_F(WideningTest, DistinctPointersToTheSameLocationAreEquivalent) {
@@ -658,26 +665,25 @@ TEST_F(WideningTest, DistinctPointersToTheSameLocationAreEquivalent) {
// [[p]]
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
-
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
- ASSERT_THAT(BarDecl, NotNull());
-
- const auto *FooLoc = cast<ScalarStorageLocation>(
- Env.getStorageLocation(*FooDecl, SkipPast::None));
- const auto *BarVal =
- cast<PointerValue>(Env.getValue(*BarDecl, SkipPast::None));
- EXPECT_EQ(&BarVal->getPointeeLoc(), FooLoc);
- });
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ const auto *FooLoc = cast<ScalarStorageLocation>(
+ Env.getStorageLocation(*FooDecl, SkipPast::None));
+ const auto *BarVal =
+ cast<PointerValue>(Env.getValue(*BarDecl, SkipPast::None));
+ EXPECT_EQ(&BarVal->getPointeeLoc(), FooLoc);
+ });
}
TEST_F(WideningTest, DistinctValuesWithSamePropertiesAreEquivalent) {
@@ -694,21 +700,20 @@ TEST_F(WideningTest, DistinctValuesWithSamePropertiesAreEquivalent) {
/*[[p]]*/
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
-
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const auto *FooVal = Env.getValue(*FooDecl, SkipPast::None);
- EXPECT_EQ(FooVal->getProperty("has_value"),
- &Env.getBoolLiteralValue(true));
- });
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const auto *FooVal = Env.getValue(*FooDecl, SkipPast::None);
+ EXPECT_EQ(FooVal->getProperty("has_value"),
+ &Env.getBoolLiteralValue(true));
+ });
}
class FlowConditionTest : public Test {
@@ -716,17 +721,18 @@ class FlowConditionTest : public Test {
template <typename Matcher>
void runDataflow(llvm::StringRef Code, Matcher Match) {
ASSERT_THAT_ERROR(
- test::checkDataflow<NoopAnalysis>(
- Code, "target",
- [](ASTContext &Context, Environment &Env) {
- return NoopAnalysis(Context, true);
- },
- [&Match](
- llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) { Match(Results, ASTCtx); },
- {"-fsyntax-only", "-std=c++17"}),
+ checkDataflow<NoopAnalysis>(
+ AnalysisInputs<NoopAnalysis>(
+ Code, ast_matchers::hasName("target"),
+ [](ASTContext &Context, Environment &Env) {
+ return NoopAnalysis(Context, true);
+ })
+ .withASTBuildArgs({"-fsyntax-only", "-std=c++17"}),
+ /*VerifyResults=*/[&Match](const llvm::StringMap<
+ DataflowAnalysisState<NoopLattice>>
+ &Results,
+ const AnalysisOutputs
+ &AO) { Match(Results, AO.ASTCtx); }),
llvm::Succeeded());
}
};
@@ -743,26 +749,25 @@ TEST_F(FlowConditionTest, IfStmtSingleVar) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
- });
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 =
+ cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 =
+ cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
+ });
}
TEST_F(FlowConditionTest, IfStmtSingleNegatedVar) {
@@ -777,26 +782,25 @@ TEST_F(FlowConditionTest, IfStmtSingleNegatedVar) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- EXPECT_TRUE(Env2.flowConditionImplies(*FooVal2));
- });
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 =
+ cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 =
+ cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ EXPECT_TRUE(Env2.flowConditionImplies(*FooVal2));
+ });
}
TEST_F(FlowConditionTest, WhileStmt) {
@@ -809,15 +813,14 @@ TEST_F(FlowConditionTest, WhileStmt) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
ASSERT_THAT(FooDecl, NotNull());
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
auto *FooVal = cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None));
EXPECT_TRUE(Env.flowConditionImplies(*FooVal));
@@ -836,35 +839,29 @@ TEST_F(FlowConditionTest, Conjunction) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
- ASSERT_THAT(BarDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- auto *BarVal1 =
- cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
- EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
- EXPECT_TRUE(Env1.flowConditionImplies(*BarVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- auto *BarVal2 =
- cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
- EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
- EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
- });
+ runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>>
+ &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
+ EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
+ EXPECT_TRUE(Env1.flowConditionImplies(*BarVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
+ EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
+ EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
+ });
}
TEST_F(FlowConditionTest, Disjunction) {
@@ -879,35 +876,29 @@ TEST_F(FlowConditionTest, Disjunction) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
- ASSERT_THAT(BarDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- auto *BarVal1 =
- cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
- EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
- EXPECT_FALSE(Env1.flowConditionImplies(*BarVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- auto *BarVal2 =
- cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
- EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
- EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
- });
+ runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>>
+ &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
+ EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
+ EXPECT_FALSE(Env1.flowConditionImplies(*BarVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
+ EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
+ EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
+ });
}
TEST_F(FlowConditionTest, NegatedConjunction) {
@@ -922,35 +913,29 @@ TEST_F(FlowConditionTest, NegatedConjunction) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
- ASSERT_THAT(BarDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- auto *BarVal1 =
- cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
- EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
- EXPECT_FALSE(Env1.flowConditionImplies(*BarVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- auto *BarVal2 =
- cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
- EXPECT_TRUE(Env2.flowConditionImplies(*FooVal2));
- EXPECT_TRUE(Env2.flowConditionImplies(*BarVal2));
- });
+ runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>>
+ &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
+ EXPECT_FALSE(Env1.flowConditionImplies(*FooVal1));
+ EXPECT_FALSE(Env1.flowConditionImplies(*BarVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
+ EXPECT_TRUE(Env2.flowConditionImplies(*FooVal2));
+ EXPECT_TRUE(Env2.flowConditionImplies(*BarVal2));
+ });
}
TEST_F(FlowConditionTest, DeMorgan) {
@@ -965,35 +950,29 @@ TEST_F(FlowConditionTest, DeMorgan) {
}
}
)";
- runDataflow(Code,
- [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
- ASSERT_THAT(FooDecl, NotNull());
-
- const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
- ASSERT_THAT(BarDecl, NotNull());
-
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
-
- const Environment &Env1 = Results[0].second.Env;
- auto *FooVal1 =
- cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
- auto *BarVal1 =
- cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
- EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
- EXPECT_TRUE(Env1.flowConditionImplies(*BarVal1));
-
- const Environment &Env2 = Results[1].second.Env;
- auto *FooVal2 =
- cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
- auto *BarVal2 =
- cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
- EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
- EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
- });
+ runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>>
+ &Results,
+ ASTContext &ASTCtx) {
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
+ auto *FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl, SkipPast::None));
+ EXPECT_TRUE(Env1.flowConditionImplies(*FooVal1));
+ EXPECT_TRUE(Env1.flowConditionImplies(*BarVal1));
+
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
+ auto *FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::None));
+ auto *BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl, SkipPast::None));
+ EXPECT_FALSE(Env2.flowConditionImplies(*FooVal2));
+ EXPECT_FALSE(Env2.flowConditionImplies(*BarVal2));
+ });
}
TEST_F(FlowConditionTest, Join) {
@@ -1011,16 +990,15 @@ TEST_F(FlowConditionTest, Join) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
ASSERT_THAT(FooDecl, NotNull());
- const Environment &Env = Results[0].second.Env;
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
auto *FooVal = cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None));
EXPECT_TRUE(Env.flowConditionImplies(*FooVal));
});
@@ -1045,12 +1023,11 @@ TEST_F(FlowConditionTest, OpaqueFlowConditionMergesToOpaqueBool) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
ASSERT_THAT(BarDecl, NotNull());
@@ -1088,12 +1065,11 @@ TEST_F(FlowConditionTest, OpaqueFieldFlowConditionMergesToOpaqueBool) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
ASSERT_THAT(BarDecl, NotNull());
@@ -1124,12 +1100,11 @@ TEST_F(FlowConditionTest, OpaqueFlowConditionInsideBranchMergesToOpaqueBool) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
- const Environment &Env = Results[0].second.Env;
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
ASSERT_THAT(BarDecl, NotNull());
@@ -1155,20 +1130,20 @@ TEST_F(FlowConditionTest, PointerToBoolImplicitCast) {
}
)";
runDataflow(
- Code, [](llvm::ArrayRef<
- std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
- Results,
- ASTContext &ASTCtx) {
- ASSERT_THAT(Results, ElementsAre(Pair("p1", _), Pair("p2", _)));
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2"));
+
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
ASSERT_THAT(FooDecl, NotNull());
- const Environment &Env1 = Results[0].second.Env;
+ const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
auto &FooVal1 =
*cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::Reference));
EXPECT_TRUE(Env1.flowConditionImplies(FooVal1));
- const Environment &Env2 = Results[1].second.Env;
+ const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2");
auto &FooVal2 =
*cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::Reference));
EXPECT_FALSE(Env2.flowConditionImplies(FooVal2));
diff --git a/utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
index 0c458a710bb98..9f26e8a7266d2 100644
--- a/utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
@@ -134,6 +134,7 @@ cc_test(
"//clang:serialization",
"//clang:tooling",
"//llvm:Support",
+ "//llvm:TestingADT",
"//llvm:TestingSupport",
"//llvm:gmock",
"//llvm:gtest",
More information about the cfe-commits
mailing list