[Flex-devel] #include <unistd.h> in middle of scanner: bad idea
flex is a tool for generating scanners
Brought to you by:
wlestes
|
From: Kaz K. <938...@ky...> - 2020-08-05 17:37:43
|
Hi,
I'd like to report a sort of bug.
Flex generates a file which contains
a #include <unistd.h> long after user material. This seems to be for
the sake of having a declaration of isatty.
The problem with this is that the user code can #define macros that
can break the header. C programs have to be careful with what they
do before system headers are included.
The programmer typically places #include's in the %{ %} section,
and then the assumption is that those are all the headers there are.
Here is small repro test case:
%{
#define pause blah[]
%}
%%
Result:
$ flex lex.l ; cc lex.yy.c
lex.l:3:15: error: declaration of ‘blah’ as array of functions
#define pause blah[]
I understand that the directive can be suppressed with YY_NO_UNISTD_H,
but that's a workaround discovered after things break.
How about just an "extern int isatty(int)" somewhere?
Or just plant it in this section:
/* First, we deal with platform-specific or compiler-specific issues.
*/
/* begin standard C headers. */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#ifndef YY_NO_UNISTD_H
#include <unistd.h>
#endif
/* end standard C headers. */
If you're going to include <unistd.h> eventually, why beat around the
bush; clump it with other system headers.
If someone wants to disable that, they can use flex --nounistd.
Cheers ...
|