Skip to content

Commit fe597ce

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 fe597ce

File tree

7 files changed

+102
-1
lines changed

7 files changed

+102
-1
lines changed

docs/rules_flex.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ A [`FlexToolchainInfo`](#FlexToolchainInfo).
168168
<pre>
169169
load("@rules_flex//flex:flex.bzl", "flex_repository")
170170

171-
flex_repository(<a href="#flex_repository-name">name</a>, <a href="#flex_repository-extra_copts">extra_copts</a>, <a href="#flex_repository-extra_linkopts">extra_linkopts</a>, <a href="#flex_repository-repo_mapping">repo_mapping</a>, <a href="#flex_repository-version">version</a>)
171+
flex_repository(<a href="#flex_repository-name">name</a>, <a href="#flex_repository-extra_copts">extra_copts</a>, <a href="#flex_repository-extra_linkopts">extra_linkopts</a>, <a href="#flex_repository-patches">patches</a>, <a href="#flex_repository-repo_mapping">repo_mapping</a>, <a href="#flex_repository-version">version</a>)
172172
</pre>
173173

174174
Repository rule for Flex.
@@ -194,6 +194,7 @@ flex_repository(
194194
| <a id="flex_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
195195
| <a id="flex_repository-extra_copts"></a>extra_copts | Additional C compiler options to use when building Flex. | List of strings | optional | `[]` |
196196
| <a id="flex_repository-extra_linkopts"></a>extra_linkopts | Additional linker options to use when building Flex. | List of strings | optional | `[]` |
197+
| <a id="flex_repository-patches"></a>patches | A mapping from Flex versions to lists of patch files to apply, relative to the root of the Flex source repository. Each patch should be in standard [unified diff format](https://en.wikipedia.org/wiki/Diff#Unified_format). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional | `{"2.6.4": ["//patches:0001-fix-noline-for-top-directives.patch"]}` |
197198
| <a id="flex_repository-repo_mapping"></a>repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
198199
| <a id="flex_repository-version"></a>version | A supported version of Flex. | String | required | |
199200

flex/rules/flex_repository.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def _flex_repository(ctx):
135135

136136
_hardcode_int_max_log10(ctx, version)
137137

138+
for patch in ctx.attr.patches.get(version, []):
139+
ctx.patch(Label(patch), strip = 1)
140+
138141
ctx.file("WORKSPACE", "workspace(name = {name})\n".format(
139142
name = repr(ctx.name),
140143
))
@@ -177,5 +180,18 @@ flex_repository(
177180
"extra_linkopts": attr.string_list(
178181
doc = "Additional linker options to use when building Flex.",
179182
),
183+
"patches": attr.string_list_dict(
184+
doc = """
185+
A mapping from Flex versions to lists of patch files to apply,
186+
relative to the root of the Flex source repository. Each patch
187+
should be in standard [unified diff
188+
format](https://en.wikipedia.org/wiki/Diff#Unified_format).
189+
""",
190+
default = {
191+
"2.6.4": [
192+
"//patches:0001-fix-noline-for-top-directives.patch",
193+
],
194+
},
195+
),
180196
},
181197
)
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+

patches/BUILD

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

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)