Skip to content

Commit 0e86d3e

Browse files
authored
[clang] Print static_assert values of arithmetic binary operators (#71671)
These are actually quite useful to print.
1 parent 2fda8ca commit 0e86d3e

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,24 @@ Improvements to Clang's diagnostics
478478
with GCC.
479479
- Clang will warn on deprecated specializations used in system headers when their instantiation
480480
is caused by user code.
481+
- Clang will now print ``static_assert`` failure details for arithmetic binary operators.
482+
Example:
483+
484+
.. code-block:: cpp
485+
486+
static_assert(1 << 4 == 15);
487+
488+
will now print:
489+
490+
.. code-block:: text
491+
492+
error: static assertion failed due to requirement '1 << 4 == 15'
493+
48 | static_assert(1 << 4 == 15);
494+
| ^~~~~~~~~~~~
495+
note: expression evaluates to '16 == 15'
496+
48 | static_assert(1 << 4 == 15);
497+
| ~~~~~~~^~~~~
498+
481499
482500
Improvements to Clang's time-trace
483501
----------------------------------

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17218,10 +17218,10 @@ static bool UsefulToPrintExpr(const Expr *E) {
1721817218
if (const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
1721917219
return UsefulToPrintExpr(UnaryOp->getSubExpr());
1722017220

17221-
// Ignore nested binary operators. This could be a FIXME for improvements
17222-
// to the diagnostics in the future.
17223-
if (isa<BinaryOperator>(E))
17224-
return false;
17221+
// Only print nested arithmetic operators.
17222+
if (const auto *BO = dyn_cast<BinaryOperator>(E))
17223+
return (BO->isShiftOp() || BO->isAdditiveOp() || BO->isMultiplicativeOp() ||
17224+
BO->isBitwiseOp());
1722517225

1722617226
return true;
1722717227
}

clang/test/SemaCXX/complex-folding.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// Test the constant folding of builtin complex numbers.
44

55
static_assert((0.0 + 0.0j) == (0.0 + 0.0j));
6-
static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // expected-error {{static assertion}}
6+
static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // expected-error {{static assertion}} \
7+
// expected-note {{evaluates to}}
78

89
static_assert((0.0 + 0.0j) == 0.0);
910
static_assert(0.0 == (0.0 + 0.0j));
@@ -14,21 +15,29 @@ static_assert(0.0 != 1.0j);
1415

1516
// Walk around the complex plane stepping between angular differences and
1617
// equality.
17-
static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // expected-error {{static assertion}}
18+
static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // expected-error {{static assertion}} \
19+
// expected-note {{evaluates to}}
1820
static_assert((1.0 + 0.0j) == (1.0 + 0.0j));
19-
static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // expected-error {{static assertion}}
21+
static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // expected-error {{static assertion}} \
22+
// expected-note {{evaluates to}}
2023
static_assert((1.0 + 1.0j) == (1.0 + 1.0j));
21-
static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // expected-error {{static assertion}}
24+
static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // expected-error {{static assertion}} \
25+
// expected-note {{evaluates to}}
2226
static_assert((0.0 + 1.0j) == (0.0 + 1.0j));
23-
static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // expected-error {{static assertion}}
27+
static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // expected-error {{static assertion}} \
28+
// expected-note {{evaluates to}}
2429
static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j));
25-
static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // expected-error {{static assertion}}
30+
static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // expected-error {{static assertion}} \
31+
// expected-note {{evaluates to}}
2632
static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j));
27-
static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // expected-error {{static assertion}}
33+
static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // expected-error {{static assertion}} \
34+
// expected-note {{evaluates to}}
2835
static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j));
29-
static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // expected-error {{static assertion}}
36+
static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // expected-error {{static assertion}} \
37+
// expected-note {{evaluates to}}
3038
static_assert((0.0 - 1.0j) == (0.0 - 1.0j));
31-
static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // expected-error {{static assertion}}
39+
static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // expected-error {{static assertion}} \
40+
// expected-note {{evaluates to}}
3241
static_assert((1.0 - 1.0j) == (1.0 - 1.0j));
3342

3443
// Test basic mathematical folding of both complex and real operands.

clang/test/SemaCXX/static-assert.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,14 @@ namespace Diagnostics {
351351
""
352352
);
353353

354+
static_assert(1 + 1 != 2, ""); // expected-error {{failed}} \
355+
// expected-note {{evaluates to '2 != 2'}}
356+
static_assert(1 - 1 == 2, ""); // expected-error {{failed}} \
357+
// expected-note {{evaluates to '0 == 2'}}
358+
static_assert(1 * 1 == 2, ""); // expected-error {{failed}} \
359+
// expected-note {{evaluates to '1 == 2'}}
360+
static_assert(1 / 1 == 2, ""); // expected-error {{failed}} \
361+
// expected-note {{evaluates to '1 == 2'}}
362+
static_assert(1 << 3 != 8, ""); // expected-error {{failed}} \
363+
// expected-note {{evaluates to '8 != 8'}}
354364
}

0 commit comments

Comments
 (0)