forked from OSchip/llvm-project
[clang-tidy] Add a checker that flags all instances of overloaded unary operator&
This handles both methods and freestanding overloads. Differential Revision: http://reviews.llvm.org/D4498 llvm-svn: 213067
This commit is contained in:
parent
cc43c85855
commit
feff134142
|
@ -5,6 +5,7 @@ add_clang_library(clangTidyGoogleModule
|
|||
ExplicitConstructorCheck.cpp
|
||||
ExplicitMakePairCheck.cpp
|
||||
GoogleTidyModule.cpp
|
||||
OverloadedUnaryAndCheck.cpp
|
||||
|
||||
LINK_LIBS
|
||||
clangAST
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "AvoidCStyleCastsCheck.h"
|
||||
#include "ExplicitConstructorCheck.h"
|
||||
#include "ExplicitMakePairCheck.h"
|
||||
#include "OverloadedUnaryAndCheck.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
|
@ -28,6 +29,9 @@ public:
|
|||
CheckFactories.addCheckFactory(
|
||||
"google-explicit-constructor",
|
||||
new ClangTidyCheckFactory<ExplicitConstructorCheck>());
|
||||
CheckFactories.addCheckFactory(
|
||||
"google-runtime-operator",
|
||||
new ClangTidyCheckFactory<runtime::OverloadedUnaryAndCheck>());
|
||||
CheckFactories.addCheckFactory(
|
||||
"google-readability-casting",
|
||||
new ClangTidyCheckFactory<readability::AvoidCStyleCastsCheck>());
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
//===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "OverloadedUnaryAndCheck.h"
|
||||
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||||
#include "clang/ASTMatchers/ASTMatchers.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace runtime {
|
||||
|
||||
void
|
||||
OverloadedUnaryAndCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
|
||||
// Match unary methods that overload operator&.
|
||||
Finder->addMatcher(methodDecl(parameterCountIs(0), hasOverloadedOperatorName(
|
||||
"&")).bind("overload"),
|
||||
this);
|
||||
// Also match freestanding unary operator& overloads. Be careful not to match
|
||||
// binary methods.
|
||||
Finder->addMatcher(
|
||||
functionDecl(
|
||||
allOf(unless(methodDecl()),
|
||||
functionDecl(parameterCountIs(1),
|
||||
hasOverloadedOperatorName("&")).bind("overload"))),
|
||||
this);
|
||||
}
|
||||
|
||||
void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
|
||||
const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
|
||||
diag(Decl->getLocStart(),
|
||||
"do not overload unary operator&, it is dangerous.");
|
||||
}
|
||||
|
||||
} // namespace runtime
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
|
@ -0,0 +1,32 @@
|
|||
//===--- OverloadedUnaryAndCheck.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_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
|
||||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
|
||||
|
||||
#include "../ClangTidy.h"
|
||||
|
||||
namespace clang {
|
||||
namespace tidy {
|
||||
namespace runtime {
|
||||
|
||||
/// \brief Finds overloads of unary operator &.
|
||||
///
|
||||
/// Corresponding cpplint.py check name: 'runtime/operator'.
|
||||
class OverloadedUnaryAndCheck : public ClangTidyCheck {
|
||||
public:
|
||||
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
||||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
||||
};
|
||||
|
||||
} // namespace runtime
|
||||
} // namespace tidy
|
||||
} // namespace clang
|
||||
|
||||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
|
|
@ -0,0 +1,24 @@
|
|||
// RUN: clang-tidy %s -checks='-*,google-runtime-operator' -- | FileCheck %s -implicit-check-not="{{warning|error}}:"
|
||||
|
||||
struct Foo {
|
||||
void *operator&();
|
||||
// CHECK: :[[@LINE-1]]:3: warning: do not overload unary operator&, it is dangerous.
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct TFoo {
|
||||
T *operator&();
|
||||
// CHECK: :[[@LINE-1]]:3: warning: do not overload unary operator&, it is dangerous.
|
||||
};
|
||||
|
||||
TFoo<int> tfoo;
|
||||
|
||||
struct Bar;
|
||||
void *operator&(Bar &b);
|
||||
// CHECK: :[[@LINE-1]]:1: warning: do not overload unary operator&, it is dangerous.
|
||||
|
||||
struct Qux {
|
||||
void *operator&(Qux &q); // no-warning
|
||||
};
|
||||
|
||||
void *operator&(Qux &q, Qux &r); // no-warning
|
Loading…
Reference in New Issue