clang-tools 22.0.0git
NoexceptSwapCheck.cpp
Go to the documentation of this file.
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 "NoexceptSwapCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
14// FixItHint - comment added to fix list.rst generation in add_new_check.py.
15// Do not remove. Fixes are generated in base class.
16
18
19void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) {
20 // Match non-const method with single argument that is non-const reference to
21 // a class type that owns method and return void.
22 // Matches: void Class::swap(Class&)
23 auto MethodMatcher = cxxMethodDecl(
24 parameterCountIs(1U), unless(isConst()), returns(voidType()),
25 hasParameter(0, hasType(qualType(hasCanonicalType(
26 qualType(unless(isConstQualified()),
27 references(namedDecl().bind("class"))))))),
28 ofClass(equalsBoundNode("class")));
29
30 // Match function with 2 arguments, both are non-const references to same type
31 // and return void.
32 // Matches: void swap(Type&, Type&)
33 auto FunctionMatcher = allOf(
34 unless(cxxMethodDecl()), parameterCountIs(2U), returns(voidType()),
35 hasParameter(
36 0, hasType(qualType(hasCanonicalType(
37 qualType(unless(isConstQualified()), references(qualType()))
38 .bind("type"))))),
39 hasParameter(1, hasType(qualType(hasCanonicalType(
40 qualType(equalsBoundNode("type")))))));
41 Finder->addMatcher(functionDecl(unless(isDeleted()),
42 hasAnyName("swap", "iter_swap"),
43 anyOf(MethodMatcher, FunctionMatcher))
44 .bind(BindFuncDeclName),
45 this);
46}
47
48DiagnosticBuilder
49NoexceptSwapCheck::reportMissingNoexcept(const FunctionDecl *FuncDecl) {
50 return diag(FuncDecl->getLocation(), "swap functions should "
51 "be marked noexcept");
52}
53
54void NoexceptSwapCheck::reportNoexceptEvaluatedToFalse(
55 const FunctionDecl *FuncDecl, const Expr *NoexceptExpr) {
56 diag(NoexceptExpr->getExprLoc(),
57 "noexcept specifier on swap function evaluates to 'false'");
58}
59
60} // namespace clang::tidy::performance
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static constexpr const char FuncDecl[]