delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/11/24/05:31:19

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Date: Mon, 24 Nov 2003 12:30:28 +0200 (EET)
From: Esa A E Peuha <peuha AT cc DOT helsinki DOT fi>
Sender: peuha AT sirppi DOT helsinki DOT fi
To: djgpp-workers AT delorie DOT com
Subject: C99 and assert
Message-ID: <Pine.OSF.4.58.0311171215310.18135@sirppi.helsinki.fi>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

C99 says that assert should print function name in addition to file name
and line number.  Compatibility with old versions of gcc demands some
complications (at least I think so):

Index: include/assert.h
===================================================================
RCS file: /cvs/djgpp/djgpp/include/assert.h,v
retrieving revision 1.3
diff -c -r1.3 assert.h
*** include/assert.h	7 Sep 1997 21:06:10 -0000	1.3
--- include/assert.h	24 Nov 2003 10:17:48 -0000
***************
*** 6,12 ****
  #if defined(NDEBUG)
  #define assert(test) ((void)0)
  #else
! #define assert(test) ((void)((test)||(__dj_assert(#test,__FILE__,__LINE__),0)))
  #endif

  #ifndef __dj_include_assert_h_
--- 6,16 ----
  #if defined(NDEBUG)
  #define assert(test) ((void)0)
  #else
! #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
! #define assert(test) ((void)((test)||(__dj_assert(#test,__FILE__,__LINE__,__func__),0)))
! #else
! #define assert(test) ((void)((test)||(__dj_assert(#test,__FILE__,__LINE__,NULL),0)))
! #endif
  #endif

  #ifndef __dj_include_assert_h_
***************
*** 16,22 ****
  extern "C" {
  #endif

! void	__dj_assert(const char *,const char *,int) __attribute__((__noreturn__));

  #ifdef __cplusplus
  }
--- 20,26 ----
  extern "C" {
  #endif

! void	__dj_assert(const char *,const char *,int, const char *) __attribute__((__noreturn__));

  #ifdef __cplusplus
  }
Index: src/libc/ansi/assert/assert.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/assert/assert.c,v
retrieving revision 1.1
diff -c -r1.1 assert.c
*** src/libc/ansi/assert/assert.c	29 Nov 1994 09:17:44 -0000	1.1
--- src/libc/ansi/assert/assert.c	24 Nov 2003 10:17:48 -0000
***************
*** 4,13 ****
  #include <signal.h>
  #include <stdlib.h>

! void	__dj_assert(const char *msg, const char *file, int line)
  {
!   /* Assertion failed at foo.c line 45: x<y */
!   fprintf(stderr, "Assertion failed at %s line %d: %s\n", file, line, msg);
    raise(SIGABRT);
    exit(1);
  }
--- 4,16 ----
  #include <signal.h>
  #include <stdlib.h>

! void	__dj_assert(const char *msg, const char *file, int line, const char *func)
  {
!   /* Assertion failed at foo.c line 45, function bar: x<y */
!   fprintf(stderr, "Assertion failed at %s line %d", file, line);
!   if(func)
!     fprintf(stderr, ", function %s", func);
!   fprintf(stderr, ": %s\n", msg);
    raise(SIGABRT);
    exit(1);
  }


-- 
Esa Peuha
student of mathematics at the University of Helsinki
http://www.helsinki.fi/~peuha/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019