Skip to content

Commit

Permalink
Fix daedalean-friend-declarations check
Browse files Browse the repository at this point in the history
Use the right argument to check if the friend is an overloaded
operator. Remove `#if 0` that prevented the test from passing.

Tested with:
./build/bin/llvm-lit -v clang-tools-extra/test/clang-tidy/checkers/daedalean-friend-declarations.cpp
  • Loading branch information
Javier-varez committed Jul 12, 2022
1 parent 8050e3c commit 03bc5bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ void FriendDeclarationsCheck::registerMatchers(MatchFinder *Finder) {
void FriendDeclarationsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<FriendDecl>("x");

const auto decl = MatchedDecl->getFriendDecl();
if (!decl) {
diag(MatchedDecl->getLocation(), "Unresolved friend declaration");
const NamedDecl *decl = MatchedDecl->getFriendDecl();
if (decl == nullptr) {
// In this case assume worst-case and simply don't allow the friend
// declaration since we just don't have enough information about it
diag(MatchedDecl->getLocation(), "Friend declaration must not be used");
return;
}

if (MatchedDecl->isFunctionOrFunctionTemplate()) {
if (decl->isFunctionOrFunctionTemplate()) {
if (const auto method = llvm::dyn_cast_or_null<FunctionDecl>(decl);
method) {
if (method->isOverloadedOperator()) {
Expand Down Expand Up @@ -59,7 +61,8 @@ void FriendDeclarationsCheck::check(const MatchFinder::MatchResult &Result) {
if (decl->isTemplated()) {
const auto ctx = MatchedDecl->getLexicalDeclContext();
if (const auto cls = llvm::dyn_cast_or_null<const CXXRecordDecl>(ctx);
cls && (cls->isTemplated() || llvm::isa<ClassTemplateSpecializationDecl>(cls)) ) {
cls && (cls->isTemplated() ||
llvm::isa<ClassTemplateSpecializationDecl>(cls))) {
if (const auto friendCls =
llvm::dyn_cast_or_null<ClassTemplateDecl>(decl);
friendCls) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// RUN: %check_clang_tidy %s daedalean-friend-declarations %t
#if 0

class S {};

class B {};

class A {
friend class B;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: Friend declaration must not be used [daedalean-friend-declarations]
friend void foo();
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: Friend declaration must not be used [daedalean-friend-declarations]
friend S& operator << (S&, const A&);
friend S &operator<<(S &, const A &);
};

#endif
template<typename T>
template <typename T>
class C {

template<typename U>
template <typename U>
friend class C;
};

C<int> bar() {
return {};
}
}

0 comments on commit 03bc5bb

Please sign in to comment.