From: David Howells Date: Fri, 11 Apr 2014 12:21:28 +0000 (+0100) Subject: rxgen: Save trailing comments on error codes in .xg files for exception message X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=88b88b71f09e19382c704763310087b383e890e3;p=users%2Fdhowells%2Fkafs-utils.git rxgen: Save trailing comments on error codes in .xg files for exception message Save the trailing comment on an error code definition in a .xg file and attach it to the python exception raised if a matching abort is received. Signed-off-by: David Howells --- diff --git a/py_rxgen.c b/py_rxgen.c index db8713a..acdfa9b 100644 --- a/py_rxgen.c +++ b/py_rxgen.c @@ -1046,6 +1046,7 @@ static int py_rxgen_received_abort_cmp(const void *key, const void *_entry) PyObject *py_rxgen_received_abort(struct rx_call *call) { const struct kafs_abort_list *entry; + const char *msg; PyObject *ex; entry = bsearch((void *)(unsigned long)call->abort_code, @@ -1054,10 +1055,16 @@ PyObject *py_rxgen_received_abort(struct rx_call *call) sizeof(kafs_abort_map[0]), py_rxgen_received_abort_cmp); - if (entry) + if (entry) { ex = entry->obj; - else + msg = entry->msg; + } else { ex = kafs_remote_abort; + msg = NULL; + } - return PyErr_Format(ex, "Aborted %u", call->abort_code); + if (msg) + return PyErr_Format(ex, "Aborted %u: %s", call->abort_code, msg); + else + return PyErr_Format(ex, "Aborted %u", call->abort_code); } diff --git a/py_rxgen.h b/py_rxgen.h index 8fd2d16..3ffce61 100644 --- a/py_rxgen.h +++ b/py_rxgen.h @@ -94,6 +94,7 @@ extern int py_rxgen_premarshal_structs(void *array, size_t n, size_t size, size_ */ struct kafs_abort_list { uint32_t id; + const char *msg; PyObject *obj; }; diff --git a/rxgen/emit_py_module.pm b/rxgen/emit_py_module.pm index ceca7ed..38b31b2 100644 --- a/rxgen/emit_py_module.pm +++ b/rxgen/emit_py_module.pm @@ -33,13 +33,14 @@ sub emit_py_module() { print PYOUT "\n"; print PYHDR "extern struct kafs_abort_list kafs_abort_map[", $abort_count, "];\n"; - my %abort_table_map = (); my $index = 0; print PYOUT "struct kafs_abort_list kafs_abort_map[", $abort_count, "] = {\n"; foreach my $id (sort {$a <=> $b} keys(%abort_ids)) { - my $name = $abort_ids{$id}; - print PYOUT "\t{ .id = ", $name, " }, /* ", $id, " */\n"; - $abort_table_map{$name} = $index++; + my $abort = $abort_ids{$id}; + print PYOUT "\t{ .id = ", $abort->{sym}; + print PYOUT ", .msg = \"", $abort->{msg}, "\"" if ($abort->{msg}); + print PYOUT " }, /* ", $abort->{id}, " */\n"; + $abort->{index} = $index++; } print PYOUT "};\n\n"; @@ -123,8 +124,8 @@ sub emit_py_module() { foreach $_ (sort(keys(%packages))) { my $pkg = $packages{$_}; - my $codes = $pkg->{abort_codes}; - next unless (@{$codes}); + my $abort_codes = $pkg->{abort_codes}; + next unless (@{$abort_codes}); my $pkg_abort = $pkg->{name} . "Abort"; my $pkg_sym = "kafs_" . $pkg->{name} . "_abort"; @@ -136,16 +137,16 @@ sub emit_py_module() { print PYOUT "\tPy_INCREF(", $pkg_sym, ");\n"; print PYOUT "\tPyModule_AddObject(m, \"", $pkg_abort, "\", ", $pkg_sym, ");\n"; - foreach my $code (sort(@{$codes})) { - my $code_abort = "Abort" . $code; - my $code_sym = "kafs_abort_map[" . $abort_table_map{$code} . "].obj"; + foreach my $abort (sort { $a->{id} <=> $b->{id}; } @{$abort_codes}) { + my $abort_name = "Abort" . $abort->{sym}; + my $abort_var = "kafs_abort_map[" . $abort->{index} . "].obj"; print PYOUT "\n"; - print PYOUT "\t", $code_sym, " = PyErr_NewException(\"kafs.", $code_abort, "\", ", $pkg_sym, ", NULL);\n"; - print PYOUT "\tif (!", $code_sym, ")\n"; + print PYOUT "\t", $abort_var, " = PyErr_NewException(\"kafs.", $abort_name, "\", ", $pkg_sym, ", NULL);\n"; + print PYOUT "\tif (!", $abort_var, ")\n"; print PYOUT "\t\treturn NULL;\n"; - print PYOUT "\tPy_INCREF(", $code_sym, ");\n"; - print PYOUT "\tPyModule_AddObject(m, \"", $code_abort, "\", ", $code_sym, ");\n"; + print PYOUT "\tPy_INCREF(", $abort_var, ");\n"; + print PYOUT "\tPyModule_AddObject(m, \"", $abort_name, "\", ", $abort_var, ");\n"; } } diff --git a/rxgen/rxgen.pl b/rxgen/rxgen.pl index 5c5a750..9a2e18a 100755 --- a/rxgen/rxgen.pl +++ b/rxgen/rxgen.pl @@ -175,11 +175,13 @@ sub parse_xg($) { $error_codes = 0 if ($line eq ""); # Discard comments + my $line_comment = ""; find_comment_terminator: if ($comment_discard) { # Find the terminator for a comment we're discarding - if ($line =~ m@.*[*]/(.*)@) { - $line = $pre_comment . $1; + if ($line =~ m@(.*)[*]/(.*)@) { + $line_comment = $1; + $line = $pre_comment . $2; $comment_discard = 0; } else { $line = $pre_comment; @@ -208,6 +210,10 @@ discarded_comments: $line =~ s!\t!!g; next if (!$line); + $line_comment =~ s/^\s+//; + $line_comment =~ s/\s+$//; + $line_comment =~ s/\s+/ /g; + #print "'$line'\n"; # Complain about #defines @@ -249,9 +255,16 @@ discarded_comments: die $where, ": Duplicate abort ID" if (exists $abort_ids{$v}); - push @{$abort_codes}, $c; - $abort_syms{$c} = $v; - $abort_ids{$v} = $c; + + my %abort = ( + sym => $c, + id => $v, + msg => $line_comment, + ); + + push @{$abort_codes}, \%abort; + $abort_syms{$c} = \%abort; + $abort_ids{$v} = \%abort; $abort_count++; } next;