clang 24.0.0git
GtestModelHelpers.cpp
Go to the documentation of this file.
1//===-- GtestModelHelpers.cpp -----------------------------------*- 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 helpers for handling gtest constructs in dataflow models.
10//
11//===----------------------------------------------------------------------===//
12
13#include "GtestModelHelpers.h"
16#include "llvm/ADT/STLFunctionalExtras.h"
17
18using namespace clang::dataflow::gtest;
19using namespace clang::dataflow;
20using namespace clang;
21
24 llvm::function_ref<StorageLocation &(RecordStorageLocation &)> GetOk) {
26 if (RecordLoc == nullptr)
27 return;
28 RecordStorageLocation *AssertionResultLoc = nullptr;
29 StorageLocation *ExpectedResultLoc = nullptr;
30 for (auto [Field, ChildLoc] : RecordLoc->children()) {
31 if (Field->getName() == "assertion_result")
32 AssertionResultLoc = dyn_cast_or_null<RecordStorageLocation>(ChildLoc);
33 else if (Field->getName() == "expected_result")
34 ExpectedResultLoc = ChildLoc;
35 }
36 if (AssertionResultLoc == nullptr || ExpectedResultLoc == nullptr)
37 return;
38 BoolValue *SuccessVal = Env.get<BoolValue>(GetOk(*AssertionResultLoc));
39 BoolValue *ExpectedVal = Env.get<BoolValue>(*ExpectedResultLoc);
40 if (SuccessVal == nullptr || ExpectedVal == nullptr)
41 return;
42 auto &A = Env.arena();
43 auto &Res = Env.makeAtomicBoolValue();
44 Env.assume(A.makeEquals(Res.formula(), A.makeEquals(SuccessVal->formula(),
45 ExpectedVal->formula())));
46 Env.setValue(*Expr, Res);
47}
48
51 using namespace clang::ast_matchers;
52 return cxxMemberCallExpr(
53 on(expr(unless(cxxThisExpr()))),
54 callee(cxxMethodDecl(
55 hasName("operator bool"),
56 ofClass(hasName("testing::internal::AssertionResultExpectation")))));
57}
llvm::MachO::RecordLoc RecordLoc
Definition MachO.h:41
Represents a call to a member function that may be written either with member call syntax (e....
Definition ExprCXX.h:184
This represents one expression.
Definition Expr.h:113
Models a boolean.
Definition Value.h:94
const Formula & formula() const
Definition Value.h:107
Holds the state of the program (store and heap) at a given program point.
BoolValue & makeAtomicBoolValue() const
Returns an atomic boolean value.
void assume(const Formula &)
Record a fact that must be true if this point in the program is reached.
void setValue(const StorageLocation &Loc, Value &Val)
Assigns Val as the value of Loc in the environment.
std::enable_if_t< std::is_base_of_v< StorageLocation, T >, T * > get(const ValueDecl &D) const
Returns the result of casting getStorageLocation(...) to a subclass of StorageLocation (using cast_or...
A storage location for a record (struct, class, or union).
Base class for elements of the local variable store and of the heap.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
internal::Matcher< Stmt > StatementMatcher
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXThisExpr > cxxThisExpr
Matches implicit and explicit this expressions.
void transferAssertionResultExpectationOperatorBoolCall(const CXXMemberCallExpr *Expr, Environment &Env, llvm::function_ref< StorageLocation &(RecordStorageLocation &)> GetOk)
clang::ast_matchers::StatementMatcher isAssertionResultExpectationOperatorBoolCall()
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...
Top level wrappers for InstallAPI frontend operations.