Skip to content

Commit 3f89222

Browse files
committed
[libc++] Clang-tidy operator& hijacker.
Guards against introducing new places where operator& depends on a template type.
1 parent 168177a commit 3f89222

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

libcxx/test/tools/clang_tidy_checks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ set(SOURCES
9595
nodebug_on_aliases.cpp
9696
proper_version_checks.cpp
9797
robust_against_adl.cpp
98+
robust_against_operator_ampersand.cpp
9899
uglify_attributes.cpp
99100

100101
libcpp_module.cpp

libcxx/test/tools/clang_tidy_checks/libcpp_module.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "nodebug_on_aliases.hpp"
1717
#include "proper_version_checks.hpp"
1818
#include "robust_against_adl.hpp"
19+
#include "robust_against_operator_ampersand.hpp"
1920
#include "uglify_attributes.hpp"
2021

2122
namespace {
@@ -29,6 +30,8 @@ class LibcxxTestModule : public clang::tidy::ClangTidyModule {
2930
check_factories.registerCheck<libcpp::nodebug_on_aliases>("libcpp-nodebug-on-aliases");
3031
check_factories.registerCheck<libcpp::proper_version_checks>("libcpp-cpp-version-check");
3132
check_factories.registerCheck<libcpp::robust_against_adl_check>("libcpp-robust-against-adl");
33+
check_factories.registerCheck<libcpp::robust_against_operator_ampersand>(
34+
"libcpp-robust-against-operator-ampersand");
3235
check_factories.registerCheck<libcpp::uglify_attributes>("libcpp-uglify-attributes");
3336
}
3437
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "clang-tidy/ClangTidyCheck.h"
10+
#include "clang-tidy/ClangTidyModuleRegistry.h"
11+
#include "clang/Tooling/FixIt.h"
12+
13+
#include "robust_against_operator_ampersand.hpp"
14+
15+
// This clang-tidy check ensures that we don't use operator& on dependant
16+
// types. If the type is user supplied it may call the type's operator&.
17+
// Instead use std::addressof.
18+
19+
namespace libcpp {
20+
robust_against_operator_ampersand::robust_against_operator_ampersand(
21+
llvm::StringRef name, clang::tidy::ClangTidyContext* context)
22+
: clang::tidy::ClangTidyCheck(name, context), disabled_(!context->getLangOpts().CPlusPlus11) {}
23+
24+
void robust_against_operator_ampersand::registerMatchers(clang::ast_matchers::MatchFinder* finder) {
25+
if (disabled_)
26+
return;
27+
28+
using namespace clang::ast_matchers;
29+
finder->addMatcher(
30+
cxxOperatorCallExpr(allOf(hasOperatorName("&"), argumentCountIs(1), isTypeDependent()),
31+
unless(hasUnaryOperand(dependentScopeDeclRefExpr())))
32+
.bind("match"),
33+
this);
34+
}
35+
36+
void robust_against_operator_ampersand::check(const clang::ast_matchers::MatchFinder::MatchResult& result) {
37+
if (const auto* call = result.Nodes.getNodeAs< clang::CXXOperatorCallExpr >("match"); call != nullptr) {
38+
diag(call->getBeginLoc(), "Guard against user provided operator& for dependent types.")
39+
<< clang::FixItHint::CreateReplacement(
40+
call->getSourceRange(),
41+
(llvm::Twine(
42+
"std::addressof(" + clang::tooling::fixit::getText(*call->getArg(0), *result.Context) + ")"))
43+
.str());
44+
}
45+
}
46+
47+
} // namespace libcpp
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "clang-tidy/ClangTidyCheck.h"
10+
11+
namespace libcpp {
12+
class robust_against_operator_ampersand : public clang::tidy::ClangTidyCheck {
13+
// At the moment libc++ is phasing out development on C++03.
14+
// To avoid testing in C++03, this test is automatically disabled in C++03
15+
// mode. (Doing this from the tests is a lot harder.)
16+
// TODO Remove after dropping C++03 support.
17+
bool disabled_;
18+
19+
public:
20+
robust_against_operator_ampersand(llvm::StringRef, clang::tidy::ClangTidyContext*);
21+
void registerMatchers(clang::ast_matchers::MatchFinder*) override;
22+
void check(const clang::ast_matchers::MatchFinder::MatchResult&) override;
23+
};
24+
} // namespace libcpp

0 commit comments

Comments
 (0)