summaryrefslogtreecommitdiffstats
path: root/clang-tidy/abseil/FasterStrsplitDelimiterCheck.cpp
blob: 57f822ecbc6e7cad93004432c5679e476e5990b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//===--- FasterStrsplitDelimiterCheck.cpp - clang-tidy---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "FasterStrsplitDelimiterCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace abseil {

namespace {

AST_MATCHER(StringLiteral, lengthIsOne) { return Node.getLength() == 1; }

::internal::Matcher<Expr>
constructExprWithArg(llvm::StringRef ClassName,
                     const ::internal::Matcher<Expr> &Arg) {
  auto ConstrExpr = cxxConstructExpr(hasType(recordDecl(hasName(ClassName))),
                                     hasArgument(0, ignoringParenCasts(Arg)));

  return anyOf(ConstrExpr, cxxBindTemporaryExpr(has(ConstrExpr)));
}

::internal::Matcher<Expr>
copyConstructExprWithArg(llvm::StringRef ClassName,
                         const ::internal::Matcher<Expr> &Arg) {
  return constructExprWithArg(ClassName, constructExprWithArg(ClassName, Arg));
}

llvm::Optional<std::string> makeCharacterLiteral(const StringLiteral *Literal) {
  std::string Result;
  {
    llvm::raw_string_ostream Stream(Result);
    Literal->outputString(Stream);
  }

  // Special case: If the string contains a single quote, we just need to return
  // a character of the single quote. This is a special case because we need to
  // escape it in the character literal.
  if (Result == R"("'")")
    return std::string(R"('\'')");

  assert(Result.size() == 3 || (Result.size() == 4 && Result.substr(0, 2) == "\"\\"));

  // Now replace the " with '.
  auto Pos = Result.find_first_of('"');
  if (Pos == Result.npos)
    return llvm::None;
  Result[Pos] = '\'';
  Pos = Result.find_last_of('"');
  if (Pos == Result.npos)
    return llvm::None;
  Result[Pos] = '\'';
  return Result;
}

} // anonymous namespace

void FasterStrsplitDelimiterCheck::registerMatchers(MatchFinder *Finder) {
  if (!getLangOpts().CPlusPlus)
    return;

  // Binds to one character string literals.
  const auto SingleChar =
      expr(ignoringParenCasts(stringLiteral(lengthIsOne()).bind("Literal")));

  // Binds to a string_view (either absl or std) that was passed by value and
  // contructed from string literal.
  auto StringViewArg =
      copyConstructExprWithArg("::absl::string_view", SingleChar);

  auto ByAnyCharArg =
      expr(copyConstructExprWithArg("::absl::ByAnyChar", StringViewArg))
          .bind("ByAnyChar");

  // Find uses of absl::StrSplit(..., "x") and absl::StrSplit(...,
  // absl::ByAnyChar("x")) to transform them into absl::StrSplit(..., 'x').
  Finder->addMatcher(callExpr(callee(functionDecl(hasName("::absl::StrSplit"))),
                              hasArgument(1, anyOf(ByAnyCharArg, SingleChar)),
                              unless(isInTemplateInstantiation()))
                         .bind("StrSplit"),
                     this);

  // Find uses of absl::MaxSplits("x", N) and
  // absl::MaxSplits(absl::ByAnyChar("x"), N) to transform them into
  // absl::MaxSplits('x', N).
  Finder->addMatcher(
      callExpr(
          callee(functionDecl(hasName("::absl::MaxSplits"))),
          hasArgument(0, anyOf(ByAnyCharArg, ignoringParenCasts(SingleChar))),
          unless(isInTemplateInstantiation())),
      this);
}

void FasterStrsplitDelimiterCheck::check(
    const MatchFinder::MatchResult &Result) {
  const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("Literal");

  if (Literal->getBeginLoc().isMacroID() || Literal->getEndLoc().isMacroID())
    return;

  llvm::Optional<std::string> Replacement = makeCharacterLiteral(Literal);
  if (!Replacement)
    return;
  SourceRange Range = Literal->getSourceRange();

  if (const auto *ByAnyChar = Result.Nodes.getNodeAs<Expr>("ByAnyChar"))
    Range = ByAnyChar->getSourceRange();

  diag(
      Literal->getBeginLoc(),
      "%select{absl::StrSplit()|absl::MaxSplits()}0 called with a string "
      "literal "
      "consisting of a single character; consider using the character overload")
      << (Result.Nodes.getNodeAs<CallExpr>("StrSplit") ? 0 : 1)
      << FixItHint::CreateReplacement(CharSourceRange::getTokenRange(Range),
                                      *Replacement);
}

} // namespace abseil
} // namespace tidy
} // namespace clang