xref: /llvm-project/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp (revision 8f73c4432b5fa8510c99a5053c07dc70a610e1fb)
1 //===--- StringFindStartswithCheck.cc - clang-tidy---------------*- C++ -*-===//
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 "StringFindStartswithCheck.h"
10 
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 
16 #include <cassert>
17 
18 using namespace clang::ast_matchers;
19 
20 namespace clang {
21 namespace tidy {
22 namespace abseil {
23 
24 StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,
25                                                      ClangTidyContext *Context)
26     : ClangTidyCheck(Name, Context),
27       StringLikeClasses(utils::options::parseStringList(
28           Options.get("StringLikeClasses", "::std::basic_string"))),
29       IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
30                                             utils::IncludeSorter::getMapping(),
31                                             utils::IncludeSorter::IS_LLVM)),
32       AbseilStringsMatchHeader(
33           Options.get("AbseilStringsMatchHeader", "absl/strings/match.h")) {}
34 
35 void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) {
36   auto ZeroLiteral = integerLiteral(equals(0));
37   auto StringClassMatcher = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
38       StringLikeClasses.begin(), StringLikeClasses.end())));
39   auto StringType = hasUnqualifiedDesugaredType(
40       recordType(hasDeclaration(StringClassMatcher)));
41 
42   auto StringFind = cxxMemberCallExpr(
43       // .find()-call on a string...
44       callee(cxxMethodDecl(hasName("find"))),
45       on(hasType(StringType)),
46       // ... with some search expression ...
47       hasArgument(0, expr().bind("needle")),
48       // ... and either "0" as second argument or the default argument (also 0).
49       anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr())));
50 
51   Finder->addMatcher(
52       // Match [=!]= with a zero on one side and a string.find on the other.
53       binaryOperator(
54           hasAnyOperatorName("==", "!="),
55           hasOperands(ignoringParenImpCasts(ZeroLiteral),
56                       ignoringParenImpCasts(StringFind.bind("findexpr"))))
57           .bind("expr"),
58       this);
59 }
60 
61 void StringFindStartswithCheck::check(const MatchFinder::MatchResult &Result) {
62   const ASTContext &Context = *Result.Context;
63   const SourceManager &Source = Context.getSourceManager();
64 
65   // Extract matching (sub)expressions
66   const auto *ComparisonExpr = Result.Nodes.getNodeAs<BinaryOperator>("expr");
67   assert(ComparisonExpr != nullptr);
68   const auto *Needle = Result.Nodes.getNodeAs<Expr>("needle");
69   assert(Needle != nullptr);
70   const Expr *Haystack = Result.Nodes.getNodeAs<CXXMemberCallExpr>("findexpr")
71                              ->getImplicitObjectArgument();
72   assert(Haystack != nullptr);
73 
74   if (ComparisonExpr->getBeginLoc().isMacroID())
75     return;
76 
77   // Get the source code blocks (as characters) for both the string object
78   // and the search expression
79   const StringRef NeedleExprCode = Lexer::getSourceText(
80       CharSourceRange::getTokenRange(Needle->getSourceRange()), Source,
81       Context.getLangOpts());
82   const StringRef HaystackExprCode = Lexer::getSourceText(
83       CharSourceRange::getTokenRange(Haystack->getSourceRange()), Source,
84       Context.getLangOpts());
85 
86   // Create the StartsWith string, negating if comparison was "!=".
87   bool Neg = ComparisonExpr->getOpcodeStr() == "!=";
88   StringRef StartswithStr;
89   if (Neg) {
90     StartswithStr = "!absl::StartsWith";
91   } else {
92     StartswithStr = "absl::StartsWith";
93   }
94 
95   // Create the warning message and a FixIt hint replacing the original expr.
96   auto Diagnostic =
97       diag(ComparisonExpr->getBeginLoc(),
98            (StringRef("use ") + StartswithStr + " instead of find() " +
99             ComparisonExpr->getOpcodeStr() + " 0")
100                .str());
101 
102   Diagnostic << FixItHint::CreateReplacement(
103       ComparisonExpr->getSourceRange(),
104       (StartswithStr + "(" + HaystackExprCode + ", " + NeedleExprCode + ")")
105           .str());
106 
107   // Create a preprocessor #include FixIt hint (CreateIncludeInsertion checks
108   // whether this already exists).
109   Diagnostic << IncludeInserter->CreateIncludeInsertion(
110       Source.getFileID(ComparisonExpr->getBeginLoc()), AbseilStringsMatchHeader,
111       false);
112 }
113 
114 void StringFindStartswithCheck::registerPPCallbacks(
115     const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
116   IncludeInserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
117                                                               IncludeStyle);
118   PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
119 }
120 
121 void StringFindStartswithCheck::storeOptions(
122     ClangTidyOptions::OptionMap &Opts) {
123   Options.store(Opts, "StringLikeClasses",
124                 utils::options::serializeStringList(StringLikeClasses));
125   Options.store(Opts, "IncludeStyle", IncludeStyle,
126                 utils::IncludeSorter::getMapping());
127   Options.store(Opts, "AbseilStringsMatchHeader", AbseilStringsMatchHeader);
128 }
129 
130 } // namespace abseil
131 } // namespace tidy
132 } // namespace clang
133