diff options
| author | Piotr Padlewski <piotr.padlewski@gmail.com> | 2016-04-29 17:58:29 +0000 |
|---|---|---|
| committer | Piotr Padlewski <piotr.padlewski@gmail.com> | 2016-04-29 17:58:29 +0000 |
| commit | 5625f656678a7d03fa65398d6d751dc88cba75a8 (patch) | |
| tree | 68a9658777a1f669875f5ebd26a96572140d0c89 /clang-tools-extra/clang-tidy/boost | |
| parent | 46977b62aaaca7a5853a6e3eeabc0adc8f53ef32 (diff) | |
| download | bcm5719-llvm-5625f656678a7d03fa65398d6d751dc88cba75a8.tar.gz bcm5719-llvm-5625f656678a7d03fa65398d6d751dc88cba75a8.zip | |
Add boost-use-to-string
http://reviews.llvm.org/D18136
llvm-svn: 268079
Diffstat (limited to 'clang-tools-extra/clang-tidy/boost')
4 files changed, 162 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/boost/BoostTidyModule.cpp b/clang-tools-extra/clang-tidy/boost/BoostTidyModule.cpp new file mode 100644 index 00000000000..28eb1d656b0 --- /dev/null +++ b/clang-tools-extra/clang-tidy/boost/BoostTidyModule.cpp @@ -0,0 +1,38 @@ +//===------- BoostTidyModule.cpp - clang-tidy -----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "../ClangTidy.h" +#include "../ClangTidyModule.h" +#include "../ClangTidyModuleRegistry.h" +#include "UseToStringCheck.h" +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace boost { + +class BoostModule : public ClangTidyModule { +public: + void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck<UseToStringCheck>("boost-use-to-string"); + } +}; + +// Register the BoostModule using this statically initialized variable. +static ClangTidyModuleRegistry::Add<BoostModule> X("boost-module", + "Add boost checks."); + +} // namespace boost + +// This anchor is used to force the linker to link in the generated object file +// and thus register the BoostModule. +volatile int BoostModuleAnchorSource = 0; + +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/boost/CMakeLists.txt b/clang-tools-extra/clang-tidy/boost/CMakeLists.txt new file mode 100644 index 00000000000..059f6e91eca --- /dev/null +++ b/clang-tools-extra/clang-tidy/boost/CMakeLists.txt @@ -0,0 +1,14 @@ +set(LLVM_LINK_COMPONENTS support) + +add_clang_library(clangTidyBoostModule + BoostTidyModule.cpp + UseToStringCheck.cpp + + LINK_LIBS + clangAST + clangASTMatchers + clangBasic + clangLex + clangTidy + clangTidyUtils + ) diff --git a/clang-tools-extra/clang-tidy/boost/UseToStringCheck.cpp b/clang-tools-extra/clang-tidy/boost/UseToStringCheck.cpp new file mode 100644 index 00000000000..8982c8e459d --- /dev/null +++ b/clang-tools-extra/clang-tidy/boost/UseToStringCheck.cpp @@ -0,0 +1,73 @@ +//===--- UseToStringCheck.cpp - clang-tidy---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "UseToStringCheck.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace boost { + +AST_MATCHER(Type, isStrictlyInteger) { + return Node.isIntegerType() && !Node.isAnyCharacterType() && + !Node.isBooleanType(); +} + +void UseToStringCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher( + callExpr( + hasDeclaration(functionDecl( + returns(hasDeclaration(classTemplateSpecializationDecl( + hasName("std::basic_string"), + hasTemplateArgument(0, + templateArgument().bind("char_type"))))), + hasName("boost::lexical_cast"), + hasParameter(0, hasType(qualType(has(substTemplateTypeParmType( + isStrictlyInteger()))))))), + argumentCountIs(1), unless(isInTemplateInstantiation())) + .bind("to_string"), + this); +} + +void UseToStringCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Call = Result.Nodes.getNodeAs<CallExpr>("to_string"); + auto CharType = + Result.Nodes.getNodeAs<TemplateArgument>("char_type")->getAsType(); + + StringRef StringType; + if (CharType->isSpecificBuiltinType(BuiltinType::Char_S) || + CharType->isSpecificBuiltinType(BuiltinType::Char_U)) + StringType = "string"; + else if (CharType->isSpecificBuiltinType(BuiltinType::WChar_S) || + CharType->isSpecificBuiltinType(BuiltinType::WChar_U)) + StringType = "wstring"; + else + return; + + auto Loc = Call->getLocStart(); + auto Diag = + diag(Loc, "use std::to_%0 instead of boost::lexical_cast<std::%0>") + << StringType; + + if (Loc.isMacroID()) + return; + + Diag << FixItHint::CreateReplacement( + CharSourceRange::getCharRange(Call->getLocStart(), + Call->getArg(0)->getExprLoc()), + (llvm::Twine("std::to_") + StringType + "(").str()); +} + +} // namespace boost +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/boost/UseToStringCheck.h b/clang-tools-extra/clang-tidy/boost/UseToStringCheck.h new file mode 100644 index 00000000000..76e7823bf87 --- /dev/null +++ b/clang-tools-extra/clang-tidy/boost/UseToStringCheck.h @@ -0,0 +1,37 @@ +//===--- UseToStringCheck.h - clang-tidy-------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BOOST_USE_TO_STRING_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BOOST_USE_TO_STRING_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace boost { + +/// Finds calls to ``boost::lexical_cast<std::string>`` and +/// ``boost::lexical_cast<std::wstring>`` and replaces them with +/// ``std::to_string`` and ``std::to_wstring`` calls. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/boost-use-to-string.html +class UseToStringCheck : public ClangTidyCheck { +public: + UseToStringCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace boost +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BOOST_USE_TO_STRING_H |

