Index: clang-tidy/CMakeLists.txt =================================================================== --- clang-tidy/CMakeLists.txt +++ clang-tidy/CMakeLists.txt @@ -28,6 +28,7 @@ add_subdirectory(tool) add_subdirectory(cert) add_subdirectory(llvm) +add_subdirectory(cppcoreguidelines) add_subdirectory(google) add_subdirectory(misc) add_subdirectory(modernize) Index: clang-tidy/Makefile =================================================================== --- clang-tidy/Makefile +++ clang-tidy/Makefile @@ -11,6 +11,6 @@ LIBRARYNAME := clangTidy include $(CLANG_LEVEL)/../../Makefile.config -DIRS = utils cert readability llvm google misc modernize tool +DIRS = utils cert cppcoreguidelines readability llvm google misc modernize tool include $(CLANG_LEVEL)/Makefile Index: clang-tidy/add_new_check.py =================================================================== --- clang-tidy/add_new_check.py +++ clang-tidy/add_new_check.py @@ -147,7 +147,8 @@ # Modifies the module to include the new check. def adapt_module(module_path, module, check_name, check_name_camel): - filename = os.path.join(module_path, module.capitalize() + 'TidyModule.cpp') + modulecpp = filter(lambda p: p.lower() == module.lower() + "tidymodule.cpp", os.listdir(module_path))[0] + filename = os.path.join(module_path, modulecpp) with open(filename, 'r') as f: lines = f.readlines() Index: clang-tidy/cppcoreguidelines/CMakeLists.txt =================================================================== --- /dev/null +++ clang-tidy/cppcoreguidelines/CMakeLists.txt @@ -0,0 +1,15 @@ +set(LLVM_LINK_COMPONENTS support) + +add_clang_library(clangTidyCppCoreGuidelinesModule + CppCoreGuidelinesTidyModule.cpp + ProTypeReinterpretCastCheck.cpp + + LINK_LIBS + clangAST + clangASTMatchers + clangBasic + clangLex + clangTidy + clangTidyUtils + clangTooling + ) Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp =================================================================== --- /dev/null +++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -0,0 +1,38 @@ +//===--- CppCoreGuidelinesModule.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 "ProTypeReinterpretCastCheck.h" + +namespace clang { +namespace tidy { +namespace cppcoreguidelines { + +class CppCoreGuidelinesModule : public ClangTidyModule { +public: + void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "cppcoreguidelines-pro-type-reinterpret-cast"); + } +}; + +// Register the LLVMTidyModule using this statically initialized variable. +static ClangTidyModuleRegistry::Add + X("cppcoreguidelines-module", "Adds checks for the C++ Core Guidelines."); + +} // namespace cppcoreguidelines + +// This anchor is used to force the linker to link in the generated object file +// and thus register the CppCoreGuidelinesModule. +volatile int CppCoreGuidelinesModuleAnchorSource = 0; + +} // namespace tidy +} // namespace clang Index: clang-tidy/cppcoreguidelines/Makefile =================================================================== --- /dev/null +++ clang-tidy/cppcoreguidelines/Makefile @@ -0,0 +1,12 @@ +##===- clang-tidy/cppcoreguidelines/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +CLANG_LEVEL := ../../../.. +LIBRARYNAME := clangTidyCppCoreGuidelinesModule + +include $(CLANG_LEVEL)/Makefile Index: clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h =================================================================== --- /dev/null +++ clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h @@ -0,0 +1,33 @@ +//===--- ProTypeReinterpretCast.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_REINTERPRETCAST_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_REINTERPRETCAST_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// Flags all occurrences of reinterpret_cast +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-no-reinterpret-cast.html +class ProTypeReinterpretCastCheck : public ClangTidyCheck { +public: + ProTypeReinterpretCastCheck(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_REINTERPRETCAST_CHECK_H Index: clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp @@ -0,0 +1,34 @@ +//===--- ProTypeReinterpretCastCheck.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 "ProTypeReinterpretCastCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void ProTypeReinterpretCastCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher(cxxReinterpretCastExpr().bind("cast"), this); +} + +void ProTypeReinterpretCastCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCast = + Result.Nodes.getNodeAs("cast"); + diag(MatchedCast->getOperatorLoc(), + "do not use reinterpret_cast"); +} + +} // namespace tidy +} // namespace clang Index: clang-tidy/tool/CMakeLists.txt =================================================================== --- clang-tidy/tool/CMakeLists.txt +++ clang-tidy/tool/CMakeLists.txt @@ -11,6 +11,7 @@ clangBasic clangTidy clangTidyCERTModule + clangTidyCppCoreGuidelinesModule clangTidyGoogleModule clangTidyLLVMModule clangTidyMiscModule Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -357,6 +357,11 @@ static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination = LLVMModuleAnchorSource; +// This anchor is used to force the linker to link the CppCoreGuidelinesModule. +extern volatile int CppCoreGuidelinesModuleAnchorSource; +static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination = + CppCoreGuidelinesModuleAnchorSource; + // This anchor is used to force the linker to link the GoogleModule. extern volatile int GoogleModuleAnchorSource; static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination = Index: clang-tidy/tool/Makefile =================================================================== --- clang-tidy/tool/Makefile +++ clang-tidy/tool/Makefile @@ -19,6 +19,7 @@ USEDLIBS = clangTidy.a clangTidyLLVMModule.a clangTidyGoogleModule.a \ clangTidyMiscModule.a clangTidyModernizeModule.a clangTidyReadability.a \ clangTidyUtils.a clangTidyCERTModule.a clangStaticAnalyzerFrontend.a \ + clangTidyCppCoreGuidelinesModule.a \ clangStaticAnalyzerCheckers.a clangStaticAnalyzerCore.a \ clangFormat.a clangASTMatchers.a clangTooling.a clangToolingCore.a \ clangFrontend.a clangSerialization.a clangDriver.a clangParse.a \ Index: docs/clang-tidy/checks/cppcoreguidelines-pro-type-reinterpret-cast.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/cppcoreguidelines-pro-type-reinterpret-cast.rst @@ -0,0 +1,9 @@ +cppcoreguidelines-pro-type-reinterpret-cast +=========================================== + +This check flags all uses of reinterpret_cast in C++ code. + +Use of these casts can violate type safety and cause the program to access a variable that is actually of type X to be accessed as if it were of an unrelated type Z. + +This rule is part of the "Type safety" profile of the C++ Core Guidelines, see +https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type1-dont-use-reinterpret_cast. Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -3,6 +3,7 @@ .. toctree:: cert-variadic-function-def + cppcoreguidelines-pro-type-reinterpret-cast google-build-explicit-make-pair google-build-namespaces google-build-using-namespace Index: test/clang-tidy/cppcoreguidelines-pro-type-reinterpret-cast.cpp =================================================================== --- /dev/null +++ test/clang-tidy/cppcoreguidelines-pro-type-reinterpret-cast.cpp @@ -0,0 +1,6 @@ +// RUN: %python %S/check_clang_tidy.py %s cppcoreguidelines-pro-type-reinterpret-cast %t + +int i = 0; +void* j; +void f() { j = reinterpret_cast(i); } +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]