blob: 2ff88ad784004f53b739f0ff048f4e85e6d39726 [file] [log] [blame]
Martin Brænne75852732023-03-27 23:49:41 -07001// 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
Googler7f19b2b2023-05-01 09:44:57 -07005#include "nullability/test/check_diagnostics.h"
Martin Brænne75852732023-03-27 23:49:41 -07006
Googler3d9adbe2023-07-20 12:41:23 -07007#include <iterator>
8#include <vector>
9
Googler7f19b2b2023-05-01 09:44:57 -070010#include "nullability/pointer_nullability_analysis.h"
11#include "nullability/pointer_nullability_diagnosis.h"
Martin Brænne8687b602023-11-13 05:53:17 -080012#include "nullability/test/headers_for_test.h"
Martin Brænne75852732023-03-27 23:49:41 -070013#include "clang/Analysis/CFG.h"
14#include "third_party/llvm/llvm-project/clang/unittests/Analysis/FlowSensitive/TestingSupport.h"
Googler3d9adbe2023-07-20 12:41:23 -070015#include "llvm/ADT/STLExtras.h"
Martin Brænne75852732023-03-27 23:49:41 -070016#include "llvm/Testing/Support/Error.h"
17#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
18
Sam McCall4f6be422023-06-27 02:51:22 -070019namespace clang::tidy::nullability {
Martin Brænne75852732023-03-27 23:49:41 -070020
21bool checkDiagnostics(llvm::StringRef SourceCode) {
Googler3d9adbe2023-07-20 12:41:23 -070022 std::vector<PointerNullabilityDiagnostic> Diagnostics;
23 PointerNullabilityDiagnoser Diagnoser = pointerNullabilityDiagnoser();
Martin Brænne75852732023-03-27 23:49:41 -070024 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) {
Googler3d9adbe2023-07-20 12:41:23 -070036 auto EltDiagnostics = Diagnoser(Elt, Ctx, State);
37 llvm::move(EltDiagnostics, std::back_inserter(Diagnostics));
Martin Brænne75852732023-03-27 23:49:41 -070038 })
Martin Brænne8687b602023-11-13 05:53:17 -080039 .withASTBuildVirtualMappedFiles(headersForTest())
Martin Brænne75852732023-03-27 23:49:41 -070040 .withASTBuildArgs({"-fsyntax-only", "-std=c++17",
Martin Brænnec49da982023-04-06 06:48:49 -070041 "-Wno-unused-value", "-Wno-nonnull",
Martin Brænne66bc2432023-04-18 23:48:49 -070042 "-include", "preamble.h", "-I."}),
Martin Brænne75852732023-03-27 23:49:41 -070043 [&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();
Googler3d9adbe2023-07-20 12:41:23 -070053 for (auto Diag : Diagnostics)
54 ActualLines.insert(
55 SrcMgr.getPresumedLineNumber(Diag.Range.getBegin()));
Martin Brænne75852732023-03-27 23:49:41 -070056 EXPECT_THAT(ActualLines, testing::ContainerEq(ExpectedLines));
Googler3d9adbe2023-07-20 12:41:23 -070057 if (ActualLines != ExpectedLines) Failed = true;
Martin Brænne75852732023-03-27 23:49:41 -070058 }),
59 llvm::Succeeded());
60 return !Failed;
61}
62
Sam McCall4f6be422023-06-27 02:51:22 -070063} // namespace clang::tidy::nullability