Skip to content

Commit

Permalink
Merge pull request #13344 from stedolan/better-failed-assert
Browse files Browse the repository at this point in the history
Make caml_failed_assert friendlier to debuggers
  • Loading branch information
gasche authored Jul 31, 2024
2 parents ee2c5cf + cd7f011 commit 7678ef7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 12 additions & 1 deletion runtime/caml/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,20 @@ typedef char char_os;
#define __OSFILE__ __FILE__
#endif

/* Although caml_failed_assert never returns, it is not marked as such.
This prevents the C compiler optimising away all of the useful context
from the callsite, making debuggers able to see it. */
#define CAMLassert(x) \
(CAMLlikely(x) ? (void) 0 : caml_failed_assert ( #x , __OSFILE__, __LINE__))
CAMLnoret CAMLextern void caml_failed_assert (char *, char_os *, int);
CAMLextern void caml_failed_assert (char *, char_os *, int)
#if defined(__has_feature)
/* However, we do inform clang-analyzer that this function never returns,
since that improves analysis without breaking debugging */
#if __has_feature(attribute_analyzer_noreturn)
__attribute__((analyzer_noreturn))
#endif
#endif
;
#else
#define CAMLassert(x) ((void) 0)
#endif
Expand Down
3 changes: 3 additions & 0 deletions runtime/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ void caml_failed_assert (char * expr, char_os * file_os, int line)
(Caml_state_opt != NULL) ? Caml_state_opt->id : -1, file, line, expr);
fflush(stderr);
caml_stat_free(file);
#if __has_builtin(__builtin_trap) || defined(__GNUC__)
__builtin_trap();
#endif
abort();
}
#endif
Expand Down

0 comments on commit 7678ef7

Please sign in to comment.