diff --git a/clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp b/clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp +++ b/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 @@ -211,18 +212,20 @@ protected: template void RunDataflow(llvm::StringRef Code, Matcher Expectations) { - test::checkDataflow( - Code, "fun", - [](ASTContext &C, Environment &) { - return ConstantPropagationAnalysis(C); - }, - [&Expectations]( - llvm::ArrayRef>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow( + Code, "fun", + [](ASTContext &C, Environment &) { + return ConstantPropagationAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } }; diff --git a/clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp b/clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp +++ b/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 @@ -189,18 +190,20 @@ protected: template void RunDataflow(llvm::StringRef Code, Matcher Expectations) { - test::checkDataflow( - Code, "fun", - [](ASTContext &C, Environment &) { - return ConstantPropagationAnalysis(C); - }, - [&Expectations]( - llvm::ArrayRef>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow( + Code, "fun", + [](ASTContext &C, Environment &) { + return ConstantPropagationAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } }; diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h --- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h +++ b/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 #include +#include #include #include @@ -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 -void checkDataflow( +llvm::Error checkDataflow( llvm::StringRef Code, ast_matchers::internal::Matcher FuncMatcher, std::function 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::errc::invalid_argument, "Source file has syntax or type errors, " + "they were printed to the test log"); } const FunctionDecl *F = ast_matchers::selectFirst( @@ -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::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> 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> 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,19 @@ auto It = Annotations.find(Stmt.getStmt()); if (It == Annotations.end()) return; - if (auto *Lattice = llvm::any_cast( - &State.Lattice.Value)) { - Results.emplace_back(It->second, StateT{*Lattice, State.Env}); - } else { - FAIL() << "Could not cast lattice element to expected type."; - } + auto *Lattice = + llvm::any_cast(&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 -void checkDataflow( +llvm::Error checkDataflow( llvm::StringRef Code, llvm::StringRef TargetFun, std::function MakeAnalysis, std::function 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 diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp b/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp --- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp +++ b/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 #include #include @@ -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 Operation) - : Operation{Operation} {} - - void run(const MatchFinder::MatchResult &Result) override { - const auto *TU = Result.Nodes.getNodeAs("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 Operation; -}; -} // namespace - static bool isAnnotationDirectlyAfterStatement(const Stmt *Stmt, unsigned AnnotationBegin, const SourceManager &SourceManager, diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp b/clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/TestingSupportTest.cpp +++ b/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>>, ASTContext &)> Expectations) { - test::checkDataflow( - Code, Target, - [](ASTContext &Context, Environment &) { return NoopAnalysis(Context); }, - std::move(Expectations), {"-fsyntax-only", "-std=c++17"}); + ASSERT_THAT_ERROR( + test::checkDataflow(Code, Target, + [](ASTContext &Context, Environment &) { + return NoopAnalysis(Context); + }, + std::move(Expectations), + {"-fsyntax-only", "-std=c++17"}), + llvm::Succeeded()); } TEST(ProgramPointAnnotations, NoAnnotations) { diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -19,6 +19,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 @@ -40,16 +41,20 @@ template void runDataflow(llvm::StringRef Code, Matcher Match, LangStandard::Kind Std = LangStandard::lang_cxx17) { - test::checkDataflow( - Code, "target", - [](ASTContext &C, Environment &) { return NoopAnalysis(C); }, - [&Match](llvm::ArrayRef< - std::pair>> - Results, - ASTContext &ASTCtx) { Match(Results, ASTCtx); }, - {"-fsyntax-only", - "-std=" + - std::string(LangStandard::getLangStandardForKind(Std).getName())}); + ASSERT_THAT_ERROR( + test::checkDataflow( + Code, "target", + [](ASTContext &C, Environment &) { return NoopAnalysis(C); }, + [&Match]( + llvm::ArrayRef< + std::pair>> + Results, + ASTContext &ASTCtx) { Match(Results, ASTCtx); }, + {"-fsyntax-only", + "-std=" + + std::string( + LangStandard::getLangStandardForKind(Std).getName())}), + llvm::Succeeded()); } }; diff --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp +++ b/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 @@ -196,15 +197,19 @@ }; )")); - test::checkDataflow( - Code, "target", - [](ASTContext &C, Environment &) { return FunctionCallAnalysis(C); }, - [&Expectations]( - llvm::ArrayRef>> - Results, - ASTContext &) { EXPECT_THAT(Results, Expectations); }, - {"-fsyntax-only", "-std=c++17"}, FilesContents); + ASSERT_THAT_ERROR( + test::checkDataflow( + Code, "target", + [](ASTContext &C, Environment &) { + return FunctionCallAnalysis(C); + }, + [&Expectations]( + llvm::ArrayRef>> + Results, + ASTContext &) { EXPECT_THAT(Results, Expectations); }, + {"-fsyntax-only", "-std=c++17"}, FilesContents), + llvm::Succeeded()); } };