================
@@ -0,0 +1,202 @@
+//===-- UncheckedExpectedAccessModel.cpp ------------------------*- C++ 
-*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines a dataflow analysis that detects unsafe accesses to the
+//  values of `std::expected` and `tl::expected` objects.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Analysis/FlowSensitive/Arena.h"
+#include "clang/Analysis/FlowSensitive/Formula.h"
+#include "clang/Analysis/FlowSensitive/StorageLocation.h"
+#include "clang/Analysis/FlowSensitive/Value.h"
+
+namespace clang {
+namespace dataflow {
+
+/// Returns true if `DC` is `std` or the top-level `tl` namespace, which holds
+/// `tl::expected` (https://github.com/TartanLlama/expected).
+static bool isSupportedNamespace(const DeclContext &DC) {
+  if (DC.isStdNamespace())
+    return true;
+  const auto *NS = dyn_cast<NamespaceDecl>(&DC);
+  return NS != nullptr && NS->getDeclName().isIdentifier() &&
+         NS->getName() == "tl" && NS->getParent()->isTranslationUnit();
+}
+
+static bool isExpectedClass(const CXXRecordDecl &RD) {
+  return RD.getDeclName().isIdentifier() && RD.getName() == "expected" &&
+         isSupportedNamespace(*RD.getDeclContext());
+}
+
+/// Returns the name of the expected class `RD` to show in diagnostics.
+static llvm::StringRef getExpectedTypeName(const CXXRecordDecl &RD) {
+  return RD.getDeclContext()->isStdNamespace() ? "std::expected"
+                                               : "tl::expected";
+}
+
+// Classes derived from `std::expected` or `tl::expected` are deliberately not
+// supported.
+static bool isExpectedType(QualType Ty) {
+  const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
+  return RD != nullptr && isExpectedClass(*RD);
+}
+
+namespace {
+
+using namespace ::clang::ast_matchers;
+
+using LatticeTransferState = TransferState<NoopLattice>;
+
+AST_MATCHER(CXXRecordDecl, expectedClass) { return isExpectedClass(Node); }
+
+auto expectedMemberCall(ast_matchers::internal::Matcher<NamedDecl> Name) {
+  return cxxMemberCallExpr(
+      callee(cxxMethodDecl(Name, ofClass(expectedClass()))));
+}
+
+auto expectedOperatorCall(ArrayRef<StringRef> Names) {
+  return cxxOperatorCallExpr(hasAnyOverloadedOperatorName(Names),
+                             callee(cxxMethodDecl(ofClass(expectedClass()))));
+}
+
+StorageLocation &locForHasValue(const RecordStorageLocation &ExpectedLoc) {
+  return ExpectedLoc.getSyntheticField("has_value");
+}
+
+/// Returns the symbolic value that represents the "has_value" property of the
+/// expected at `ExpectedLoc`. Returns null if `ExpectedLoc` is null or is not
+/// the location of a `std::expected` or `tl::expected` object (e.g. of a
+/// derived class object).
+BoolValue *getHasValue(Environment &Env, RecordStorageLocation *ExpectedLoc) {
+  if (ExpectedLoc == nullptr || !isExpectedType(ExpectedLoc->getType()))
+    return nullptr;
+  StorageLocation &HasValueLoc = locForHasValue(*ExpectedLoc);
+  auto *HasValueVal = Env.get<BoolValue>(HasValueLoc);
+  if (HasValueVal == nullptr) {
+    HasValueVal = &Env.makeAtomicBoolValue();
+    Env.setValue(HasValueLoc, *HasValueVal);
+  }
+  return HasValueVal;
+}
+
+void transferHasValueCall(const CXXMemberCallExpr *E,
+                          const MatchFinder::MatchResult &,
+                          LatticeTransferState &State) {
+  if (BoolValue *HasValueVal =
+          getHasValue(State.Env, getImplicitObjectLocation(*E, State.Env)))
+    State.Env.setValue(*E, *HasValueVal);
+}
+
+// FIXME: Model the constructors, assignments, `emplace` and `swap`.
+auto buildTransferMatchSwitch() {
+  return CFGMatchSwitchBuilder<LatticeTransferState>()
+      // expected::has_value, expected::operator bool
+      .CaseOfCFGStmt<CXXMemberCallExpr>(
+          expectedMemberCall(hasAnyName("has_value", "operator bool")),
+          transferHasValueCall)
+      .Build();
+}
+
+/// `ExpectedClass` is the class of the called accessor.
+llvm::SmallVector<UncheckedExpectedAccessDiagnostic>
+diagnoseAccess(SourceLocation AccessLoc, UncheckedExpectedAccessKind Kind,
+               const CXXRecordDecl &ExpectedClass,
+               const RecordStorageLocation *ExpectedLoc,
+               const Environment &Env) {
+  if (ExpectedLoc != nullptr) {
+    // Accesses through classes derived from an expected are not modeled.
----------------
BaLiKfromUA wrote:

http://github.com/llvm/llvm-project/pull/242868/commits/be84187858b55edcf92aa0ff127976b42be7fb5a

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