From 8c3c0b54b152bdb99f211f8b04b85aa28ba34248 Mon Sep 17 00:00:00 2001 From: virskl Date: Sun, 15 Nov 2026 12:04:38 +0100 Subject: [PATCH 1/2] C-12: a class may befriend its own CRTP base A CRTP base calls hooks of its derived class, which the class keeps private with `friend Base;` so that no one else calls them. Without it the hooks had to be public, which C-12 means to prevent. The friend of a direct base whose class template takes the class itself as an argument passes house-no-friend now: written as the base, through an alias or with the injected name. A base of another class, or a class that is no base, is still reported. The derived side of v0.11.0's `friend Self;`. --- template/docs/CodingStyle.md.jinja | 2 +- .../tools/lib/tidy_module/no_friend_check.cpp | 62 ++++++++++++++++++- tests/tidy_module/no_friend.cpp | 51 +++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/template/docs/CodingStyle.md.jinja b/template/docs/CodingStyle.md.jinja index a23a873..d48f9ce 100644 --- a/template/docs/CodingStyle.md.jinja +++ b/template/docs/CodingStyle.md.jinja @@ -135,7 +135,7 @@ if (elapsed && running) { | C-9 | Data members are `private` and end in `_`. | `readability-identifier-naming` | | C-10 | A function that overrides says `override`, or `final`, and not `virtual` again. | `modernize-use-override` | | C-11 | `mutable` only for what is not part of the state a user sees, as a cache (FN-11). | Review | -| C-12 | No `friend`: a class is reached through its public functions, by its own code as by a test (Testing.md TS-8). Neither a friend class nor a friend function, and no friend operator declared in the class and defined outside it. Two exceptions: an operator defined in the class, a hidden friend (C-15); and the derived class of a CRTP base, its type parameter, `friend Self;`, with the base's constructors private, so that only that class makes one, as `bugprone-crtp-constructor-accessibility` asks. | `house-no-friend` | +| C-12 | No `friend`: a class is reached through its public functions, by its own code as by a test (Testing.md TS-8). Neither a friend class nor a friend function, and no friend operator declared in the class and defined outside it. Three exceptions: an operator defined in the class, a hidden friend (C-15); the derived class of a CRTP base, its type parameter, `friend Self;`, with the base's constructors private, so that only that class makes one, as `bugprone-crtp-constructor-accessibility` asks; and, the other way round, the class's own CRTP base, a direct base with the class among its arguments, `friend Base;`, so that the hooks the base calls stay private to everyone else. | `house-no-friend` | | C-13 | An operator is overloaded only where its meaning is obvious: `Point + Point`, not `Point * Point`. | `misc-unconventional-assign-operator`, Review | | C-14 | A special member that does what the compiler's would is `= default`, not written out: `Sensor() = default;`, not `Sensor() {}`. | `modernize-use-equals-default` | | C-15 | An operator takes one of two forms, by what it does. A **member**: one the language makes a member (`=`, `[]`, `()`, `->`, a conversion), one that changes its operand (`+=` and its like, `++`, `--`), and a unary one (`-x`, `!x`, `~x`, `*x`). A **hidden friend**, a `friend` defined in the class with both operands as its parameters: every other binary operator, `+`, `==`, `<`, `<<`. Both operands convert alike, which a member's left one does not, and the operator is found only through its class, so it neither turns up in the overloads the compiler weighs for every other `operator==` nor in their error messages. It is built on the others where it can: `+` on `+=`, `!=` on `==`, `>`, `<=` and `>=` on `<`. An operator of an `enum`, which can have no friend, is a free function beside it; so is one of a class of another file, which cannot be its friend, as a test's `operator<<` that prints a type of the library. | `house-operator-form` | diff --git a/template/tools/lib/tidy_module/no_friend_check.cpp b/template/tools/lib/tidy_module/no_friend_check.cpp index 4e26355..974aef2 100644 --- a/template/tools/lib/tidy_module/no_friend_check.cpp +++ b/template/tools/lib/tidy_module/no_friend_check.cpp @@ -30,6 +30,66 @@ bool IsOwnTypeParameter(clang::FriendDecl const& friend_decl) { friend_decl.getDeclContext(); } +// The class a type names: a class, the injected name of a class template, or a +// specialization of a class template, by that template's pattern where it +// depends on a parameter, as `Length` in the template `Length`. +clang::CXXRecordDecl const* ClassOf(clang::QualType type) { + clang::CXXRecordDecl const* record{type->getAsCXXRecordDecl()}; + if (record == nullptr) { + if (auto const* specialization = + type->getAs()) { + auto const* const pattern{ + llvm::dyn_cast_or_null( + specialization->getTemplateName().getAsTemplateDecl())}; + record = pattern == nullptr ? nullptr : pattern->getTemplatedDecl(); + } + } + return record == nullptr ? nullptr : record->getCanonicalDecl(); +} + +// The class template a type is a specialization of, as written, by its +// canonical declaration; null for any other type. +clang::Decl const* TemplateOf(clang::QualType type) { + auto const* const specialization{ + type->getAs()}; + clang::TemplateDecl const* const pattern{ + specialization == nullptr + ? nullptr + : specialization->getTemplateName().getAsTemplateDecl()}; + return pattern == nullptr ? nullptr : pattern->getCanonicalDecl(); +} + +// Whether `friend_decl` names the class's own CRTP base: a specialization of +// the class template of one of its direct bases, with the class itself among +// its arguments. The derived side of the pattern IsOwnTypeParameter() lets +// pass: the base calls the hooks the class keeps private to everyone else. +bool IsOwnCrtpBase(clang::FriendDecl const& friend_decl) { + clang::TypeSourceInfo const* const written{friend_decl.getFriendType()}; + auto const* const self{ + llvm::dyn_cast(friend_decl.getDeclContext())}; + if (written == nullptr || self == nullptr || !self->hasDefinition()) { + return false; + } + clang::QualType const friend_type{written->getType()}; + clang::Decl const* const base_template{TemplateOf(friend_type)}; + if (base_template == nullptr) { + return false; + } + bool is_a_base{false}; + for (clang::CXXBaseSpecifier const& base : self->bases()) { + is_a_base = is_a_base || TemplateOf(base.getType()) == base_template; + } + bool names_self{false}; + for (clang::TemplateArgument const& argument : + friend_type->getAs() + ->template_arguments()) { + bool const is_type{argument.getKind() == clang::TemplateArgument::Type}; + names_self = names_self || (is_type && ClassOf(argument.getAsType()) == + self->getCanonicalDecl()); + } + return is_a_base && names_self; +} + } // namespace void NoFriendCheck::registerMatchers(clang::ast_matchers::MatchFinder* finder) { @@ -57,7 +117,7 @@ void NoFriendCheck::check( return; } } - if (IsOwnTypeParameter(*friend_decl)) { + if (IsOwnTypeParameter(*friend_decl) || IsOwnCrtpBase(*friend_decl)) { return; } diag(friend_decl->getFriendLoc(), diff --git a/tests/tidy_module/no_friend.cpp b/tests/tidy_module/no_friend.cpp index e61c889..848cb82 100644 --- a/tests/tidy_module/no_friend.cpp +++ b/tests/tidy_module/no_friend.cpp @@ -68,6 +68,57 @@ class Holder { }; }; +// A derived class befriends its own CRTP base, a direct base whose template +// argument is the class itself: the base reaches the hooks it calls, which stay +// private to everyone else. +template +class Scaled { + public: + int Twice() const { return 2 * static_cast(*this).Value(); } +}; + +class Meters : public Scaled { + friend Scaled; + + int Value() const { return 3; } +}; + +// In a class template: through an alias of the base, and by the injected name. +template +class Length : public Scaled> { + using Base = Scaled>; + friend Base; + + int Value() const { return 1; } +}; + +template +class Width : public Scaled> { + friend Scaled; + + int Value() const { return 1; } +}; + +int Total() { + return Meters{}.Twice() + Length{}.Twice() + Width{}.Twice(); +} + +// A base of another class's, or no base at all, is not the class's own. +class Feet : public Scaled { + friend Scaled; // house: friend +}; + +class Yards { + friend Scaled; // house: friend +}; + +// A base that is no CRTP base. +class Plain {}; + +class Child : public Plain { + friend Plain; // house: friend +}; + } // namespace crtp namespace reported { From b5dcf47a17789a81ab9d8ba5004ea130fc509455 Mon Sep 17 00:00:00 2001 From: virskl Date: Sun, 15 Nov 2026 12:04:38 +0100 Subject: [PATCH 2/2] CHANGELOG: C-12 lets a class befriend its own CRTP base --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95e6f34..4038aaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,19 @@ What changed in each version of the template, and what a project has to do when `copier update` brings it. +## Unreleased + +- **C-12 lets a class befriend its own CRTP base:** a CRTP base calls hooks of its derived class, + which the class keeps private with `friend Base;`, so that a sketch or a test cannot call them; + `house-no-friend` reported that friend, and the hooks had to be public to pass. The friend of a + direct base whose class template takes the class itself as an argument passes now, written as + the base, through an alias or with the injected name; a base of another class, or a class that + is no base, is still reported. The derived side of v0.11.0's `friend Self;`. AtmegaTimer's + modes befriend their core so, and kept `house-no-friend` off for it (AtmegaTimer A-13). + +On the update: a NOLINT(house-no-friend) on a class's `friend` of its own CRTP base can go, and a +project that turned the check off for it turns it on again. + ## v0.11.1 A shorter `make check`: the fuzz build builds the fuzzers alone, the host builds go through