Skip to content

Commit 04d3de7

Browse files
committed
feat: backport flex noline fix
When using the --noline flag or the %option noline, flex was failing to suppress the %top line directives. There was an if-guard missing in the code that adds the line directives to the top_buf. I've fixed it in [flex PR #704]. This commit adds patching capabilities to ease maintaining fixes / backports like the noline fix. [flex PR #704]: westes/flex#704
1 parent c58ff42 commit 04d3de7

File tree

8 files changed

+109
-1
lines changed

8 files changed

+109
-1
lines changed

flex/internal/BUILD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
22

33
filegroup(
44
name = "bzl_srcs",
5-
srcs = glob(["*.bzl"]),
5+
srcs = glob(["*.bzl"]) + [
6+
"//flex/internal/patches:bzl_srcs",
7+
],
68
visibility = ["//:__subpackages__"],
79
)
810

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
From 4f81a7293993211ae31970c5a349565dcd1faf21 Mon Sep 17 00:00:00 2001
2+
From: Javier Maestro <jjmaestro@ieee.org>
3+
Date: Fri, 16 May 2025 13:47:11 -0400
4+
Subject: [PATCH] backport: noline for %top directives
5+
6+
See https://github.com/westes/flex/pull/704
7+
---
8+
src/scan.c | 4 +++-
9+
src/scan.l | 4 +++-
10+
2 files changed, 6 insertions(+), 2 deletion(-)
11+
12+
diff --git a/src/scan.c b/src/scan.c
13+
index 66db864..5e750fb 100644
14+
--- a/src/scan.c
15+
+++ b/src/scan.c
16+
@@ -2326,7 +2326,9 @@
17+
{
18+
brace_start_line = linenum;
19+
++linenum;
20+
- buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
21+
+ // backporting https://github.com/westes/flex/pull/704/
22+
+ if (gen_line_dirs)
23+
+ buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
24+
brace_depth = 1;
25+
yy_push_state(CODEBLOCK_MATCH_BRACE);
26+
}
27+
diff --git a/src/scan.l b/src/scan.l
28+
index 66db864..5e750fb 100644
29+
--- a/src/scan.l
30+
+++ b/src/scan.l
31+
@@ -175,7 +175,9 @@ M4QEND "]""]"
32+
^"%top"[[:blank:]]*"{"[[:blank:]]*{NL} {
33+
brace_start_line = linenum;
34+
++linenum;
35+
- buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
36+
+ // backporting https://github.com/westes/flex/pull/704/
37+
+ if (gen_line_dirs)
38+
+ buf_linedir( &top_buf, infilename?infilename:"<stdin>", linenum);
39+
brace_depth = 1;
40+
yy_push_state(CODEBLOCK_MATCH_BRACE);
41+
}
42+
--
43+
2.39.5 (Apple Git-154)
44+

flex/internal/patches/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
filegroup(
2+
name = "bzl_srcs",
3+
srcs = glob(["*.bzl"]),
4+
visibility = ["//:__subpackages__"],
5+
)
6+
7+
filegroup(
8+
name = "patches",
9+
srcs = glob(["*.patch"]),
10+
visibility = ["//:__subpackages__"],
11+
)

flex/internal/patches/patches.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Flex patches"""
2+
3+
PATCHES = {
4+
"2.6.4": [
5+
# backporting https://github.com/westes/flex/pull/704
6+
Label(":0001-fix-noline-for-top-directives.patch"),
7+
],
8+
}

flex/rules/flex_repository.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""Definition of the `flex_repository` repository rule."""
1818

1919
load("//flex/internal:versions.bzl", "VERSION_URLS")
20+
load("//flex/internal/patches:patches.bzl", "PATCHES")
2021

2122
_FLEX_BUILD = """
2223
load("@rules_cc//cc:cc_library.bzl", "cc_library")
@@ -117,6 +118,12 @@ def _hardcode_int_max_log10(ctx, version):
117118
"(size_t) (1 + ceil (log10 (abs (lineno))))": INT_FORMAT_CAPACITY,
118119
}, executable = False)
119120

121+
def _apply_patches(ctx, version):
122+
patches = PATCHES.get(version, [])
123+
124+
for patch in patches:
125+
ctx.patch(patch, strip = 1)
126+
120127
def _flex_repository(ctx):
121128
version = ctx.attr.version
122129
source = VERSION_URLS[version]
@@ -134,6 +141,7 @@ def _flex_repository(ctx):
134141
}, executable = False)
135142

136143
_hardcode_int_max_log10(ctx, version)
144+
_apply_patches(ctx, version)
137145

138146
ctx.file("WORKSPACE", "workspace(name = {name})\n".format(
139147
name = repr(ctx.name),

tests/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,20 @@ alias(
9393
actual = "@rules_flex_testutil//:all_versions",
9494
tags = ["manual"],
9595
)
96+
97+
genrule(
98+
name = "genrule_noline_top_c",
99+
srcs = ["top_c.l"],
100+
outs = ["top_c.c"],
101+
cmd = "M4=$(M4) $(FLEX) --noline --outfile=$@ $(location top_c.l)",
102+
toolchains = [
103+
"//flex:current_flex_toolchain",
104+
"@rules_m4//m4:current_m4_toolchain",
105+
],
106+
)
107+
108+
sh_test(
109+
name = "flex_noline_test",
110+
srcs = ["flex_noline_test.sh"],
111+
data = [":genrule_noline_top_c"],
112+
)

tests/flex_noline_test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
grep -E '#line [0-9]+' "$RUNFILES_DIR/$TEST_WORKSPACE/tests/top_c.c" || exit 0
4+
exit 1

tests/top_c.l

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%top{
2+
#include <stdio.h>
3+
}
4+
5+
%%
6+
7+
[0-9]+ { printf("NUMBER: %s\n", yytext); }
8+
9+
%%
10+
11+
int main(void) {
12+
yylex();
13+
return 0;
14+
}

0 commit comments

Comments
 (0)