Skip to content

Commit

Permalink
Use C-tyle comments in .dot output regardless of the target language.
Browse files Browse the repository at this point in the history
This fixes #495.
  • Loading branch information
skvadrik committed Oct 31, 2024
1 parent 04eb0ae commit cffecf2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
60 changes: 29 additions & 31 deletions src/codegen/pass2_generate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1573,48 +1573,42 @@ static CodeList* gen_cond_goto(Output& output) {
bool warn_cond_ord = output.warn_condition_order;

DCHECK(opts->code_model == CodeModel::GOTO_LABEL);
DCHECK(opts->target != Target::DOT);

const size_t ncond = conds.size();
CodeList* stmts = code_list(alc);

GenGetCond callback(buf.stream(), opts);
const char* getcond = opts->gen_code_yygetcond(buf, callback);

if (opts->target == Target::DOT) {
for (const StartCond& cond : conds) {
buf.cstr("0 -> ").str(cond.name).cstr(" [label=\"state=").str(cond.name).cstr("\"]");
append(stmts, code_text(alc, buf.flush()));
}
if (opts->computed_gotos) {
buf.cstr("*").str(opts->var_cond_table).cstr("[").cstr(getcond).cstr("]");
append(stmts, code_goto(alc, buf.flush()));
} else if (opts->nested_ifs) {
warn_cond_ord &= ncond > 1;
append(stmts, gen_cond_goto_binary(output, getcond, 0, ncond - 1));
} else {
if (opts->computed_gotos) {
buf.cstr("*").str(opts->var_cond_table).cstr("[").cstr(getcond).cstr("]");
append(stmts, code_goto(alc, buf.flush()));
} else if (opts->nested_ifs) {
warn_cond_ord &= ncond > 1;
append(stmts, gen_cond_goto_binary(output, getcond, 0, ncond - 1));
} else {
warn_cond_ord = false;
warn_cond_ord = false;

CodeCases* ccases = code_cases(alc);
for (const StartCond& cond : conds) {
CodeList* body = code_list(alc);
buf.str(opts->cond_label_prefix).str(cond.name);
append(body, code_goto(alc, buf.flush()));
CodeCases* ccases = code_cases(alc);
for (const StartCond& cond : conds) {
CodeList* body = code_list(alc);
buf.str(opts->cond_label_prefix).str(cond.name);
append(body, code_goto(alc, buf.flush()));

append(ccases, code_case_string(alc, body,
gen_cond_enum_elem(buf, opts, cond.name)));
}
if (opts->cond_abort) {
append(ccases, code_case_default(alc, gen_abort(alc)));
}
append(stmts, code_switch(alc, getcond, ccases));
append(ccases, code_case_string(alc, body,
gen_cond_enum_elem(buf, opts, cond.name)));
}

// see note [condition order]
warn_cond_ord &= opts->header_file.empty();
if (warn_cond_ord) {
output.msg.warn.condition_order(block.loc);
if (opts->cond_abort) {
append(ccases, code_case_default(alc, gen_abort(alc)));
}
append(stmts, code_switch(alc, getcond, ccases));
}

// see note [condition order]
warn_cond_ord &= opts->header_file.empty();
if (warn_cond_ord) {
output.msg.warn.condition_order(block.loc);
}

return stmts;
Expand Down Expand Up @@ -2022,7 +2016,11 @@ static void gen_block_dot(Output& output, const Adfas& dfas, CodeList* code) {
Scratchbuf& buf = output.scratchbuf;

append(code, code_text(alc, "digraph re2c {"));
append(code, gen_cond_goto(output));

for (const StartCond& cond : output.block().conds) {
buf.cstr("0 -> ").str(cond.name).cstr(" [label=\"state=").str(cond.name).cstr("\"]");
append(code, code_text(alc, buf.flush()));
}

for (const std::unique_ptr<Adfa>& dfa : dfas) {
if (!dfa->cond.empty()) {
Expand Down
20 changes: 16 additions & 4 deletions src/codegen/pass4_render.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1589,11 +1589,23 @@ static void render(RenderContext& rctx, const Code* code) {
rctx.opts->render_code_line_info(rctx.os, callback);
break;
}
case CodeKind::FINGERPRINT: {
RenderFingerprint callback(rctx);
rctx.opts->render_code_fingerprint(rctx.os, callback);
case CodeKind::FINGERPRINT:
if (rctx.opts->target == Target::DOT) {
// Don't use `code:fingerprint`, as it has language-specific comment syntax.
rctx.os << "/* Generated by re2c";
if (rctx.opts->version) rctx.os << " " PACKAGE_VERSION;
if (rctx.opts->date) {
rctx.os << " on ";
time_t now = time(nullptr);
rctx.os.write(ctime(&now), 24);
}
rctx.os << " */";
render_nl(rctx);
} else {
RenderFingerprint callback(rctx);
rctx.opts->render_code_fingerprint(rctx.os, callback);
}
break;
}
case CodeKind::VAR: {
RenderVar callback(rctx, &code->var);
rctx.opts->render_code_var_local(rctx.os, callback);
Expand Down
12 changes: 6 additions & 6 deletions test/dot/dot2.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* Generated by re2c */
digraph re2c {
1 -> 2 [label="[0x00]"]
1 -> 4 [label="[0x01-0xFF]"]
2 -> 3
3 [label="dot/dot2.re:4"]
4 -> 5
5 [label="dot/dot2.re:5"]
0 -> 1 [label="[0x00]"]
0 -> 3 [label="[0x01-0xFF]"]
1 -> 2
2 [label="dot/dot2.re:8"]
3 -> 4
4 [label="dot/dot2.re:9"]
}
6 changes: 5 additions & 1 deletion test/dot/dot2.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// re2c $INPUT -o $OUTPUT --emit-dot
// re2c $INPUT -o $OUTPUT --emit-dot --lang haskell

// Set a different language to ensure that the generated fingerprint
// still uses C-style comment syntax.

/*!re2c
"\x00" { return 1; }
Expand Down

0 comments on commit cffecf2

Please sign in to comment.