https://github.com/Da-Viper updated https://github.com/llvm/llvm-project/pull/102831
>From 8a4f6af9fc1f44c2f8b5fd3693ca14eaf776fd02 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/12] [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 efb102b86e6ba2d05141040faf31eaaff5886f18 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/12] 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 19cba070d37c55fca1d1c872ffdb8fb0dfa9e281 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/12] 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 e38a7ad0c315bfc93c36a370642b1837466c6f44 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/12] 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 d8568e42e8a38ffdd45601c23bb00301a5ec7551 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/12] 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 b06fb9a09609b766102f863409559a67f357987a 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/12] 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 b72d109b3d3938..d5002bd81a7b5c 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -108,6 +108,10 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when reduntant `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 b6d0537d339071fb91b7359d0c6609810e30c623 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/12] 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 27d059c86418c16ccafd77e0fa5e3cb2ef9906ff 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/12] 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 d5002bd81a7b5c..27669fb22fb750 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -109,8 +109,8 @@ Changes in existing checks remove `->`, when reduntant `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 214e252a23fe6473aa21ccfbf8d098e1dbac5eb0 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/12] 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 27669fb22fb750..123c7ec4ab7ae0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -108,7 +108,7 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when reduntant `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 44189b9a2f41c5898c6526589071508f8639056d 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/12] 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 96e1230623037ff8b2c90e72ce434dfa33caf9f2 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/12] 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 123c7ec4ab7ae0..90dc926bcc5811 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -108,8 +108,9 @@ Changes in existing checks <clang-tidy/checks/readability/redundant-smartptr-get>` check to remove `->`, when reduntant `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 15fd44bb8a9f8258420d6a75eaaf48c3eac38fb6 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/12] 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 90dc926bcc5811..ee9ffc6132ead9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,7 +110,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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits