[m-rev.] diff: mdemangle
Zoltan Somogyi
zs at csse.unimelb.edu.au
Thu Nov 27 13:51:15 AEDT 2008
util/mdemangle.c:
Convert this file to four-space indentation, and fix code style.
Zoltan.
cvs diff: Diffing .
Index: mdemangle.c
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/util/mdemangle.c,v
retrieving revision 1.54
diff -u -b -r1.54 mdemangle.c
--- mdemangle.c 21 Dec 2006 11:11:37 -0000 1.54
+++ mdemangle.c 26 Nov 2008 07:01:16 -0000
@@ -1,3 +1,6 @@
+/*
+** vim: ft=c ts=4 sw=4 et
+*/
/*---------------------------------------------------------------------------*/
/*
@@ -33,7 +36,8 @@
static void demangle(const char *name);
static const char *strip_module_name(char **start_ptr, char *end,
- const char *special_prefixes[], const char *special_suffixes[]);
+ const char *special_prefixes[],
+ const char *special_suffixes[]);
static MR_bool check_for_suffix(char *start, char *position,
const char *suffix, int sizeof_suffix, int *mode_num2);
static char *fix_mangled_ascii(char *str, char **end);
@@ -44,7 +48,8 @@
static MR_bool cut_trailing_underscore_integer(char *str,
char **end, int *num);
static MR_bool strip_prefix(char **str, const char *prefix);
-static MR_bool strip_suffix(const char *str, char **end, const char *suffix);
+static MR_bool strip_suffix(const char *str, char **end,
+ const char *suffix);
static MR_bool strip_leading_integer(char **start_ptr, int *num);
/*
@@ -66,6 +71,7 @@
** This variable gets set if the symbols MR_grade_* or MR_mercury_grade
** were found. If it gets set, then we print out the error message below.
*/
+
char *found_grade_symbol = NULL;
const char probably_grade_error[] =
"Mercury Linker:\n"
@@ -79,7 +85,7 @@
{
const char *progname = argv[0];
- /* we should use getopt_long(), but for one option, that is overkill */
+ /* We should use getopt_long(), but for one option, that is overkill. */
while (argc > 1 && argv[1][0] == '-') {
if (strcmp(argv[1], "-e") == 0 ||
strcmp(argv[1], "--explain-link-errors") == 0)
@@ -90,43 +96,53 @@
argc--, argv++;
break;
} else {
- fprintf(stderr, "%s: unknown option `%s'\n",
- progname, argv[1]);
+ fprintf(stderr, "%s: unknown option `%s'\n", progname, argv[1]);
exit(1);
}
}
if (argc > 1) {
+ int i;
+
/*
- ** invoke demangle() on each command line argument
+ ** Invoke demangle() on each command line argument.
*/
- int i;
for (i = 1; i < argc; i++) {
demangle(argv[i]);
putchar('\n');
}
} else {
/*
- ** copy stdin to stdout, calling demangle() for
- ** every valid C identifier in the input
+ ** Copy stdin to stdout, calling demangle() for every valid
+ ** C identifier in the input.
*/
+
for (;;) {
char buf[MAX_SYMBOL_LENGTH];
- size_t len = 0;
- int c = getchar();
+ size_t len;
+ int c;
+
+ len = 0;
+ c = getchar();
while (c != EOF && (isalnum(c) || c == '_')) {
- if (len >= sizeof(buf) - 1)
+ if (len >= sizeof(buf) - 1) {
break;
+ }
+
buf[len++] = c;
c = getchar();
}
+
if (len > 0) {
buf[len] = '\0';
demangle(buf);
fflush(stdout);
}
- if (c == EOF)
+
+ if (c == EOF) {
break;
+ }
+
putchar(c);
}
}
@@ -140,8 +156,9 @@
}
/*
-** demangle() - convert a mangled Mercury identifier into
-** human-readable form and then print it to stdout
+** demangle():
+** Convert a mangled Mercury identifier into human-readable form
+** and then print it to stdout.
*/
static void
@@ -192,7 +209,7 @@
static const char MR_grade[] = "MR_grade_";
static const char MR_runtime_grade[] = "MR_runtime_grade";
- static const char * trailing_context_1[] = {
+ static const char *trailing_context_1[] = {
introduced,
deforestation,
accumulator,
@@ -201,14 +218,15 @@
unify1, compare1, index1, initialise1,
NULL
};
- static const char * trailing_context_1_hl_suffixes[] = {
+
+ static const char *trailing_context_1_hl_suffixes[] = {
ua_suffix,
ua_suffix2,
ho_suffix,
NULL
};
- static const char * trailing_context_2[] = {
+ static const char *trailing_context_2[] = {
type_ctor_layout,
type_ctor_info,
type_ctor_functors,
@@ -216,7 +234,7 @@
NULL
};
- static const char * trailing_context_3[] = {
+ static const char *trailing_context_3[] = {
arity_string,
NULL
};
@@ -232,10 +250,10 @@
MR_bool high_level = MR_TRUE;
MR_bool matched = MR_FALSE;
const char *pred_or_func; /* either "predicate" or "function" */
- /* does this proc have any unused arguments */
MR_bool unused_args = MR_FALSE;
- /* __uab suffix rather than __ua */
+ /* does this proc have any unused arguments */
MR_bool unused_args_extra = MR_FALSE;
+ /* __uab suffix rather than __ua */
int unused_args_num = 0;
MR_bool higher_order = MR_FALSE; /* has this proc been specialized */
int higher_order_num = 0;
@@ -247,36 +265,34 @@
char *end_of_lambda_pred_name = NULL;
const char *lambda_kind = NULL;
enum { ORDINARY, UNIFY, COMPARE, INDEX, INITIALISE,
- LAMBDA, DEFORESTATION, ACCUMULATOR, TYPE_SPEC }
- category;
+ LAMBDA, DEFORESTATION, ACCUMULATOR, TYPE_SPEC } category;
enum { COMMON, INFO, LAYOUT, FUNCTORS } data_category;
- const char * class_name;
+ const char *class_name;
int class_arity;
char class_arg_buf[MAX_SYMBOL_LENGTH];
int class_arg_num;
- const char * class_arg;
- const char * type_spec_sub;
+ const char *class_arg;
+ const char *type_spec_sub;
/*
- ** copy orig_name to a local buffer which we can modify,
- ** making sure that we don't overflow the buffer
+ ** Copy orig_name to a local buffer which we can modify,
+ ** making sure that we don't overflow the buffer.
*/
+
if (strlen(orig_name) >= sizeof(name)) {
goto too_long;
}
strcpy(name, orig_name);
/*
- ** skip any leading underscore inserted by the C compiler
- ** (but don't skip it if it came from the `_entry_' prefix)
+ ** Skip any leading underscore inserted by the C compiler
+ ** (but don't skip it if it came from the `_entry_' prefix).
*/
if (*start == '_' && strncmp(start, entry, strlen(entry)) != 0) {
start++;
}
- /*
- ** check for `MR_grade_*' and `MR_runtime_grade'.
- */
+ /* Check for `MR_grade_*' and `MR_runtime_grade'. */
if (strncmp(start, MR_grade, strlen(MR_grade)) == 0 ||
strcmp(start, MR_runtime_grade) == 0)
{
@@ -289,14 +305,10 @@
goto wrong_format;
}
- /*
- ** skip the `_entry_' prefix, if any
- */
+ /* Skip the `_entry_' prefix, if any. */
strip_prefix(&start, entry);
- /*
- ** strip off the `mercury__' prefix, if any
- */
+ /* Strip off the `mercury__' prefix, if any. */
if (strip_prefix(&start, mercury)) {
matched = MR_TRUE;
}
@@ -315,27 +327,32 @@
goto not_plain_mercury;
}
- if (end == start) goto not_plain_mercury;
+ if (end == start) {
+ goto not_plain_mercury;
+ }
/*
- ** if we got to an `i', that means it is an internal
- ** label of the form `mercury__append_3_0_i1'
- ** in that case, save the internal label number and then
- ** get the mode number
+ ** If we got to an `i', that means it is an internal label of the form
+ ** `mercury__append_3_0_i1'. In that case, save the internal label number
+ ** and then get the mode number.
*/
+
if (*--end == 'i') {
internal = mode_num;
- if (end == start || *--end != '_') goto not_plain_mercury;
+ if (end == start || *--end != '_') {
+ goto not_plain_mercury;
+ }
if (!cut_trailing_underscore_integer(start, &end, &mode_num)) {
goto not_plain_mercury;
}
}
- if (end == start) goto not_plain_mercury;
- /*
- ** strip off the `fn__' prefix, if any
- */
+ if (end == start) {
+ goto not_plain_mercury;
+ }
+
+ /* Strip off the `fn__' prefix, if any. */
if (strip_prefix(&start, func_prefix)) {
high_level = MR_FALSE;
pred_or_func = "function";
@@ -349,8 +366,7 @@
pred_or_func = "predicate";
} else {
/*
- ** It's not a function.
- ** But it could be either an LLDS predicate,
+ ** It is not a function. But it could be either an LLDS predicate,
** or an MLDS compiler-generated predicate.
*/
high_level = (strstr(start, unify2) ||
@@ -360,10 +376,12 @@
pred_or_func = "predicate";
}
- if (end == start) goto not_plain_mercury;
+ if (end == start) {
+ goto not_plain_mercury;
+ }
/*
- ** scan back past the arity number and then parse it
+ ** Scan back past the arity number and then parse it.
*/
if (!cut_trailing_underscore_integer(start, &end, &arity)) {
@@ -374,12 +392,12 @@
module = strip_module_name(&start, end,
trailing_context_1, trailing_context_1_hl_suffixes);
}
+
/*
- ** Now start processing from the start of the string again.
- ** Check whether the start of the string matches the name of
- ** one of the special compiler-generated predicates; if so,
- ** set the `category' to the appropriate value and then
- ** skip past the prefix.
+ ** Now start processing from the start of the string again. Check whether
+ ** the start of the string matches the name of one of the special
+ ** compiler-generated predicates; if so, set the `category' to the
+ ** appropriate value and then skip past the prefix.
*/
if (strip_prefix(&start, unify1)) {
@@ -395,21 +413,21 @@
} else {
category = ORDINARY;
/*
- ** For ordinary predicates, we should have matched
- ** against something by now --
+ ** For ordinary predicates, we should have matched against something
+ ** by now --
** either the "mercury__" prefix, for LLDS mangling,
** or the "_f" or "_p" suffix, for MLDS mangling.
*/
- if (!matched) goto not_plain_mercury;
+ if (!matched) {
+ goto not_plain_mercury;
+ }
}
if (category != ORDINARY && start[0] == '_') {
start++;
}
- /*
- ** Fix any ascii codes mangled in the predicate name
- */
+ /* Fix any ascii codes mangled in the predicate name. */
start = fix_mangled_ascii(start, &end);
/*
@@ -421,20 +439,24 @@
position = end; /* save end of name */
do {
- if (position == start) goto wrong_format;
+ if (position == start) {
+ goto wrong_format;
+ }
position--;
} while (MR_isdigit(*position));
- /* get the mode number */
+ /* get the mode number */
if (check_for_suffix(start, position, ua_suffix,
- sizeof(ua_suffix), &mode_num2)) {
+ sizeof(ua_suffix), &mode_num2))
+ {
unused_args = MR_TRUE;
unused_args_extra = MR_FALSE;
unused_args_num = mode_num;
end = position + 1 - (sizeof(ua_suffix) - 1);
mode_num = mode_num2 % 10000;
} else if (check_for_suffix(start, position, ua_suffix2,
- sizeof(ua_suffix2), &mode_num2)) {
+ sizeof(ua_suffix2), &mode_num2))
+ {
unused_args = MR_TRUE;
unused_args_extra = MR_TRUE;
unused_args_num = mode_num;
@@ -451,11 +473,15 @@
position = end;
do {
- if (position == start) goto wrong_format;
+ if (position == start) {
+ goto wrong_format;
+ }
position--;
} while (MR_isdigit(*position));
+
if (check_for_suffix(start, position, ho_suffix,
- sizeof(ho_suffix), &higher_order_num)) {
+ sizeof(ho_suffix), &higher_order_num))
+ {
end = position + 1 - (sizeof(ho_suffix) - 1);
higher_order = MR_TRUE;
}
@@ -479,16 +505,14 @@
}
if (!high_level) {
- module = strip_module_name(&start, end, trailing_context_1,
- NULL);
+ module = strip_module_name(&start, end, trailing_context_1, NULL);
}
/*
- ** look for "IntroducedFrom" or "DeforestationIn" or "AccFrom"
- ** of "TypeSpecOf"
- ** XXX This don't yet handle multiple prefixes. If we get an
- ** error after this point, just treat predicate name as an
- ** ordinary predicate.
+ ** Look for "IntroducedFrom" or "DeforestationIn" or "AccFrom"
+ ** or "TypeSpecOf".
+ ** XXX This don't yet handle multiple prefixes. If we get an error after
+ ** this point, just treat predicate name as an ordinary predicate.
*/
name_before_prefixes = start;
if (category == ORDINARY) {
@@ -510,13 +534,12 @@
lambda_kind = "pred";
} else if (strip_prefix(&start, func)) {
lambda_kind = "func";
- } else if (category == TYPE_SPEC
- && strip_prefix(&start, porf))
- {
+ } else if (category == TYPE_SPEC && strip_prefix(&start, porf)) {
lambda_kind = "";
} else {
goto wrong_format;
}
+
lambda_pred_name = start;
if (!find_double_underscore(&start, end)) {
category = ORDINARY;
@@ -527,13 +550,14 @@
}
if (category == TYPE_SPEC) {
if (start < end && *start == '[') {
- int nest_level = 1;
+ int nest_level;
+
+ nest_level = 1;
type_spec_sub = start;
start++;
- /*
- ** Handle matched brackets in type names.
- */
+
+ /* Handle matched brackets in type names. */
while (start < end) {
if (*start == '[') {
nest_level++;
@@ -552,17 +576,14 @@
start = name_before_prefixes;
} else {
/*
- ** The compiler adds a redundant mode
- ** number to the predicate name
- ** to avoid creating two predicates
- ** with the same name (deep profiling
- ** doesn't like that). It isn't used
- ** here, so we just ignore it.
- ** The compiler also adds a version
- ** number for the argument order used
- ** for specialized versions, which
- ** can also be ignored.
+ ** The compiler adds a redundant mode number to the
+ ** predicate name to avoid creating two predicates
+ ** with the same name (deep profiling doesn't like that).
+ ** It isn't used here, so we just ignore it. The compiler
+ ** also adds a version number for the argument order used
+ ** for specialized versions, which can also be ignored.
*/
+
*end_of_lambda_pred_name = '\0';
start = lambda_pred_name;
}
@@ -579,18 +600,16 @@
}
while (start < end && MR_isdigit(*start)) {
- lambda_line = lambda_line * 10 +
- (*start - '0');
+ lambda_line = lambda_line * 10 + (*start - '0');
start++;
}
- if (strip_prefix(&start, "__")) {
+ if (strip_prefix(&start, "__")) {
if (start < end && MR_isdigit(*start)) {
lambda_seq_number = 0;
while (start < end && MR_isdigit(*start)) {
lambda_seq_number =
- lambda_seq_number * 10 +
- (*start - '0');
+ lambda_seq_number * 10 + (*start - '0');
start++;
}
*end_of_lambda_pred_name = '\0';
@@ -606,40 +625,49 @@
}
/*
- ** Now, finally, we can print the demangled symbol name
+ ** Now, finally, we can print the demangled symbol name.
*/
+
printf("<");
switch(category) {
case UNIFY:
printf("unification predicate for type '%s.%s'/%d mode %d",
module, start, arity, mode_num);
break;
+
case COMPARE:
printf("compare/3 predicate for type '%s.%s'/%d",
module, start, arity);
break;
+
case INDEX:
printf("index/2 predicate for type '%s.%s'/%d",
module, start, arity);
break;
+
case INITIALISE:
printf("initialisation predicate for type '%s.%s'/%d",
module, start, arity);
break;
+
case LAMBDA:
printf("%s goal (#%d) from '%s' in module '%s' line %d",
lambda_kind, lambda_seq_number,
lambda_pred_name, module, lambda_line);
break;
+
case ACCUMULATOR:
printf("accumulator procedure from '%s' in module '%s' line %d",
lambda_pred_name, module, lambda_line);
break;
+
case DEFORESTATION:
- printf("deforestation procedure (#%d) from '%s' in module '%s' line %d",
+ printf("deforestation procedure (#%d) from '%s' "
+ "in module '%s' line %d",
lambda_seq_number, lambda_pred_name,
module, lambda_line);
break;
+
case TYPE_SPEC:
default:
if (*module == '\0') {
@@ -650,23 +678,27 @@
pred_or_func, module, start, arity, mode_num);
}
}
+
if (category == TYPE_SPEC) {
printf(" (type specialized %s)", type_spec_sub);
}
+
if (higher_order) {
printf(" (specialized [#%d])", higher_order_num);
}
+
if (unused_args) {
if (unused_args_extra) {
- printf(" (minus extra unused args [#%d])",
- unused_args_num);
+ printf(" (minus extra unused args [#%d])", unused_args_num);
} else {
printf(" (minus unused args [#%d])", unused_args_num);
}
}
+
if (internal != -1) {
printf(" label %d", internal);
}
+
printf(">");
return;
@@ -679,13 +711,12 @@
** Undo any in-place modifications done while trying to demangle
** predicate names.
*/
+
strcpy(name, orig_name);
start = name;
end = name + strlen(name);
- /*
- ** skip any leading underscore inserted by the C compiler
- */
+ /* Skip any leading underscore inserted by the C compiler. */
if (*start == '_') {
start++;
}
@@ -704,8 +735,7 @@
if (strip_prefix(&start, base_typeclass_info)) {
goto typeclass_info;
}
- /* also try the old format,
- in case we're demangling old files */
+ /* Also try the old format, in case we're demangling old files. */
if (strip_prefix(&start, underscores_base_typeclass_info)) {
goto typeclass_info;
}
@@ -724,7 +754,9 @@
** For MLDS, the module name gets duplicated (XXX why?)
** So here we must replace `foo.foo' with just `foo'.
*/
- size_t half_len = strlen(module) / 2;
+ size_t half_len;
+
+ half_len = strlen(module) / 2;
if (strncmp(module, module + half_len + 1, half_len) != 0) {
goto wrong_format;
}
@@ -758,7 +790,6 @@
start = fix_mangled_ascii(start, &end);
switch (data_category) {
-
case INFO:
if (*module == '\0') {
printf("<type_ctor_info for type '%s'/%d>",
@@ -768,6 +799,7 @@
module, start, arity);
}
break;
+
case LAYOUT:
if (*module == '\0') {
printf("<type_ctor_layout for type '%s'/%d>",
@@ -777,6 +809,7 @@
module, start, arity);
}
break;
+
case FUNCTORS:
if (*module == '\0') {
printf("<type_ctor_functors for type '%s'/%d>",
@@ -786,6 +819,7 @@
module, start, arity);
}
break;
+
case COMMON:
printf("<shared constant number %d for module %s>",
arity, module);
@@ -793,16 +827,16 @@
default:
goto wrong_format;
-
}
+
return;
typeclass_info:
/*
- ** Parse the class name and class arity, which have the following
- ** layout:
+ ** Parse the class name and class arity, which have the following layout:
** <module-qualified class name>__arity<arity>__
*/
+
class_name = strip_module_name(&start, end, trailing_context_3, NULL);
/* XXX fix_mangled_ascii() */
if (!(strip_prefix(&start, arity_string)
@@ -813,36 +847,32 @@
}
/*
- ** Parse the class argument types, which each have the following
- ** layout:
+ ** Parse the class argument types, which each have the following layout:
** <module-qualified type name>__arity<arity>__
**
- ** We store the human-readable formatted output in
- ** class_arg_buf as we go.
+ ** We store the human-readable formatted output in class_arg_buf as we go.
*/
+
fix_mangled_ascii(start, &end);
strcpy(class_arg_buf, "");
for (class_arg_num = 0; class_arg_num < class_arity; class_arg_num++) {
if (class_arg_num != 0) {
strcat(class_arg_buf, ", ");
}
- class_arg = strip_module_name(&start, end, trailing_context_3,
- NULL);
+ class_arg = strip_module_name(&start, end, trailing_context_3, NULL);
if (!(strip_prefix(&start, arity_string)
&& strip_leading_integer(&start, &arity)
&& strip_prefix(&start, "__")))
{
goto wrong_format;
}
- sprintf(class_arg_buf + strlen(class_arg_buf),
- "%s/%d", class_arg, arity);
+
+ sprintf(class_arg_buf + strlen(class_arg_buf), "%s/%d",
+ class_arg, arity);
}
- /*
- ** now print the results
- */
- printf("<instance declaration for %s(%s)>",
- class_name, class_arg_buf);
+ /* Now print the results. */
+ printf("<instance declaration for %s(%s)>", class_name, class_arg_buf);
return;
wrong_format:
@@ -858,13 +888,13 @@
return;
} /* end demangle() */
- /*
- ** Remove a module name prefix.
- ** Just keep munching up double-underscores until we
- ** get to something that matches the specified trailing context,
- ** at which point we stop, or until there are no double-underscores
- ** left.
- */
+/*
+** Remove a module name prefix.
+** Just keep munching up double-underscores until we get to something
+** that matches the specified trailing context, at which point we stop,
+** or until there are no double-underscores left.
+*/
+
static const char *
strip_module_name(char **start_ptr, char *end,
const char *special_prefixes[], const char *special_suffixes[])
@@ -882,46 +912,49 @@
module = start;
module_end = start;
while ((next_double_underscore = strstr(start, "__")) != NULL) {
- int len, i;
+ int len;
+ int i;
+ MR_bool stop;
- /*
- ** Check for special cases
- */
- MR_bool stop = MR_FALSE;
+ /* Check for special cases. */
+ stop = MR_FALSE;
for (i = 0; special_prefixes[i] != NULL; i++) {
- if (strncmp(start,
- special_prefixes[i],
+ if (strncmp(start, special_prefixes[i],
strlen(special_prefixes[i])) == 0)
{
stop = MR_TRUE;
}
}
- for (i = 0; special_suffixes != NULL &&
- special_suffixes[i] != NULL;
- i++) {
- if (strncmp(next_double_underscore,
- special_suffixes[i],
+ for (i = 0; special_suffixes != NULL && special_suffixes[i] != NULL;
+ i++)
+ {
+ if (strncmp(next_double_underscore, special_suffixes[i],
strlen(special_suffixes[i])) == 0)
{
stop = MR_TRUE;
}
}
- if (stop) break;
+
+ if (stop) {
+ break;
+ }
len = next_double_underscore - start;
if (module != module_end) {
/*
- ** append a module qualifier, and
- ** shift the module name into the right place
+ ** Append a module qualifier, and shift the module name
+ ** into the right place.
*/
+
*module_end = ':';
module_end++;
memmove(module_end, start, len);
}
- module_end += len;
+ module_end += len;
start = next_double_underscore + 2;
}
+
if (module == module_end) {
module = "";
} else {
@@ -932,13 +965,13 @@
return module;
}
- /*
- ** Remove the prefix from a string, if it has it.
- ** Returns MR_TRUE if the string has that prefix, and
- ** *str will then point to the rest of that string.
- ** If the string doesn't have that prefix, *str will
- ** be unchanged, and the function will return MR_FALSE.
- */
+/*
+** Remove the prefix from a string, if it has it.
+** Returns MR_TRUE if the string has that prefix, and *str will then point
+** to the rest of that string. If the string doesn't have that prefix,
+** *str will be unchanged, and the function will return MR_FALSE.
+*/
+
static MR_bool
strip_prefix(char **str, const char *prefix)
{
@@ -950,15 +983,16 @@
*str += len;
return MR_TRUE;
}
+
return MR_FALSE;
}
- /*
- ** Remove the suffix from a string, if it has it.
- ** Returns MR_TRUE if the string between start and *end
- ** has the specified suffix, and sets *end to point to
- ** the beginning of the suffix.
- */
+/*
+** Remove the suffix from a string, if it has it.
+** Returns MR_TRUE if the string between start and *end has the specified
+** suffix, and sets *end to point to the beginning of the suffix.
+*/
+
static MR_bool
strip_suffix(const char *start, char **end, const char *suffix)
{
@@ -970,31 +1004,39 @@
*end -= len;
return MR_TRUE;
}
+
return MR_FALSE;
}
- /*
- ** If the string pointed to by *start_ptr starts with
- ** an integer, then advance *start_ptr past the leading integer,
- ** store the value of the integer in the int pointed to by `num',
- ** and return true; otherwise leave *start_ptr unchanged and
- ** return false. (The string itself is always left unchanged.)
- */
+/*
+** If the string pointed to by *start_ptr starts with an integer,
+** then advance *start_ptr past the leading integer, store the value
+** of the integer in the int pointed to by `num', and return true;
+** otherwise leave *start_ptr unchanged and return false.
+** (The string itself is always left unchanged.)
+*/
+
static MR_bool
strip_leading_integer(char **start_ptr, int *num)
{
- char *start = *start_ptr;
+ char *start;
char save_char;
- MR_bool got_int;;
+ MR_bool got_int;
+ start = *start_ptr;
while(MR_isdigit(*start)) {
start++;
}
- if (start == *start_ptr) return MR_FALSE;
+
+ if (start == *start_ptr) {
+ return MR_FALSE;
+ }
+
save_char = *start;
*start = '\0';
got_int = (sscanf(*start_ptr, "%d", num) == 1);
*start = save_char;
+
if (got_int) {
*start_ptr = start;
return MR_TRUE;
@@ -1003,63 +1045,71 @@
}
}
- /*
- ** Remove trailing integer (at the supplied `real_end' of the
- ** string), and return it in the int pointed to by `num'. True
- ** is returned if there is an integer at the end, false if not.
- ** If false is returned, the string will not be cut.
- ** `real_end' is updated with the new end of the string
- ** Requires *str to contain more than just a number; doesn't work
- ** if the trailing integer starts at the first character of str.
- */
+/*
+** Remove trailing integer (at the supplied `real_end' of the string),
+** and return it in the int pointed to by `num'. We return true if there is
+** an integer at the end, and false if there is not. If we return false,
+** the string will not be cut. `real_end' is updated with the new end
+** of the string.
+**
+** Requires *str to contain more than just a number; doesn't work
+** if the trailing integer starts at the first character of str.
+*/
+
static MR_bool
cut_trailing_integer(char *str, char **real_end, int *num)
{
- char *end = *real_end;
+ char *end;
+ end = *real_end;
do {
- if (end == str) return MR_FALSE;
+ if (end == str) {
+ return MR_FALSE;
+ }
end--;
} while (MR_isdigit(*end));
if (sscanf(end + 1, "%d", num) != 1) {
return MR_FALSE;
}
+
*++end = '\0';
*real_end = end;
return MR_TRUE;
}
- /*
- ** Same as cut_trailing_integer, but move end back past
- ** the underscore as well. If cut_trailing_underscore_integer
- ** returns MR_TRUE, the `real_end' will be moved back before the
- ** underscore and the integer. If it returns MR_FALSE, the
- ** `real_end' is unchanged.
- */
+/*
+** Same as cut_trailing_integer, but move end back past the underscore as well.
+** If cut_trailing_underscore_integer returns MR_TRUE, the `real_end' will be
+** moved back before the underscore and the integer. If it returns MR_FALSE,
+** the `real_end' is unchanged.
+*/
+
static MR_bool
-cut_trailing_underscore_integer(char *str, char **real_end,
- int *num)
+cut_trailing_underscore_integer(char *str, char **real_end, int *num)
{
- char *end = *real_end;
+ char *end;
+ end = *real_end;
if (!cut_trailing_integer(str, &end, num)) {
return MR_FALSE;
}
+
if (end == str || *(--end) != '_') {
return MR_FALSE;
}
+
*end = '\0';
*real_end = end;
return MR_TRUE;
}
- /*
- ** Scan for `__' and cut the string at there (replace first
- ** `_' with `\0', return the part of the string after the `__').
- ** Returns MR_TRUE if `__' was found, MR_FALSE otherwise.
- */
+/*
+** Scan for `__' and cut the string at there (replace first `_' with `\0',
+** return the part of the string after the `__').
+** Returns MR_TRUE if `__' was found, MR_FALSE otherwise.
+*/
static MR_bool
cut_at_double_underscore(char **start, char *end)
@@ -1073,15 +1123,17 @@
return MR_TRUE;
}
- /*
- ** Scan for `__' and return a pointer to the first `_'.
- ** Returns MR_TRUE if `__' was found, MR_FALSE otherwise.
- */
+/*
+** Scan for `__' and return a pointer to the first `_'.
+** Returns MR_TRUE if `__' was found, MR_FALSE otherwise.
+*/
+
static MR_bool
find_double_underscore(char **start, char *end)
{
- char *str = *start;
+ char *str;
+ str = *start;
while (*str != '_' || *(str + 1) != '_') {
if (str == end) {
return MR_FALSE;
@@ -1093,22 +1145,22 @@
return MR_TRUE;
}
- /*
- ** The compiler changes all names starting with `f_' so that
- ** they start with `f__' instead, and uses names starting with
- ** `f_' for mangled names which are either descriptions (such
- ** as `f_greater_than' for `>') or sequences of decimal
- ** reprententations of ASCII codes separated by underscores.
- ** If the name starts with `f__', we must change it back to
- ** start with `f_'. Otherwise, if it starts with `f_' we must
- ** convert the mnemonic or list of ASCII codes back into an
- ** identifier.
- */
+/*
+** The compiler changes all names starting with `f_' so that they start with
+** `f__' instead, and uses names starting with `f_' for mangled names
+** which are either descriptions (such as `f_greater_than' for `>')
+** or sequences of decimal reprententations of ASCII codes separated by
+** underscores. If the name starts with `f__', we must change it back to
+** start with `f_'. Otherwise, if it starts with `f_' we must convert
+** the mnemonic or list of ASCII codes back into an identifier.
+*/
static char *
fix_mangled_ascii(char *str, char **real_end)
{
- char *end = *real_end;
+ char *end;
+
+ end = *real_end;
/*
** If it starts with `f__', replace that with `f_'.
@@ -1135,23 +1187,33 @@
*/
if (strncmp(str, "f_", 2) == 0) {
char buf[MAX_SYMBOL_LENGTH];
- char *num = str + 2;
- int count = 0;
+ char *num;
+ int count;
+
+ num = str + 2;
+ count = 0;
while (num < end) {
- char *next_num = num;
+ char *next_num;
+
+ next_num = num;
while (MR_isdigit(*next_num)) {
next_num++;
}
- if (*next_num != '_' && *next_num != '\0')
+
+ if (*next_num != '_' && *next_num != '\0') {
break;
+ }
+
*next_num = '\0';
buf[count++] = atoi(num);
num = next_num + 1;
}
- /* copy anything after the mangled string */
+
+ /* Copy anything after the mangled string. */
while (num < end) {
buf[count++] = *num++;
}
+
buf[count] = '\0';
strcpy(str, buf);
*real_end = str + count;
@@ -1167,8 +1229,8 @@
const char *unmangled_name;
} translations[] = {
/*
- ** Beware: we assume that the unmangled name is always
- ** shorter than the mangled name.
+ ** Beware: we assume that the unmangled name is always shorter
+ ** than the mangled name.
*/
{ "f_not_equal", "\\=" },
{ "f_greater_or_equal", ">=" },
@@ -1193,21 +1255,26 @@
int i;
/*
- ** check for the special cases listed in the table above.
+ ** Check for the special cases listed in the table above.
*/
for (i = 0; i < num_translations; i++) {
- const char *mangled = translations[i].mangled_name;
- size_t mangled_len = strlen(mangled);
- if (strncmp(str, mangled, mangled_len) == 0) {
- const char *unmangled = translations[i].unmangled_name;
- size_t unmangled_len = strlen(unmangled);
- size_t leftover_len = strlen(str) - mangled_len;
+ const char *mangled;
+ size_t mangled_len;
+ mangled = translations[i].mangled_name;
+ mangled_len = strlen(mangled);
+ if (strncmp(str, mangled, mangled_len) == 0) {
+ const char *unmangled;
+ size_t unmangled_len;
+ size_t leftover_len;
+
+ unmangled = translations[i].unmangled_name;
+ unmangled_len = strlen(unmangled);
+ leftover_len = strlen(str) - mangled_len;
assert(unmangled_len <= mangled_len);
strcpy(str, unmangled);
- memmove(str + unmangled_len, str + mangled_len,
- leftover_len + 1);
+ memmove(str + unmangled_len, str + mangled_len, leftover_len + 1);
*real_end = str + unmangled_len + leftover_len;
return MR_TRUE;
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list