Hi djasper,
Add a check to flag the usage of C-style casts, as per Google Style
Guide:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Casting#Casting
http://reviews.llvm.org/D4189
Files:
clang-tidy/google/CMakeLists.txt
clang-tidy/google/CStyleCastsCheck.cpp
clang-tidy/google/CStyleCastsCheck.h
clang-tidy/google/GoogleTidyModule.cpp
test/clang-tidy/c-style-casts.cpp
Index: clang-tidy/google/CMakeLists.txt
===================================================================
--- clang-tidy/google/CMakeLists.txt
+++ clang-tidy/google/CMakeLists.txt
@@ -1,6 +1,7 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyGoogleModule
+ CStyleCastsCheck.cpp
ExplicitConstructorCheck.cpp
GoogleTidyModule.cpp
Index: clang-tidy/google/CStyleCastsCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/google/CStyleCastsCheck.cpp
@@ -0,0 +1,50 @@
+//===--- CStyleCastsCheck.cpp - clang-tidy ----------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CStyleCastsCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+void CStyleCastsCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
+ Finder->addMatcher(
+ cStyleCastExpr(
+ // Filter out (EnumType)IntegerLiteral construct, which is generated
+ // for non-type template arguments of enum types.
+ // FIXME: Remove this once this is fixed in the AST.
+ unless(allOf(hasDestinationType(hasDeclaration(enumDecl())),
+ hasSourceExpression(integerLiteral())))).bind("cast"),
+ this);
+}
+
+void CStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
+
+ // Ignore casts in macros for now.
+ if (CastExpr->getLocStart().isMacroID())
+ return;
+
+ // Casting to void is an idiomatic way to mute "unused variable" and similar
+ // warnings.
+ if (CastExpr->getTypeAsWritten()->isVoidType())
+ return;
+
+ diag(CastExpr->getLocStart(), "C-style casts are discouraged. Use "
+ "static_cast/const_cast/reinterpret_cast "
+ "instead.");
+}
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/google/CStyleCastsCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/google/CStyleCastsCheck.h
@@ -0,0 +1,33 @@
+//===--- CStyleCastsCheck.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_GOOGLE_C_STYLE_CASTS_CHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_C_STYLE_CASTS_CHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+/// \brief Finds usages of C-style casts.
+///
+/// 'readability/casting'
+/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Casting#Casting
+class CStyleCastsCheck : public ClangTidyCheck {
+public:
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_C_STYLE_CASTS_CHECK_H
Index: clang-tidy/google/GoogleTidyModule.cpp
===================================================================
--- clang-tidy/google/GoogleTidyModule.cpp
+++ clang-tidy/google/GoogleTidyModule.cpp
@@ -10,6 +10,7 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
+#include "CStyleCastsCheck.h"
#include "ExplicitConstructorCheck.h"
using namespace clang::ast_matchers;
@@ -21,6 +22,9 @@
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.addCheckFactory(
+ "google-readability-casting",
+ new ClangTidyCheckFactory<readability::CStyleCastsCheck>());
+ CheckFactories.addCheckFactory(
"google-explicit-constructor",
new ClangTidyCheckFactory<ExplicitConstructorCheck>());
}
Index: test/clang-tidy/c-style-casts.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/c-style-casts.cpp
@@ -0,0 +1,23 @@
+// RUN: clang-tidy -checks=-*,google-readability-casting %s -- | FileCheck %s
+
+// CHECK-NOT: warning:
+bool g() { return false; }
+
+void f(int a, double b) {
+ int b1 = (int)b;
+ // CHECK: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast{{.*}}
+
+ // CHECK-NOT: warning:
+ int b2 = int(b);
+ int b3 = static_cast<double>(b);
+ int b4 = b;
+ double aa = a;
+ (void)b2;
+ return (void)g();
+}
+
+// CHECK-NOT: warning:
+enum E { E1 = 1 };
+template <E e>
+struct A { static const E ee = e; };
+struct B : public A<E1> {};
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits