Skip to content

Commit 3638aa2

Browse files
AmrDeveloperjph-13
authored andcommitted
[Clang][diagnostics] Improve the diagnostics for chained comparisons (llvm#129285)
Improve the diagnostics for chained comparisons to report actual expressions and operators Fixes llvm#129069
1 parent a60975d commit 3638aa2

File tree

9 files changed

+16
-13
lines changed

9 files changed

+16
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ Improvements to Clang's diagnostics
229229
:doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
230230
feature will be default-enabled with ``-Wthread-safety`` in a future release.
231231

232+
- Improve the diagnostics for chained comparisons to report actual expressions and operators (#GH129069).
233+
232234
- Improve the diagnostics for shadows template parameter to report correct location (#GH129060).
233235

234236
Improvements to Clang's time-trace

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7160,7 +7160,7 @@ def note_precedence_conditional_first : Note<
71607160
"place parentheses around the '?:' expression to evaluate it first">;
71617161

71627162
def warn_consecutive_comparison : Warning<
7163-
"comparisons like 'X<=Y<=Z' don't have their mathematical meaning">,
7163+
"chained comparison 'X %0 Y %1 Z' does not behave the same as a mathematical expression">,
71647164
InGroup<Parentheses>, DefaultError;
71657165

71667166
def warn_enum_constant_in_bool_context : Warning<

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14924,7 +14924,8 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
1492414924

1492514925
if (const auto *BI = dyn_cast<BinaryOperator>(LHSExpr);
1492614926
BI && BI->isComparisonOp())
14927-
Diag(OpLoc, diag::warn_consecutive_comparison);
14927+
Diag(OpLoc, diag::warn_consecutive_comparison)
14928+
<< BI->getOpcodeStr() << BinaryOperator::getOpcodeStr(Opc);
1492814929

1492914930
break;
1493014931
case BO_EQ:

clang/test/Sema/bool-compare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void f(int x, int y, int z) {
8585
if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with boolean expression is always true}}
8686

8787
if ((a<y) == z) {} // no warning
88-
if (a>y<z) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
88+
if (a>y<z) {} // expected-error {{chained comparison 'X > Y < Z' does not behave the same as a mathematical expression}}
8989
if ((a<y) > z) {} // no warning
9090
if((a<y)>(z<y)) {} // no warning
9191
if((a<y)==(z<y)){} // no warning
@@ -145,7 +145,7 @@ void f(int x, int y, int z) {
145145
if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with boolean expression is always true}}
146146

147147
if (z ==(a<y)) {} // no warning
148-
if (z<a>y) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
148+
if (z<a>y) {} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
149149
if (z > (a<y)) {} // no warning
150150
if((z<y)>(a<y)) {} // no warning
151151
if((z<y)==(a<y)){} // no warning

clang/test/Sema/parentheses.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ namespace PR20735 {
217217
}
218218

219219
void consecutive_builtin_compare(int x, int y, int z) {
220-
(void)(x < y < z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
221-
(void)(x < y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
222-
(void)(x < y <= z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
223-
(void)(x <= y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
220+
(void)(x < y < z); // expected-warning {{chained comparison 'X < Y < Z' does not behave the same as a mathematical expression}}
221+
(void)(x < y > z); // expected-warning {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
222+
(void)(x < y <= z); // expected-warning {{chained comparison 'X < Y <= Z' does not behave the same as a mathematical expression}}
223+
(void)(x <= y > z); // expected-warning {{chained comparison 'X <= Y > Z' does not behave the same as a mathematical expression}}
224224
(void)((x < y) < z); // no-warning
225225
(void)((x < y) >= z); // no-warning
226226

clang/test/SemaCXX/bool-compare.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void f(int x, int y, int z) {
9898
if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
9999

100100
if ((a<y) == z) {} // no warning
101-
if (a>y<z) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
101+
if (a>y<z) {} // expected-error {{chained comparison 'X > Y < Z' does not behave the same as a mathematical expression}}
102102
if ((a<y) > z) {} // no warning
103103
if((a<y)>(z<y)) {} // no warning
104104
if((a<y)==(z<y)){} // no warning
@@ -159,7 +159,7 @@ void f(int x, int y, int z) {
159159
if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
160160

161161
if (z ==(a<y)) {} // no warning
162-
if (z<a>y) {} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
162+
if (z<a>y) {} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
163163
if (z > (a<y)) {} // no warning
164164
if((z<y)>(a<y)) {} // no warning
165165
if((z<y)==(a<y)){} // no warning

clang/test/SemaCXX/cxx2a-adl-only-template-id.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int e = h<0>(q); // ok, found by unqualified lookup
2727
void fn() {
2828
f<0>(q);
2929
int f;
30-
f<0>(q); // expected-error {{invalid operands to binary expression}} // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
30+
f<0>(q); // expected-error {{invalid operands to binary expression}} // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
3131
}
3232

3333
void disambig() {

clang/test/SemaTemplate/typo-dependent-name.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct X : Base<T> {
2020
bool f(T other) {
2121
// A pair of comparisons; 'inner' is a dependent name so can't be assumed
2222
// to be a template.
23-
return this->inner < other > ::z; // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
23+
return this->inner < other > ::z; // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
2424
}
2525
};
2626

clang/test/SemaTemplate/typo-template-name.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace InExpr {
3636

3737
// These are valid expressions.
3838
foo<foo; // expected-warning {{self-comparison}}
39-
foo<int()>(0); // expected-error {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
39+
foo<int()>(0); // expected-error {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}
4040
foo<int(), true>(false);
4141
foo<Base{}.n;
4242
}

0 commit comments

Comments
 (0)