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
4 changes: 4 additions & 0 deletions libcxx/test/libcxx/clang_tidy.gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# ===----------------------------------------------------------------------===##


# Run our custom libc++ clang-tidy checks on all public headers.

# RUN: %{python} %s %{libcxx-dir}/utils
Expand All @@ -23,6 +24,9 @@

// REQUIRES: has-clang-tidy

// The frozen headers should not be updated to the latest libc++ style, so don't test.
// UNSUPPORTED: FROZEN-CXX03-HEADERS-FIXME

// The GCC compiler flags are not always compatible with clang-tidy.
// UNSUPPORTED: gcc

Expand Down
1 change: 1 addition & 0 deletions libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ set(SOURCES
nodebug_on_aliases.cpp
proper_version_checks.cpp
robust_against_adl.cpp
robust_against_operator_ampersand.cpp
uglify_attributes.cpp

libcpp_module.cpp
Expand Down
3 changes: 3 additions & 0 deletions libcxx/test/tools/clang_tidy_checks/libcpp_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nodebug_on_aliases.hpp"
#include "proper_version_checks.hpp"
#include "robust_against_adl.hpp"
#include "robust_against_operator_ampersand.hpp"
#include "uglify_attributes.hpp"

namespace {
Expand All @@ -29,6 +30,8 @@ class LibcxxTestModule : public clang::tidy::ClangTidyModule {
check_factories.registerCheck<libcpp::nodebug_on_aliases>("libcpp-nodebug-on-aliases");
check_factories.registerCheck<libcpp::proper_version_checks>("libcpp-cpp-version-check");
check_factories.registerCheck<libcpp::robust_against_adl_check>("libcpp-robust-against-adl");
check_factories.registerCheck<libcpp::robust_against_operator_ampersand>(
"libcpp-robust-against-operator-ampersand");
check_factories.registerCheck<libcpp::uglify_attributes>("libcpp-uglify-attributes");
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "clang-tidy/ClangTidyCheck.h"
#include "clang-tidy/ClangTidyModuleRegistry.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/FixIt.h"

#include "robust_against_operator_ampersand.hpp"

// This clang-tidy check ensures that we don't use operator& on dependant
// types. If the type is user supplied it may call the type's operator&.
// Instead use std::addressof.
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into the coding guidelines instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. The code should documented what the intention is.

As for the coding guidelines; it already has this information documented.

(Note testing for operator, in the same part of the documentation is on my todo list.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @mordante here, this is documented explicitly in the coding guidelines already:

Function overloading also applies to operators. Using &user_object may call a user-defined operator&. Use std::addressof instead. Similarly, to avoid invoking a user-defined operator,, make sure to cast the result to void when using the , or avoid it in the first place. For example:

IMO this comment doesn't provide absolutely necessary information, but it also doesn't hurt. I'd keep it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guidelines should still document that this check exists. IMO this comment isn't useful, since it will get out of sync with the guidelines. I'd be much happier with a reference to the guidelines than documenting the same thing in different places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest the following, then:

Suggested change
// This clang-tidy check ensures that we don't use operator& on dependant
// types. If the type is user supplied it may call the type's operator&.
// Instead use std::addressof.
// This clang-tidy check ensures that we don't use operator& on dependant
// types, as documented in our coding guidelines.

I am mostly neutral on this issue. It's true that we're duplicating a bit of information, but we're really not duplicating a lot of it. I'd let @mordante decide here to avoid blocking this whole patch on such a minor point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's useful still, but I wouldn't block it on this. However, I would block on not mentioning the check in the documentation as we do with all other checks.

//
// This is part of libc++'s policy
// https://libcxx.llvm.org/CodingGuidelines.html#don-t-use-argument-dependent-lookup-unless-required-by-the-standard

// TODO(LLVM-21) Remove dependentScopeDeclRefExpr
// dependentScopeDeclRefExpr requires Clang 20, this uses the same definition as Clang
#if defined(__clang_major__) && __clang_major__ < 20
namespace clang::ast_matchers {
const internal::VariadicDynCastAllOfMatcher<Stmt, DependentScopeDeclRefExpr> dependentScopeDeclRefExpr;
} // namespace clang::ast_matchers
#endif

namespace libcpp {
robust_against_operator_ampersand::robust_against_operator_ampersand(
llvm::StringRef name, clang::tidy::ClangTidyContext* context)
: clang::tidy::ClangTidyCheck(name, context) {}

void robust_against_operator_ampersand::registerMatchers(clang::ast_matchers::MatchFinder* finder) {
using namespace clang::ast_matchers;
finder->addMatcher(
cxxOperatorCallExpr(allOf(hasOperatorName("&"), argumentCountIs(1), isTypeDependent()),
unless(hasUnaryOperand(dependentScopeDeclRefExpr())))
.bind("match"),
this);
}

void robust_against_operator_ampersand::check(const clang::ast_matchers::MatchFinder::MatchResult& result) {
if (const auto* call = result.Nodes.getNodeAs< clang::CXXOperatorCallExpr >("match"); call != nullptr) {
diag(call->getBeginLoc(), "Guard against user provided operator& for dependent types.")
<< clang::FixItHint::CreateReplacement(
call->getSourceRange(),
(llvm::Twine(
"std::addressof(" + clang::tooling::fixit::getText(*call->getArg(0), *result.Context) + ")"))
.str());
}
}

} // namespace libcpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "clang-tidy/ClangTidyCheck.h"

namespace libcpp {
class robust_against_operator_ampersand : public clang::tidy::ClangTidyCheck {
public:
robust_against_operator_ampersand(llvm::StringRef, clang::tidy::ClangTidyContext*);
void registerMatchers(clang::ast_matchers::MatchFinder*) override;
void check(const clang::ast_matchers::MatchFinder::MatchResult&) override;
};
} // namespace libcpp
Loading