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"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26
28
29// The helper functions exported here are for use of downstream vendor
30// extensions of this model.
31
32// Match declaration of `absl::StatusOr<T>` and bind `T` to "T".
34// Match declaration of `absl::Status`.
36// Match declaration of `absl::internal_statusor::OperatorBase`.
39
40// Get RecordStorageLocation for the `Status` contained in the `StatusOr`
42// Get the StorageLocation for the OK boolean in the `Status`
44// Get the OK boolean in the `Status`, and initialize it if necessary.
46// Get synthetic fields for the types modelled by
47// `UncheckedStatusOrAccessModel`.
48llvm::StringMap<QualType> getSyntheticFields(QualType Ty, QualType StatusType,
49 const CXXRecordDecl &RD);
50
51// Initialize the synthetic fields of the `StatusOr`.
52// N.B. if it is already initialized, the value gets reset.
54 Environment &Env);
55// Initialize the synthetic fields of the `Status`.
56// N.B. if it is already initialized, the value gets reset.
58
59// Return true if `Type` is instantiation of `absl::StatusOr<T>`
61// Return true if `Type` is `absl::Status`
63
64// Get `QualType` for `absl::Status`, or default-constructed
65// QualType if it does not exist.
67
69
70// Dataflow analysis that discovers unsafe uses of StatusOr values.
72 : public DataflowAnalysis<UncheckedStatusOrAccessModel, NoopLattice> {
73public:
75
76 static Lattice initialElement() { return {}; }
77
78 void transfer(const CFGElement &Elt, Lattice &L, Environment &Env);
79
80private:
81 CFGMatchSwitch<TransferState<Lattice>> TransferMatchSwitch;
82};
83
86
87// Extend the Builder with the transfer functions for
88// `UncheckedStatusOrAccessModel`. This is useful to write downstream models
89// that extend the model.
93
108
109} // namespace clang::dataflow::statusor_model
110
111#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::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