clang 22.0.0git
UncheckedStatusOrAccessModel.h
Go to the documentation of this file.
1//===- UncheckedStatusOrAccessModel.h -------------------------------------===//
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#ifndef CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H
10#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H
11
12#include "clang/AST/Type.h"
14#include "clang/Analysis/CFG.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
27
29
30// The helper functions exported here are for use of downstream vendor
31// extensions of this model.
32
33// Match declaration of `absl::StatusOr<T>` and bind `T` to "T".
35// Match declaration of `absl::Status`.
37// Match declaration of `absl::internal_statusor::OperatorBase`.
41
42// Get RecordStorageLocation for the `Status` contained in the `StatusOr`
44// Get the StorageLocation for the OK boolean in the `Status`
46// Get the OK boolean in the `Status`, and initialize it if necessary.
48// Get synthetic fields for the types modelled by
49// `UncheckedStatusOrAccessModel`.
50llvm::StringMap<QualType> getSyntheticFields(QualType Ty, QualType StatusType,
51 const CXXRecordDecl &RD);
52
53// Initialize the synthetic fields of the `StatusOr`.
54// N.B. if it is already initialized, the value gets reset.
56 Environment &Env);
57// Initialize the synthetic fields of the `Status`.
58// N.B. if it is already initialized, the value gets reset.
60
61// Return true if `Type` is instantiation of `absl::StatusOr<T>`
63// Return true if `Type` is `absl::Status`
65
66// Get `QualType` for `absl::Status`, or default-constructed
67// QualType if it does not exist.
69
71
72// Dataflow analysis that discovers unsafe uses of StatusOr values.
74 : public DataflowAnalysis<UncheckedStatusOrAccessModel,
75 CachedConstAccessorsLattice<NoopLattice>> {
76public:
78
79 static Lattice initialElement() { return {}; }
80
81 void transfer(const CFGElement &Elt, Lattice &L, Environment &Env);
82
83private:
84 CFGMatchSwitch<TransferState<Lattice>> TransferMatchSwitch;
85};
86
89
90// Extend the Builder with the transfer functions for
91// `UncheckedStatusOrAccessModel`. This is useful to write downstream models
92// that extend the model.
96
111
112} // namespace clang::dataflow::statusor_model
113
114#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
Represents a top-level expression in a basic block.
Definition CFG.h:55
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
A (possibly-)qualified type.
Definition TypeBase.h:937
The base class of the type hierarchy.
Definition TypeBase.h:1833
Models a boolean.
Definition Value.h:94
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
Holds the state of the program (store and heap) at a given program point.
A storage location for a record (struct, class, or union).
Base class for elements of the local variable store and of the heap.
UncheckedStatusOrAccessDiagnoser(UncheckedStatusOrAccessModelOptions Options={})
llvm::SmallVector< SourceLocation > operator()(const CFGElement &Elt, ASTContext &Ctx, const TransferStateForDiagnostics< UncheckedStatusOrAccessModel::Lattice > &State)
void transfer(const CFGElement &Elt, Lattice &L, Environment &Env)
internal::Matcher< QualType > TypeMatcher
internal::Matcher< Decl > DeclarationMatcher
Types of matchers for the top-level classes in the AST class hierarchy.
TransferState< UncheckedStatusOrAccessModel::Lattice > LatticeTransferState
RecordStorageLocation & locForStatus(RecordStorageLocation &StatusOrLoc)
clang::ast_matchers::TypeMatcher statusType()
clang::ast_matchers::DeclarationMatcher statusOrClass()
clang::ast_matchers::TypeMatcher statusOrType()
clang::ast_matchers::DeclarationMatcher statusClass()
BoolValue & initializeStatusOr(RecordStorageLocation &StatusOrLoc, Environment &Env)
QualType findStatusType(const ASTContext &Ctx)
BoolValue & initializeStatus(RecordStorageLocation &StatusLoc, Environment &Env)
llvm::StringMap< QualType > getSyntheticFields(QualType Ty, QualType StatusType, const CXXRecordDecl &RD)
StorageLocation & locForOk(RecordStorageLocation &StatusLoc)
BoolValue & valForOk(RecordStorageLocation &StatusLoc, Environment &Env)
CFGMatchSwitch< LatticeTransferState > buildTransferMatchSwitch(ASTContext &Ctx, CFGMatchSwitchBuilder< LatticeTransferState > Builder)
clang::ast_matchers::DeclarationMatcher statusOrOperatorBaseClass()
std::function< Result(const CFGElement &, ASTContext &, State &)> CFGMatchSwitch
A read-only version of TransferState.
Definition MatchSwitch.h:55
A common form of state shared between the cases of a transfer function.
Definition MatchSwitch.h:41