Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion template/docs/CodingStyle.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
62 changes: 61 additions & 1 deletion template/tools/lib/tidy_module/no_friend_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>` 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<clang::TemplateSpecializationType>()) {
auto const* const pattern{
llvm::dyn_cast_or_null<clang::ClassTemplateDecl>(
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::TemplateSpecializationType>()};
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<clang::CXXRecordDecl>(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<clang::TemplateSpecializationType>()
->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) {
Expand Down Expand Up @@ -57,7 +117,7 @@ void NoFriendCheck::check(
return;
}
}
if (IsOwnTypeParameter(*friend_decl)) {
if (IsOwnTypeParameter(*friend_decl) || IsOwnCrtpBase(*friend_decl)) {
return;
}
diag(friend_decl->getFriendLoc(),
Expand Down
51 changes: 51 additions & 0 deletions tests/tidy_module/no_friend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Self>
class Scaled {
public:
int Twice() const { return 2 * static_cast<Self const&>(*this).Value(); }
};

class Meters : public Scaled<Meters> {
friend Scaled<Meters>;

int Value() const { return 3; }
};

// In a class template: through an alias of the base, and by the injected name.
template <typename Unit>
class Length : public Scaled<Length<Unit>> {
using Base = Scaled<Length<Unit>>;
friend Base;

int Value() const { return 1; }
};

template <typename Unit>
class Width : public Scaled<Width<Unit>> {
friend Scaled<Width>;

int Value() const { return 1; }
};

int Total() {
return Meters{}.Twice() + Length<int>{}.Twice() + Width<int>{}.Twice();
}

// A base of another class's, or no base at all, is not the class's own.
class Feet : public Scaled<Meters> {
friend Scaled<Meters>; // house: friend
};

class Yards {
friend Scaled<Yards>; // house: friend
};

// A base that is no CRTP base.
class Plain {};

class Child : public Plain {
friend Plain; // house: friend
};

} // namespace crtp

namespace reported {
Expand Down
Loading