00001 /* 00002 * assert_pp.h 00003 * 00004 * Copyright (c) 2003 The University of Utah and the Flux Group. 00005 * All rights reserved. 00006 * 00007 * This file is licensed under the terms of the GNU Public License. 00008 * See the file "license.terms" for restrictions on redistribution 00009 * of this file, and for a DISCLAIMER OF ALL WARRANTIES. 00010 */ 00011 00012 /** 00013 * @file assert_pp.h 00014 * 00015 * Provides require/ensure macros for testing preconditions and 00016 * postconditions. 00017 */ 00018 00019 #ifndef _assert_pp_h 00020 #define _assert_pp_h 00021 00022 #include <stdlib.h> 00023 00024 #if !defined(DEBUG) 00025 #define NO_PRECONDITIONS 00026 #define NO_POSTCONDITIONS 00027 #endif 00028 00029 #if !defined(NO_POSTCONDITIONS) || !defined(NO_PRECONDITIONS) 00030 #if defined(__GNUC__) 00031 /** 00032 * The 'noreturn' attribute for this compiler. 00033 */ 00034 #define ASSERT_FUNCATTR_NORETURN __attribute__((__noreturn__)) 00035 #else 00036 /** 00037 * Empty 'noreturn' attribute for unsupported compilers. 00038 */ 00039 #define ASSERT_FUNCATTR_NORETURN 00040 #endif 00041 #ifdef __cplusplus 00042 extern "C" { 00043 #endif 00044 /** 00045 * The routine which prints failure messages and aborts the program. 00046 * Defined in misc/src/assert_pp.c. 00047 * 00048 * @param file The file name containing the condition. 00049 * @param line The line number containing the condition. 00050 * @param func The name of the function containing the condition. 00051 * @param type Type of condition. 00052 * @param expr The stringified expression that failed. 00053 * @return Doesn't. 00054 */ 00055 extern int pp_failed(char *file, unsigned int line, 00056 const char* func, char *type, char *expr) 00057 ASSERT_FUNCATTR_NORETURN; 00058 #ifdef __cplusplus 00059 } 00060 #endif 00061 #endif 00062 00063 #ifndef NO_PRECONDITIONS 00064 /** 00065 * Wrapper for declarations that need to be conditionally compiled because they 00066 * are only used by a precondition. 00067 * 00068 * @param decl The declaration to conditionally compile. 00069 */ 00070 #define pp_decl(decl) decl 00071 /** 00072 * Wrapper for code blocks that need to be conditionally compiled because they 00073 * are only used by a precondition. 00074 * 00075 * @param block The code block to conditionally compile. 00076 */ 00077 #define pp_block(block) block 00078 /** 00079 * Require an expression to be true. If the expression turns out to be false, 00080 * pp_failed is called with the location of this use of require. 00081 * 00082 * @see assert 00083 * @see ensure 00084 * 00085 * @param precon The expression to test. 00086 */ 00087 #define require(precon) \ 00088 ((void)((precon) ? 0 : \ 00089 (pp_failed(__FILE__,__LINE__,__PRETTY_FUNCTION__,"precondition",__STRING(precon))))) 00090 #else 00091 #define require(x) 00092 #define pp_decl(x) 00093 #define pp_block(x) 00094 #endif 00095 00096 #ifndef NO_POSTCONDITIONS 00097 /** 00098 * Ensure an expression to be true. If the expression turns out to be false, 00099 * pp_failed is called with the location of this use of ensure. 00100 * 00101 * @see assert 00102 * @see require 00103 * 00104 * @param postcon The expression to test. 00105 */ 00106 #define ensure(postcon) \ 00107 ((void)((postcon) ? 0 : \ 00108 (pp_failed(__FILE__,__LINE__,__PRETTY_FUNCTION__,"postcondition",__STRING(postcon))))) 00109 #else 00110 #define ensure(x) 00111 #endif 00112 00113 #endif