[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

Takuya Shimizu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 08:35:54 PDT 2023


hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, tbaeder, cjdb.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

BEFORE this patch, the values printed in `static_assert` were printed after ignoring the implicit type casts on them. As a result, clang is printing character type values that are visually hard to recognize such as `static_assert((char)9 == 0)` (Live demo: https://godbolt.org/z/6WKP3jjoE)

This patch makes clang print integers instead of the character representantions in these cases so as not to output nonprintable characters. (In the case above, `9 == 0`)
When the user-provided expression is of bool type, this patch does not print them as `0` and `1` because `false` and `true` would be easier to understand.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert.cpp


Index: clang/test/SemaCXX/static-assert.cpp
===================================================================
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,15 @@
     return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-                                       // expected-note {{evaluates to ''c' == 'a''}}
+                                       // expected-note {{evaluates to '99 == 97'}}
+  static_assert((char)9 == 'a', ""); // expected-error {{failed}} \
+                                     // expected-note {{evaluates to '9 == 97'}}
+  static_assert((unsigned char)10 == 'a', ""); // expected-error {{failed}} \
+                                               // expected-note {{evaluates to '10 == 97'}}
+  static_assert((char)-123 == 'a', ""); // expected-error {{failed}} \
+                                        // expected-note {{evaluates to '-123 == 97'}}
+  static_assert((unsigned char)-4 == 'a', ""); // expected-error {{failed}} \
+                                               // expected-note {{evaluates to '252 == 97'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===================================================================
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error at 11 {{}} expected-warning at 11 {{trigraph ignored}}
-// expected-error at 13 {{failed}} expected-warning at 13 {{trigraph ignored}} expected-note at 13 {{evaluates to ''?' == '#''}}
+// expected-error at 13 {{failed}} expected-warning at 13 {{trigraph ignored}} expected-note at 13 {{evaluates to '63 == 35'}}
 // expected-error at 16 {{}}
 // expected-error at 20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -16866,8 +16866,14 @@
       Expr::EvalResult Result;
       SmallString<12> ValueString;
       bool Print;
-    } DiagSide[2] = {{LHS, Expr::EvalResult(), {}, false},
-                     {RHS, Expr::EvalResult(), {}, false}};
+    } DiagSide[2] = {{LHS->getType()->isBooleanType() ? LHS : Op->getLHS(),
+                      Expr::EvalResult(),
+                      {},
+                      false},
+                     {RHS->getType()->isBooleanType() ? RHS : Op->getRHS(),
+                      Expr::EvalResult(),
+                      {},
+                      false}};
     for (unsigned I = 0; I < 2; I++) {
       const Expr *Side = DiagSide[I].Cond;
 
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -425,6 +425,9 @@
 - ``-Wformat`` will no longer suggest a no-op fix-it for fixing scoped enum format
   warnings. Instead, it will suggest casting the enum object to the type specified
   in the format string.
+- When describing the failure of static assertion, clang prints the integer representation
+  of the value instead of its character representation even when the user-provided value
+  is of character type so as not to output non-printable characters.
 
 Bug Fixes in This Version
 -------------------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155610.541556.patch
Type: text/x-patch
Size: 3355 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230718/21b402af/attachment-0001.bin>


More information about the cfe-commits mailing list