clang 17.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"
24#include <vector>
25
26namespace clang {
27namespace dataflow {
28
29// FIXME: Explore using an allowlist-approach, where constructs supported by the
30// analysis are always enabled and additional constructs are enabled through the
31// `Options`.
33 /// In generating diagnostics, ignore optionals reachable through overloaded
34 /// `operator*` or `operator->` (other than those of the optional type
35 /// itself). The analysis does not equate the results of such calls, so it
36 /// can't identify when their results are used safely (across calls),
37 /// resulting in false positives in all such cases. Note: this option does not
38 /// cover access through `operator[]`.
40};
41
42/// Dataflow analysis that models whether optionals hold values or not.
43///
44/// Models the `std::optional`, `absl::optional`, and `base::Optional` types.
46 : public DataflowAnalysis<UncheckedOptionalAccessModel, NoopLattice> {
47public:
49
50 /// Returns a matcher for the optional classes covered by this model.
52
53 static NoopLattice initialElement() { return {}; }
54
55 void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env);
56
58 const Environment &Env1, const Value &Val2,
59 const Environment &Env2) override;
60
61 bool merge(QualType Type, const Value &Val1, const Environment &Env1,
62 const Value &Val2, const Environment &Env2, Value &MergedVal,
63 Environment &MergedEnv) override;
64
65 Value *widen(QualType Type, Value &Prev, const Environment &PrevEnv,
66 Value &Current, Environment &CurrentEnv) override;
67
68private:
70};
71
73public:
76
77 std::vector<SourceLocation> diagnose(ASTContext &Ctx, const CFGElement *Elt,
78 const Environment &Env);
79
80private:
82 DiagnoseMatchSwitch;
83};
84
85} // namespace dataflow
86} // namespace clang
87
88#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDOPTIONALACCESSMODEL_H
Defines the clang::ASTContext interface.
const Environment & Env
Definition: HTMLLogger.cpp:170
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:182
Represents a top-level expression in a basic block.
Definition: CFG.h:54
A (possibly-)qualified type.
Definition: Type.h:736
The base class of the type hierarchy.
Definition: Type.h:1568
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.
Trivial lattice for dataflow analysis with exactly one element.
Definition: NoopLattice.h:25
std::vector< SourceLocation > diagnose(ASTContext &Ctx, const CFGElement *Elt, const Environment &Env)
Dataflow analysis that models whether optionals hold values or not.
ComparisonResult compare(QualType Type, const Value &Val1, const Environment &Env1, const Value &Val2, const Environment &Env2) override
Returns: Same: Val1 is equivalent to Val2, according to the model.
bool merge(QualType Type, const Value &Val1, const Environment &Env1, const Value &Val2, const Environment &Env2, Value &MergedVal, Environment &MergedEnv) override
Modifies MergedVal to approximate both Val1 and Val2.
void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env)
Value * widen(QualType Type, Value &Prev, const Environment &PrevEnv, Value &Current, Environment &CurrentEnv) override
This function may widen the current value – replace it with an approximation that can reach a fixed p...
static ast_matchers::DeclarationMatcher optionalClassDecl()
Returns a matcher for the optional classes covered by this model.
Base class for all values computed by abstract interpretation.
Definition: Value.h:33
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
Definition: ASTMatchers.h:142
ComparisonResult
Indicates the result of a tentative comparison.
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
bool IgnoreSmartPointerDereference
In generating diagnostics, ignore optionals reachable through overloaded operator* or operator-> (oth...