Repository navigation
Description
When generating C sources, flex issues #line directives pointing to the .l file. This is generally useful: compiler diagnostics and coverage tools will refer to the relevant human-written .l file, not the generated C sources.
However, there are two issues:
- flex does not emit a second
#lineto switch back to the generated C sources once it's done copying C sources from the.lfile. This results in diagnostics that refer to irrelevant parts of the.lfile. In comparison, Bison will issue a matching#linethat switches back to generated C sources after every action. - flex also issues a
#linefor the default action (ECHO). It points at the line after the last rule in the.lfile, which may not exist (past the end of the file). Arguably, it should not issue this#lineat all. In this case, flex does actually issue a second#linethat points back to generated sources, but it does so too late. This means that tools that use line numbers will end up referring past the end of the.lfile. Some of these tools, such as lcov 2.1+ (for coverage) will error out when they can't find the lines they expect. We had to work around this in igraph.
Proposal:
- Issue a
#lineto switch back to the generated C sources after every action, just like Bison does. - Do not issue
#linedirectives for the default action.
What follows is a minimal example generated using AI-assistance. Everything tested with flex 2.7.5.
Minimal example
repro.l(8 lines, no user-code section):%option noyywrap nounput noinput %{ #include <stdio.h> #define YY_USER_ACTION printf("rule for %s: __LINE__ = %d\n", yytext, __LINE__); %} %% a { } b { }
main.c:int yylex(void); int main(void) { return yylex(); }Commands:
flex -o lex.yy.c repro.l gcc -o repro lex.yy.c main.c printf 'ab' | ./reproOutput:
rule for a: __LINE__ = 942 rule for b: __LINE__ = 10
repro.lhas 8 lines.YY_USER_ACTIONfor rulebis expanded at line 10, which does
not exist. The rules part oflex.yy.c, with the line each line is counted as:case 1: YY_RULE_SETUP <- lex.yy.c:942 #line 7 "repro.l" { } <- repro.l:7 YY_BREAK <- repro.l:8 case 2: <- repro.l:9 YY_RULE_SETUP <- repro.l:10 <- repro.l:11 #line 8 "repro.l" { } <- repro.l:8 YY_BREAK <- repro.l:9 case 3: <- repro.l:10 /* rule 3 can match eol */ <- repro.l:11 YY_RULE_SETUP <- repro.l:12 <- repro.l:13 #line 9 "repro.l" ECHO; <- repro.l:9 YY_BREAK <- repro.l:10 #line 961 "lex.yy.c" #line 962 "lex.yy.c" case YY_STATE_EOF(INITIAL):Rule 3 is flex's default rule (ECHO). Its own
#line 9 "repro.l"also points past the end
of the file: it is the line after the last rule.The same with coverage:
gcc --coverage -o repro-cov lex.yy.c main.c printf 'ab' | ./repro-cov gcov -i -t repro-cov-lex.yy.gcdagcov records lines 8, 9, 10 and 12 of
repro.l. Only line 8 exists.Expected
Code that flex generates should be attributed to the generated file, e.g.:
#line 7 "repro.l" { } #line NNN "lex.yy.c" YY_BREAK case 2: YY_RULE_SETUP #line 8 "repro.l" { } #line NNN "lex.yy.c" YY_BREAK
Activity
westes commentedon Nov 24, 2026
Thanks for this report. The fix is on master and is included in the next release of flex.
fix(scanner): switch back to the generated file after each action
szhorvat commentedon Nov 24, 2026
Thank you for the very quick fix!
It seems the
#linedirectives now come after theYY_BREAK, meaning that should there be diagnostics or coverage for theYY_BREAKline, it would still refer to an unrelated part of the.lfile.In practice this won't really become an issue in the vast majority of cases, but I think that the correct place for the
#linewould be before, not after theYY_BREAK. With the minimal example from the top post,fix(scanner): put the switch back #line before YY_BREAK
westes commentedon Nov 24, 2026
Thanks for catching this. You're right: the directive that switches back to the generated file was being written after YY_BREAK instead of before it.
It now comes before. Flex also no longer writes two other directives that pointed into the scanner file: one before the case label of a rule whose action is '|', and a repeated one after the default rule.
This is on master and will be in the next release of flex.