diff --git a/gtests/net/packetdrill/lexer.l b/gtests/net/packetdrill/lexer.l index 4063e17314f3643390d050ecd23881293b115496..f8efc970ae132e3d9977b6823b78aacce51b6c37 100644 --- a/gtests/net/packetdrill/lexer.l +++ b/gtests/net/packetdrill/lexer.l @@ -111,54 +111,65 @@ enum ifdef_os { YY_BUFFER_STATE ifdef_stack[MAX_IFDEF_DEPTH]; int ifdef_stack_ptr = 0; -static void handle_ifdef(enum ifdef_os os, const char *s) { - char *code = strdup(s); - - unsigned int ifdef_length = strlen("#ifdef "); - unsigned int endif_length = strlen("#endif"); - unsigned int os_name_length = 0; - +static inline int get_os_name_length(enum ifdef_os os) { switch (os) { case Linux_IFDEF: - os_name_length = strlen("Linux"); - break; + return strlen("Linux"); case FreeBSD_IFDEF: - os_name_length = strlen("FreeBSD"); - break; + return strlen("FreeBSD"); case NetBSD_IFDEF: - os_name_length = strlen("NetBSD"); - break; + return strlen("NetBSD"); case OpenBSD_IFDEF: - os_name_length = strlen("OpenBSD"); - break; + return strlen("OpenBSD"); default: - fprintf(stderr, "handle_ifdef with unknown os called.\n"); - exit(1); + return -1; } - +} + +static inline bool ignore_ifdef(enum ifdef_os os) { #ifdef linux - if (os != Linux_IFDEF) { - return; + if (os == Linux_IFDEF) { + return false; } #endif #ifdef __FreeBSD__ - if (os != FreeBSD_IFDEF) { - return; + if (os == FreeBSD_IFDEF) { + return false; } #endif #ifdef __OpenBSD__ - if (os != OpenBSD_IFDEF) { - return; + if (os == OpenBSD_IFDEF) { + return false; } #endif #ifdef __NetBSD__ - if (os != NetBSD_IFDEF) { - return; + if (os == NetBSD_IFDEF) { + return false; } #endif - char *code_without_ifdef = code+ifdef_length+os_name_length; - code_without_ifdef[strlen(code_without_ifdef) - (endif_length)] = (char) 0; + return true; +} + +static void handle_ifdef(enum ifdef_os os, const char *s) { + char *code = strdup(s); + + unsigned int ifdef_length = strlen("#ifdef "); + unsigned int endif_length = strlen("#endif"); + int os_name_length = get_os_name_length(os); + + if (os_name_length == -1) { + fprintf(stderr, "handle_ifdef with unknown os called.\n"); + exit(1); + } + + if (ignore_ifdef(os)) { + return; + } + + char *code_without_ifdef = code + ifdef_length + os_name_length; + unsigned int newline_before_endif = strlen(code_without_ifdef) - endif_length; + code_without_ifdef[newline_before_endif] = (char) 0; // fprintf( stdout, "\n%s\n", code_without_ifdef); if (ifdef_stack_ptr >= MAX_IFDEF_DEPTH) {