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