reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
//===--- UseToStringCheck.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 "UseToStringCheck.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace boost {

namespace {
AST_MATCHER(Type, isStrictlyInteger) {
  return Node.isIntegerType() && !Node.isAnyCharacterType() &&
         !Node.isBooleanType();
}
} // namespace

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

  Finder->addMatcher(
      callExpr(
          hasDeclaration(functionDecl(
              returns(hasDeclaration(classTemplateSpecializationDecl(
                  hasName("std::basic_string"),
                  hasTemplateArgument(0,
                                      templateArgument().bind("char_type"))))),
              hasName("boost::lexical_cast"),
              hasParameter(0, hasType(qualType(has(substTemplateTypeParmType(
                                  isStrictlyInteger()))))))),
          argumentCountIs(1), unless(isInTemplateInstantiation()))
          .bind("to_string"),
      this);
}

void UseToStringCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("to_string");
  auto CharType =
      Result.Nodes.getNodeAs<TemplateArgument>("char_type")->getAsType();

  StringRef StringType;
  if (CharType->isSpecificBuiltinType(BuiltinType::Char_S) ||
      CharType->isSpecificBuiltinType(BuiltinType::Char_U))
    StringType = "string";
  else if (CharType->isSpecificBuiltinType(BuiltinType::WChar_S) ||
           CharType->isSpecificBuiltinType(BuiltinType::WChar_U))
    StringType = "wstring";
  else
    return;

  auto Loc = Call->getBeginLoc();
  auto Diag =
      diag(Loc, "use std::to_%0 instead of boost::lexical_cast<std::%0>")
      << StringType;

  if (Loc.isMacroID())
    return;

  Diag << FixItHint::CreateReplacement(
      CharSourceRange::getCharRange(Call->getBeginLoc(),
                                    Call->getArg(0)->getBeginLoc()),
      (llvm::Twine("std::to_") + StringType + "(").str());
}

} // namespace boost
} // namespace tidy
} // namespace clang