brintos

brintos / llvm-project-archived public Read only

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