Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip linenum increment for indented code blocks #599

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scan.l
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extern const char *escaped_qstart, *escaped_qend;
#define START_CODEBLOCK(x) do { \
/* Emit the needed line directive... */\
if (indented_code == false) { \
linenum++; \
if (!x) linenum++; \
line_directive_out(NULL, infilename, linenum); \
} \
add_action(M4QSTART); \
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ lexcompat*
!lexcompat.rules
!lexcompat.txt
lineno*
!lineno_generated.l.in
!lineno.rules
!lineno.txt
mem_nr
Expand Down
11 changes: 11 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ SPORADIC_TESTS = \
cxx_restart \
header_nr \
header_r \
lineno_generated \
mem_nr \
mem_r \
mem_c99 \
Expand Down Expand Up @@ -133,6 +134,9 @@ nodist_header_r_SOURCES = header_r_scanner.h
include_by_buffer_direct_SOURCES = include_by_buffer.direct.l
include_by_push_direct_SOURCES = include_by_push.direct.l
include_by_reentrant_direct_SOURCES = include_by_reentrant.direct.l
lineno_generated_SOURCES = lineno_generated.l
lineno_generated.l: lineno_generated.l.in
grep -n '@LINE@' $^ | sed -e 's%^\([0-9]\+\):.*%\1s/@LINE@/\1/%' | sed -f - $^ > [email protected] && mv [email protected] $@
mem_nr_SOURCES = mem_nr.l
mem_r_SOURCES = mem_r.l
mem_c99_SOURCES = mem_c99.l
Expand Down Expand Up @@ -196,6 +200,8 @@ CLEANFILES = \
include_by_buffer.direct.c \
include_by_push.direct.c \
include_by_reentrant.direct.c \
lineno_generated.c \
lineno_generated.l \
mem_nr.c \
mem_r.c \
mem_c99.c \
Expand Down Expand Up @@ -281,6 +287,7 @@ EXTRA_DIST = \
flexname.txt \
lexcompat.txt \
lineno.txt \
lineno_generated.l.in \
posix.txt \
posixlycorrect.txt \
preposix.txt \
Expand Down Expand Up @@ -321,6 +328,10 @@ FLEX = $(top_builddir)/src/flex
.ll.cc: $(FLEX)
$(AM_V_LEX)$(FLEX) $(TESTOPTS) -+ -o $@ $<

#want line numbers, so override the default rule
lineno_generated.c: lineno_generated.l
$(AM_V_LEX)$(FLEX) -o $@ $<

bison_nr_main.$(OBJEXT): bison_nr_parser.h bison_nr_scanner.h
bison_nr_scanner.$(OBJEXT): bison_nr_parser.h

Expand Down
73 changes: 73 additions & 0 deletions tests/lineno_generated.l.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of flex.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/

%{
/* Test generated line numbers. Be careful editing this file
as it's all about ensuring flex generates correct line
directives, so the @LINE@ markers must be on the same line as a
their corresponding __LINE__ macros.
*/
#include <stdio.h>
#include "config.h"

static int defs_percent_expect = @LINE@, defs_percent_got = __LINE__;
static int error_count = 0;

static void test_line (const char *desc, int expect, int got);

%}

static int defs_indent_expect = @LINE@, defs_indent_got = __LINE__;

%option nounput noyywrap noinput
%option warn

%%
test_line ("prolog indent", @LINE@, __LINE__);
%{
test_line ("prolog percent", @LINE@, __LINE__);
%}
\n { test_line ("rules braced", @LINE@, __LINE__); }
. test_line ("rules bare", @LINE@, __LINE__);

%%

static void
test_line (const char *desc, int expect, int got)
{
printf ("%s: expect: %d got: %d\n", desc, expect, got);
error_count += expect != got;
}

int
main (void)
{
yybuffer buffer = yy_scan_string ("a\na\n");
yylex ();
yy_delete_buffer (buffer);
test_line ("defs percent", defs_percent_expect, defs_percent_got);
test_line ("defs indent", defs_indent_expect, defs_indent_got);
test_line ("user code", @LINE@, __LINE__);

return error_count != 0;
}