clang
23.0.0git
include
clang
Analysis
FlowSensitive
Models
UncheckedOptionalAccessModel.h
Go to the documentation of this file.
1
//===-- UncheckedOptionalAccessModel.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 uses of optional
10
// values.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
15
#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
16
17
#include "
clang/AST/ASTContext.h
"
18
#include "
clang/Analysis/CFG.h
"
19
#include "
clang/Analysis/FlowSensitive/CFGMatchSwitch.h
"
20
#include "
clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h
"
21
#include "
clang/Analysis/FlowSensitive/DataflowAnalysis.h
"
22
#include "
clang/Analysis/FlowSensitive/DataflowEnvironment.h
"
23
#include "
clang/Analysis/FlowSensitive/MatchSwitch.h
"
24
#include "
clang/Analysis/FlowSensitive/NoopLattice.h
"
25
#include "
clang/Basic/SourceLocation.h
"
26
#include "llvm/ADT/SmallVector.h"
27
28
namespace
clang
{
29
namespace
dataflow
{
30
31
// FIXME: Explore using an allowlist-approach, where constructs supported by the
32
// analysis are always enabled and additional constructs are enabled through the
33
// `Options`.
34
struct
UncheckedOptionalAccessModelOptions
{
35
/// In generating diagnostics, ignore optionals reachable through overloaded
36
/// `operator*` or `operator->` (other than those of the optional type
37
/// itself). The analysis does not equate the results of such calls, so it
38
/// can't identify when their results are used safely (across calls),
39
/// resulting in false positives in all such cases. Note: this option does not
40
/// cover access through `operator[]`.
41
///
42
/// FIXME: we now cache and equate the result of const accessors
43
/// that look like unique_ptr, have both `->` (returning a pointer type) and
44
/// `*` (returning a reference type). This includes mixing `->` and
45
/// `*` in a sequence of calls as long as the object is not modified. Once we
46
/// are confident in this const accessor caching, we shouldn't need the
47
/// IgnoreSmartPointerDereference option anymore.
48
bool
IgnoreSmartPointerDereference
=
false
;
49
50
/// In generating diagnostics, ignore calls to `optional::value()`.
51
bool
IgnoreValueCalls
=
false
;
52
};
53
54
using
UncheckedOptionalAccessLattice
=
CachedConstAccessorsLattice<NoopLattice>
;
55
56
/// Dataflow analysis that models whether optionals hold values or not.
57
///
58
/// Models the `std::optional`, `absl::optional`, and `base::Optional` types.
59
class
UncheckedOptionalAccessModel
60
:
public
DataflowAnalysis
<UncheckedOptionalAccessModel,
61
UncheckedOptionalAccessLattice> {
62
public
:
63
UncheckedOptionalAccessModel
(
ASTContext
&Ctx,
dataflow::Environment
&Env);
64
65
/// Returns a matcher for calls to optional classes diagnosed by this model.
66
static
ast_matchers::StatementMatcher
memberCallToOptionalClass
();
67
static
ast_matchers::StatementMatcher
operatorCallToOptionalClass
();
68
69
static
UncheckedOptionalAccessLattice
initialElement
() {
return
{}; }
70
71
void
transfer
(
const
CFGElement
&Elt,
UncheckedOptionalAccessLattice
&L,
72
Environment
&Env);
73
74
private
:
75
CFGMatchSwitch<TransferState<UncheckedOptionalAccessLattice>
>
76
TransferMatchSwitch;
77
};
78
79
/// Diagnostic information for an unchecked optional access.
80
struct
UncheckedOptionalAccessDiagnostic
{
81
CharSourceRange
Range
;
82
};
83
84
class
UncheckedOptionalAccessDiagnoser
{
85
public
:
86
UncheckedOptionalAccessDiagnoser
(
87
UncheckedOptionalAccessModelOptions
Options = {});
88
89
llvm::SmallVector<UncheckedOptionalAccessDiagnostic>
90
operator()
(
const
CFGElement
&Elt,
ASTContext
&Ctx,
91
const
TransferStateForDiagnostics<UncheckedOptionalAccessLattice>
92
&State) {
93
return
DiagnoseMatchSwitch(Elt, Ctx, State.Env);
94
}
95
96
private
:
97
CFGMatchSwitch
<
const
Environment
,
98
llvm::SmallVector<UncheckedOptionalAccessDiagnostic>
>
99
DiagnoseMatchSwitch;
100
};
101
102
}
// namespace dataflow
103
}
// namespace clang
104
105
#endif
// CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
ASTContext.h
Defines the clang::ASTContext interface.
CFGMatchSwitch.h
CFG.h
CachedConstAccessorsLattice.h
DataflowAnalysis.h
DataflowEnvironment.h
MatchSwitch.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:227
clang::CFGElement
Represents a top-level expression in a basic block.
Definition
CFG.h:55
clang::CharSourceRange
Represents a byte-granular source range.
Definition
SourceLocation.h:276
clang::dataflow::CachedConstAccessorsLattice
A mixin for a lattice that additionally maintains a cache of stable method call return values to mode...
Definition
CachedConstAccessorsLattice.h:50
clang::dataflow::DataflowAnalysis< UncheckedOptionalAccessModel, UncheckedOptionalAccessLattice >::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:65
clang::dataflow::UncheckedOptionalAccessDiagnoser::UncheckedOptionalAccessDiagnoser
UncheckedOptionalAccessDiagnoser(UncheckedOptionalAccessModelOptions Options={})
Definition
UncheckedOptionalAccessModel.cpp:1317
clang::dataflow::UncheckedOptionalAccessDiagnoser::operator()
llvm::SmallVector< UncheckedOptionalAccessDiagnostic > operator()(const CFGElement &Elt, ASTContext &Ctx, const TransferStateForDiagnostics< UncheckedOptionalAccessLattice > &State)
Definition
UncheckedOptionalAccessModel.h:90
clang::dataflow::UncheckedOptionalAccessModel::UncheckedOptionalAccessModel
UncheckedOptionalAccessModel(ASTContext &Ctx, dataflow::Environment &Env)
Definition
UncheckedOptionalAccessModel.cpp:1291
clang::dataflow::UncheckedOptionalAccessModel::transfer
void transfer(const CFGElement &Elt, UncheckedOptionalAccessLattice &L, Environment &Env)
Definition
UncheckedOptionalAccessModel.cpp:1310
clang::dataflow::UncheckedOptionalAccessModel::operatorCallToOptionalClass
static ast_matchers::StatementMatcher operatorCallToOptionalClass()
Definition
UncheckedOptionalAccessModel.cpp:1287
clang::dataflow::UncheckedOptionalAccessModel::memberCallToOptionalClass
static ast_matchers::StatementMatcher memberCallToOptionalClass()
Returns a matcher for calls to optional classes diagnosed by this model.
Definition
UncheckedOptionalAccessModel.cpp:1282
clang::dataflow::UncheckedOptionalAccessModel::initialElement
static UncheckedOptionalAccessLattice initialElement()
Definition
UncheckedOptionalAccessModel.h:69
llvm::SmallVector
Definition
LLVM.h:34
clang::ast_matchers::StatementMatcher
internal::Matcher< Stmt > StatementMatcher
Definition
ASTMatchers.h:146
clang::dataflow
Dataflow Directional Tag Classes.
Definition
AdornedCFG.h:29
clang::dataflow::UncheckedOptionalAccessLattice
CachedConstAccessorsLattice< NoopLattice > UncheckedOptionalAccessLattice
Definition
UncheckedOptionalAccessModel.h:54
clang::dataflow::CFGMatchSwitch
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
Definition
CFGMatchSwitch.h:33
clang
The JSON file list parser is used to communicate input to InstallAPI.
Definition
CalledOnceCheck.h:17
clang::dataflow::TransferStateForDiagnostics
A read-only version of TransferState.
Definition
MatchSwitch.h:55
clang::dataflow::UncheckedOptionalAccessDiagnostic
Diagnostic information for an unchecked optional access.
Definition
UncheckedOptionalAccessModel.h:80
clang::dataflow::UncheckedOptionalAccessDiagnostic::Range
CharSourceRange Range
Definition
UncheckedOptionalAccessModel.h:81
clang::dataflow::UncheckedOptionalAccessModelOptions
Definition
UncheckedOptionalAccessModel.h:34
clang::dataflow::UncheckedOptionalAccessModelOptions::IgnoreValueCalls
bool IgnoreValueCalls
In generating diagnostics, ignore calls to optional::value().
Definition
UncheckedOptionalAccessModel.h:51
clang::dataflow::UncheckedOptionalAccessModelOptions::IgnoreSmartPointerDereference
bool IgnoreSmartPointerDereference
In generating diagnostics, ignore optionals reachable through overloaded operator* or operator-> (oth...
Definition
UncheckedOptionalAccessModel.h:48
Generated on
for clang by
1.14.0