[libc-commits] [libc] 65fe1ac - [libc][NFC] Simplify assert message generation

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Jul 25 11:45:49 PDT 2023


Author: Michael Jones
Date: 2023-07-25T11:45:45-07:00
New Revision: 65fe1ac30a4cbca61fb2fcfb60a42aca3777758b

URL: https://github.com/llvm/llvm-project/commit/65fe1ac30a4cbca61fb2fcfb60a42aca3777758b
DIFF: https://github.com/llvm/llvm-project/commit/65fe1ac30a4cbca61fb2fcfb60a42aca3777758b.diff

LOG: [libc][NFC] Simplify assert message generation

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

Reviewed By: lntue, sivachandra, JonChesterfield

Differential Revision: https://reviews.llvm.org/D156168

Added: 
    

Modified: 
    libc/src/__support/libc_assert.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h
index 549c5228ff225d..ff6ed52ecee33a 100644
--- a/libc/src/__support/libc_assert.h
+++ b/libc/src/__support/libc_assert.h
@@ -25,26 +25,6 @@
 #include "src/__support/integer_to_string.h"
 #include "src/__support/macros/attributes.h" // For LIBC_INLINE
 
-namespace __llvm_libc {
-
-LIBC_INLINE void report_assertion_failure(const char *assertion,
-                                          const char *filename, unsigned line,
-                                          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(funcname);
-  __llvm_libc::write_to_stderr("'\n");
-}
-
-} // namespace __llvm_libc
-
 #ifdef LIBC_ASSERT
 #error "Unexpected: LIBC_ASSERT macro already defined"
 #endif
@@ -60,11 +40,22 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,
   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 __LIBC_MACRO_TO_STR(x) #x
+#define __LIBC_MACRO_TO_STR_INDIR(y) __LIBC_MACRO_TO_STR(y)
+#define __LIBC_LINE_STR__ __LIBC_MACRO_TO_STR_INDIR(__LINE__)
+
 #define LIBC_ASSERT(COND)                                                      \
   do {                                                                         \
     if (!(COND)) {                                                             \
-      __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__,         \
-                                            __PRETTY_FUNCTION__);              \
+      __llvm_libc::write_to_stderr(__FILE__ ":" __LIBC_LINE_STR__              \
+                                            ": Assertion failed: '" #COND      \
+                                            "' in function: '");               \
+      __llvm_libc::write_to_stderr(__PRETTY_FUNCTION__);                       \
+      __llvm_libc::write_to_stderr("'\n");                                     \
       __llvm_libc::quick_exit(0xFF);                                           \
     }                                                                          \
   } while (false)


        


More information about the libc-commits mailing list