1 //===--- UseToStringCheck.cpp - clang-tidy---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "UseToStringCheck.h"
11 using namespace clang::ast_matchers
;
13 namespace clang::tidy::boost
{
16 AST_MATCHER(Type
, isStrictlyInteger
) {
17 return Node
.isIntegerType() && !Node
.isAnyCharacterType() &&
18 !Node
.isBooleanType();
22 void UseToStringCheck::registerMatchers(MatchFinder
*Finder
) {
25 hasDeclaration(functionDecl(
26 returns(hasDeclaration(classTemplateSpecializationDecl(
27 hasName("std::basic_string"),
28 hasTemplateArgument(0,
29 templateArgument().bind("char_type"))))),
30 hasName("boost::lexical_cast"),
31 hasParameter(0, hasType(qualType(has(substTemplateTypeParmType(
32 isStrictlyInteger()))))))),
33 argumentCountIs(1), unless(isInTemplateInstantiation()))
38 void UseToStringCheck::check(const MatchFinder::MatchResult
&Result
) {
39 const auto *Call
= Result
.Nodes
.getNodeAs
<CallExpr
>("to_string");
41 Result
.Nodes
.getNodeAs
<TemplateArgument
>("char_type")->getAsType();
44 if (CharType
->isSpecificBuiltinType(BuiltinType::Char_S
) ||
45 CharType
->isSpecificBuiltinType(BuiltinType::Char_U
))
46 StringType
= "string";
47 else if (CharType
->isSpecificBuiltinType(BuiltinType::WChar_S
) ||
48 CharType
->isSpecificBuiltinType(BuiltinType::WChar_U
))
49 StringType
= "wstring";
53 auto Loc
= Call
->getBeginLoc();
55 diag(Loc
, "use std::to_%0 instead of boost::lexical_cast<std::%0>")
61 Diag
<< FixItHint::CreateReplacement(
62 CharSourceRange::getCharRange(Call
->getBeginLoc(),
63 Call
->getArg(0)->getBeginLoc()),
64 (llvm::Twine("std::to_") + StringType
+ "(").str());
67 } // namespace clang::tidy::boost