10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
19 UseTransparentFunctorsCheck::UseTransparentFunctorsCheck(
29 const auto TransparentFunctors =
30 classTemplateSpecializationDecl(
31 unless(hasAnyTemplateArgument(refersToType(voidType()))),
32 hasAnyName(
"::std::plus",
"::std::minus",
"::std::multiplies",
33 "::std::divides",
"::std::modulus",
"::std::negate",
34 "::std::equal_to",
"::std::not_equal_to",
"::std::greater",
35 "::std::less",
"::std::greater_equal",
"::std::less_equal",
36 "::std::logical_and",
"::std::logical_or",
37 "::std::logical_not",
"::std::bit_and",
"::std::bit_or",
38 "::std::bit_xor",
"::std::bit_not"))
39 .bind(
"FunctorClass");
44 unless(elaboratedType()),
45 hasDeclaration(classTemplateSpecializationDecl(
46 unless(hasAnyTemplateArgument(templateArgument(refersToType(
47 qualType(pointsTo(qualType(isAnyCharacter()))))))),
48 hasAnyTemplateArgument(
49 templateArgument(refersToType(qualType(hasDeclaration(
50 TransparentFunctors))))
52 .bind(
"FunctorParentLoc"),
60 Finder->addMatcher(cxxConstructExpr(hasDeclaration(cxxMethodDecl(
61 ofClass(TransparentFunctors))),
62 unless(isInTemplateInstantiation()))
67 static const StringRef
Message =
"prefer transparent functors '%0<>'";
71 while (Result.isNull() && !
Loc.isNull()) {
72 Result =
Loc.getAs<T>();
73 Loc =
Loc.getNextTypeLoc();
79 const MatchFinder::MatchResult &Result) {
80 const auto *FuncClass =
81 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"FunctorClass");
82 if (
const auto *FuncInst =
83 Result.Nodes.getNodeAs<CXXConstructExpr>(
"FuncInst")) {
84 diag(FuncInst->getBeginLoc(),
Message) << FuncClass->getName();
88 const auto *Functor = Result.Nodes.getNodeAs<TemplateArgument>(
"Functor");
89 const auto FunctorParentLoc =
90 Result.Nodes.getNodeAs<TypeLoc>(
"FunctorParentLoc")
91 ->getAs<TemplateSpecializationTypeLoc>();
93 if (!FunctorParentLoc)
97 const auto *FunctorParentType =
98 FunctorParentLoc.getType()->castAs<TemplateSpecializationType>();
99 for (; ArgNum < FunctorParentType->getNumArgs(); ++ArgNum) {
100 const TemplateArgument &Arg = FunctorParentType->getArg(ArgNum);
103 QualType ParentArgType = Arg.getAsType();
104 if (ParentArgType->isRecordType() &&
105 ParentArgType->getAsCXXRecordDecl() ==
106 Functor->getAsType()->getAsCXXRecordDecl())
110 if (ArgNum == FunctorParentType->getNumArgs())
112 TemplateArgumentLoc FunctorLoc = FunctorParentLoc.getArgLoc(ArgNum);
113 auto FunctorTypeLoc = getInnerTypeLocAs<TemplateSpecializationTypeLoc>(
114 FunctorLoc.getTypeSourceInfo()->getTypeLoc());
115 if (FunctorTypeLoc.isNull())
118 SourceLocation ReportLoc = FunctorLoc.getLocation();
119 if (ReportLoc.isInvalid())
122 << FixItHint::CreateRemoval(
123 FunctorTypeLoc.getArgLoc(0).getSourceRange());