[clang-tools-extra] r308181 - [clang-tidy] Add modernize-use-bool-literals.IgnoreMacros option

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 17 07:43:06 PDT 2017


Author: alexfh
Date: Mon Jul 17 07:43:06 2017
New Revision: 308181

URL: http://llvm.org/viewvc/llvm-project?rev=308181&view=rev
Log:
[clang-tidy] Add modernize-use-bool-literals.IgnoreMacros option

Added:
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals-ignore-macros.cpp
      - copied, changed from r308085, clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.h
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.cpp?rev=308181&r1=308180&r2=308181&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.cpp Mon Jul 17 07:43:06 2017
@@ -18,6 +18,11 @@ namespace clang {
 namespace tidy {
 namespace modernize {
 
+UseBoolLiteralsCheck::UseBoolLiteralsCheck(StringRef Name,
+                                           ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
+
 void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
     return;
@@ -52,11 +57,16 @@ void UseBoolLiteralsCheck::check(const M
 
   const Expr *Expression = Cast ? Cast : Literal;
 
+  bool InMacro = Expression->getLocStart().isMacroID();
+
+  if (InMacro && IgnoreMacros)
+    return;
+
   auto Diag =
       diag(Expression->getExprLoc(),
            "converting integer literal to bool, use bool literal instead");
 
-  if (!Expression->getLocStart().isMacroID())
+  if (!InMacro)
     Diag << FixItHint::CreateReplacement(
         Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
 }

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.h?rev=308181&r1=308180&r2=308181&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseBoolLiteralsCheck.h Mon Jul 17 07:43:06 2017
@@ -22,10 +22,12 @@ namespace modernize {
 /// 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) {}
+  UseBoolLiteralsCheck(StringRef Name, ClangTidyContext *Context);
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+  const bool IgnoreMacros;
 };
 
 } // namespace modernize

Copied: clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals-ignore-macros.cpp (from r308085, clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp)
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals-ignore-macros.cpp?p2=clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals-ignore-macros.cpp&p1=clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp&r1=308085&r2=308181&rev=308181&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals-ignore-macros.cpp Mon Jul 17 07:43:06 2017
@@ -1,4 +1,8 @@
-// RUN: %check_clang_tidy %s modernize-use-bool-literals %t
+// RUN: %check_clang_tidy %s modernize-use-bool-literals %t -- \
+// RUN:   -config="{CheckOptions: \
+// RUN:             [{key: modernize-use-bool-literals.IgnoreMacros, \
+// RUN:               value: 1}]}" \
+// RUN:   -- -std=c++11
 
 bool IntToTrue = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals]
@@ -28,7 +32,6 @@ bool ExplicitStaticIntToFalse = static_c
 // CHECK-FIXES: {{^}}#define TRUE_MACRO 1{{$}}
 
 bool MacroIntToTrue = TRUE_MACRO;
-// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: converting integer literal to bool
 // CHECK-FIXES: {{^}}bool MacroIntToTrue = TRUE_MACRO;{{$}}
 
 #define FALSE_MACRO bool(0)
@@ -37,7 +40,6 @@ bool MacroIntToTrue = TRUE_MACRO;
 bool TrueBool = true; // OK
 
 bool FalseBool = bool(FALSE_MACRO);
-// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: converting integer literal to bool
 // CHECK-FIXES: {{^}}bool FalseBool = bool(FALSE_MACRO);{{$}}
 
 void boolFunction(bool bar) {
@@ -52,11 +54,9 @@ unsigned long long LongInteger = 1; // O
 // CHECK-FIXES: {{^}}#define MACRO_DEPENDENT_CAST(x) static_cast<bool>(x){{$}}
 
 bool MacroDependentBool = MACRO_DEPENDENT_CAST(0);
-// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: converting integer literal to bool
 // CHECK-FIXES: {{^}}bool MacroDependentBool = MACRO_DEPENDENT_CAST(0);{{$}}
 
 bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO);
-// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: converting integer literal to bool
 // CHECK-FIXES: {{^}}bool ManyMacrosDependent = MACRO_DEPENDENT_CAST(FALSE_MACRO);{{$}}
 
 class FooClass {

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp?rev=308181&r1=308180&r2=308181&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-bool-literals.cpp Mon Jul 17 07:43:06 2017
@@ -1,4 +1,8 @@
-// RUN: %check_clang_tidy %s modernize-use-bool-literals %t
+// RUN: %check_clang_tidy %s modernize-use-bool-literals %t -- \
+// RUN:   -config="{CheckOptions: \
+// RUN:             [{key: modernize-use-bool-literals.IgnoreMacros, \
+// RUN:               value: 0}]}" \
+// RUN:   -- -std=c++11
 
 bool IntToTrue = 1;
 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: converting integer literal to bool, use bool literal instead [modernize-use-bool-literals]




More information about the cfe-commits mailing list