Skip to content

#line directives issued by flex effectively point to irrelevant or non-existent parts of the .l file #903

Done

Description

@szhorvat

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:

  1. flex does not emit a second #line to switch back to the generated C sources once it's done copying C sources from the .l file. This results in diagnostics that refer to irrelevant parts of the .l file. In comparison, Bison will issue a matching #line that switches back to generated C sources after every action.
  2. flex also issues a #line for the default action (ECHO). It points at the line after the last rule in the .l file, which may not exist (past the end of the file). Arguably, it should not issue this #line at all. In this case, flex does actually issue a second #line that 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 .l file. 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 #line to switch back to the generated C sources after every action, just like Bison does.
  • Do not issue #line directives 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' | ./repro

Output:

rule for a: __LINE__ = 942
rule for b: __LINE__ = 10

repro.l has 8 lines. YY_USER_ACTION for rule b is expanded at line 10, which does
not exist. The rules part of lex.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.gcda

gcov 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

  1. westes commented on Nov 24, 2026

    @westes
    Owner

    Thanks for this report. The fix is on master and is included in the next release of flex.

  2. added a commit that references this issue on Nov 24, 2026
    a5e7305
  3. szhorvat commented on Nov 24, 2026

    @szhorvat
    Author

    Thank you for the very quick fix!

    It seems the #line directives now come after the YY_BREAK, meaning that should there be diagnostics or coverage for the YY_BREAK line, it would still refer to an unrelated part of the .l file.

    In practice this won't really become an issue in the vast majority of cases, but I think that the correct place for the #line would be before, not after the YY_BREAK. With the minimal example from the top post,

    #line 8 "repro.l"
    { }
    	YY_BREAK            <- still counted as repro.l:9
    #line 958 "lex.yy.c"
    	case 3:
    
  4. westes commented on Nov 24, 2026

    @westes
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions