Added a comment in r212033.
On Sun, Jun 29, 2014 at 11:56 PM, Alp Toker <[email protected]> wrote: > I'm not particularly opposed to duplicating -Wold-style-cast in clang-tidy > using ASTMatches because if nothing else, it's interesting to compare and > contrast the two methods. > > But please do add a brief comment explaining how this new check relates to > the existing warning option in clang and whether the output locations are > expected to match precisely, or if they differ, why that is. > > Thanks > > > > On 30/06/2014 01:19, Alexander Kornienko wrote: > >> Author: alexfh >> Date: Sun Jun 29 17:19:53 2014 >> New Revision: 212002 >> >> URL: http://llvm.org/viewvc/llvm-project?rev=212002&view=rev >> Log: >> Another attempt to add a clang-tidy check for flagging C-style casts. >> >> Summary: >> The first version failed the SubstNonTypeTempateParmExpr-related test >> on some buildbots. This one uses the new substNonTypeTempateParmExpr >> matcher to >> filter out implicit C-style casts. >> >> This patch depends on D4327. >> >> Reviewers: djasper >> >> Reviewed By: djasper >> >> Subscribers: aemerson, cfe-commits >> >> Differential Revision: http://reviews.llvm.org/D4328 >> >> Added: >> clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp >> clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.h >> clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp >> Modified: >> clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt >> clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp >> >> Added: clang-tools-extra/trunk/clang-tidy/google/ >> AvoidCStyleCastsCheck.cpp >> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/ >> trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=212002&view=auto >> ============================================================ >> ================== >> --- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp >> (added) >> +++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp >> Sun Jun 29 17:19:53 2014 >> @@ -0,0 +1,52 @@ >> +//===--- AvoidCStyleCastsCheck.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 "AvoidCStyleCastsCheck.h" >> +#include "clang/ASTMatchers/ASTMatchFinder.h" >> +#include "clang/ASTMatchers/ASTMatchers.h" >> + >> +using namespace clang::ast_matchers; >> + >> +namespace clang { >> +namespace tidy { >> +namespace readability { >> + >> +void >> +AvoidCStyleCastsCheck::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(hasParent(substNonTypeTemplateParmExpr() >> ))).bind("cast"), >> + this); >> +} >> + >> +void AvoidCStyleCastsCheck::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."); >> + // FIXME: Suggest appropriate C++ cast. See [expr.cast] for cast >> notation >> + // semantics. >> +} >> + >> +} // namespace readability >> +} // namespace tidy >> +} // namespace clang >> >> Added: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.h >> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/ >> trunk/clang-tidy/google/AvoidCStyleCastsCheck.h?rev=212002&view=auto >> ============================================================ >> ================== >> --- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.h >> (added) >> +++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.h >> Sun Jun 29 17:19:53 2014 >> @@ -0,0 +1,33 @@ >> +//===--- AvoidCStyleCastsCheck.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_AVOID_C_STYLE_ >> CASTS_CHECK_H >> +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_AVOID_C_STYLE_ >> CASTS_CHECK_H >> + >> +#include "../ClangTidy.h" >> + >> +namespace clang { >> +namespace tidy { >> +namespace readability { >> + >> +/// \brief Finds usages of C-style casts. >> +/// >> +/// http://google-styleguide.googlecode.com/svn/trunk/ >> cppguide.xml?showone=Casting#Casting >> +/// Corresponding cpplint.py check name: 'readability/casting'. >> +class AvoidCStyleCastsCheck : 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_AVOID_C_STYLE_ >> CASTS_CHECK_H >> >> Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt >> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/ >> trunk/clang-tidy/google/CMakeLists.txt?rev=212002&r1= >> 212001&r2=212002&view=diff >> ============================================================ >> ================== >> --- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt (original) >> +++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt Sun Jun 29 >> 17:19:53 2014 >> @@ -1,6 +1,7 @@ >> set(LLVM_LINK_COMPONENTS support) >> add_clang_library(clangTidyGoogleModule >> + AvoidCStyleCastsCheck.cpp >> ExplicitConstructorCheck.cpp >> GoogleTidyModule.cpp >> >> Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp >> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/ >> trunk/clang-tidy/google/GoogleTidyModule.cpp?rev= >> 212002&r1=212001&r2=212002&view=diff >> ============================================================ >> ================== >> --- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp >> (original) >> +++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Sun >> Jun 29 17:19:53 2014 >> @@ -10,6 +10,7 @@ >> #include "../ClangTidy.h" >> #include "../ClangTidyModule.h" >> #include "../ClangTidyModuleRegistry.h" >> +#include "AvoidCStyleCastsCheck.h" >> #include "ExplicitConstructorCheck.h" >> using namespace clang::ast_matchers; >> @@ -23,6 +24,9 @@ public: >> CheckFactories.addCheckFactory( >> "google-explicit-constructor", >> new ClangTidyCheckFactory<ExplicitConstructorCheck>()); >> + CheckFactories.addCheckFactory( >> + "google-readability-casting", >> + new ClangTidyCheckFactory<readability:: >> AvoidCStyleCastsCheck>()); >> } >> }; >> >> Added: clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp >> URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/ >> trunk/test/clang-tidy/avoid-c-style-casts.cpp?rev=212002&view=auto >> ============================================================ >> ================== >> --- clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp >> (added) >> +++ clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp Sun >> Jun 29 17:19:53 2014 >> @@ -0,0 +1,24 @@ >> +// 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 >> > > -- > http://www.nuanti.com > the browser experts > > -- Alexander Kornienko | Software Engineer | [email protected] | Google Germany, Munich
_______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
