staronj retitled this revision from "[clang-tidy] Adds misc-use-bool-literals check." to "[clang-tidy] Adds modernize-use-bool-literals check.". staronj updated this revision to Diff 52739. staronj added a comment.
1. Name changed from misc-use-bool-literals to modernize-use-bool-literals. 2. Code clang-formatted. 3. Check ran on LLVM code. http://reviews.llvm.org/D18745 Files: clang-tidy/modernize/CMakeLists.txt clang-tidy/modernize/ModernizeTidyModule.cpp clang-tidy/modernize/UseBoolLiteralsCheck.cpp clang-tidy/modernize/UseBoolLiteralsCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/modernize-use-bool-literals.rst test/clang-tidy/modernize-use-bool-literals.cpp
Index: test/clang-tidy/modernize-use-bool-literals.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-use-bool-literals.cpp @@ -0,0 +1,41 @@ +// RUN: %check_clang_tidy %s modernize-use-bool-literals %t + +bool bar1 = 1; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: implicitly converting integer literal to bool, use bool literal instead [modernize-use-bool-literals] +// CHECK-FIXES: {{^}}bool bar1 = true;{{$}} + +bool bar2 = 0; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}} +// CHECK-FIXES: {{^}}bool bar2 = false;{{$}} + +bool bar3 = 0x123ABCLL; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}} +// CHECK-FIXES: {{^}}bool bar3 = true;{{$}} + +#define TRUE_FALSE 1 + +bool bar4 = TRUE_FALSE; +// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: implicitly converting integer literal to bool inside macro, use bool literal instead [modernize-use-bool-literals] + +#define TRUE_FALSE2 bool(1) // OK + +bool bar6 = true; // OK + +void foo4(bool bar) { + +} + +char bar7 = 0; +unsigned long long bar8 = 1; + +int main() { + foo4(1); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}} + // CHECK-FIXES: {{^}} foo4(true);{{$}} + + foo4(false); // OK + + bar1 = 0; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: {{.*}} + // CHECK-FIXES: {{^}} bar1 = false;{{$}} +} Index: docs/clang-tidy/checks/modernize-use-bool-literals.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/modernize-use-bool-literals.rst @@ -0,0 +1,16 @@ +.. title:: clang-tidy - modernize-use-bool-literals + +modernize-use-bool-literals +=========================== + +Finds integer literals, which are implicitly cast to bool. + +.. code-block:: c++ + + bool p = 1; + std::ios_base::sync_with_stdio(0); + + // transforms to + + bool p = true; + std::ios_base::sync_with_stdio(false); \ No newline at end of file Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -31,9 +31,9 @@ google-build-using-namespace google-explicit-constructor google-global-names-in-headers - google-readability-braces-around-statements (redirects to readability-braces-around-statements) <readability-braces-around-statements> + google-readability-braces-around-statements (redirects to readability-braces-around-statements) <google-readability-braces-around-statements> google-readability-casting - google-readability-function-size (redirects to readability-function-size) <readability-function-size> + google-readability-function-size (redirects to readability-function-size) <google-readability-function-size> google-readability-namespace-comments google-readability-redundant-smartptr-get google-readability-todo @@ -85,6 +85,7 @@ modernize-replace-auto-ptr modernize-shrink-to-fit modernize-use-auto + modernize-use-bool-literals modernize-use-default modernize-use-nullptr modernize-use-override Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -119,6 +119,11 @@ Selectively replaces string literals containing escaped characters with raw string literals. +- New `modernize-use-bool-literals + <http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-bool-literals.html>`_ check + + Finds integer literals, which are implicitly cast to bool. + - New `performance-faster-string-find <http://clang.llvm.org/extra/clang-tidy/checks/performance-faster-string-find.html>`_ check Index: clang-tidy/modernize/UseBoolLiteralsCheck.h =================================================================== --- /dev/null +++ clang-tidy/modernize/UseBoolLiteralsCheck.h @@ -0,0 +1,35 @@ +//===--- UseBoolLiteralsCheck.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_MODERNIZE_USE_BOOL_LITERALS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_BOOL_LITERALS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace modernize { + +/// Finds integer literals, which are implicitly cast to bool. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-bool-literals.html +class UseBoolLiteralsCheck : public ClangTidyCheck { +public: + UseBoolLiteralsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace modernize +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_BOOL_LITERALS_H Index: clang-tidy/modernize/UseBoolLiteralsCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/modernize/UseBoolLiteralsCheck.cpp @@ -0,0 +1,59 @@ +//===--- UseBoolLiteralsCheck.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 "UseBoolLiteralsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace modernize { + +void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher( + implicitCastExpr(has(integerLiteral().bind("literal")), + hasImplicitDestinationType(qualType(booleanType()))), + this); +} + +/// \brief Checks, if integer \p Literal is preprocessor dependent +static bool isPreprocessorIndependent(const IntegerLiteral *Literal, + const MatchFinder::MatchResult &Result) { + const LangOptions &Opts = Result.Context->getLangOpts(); + StringRef LiteralSource = Lexer::getSourceText( + CharSourceRange::getTokenRange(Literal->getSourceRange()), + *Result.SourceManager, Opts); + + return LiteralSource.size() >= 1 && isDigit(LiteralSource.front()); +} + +void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal"); + + if (isPreprocessorIndependent(Literal, Result)) { + bool LiteralBooleanValue = Literal->getValue().getBoolValue(); + diag(Literal->getLocation(), "implicitly converting integer literal to " + "bool, use bool literal instead") + << FixItHint::CreateReplacement(Literal->getSourceRange(), + LiteralBooleanValue ? "true" : "false"); + } else { + diag(Literal->getLocation(), "implicitly converting integer literal to " + "bool inside macro, use bool literal instead"); + } +} + +} // namespace modernize +} // namespace tidy +} // namespace clang Index: clang-tidy/modernize/ModernizeTidyModule.cpp =================================================================== --- clang-tidy/modernize/ModernizeTidyModule.cpp +++ clang-tidy/modernize/ModernizeTidyModule.cpp @@ -19,6 +19,7 @@ #include "ReplaceAutoPtrCheck.h" #include "ShrinkToFitCheck.h" #include "UseAutoCheck.h" +#include "UseBoolLiteralsCheck.h" #include "UseDefaultCheck.h" #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" @@ -45,6 +46,8 @@ "modernize-replace-auto-ptr"); CheckFactories.registerCheck<ShrinkToFitCheck>("modernize-shrink-to-fit"); CheckFactories.registerCheck<UseAutoCheck>("modernize-use-auto"); + CheckFactories.registerCheck<UseBoolLiteralsCheck>( + "modernize-use-bool-literals"); CheckFactories.registerCheck<UseDefaultCheck>("modernize-use-default"); CheckFactories.registerCheck<UseNullptrCheck>("modernize-use-nullptr"); CheckFactories.registerCheck<UseOverrideCheck>("modernize-use-override"); Index: clang-tidy/modernize/CMakeLists.txt =================================================================== --- clang-tidy/modernize/CMakeLists.txt +++ clang-tidy/modernize/CMakeLists.txt @@ -12,6 +12,7 @@ ReplaceAutoPtrCheck.cpp ShrinkToFitCheck.cpp UseAutoCheck.cpp + UseBoolLiteralsCheck.cpp UseDefaultCheck.cpp UseNullptrCheck.cpp UseOverrideCheck.cpp
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits