clang
24.0.0git
include
clang
Analysis
FlowSensitive
Models
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
17
#include "
clang/AST/ASTContext.h
"
18
#include "
clang/ASTMatchers/ASTMatchers.h
"
19
#include "
clang/Analysis/CFG.h
"
20
#include "
clang/Analysis/FlowSensitive/CFGMatchSwitch.h
"
21
#include "
clang/Analysis/FlowSensitive/DataflowAnalysis.h
"
22
#include "
clang/Analysis/FlowSensitive/DataflowEnvironment.h
"
23
#include "
clang/Analysis/FlowSensitive/NoopLattice.h
"
24
#include "
clang/Basic/SourceLocation.h
"
25
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/ADT/StringRef.h"
27
28
namespace
clang
{
29
namespace
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).
35
class
UncheckedExpectedAccessModel
36
:
public
DataflowAnalysis
<UncheckedExpectedAccessModel, NoopLattice> {
37
public
:
38
UncheckedExpectedAccessModel
(
ASTContext
&Ctx,
Environment
&Env);
39
40
/// Returns a matcher for calls to member functions of a supported expected
41
/// type.
42
static
ast_matchers::StatementMatcher
callToExpectedClass
();
43
44
static
NoopLattice
initialElement
() {
return
{}; }
45
46
void
transfer
(
const
CFGElement
&Elt,
NoopLattice
&L,
Environment
&Env);
47
48
private
:
49
CFGMatchSwitch<TransferState<NoopLattice>
> TransferMatchSwitch;
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.
56
enum class
UncheckedExpectedAccessKind
:
int
{
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.
64
struct
UncheckedExpectedAccessDiagnostic
{
65
SourceLocation
Loc
;
66
UncheckedExpectedAccessKind
Kind
;
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).
73
class
UncheckedExpectedAccessDiagnoser
{
74
public
:
75
UncheckedExpectedAccessDiagnoser
();
76
77
llvm::SmallVector<UncheckedExpectedAccessDiagnostic>
78
operator()
(
const
CFGElement
&Elt,
ASTContext
&Ctx,
79
const
TransferStateForDiagnostics<NoopLattice>
&State) {
80
return
DiagnoseMatchSwitch(Elt, Ctx, State.Env);
81
}
82
83
private
:
84
CFGMatchSwitch
<
const
Environment
,
85
llvm::SmallVector<UncheckedExpectedAccessDiagnostic>
>
86
DiagnoseMatchSwitch;
87
};
88
89
}
// namespace dataflow
90
}
// namespace clang
91
92
#endif
// CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDEXPECTEDACCESSMODEL_H
ASTContext.h
Defines the clang::ASTContext interface.
ASTMatchers.h
CFGMatchSwitch.h
CFG.h
DataflowAnalysis.h
DataflowEnvironment.h
NoopLattice.h
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition
ASTContext.h:245
clang::CFGElement
Represents a top-level expression in a basic block.
Definition
CFG.h:55
clang::SourceLocation
Encodes a location in the source.
Definition
SourceLocation.h:98
clang::dataflow::DataflowAnalysis< UncheckedExpectedAccessModel, NoopLattice >::DataflowAnalysis
DataflowAnalysis(ASTContext &Context)
Definition
DataflowAnalysis.h:85
clang::dataflow::Environment
Holds the state of the program (store and heap) at a given program point.
Definition
DataflowEnvironment.h:67
clang::dataflow::NoopLattice
Trivial lattice for dataflow analysis with exactly one element.
Definition
NoopLattice.h:27
clang::dataflow::UncheckedExpectedAccessDiagnoser::operator()
llvm::SmallVector< UncheckedExpectedAccessDiagnostic > operator()(const CFGElement &Elt, ASTContext &Ctx, const TransferStateForDiagnostics< NoopLattice > &State)
Definition
UncheckedExpectedAccessModel.h:78
clang::dataflow::UncheckedExpectedAccessDiagnoser::UncheckedExpectedAccessDiagnoser
UncheckedExpectedAccessDiagnoser()
Definition
UncheckedExpectedAccessModel.cpp:199
clang::dataflow::UncheckedExpectedAccessModel::initialElement
static NoopLattice initialElement()
Definition
UncheckedExpectedAccessModel.h:44
clang::dataflow::UncheckedExpectedAccessModel::UncheckedExpectedAccessModel
UncheckedExpectedAccessModel(ASTContext &Ctx, Environment &Env)
Definition
UncheckedExpectedAccessModel.cpp:181
clang::dataflow::UncheckedExpectedAccessModel::callToExpectedClass
static ast_matchers::StatementMatcher callToExpectedClass()
Returns a matcher for calls to member functions of a supported expected type.
Definition
UncheckedExpectedAccessModel.cpp:177
clang::dataflow::UncheckedExpectedAccessModel::transfer
void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env)
Definition
UncheckedExpectedAccessModel.cpp:193
clang::dataflow::Value
Base class for all values computed by abstract interpretation.
Definition
Value.h:33
llvm::SmallVector
Definition
LLVM.h:34
clang::ast_matchers::StatementMatcher
internal::Matcher< Stmt > StatementMatcher
Definition
ASTMatchers.h:147
clang::dataflow
Definition
AdornedCFG.h:29
clang::dataflow::UncheckedExpectedAccessKind
UncheckedExpectedAccessKind
The part of an expected object that is accessed.
Definition
UncheckedExpectedAccessModel.h:56
clang::dataflow::UncheckedExpectedAccessKind::Error
@ Error
error(), which requires an error.
Definition
UncheckedExpectedAccessModel.h:60
clang::dataflow::CFGMatchSwitch
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
Definition
CFGMatchSwitch.h:33
clang
Top level wrappers for InstallAPI frontend operations.
Definition
CalledOnceCheck.h:17
clang::dataflow::TransferStateForDiagnostics
A read-only version of TransferState.
Definition
MatchSwitch.h:55
clang::dataflow::UncheckedExpectedAccessDiagnostic
Diagnostic information for an unchecked expected access.
Definition
UncheckedExpectedAccessModel.h:64
clang::dataflow::UncheckedExpectedAccessDiagnostic::Kind
UncheckedExpectedAccessKind Kind
Definition
UncheckedExpectedAccessModel.h:66
clang::dataflow::UncheckedExpectedAccessDiagnostic::Loc
SourceLocation Loc
Definition
UncheckedExpectedAccessModel.h:65
clang::dataflow::UncheckedExpectedAccessDiagnostic::TypeName
llvm::StringRef TypeName
The accessed type, e.g. "std::expected".
Definition
UncheckedExpectedAccessModel.h:68
Generated on
for clang by
1.14.0