clang-tools  14.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 
11 using namespace clang::ast_matchers;
12 
13 namespace clang {
14 namespace tidy {
15 namespace boost {
16 
17 namespace {
18 AST_MATCHER(Type, isStrictlyInteger) {
19  return Node.isIntegerType() && !Node.isAnyCharacterType() &&
20  !Node.isBooleanType();
21 }
22 } // namespace
23 
24 void UseToStringCheck::registerMatchers(MatchFinder *Finder) {
25  Finder->addMatcher(
26  callExpr(
27  hasDeclaration(functionDecl(
28  returns(hasDeclaration(classTemplateSpecializationDecl(
29  hasName("std::basic_string"),
30  hasTemplateArgument(0,
31  templateArgument().bind("char_type"))))),
32  hasName("boost::lexical_cast"),
33  hasParameter(0, hasType(qualType(has(substTemplateTypeParmType(
34  isStrictlyInteger()))))))),
35  argumentCountIs(1), unless(isInTemplateInstantiation()))
36  .bind("to_string"),
37  this);
38 }
39 
40 void UseToStringCheck::check(const MatchFinder::MatchResult &Result) {
41  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("to_string");
42  auto CharType =
43  Result.Nodes.getNodeAs<TemplateArgument>("char_type")->getAsType();
44 
45  StringRef StringType;
46  if (CharType->isSpecificBuiltinType(BuiltinType::Char_S) ||
47  CharType->isSpecificBuiltinType(BuiltinType::Char_U))
48  StringType = "string";
49  else if (CharType->isSpecificBuiltinType(BuiltinType::WChar_S) ||
50  CharType->isSpecificBuiltinType(BuiltinType::WChar_U))
51  StringType = "wstring";
52  else
53  return;
54 
55  auto Loc = Call->getBeginLoc();
56  auto Diag =
57  diag(Loc, "use std::to_%0 instead of boost::lexical_cast<std::%0>")
58  << StringType;
59 
60  if (Loc.isMacroID())
61  return;
62 
63  Diag << FixItHint::CreateReplacement(
64  CharSourceRange::getCharRange(Call->getBeginLoc(),
65  Call->getArg(0)->getBeginLoc()),
66  (llvm::Twine("std::to_") + StringType + "(").str());
67 }
68 
69 } // namespace boost
70 } // namespace tidy
71 } // namespace clang
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:258
clang::clangd::CharType
CharType
Definition: FuzzyMatch.h:41
UseToStringCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27