[libc-commits] [PATCH] D156168: [libc][NFC] Simplify assert message generation

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Jul 24 14:11:58 PDT 2023


michaelrj created this revision.
michaelrj added reviewers: lntue, sivachandra, JonChesterfield.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
michaelrj requested review of this revision.

Previously displaying a failed assert would involve a runtime integer to
string conversion. This patch changes that to be a compile time string
conversion.

This was inspired by a comment by JonChesterfield on https://reviews.llvm.org/D155899


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156168

Files:
  libc/src/__support/libc_assert.h


Index: libc/src/__support/libc_assert.h
===================================================================
--- libc/src/__support/libc_assert.h
+++ libc/src/__support/libc_assert.h
@@ -27,18 +27,9 @@
 
 namespace __llvm_libc {
 
-LIBC_INLINE void report_assertion_failure(const char *assertion,
-                                          const char *filename, unsigned line,
+LIBC_INLINE void report_assertion_failure(const char *error_str,
                                           const char *funcname) {
-  char line_str[IntegerToString::dec_bufsize<unsigned>()];
-  // dec returns an optional, will always be valid for this size buffer
-  auto line_number = IntegerToString::dec(line, line_str);
-  __llvm_libc::write_to_stderr(filename);
-  __llvm_libc::write_to_stderr(":");
-  __llvm_libc::write_to_stderr(*line_number);
-  __llvm_libc::write_to_stderr(": Assertion failed: '");
-  __llvm_libc::write_to_stderr(assertion);
-  __llvm_libc::write_to_stderr("' in function: '");
+  __llvm_libc::write_to_stderr(error_str);
   __llvm_libc::write_to_stderr(funcname);
   __llvm_libc::write_to_stderr("'\n");
 }
@@ -60,11 +51,21 @@
   do {                                                                         \
   } while (false)
 #else
+
+// Convert __LINE__ to a string using macros. The indirection is necessary
+// because otherwise it will turn "__LINE__" into a string, not its value. The
+// value is evaluated in the indirection step.
+#define __MACRO_TO_STR(x) #x
+#define __MACRO_TO_STR_INDIR(y) __MACRO_TO_STR(y)
+#define __LINE_STR__ __MACRO_TO_STR_INDIR(__LINE__)
+
 #define LIBC_ASSERT(COND)                                                      \
   do {                                                                         \
     if (!(COND)) {                                                             \
-      __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__,         \
-                                            __PRETTY_FUNCTION__);              \
+      __llvm_libc::report_assertion_failure(                                   \
+          __FILE__ ":" __LINE_STR__ ": Assertion failed: '" #COND              \
+                   "' in function: '",                                         \
+          __PRETTY_FUNCTION__);                                                \
       __llvm_libc::quick_exit(0xFF);                                           \
     }                                                                          \
   } while (false)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156168.543695.patch
Type: text/x-patch
Size: 2506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230724/a8b408c0/attachment.bin>


More information about the libc-commits mailing list