2014-07-15 20:48:14 +08:00
|
|
|
//===--- 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"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-07-15 20:48:14 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-07-15 20:48:14 +08:00
|
|
|
namespace runtime {
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
void OverloadedUnaryAndCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2015-09-03 00:20:42 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2014-07-15 20:48:14 +08:00
|
|
|
// Match unary methods that overload operator&.
|
2015-09-17 21:31:25 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
|
|
|
|
.bind("overload"),
|
|
|
|
this);
|
2014-07-15 20:48:14 +08:00
|
|
|
// Also match freestanding unary operator& overloads. Be careful not to match
|
|
|
|
// binary methods.
|
|
|
|
Finder->addMatcher(
|
2016-11-08 15:50:19 +08:00
|
|
|
functionDecl(allOf(
|
|
|
|
unless(cxxMethodDecl()),
|
|
|
|
functionDecl(parameterCountIs(1), hasOverloadedOperatorName("&"))
|
|
|
|
.bind("overload"))),
|
2014-07-15 20:48:14 +08:00
|
|
|
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
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-07-15 20:48:14 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|