================
@@ -0,0 +1,588 @@
+//===- UncheckedExpectedAccessModelTest.cpp 
-------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.h"
+#include "MockHeaders.h"
+#include "TestingSupport.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Frontend/TextDiagnostic.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Error.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include <string>
+#include <utility>
+#include <vector>
+
+using namespace clang;
+using namespace dataflow;
+using namespace test;
+
+using ::testing::ContainerEq;
+
+/// Replaces all occurrences of `Pattern` in `S` with `Replacement`.
+static void ReplaceAllOccurrences(std::string &S, const std::string &Pattern,
+                                  const std::string &Replacement) {
+  size_t Pos = 0;
+  while (true) {
+    Pos = S.find(Pattern, Pos);
+    if (Pos == std::string::npos)
+      break;
+    S.replace(Pos, Pattern.size(), Replacement);
+  }
+}
+
+struct ExpectedTypeIdentifier {
+  std::string NamespaceName;
+};
+
+static raw_ostream &operator<<(raw_ostream &OS,
+                               const ExpectedTypeIdentifier &TypeId) {
+  OS << TypeId.NamespaceName << "::expected";
+  return OS;
+}
+
+class UncheckedExpectedAccessTest
+    : public ::testing::TestWithParam<ExpectedTypeIdentifier> {
+protected:
+  // Check that after running the analysis on SourceCode, it produces the
+  // expected diagnostics according to [[unsafe]] annotations.
+  // - No annotations => no diagnostics.
+  // - Given "// [[unsafe]]" annotations on a line, we expect a diagnostic for
+  //   an unchecked value access on that line.
+  // - Given "// [[unsafe:error]]" annotations on a line, we expect a 
diagnostic
+  //   for an unchecked error access on that line.
+  void ExpectDiagnosticsFor(std::string SourceCode) {
+    // `tl::expected` is usable since C++17, and `std::expected` since C++23.
+    for (const char *CxxMode : {"-std=c++17", "-std=c++20", "-std=c++23"})
+      ExpectDiagnosticsFor(SourceCode, CxxMode);
+  }
+
+  void ExpectDiagnosticsFor(std::string SourceCode, const char *CxxMode) {
+    ReplaceAllOccurrences(SourceCode, "$ns", GetParam().NamespaceName);
+
+    auto Headers = getMockHeaders();
+    Headers.emplace_back("unchecked_expected_access_test.h", R"(
+      #include "std_expected.h"
+      #include "std_utility.h"
+      #include "tl_expected.h"
+
+      template <typename T>
+      T Make();
+    )");
+    std::vector<UncheckedExpectedAccessDiagnostic> Diagnostics;
+    llvm::Error Error = checkDataflow<UncheckedExpectedAccessModel>(
+        AnalysisInputs<UncheckedExpectedAccessModel>(
+            SourceCode, ast_matchers::hasName("target"),
+            [](ASTContext &Ctx, Environment &Env) {
+              return UncheckedExpectedAccessModel(Ctx, Env);
+            })
+            .withDiagnosisCallbacks(
+                {/*Before=*/[&Diagnostics,
+                             Diagnoser = UncheckedExpectedAccessDiagnoser()](
+                                ASTContext &Ctx, const CFGElement &Elt,
+                                const TransferStateForDiagnostics<NoopLattice>
+                                    &State) mutable {
+                   auto EltDiagnostics = Diagnoser(Elt, Ctx, State);
+                   llvm::move(EltDiagnostics, std::back_inserter(Diagnostics));
+                 },
+                 /*After=*/nullptr})
+            .withASTBuildArgs(
+                {"-fsyntax-only", CxxMode, "-Wno-undefined-inline"})
+            .withASTBuildVirtualMappedFiles(
+                tooling::FileContentMappings(Headers.begin(), Headers.end())),
+        /*VerifyResults=*/[&Diagnostics,
+                           TypeName = GetParam().NamespaceName + "::expected"](
+                              const llvm::DenseMap<unsigned, std::string>
+                                  &Annotations,
+                              const AnalysisOutputs &AO) {
+          llvm::DenseSet<unsigned> AnnotationLines;
+          for (const auto &[Line, _] : Annotations)
+            AnnotationLines.insert(Line);
+          auto &SrcMgr = AO.ASTCtx.getSourceManager();
+          llvm::DenseSet<unsigned> DiagnosticLines;
+          for (const UncheckedExpectedAccessDiagnostic &Diag : Diagnostics) {
+            unsigned Line = SrcMgr.getPresumedLineNumber(Diag.Loc);
+            DiagnosticLines.insert(Line);
+            auto It = Annotations.find(Line);
+            if (It == Annotations.end()) {
+              DiagnosticOptions DiagOpts;
+              TextDiagnostic TD(llvm::errs(), AO.ASTCtx.getLangOpts(),
+                                DiagOpts);
+              TD.emitDiagnostic(FullSourceLoc(Diag.Loc, SrcMgr),
+                                DiagnosticsEngine::Error,
+                                "unexpected diagnostic", {}, {});
+            } else {
+              UncheckedExpectedAccessKind ExpectedKind =
+                  It->second == "unsafe:error"
+                      ? UncheckedExpectedAccessKind::Error
+                      : UncheckedExpectedAccessKind::Value;
+              EXPECT_EQ(Diag.Kind, ExpectedKind) << "on line " << Line;
+              EXPECT_EQ(Diag.TypeName, TypeName) << "on line " << Line;
+            }
+          }
+
+          EXPECT_THAT(DiagnosticLines, ContainerEq(AnnotationLines));
+        });
+    if (Error)
+      FAIL() << llvm::toString(std::move(Error));
+  }
+};
+
+INSTANTIATE_TEST_SUITE_P(
+    UncheckedExpectedUseTestInst, UncheckedExpectedAccessTest,
+    ::testing::Values(ExpectedTypeIdentifier{"std"},
+                      ExpectedTypeIdentifier{"tl"}),
+    [](const ::testing::TestParamInfo<ExpectedTypeIdentifier> &Info) {
+      return Info.param.NamespaceName;
+    });
+
+// Verifies that similarly-named types are ignored.
+TEST_P(UncheckedExpectedAccessTest, NonTrackedExpectedType) {
+  ExpectDiagnosticsFor(
+      R"(
+    namespace other {
+    namespace $ns {
+    template <typename T, typename E>
+    struct expected {
+      T value();
+      E error();
+    };
+    }
+
+    void target($ns::expected<int, int> e) {
+      e.value();
+      e.error();
+    }
+    }
+  )");
+}
+
+// Classes derived from an expected are deliberately not analyzed.
+TEST_P(UncheckedExpectedAccessTest, DerivedClassIsNotAnalyzed) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    struct Derived : $ns::expected<int, int> {};
+
+    void target(Derived d) {
+      d.value();
+      *d;
+      d.error();
+    }
+  )");
+}
+
----------------
BaLiKfromUA wrote:

Yes, I even tested it extensively on a real codebase that used something like 
```cpp
 using MyExpectedT = std::expected<T, int>;
```

But forgot to add the test; fixed in 
https://github.com/llvm/llvm-project/pull/242868/commits/ddced0d176571a7e56f3d52acc89a02fdbe84c9c


https://github.com/llvm/llvm-project/pull/242868
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to