clang-tools 20.0.0git
UseToStringCheck.cpp
Go to the documentation of this file.
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
11using namespace clang::ast_matchers;
12
13namespace clang::tidy::boost {
14
15namespace {
16AST_MATCHER(Type, isStrictlyInteger) {
17 return Node.isIntegerType() && !Node.isAnyCharacterType() &&
18 !Node.isBooleanType();
19}
20} // namespace
21
22void 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);
36}
37
38void 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();
42
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;
52
53 auto Loc = Call->getBeginLoc();
54 auto Diag =
55 diag(Loc, "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(0)->getBeginLoc()),
64 (llvm::Twine("std::to_") + StringType + "(").str());
65}
66
67} // namespace clang::tidy::boost
NodeType Type
SourceLocation Loc
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind