Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 5 | #include "nullability/test/check_diagnostics.h" |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 6 | |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 7 | #include <iterator> |
| 8 | #include <vector> |
| 9 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 10 | #include "nullability/pointer_nullability_analysis.h" |
| 11 | #include "nullability/pointer_nullability_diagnosis.h" |
Martin Brænne | 8687b60 | 2023-11-13 05:53:17 -0800 | [diff] [blame^] | 12 | #include "nullability/test/headers_for_test.h" |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 13 | #include "clang/Analysis/CFG.h" |
| 14 | #include "third_party/llvm/llvm-project/clang/unittests/Analysis/FlowSensitive/TestingSupport.h" |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 16 | #include "llvm/Testing/Support/Error.h" |
| 17 | #include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h" |
| 18 | |
Sam McCall | 4f6be42 | 2023-06-27 02:51:22 -0700 | [diff] [blame] | 19 | namespace clang::tidy::nullability { |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 20 | |
| 21 | bool checkDiagnostics(llvm::StringRef SourceCode) { |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 22 | std::vector<PointerNullabilityDiagnostic> Diagnostics; |
| 23 | PointerNullabilityDiagnoser Diagnoser = pointerNullabilityDiagnoser(); |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 24 | bool Failed = false; |
| 25 | EXPECT_THAT_ERROR( |
| 26 | dataflow::test::checkDataflow<PointerNullabilityAnalysis>( |
| 27 | dataflow::test::AnalysisInputs<PointerNullabilityAnalysis>( |
| 28 | SourceCode, ast_matchers::hasName("target"), |
| 29 | [](ASTContext &ASTCtx, dataflow::Environment &) { |
| 30 | return PointerNullabilityAnalysis(ASTCtx); |
| 31 | }) |
| 32 | .withPostVisitCFG([&Diagnostics, &Diagnoser]( |
| 33 | ASTContext &Ctx, const CFGElement &Elt, |
| 34 | const dataflow::TransferStateForDiagnostics< |
| 35 | PointerNullabilityLattice> &State) { |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 36 | auto EltDiagnostics = Diagnoser(Elt, Ctx, State); |
| 37 | llvm::move(EltDiagnostics, std::back_inserter(Diagnostics)); |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 38 | }) |
Martin Brænne | 8687b60 | 2023-11-13 05:53:17 -0800 | [diff] [blame^] | 39 | .withASTBuildVirtualMappedFiles(headersForTest()) |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 40 | .withASTBuildArgs({"-fsyntax-only", "-std=c++17", |
Martin Brænne | c49da98 | 2023-04-06 06:48:49 -0700 | [diff] [blame] | 41 | "-Wno-unused-value", "-Wno-nonnull", |
Martin Brænne | 66bc243 | 2023-04-18 23:48:49 -0700 | [diff] [blame] | 42 | "-include", "preamble.h", "-I."}), |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 43 | [&Diagnostics, &Failed]( |
| 44 | const llvm::DenseMap<unsigned, std::string> &Annotations, |
| 45 | const dataflow::test::AnalysisOutputs &AnalysisData) { |
| 46 | // Note: use sorted sets for expected and actual lines to improve |
| 47 | // readability of the error output in case the test fails. |
| 48 | std::set<unsigned> ExpectedLines, ActualLines; |
| 49 | for (const auto &[Line, _] : Annotations) { |
| 50 | ExpectedLines.insert(Line); |
| 51 | } |
| 52 | auto &SrcMgr = AnalysisData.ASTCtx.getSourceManager(); |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 53 | for (auto Diag : Diagnostics) |
| 54 | ActualLines.insert( |
| 55 | SrcMgr.getPresumedLineNumber(Diag.Range.getBegin())); |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 56 | EXPECT_THAT(ActualLines, testing::ContainerEq(ExpectedLines)); |
Googler | 3d9adbe | 2023-07-20 12:41:23 -0700 | [diff] [blame] | 57 | if (ActualLines != ExpectedLines) Failed = true; |
Martin Brænne | 7585273 | 2023-03-27 23:49:41 -0700 | [diff] [blame] | 58 | }), |
| 59 | llvm::Succeeded()); |
| 60 | return !Failed; |
| 61 | } |
| 62 | |
Sam McCall | 4f6be42 | 2023-06-27 02:51:22 -0700 | [diff] [blame] | 63 | } // namespace clang::tidy::nullability |