31 const auto *NS = dyn_cast<NamespaceDecl>(&DC);
32 return NS !=
nullptr && NS->getDeclName().isIdentifier() &&
33 NS->getName() ==
"tl" && NS->getParent()->isTranslationUnit();
58using LatticeTransferState = TransferState<NoopLattice>;
62auto expectedMemberCall(ast_matchers::internal::Matcher<NamedDecl> Name) {
67auto expectedOperatorCall(ArrayRef<StringRef> Names) {
73 return ExpectedLoc.getSyntheticField(
"has_value");
81 if (ExpectedLoc ==
nullptr || !
isExpectedType(ExpectedLoc->getType()))
84 auto *HasValueVal = Env.get<
BoolValue>(HasValueLoc);
85 if (HasValueVal ==
nullptr) {
86 HasValueVal = &Env.makeAtomicBoolValue();
87 Env.setValue(HasValueLoc, *HasValueVal);
92void transferHasValueCall(
const CXXMemberCallExpr *E,
93 const MatchFinder::MatchResult &,
94 LatticeTransferState &State) {
97 State.Env.setValue(*E, *HasValueVal);
105 expectedMemberCall(
hasAnyName(
"has_value",
"operator bool")),
106 transferHasValueCall)
111llvm::SmallVector<UncheckedExpectedAccessDiagnostic>
116 if (ExpectedLoc !=
nullptr) {
121 if (
auto *HasValueVal = Env.get<
BoolValue>(locForHasValue(*ExpectedLoc))) {
125 ? HasValueVal->formula()
126 : Env.arena().makeNot(HasValueVal->formula());
127 if (Env.proves(Safe))
135auto buildDiagnoseMatchSwitch() {
138 llvm::SmallVector<UncheckedExpectedAccessDiagnostic>>()
140 .CaseOfCFGStmt<CXXMemberCallExpr>(
141 expectedMemberCall(
hasName(
"value")),
142 [](
const CXXMemberCallExpr *E,
const MatchFinder::MatchResult &,
144 return diagnoseAccess(E->getExprLoc(),
146 *E->getMethodDecl()->getParent(),
150 .CaseOfCFGStmt<CXXOperatorCallExpr>(
151 expectedOperatorCall({
"*",
"->"}),
152 [](
const CXXOperatorCallExpr *E,
const MatchFinder::MatchResult &,
156 return diagnoseAccess(
162 .CaseOfCFGStmt<CXXMemberCallExpr>(
163 expectedMemberCall(
hasName(
"error")),
164 [](
const CXXMemberCallExpr *E,
const MatchFinder::MatchResult &,
166 return diagnoseAccess(E->getExprLoc(),
168 *E->getMethodDecl()->getParent(),
184 TransferMatchSwitch(buildTransferMatchSwitch()) {
186 [&Ctx](
QualType Ty) -> llvm::StringMap<QualType> {
189 return {{
"has_value", Ctx.
BoolTy}};
195 LatticeTransferState State(L, Env);
200 : DiagnoseMatchSwitch(buildDiagnoseMatchSwitch()) {}
#define AST_MATCHER(Type, DefineMatcher)
AST_MATCHER(Type, DefineMatcher) { ... } defines a zero parameter function named DefineMatcher() that...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Represents a top-level expression in a basic block.
Represents a C++ struct/union/class.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
bool isStdNamespace() const
DeclContext * getDeclContext()
bool isIdentifier() const
Predicate functions for querying what type of name this is.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
A (possibly-)qualified type.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Collects cases of a "match switch": a collection of matchers paired with callbacks,...
CFGMatchSwitchBuilder && CaseOfCFGStmt(MatchSwitchMatcher< Stmt > M, MatchSwitchAction< NodeT, State, Result > A) &&
Registers an action A for CFGStmts that will be triggered by the match of the pattern M against the S...
ASTContext & getASTContext() final
DataflowAnalysis(ASTContext &Context)
Holds the state of the program (store and heap) at a given program point.
DataflowAnalysisContext & getDataflowAnalysisContext() const
Returns the DataflowAnalysisContext used by the environment.
Trivial lattice for dataflow analysis with exactly one element.
A storage location for a record (struct, class, or union).
Base class for elements of the local variable store and of the heap.
UncheckedExpectedAccessDiagnoser()
UncheckedExpectedAccessModel(ASTContext &Ctx, Environment &Env)
static ast_matchers::StatementMatcher callToExpectedClass()
Returns a matcher for calls to member functions of a supported expected type.
void transfer(const CFGElement &Elt, NoopLattice &L, Environment &Env)
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
const internal::VariadicFunction< internal::PolymorphicMatcher< internal::HasOverloadedOperatorNameMatcher, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl), std::vector< std::string > >, StringRef, internal::hasAnyOverloadedOperatorNameFunc > hasAnyOverloadedOperatorName
Matches overloaded operator names.
const internal::VariadicFunction< internal::Matcher< NamedDecl >, StringRef, internal::hasAnyNameFunc > hasAnyName
Matches NamedDecl nodes that have any of the specified names.
internal::Matcher< Stmt > StatementMatcher
CFGMatchSwitch< LatticeTransferState > buildTransferMatchSwitch(ASTContext &Ctx, CFGMatchSwitchBuilder< LatticeTransferState > Builder)
static bool isSupportedNamespace(const DeclContext &DC)
Returns true if DC is std or the top-level tl namespace, which holds tl::expected (https://github....
static bool isExpectedType(QualType Ty)
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
UncheckedExpectedAccessKind
The part of an expected object that is accessed.
@ Value
value(), operator* or operator->, which require a value.
@ Error
error(), which requires an error.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
static llvm::StringRef getExpectedTypeName(const CXXRecordDecl &RD)
Returns the name of the expected class RD to show in diagnostics.
static bool isExpectedClass(const CXXRecordDecl &RD)
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
RecordStorageLocation * getImplicitObjectLocation(const CXXMemberCallExpr &MCE, const Environment &Env)
Returns the storage location for the implicit object of a CXXMemberCallExpr, or null if none is defin...
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
Top level wrappers for InstallAPI frontend operations.
U cast(CodeGen::Address addr)