https://github.com/JOE1994 created https://github.com/llvm/llvm-project/pull/92200
Made the following decisions for consistency with `gcc 14.1`: * Add warning under -Wparentheses * Set the warning to DefaultIgnore, although -Wparentheses is enabled by default * This warning is only issued when -Wparentheses is explicitly enabled (via -Wparentheses or -Wall) Closes #20456 >From 2c7f9a083c129df70a79d019286b6a29643a8973 Mon Sep 17 00:00:00 2001 From: Youngsuk Kim <youngsuk....@hpe.com> Date: Tue, 14 May 2024 17:42:59 -0500 Subject: [PATCH] [clang][Sema] Warn consecutive builtin comparisons in an expression Made the following decisions for consistency with `gcc 14.1`: * Add warning under -Wparentheses * Set the warning to DefaultIgnore, although -Wparentheses is enabled by default * This warning is only issued when -Wparentheses is explicitly enabled (via -Wparentheses or -Wall) Closes #20456 --- clang/docs/ReleaseNotes.rst | 3 +++ clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++ clang/lib/Sema/SemaExpr.cpp | 5 +++++ clang/test/Sema/parentheses.cpp | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index ae699ebfc6038..13d58e69aeeb7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -487,6 +487,9 @@ Improvements to Clang's diagnostics } }; +- Clang emits a ``-Wparentheses`` warning for expressions with consecutive comparisons like ``x < y < z``. + It was made a ``-Wparentheses`` warning to be consistent with gcc. + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 6100fba510059..612043f410890 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6911,6 +6911,10 @@ def warn_precedence_bitwise_conditional : Warning< def note_precedence_conditional_first : Note< "place parentheses around the '?:' expression to evaluate it first">; +def warn_consecutive_comparison : Warning< + "comparisons like 'X<=Y<=Z' don't have their mathematical meaning">, + InGroup<Parentheses>, DefaultIgnore; + def warn_enum_constant_in_bool_context : Warning< "converting the enum constant to a boolean">, InGroup<IntInBoolContext>, DefaultIgnore; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index ec84798e4ce60..fab34f4fa3e14 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -14878,6 +14878,11 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, case BO_GT: ConvertHalfVec = true; ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc); + + if (const auto *BI = dyn_cast<BinaryOperator>(LHSExpr)) + if (BI->isComparisonOp()) + Diag(OpLoc, diag::warn_consecutive_comparison); + break; case BO_EQ: case BO_NE: diff --git a/clang/test/Sema/parentheses.cpp b/clang/test/Sema/parentheses.cpp index 324d9b5f1e414..8e546461fb643 100644 --- a/clang/test/Sema/parentheses.cpp +++ b/clang/test/Sema/parentheses.cpp @@ -215,3 +215,10 @@ namespace PR20735 { // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")" } } + +void consecutive_builtin_compare(int x, int y, int z) { + (void)(x < y < z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}} + (void)(x < y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}} + (void)(x < y <= z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}} + (void)(x <= y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits