[clang] [clang] Print static_assert values of arithmetic binary operators (PR #71671)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 23 10:46:38 PST 2023


================
@@ -17219,10 +17219,10 @@ static bool UsefulToPrintExpr(const Expr *E) {
   if (const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
     return UsefulToPrintExpr(UnaryOp->getSubExpr());
 
-  // Ignore nested binary operators. This could be a FIXME for improvements
-  // to the diagnostics in the future.
-  if (isa<BinaryOperator>(E))
-    return false;
+  // Only print nested arithmetic operators.
+  if (const auto *BO = dyn_cast<BinaryOperator>(E))
+    return (BO->isShiftOp() || BO->isAdditiveOp() || BO->isMultiplicativeOp() ||
+            BO->isBitwiseOp());
 
----------------
tbaederr wrote:

For
```c++
static_assert(1 << 1 == 0);
```
we can print the result rather easily, but for 
```c++
static_assert(foo() > 1 && bar() == 10);
```
it's kind useless to print the LHS and RHS of the `&&` operator because they just evaluate to `true` or `false`. To print this in a useful way, we would have to dig deeper into the expression and print what `foo()` and `bar()` evaluate to. 

https://github.com/llvm/llvm-project/pull/71671


More information about the cfe-commits mailing list