Skip to content

Commit 22a5ce6

Browse files
authored
Merge pull request #465 from ydah/cpp-ci
Add CI env for C++ and fix errors and warnings when compiling as C++
2 parents e61bb6f + 338067f commit 22a5ce6

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

.github/workflows/test.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ jobs:
5454
- run: flex --help
5555
- run: bundle install
5656
- run: bundle exec rspec
57+
test-cpp:
58+
needs: ruby-versions
59+
runs-on: ubuntu-20.04
60+
strategy:
61+
fail-fast: false
62+
matrix:
63+
ruby: ['head']
64+
compiler: ['g++', 'clang++']
65+
steps:
66+
- uses: actions/checkout@v4
67+
- uses: ruby/setup-ruby@v1
68+
with:
69+
ruby-version: ${{ matrix.ruby }}
70+
bundler-cache: true
71+
- run: flex --help
72+
- run: bundle install
73+
- run: bundle exec rspec
74+
env:
75+
COMPILER: ${{ matrix.compiler }}
5776
test-memory:
5877
runs-on: ubuntu-20.04
5978
strategy:

sample/parse.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ factor : number
4646
static enum yytokentype
4747
yylex(YYSTYPE *lval)
4848
{
49-
return 0;
49+
return (enum yytokentype)0;
5050
}
5151

5252
static void yyerror(YYLTYPE *yylloc, const char *msg)

spec/fixtures/integration/named_references.y

