UseTransparentFunctorsCheck.cpp 4.8 KB

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