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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "UseToStringCheck.h" |
10 | |
11 | using namespace clang::ast_matchers; |
12 | |
13 | namespace clang::tidy::boost { |
14 | |
15 | namespace { |
16 | AST_MATCHER(Type, isStrictlyInteger) { |
17 | return Node.isIntegerType() && !Node.isAnyCharacterType() && |
18 | !Node.isBooleanType(); |
19 | } |
20 | } // namespace |
21 | |
22 | void UseToStringCheck::registerMatchers(MatchFinder *Finder) { |
23 | Finder->addMatcher( |
24 | NodeMatch: callExpr( |
25 | hasDeclaration(InnerMatcher: functionDecl( |
26 | returns(InnerMatcher: hasDeclaration(InnerMatcher: classTemplateSpecializationDecl( |
27 | hasName(Name: "std::basic_string" ), |
28 | hasTemplateArgument(N: 0, |
29 | InnerMatcher: templateArgument().bind(ID: "char_type" ))))), |
30 | hasName(Name: "boost::lexical_cast" ), |
31 | hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: qualType(has(substTemplateTypeParmType( |
32 | isStrictlyInteger()))))))), |
33 | argumentCountIs(N: 1), unless(isInTemplateInstantiation())) |
34 | .bind(ID: "to_string" ), |
35 | Action: this); |
36 | } |
37 | |
38 | void UseToStringCheck::check(const MatchFinder::MatchResult &Result) { |
39 | const auto *Call = Result.Nodes.getNodeAs<CallExpr>(ID: "to_string" ); |
40 | auto CharType = |
41 | Result.Nodes.getNodeAs<TemplateArgument>(ID: "char_type" )->getAsType(); |
42 | |
43 | StringRef StringType; |
44 | if (CharType->isSpecificBuiltinType(K: BuiltinType::Char_S) || |
45 | CharType->isSpecificBuiltinType(K: BuiltinType::Char_U)) |
46 | StringType = "string" ; |
47 | else if (CharType->isSpecificBuiltinType(K: BuiltinType::WChar_S) || |
48 | CharType->isSpecificBuiltinType(K: BuiltinType::WChar_U)) |
49 | StringType = "wstring" ; |
50 | else |
51 | return; |
52 | |
53 | auto Loc = Call->getBeginLoc(); |
54 | auto Diag = |
55 | diag(Loc, Description: "use std::to_%0 instead of boost::lexical_cast<std::%0>" ) |
56 | << StringType; |
57 | |
58 | if (Loc.isMacroID()) |
59 | return; |
60 | |
61 | Diag << FixItHint::CreateReplacement( |
62 | CharSourceRange::getCharRange(Call->getBeginLoc(), |
63 | Call->getArg(Arg: 0)->getBeginLoc()), |
64 | (llvm::Twine("std::to_" ) + StringType + "(" ).str()); |
65 | } |
66 | |
67 | } // namespace clang::tidy::boost |
68 | |