-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
%{
22
#include <stdio.h>
33

4-
typedef struct code_location {
5-
int first_line;
6-
int first_column;
7-
int last_line;
8-
int last_column;
9-
} code_location_t;
10-
11-
#define YYLTYPE code_location_t
124
#define YYLLOC_DEFAULT(Current, Rhs, N) \
135
do \
146
if (N) \

spec/fixtures/integration/typed_midrule_actions.y

-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
%{
22
#include <stdio.h>
33

4-
typedef struct code_location {
5-
int first_line;
6-
int first_column;
7-
int last_line;
8-
int last_column;
9-
} code_location_t;
10-
11-
#define YYLTYPE code_location_t
124
#define YYLLOC_DEFAULT(Current, Rhs, N) \
135
do \
146
if (N) \

spec/lrama/integration_spec.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ def exec_command(command)
1111
raise "#{command} failed." unless $?.success?
1212
end
1313

14+
def compiler
15+
ENV['COMPILER'] || "gcc"
16+
end
17+
18+
def file_extension
19+
ENV['COMPILER'] == "gcc" ? ".c" : ".cpp"
20+
end
21+
1422
def test_parser(parser_name, input, expected, expect_success: true, lrama_command_args: [], debug: false)
1523
tmpdir = Dir.tmpdir
1624
grammar_file_path = fixture_path("integration/#{parser_name}.y")
1725
lexer_file_path = fixture_path("integration/#{parser_name}.l")
18-
parser_c_path = tmpdir + "/#{parser_name}.c"
26+
parser_c_path = tmpdir + "/#{parser_name}#{file_extension}"
1927
parser_h_path = tmpdir + "/#{parser_name}.h"
20-
lexer_c_path = tmpdir + "/#{parser_name}-lexer.c"
28+
lexer_c_path = tmpdir + "/#{parser_name}-lexer#{file_extension}"
2129
lexer_h_path = tmpdir + "/#{parser_name}-lexer.h"
2230
obj_path = tmpdir + "/#{parser_name}"
2331

@@ -29,7 +37,7 @@ def test_parser(parser_name, input, expected, expect_success: true, lrama_comman
2937

3038
Lrama::Command.new.run(%W[-H#{parser_h_path} -o#{parser_c_path}] + lrama_command_args + %W[#{grammar_file_path}])
3139
exec_command("flex --header-file=#{lexer_h_path} -o #{lexer_c_path} #{lexer_file_path}")
32-
exec_command("gcc -Wall -ggdb3 -I#{tmpdir} #{parser_c_path} #{lexer_c_path} -o #{obj_path}")
40+
exec_command("#{compiler} -Wall -ggdb3 -I#{tmpdir} #{parser_c_path} #{lexer_c_path} -o #{obj_path}")
3341

3442
out = err = status = nil
3543

@@ -50,7 +58,7 @@ def test_parser(parser_name, input, expected, expect_success: true, lrama_comman
5058

5159
def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
5260
Lrama::Command.new.run(%W[-d -o #{c_path}] + command_args + %W[#{grammar_file_path}])
53-
exec_command("gcc -Wall #{c_path} -o #{obj_path}")
61+
exec_command("#{compiler} -Wall #{c_path} -o #{obj_path}")
5462
end
5563
end
5664

@@ -262,7 +270,7 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
262270
end
263271

264272
describe "sample files" do
265-
let(:c_path) { Dir.tmpdir + "/test.c" }
273+
let(:c_path) { Dir.tmpdir + "/test#{file_extension}" }
266274
let(:obj_path) { Dir.tmpdir + "/test" }
267275

268276
describe "calc.y" do

template/bison/yacc.c

+21-20
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,9 @@ yydestruct (const char *yymsg,
11661166
#endif
11671167

11681168
enum yy_repair_type {
1169-
insert,
1170-
delete,
1171-
shift,
1169+
inserting,
1170+
deleting,
1171+
shifting,
11721172
};
11731173

11741174
struct yy_repair {
@@ -1401,27 +1401,27 @@ yyrecover(yy_state_t *yyss, yy_state_t *yyssp, int yychar<%= output.user_formals
14011401
if (current->repair_length + 1 > YYMAXREPAIR(<%= output.parse_param_name %>))
14021402
continue;
14031403

1404-
yy_repairs *new = (yy_repairs *) YYMALLOC (sizeof (yy_repairs));
1405-
new->id = count;
1406-
new->next = 0;
1407-
new->stack_length = stack_length;
1408-
new->states = (yy_state_t *) YYMALLOC (sizeof (yy_state_t) * (stack_length));
1409-
new->state = new->states + (current->state - current->states);
1410-
YYCOPY (new->states, current->states, current->state - current->states + 1);
1411-
new->repair_length = current->repair_length + 1;
1412-
new->prev_repair = current;
1413-
new->repair.type = insert;
1414-
new->repair.term = (yysymbol_kind_t) yyx;
1404+
yy_repairs *reps = (yy_repairs *) YYMALLOC (sizeof (yy_repairs));
1405+
reps->id = count;
1406+
reps->next = 0;
1407+
reps->stack_length = stack_length;
1408+
reps->states = (yy_state_t *) YYMALLOC (sizeof (yy_state_t) * (stack_length));
1409+
reps->state = reps->states + (current->state - current->states);
1410+
YYCOPY (reps->states, current->states, current->state - current->states + 1);
1411+
reps->repair_length = current->repair_length + 1;
1412+
reps->prev_repair = current;
1413+
reps->repair.type = inserting;
1414+
reps->repair.term = (yysymbol_kind_t) yyx;
14151415

14161416
/* Process PDA assuming next token is yyx */
1417-
if (! yy_process_repairs (new, yyx))
1417+
if (! yy_process_repairs (reps, (yysymbol_kind_t)yyx))
14181418
{
1419-
YYFREE (new);
1419+
YYFREE (reps);
14201420
continue;
14211421
}
14221422

1423-
tail->next = new;
1424-
tail = new;
1423+
tail->next = reps;
1424+
tail = reps;
14251425
count++;
14261426

14271427
if (yyx == yytoken)
@@ -1437,7 +1437,7 @@ yyrecover(yy_state_t *yyss, yy_state_t *yyssp, int yychar<%= output.user_formals
14371437
YYDPRINTF ((stderr,
14381438
"New repairs is enqueued. count: %d, yystate: %d, yyx: %d\n",
14391439
count, yystate, yyx));
1440-
yy_print_repairs (new<%= output.user_args %>);
1440+
yy_print_repairs (reps<%= output.user_args %>);
14411441
}
14421442
}
14431443
}
@@ -1476,7 +1476,8 @@ int yychar;
14761476
/* Default value used for initialization, for pacifying older GCCs
14771477
or non-GCC compilers. */
14781478
#ifdef __cplusplus
1479-
static const YYSTYPE yyval_default = YY_INITIAL_VALUE(YYSTYPE());
1479+
static const YYSTYPE yyval_default = {};
1480+
(void) yyval_default;
14801481
#else
14811482
YY_INITIAL_VALUE (static const YYSTYPE yyval_default;)
14821483
#endif

0 commit comments

Comments
 (0)