Get some information means code macro definitions, etc.

Get some information means code macro definitions, etc.

Source  https://www.cnblogs.com/clover-toeic/p/3849113.html

 

1.   precompiled stage knowable information

     Suppose the code somewhere macros are defined as follows:

#define MACRO  2

     then:

     1) Check whether the definition of the macro name

#if defined MACRO
    #error defined MACRO!
#endif

     2) Check the macro value is a value

#if (2 == MACRO)
    #error (2 == MACRO)!
#endif

     Compiler output:

test.c:3:10: error: #error defined MACRO!
test.c:6:10: error: #error (2 == MACRO)!

     Wherein, # error for the output of the pre-defined information, does not indicate the true errors occur.

 

2. The  compilation phase knowable information

     The following definition of static assertion macro:

#define CONCATE(x, y)             _CONCATE(x, y)
#define _CONCATE(x, y)            x##y
#define STATIC_ASSERT(exp)        typedef char \
     CONCATE(Assertion_Failed_at_Line, __LINE__)[(exp) ? 1 : -1]
#define STATIC_ASSERT2(exp, str)  typedef char \
     CONCATE(str##_at_Line, __LINE__)[(exp) ? 1 : -1]

     Clever use of the macro array subscript gcc compiler compiler error of -1. Which, exp is a need to check the expression; str customized error message string, must meet the C language variable naming rules (not written as a string or with spaces or in digital starting, etc.). typedef char is intended to avoid variable naming conflicts or namespace pollution.

     Application examples are as follows:

typedef struct {
     char acRecord [ 32 ]; 
} T_HIST_ALM; 
#define HIS_ALM_NUM 300      // historical alarm maximum number 

#define HIS_ALM_BASE 0x3000   // historical alarm start address 
#define HIS_LOG_BASE 0x4000   // history log starting address 

int main ( void ) { 
    static_assert ( the sizeof (T_HIST_ALM) <= 20 is ); 
    STATIC_ASSERT2 ((HIS_ALM_BASE + the sizeof (T_HIST_ALM) * HIS_ALM_NUM) < HIS_LOG_BASE, \ 
                   History_Alarm_Space_Is_Not_Enough); 
    return  0;
}

     After compiling given below (sample code and assertions omitted macro definitions file header, so that differ from line numbers):

[wangxiaoyuan_@localhost test1]$ gcc -o test test.c
test.c: In function 'main':
test.c:17: error: size of array 'Assertion_Failed_at_Line17' is negative
test.c:18: error: size of array 'History_Alarm_Space_Is_Not_Enough_at_Line19' is negative

     Visible, compile error in itself provide a file name, function name and line number information, so the assertion optional line number within the macro.

     Static assertion checking can be used to compile an array, such as heap buffer overflows, resource allocation and adequacy.

 

3.  knowable information during operation

     Debug tracing macros defined in the log information to be attached before the log file name, line number, function name and other information:

#define TRACE(fmt, args...) do{\
    printf("[%s(%d)<%s>]", __FILE__, __LINE__, __FUNCTION__);\
    printf((fmt), ##args);\
}while(0)

     Application examples are as follows:

int main(void){
    TRACE("Minus 128 = 0x%x!\n", -128);               
    return 0;
}

     Output Runtime:

[test.c(2)<main>]Minus 128 = 0xffffff80!

 

================ End

 

Guess you like

Origin www.cnblogs.com/lsgxeva/p/11696019.html