clang 20.0.0git
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
18#include "clang/Analysis/CFG.h"
25#include "llvm/ADT/SmallVector.h"
26
27namespace clang {
28namespace dataflow {
29
30// FIXME: Explore using an allowlist-approach, where constructs supported by the
31// analysis are always enabled and additional constructs are enabled through the
32// `Options`.
34 /// In generating diagnostics, ignore optionals reachable through overloaded
35 /// `operator*` or `operator->` (other than those of the optional type
36 /// itself). The analysis does not equate the results of such calls, so it
37 /// can't identify when their results are used safely (across calls),
38 /// resulting in false positives in all such cases. Note: this option does not
39 /// cover access through `operator[]`.
40 /// FIXME: we currently cache and equate the result of const accessors
41 /// returning pointers, so cover the case of operator-> followed by
42 /// operator->, which covers the common case of smart pointers. We also cover
43 /// some limited cases of returning references (if return type is an optional
44 /// type), so cover some cases of operator* followed by operator*. We don't
45 /// cover mixing operator-> and operator*. Once we are confident in this const
46 /// accessor caching, we shouldn't need the IgnoreSmartPointerDereference
47 /// option anymore.
49};
50
52
53/// Dataflow analysis that models whether optionals hold values or not.
54///
55/// Models the `std::optional`, `absl::optional`, and `base::Optional` types.
57 : public DataflowAnalysis<UncheckedOptionalAccessModel,
58 UncheckedOptionalAccessLattice> {
59public:
61
62 /// Returns a matcher for the optional classes covered by this model.
64
66
69
70private:
72 TransferMatchSwitch;
73};
74
76public:
79
83 &State) {
84 return DiagnoseMatchSwitch(Elt, Ctx, State.Env);
85 }
86
87private:
89 DiagnoseMatchSwitch;
90};
91
92} // namespace dataflow
93} // namespace clang
94
95#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
Defines the clang::ASTContext interface.
const Environment & Env
Definition: HTMLLogger.cpp:147
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:188
Represents a top-level expression in a basic block.
Definition: CFG.h:55
A mixin for a lattice that additionally maintains a cache of stable method call return values to mode...
Base class template for dataflow analyses built on a single lattice type.
Holds the state of the program (store and heap) at a given program point.
llvm::SmallVector< SourceLocation > operator()(const CFGElement &Elt, ASTContext &Ctx, const TransferStateForDiagnostics< UncheckedOptionalAccessLattice > &State)
Dataflow analysis that models whether optionals hold values or not.
void transfer(const CFGElement &Elt, UncheckedOptionalAccessLattice &L, Environment &Env)
static UncheckedOptionalAccessLattice initialElement()
static ast_matchers::DeclarationMatcher optionalClassDecl()
Returns a matcher for the optional classes covered by this model.
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
Definition: ASTMatchers.h:143
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
The JSON file list parser is used to communicate input to InstallAPI.
A read-only version of TransferState.
Definition: MatchSwitch.h:55
bool IgnoreSmartPointerDereference
In generating diagnostics, ignore optionals reachable through overloaded operator* or operator-> (oth...