[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / clang-tools-extra / clang-tidy / performance / NoexceptSwapCheck.cpp
blobe7cba6e54e86a92ef41efb0b871c89023b8201a8
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 //===----------------------------------------------------------------------===//
9 #include "NoexceptSwapCheck.h"
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 using namespace clang::ast_matchers;
14 // FixItHint - comment added to fix list.rst generation in add_new_check.py.
15 // Do not remove. Fixes are generated in base class.
17 namespace clang::tidy::performance {
19 void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) {
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")));
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);
49 DiagnosticBuilder
50 NoexceptSwapCheck::reportMissingNoexcept(const FunctionDecl *FuncDecl) {
51 return diag(FuncDecl->getLocation(), "swap functions should "
52 "be marked noexcept");
55 void NoexceptSwapCheck::reportNoexceptEvaluatedToFalse(
56 const FunctionDecl *FuncDecl, const Expr *NoexceptExpr) {
57 diag(NoexceptExpr->getExprLoc(),
58 "noexcept specifier on swap function evaluates to 'false'");
61 } // namespace clang::tidy::performance