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

Add CI env for C++ and fix errors and warnings when compiling as C++ #465

Merged
merged 10 commits into from
Aug 29, 2024
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -54,6 +54,25 @@ jobs:
- run: flex --help
- run: bundle install
- run: bundle exec rspec
test-cpp:
needs: ruby-versions
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
ruby: ['head']
compiler: ['g++', 'clang++']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: flex --help
- run: bundle install
- run: bundle exec rspec
env:
COMPILER: ${{ matrix.compiler }}
test-memory:
runs-on: ubuntu-20.04
strategy:
2 changes: 1 addition & 1 deletion sample/parse.y
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ factor : number
static enum yytokentype
yylex(YYSTYPE *lval)
{
return 0;
return (enum yytokentype)0;
}

static void yyerror(YYLTYPE *yylloc, const char *msg)
8 changes: 0 additions & 8 deletions spec/fixtures/integration/named_references.y
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
%{
#include <stdio.h>

typedef struct code_location {
int first_line;
int first_column;
int last_line;
int last_column;
} code_location_t;

#define YYLTYPE code_location_t
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
if (N) \
8 changes: 0 additions & 8 deletions spec/fixtures/integration/typed_midrule_actions.y
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
%{
#include <stdio.h>

typedef struct code_location {
int first_line;
int first_column;
int last_line;
int last_column;
} code_location_t;

#define YYLTYPE code_location_t
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
if (N) \
18 changes: 13 additions & 5 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -11,13 +11,21 @@ def exec_command(command)
raise "#{command} failed." unless $?.success?
end

def compiler
ENV['COMPILER'] || "gcc"
end

def file_extension
ENV['COMPILER'] == "gcc" ? ".c" : ".cpp"
end

def test_parser(parser_name, input, expected, expect_success: true, lrama_command_args: [], debug: false)
tmpdir = Dir.tmpdir
grammar_file_path = fixture_path("integration/#{parser_name}.y")
lexer_file_path = fixture_path("integration/#{parser_name}.l")
parser_c_path = tmpdir + "/#{parser_name}.c"
parser_c_path = tmpdir + "/#{parser_name}#{file_extension}"
parser_h_path = tmpdir + "/#{parser_name}.h"
lexer_c_path = tmpdir + "/#{parser_name}-lexer.c"
lexer_c_path = tmpdir + "/#{parser_name}-lexer#{file_extension}"
lexer_h_path = tmpdir + "/#{parser_name}-lexer.h"
obj_path = tmpdir + "/#{parser_name}"

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

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

out = err = status = nil

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

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

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

describe "sample files" do
let(:c_path) { Dir.tmpdir + "/test.c" }
let(:c_path) { Dir.tmpdir + "/test#{file_extension}" }
let(:obj_path) { Dir.tmpdir + "/test" }

describe "calc.y" do
41 changes: 21 additions & 20 deletions template/bison/yacc.c
Original file line number Diff line number Diff line change
@@ -1166,9 +1166,9 @@ yydestruct (const char *yymsg,
#endif

enum yy_repair_type {
insert,
delete,
shift,
inserting,
deleting,
shifting,
};

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

yy_repairs *new = (yy_repairs *) YYMALLOC (sizeof (yy_repairs));
new->id = count;
new->next = 0;
new->stack_length = stack_length;
new->states = (yy_state_t *) YYMALLOC (sizeof (yy_state_t) * (stack_length));
new->state = new->states + (current->state - current->states);
YYCOPY (new->states, current->states, current->state - current->states + 1);
new->repair_length = current->repair_length + 1;
new->prev_repair = current;
new->repair.type = insert;
new->repair.term = (yysymbol_kind_t) yyx;
yy_repairs *reps = (yy_repairs *) YYMALLOC (sizeof (yy_repairs));
reps->id = count;
reps->next = 0;
reps->stack_length = stack_length;
reps->states = (yy_state_t *) YYMALLOC (sizeof (yy_state_t) * (stack_length));
reps->state = reps->states + (current->state - current->states);
YYCOPY (reps->states, current->states, current->state - current->states + 1);
reps->repair_length = current->repair_length + 1;
reps->prev_repair = current;
reps->repair.type = inserting;
reps->repair.term = (yysymbol_kind_t) yyx;

/* Process PDA assuming next token is yyx */
if (! yy_process_repairs (new, yyx))
if (! yy_process_repairs (reps, (yysymbol_kind_t)yyx))
{
YYFREE (new);
YYFREE (reps);
continue;
}

tail->next = new;
tail = new;
tail->next = reps;
tail = reps;
count++;

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