| 1 | //===--- UseTransparentFunctorsCheck.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 "UseTransparentFunctorsCheck.h" |
| 10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::modernize { |
| 15 | |
| 16 | UseTransparentFunctorsCheck::UseTransparentFunctorsCheck( |
| 17 | StringRef Name, ClangTidyContext *Context) |
| 18 | : ClangTidyCheck(Name, Context), SafeMode(Options.get(LocalName: "SafeMode" , Default: false)) {} |
| 19 | |
| 20 | void UseTransparentFunctorsCheck::storeOptions( |
| 21 | ClangTidyOptions::OptionMap &Opts) { |
| 22 | Options.store(Options&: Opts, LocalName: "SafeMode" , Value: SafeMode); |
| 23 | } |
| 24 | |
| 25 | void UseTransparentFunctorsCheck::registerMatchers(MatchFinder *Finder) { |
| 26 | const auto TransparentFunctors = |
| 27 | classTemplateSpecializationDecl( |
| 28 | unless(hasAnyTemplateArgument(InnerMatcher: refersToType(InnerMatcher: voidType()))), |
| 29 | hasAnyName("::std::plus" , "::std::minus" , "::std::multiplies" , |
| 30 | "::std::divides" , "::std::modulus" , "::std::negate" , |
| 31 | "::std::equal_to" , "::std::not_equal_to" , "::std::greater" , |
| 32 | "::std::less" , "::std::greater_equal" , "::std::less_equal" , |
| 33 | "::std::logical_and" , "::std::logical_or" , |
| 34 | "::std::logical_not" , "::std::bit_and" , "::std::bit_or" , |
| 35 | "::std::bit_xor" , "::std::bit_not" )) |
| 36 | .bind(ID: "FunctorClass" ); |
| 37 | |
| 38 | // Non-transparent functor mentioned as a template parameter. FIXIT. |
| 39 | Finder->addMatcher( |
| 40 | NodeMatch: loc(InnerMatcher: qualType( |
| 41 | unless(elaboratedType()), |
| 42 | hasDeclaration(InnerMatcher: classTemplateSpecializationDecl( |
| 43 | unless(hasAnyTemplateArgument(InnerMatcher: templateArgument(refersToType( |
| 44 | InnerMatcher: qualType(pointsTo(InnerMatcher: qualType(isAnyCharacter()))))))), |
| 45 | hasAnyTemplateArgument( |
| 46 | InnerMatcher: templateArgument(refersToType(InnerMatcher: qualType(hasDeclaration( |
| 47 | InnerMatcher: TransparentFunctors)))) |
| 48 | .bind(ID: "Functor" )))))) |
| 49 | .bind(ID: "FunctorParentLoc" ), |
| 50 | Action: this); |
| 51 | |
| 52 | if (SafeMode) |
| 53 | return; |
| 54 | |
| 55 | // Non-transparent functor constructed. No FIXIT. There is no easy way |
| 56 | // to rule out the problematic char* vs string case. |
| 57 | Finder->addMatcher(NodeMatch: cxxConstructExpr(hasDeclaration(InnerMatcher: cxxMethodDecl( |
| 58 | ofClass(InnerMatcher: TransparentFunctors))), |
| 59 | unless(isInTemplateInstantiation())) |
| 60 | .bind(ID: "FuncInst" ), |
| 61 | Action: this); |
| 62 | } |
| 63 | |
| 64 | static const StringRef Message = "prefer transparent functors '%0<>'" ; |
| 65 | |
| 66 | template <typename T> static T getInnerTypeLocAs(TypeLoc Loc) { |
| 67 | T Result; |
| 68 | while (Result.isNull() && !Loc.isNull()) { |
| 69 | Result = Loc.getAs<T>(); |
| 70 | Loc = Loc.getNextTypeLoc(); |
| 71 | } |
| 72 | return Result; |
| 73 | } |
| 74 | |
| 75 | void UseTransparentFunctorsCheck::check( |
| 76 | const MatchFinder::MatchResult &Result) { |
| 77 | const auto *FuncClass = |
| 78 | Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(ID: "FunctorClass" ); |
| 79 | if (const auto *FuncInst = |
| 80 | Result.Nodes.getNodeAs<CXXConstructExpr>(ID: "FuncInst" )) { |
| 81 | diag(Loc: FuncInst->getBeginLoc(), Description: Message) << FuncClass->getName(); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const auto *Functor = Result.Nodes.getNodeAs<TemplateArgument>(ID: "Functor" ); |
| 86 | const auto FunctorParentLoc = |
| 87 | Result.Nodes.getNodeAs<TypeLoc>(ID: "FunctorParentLoc" ) |
| 88 | ->getAs<TemplateSpecializationTypeLoc>(); |
| 89 | |
| 90 | if (!FunctorParentLoc) |
| 91 | return; |
| 92 | |
| 93 | unsigned ArgNum = 0; |
| 94 | const auto *FunctorParentType = |
| 95 | FunctorParentLoc.getType()->castAs<TemplateSpecializationType>(); |
| 96 | for (; ArgNum < FunctorParentType->template_arguments().size(); ++ArgNum) { |
| 97 | const TemplateArgument &Arg = |
| 98 | FunctorParentType->template_arguments()[ArgNum]; |
| 99 | if (Arg.getKind() != TemplateArgument::Type) |
| 100 | continue; |
| 101 | QualType ParentArgType = Arg.getAsType(); |
| 102 | if (ParentArgType->isRecordType() && |
| 103 | ParentArgType->getAsCXXRecordDecl() == |
| 104 | Functor->getAsType()->getAsCXXRecordDecl()) |
| 105 | break; |
| 106 | } |
| 107 | // Functor is a default template argument. |
| 108 | if (ArgNum == FunctorParentType->template_arguments().size()) |
| 109 | return; |
| 110 | TemplateArgumentLoc FunctorLoc = FunctorParentLoc.getArgLoc(i: ArgNum); |
| 111 | auto FunctorTypeLoc = getInnerTypeLocAs<TemplateSpecializationTypeLoc>( |
| 112 | Loc: FunctorLoc.getTypeSourceInfo()->getTypeLoc()); |
| 113 | if (FunctorTypeLoc.isNull()) |
| 114 | return; |
| 115 | |
| 116 | SourceLocation ReportLoc = FunctorLoc.getLocation(); |
| 117 | if (ReportLoc.isInvalid()) |
| 118 | return; |
| 119 | diag(Loc: ReportLoc, Description: Message) << FuncClass->getName() |
| 120 | << FixItHint::CreateRemoval( |
| 121 | RemoveRange: FunctorTypeLoc.getArgLoc(i: 0).getSourceRange()); |
| 122 | } |
| 123 | |
| 124 | } // namespace clang::tidy::modernize |
| 125 | |