clang-tools 20.0.0git
OverloadedUnaryAndCheck.cpp
Go to the documentation of this file.
1//===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===//
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13
14using namespace clang::ast_matchers;
15
17
19 ast_matchers::MatchFinder *Finder) {
20 // Match unary methods that overload operator&.
21 Finder->addMatcher(
22 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
23 .bind("overload"),
24 this);
25 // Also match freestanding unary operator& overloads. Be careful not to match
26 // binary methods.
27 Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
28 hasOverloadedOperatorName("&"))
29 .bind("overload"),
30 this);
31}
32
33void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
34 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
35 diag(Decl->getBeginLoc(),
36 "do not overload unary operator&, it is dangerous.");
37}
38
39} // namespace clang::tidy::google::runtime
const FunctionDecl * Decl
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.