ymandel created this revision. ymandel added a reviewer: gribozavr2. ymandel requested review of this revision. Herald added a project: clang.
Users outside of the clang repo may use different googletest versions. So, it's better not to depend on llvm's googletest. This patch removes the dependency by having `checkDataflow` return an `llvm::Error` instead of calling googletest's `FAIL` or `ASSERT...` macros. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D117304 Files: clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp clang/unittests/Analysis/FlowSensitive/TestingSupport.h clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp clang/unittests/Analysis/FlowSensitive/TransferTest.cpp clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
Index: clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp @@ -21,6 +21,7 @@ #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <cassert> @@ -196,15 +197,19 @@ }; )")); - test::checkDataflow<FunctionCallAnalysis>( - Code, "target", - [](ASTContext &C, Environment &) { return FunctionCallAnalysis(C); }, - [&Expectations]( - llvm::ArrayRef<std::pair< - std::string, DataflowAnalysisState<FunctionCallLattice>>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}, FilesContents); + ASSERT_THAT_ERROR( + test::checkDataflow<FunctionCallAnalysis>( + Code, "target", + [](ASTContext &C, Environment &) { + return FunctionCallAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef<std::pair< + std::string, DataflowAnalysisState<FunctionCallLattice>>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}, FilesContents), + llvm::Succeeded()); } }; Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -18,6 +18,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <cassert> @@ -38,14 +39,17 @@ protected: template <typename Matcher> void runDataflow(llvm::StringRef Code, Matcher Match) { - test::checkDataflow<NoopAnalysis>( - Code, "target", - [](ASTContext &C, Environment &) { return NoopAnalysis(C); }, - [&Match](llvm::ArrayRef< - std::pair<std::string, DataflowAnalysisState<NoopLattice>>> - Results, - ASTContext &ASTCtx) { Match(Results, ASTCtx); }, - {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow<NoopAnalysis>( + Code, "target", + [](ASTContext &C, Environment &) { return NoopAnalysis(C); }, + [&Match]( + llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { Match(Results, ASTCtx); }, + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } }; Index: clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp @@ -4,6 +4,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Tooling/Tooling.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -77,10 +78,14 @@ std::string, DataflowAnalysisState<NoopLattice>>>, ASTContext &)> Expectations) { - test::checkDataflow<NoopAnalysis>( - Code, Target, - [](ASTContext &Context, Environment &) { return NoopAnalysis(Context); }, - std::move(Expectations), {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow<NoopAnalysis>(Code, Target, + [](ASTContext &Context, Environment &) { + return NoopAnalysis(Context); + }, + std::move(Expectations), + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } TEST(ProgramPointAnnotations, NoAnnotations) { Index: clang/unittests/Analysis/FlowSensitive/TestingSupport.h =================================================================== --- clang/unittests/Analysis/FlowSensitive/TestingSupport.h +++ clang/unittests/Analysis/FlowSensitive/TestingSupport.h @@ -31,11 +31,13 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Testing/Support/Annotations.h" -#include "gtest/gtest.h" #include <functional> #include <memory> +#include <ostream> #include <string> #include <utility> @@ -62,7 +64,7 @@ // Runs dataflow on the body of the function that matches `func_matcher` in code // snippet `code`. Requires: `Analysis` contains a type `Lattice`. template <typename AnalysisT> -void checkDataflow( +llvm::Error checkDataflow( llvm::StringRef Code, ast_matchers::internal::Matcher<FunctionDecl> FuncMatcher, std::function<AnalysisT(ASTContext &, Environment &)> MakeAnalysis, @@ -83,8 +85,9 @@ auto &Context = Unit->getASTContext(); if (Context.getDiagnostics().getClient()->getNumErrors() != 0) { - FAIL() << "Source file has syntax or type errors, they were printed to " - "the test log"; + return llvm::make_error<llvm::StringError>( + llvm::errc::invalid_argument, "Source file has syntax or type errors, " + "they were printed to the test log"); } const FunctionDecl *F = ast_matchers::selectFirst<FunctionDecl>( @@ -93,10 +96,13 @@ ast_matchers::functionDecl(ast_matchers::isDefinition(), FuncMatcher) .bind("target"), Context)); - ASSERT_TRUE(F != nullptr) << "Could not find target function."; + if (F == nullptr) + return llvm::make_error<llvm::StringError>( + llvm::errc::invalid_argument, "Could not find target function."); auto CFCtx = ControlFlowContext::build(F, F->getBody(), &F->getASTContext()); - ASSERT_TRUE((bool)CFCtx) << "Could not build ControlFlowContext."; + if (!CFCtx) + return CFCtx.takeError(); DataflowAnalysisContext DACtx; Environment Env(DACtx, *F); @@ -104,11 +110,9 @@ llvm::Expected<llvm::DenseMap<const clang::Stmt *, std::string>> StmtToAnnotations = buildStatementToAnnotationMapping(F, AnnotatedCode); - if (auto E = StmtToAnnotations.takeError()) { - FAIL() << "Failed to build annotation map: " - << llvm::toString(std::move(E)); - return; - } + if (!StmtToAnnotations) + return StmtToAnnotations.takeError(); + auto &Annotations = *StmtToAnnotations; std::vector<llvm::Optional<TypeErasedDataflowAnalysisState>> BlockStates = @@ -116,7 +120,7 @@ if (BlockStates.empty()) { Expectations({}, Context); - return; + return llvm::Error::success(); } // Compute a map from statement annotations to the state computed for @@ -134,21 +138,21 @@ auto It = Annotations.find(Stmt.getStmt()); if (It == Annotations.end()) return; - if (auto *Lattice = llvm::any_cast<typename AnalysisT::Lattice>( - &State.Lattice.Value)) { - Results.emplace_back(It->second, StateT{*Lattice, State.Env}); - } else { - FAIL() << "Could not cast lattice element to expected type."; - } + assert( + llvm::any_isa<typename AnalysisT::Lattice>(State.Lattice.Value)); + auto *Lattice = + llvm::any_cast<typename AnalysisT::Lattice>(&State.Lattice.Value); + Results.emplace_back(It->second, StateT{*Lattice, State.Env}); }); } Expectations(Results, Context); + return llvm::Error::success(); } // Runs dataflow on the body of the function named `target_fun` in code snippet // `code`. template <typename AnalysisT> -void checkDataflow( +llvm::Error checkDataflow( llvm::StringRef Code, llvm::StringRef TargetFun, std::function<AnalysisT(ASTContext &, Environment &)> MakeAnalysis, std::function<void( @@ -158,8 +162,9 @@ Expectations, ArrayRef<std::string> Args, const tooling::FileContentMappings &VirtualMappedFiles = {}) { - checkDataflow(Code, ast_matchers::hasName(TargetFun), std::move(MakeAnalysis), - std::move(Expectations), Args, VirtualMappedFiles); + return checkDataflow(Code, ast_matchers::hasName(TargetFun), + std::move(MakeAnalysis), std::move(Expectations), Args, + VirtualMappedFiles); } } // namespace test Index: clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp +++ clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp @@ -20,7 +20,6 @@ #include "llvm/ADT/Optional.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/Annotations.h" -#include "gtest/gtest.h" #include <functional> #include <memory> #include <string> @@ -31,28 +30,6 @@ using namespace clang; using namespace dataflow; -namespace { -using ast_matchers::MatchFinder; - -class FindTranslationUnitCallback : public MatchFinder::MatchCallback { -public: - explicit FindTranslationUnitCallback( - std::function<void(ASTContext &)> Operation) - : Operation{Operation} {} - - void run(const MatchFinder::MatchResult &Result) override { - const auto *TU = Result.Nodes.getNodeAs<TranslationUnitDecl>("tu"); - if (TU->getASTContext().getDiagnostics().getClient()->getNumErrors() != 0) { - FAIL() << "Source file has syntax or type errors, they were printed to " - "the test log"; - } - Operation(TU->getASTContext()); - } - - std::function<void(ASTContext &)> Operation; -}; -} // namespace - static bool isAnnotationDirectlyAfterStatement(const Stmt *Stmt, unsigned AnnotationBegin, const SourceManager &SourceManager, Index: clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp +++ clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp @@ -29,6 +29,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/Annotations.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <cstdint> @@ -189,18 +190,20 @@ protected: template <typename Matcher> void RunDataflow(llvm::StringRef Code, Matcher Expectations) { - test::checkDataflow<ConstantPropagationAnalysis>( - Code, "fun", - [](ASTContext &C, Environment &) { - return ConstantPropagationAnalysis(C); - }, - [&Expectations]( - llvm::ArrayRef<std::pair< - std::string, - DataflowAnalysisState<ConstantPropagationAnalysis::Lattice>>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow<ConstantPropagationAnalysis>( + Code, "fun", + [](ASTContext &C, Environment &) { + return ConstantPropagationAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef<std::pair< + std::string, DataflowAnalysisState< + ConstantPropagationAnalysis::Lattice>>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } }; Index: clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp +++ clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp @@ -30,6 +30,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/Annotations.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <cstdint> @@ -211,18 +212,20 @@ protected: template <typename Matcher> void RunDataflow(llvm::StringRef Code, Matcher Expectations) { - test::checkDataflow<ConstantPropagationAnalysis>( - Code, "fun", - [](ASTContext &C, Environment &) { - return ConstantPropagationAnalysis(C); - }, - [&Expectations]( - llvm::ArrayRef<std::pair< - std::string, - DataflowAnalysisState<ConstantPropagationAnalysis::Lattice>>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow<ConstantPropagationAnalysis>( + Code, "fun", + [](ASTContext &C, Environment &) { + return ConstantPropagationAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef<std::pair< + std::string, DataflowAnalysisState< + ConstantPropagationAnalysis::Lattice>>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } };
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits