Skip to contentSkip to conversation

Lexer sed post-processing instead of pragmas

PATCH New Grégoire Sutre 1 participant 1 message Mar 16, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Lexer sed post-processing instead of pragmas

Hi,

The lexer code in script/yylex.l uses pragmas that are not supported by gcc < 4.2. However, without these pragmas, the build fails because of warnings in the lexer code generated by flex (recall that some targets are built with -Werror by default).

I've discussed this issue with sethmeisterg on irc. Of course, dropping compatibility with gcc 4.1.3 is a possible solution, but we believe that a better alternative would be to modify the generated lexer so that it does not trigger any warning.

There are already some sed commands in conf/common.rmk that remove #includes from the generated lexer. The attached patch extends the sed post-processing of the lexer to get rid of the warnings, and removes the pragmas. This way, grub can still be compiled with gcc 4.1.3 (which is the current required version in INSTALL).

The patch also replaces the non-portable -i sed option with temporary files.

Thanks for your comments,

Grégoire

=== modified file 'conf/common.rmk'

common.rmk +13 -2
@@ -95,10 +95,21 @@
 grub_bin2h_SOURCES = gnulib/progname.c util/bin2h.c
 
 # For the lexer.
+lex_fix_includes = '/^\#include[[:space:]]*<(stdio|string|errno|stdlib|unistd)\.h>/d'
+lex_fix_h  = '/^(static)?[[:space:]]*void[[:space:]]*yy_flex_strncpy[[:space:]]*\(.*\)[[:space:]]*;$$/d'
+lex_fix_c1 = '/^(static)?[[:space:]]*void[[:space:]]*yy_fatal_error[[:space:]]*\(.*\)[[:space:]]*;$$/d'
+lex_fix_c2 = '/^(static)?[[:space:]]*(int|void)[[:space:]]*yy(get_column|set_column|_fatal_error)[[:space:]]*\(.*\)$$/,/^}$$/d'
+lex_fix_c3 = '/^static[[:space:]]*void[[:space:]]*yy_flex_strncpy[[:space:]]*\(.*\)$$/{\
+s/yyscanner/yyscanner __attribute__ ((unused))/;\
+};'
+
 grub_script.yy.c grub_script.yy.h: script/yylex.l
 	$(LEX) -o grub_script.yy.c --header-file=grub_script.yy.h $(srcdir)/script/yylex.l
-	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.h
-	sed -i 's/^#include.*\(<stdio\.h>\|<string\.h>\|<errno\.h>\|<stdlib\.h>\|<unistd\.h>\)//g' grub_script.yy.c
+	mv grub_script.yy.h grub_script.yy.h.tmp
+	mv grub_script.yy.c grub_script.yy.c.tmp
+	sed -r -e $(lex_fix_includes) -e $(lex_fix_h) grub_script.yy.h.tmp > grub_script.yy.h
+	sed -r -e $(lex_fix_includes) -e $(lex_fix_c1) -e $(lex_fix_c2) -e $(lex_fix_c3) grub_script.yy.c.tmp > grub_script.yy.c
+	rm grub_script.yy.h.tmp grub_script.yy.c.tmp
 DISTCLEANFILES += grub_script.yy.c grub_script.yy.h
 
 # For grub-script-check.

=== modified file 'script/yylex.l'

yylex.l +0 -6
@@ -94,12 +94,6 @@
 #define fprintf(...) 0
 #define exit(...)
 
-#pragma GCC diagnostic warning "-Wunused-variable"
-#pragma GCC diagnostic warning "-Wunused-function"
-#pragma GCC diagnostic warning "-Wunused-parameter"
-#pragma GCC diagnostic warning "-Wstrict-prototypes"
-#pragma GCC diagnostic warning "-Wmissing-prototypes"
-
 }
 
 %option ecs