12 using namespace clang::ast_matchers;
19 return Node.isIntegerType() && !Node.isAnyCharacterType() &&
20 !Node.isBooleanType();
23 void UseToStringCheck::registerMatchers(MatchFinder *
Finder) {
24 if (!getLangOpts().CPlusPlus)
29 hasDeclaration(functionDecl(
30 returns(hasDeclaration(classTemplateSpecializationDecl(
31 hasName(
"std::basic_string"),
32 hasTemplateArgument(0,
33 templateArgument().bind(
"char_type"))))),
34 hasName(
"boost::lexical_cast"),
35 hasParameter(0, hasType(qualType(has(substTemplateTypeParmType(
36 isStrictlyInteger()))))))),
37 argumentCountIs(1), unless(isInTemplateInstantiation()))
42 void UseToStringCheck::check(
const MatchFinder::MatchResult &
Result) {
43 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"to_string");
45 Result.Nodes.getNodeAs<TemplateArgument>(
"char_type")->getAsType();
48 if (CharType->isSpecificBuiltinType(BuiltinType::Char_S) ||
49 CharType->isSpecificBuiltinType(BuiltinType::Char_U))
50 StringType =
"string";
51 else if (CharType->isSpecificBuiltinType(BuiltinType::WChar_S) ||
52 CharType->isSpecificBuiltinType(BuiltinType::WChar_U))
53 StringType =
"wstring";
57 auto Loc = Call->getLocStart();
59 diag(
Loc,
"use std::to_%0 instead of boost::lexical_cast<std::%0>")
65 Diag << FixItHint::CreateReplacement(
66 CharSourceRange::getCharRange(Call->getLocStart(),
67 Call->getArg(0)->getLocStart()),
68 (llvm::Twine(
"std::to_") + StringType +
"(").str());
SourceLocation Loc
'#' location in the include directive
AST_MATCHER(Type, isStrictlyInteger)
std::unique_ptr< ast_matchers::MatchFinder > Finder