clang 24.0.0git
UncheckedExpectedAccessModel.h
Go to the documentation of this file.
1//===-- UncheckedExpectedAccessModel.h --------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a dataflow analysis that detects unsafe accesses to the
10// values of `std::expected` and `tl::expected` objects.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDEXPECTEDACCESSMODEL_H
15#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDEXPECTEDACCESSMODEL_H
16
19#include "clang/Analysis/CFG.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringRef.h"
27
28namespace clang {
29namespace dataflow {
30
31/// Dataflow analysis that models whether expected objects hold values.
32///
33/// Supported types are `std::expected` and `tl::expected`
34/// (https://github.com/TartanLlama/expected).
36 : public DataflowAnalysis<UncheckedExpectedAccessModel, NoopLattice> {
37public:
39
40 /// Returns a matcher for calls to member functions of a supported expected
41 /// type.
43
44 static NoopLattice initialElement() { return {}; }
45
46 void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env);
47
48private:
50};
51
52/// The part of an expected object that is accessed.
53///
54/// The values are used as `%select` indices in diagnostics, so they must not
55/// be reordered.
57 /// `value()`, `operator*` or `operator->`, which require a value.
58 Value = 0,
59 /// `error()`, which requires an error.
60 Error = 1,
61};
62
63/// Diagnostic information for an unchecked expected access.
67 /// The accessed type, e.g. "std::expected".
68 llvm::StringRef TypeName;
69};
70
71/// Reports accesses to the value (or error) of expected objects that are not
72/// provably preceded by a check that the object holds a value (or error).
74public:
76
80 return DiagnoseMatchSwitch(Elt, Ctx, State.Env);
81 }
82
83private:
86 DiagnoseMatchSwitch;
87};
88
89} // namespace dataflow
90} // namespace clang
91
92#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDEXPECTEDACCESSMODEL_H
Defines the clang::ASTContext interface.
Defines the clang::SourceLocation class and associated facilities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
Represents a top-level expression in a basic block.
Definition CFG.h:55
Encodes a location in the source.
Holds the state of the program (store and heap) at a given program point.
Trivial lattice for dataflow analysis with exactly one element.
Definition NoopLattice.h:27
llvm::SmallVector< UncheckedExpectedAccessDiagnostic > operator()(const CFGElement &Elt, ASTContext &Ctx, const TransferStateForDiagnostics< NoopLattice > &State)
UncheckedExpectedAccessModel(ASTContext &Ctx, Environment &Env)
static ast_matchers::StatementMatcher callToExpectedClass()
Returns a matcher for calls to member functions of a supported expected type.
void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env)
Base class for all values computed by abstract interpretation.
Definition Value.h:33
internal::Matcher< Stmt > StatementMatcher
UncheckedExpectedAccessKind
The part of an expected object that is accessed.
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
Top level wrappers for InstallAPI frontend operations.
A read-only version of TransferState.
Definition MatchSwitch.h:55
Diagnostic information for an unchecked expected access.
llvm::StringRef TypeName
The accessed type, e.g. "std::expected".