19 if (
const auto *LeftRefType = Left->getAs<ReferenceType>())
20 Left = LeftRefType->getPointeeType();
21 if (
const auto *RightRefType = Right->getAs<ReferenceType>())
22 Right = RightRefType->getPointeeType();
23 return Left->getCanonicalTypeUnqualified() ==
24 Right->getCanonicalTypeUnqualified();
28 const auto Algorithms =
29 hasAnyName(
"::std::find",
"::std::count",
"::std::equal_range",
30 "::std::lower_bound",
"::std::upper_bound");
31 const auto ContainerMatcher = classTemplateSpecializationDecl(hasAnyName(
32 "::std::set",
"::std::map",
"::std::multiset",
"::std::multimap",
33 "::std::unordered_set",
"::std::unordered_map",
34 "::std::unordered_multiset",
"::std::unordered_multimap"));
38 callee(functionDecl(Algorithms)), argumentCountAtLeast(3),
41 callee(cxxMethodDecl(hasName(
"begin"))),
43 hasDeclaration(decl().bind(
"IneffContObj")),
44 anyOf(hasType(ContainerMatcher.bind(
"IneffCont")),
46 ContainerMatcher.bind(
"IneffContPtr")))))
47 .bind(
"IneffContExpr")))),
49 1, cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"end"))),
50 on(declRefExpr(hasDeclaration(
51 equalsBoundNode(
"IneffContObj")))))))
54 Finder->addMatcher(Matcher,
this);
58 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>(
"IneffAlg");
59 const auto *IneffCont =
60 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffCont");
61 bool PtrToContainer =
false;
64 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffContPtr");
65 PtrToContainer =
true;
67 const StringRef IneffContName = IneffCont->getName();
68 const bool Unordered = IneffContName.contains(
"unordered");
69 const bool Maplike = IneffContName.contains(
"map");
73 const QualType ValueType = AlgCall->getArg(2)->getType();
74 const QualType KeyType =
75 IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType();
79 if (AlgCall->getNumArgs() == 4 && !Unordered) {
80 const Expr *Arg = AlgCall->getArg(3);
81 const QualType AlgCmp =
82 Arg->getType().getUnqualifiedType().getCanonicalType();
83 const unsigned CmpPosition = IneffContName.contains(
"map") ? 2 : 1;
84 const QualType ContainerCmp = IneffCont->getTemplateArgs()[CmpPosition]
88 if (AlgCmp != ContainerCmp) {
89 diag(Arg->getBeginLoc(),
90 "different comparers used in the algorithm and the container");
95 const auto *AlgDecl = AlgCall->getDirectCallee();
99 if (Unordered && AlgDecl->getName().contains(
"bound"))
102 const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>(
"IneffContExpr");
105 const SourceManager &SM = *Result.SourceManager;
106 const LangOptions LangOpts = getLangOpts();
108 CharSourceRange CallRange =
109 CharSourceRange::getTokenRange(AlgCall->getSourceRange());
122 if (SM.isMacroArgExpansion(CallRange.getBegin()) &&
123 SM.isMacroArgExpansion(CallRange.getEnd())) {
124 CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin()));
125 CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd()));
128 if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
129 const StringRef ContainerText = Lexer::getSourceText(
130 CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
132 const StringRef ParamText = Lexer::getSourceText(
133 CharSourceRange::getTokenRange(AlgCall->getArg(2)->getSourceRange()),
138 if (!ContainerText.empty() && !ParamText.empty()) {
139 const std::string ReplacementText =
140 (llvm::Twine(ContainerText) + (PtrToContainer ?
"->" :
".") +
141 AlgDecl->getName() +
"(" + ParamText +
")")
143 Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
147 diag(AlgCall->getBeginLoc(),
148 "this STL algorithm call should be replaced with a container method")