https://github.com/Da-Viper updated https://github.com/llvm/llvm-project/pull/104882
>From 5ca62897b62306958a67e0534a96909ea5c704de Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Sun, 11 Aug 2024 21:39:35 +0100 Subject: [PATCH 01/14] [clang-tidy] use upper cace letters for bool conversion suffix When readability-implicit-bool-conversion-check and readability-uppercase-literal-suffix-check is enabled this will cause you to apply a fix twice from (!i) -> (i == 0u) to (i == 0U) twice instead will skip the middle one --- .../readability/ImplicitBoolConversionCheck.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index aa115cd450c4f6..258cec80dd0bac 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -43,10 +43,10 @@ StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, ASTContext &Context) { switch (CastExprKind) { case CK_IntegralToBoolean: - return Type->isUnsignedIntegerType() ? "0u" : "0"; + return Type->isUnsignedIntegerType() ? "0U" : "0"; case CK_FloatingToBoolean: - return Context.hasSameType(Type, Context.FloatTy) ? "0.0f" : "0.0"; + return Context.hasSameType(Type, Context.FloatTy) ? "0.0F" : "0.0"; case CK_PointerToBoolean: case CK_MemberPointerToBoolean: // Fall-through on purpose. @@ -202,13 +202,13 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, if (DestType->isFloatingType()) { if (Context.hasSameType(DestType, Context.FloatTy)) { - return BoolLiteral->getValue() ? "1.0f" : "0.0f"; + return BoolLiteral->getValue() ? "1.0F" : "0.0F"; } return BoolLiteral->getValue() ? "1.0" : "0.0"; } if (DestType->isUnsignedIntegerType()) { - return BoolLiteral->getValue() ? "1u" : "0u"; + return BoolLiteral->getValue() ? "1U" : "0U"; } return BoolLiteral->getValue() ? "1" : "0"; } >From 83f38ddfbfd01586b9eaec4c80be6213189d4a73 Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 00:49:41 +0100 Subject: [PATCH 02/14] Update test cases --- .../readability/implicit-bool-conversion.c | 14 +++++----- .../readability/implicit-bool-conversion.cpp | 28 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c index a8c69858f76b61..29869b100894f4 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c @@ -94,7 +94,7 @@ void implicitConversionFromBoolLiterals() { functionTakingUnsignedLong(false); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'bool' -> 'unsigned long' - // CHECK-FIXES: functionTakingUnsignedLong(0u); + // CHECK-FIXES: functionTakingUnsignedLong(0U); functionTakingSignedChar(true); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'bool' -> 'signed char' @@ -102,7 +102,7 @@ void implicitConversionFromBoolLiterals() { functionTakingFloat(false); // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: implicit conversion 'bool' -> 'float' - // CHECK-FIXES: functionTakingFloat(0.0f); + // CHECK-FIXES: functionTakingFloat(0.0F); functionTakingDouble(true); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'bool' -> 'double' @@ -159,12 +159,12 @@ void implicitConversionToBoolSimpleCases() { unsigned long unsignedLong = 10; functionTakingBool(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'unsigned long' -> 'bool' - // CHECK-FIXES: functionTakingBool(unsignedLong != 0u); + // CHECK-FIXES: functionTakingBool(unsignedLong != 0U); float floating = 0.0f; functionTakingBool(floating); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTakingBool(floating != 0.0f); + // CHECK-FIXES: functionTakingBool(floating != 0.0F); double doubleFloating = 1.0f; functionTakingBool(doubleFloating); @@ -193,7 +193,7 @@ void implicitConversionToBoolInSingleExpressions() { bool boolComingFromFloat; boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: boolComingFromFloat = (floating != 0.0f); + // CHECK-FIXES: boolComingFromFloat = (floating != 0.0F); signed char character = 'a'; bool boolComingFromChar; @@ -231,7 +231,7 @@ bool implicitConversionToBoolInReturnValue() { float floating = 1.0f; return floating; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: return floating != 0.0f; + // CHECK-FIXES: return floating != 0.0F; } void implicitConversionToBoolFromLiterals() { @@ -287,7 +287,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTakingBool(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0f); + // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0F); functionTakingBool(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'double' -> 'bool' diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp index d6e7dcc4d8867b..f3c4481a7cc430 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp @@ -98,7 +98,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<unsigned long>(false); // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'bool' -> 'unsigned long' - // CHECK-FIXES: functionTaking<unsigned long>(0u); + // CHECK-FIXES: functionTaking<unsigned long>(0U); functionTaking<signed char>(true); // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: implicit conversion 'bool' -> 'signed char' @@ -106,7 +106,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<float>(false); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'bool' -> 'float' - // CHECK-FIXES: functionTaking<float>(0.0f); + // CHECK-FIXES: functionTaking<float>(0.0F); functionTaking<double>(true); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: implicit conversion 'bool' -> 'double' @@ -177,14 +177,14 @@ void implicitConversionToBoolSimpleCases() { unsigned long unsignedLong = 10; functionTaking<bool>(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'unsigned long' -> 'bool' - // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0u); + // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0U); - float floating = 0.0f; + float floating = 0.0F; functionTaking<bool>(floating); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTaking<bool>(floating != 0.0f); + // CHECK-FIXES: functionTaking<bool>(floating != 0.0F); - double doubleFloating = 1.0f; + double doubleFloating = 1.0F; functionTaking<bool>(doubleFloating); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool' // CHECK-FIXES: functionTaking<bool>(doubleFloating != 0.0); @@ -211,10 +211,10 @@ void implicitConversionToBoolInSingleExpressions() { // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'int' -> 'bool' // CHECK-FIXES: bool boolComingFromInt = integer != 0; - float floating = 10.0f; + float floating = 10.0F; bool boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0f; + // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0F; signed char character = 'a'; bool boolComingFromChar = character; @@ -239,7 +239,7 @@ void implicitConversionToBoolInComplexExpressions() { float floating = 0.2f; bool boolComingFromFloating = floating - 0.3f || boolean; // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0f) || boolean; + // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0F) || boolean; double doubleFloating = 0.3; bool boolComingFromDoubleFloating = (doubleFloating - 0.4) && boolean; @@ -253,10 +253,10 @@ void implicitConversionInNegationExpressions() { // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: implicit conversion 'int' -> 'bool' // CHECK-FIXES: bool boolComingFromNegatedInt = integer == 0; - float floating = 10.0f; + float floating = 10.0F; bool boolComingFromNegatedFloat = ! floating; // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0f; + // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0F; signed char character = 'a'; bool boolComingFromNegatedChar = (! character); @@ -283,7 +283,7 @@ void implicitConversionToBoolInControlStatements() { float floating = 0.3f; while (floating) {} // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: while (floating != 0.0f) {} + // CHECK-FIXES: while (floating != 0.0F) {} double doubleFloating = 0.4; do {} while (doubleFloating); @@ -295,7 +295,7 @@ bool implicitConversionToBoolInReturnValue() { float floating = 1.0f; return floating; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: return floating != 0.0f; + // CHECK-FIXES: return floating != 0.0F; } void implicitConversionToBoolFromLiterals() { @@ -354,7 +354,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTaking<bool>(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0f); + // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0F); functionTaking<bool>(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool' >From 5b451632fda767cbbcc487ef43ce8a3a4f66577c Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 20:31:55 +0100 Subject: [PATCH 03/14] Revert "Update test cases" This reverts commit efb102b86e6ba2d05141040faf31eaaff5886f18. --- .../readability/implicit-bool-conversion.c | 14 +++++----- .../readability/implicit-bool-conversion.cpp | 28 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c index 29869b100894f4..a8c69858f76b61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c @@ -94,7 +94,7 @@ void implicitConversionFromBoolLiterals() { functionTakingUnsignedLong(false); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'bool' -> 'unsigned long' - // CHECK-FIXES: functionTakingUnsignedLong(0U); + // CHECK-FIXES: functionTakingUnsignedLong(0u); functionTakingSignedChar(true); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'bool' -> 'signed char' @@ -102,7 +102,7 @@ void implicitConversionFromBoolLiterals() { functionTakingFloat(false); // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: implicit conversion 'bool' -> 'float' - // CHECK-FIXES: functionTakingFloat(0.0F); + // CHECK-FIXES: functionTakingFloat(0.0f); functionTakingDouble(true); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'bool' -> 'double' @@ -159,12 +159,12 @@ void implicitConversionToBoolSimpleCases() { unsigned long unsignedLong = 10; functionTakingBool(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'unsigned long' -> 'bool' - // CHECK-FIXES: functionTakingBool(unsignedLong != 0U); + // CHECK-FIXES: functionTakingBool(unsignedLong != 0u); float floating = 0.0f; functionTakingBool(floating); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTakingBool(floating != 0.0F); + // CHECK-FIXES: functionTakingBool(floating != 0.0f); double doubleFloating = 1.0f; functionTakingBool(doubleFloating); @@ -193,7 +193,7 @@ void implicitConversionToBoolInSingleExpressions() { bool boolComingFromFloat; boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: boolComingFromFloat = (floating != 0.0F); + // CHECK-FIXES: boolComingFromFloat = (floating != 0.0f); signed char character = 'a'; bool boolComingFromChar; @@ -231,7 +231,7 @@ bool implicitConversionToBoolInReturnValue() { float floating = 1.0f; return floating; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: return floating != 0.0F; + // CHECK-FIXES: return floating != 0.0f; } void implicitConversionToBoolFromLiterals() { @@ -287,7 +287,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTakingBool(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0F); + // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0f); functionTakingBool(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'double' -> 'bool' diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp index f3c4481a7cc430..d6e7dcc4d8867b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp @@ -98,7 +98,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<unsigned long>(false); // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'bool' -> 'unsigned long' - // CHECK-FIXES: functionTaking<unsigned long>(0U); + // CHECK-FIXES: functionTaking<unsigned long>(0u); functionTaking<signed char>(true); // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: implicit conversion 'bool' -> 'signed char' @@ -106,7 +106,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<float>(false); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'bool' -> 'float' - // CHECK-FIXES: functionTaking<float>(0.0F); + // CHECK-FIXES: functionTaking<float>(0.0f); functionTaking<double>(true); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: implicit conversion 'bool' -> 'double' @@ -177,14 +177,14 @@ void implicitConversionToBoolSimpleCases() { unsigned long unsignedLong = 10; functionTaking<bool>(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'unsigned long' -> 'bool' - // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0U); + // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0u); - float floating = 0.0F; + float floating = 0.0f; functionTaking<bool>(floating); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTaking<bool>(floating != 0.0F); + // CHECK-FIXES: functionTaking<bool>(floating != 0.0f); - double doubleFloating = 1.0F; + double doubleFloating = 1.0f; functionTaking<bool>(doubleFloating); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool' // CHECK-FIXES: functionTaking<bool>(doubleFloating != 0.0); @@ -211,10 +211,10 @@ void implicitConversionToBoolInSingleExpressions() { // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'int' -> 'bool' // CHECK-FIXES: bool boolComingFromInt = integer != 0; - float floating = 10.0F; + float floating = 10.0f; bool boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0F; + // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0f; signed char character = 'a'; bool boolComingFromChar = character; @@ -239,7 +239,7 @@ void implicitConversionToBoolInComplexExpressions() { float floating = 0.2f; bool boolComingFromFloating = floating - 0.3f || boolean; // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0F) || boolean; + // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0f) || boolean; double doubleFloating = 0.3; bool boolComingFromDoubleFloating = (doubleFloating - 0.4) && boolean; @@ -253,10 +253,10 @@ void implicitConversionInNegationExpressions() { // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: implicit conversion 'int' -> 'bool' // CHECK-FIXES: bool boolComingFromNegatedInt = integer == 0; - float floating = 10.0F; + float floating = 10.0f; bool boolComingFromNegatedFloat = ! floating; // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0F; + // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0f; signed char character = 'a'; bool boolComingFromNegatedChar = (! character); @@ -283,7 +283,7 @@ void implicitConversionToBoolInControlStatements() { float floating = 0.3f; while (floating) {} // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: while (floating != 0.0F) {} + // CHECK-FIXES: while (floating != 0.0f) {} double doubleFloating = 0.4; do {} while (doubleFloating); @@ -295,7 +295,7 @@ bool implicitConversionToBoolInReturnValue() { float floating = 1.0f; return floating; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: return floating != 0.0F; + // CHECK-FIXES: return floating != 0.0f; } void implicitConversionToBoolFromLiterals() { @@ -354,7 +354,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTaking<bool>(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' - // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0F); + // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0f); functionTaking<bool>(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool' >From e1c06801e4e4547880fe181fdbc5e4c19c8a40bc Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 21:39:49 +0100 Subject: [PATCH 04/14] Add option `UseUpperCaseSuffix` to implicit-bool-conversion check --- .../ImplicitBoolConversionCheck.cpp | 46 +++++++++++++------ .../readability/ImplicitBoolConversionCheck.h | 1 + 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 258cec80dd0bac..883d16a3325e5c 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -39,14 +39,22 @@ AST_MATCHER(Stmt, isNULLMacroExpansion) { } StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, - QualType Type, - ASTContext &Context) { + QualType Type, ASTContext &Context, + bool UseUpperCaseSuffix) { switch (CastExprKind) { - case CK_IntegralToBoolean: - return Type->isUnsignedIntegerType() ? "0U" : "0"; + case CK_IntegralToBoolean: { + if (Type->isUnsignedIntegerType()) { + return UseUpperCaseSuffix ? "0U" : "0u"; + } + return "0"; + } - case CK_FloatingToBoolean: - return Context.hasSameType(Type, Context.FloatTy) ? "0.0F" : "0.0"; + case CK_FloatingToBoolean: { + if (Context.hasSameType(Type, Context.FloatTy)) { + return UseUpperCaseSuffix ? "0.0F" : "0.0f"; + } + return "0.0"; + } case CK_PointerToBoolean: case CK_MemberPointerToBoolean: // Fall-through on purpose. @@ -66,7 +74,7 @@ bool isUnaryLogicalNotOperator(const Stmt *Statement) { void fixGenericExprCastToBool(DiagnosticBuilder &Diag, const ImplicitCastExpr *Cast, const Stmt *Parent, - ASTContext &Context) { + ASTContext &Context, bool UseUpperCaseSuffix) { // In case of expressions like (! integer), we should remove the redundant not // operator and use inverted comparison (integer == 0). bool InvertComparison = @@ -113,7 +121,7 @@ void fixGenericExprCastToBool(DiagnosticBuilder &Diag, } EndLocInsertion += getZeroLiteralToCompareWithForType( - Cast->getCastKind(), SubExpr->getType(), Context); + Cast->getCastKind(), SubExpr->getType(), Context, UseUpperCaseSuffix); if (NeedOuterParens) { EndLocInsertion += ")"; @@ -192,7 +200,8 @@ void fixGenericExprCastFromBool(DiagnosticBuilder &Diag, } StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, - QualType DestType, ASTContext &Context) { + QualType DestType, ASTContext &Context, + bool UseUpperCaseSuffix) { // Prior to C++11, false literal could be implicitly converted to pointer. if (!Context.getLangOpts().CPlusPlus11 && (DestType->isPointerType() || DestType->isMemberPointerType()) && @@ -202,13 +211,19 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, if (DestType->isFloatingType()) { if (Context.hasSameType(DestType, Context.FloatTy)) { - return BoolLiteral->getValue() ? "1.0F" : "0.0F"; + if (BoolLiteral->getValue()) { + return UseUpperCaseSuffix ? "1.0F" : "1.0f"; + } + return UseUpperCaseSuffix ? "0.0F" : "0.0f"; } return BoolLiteral->getValue() ? "1.0" : "0.0"; } if (DestType->isUnsignedIntegerType()) { - return BoolLiteral->getValue() ? "1U" : "0U"; + if (BoolLiteral->getValue()) { + return UseUpperCaseSuffix ? "1U" : "1u"; + } + return UseUpperCaseSuffix ? "0U" : "0u"; } return BoolLiteral->getValue() ? "1" : "0"; } @@ -248,12 +263,14 @@ ImplicitBoolConversionCheck::ImplicitBoolConversionCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), AllowIntegerConditions(Options.get("AllowIntegerConditions", false)), - AllowPointerConditions(Options.get("AllowPointerConditions", false)) {} + AllowPointerConditions(Options.get("AllowPointerConditions", false)), + UseUpperCaseSuffix(Options.get("UseUpperCaseSuffix", false)) {} void ImplicitBoolConversionCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions); Options.store(Opts, "AllowPointerConditions", AllowPointerConditions); + Options.store(Opts, "UseUpperCaseSuffix", UseUpperCaseSuffix); } void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) { @@ -378,7 +395,7 @@ void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast, if (!EquivalentLiteral.empty()) { Diag << tooling::fixit::createReplacement(*Cast, EquivalentLiteral); } else { - fixGenericExprCastToBool(Diag, Cast, Parent, Context); + fixGenericExprCastToBool(Diag, Cast, Parent, Context, UseUpperCaseSuffix); } } @@ -393,7 +410,8 @@ void ImplicitBoolConversionCheck::handleCastFromBool( if (const auto *BoolLiteral = dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr()->IgnoreParens())) { Diag << tooling::fixit::createReplacement( - *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context)); + *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context, + UseUpperCaseSuffix)); } else { fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString()); } diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h index 9defec91e2f78d..a07d5c16c49946 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h @@ -36,6 +36,7 @@ class ImplicitBoolConversionCheck : public ClangTidyCheck { const bool AllowIntegerConditions; const bool AllowPointerConditions; + const bool UseUpperCaseSuffix; }; } // namespace clang::tidy::readability >From 51524c1fc7a0d3850bb646da73162d244e55a99f Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 21:40:16 +0100 Subject: [PATCH 05/14] Update test cases to test the new `UpperCaseSuffix` option --- .../readability/implicit-bool-conversion.c | 10 ++++++++++ .../readability/implicit-bool-conversion.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c index a8c69858f76b61..a058eb88e514b6 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c @@ -1,4 +1,8 @@ // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t -- -- -std=c23 +// RUN: %check_clang_tidy -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-implicit-bool-conversion.UseUpperCaseSuffix: true \ +// RUN: }}' -- -std=c23 #undef NULL #define NULL 0L @@ -95,6 +99,7 @@ void implicitConversionFromBoolLiterals() { functionTakingUnsignedLong(false); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'bool' -> 'unsigned long' // CHECK-FIXES: functionTakingUnsignedLong(0u); + // CHECK-FIXES-UPPER-CASE: functionTakingUnsignedLong(0U); functionTakingSignedChar(true); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: implicit conversion 'bool' -> 'signed char' @@ -103,6 +108,7 @@ void implicitConversionFromBoolLiterals() { functionTakingFloat(false); // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: implicit conversion 'bool' -> 'float' // CHECK-FIXES: functionTakingFloat(0.0f); + // CHECK-FIXES-UPPER-CASE: functionTakingFloat(0.0F); functionTakingDouble(true); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'bool' -> 'double' @@ -160,11 +166,13 @@ void implicitConversionToBoolSimpleCases() { functionTakingBool(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'unsigned long' -> 'bool' // CHECK-FIXES: functionTakingBool(unsignedLong != 0u); + // CHECK-FIXES-UPPER-CASE: functionTakingBool(unsignedLong != 0U); float floating = 0.0f; functionTakingBool(floating); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: functionTakingBool(floating != 0.0f); + // CHECK-FIXES-UPPER-CASE: functionTakingBool(floating != 0.0F); double doubleFloating = 1.0f; functionTakingBool(doubleFloating); @@ -194,6 +202,7 @@ void implicitConversionToBoolInSingleExpressions() { boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: boolComingFromFloat = (floating != 0.0f); + // CHECK-FIXES-UPPER-CASE: boolComingFromFloat = (floating != 0.0F); signed char character = 'a'; bool boolComingFromChar; @@ -288,6 +297,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTakingBool(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: functionTakingBool((-0.0f) != 0.0f); + // CHECK-FIXES-UPPER-CASE: functionTakingBool((-0.0f) != 0.0F); functionTakingBool(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: implicit conversion 'double' -> 'bool' diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp index d6e7dcc4d8867b..f24f1c99907caf 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp @@ -1,4 +1,8 @@ // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t +// RUN: %check_clang_tidy -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \ +// RUN: -config='{CheckOptions: { \ +// RUN: readability-implicit-bool-conversion.UseUpperCaseSuffix: true \ +// RUN: }}' // We need NULL macro, but some buildbots don't like including <cstddef> header // This is a portable way of getting it to work @@ -99,6 +103,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<unsigned long>(false); // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'bool' -> 'unsigned long' // CHECK-FIXES: functionTaking<unsigned long>(0u); + // CHECK-FIXES-UPPER-CASE: functionTaking<unsigned long>(0U); functionTaking<signed char>(true); // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: implicit conversion 'bool' -> 'signed char' @@ -107,6 +112,7 @@ void implicitConversionFromBoolLiterals() { functionTaking<float>(false); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: implicit conversion 'bool' -> 'float' // CHECK-FIXES: functionTaking<float>(0.0f); + // CHECK-FIXES-UPPER-CASE: functionTaking<float>(0.0F); functionTaking<double>(true); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: implicit conversion 'bool' -> 'double' @@ -178,11 +184,13 @@ void implicitConversionToBoolSimpleCases() { functionTaking<bool>(unsignedLong); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'unsigned long' -> 'bool' // CHECK-FIXES: functionTaking<bool>(unsignedLong != 0u); + // CHECK-FIXES-UPPER-CASE: functionTaking<bool>(unsignedLong != 0U); float floating = 0.0f; functionTaking<bool>(floating); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: functionTaking<bool>(floating != 0.0f); + // CHECK-FIXES-UPPER-CASE: functionTaking<bool>(floating != 0.0F); double doubleFloating = 1.0f; functionTaking<bool>(doubleFloating); @@ -215,6 +223,7 @@ void implicitConversionToBoolInSingleExpressions() { bool boolComingFromFloat = floating; // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: bool boolComingFromFloat = floating != 0.0f; + // CHECK-FIXES-UPPER-CASE: bool boolComingFromFloat = floating != 0.0F; signed char character = 'a'; bool boolComingFromChar = character; @@ -240,6 +249,7 @@ void implicitConversionToBoolInComplexExpressions() { bool boolComingFromFloating = floating - 0.3f || boolean; // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: bool boolComingFromFloating = ((floating - 0.3f) != 0.0f) || boolean; + // CHECK-FIXES-UPPER-CASE: bool boolComingFromFloating = ((floating - 0.3f) != 0.0F) || boolean; double doubleFloating = 0.3; bool boolComingFromDoubleFloating = (doubleFloating - 0.4) && boolean; @@ -257,6 +267,7 @@ void implicitConversionInNegationExpressions() { bool boolComingFromNegatedFloat = ! floating; // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: bool boolComingFromNegatedFloat = floating == 0.0f; + // CHECK-FIXES-UPPER-CASE: bool boolComingFromNegatedFloat = floating == 0.0F; signed char character = 'a'; bool boolComingFromNegatedChar = (! character); @@ -284,6 +295,7 @@ void implicitConversionToBoolInControlStatements() { while (floating) {} // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: while (floating != 0.0f) {} + // CHECK-FIXES-UPPER-CASE: while (floating != 0.0F) {} double doubleFloating = 0.4; do {} while (doubleFloating); @@ -296,6 +308,7 @@ bool implicitConversionToBoolInReturnValue() { return floating; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: return floating != 0.0f; + // CHECK-FIXES-UPPER-CASE: return floating != 0.0F; } void implicitConversionToBoolFromLiterals() { @@ -355,6 +368,7 @@ void implicitConversionToBoolFromUnaryMinusAndZeroLiterals() { functionTaking<bool>(-0.0f); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'float' -> 'bool' // CHECK-FIXES: functionTaking<bool>((-0.0f) != 0.0f); + // CHECK-FIXES-UPPER-CASE: functionTaking<bool>((-0.0f) != 0.0F); functionTaking<bool>(-0.0); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'double' -> 'bool' >From fba6e2ec734882fbf7e10f5cf657fac83bba3865 Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 21:47:24 +0100 Subject: [PATCH 06/14] Update ReleaseNotes.rst with the new `UseUpperCaseSuffix` option --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1b025e8f90f7ba..acba19b66e0fa6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -116,6 +116,10 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when redundant `get()` is removed. +- Added option `UseUpperCaseSuffix` to :doc:`readablility-implicit-bool-conversion + <clang-tidy/checks/readability/implicit-bool-conversion>` check specify the + case of the explicit literal + Removed checks ^^^^^^^^^^^^^^ >From c4a96063a7f979f3143268d20d302b8f2b4d00e6 Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Tue, 13 Aug 2024 21:57:26 +0100 Subject: [PATCH 07/14] Update Documentation with the new `UseUpperCaseSuffix` option --- .../readability/implicit-bool-conversion.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst index 1ab21ffeb42289..2afd890d5cd3b3 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst @@ -133,3 +133,18 @@ Options When `true`, the check will allow conditional pointer conversions. Default is `false`. + +.. option:: UseUpperCaseSuffix + + When `true`, the check will allow new explicit conversion use an uppercase + suffix when it applies. + +Example +^^^^^^^ + +.. code-block:: c++ + + uint32_t foo; + if (foo) {} + // ^ propose replacement default: if (foo != 0u) {} + // ^ propose replacement with option `UseUpperCaseSuffix`: if (foo != 0U) {} >From bc1da60ed45ace55622204365199fd2fae2fde27 Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Thu, 15 Aug 2024 00:52:40 +0100 Subject: [PATCH 08/14] Resolve review changes --- .../readability/ImplicitBoolConversionCheck.cpp | 12 ++++++------ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++-- .../checks/readability/implicit-bool-conversion.rst | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 883d16a3325e5c..16d626e5995f7f 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -43,9 +43,9 @@ StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, bool UseUpperCaseSuffix) { switch (CastExprKind) { case CK_IntegralToBoolean: { - if (Type->isUnsignedIntegerType()) { + if (Type->isUnsignedIntegerType()) return UseUpperCaseSuffix ? "0U" : "0u"; - } + return "0"; } @@ -211,18 +211,18 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, if (DestType->isFloatingType()) { if (Context.hasSameType(DestType, Context.FloatTy)) { - if (BoolLiteral->getValue()) { + if (BoolLiteral->getValue()) return UseUpperCaseSuffix ? "1.0F" : "1.0f"; - } + return UseUpperCaseSuffix ? "0.0F" : "0.0f"; } return BoolLiteral->getValue() ? "1.0" : "0.0"; } if (DestType->isUnsignedIntegerType()) { - if (BoolLiteral->getValue()) { + if (BoolLiteral->getValue()) return UseUpperCaseSuffix ? "1U" : "1u"; - } + return UseUpperCaseSuffix ? "0U" : "0u"; } return BoolLiteral->getValue() ? "1" : "0"; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index acba19b66e0fa6..c46000ebcfbcbf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -117,8 +117,8 @@ Changes in existing checks remove `->`, when redundant `get()` is removed. - Added option `UseUpperCaseSuffix` to :doc:`readablility-implicit-bool-conversion - <clang-tidy/checks/readability/implicit-bool-conversion>` check specify the - case of the explicit literal + <clang-tidy/checks/readability/implicit-bool-conversion>` check to select the + case of the literal suffix in fixes. Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst index 2afd890d5cd3b3..e54c418c3636f6 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst @@ -136,8 +136,8 @@ Options .. option:: UseUpperCaseSuffix - When `true`, the check will allow new explicit conversion use an uppercase - suffix when it applies. + When `true`, the replacements will use an uppercase literal suffix in the + provided fixes. Example ^^^^^^^ >From b29b034e37275dbad0edb61feba211ce295d2a2f Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Thu, 15 Aug 2024 01:19:43 +0100 Subject: [PATCH 09/14] Change the option name to `UseUpperCaseLiteralSuffix` --- .../ImplicitBoolConversionCheck.cpp | 28 +++++++++---------- .../readability/ImplicitBoolConversionCheck.h | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- .../readability/implicit-bool-conversion.rst | 4 +-- .../readability/implicit-bool-conversion.c | 2 +- .../readability/implicit-bool-conversion.cpp | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 16d626e5995f7f..7ac5ec0b2fdcea 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -40,18 +40,18 @@ AST_MATCHER(Stmt, isNULLMacroExpansion) { StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, QualType Type, ASTContext &Context, - bool UseUpperCaseSuffix) { + bool UseUpperCaseLiteralSuffix) { switch (CastExprKind) { case CK_IntegralToBoolean: { if (Type->isUnsignedIntegerType()) - return UseUpperCaseSuffix ? "0U" : "0u"; + return UseUpperCaseLiteralSuffix ? "0U" : "0u"; return "0"; } case CK_FloatingToBoolean: { if (Context.hasSameType(Type, Context.FloatTy)) { - return UseUpperCaseSuffix ? "0.0F" : "0.0f"; + return UseUpperCaseLiteralSuffix ? "0.0F" : "0.0f"; } return "0.0"; } @@ -74,7 +74,7 @@ bool isUnaryLogicalNotOperator(const Stmt *Statement) { void fixGenericExprCastToBool(DiagnosticBuilder &Diag, const ImplicitCastExpr *Cast, const Stmt *Parent, - ASTContext &Context, bool UseUpperCaseSuffix) { + ASTContext &Context, bool UseUpperCaseLiteralSuffix) { // In case of expressions like (! integer), we should remove the redundant not // operator and use inverted comparison (integer == 0). bool InvertComparison = @@ -121,7 +121,7 @@ void fixGenericExprCastToBool(DiagnosticBuilder &Diag, } EndLocInsertion += getZeroLiteralToCompareWithForType( - Cast->getCastKind(), SubExpr->getType(), Context, UseUpperCaseSuffix); + Cast->getCastKind(), SubExpr->getType(), Context, UseUpperCaseLiteralSuffix); if (NeedOuterParens) { EndLocInsertion += ")"; @@ -201,7 +201,7 @@ void fixGenericExprCastFromBool(DiagnosticBuilder &Diag, StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, QualType DestType, ASTContext &Context, - bool UseUpperCaseSuffix) { + bool UseUpperCaseLiteralSuffix) { // Prior to C++11, false literal could be implicitly converted to pointer. if (!Context.getLangOpts().CPlusPlus11 && (DestType->isPointerType() || DestType->isMemberPointerType()) && @@ -212,18 +212,18 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, if (DestType->isFloatingType()) { if (Context.hasSameType(DestType, Context.FloatTy)) { if (BoolLiteral->getValue()) - return UseUpperCaseSuffix ? "1.0F" : "1.0f"; + return UseUpperCaseLiteralSuffix ? "1.0F" : "1.0f"; - return UseUpperCaseSuffix ? "0.0F" : "0.0f"; + return UseUpperCaseLiteralSuffix ? "0.0F" : "0.0f"; } return BoolLiteral->getValue() ? "1.0" : "0.0"; } if (DestType->isUnsignedIntegerType()) { if (BoolLiteral->getValue()) - return UseUpperCaseSuffix ? "1U" : "1u"; + return UseUpperCaseLiteralSuffix ? "1U" : "1u"; - return UseUpperCaseSuffix ? "0U" : "0u"; + return UseUpperCaseLiteralSuffix ? "0U" : "0u"; } return BoolLiteral->getValue() ? "1" : "0"; } @@ -264,13 +264,13 @@ ImplicitBoolConversionCheck::ImplicitBoolConversionCheck( : ClangTidyCheck(Name, Context), AllowIntegerConditions(Options.get("AllowIntegerConditions", false)), AllowPointerConditions(Options.get("AllowPointerConditions", false)), - UseUpperCaseSuffix(Options.get("UseUpperCaseSuffix", false)) {} + UseUpperCaseLiteralSuffix(Options.get("UseUpperCaseLiteralSuffix", false)) {} void ImplicitBoolConversionCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions); Options.store(Opts, "AllowPointerConditions", AllowPointerConditions); - Options.store(Opts, "UseUpperCaseSuffix", UseUpperCaseSuffix); + Options.store(Opts, "UseUpperCaseLiteralSuffix", UseUpperCaseLiteralSuffix); } void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) { @@ -395,7 +395,7 @@ void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast, if (!EquivalentLiteral.empty()) { Diag << tooling::fixit::createReplacement(*Cast, EquivalentLiteral); } else { - fixGenericExprCastToBool(Diag, Cast, Parent, Context, UseUpperCaseSuffix); + fixGenericExprCastToBool(Diag, Cast, Parent, Context, UseUpperCaseLiteralSuffix); } } @@ -411,7 +411,7 @@ void ImplicitBoolConversionCheck::handleCastFromBool( dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr()->IgnoreParens())) { Diag << tooling::fixit::createReplacement( *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context, - UseUpperCaseSuffix)); + UseUpperCaseLiteralSuffix)); } else { fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString()); } diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h index a07d5c16c49946..5947f7316e67cc 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h @@ -36,7 +36,7 @@ class ImplicitBoolConversionCheck : public ClangTidyCheck { const bool AllowIntegerConditions; const bool AllowPointerConditions; - const bool UseUpperCaseSuffix; + const bool UseUpperCaseLiteralSuffix; }; } // namespace clang::tidy::readability diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index c46000ebcfbcbf..f15fa82a4c3390 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -116,7 +116,7 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when redundant `get()` is removed. -- Added option `UseUpperCaseSuffix` to :doc:`readablility-implicit-bool-conversion +- Added option `UseUpperCaseLiteralSuffix` to :doc:`readablility-implicit-bool-conversion <clang-tidy/checks/readability/implicit-bool-conversion>` check to select the case of the literal suffix in fixes. diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst index e54c418c3636f6..4d6b83d304fcd0 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst @@ -134,7 +134,7 @@ Options When `true`, the check will allow conditional pointer conversions. Default is `false`. -.. option:: UseUpperCaseSuffix +.. option:: UseUpperCaseLiteralSuffix When `true`, the replacements will use an uppercase literal suffix in the provided fixes. @@ -147,4 +147,4 @@ Example uint32_t foo; if (foo) {} // ^ propose replacement default: if (foo != 0u) {} - // ^ propose replacement with option `UseUpperCaseSuffix`: if (foo != 0U) {} + // ^ propose replacement with option `UseUpperCaseLiteralSuffix`: if (foo != 0U) {} diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c index a058eb88e514b6..f3dc32c10d640a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.c @@ -1,7 +1,7 @@ // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t -- -- -std=c23 // RUN: %check_clang_tidy -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \ // RUN: -config='{CheckOptions: { \ -// RUN: readability-implicit-bool-conversion.UseUpperCaseSuffix: true \ +// RUN: readability-implicit-bool-conversion.UseUpperCaseLiteralSuffix: true \ // RUN: }}' -- -std=c23 #undef NULL diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp index f24f1c99907caf..c4b7a77b92f0a5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp @@ -1,7 +1,7 @@ // RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t // RUN: %check_clang_tidy -check-suffix=UPPER-CASE %s readability-implicit-bool-conversion %t -- \ // RUN: -config='{CheckOptions: { \ -// RUN: readability-implicit-bool-conversion.UseUpperCaseSuffix: true \ +// RUN: readability-implicit-bool-conversion.UseUpperCaseLiteralSuffix: true \ // RUN: }}' // We need NULL macro, but some buildbots don't like including <cstddef> header >From 4e0e6c2ce9078d18d1d229bf7569b52df8abc04a Mon Sep 17 00:00:00 2001 From: Ebuka Ezike <yerimy...@gmail.com> Date: Thu, 15 Aug 2024 01:37:53 +0100 Subject: [PATCH 10/14] Fix Clang format --- .../readability/ImplicitBoolConversionCheck.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 7ac5ec0b2fdcea..825455e822bfc5 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -74,7 +74,8 @@ bool isUnaryLogicalNotOperator(const Stmt *Statement) { void fixGenericExprCastToBool(DiagnosticBuilder &Diag, const ImplicitCastExpr *Cast, const Stmt *Parent, - ASTContext &Context, bool UseUpperCaseLiteralSuffix) { + ASTContext &Context, + bool UseUpperCaseLiteralSuffix) { // In case of expressions like (! integer), we should remove the redundant not // operator and use inverted comparison (integer == 0). bool InvertComparison = @@ -121,7 +122,8 @@ void fixGenericExprCastToBool(DiagnosticBuilder &Diag, } EndLocInsertion += getZeroLiteralToCompareWithForType( - Cast->getCastKind(), SubExpr->getType(), Context, UseUpperCaseLiteralSuffix); + Cast->getCastKind(), SubExpr->getType(), Context, + UseUpperCaseLiteralSuffix); if (NeedOuterParens) { EndLocInsertion += ")"; @@ -264,7 +266,8 @@ ImplicitBoolConversionCheck::ImplicitBoolConversionCheck( : ClangTidyCheck(Name, Context), AllowIntegerConditions(Options.get("AllowIntegerConditions", false)), AllowPointerConditions(Options.get("AllowPointerConditions", false)), - UseUpperCaseLiteralSuffix(Options.get("UseUpperCaseLiteralSuffix", false)) {} + UseUpperCaseLiteralSuffix( + Options.get("UseUpperCaseLiteralSuffix", false)) {} void ImplicitBoolConversionCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { @@ -395,7 +398,8 @@ void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast, if (!EquivalentLiteral.empty()) { Diag << tooling::fixit::createReplacement(*Cast, EquivalentLiteral); } else { - fixGenericExprCastToBool(Diag, Cast, Parent, Context, UseUpperCaseLiteralSuffix); + fixGenericExprCastToBool(Diag, Cast, Parent, Context, + UseUpperCaseLiteralSuffix); } } >From 6d349e2cc623e96dc1272493e6e01e3d48b0b17d Mon Sep 17 00:00:00 2001 From: Ezike Ebuka <yerimy...@gmail.com> Date: Fri, 16 Aug 2024 20:42:18 +0100 Subject: [PATCH 11/14] Resolve review changes --- .../ImplicitBoolConversionCheck.cpp | 56 +++++++++---------- clang-tools-extra/docs/ReleaseNotes.rst | 5 +- .../readability/implicit-bool-conversion.rst | 15 +++-- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 825455e822bfc5..98469d025cafdc 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -39,22 +39,14 @@ AST_MATCHER(Stmt, isNULLMacroExpansion) { } StringRef getZeroLiteralToCompareWithForType(CastKind CastExprKind, - QualType Type, ASTContext &Context, - bool UseUpperCaseLiteralSuffix) { + QualType Type, + ASTContext &Context) { switch (CastExprKind) { - case CK_IntegralToBoolean: { - if (Type->isUnsignedIntegerType()) - return UseUpperCaseLiteralSuffix ? "0U" : "0u"; + case CK_IntegralToBoolean: + return Type->isUnsignedIntegerType() ? "0u" : "0"; - return "0"; - } - - case CK_FloatingToBoolean: { - if (Context.hasSameType(Type, Context.FloatTy)) { - return UseUpperCaseLiteralSuffix ? "0.0F" : "0.0f"; - } - return "0.0"; - } + case CK_FloatingToBoolean: + return Context.hasSameType(Type, Context.FloatTy) ? "0.0f" : "0.0"; case CK_PointerToBoolean: case CK_MemberPointerToBoolean: // Fall-through on purpose. @@ -121,9 +113,13 @@ void fixGenericExprCastToBool(DiagnosticBuilder &Diag, EndLocInsertion += " != "; } - EndLocInsertion += getZeroLiteralToCompareWithForType( - Cast->getCastKind(), SubExpr->getType(), Context, - UseUpperCaseLiteralSuffix); + const StringRef ZeroLiteral = getZeroLiteralToCompareWithForType( + Cast->getCastKind(), SubExpr->getType(), Context); + + if (UseUpperCaseLiteralSuffix) + EndLocInsertion += ZeroLiteral.upper(); + else + EndLocInsertion += ZeroLiteral; if (NeedOuterParens) { EndLocInsertion += ")"; @@ -202,8 +198,7 @@ void fixGenericExprCastFromBool(DiagnosticBuilder &Diag, } StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, - QualType DestType, ASTContext &Context, - bool UseUpperCaseLiteralSuffix) { + QualType DestType, ASTContext &Context) { // Prior to C++11, false literal could be implicitly converted to pointer. if (!Context.getLangOpts().CPlusPlus11 && (DestType->isPointerType() || DestType->isMemberPointerType()) && @@ -213,19 +208,13 @@ StringRef getEquivalentForBoolLiteral(const CXXBoolLiteralExpr *BoolLiteral, if (DestType->isFloatingType()) { if (Context.hasSameType(DestType, Context.FloatTy)) { - if (BoolLiteral->getValue()) - return UseUpperCaseLiteralSuffix ? "1.0F" : "1.0f"; - - return UseUpperCaseLiteralSuffix ? "0.0F" : "0.0f"; + return BoolLiteral->getValue() ? "1.0f" : "0.0f"; } return BoolLiteral->getValue() ? "1.0" : "0.0"; } if (DestType->isUnsignedIntegerType()) { - if (BoolLiteral->getValue()) - return UseUpperCaseLiteralSuffix ? "1U" : "1u"; - - return UseUpperCaseLiteralSuffix ? "0U" : "0u"; + return BoolLiteral->getValue() ? "1u" : "0u"; } return BoolLiteral->getValue() ? "1" : "0"; } @@ -413,9 +402,16 @@ void ImplicitBoolConversionCheck::handleCastFromBool( if (const auto *BoolLiteral = dyn_cast<CXXBoolLiteralExpr>(Cast->getSubExpr()->IgnoreParens())) { - Diag << tooling::fixit::createReplacement( - *Cast, getEquivalentForBoolLiteral(BoolLiteral, DestType, Context, - UseUpperCaseLiteralSuffix)); + + const auto EquivalentForBoolLiteral = + getEquivalentForBoolLiteral(BoolLiteral, DestType, Context); + if (UseUpperCaseLiteralSuffix) { + Diag << tooling::fixit::createReplacement( + *Cast, EquivalentForBoolLiteral.upper()); + } else { + Diag << tooling::fixit::createReplacement(*Cast, + EquivalentForBoolLiteral); + } } else { fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString()); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index f15fa82a4c3390..35f61224d7bb1b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -116,8 +116,9 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when redundant `get()` is removed. -- Added option `UseUpperCaseLiteralSuffix` to :doc:`readablility-implicit-bool-conversion - <clang-tidy/checks/readability/implicit-bool-conversion>` check to select the +- Improved :doc:`readablility-implicit-bool-conversion + <clang-tidy/checks/readability/implicit-bool-conversion>` check + Added option `UseUpperCaseLiteralSuffix` to to select the case of the literal suffix in fixes. Removed checks diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst index 4d6b83d304fcd0..be56beebe394d8 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst @@ -137,14 +137,13 @@ Options .. option:: UseUpperCaseLiteralSuffix When `true`, the replacements will use an uppercase literal suffix in the - provided fixes. + provided fixes. Default a lowercase literal suffix is used. -Example -^^^^^^^ + Example -.. code-block:: c++ + .. code-block:: c++ - uint32_t foo; - if (foo) {} - // ^ propose replacement default: if (foo != 0u) {} - // ^ propose replacement with option `UseUpperCaseLiteralSuffix`: if (foo != 0U) {} + uint32_t foo; + if (foo) {} + // ^ propose replacement default: if (foo != 0u) {} + // ^ propose replacement with option `UseUpperCaseLiteralSuffix`: if (foo != 0U) {} >From ac8eca4ce6e32865fa45fcaf97e6f12d0b176d1f Mon Sep 17 00:00:00 2001 From: Ezike Ebuka <yerimy...@gmail.com> Date: Sun, 18 Aug 2024 23:31:16 +0100 Subject: [PATCH 12/14] Resolve review changes --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 35f61224d7bb1b..d7f6bee550e474 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -118,7 +118,7 @@ Changes in existing checks - Improved :doc:`readablility-implicit-bool-conversion <clang-tidy/checks/readability/implicit-bool-conversion>` check - Added option `UseUpperCaseLiteralSuffix` to to select the + by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. Removed checks >From d1b19f9e75e081270ef2f38a668455fb409ee047 Mon Sep 17 00:00:00 2001 From: Ezike Ebuka <yerimy...@gmail.com> Date: Sun, 18 Aug 2024 23:32:24 +0100 Subject: [PATCH 13/14] Resolve review changes --- .../clang-tidy/checks/readability/implicit-bool-conversion.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst index be56beebe394d8..88cff387f4c165 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/implicit-bool-conversion.rst @@ -137,7 +137,7 @@ Options .. option:: UseUpperCaseLiteralSuffix When `true`, the replacements will use an uppercase literal suffix in the - provided fixes. Default a lowercase literal suffix is used. + provided fixes. Default is `false`. Example >From 1bf9179ec454df0287496dccafe8e8be73b77352 Mon Sep 17 00:00:00 2001 From: Ezike Ebuka <yerimy...@gmail.com> Date: Sun, 25 Aug 2024 16:49:48 +0100 Subject: [PATCH 14/14] Resolve: Order the release notes Order the release notes. --- .../readability/ImplicitBoolConversionCheck.cpp | 6 +++--- clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp index 98469d025cafdc..968a4a55a6d798 100644 --- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp @@ -405,13 +405,13 @@ void ImplicitBoolConversionCheck::handleCastFromBool( const auto EquivalentForBoolLiteral = getEquivalentForBoolLiteral(BoolLiteral, DestType, Context); - if (UseUpperCaseLiteralSuffix) { + if (UseUpperCaseLiteralSuffix) Diag << tooling::fixit::createReplacement( *Cast, EquivalentForBoolLiteral.upper()); - } else { + else Diag << tooling::fixit::createReplacement(*Cast, EquivalentForBoolLiteral); - } + } else { fixGenericExprCastFromBool(Diag, Cast, Context, DestType.getAsString()); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d7f6bee550e474..b001a6ad446695 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -112,15 +112,15 @@ Changes in existing checks <clang-tidy/checks/modernize/use-std-print>` check to support replacing member function calls too. -- Improved :doc:`readability-redundant-smartptr-get - <clang-tidy/checks/readability/redundant-smartptr-get>` check to - remove `->`, when redundant `get()` is removed. - - Improved :doc:`readablility-implicit-bool-conversion <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding the option `UseUpperCaseLiteralSuffix` to select the case of the literal suffix in fixes. +- Improved :doc:`readability-redundant-smartptr-get + <clang-tidy/checks/readability/redundant-smartptr-get>` check to + remove `->`, when redundant `get()` is removed. + Removed checks ^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits