[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / clang-tools-extra / clang-tidy / boost / UseToStringCheck.cpp
blobbcd5def290599772976e04bfc1e43709b58b19c6
1 //===--- UseToStringCheck.cpp - clang-tidy---------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "UseToStringCheck.h"
11 using namespace clang::ast_matchers;
13 namespace clang::tidy::boost {
15 namespace {
16 AST_MATCHER(Type, isStrictlyInteger) {
17 return Node.isIntegerType() && !Node.isAnyCharacterType() &&
18 !Node.isBooleanType();
20 } // namespace
22 void UseToStringCheck::registerMatchers(MatchFinder *Finder) {
23 Finder->addMatcher(
24 callExpr(
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()))
34 .bind("to_string"),
35 this);
38 void UseToStringCheck::check(const MatchFinder::MatchResult &Result) {
39 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("to_string");
40 auto CharType =
41 Result.Nodes.getNodeAs<TemplateArgument>("char_type")->getAsType();
43 StringRef StringType;
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";
50 else
51 return;
53 auto Loc = Call->getBeginLoc();
54 auto Diag =
55 diag(Loc, "use std::to_%0 instead of boost::lexical_cast<std::%0>")
56 << StringType;
58 if (Loc.isMacroID())
59 return;
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