diff options
author | Julie Hockett <juliehockett@google.com> | 2017-12-22 16:52:25 +0000 |
---|---|---|
committer | Julie Hockett <juliehockett@google.com> | 2017-12-22 16:52:25 +0000 |
commit | fb4502db623884bf2ba2f354e25e6645b80a705b (patch) | |
tree | 87ac3950dc1d1ac73196247d4f26055bed7bf32a /clang-tidy/fuchsia | |
parent | fd8788844c565fdc8d61bf7499dae656305d4b6e (diff) |
[clang-tidy] Adding Fuchsia checker for overloaded operators
Adds a check to the Fuchsia module to warn if an operator is overloaded,
except move and copy operators.
See https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md for
reference.
Differential Revision: https://reviews.llvm.org/D41363
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@321363 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/fuchsia')
-rw-r--r-- | clang-tidy/fuchsia/CMakeLists.txt | 1 | ||||
-rw-r--r-- | clang-tidy/fuchsia/FuchsiaTidyModule.cpp | 3 | ||||
-rw-r--r-- | clang-tidy/fuchsia/OverloadedOperatorCheck.cpp | 39 | ||||
-rw-r--r-- | clang-tidy/fuchsia/OverloadedOperatorCheck.h | 35 |
4 files changed, 78 insertions, 0 deletions
diff --git a/clang-tidy/fuchsia/CMakeLists.txt b/clang-tidy/fuchsia/CMakeLists.txt index 692a3677..3b223ba7 100644 --- a/clang-tidy/fuchsia/CMakeLists.txt +++ b/clang-tidy/fuchsia/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyFuchsiaModule DefaultArgumentsCheck.cpp FuchsiaTidyModule.cpp + OverloadedOperatorCheck.cpp VirtualInheritanceCheck.cpp LINK_LIBS diff --git a/clang-tidy/fuchsia/FuchsiaTidyModule.cpp b/clang-tidy/fuchsia/FuchsiaTidyModule.cpp index 5ed834d9..1860115a 100644 --- a/clang-tidy/fuchsia/FuchsiaTidyModule.cpp +++ b/clang-tidy/fuchsia/FuchsiaTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "DefaultArgumentsCheck.h" +#include "OverloadedOperatorCheck.h" #include "VirtualInheritanceCheck.h" using namespace clang::ast_matchers; @@ -25,6 +26,8 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<DefaultArgumentsCheck>( "fuchsia-default-arguments"); + CheckFactories.registerCheck<OverloadedOperatorCheck>( + "fuchsia-overloaded-operator"); CheckFactories.registerCheck<VirtualInheritanceCheck>( "fuchsia-virtual-inheritance"); } diff --git a/clang-tidy/fuchsia/OverloadedOperatorCheck.cpp b/clang-tidy/fuchsia/OverloadedOperatorCheck.cpp new file mode 100644 index 00000000..57b4628c --- /dev/null +++ b/clang-tidy/fuchsia/OverloadedOperatorCheck.cpp @@ -0,0 +1,39 @@ +//===--- OverloadedOperatorCheck.cpp - clang-tidy--------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "OverloadedOperatorCheck.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace fuchsia { + +AST_MATCHER(FunctionDecl, isFuchsiaOverloadedOperator) { + if (const auto *CXXMethodNode = dyn_cast<CXXMethodDecl>(&Node)) { + if (CXXMethodNode->isCopyAssignmentOperator() || + CXXMethodNode->isMoveAssignmentOperator()) + return false; + } + return Node.isOverloadedOperator(); +} + +void OverloadedOperatorCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(functionDecl(isFuchsiaOverloadedOperator()).bind("decl"), + this); +} + +void OverloadedOperatorCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *D = Result.Nodes.getNodeAs<FunctionDecl>("decl")) + diag(D->getLocStart(), "cannot overload %0") << D; +} + +} // namespace fuchsia +} // namespace tidy +} // namespace clang diff --git a/clang-tidy/fuchsia/OverloadedOperatorCheck.h b/clang-tidy/fuchsia/OverloadedOperatorCheck.h new file mode 100644 index 00000000..a22e5ae5 --- /dev/null +++ b/clang-tidy/fuchsia/OverloadedOperatorCheck.h @@ -0,0 +1,35 @@ +//===--- OverloadedOperatorCheck.h - clang-tidy------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_OVERLOADED_OPERATOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_OVERLOADED_OPERATOR_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace fuchsia { + +/// Overloading operators is disallowed by the Fuchsia coding standard. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-overloaded-operator.html +class OverloadedOperatorCheck : public ClangTidyCheck { +public: + OverloadedOperatorCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace fuchsia +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_OVERLOADED_OPERATOR_H |