diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2015-10-07 12:24:57 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2015-10-07 12:24:57 +0000 |
commit | 0bf129823a254c7e48db7db0e50048a14b56b449 (patch) | |
tree | 5db8c9ebf0418a66fc6f759850740d78268682c5 /clang-tools-extra/clang-tidy/cppcoreguidelines | |
parent | ca2bc7865dacaad09d6cfb57f1e328ab32bcb8dc (diff) | |
download | bcm5719-llvm-0bf129823a254c7e48db7db0e50048a14b56b449.tar.gz bcm5719-llvm-0bf129823a254c7e48db7db0e50048a14b56b449.zip |
Add checker for the C++ Core Guidelines: cppcoreguidelines-pro-type-const-cast.
Patch by Matthias Gehre!
llvm-svn: 249540
Diffstat (limited to 'clang-tools-extra/clang-tidy/cppcoreguidelines')
4 files changed, 71 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt index 7fb8c3f5bc4..5a83b3fef08 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyCppCoreGuidelinesModule CppCoreGuidelinesTidyModule.cpp + ProTypeConstCastCheck.cpp ProTypeReinterpretCastCheck.cpp LINK_LIBS diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp index 664f5c5a85b..0e33d8834bf 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "ProTypeConstCastCheck.h" #include "ProTypeReinterpretCastCheck.h" namespace clang { @@ -19,6 +20,8 @@ namespace cppcoreguidelines { class CppCoreGuidelinesModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck<ProTypeConstCastCheck>( + "cppcoreguidelines-pro-type-const-cast"); CheckFactories.registerCheck<ProTypeReinterpretCastCheck>( "cppcoreguidelines-pro-type-reinterpret-cast"); } diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.cpp new file mode 100644 index 00000000000..37aa04e605b --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.cpp @@ -0,0 +1,33 @@ +//===--- ProTypeConstCastCheck.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 "ProTypeConstCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void ProTypeConstCastCheck::registerMatchers(MatchFinder *Finder) { + if(!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher(cxxConstCastExpr().bind("cast"), this); +} + +void ProTypeConstCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCast = Result.Nodes.getNodeAs<CXXConstCastExpr>("cast"); + diag(MatchedCast->getOperatorLoc(), "do not use const_cast"); +} + +} // namespace tidy +} // namespace clang + diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.h new file mode 100644 index 00000000000..1515797acc1 --- /dev/null +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.h @@ -0,0 +1,34 @@ +//===--- ProTypeConstCastCheck.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_CPPCOREGUIDELINES_PRO_TYPE_CONST_CAST_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_CONST_CAST_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// This check flags all instances of const_cast +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-const-cast.html +class ProTypeConstCastCheck : public ClangTidyCheck { +public: + ProTypeConstCastCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_CONST_CAST_H + |