1 //===--- NoexceptSwapCheck.cpp - clang-tidy -------------------------------===//
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
12 using 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
17 namespace clang::tidy::performance {
18
registerMatchers(MatchFinder * Finder)19 void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) {
20
21 // Match non-const method with single argument that is non-const reference to
22 // a class type that owns method and return void.
23 // Matches: void Class::swap(Class&)
24 auto MethodMatcher = cxxMethodDecl(
25 parameterCountIs(1U), unless(isConst()), returns(voidType()),
26 hasParameter(0, hasType(qualType(hasCanonicalType(
27 qualType(unless(isConstQualified()),
28 references(namedDecl().bind("class"))))))),
29 ofClass(equalsBoundNode("class")));
30
31 // Match function with 2 arguments, both are non-const references to same type
32 // and return void.
33 // Matches: void swap(Type&, Type&)
34 auto FunctionMatcher = allOf(
35 unless(cxxMethodDecl()), parameterCountIs(2U), returns(voidType()),
36 hasParameter(
37 0, hasType(qualType(hasCanonicalType(
38 qualType(unless(isConstQualified()), references(qualType()))
39 .bind("type"))))),
40 hasParameter(1, hasType(qualType(hasCanonicalType(
41 qualType(equalsBoundNode("type")))))));
42 Finder->addMatcher(functionDecl(unless(isDeleted()),
43 hasAnyName("swap", "iter_swap"),
44 anyOf(MethodMatcher, FunctionMatcher))
45 .bind(BindFuncDeclName),
46 this);
47 }
48
49 DiagnosticBuilder
reportMissingNoexcept(const FunctionDecl * FuncDecl)50 NoexceptSwapCheck::reportMissingNoexcept(const FunctionDecl *FuncDecl) {
51 return diag(FuncDecl->getLocation(), "swap functions should "
52 "be marked noexcept");
53 }
54
reportNoexceptEvaluatedToFalse(const FunctionDecl * FuncDecl,const Expr * NoexceptExpr)55 void NoexceptSwapCheck::reportNoexceptEvaluatedToFalse(
56 const FunctionDecl *FuncDecl, const Expr *NoexceptExpr) {
57 diag(NoexceptExpr->getExprLoc(),
58 "noexcept specifier on swap function evaluates to 'false'");
59 }
60
61 } // namespace clang::tidy::performance
62