mgehre created this revision.
mgehre added a subscriber: cfe-commits.
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.
http://reviews.llvm.org/D13313
Files:
clang-tidy/misc/CMakeLists.txt
clang-tidy/misc/MiscTidyModule.cpp
clang-tidy/misc/NoReinterpretCastCheck.cpp
clang-tidy/misc/NoReinterpretCastCheck.h
docs/clang-tidy/checks/list.rst
docs/clang-tidy/checks/misc-no-reinterpret-cast.rst
test/clang-tidy/misc-no-reinterpret-cast.cpp
Index: test/clang-tidy/misc-no-reinterpret-cast.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/misc-no-reinterpret-cast.cpp
@@ -0,0 +1,6 @@
+// RUN: %python %S/check_clang_tidy.py %s misc-no-reinterpret-cast %t
+
+int i = 0;
+void* j;
+void f() { j = reinterpret_cast<void*>(i); }
+// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use reinterpret_cast (C++ Core Guidelines, rule Type.1) [misc-no-reinterpret-cast]
\ No newline at end of file
Index: docs/clang-tidy/checks/misc-no-reinterpret-cast.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/misc-no-reinterpret-cast.rst
@@ -0,0 +1,9 @@
+misc-no-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
@@ -31,6 +31,7 @@
misc-macro-repeated-side-effects
misc-move-constructor-init
misc-new-delete-overloads
+ misc-no-reinterpret-cast
misc-noexcept-move-constructor
misc-non-copyable-objects
misc-sizeof-container
Index: clang-tidy/misc/NoReinterpretCastCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/misc/NoReinterpretCastCheck.h
@@ -0,0 +1,33 @@
+//===--- NoReinterpretCastCheck.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_MISC_NO_REINTERPRETCAST_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NO_REINTERPRETCAST_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 NoReinterpretCastCheck : public ClangTidyCheck {
+public:
+ NoReinterpretCastCheck(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_MISC_NO_REINTERPRETCAST_H
Index: clang-tidy/misc/NoReinterpretCastCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/misc/NoReinterpretCastCheck.cpp
@@ -0,0 +1,34 @@
+//===--- NoReinterpretCastCheck.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 "NoReinterpretCastCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+
+void NoReinterpretCastCheck::registerMatchers(MatchFinder *Finder) {
+ if (!getLangOpts().CPlusPlus)
+ return;
+
+ Finder->addMatcher(cxxReinterpretCastExpr().bind("cast"), this);
+}
+
+void NoReinterpretCastCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedCast =
+ Result.Nodes.getNodeAs<CXXReinterpretCastExpr>("cast");
+ diag(MatchedCast->getOperatorLoc(),
+ "do not use reinterpret_cast (C++ Core Guidelines, rule Type.1)");
+}
+
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/misc/MiscTidyModule.cpp
===================================================================
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -20,6 +20,7 @@
#include "MacroRepeatedSideEffectsCheck.h"
#include "MoveConstructorInitCheck.h"
#include "NewDeleteOverloadsCheck.h"
+#include "NoReinterpretCastCheck.h"
#include "NoexceptMoveConstructorCheck.h"
#include "NonCopyableObjects.h"
#include "SizeofContainerCheck.h"
@@ -57,6 +58,8 @@
"misc-move-constructor-init");
CheckFactories.registerCheck<NewDeleteOverloadsCheck>(
"misc-new-delete-overloads");
+ CheckFactories.registerCheck<NoReinterpretCastCheck>(
+ "misc-no-reinterpret-cast");
CheckFactories.registerCheck<NoexceptMoveConstructorCheck>(
"misc-noexcept-move-constructor");
CheckFactories.registerCheck<NonCopyableObjectsCheck>(
Index: clang-tidy/misc/CMakeLists.txt
===================================================================
--- clang-tidy/misc/CMakeLists.txt
+++ clang-tidy/misc/CMakeLists.txt
@@ -12,6 +12,7 @@
MiscTidyModule.cpp
MoveConstructorInitCheck.cpp
NewDeleteOverloadsCheck.cpp
+ NoReinterpretCastCheck.cpp
NoexceptMoveConstructorCheck.cpp
NonCopyableObjects.cpp
SizeofContainerCheck.cpp
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits