Files
RedBear-OS/local/recipes/dev/libclc/source/clang/lib/ASTMatchers/GtestMatchers.cpp
T
vasilito cb424d7448 build: static patch-sanity linter (shift-left the malformed-patch class)
verify-patch-sanity.py validates every active recipe .patch has internally-
consistent hunk line counts — catching the 'malformed patch at line N' failure
at commit/CI/preflight time instead of hours into a cook. This cycle hit that
class three times (qtwaylandscanner, sddm, xwayland), each only discovered when
cookbook tried to apply the patch.

Running it across the repo found 29 latent malformed patches (validated against
GNU patch: e.g. relibc/P3-sysv-ipc reproduces 'malformed patch at line 22').
They were harmless only because they sit in vendored recipes (baked, not re-
applied) — but would fail on any version-bump re-derivation. --fix recounts the
hunk headers (body untouched) and repaired all 29.

Wired into build-preflight.sh (Phase 1.0D) and redbear-ci.yml, with a unit test
(test-patch-sanity.sh). Skips archived/legacy trees and unvalidatable formats
(empty placeholders, bare-@@ git hunks).
2026-08-01 05:13:02 +03:00

229 lines
8.7 KiB
C++

//===- GtestMatchers.cpp - AST Matchers for Gtest ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements several matchers for popular gtest macros. In general,
// AST matchers cannot match calls to macros. However, we can simulate such
// matches if the macro definition has identifiable elements that themselves can
// be matched. In that case, we can match on those elements and then check that
// the match occurs within an expansion of the desired macro. The more uncommon
// the identified elements, the more efficient this process will be.
//
//===----------------------------------------------------------------------===//
#include "clang/ASTMatchers/GtestMatchers.h"
#include "llvm/ADT/StringRef.h"
namespace clang {
namespace ast_matchers {
namespace {
enum class MacroType {
Expect,
Assert,
On,
};
} // namespace
static DeclarationMatcher getComparisonDecl(GtestCmp Cmp) {
switch (Cmp) {
case GtestCmp::Eq:
return cxxMethodDecl(hasName("Compare"),
ofClass(cxxRecordDecl(isSameOrDerivedFrom(
hasName("::testing::internal::EqHelper")))));
case GtestCmp::Ne:
return functionDecl(hasName("::testing::internal::CmpHelperNE"));
case GtestCmp::Ge:
return functionDecl(hasName("::testing::internal::CmpHelperGE"));
case GtestCmp::Gt:
return functionDecl(hasName("::testing::internal::CmpHelperGT"));
case GtestCmp::Le:
return functionDecl(hasName("::testing::internal::CmpHelperLE"));
case GtestCmp::Lt:
return functionDecl(hasName("::testing::internal::CmpHelperLT"));
}
llvm_unreachable("Unhandled GtestCmp enum");
}
static llvm::StringRef getMacroTypeName(MacroType Macro) {
switch (Macro) {
case MacroType::Expect:
return "EXPECT";
case MacroType::Assert:
return "ASSERT";
case MacroType::On:
return "ON";
}
llvm_unreachable("Unhandled MacroType enum");
}
static llvm::StringRef getComparisonTypeName(GtestCmp Cmp) {
switch (Cmp) {
case GtestCmp::Eq:
return "EQ";
case GtestCmp::Ne:
return "NE";
case GtestCmp::Ge:
return "GE";
case GtestCmp::Gt:
return "GT";
case GtestCmp::Le:
return "LE";
case GtestCmp::Lt:
return "LT";
}
llvm_unreachable("Unhandled GtestCmp enum");
}
static std::string getMacroName(MacroType Macro, GtestCmp Cmp) {
return (getMacroTypeName(Macro) + "_" + getComparisonTypeName(Cmp)).str();
}
static std::string getMacroName(MacroType Macro, llvm::StringRef Operation) {
return (getMacroTypeName(Macro) + "_" + Operation).str();
}
// Under the hood, ON_CALL is expanded to a call to `InternalDefaultActionSetAt`
// to set a default action spec to the underlying function mocker, while
// EXPECT_CALL is expanded to a call to `InternalExpectedAt` to set a new
// expectation spec.
static llvm::StringRef getSpecSetterName(MacroType Macro) {
switch (Macro) {
case MacroType::On:
return "InternalDefaultActionSetAt";
case MacroType::Expect:
return "InternalExpectedAt";
default:
llvm_unreachable("Unhandled MacroType enum");
}
llvm_unreachable("Unhandled MacroType enum");
}
// In general, AST matchers cannot match calls to macros. However, we can
// simulate such matches if the macro definition has identifiable elements that
// themselves can be matched. In that case, we can match on those elements and
// then check that the match occurs within an expansion of the desired
// macro. The more uncommon the identified elements, the more efficient this
// process will be.
//
// We use this approach to implement the derived matchers gtestAssert and
// gtestExpect.
static internal::BindableMatcher<Stmt>
gtestComparisonInternal(MacroType Macro, GtestCmp Cmp, StatementMatcher Left,
StatementMatcher Right) {
return callExpr(isExpandedFromMacro(getMacroName(Macro, Cmp)),
callee(getComparisonDecl(Cmp)), hasArgument(2, Left),
hasArgument(3, Right));
}
static internal::BindableMatcher<Stmt>
gtestThatInternal(MacroType Macro, StatementMatcher Actual,
StatementMatcher Matcher) {
return cxxOperatorCallExpr(
isExpandedFromMacro(getMacroName(Macro, "THAT")),
hasOverloadedOperatorName("()"), hasArgument(2, Actual),
hasArgument(
0, expr(hasType(classTemplateSpecializationDecl(hasName(
"::testing::internal::PredicateFormatterFromMatcher"))),
ignoringImplicit(
callExpr(callee(functionDecl(hasName(
"::testing::internal::"
"MakePredicateFormatterFromMatcher"))),
hasArgument(0, ignoringImplicit(Matcher)))))));
}
static internal::BindableMatcher<Stmt>
gtestCallInternal(MacroType Macro, StatementMatcher MockCall, MockArgs Args) {
// A ON_CALL or EXPECT_CALL macro expands to different AST structures
// depending on whether the mock method has arguments or not.
switch (Args) {
// For example,
// `ON_CALL(mock, TwoParamMethod)` is expanded to
// `mock.gmock_TwoArgsMethod(WithoutMatchers(),
// nullptr).InternalDefaultActionSetAt(...)`.
// EXPECT_CALL is the same except
// that it calls `InternalExpectedAt` instead of `InternalDefaultActionSetAt`
// in the end.
case MockArgs::None:
return cxxMemberCallExpr(
isExpandedFromMacro(getMacroName(Macro, "CALL")),
callee(functionDecl(hasName(getSpecSetterName(Macro)))),
onImplicitObjectArgument(ignoringImplicit(MockCall)));
// For example,
// `ON_CALL(mock, TwoParamMethod(m1, m2))` is expanded to
// `mock.gmock_TwoParamMethod(m1,m2)(WithoutMatchers(),
// nullptr).InternalDefaultActionSetAt(...)`.
// EXPECT_CALL is the same except that it calls `InternalExpectedAt` instead
// of `InternalDefaultActionSetAt` in the end.
case MockArgs::Some:
return cxxMemberCallExpr(
isExpandedFromMacro(getMacroName(Macro, "CALL")),
callee(functionDecl(hasName(getSpecSetterName(Macro)))),
onImplicitObjectArgument(ignoringImplicit(cxxOperatorCallExpr(
hasOverloadedOperatorName("()"), argumentCountIs(3),
hasArgument(0, ignoringImplicit(MockCall))))));
}
llvm_unreachable("Unhandled MockArgs enum");
}
static internal::BindableMatcher<Stmt>
gtestCallInternal(MacroType Macro, StatementMatcher MockObject,
llvm::StringRef MockMethodName, MockArgs Args) {
return gtestCallInternal(
Macro,
cxxMemberCallExpr(
onImplicitObjectArgument(MockObject),
callee(functionDecl(hasName(("gmock_" + MockMethodName).str())))),
Args);
}
internal::BindableMatcher<Stmt> gtestAssert(GtestCmp Cmp, StatementMatcher Left,
StatementMatcher Right) {
return gtestComparisonInternal(MacroType::Assert, Cmp, Left, Right);
}
internal::BindableMatcher<Stmt> gtestExpect(GtestCmp Cmp, StatementMatcher Left,
StatementMatcher Right) {
return gtestComparisonInternal(MacroType::Expect, Cmp, Left, Right);
}
internal::BindableMatcher<Stmt> gtestAssertThat(StatementMatcher Actual,
StatementMatcher Matcher) {
return gtestThatInternal(MacroType::Assert, Actual, Matcher);
}
internal::BindableMatcher<Stmt> gtestExpectThat(StatementMatcher Actual,
StatementMatcher Matcher) {
return gtestThatInternal(MacroType::Expect, Actual, Matcher);
}
internal::BindableMatcher<Stmt> gtestOnCall(StatementMatcher MockObject,
llvm::StringRef MockMethodName,
MockArgs Args) {
return gtestCallInternal(MacroType::On, MockObject, MockMethodName, Args);
}
internal::BindableMatcher<Stmt> gtestOnCall(StatementMatcher MockCall,
MockArgs Args) {
return gtestCallInternal(MacroType::On, MockCall, Args);
}
internal::BindableMatcher<Stmt> gtestExpectCall(StatementMatcher MockObject,
llvm::StringRef MockMethodName,
MockArgs Args) {
return gtestCallInternal(MacroType::Expect, MockObject, MockMethodName, Args);
}
internal::BindableMatcher<Stmt> gtestExpectCall(StatementMatcher MockCall,
MockArgs Args) {
return gtestCallInternal(MacroType::Expect, MockCall, Args);
}
} // end namespace ast_matchers
} // end namespace clang